From d7c9baf791788fd99131ffe2ee0eca7b3d37f924 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 1 Jun 2022 13:32:55 +0200 Subject: [PATCH] Update permission WIP, proxied download link, and fix css --- fuel/app/bootstrap.php | 1 + fuel/app/classes/controller/movie.php | 60 +++++++++++++++ fuel/app/classes/controller/rest/install.php | 1 + fuel/app/classes/controller/rest/movie.php | 76 ++----------------- fuel/app/classes/controller/rest/settings.php | 5 +- fuel/app/classes/file.php | 47 ++++++++++++ fuel/app/classes/model/movie.php | 3 +- fuel/app/classes/model/permission.php | 30 ++++---- fuel/app/config/routes.php | 22 +++--- fuel/app/views/episode/index.php | 4 +- fuel/app/views/movie/index.php | 8 +- fuel/app/views/player/index.php | 2 +- .../views/settings/libraries/permissions.php | 6 +- fuel/app/views/tvshow/index.php | 4 +- 14 files changed, 161 insertions(+), 108 deletions(-) create mode 100644 fuel/app/classes/file.php diff --git a/fuel/app/bootstrap.php b/fuel/app/bootstrap.php index 32b19d5..b1f0d41 100755 --- a/fuel/app/bootstrap.php +++ b/fuel/app/bootstrap.php @@ -6,6 +6,7 @@ require COREPATH.'bootstrap.php'; // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Database_Query_Builder_Insert' => APPPATH.'classes/database/query/builder/insert.php', + 'File' => APPPATH.'classes/file.php', )); // Register the autoloader diff --git a/fuel/app/classes/controller/movie.php b/fuel/app/classes/controller/movie.php index 41d8b5c..47d85ab 100644 --- a/fuel/app/classes/controller/movie.php +++ b/fuel/app/classes/controller/movie.php @@ -1,5 +1,7 @@ template->body = $body; } + + public function action_download() + { + try { + $movie_id = $this->param('movie_id'); + + if (!$movie_id) + throw new FuelException('No movie id'); + + $movie = Model_Movie::find_by_pk($movie_id); + + if (!$movie) + throw new FuelException('No movie found'); + + if (Model_Permission::isGranted('RIGHT_DOWNLOAD_DISABLED', $movie->getLibrary())) + throw new FuelException('You dont have the permission to download in this library!'); + + $url = $movie->getDownloadLink(); + + $filename = ''; + $size = 0; + + if(isset($movie->getMetaData()['Media'][0])) { + $filename = $movie->getMetaData()['Media'][0]['Part']['@attributes']['file']; + $size = $movie->getMetaData()['Media'][0]['Part']['@attributes']['size']; + } else { + $filename = $movie->getMetaData()['Media']['Part']['@attributes']['file']; + $size = $movie->getMetaData()['Media']['Part']['@attributes']['size']; + } + + $filename = explode('/', $filename); + $filename = $filename[count($filename) - 1]; + + ini_set('memory_limit', -1); + ini_set('max_execution_time', -1); + ini_set('max_input_time', -1); + + header('Cache-control: no-cache, no-store, max-age=0, must-revalidate'); + header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); + header('Pragma: no-cache'); + header('Content-Description: File Transfer'); + header('Content-Transfer-Encoding: binary'); + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename="'.$filename.'"'); + header('Content-Length: '.$size); + + // @TODO: add it to admin panel and permission + $speed = 2000; + + File::readChunked($url, $speed); + + exit(200); + } catch (Exception $exception) { + return new Response($exception->getMessage(),500); + } + } + + } \ No newline at end of file diff --git a/fuel/app/classes/controller/rest/install.php b/fuel/app/classes/controller/rest/install.php index 485cc8f..cb6b8f4 100755 --- a/fuel/app/classes/controller/rest/install.php +++ b/fuel/app/classes/controller/rest/install.php @@ -494,6 +494,7 @@ class Controller_Rest_Install extends Controller_Rest DB::insert('permission')->set(['id' => Str::random('uuid'), 'name' => 'RIGHT_MAX_QUALITY', 'parameters' => 1])->execute(); DB::insert('permission')->set(['id' => Str::random('uuid'), 'name' => 'RIGHT_MAX_CONCURRENT_STREAM', 'parameters' => 1])->execute(); DB::insert('permission')->set(['id' => Str::random('uuid'), 'name' => 'RIGHT_MAX_DOWNLOAD', 'parameters' => 1])->execute(); + DB::insert('permission')->set(['id' => Str::random('uuid'), 'name' => 'RIGHT_MAX_DOWNLOAD_SPEED', 'parameters' => 1])->execute(); $logs .= 'Permission created!'."\r\n"; diff --git a/fuel/app/classes/controller/rest/movie.php b/fuel/app/classes/controller/rest/movie.php index 9d38cdc..8c84c51 100644 --- a/fuel/app/classes/controller/rest/movie.php +++ b/fuel/app/classes/controller/rest/movie.php @@ -22,22 +22,20 @@ class Controller_Rest_Movie extends Controller_Rest /** @var Model_Movie $movie */ $movie = Model_Movie::find_by_pk($movie_id); - if (!$movie) { + if (!$movie) throw new FuelException('No movie found'); - } - if (!Model_Permission::isGranted('RIGHT_WATCH_DISABLED', $movie->getLibrary())) { + + if (Model_Permission::isGranted('RIGHT_WATCH_DISABLED', $movie->getLibrary())) throw new FuelException('You dont have the permission to watch in this library!'); - } + $user_settings = Model_User_Settings::find_one_by('user_id', Session::get('user')->id); - if ($movie->type !== 'movie') { + if ($movie->type !== 'movie') $episodes = $movie->getSeason()->getEpisodes(); - } - else { + else $episodes = [$movie]; - } $view = View::forge('player/index'); @@ -81,66 +79,4 @@ class Controller_Rest_Movie extends Controller_Rest return $this->response($exception->getMessage(), 500); } } - - - public function get_download() { - try { - - $movie_id = Input::get('movie_id'); - - if (!$movie_id) - throw new FuelException('No movie id'); - - /** @var Model_Movie $movie */ - $movie = Model_Movie::find_by_pk($movie_id); - - if (!$movie) - throw new FuelException('No movie found'); - - if ( !Model_Permission::isGranted('RIGHT_DOWNLOAD_DISABLED', $movie->getLibrary()) ) - throw new FuelException('You dont have the permission to watch in this library!'); - - $url = $movie->getDownloadLink(); - - $filename = ''; - $size = 0; - - if(isset($this->metadata['Media'][0])) { - $filename = $movie->getMetaData()['Media'][0]['Part']['@attributes']['file']; - $size = $movie->getMetaData()['Media'][0]['Part']['@attributes']['size']; - } else { - $filename = $movie->getMetaData()['Media']['Part']['@attributes']['file']; - $size = $movie->getMetaData()['Media']['Part']['@attributes']['size']; - } - - $filename = explode('/', $filename); - $filename = $filename[count($filename) - 1]; - - ini_set('memory_limit', -1); - ini_set('max_execution_time', -1); - ini_set('max_input_time', -1); - - $this->headers = array ( - 'Cache-Control' => 'no-cache, no-store, max-age=0, must-revalidate', - 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', - 'Pragma' => 'no-cache', - "Content-Description" => "File Transfer", - "Content-Transfer-Encoding" => "binary", - 'Content-Type' => 'application/octet-stream', - 'Content-Disposition' => 'inline; attachment; filename="'.$filename.'"', - 'Content-Length' => $size - ); - - if(!File::exists(APPPATH.'tmp/'.$filename)) { - $file = file_get_contents($url); - File::create(APPPATH . 'tmp/', $filename, $file); - } - - $file_info = File::file_info(APPPATH.'tmp/'.$filename); - - File::download(APPPATH.'tmp/'.$filename, $filename, $file_info['mimetype'], null, true); - } catch (Exception $exception) { - return $this->response($exception->getMessage(), 500); - } - } } \ No newline at end of file diff --git a/fuel/app/classes/controller/rest/settings.php b/fuel/app/classes/controller/rest/settings.php index c79dbdd..b0b9b1a 100644 --- a/fuel/app/classes/controller/rest/settings.php +++ b/fuel/app/classes/controller/rest/settings.php @@ -26,8 +26,7 @@ class Controller_Rest_Settings extends Controller_Rest $https = Input::post('https') === 'true' ? true : false; //@TODO CHECK AND REMOVE HTTP AND HTTPS - - $curl = Request::forge(($https ? 'https' : 'http') . '://' . $url . ($port ? ':' . $port : '') . '/?X-Plex-Token=' . $token, 'curl'); + $curl = Request::forge(($https ? 'https' : 'http') . '://' . $url . (!empty($port) ? ':' . $port : '') . '/?X-Plex-Token=' . $token, 'curl'); if($https) { $curl->set_options([ @@ -46,7 +45,7 @@ class Controller_Rest_Settings extends Controller_Rest 'user_id' => Session::get('user')->id, 'https' => $https, 'url' => $url, - 'port' => $port, + 'port' => !empty($port) ? $port : null, 'token' => $token, 'lastcheck' => time() ]); diff --git a/fuel/app/classes/file.php b/fuel/app/classes/file.php new file mode 100644 index 0000000..59ae7c8 --- /dev/null +++ b/fuel/app/classes/file.php @@ -0,0 +1,47 @@ +download = Cache::get($this->id . '.download'); - if ($this->download) - return $this->download; + return $this->download; } catch (CacheNotFoundException $e) { if(!$this->_server) $this->getServer(); diff --git a/fuel/app/classes/model/permission.php b/fuel/app/classes/model/permission.php index 48f2412..3d61159 100644 --- a/fuel/app/classes/model/permission.php +++ b/fuel/app/classes/model/permission.php @@ -8,6 +8,7 @@ class Model_Permission extends Model_Overwrite * RIGHT_WATCH_DISABLED * RIGHT_DOWNLOAD_DISABLED * RIGHT_MAX_DOWNLOAD + * RIGHT_MAX_DOWNLOAD_SPEED * RIGHT_MAX_WATCH * RIGHT_MAX_QUALITY * RIGHT_MAX_CONCURRENT_STREAM @@ -24,9 +25,9 @@ class Model_Permission extends Model_Overwrite /** * @param string $permission * @param Model_Movie $movie - * @return bool + * @return bool|int */ - public static function isGranted($permission, Model_Library $library = null, $data = null ) + public static function isGranted($permission, Model_Library $library = null, $data = null) { $user = Session::get('user'); @@ -45,25 +46,17 @@ class Model_Permission extends Model_Overwrite ]); if ($permission->name === 'RIGHT_WATCH_DISABLED') { - if($library_permission !== null && $library_permission->value === 1) + if($library_permission === null) return false; - else if($library_permission !== null && $library_permission->value === 0) - return true; - else if($library_permission === null) - return true; else - return false; + return true; } if ($permission->name === 'RIGHT_DOWNLOAD_DISABLED') { - if($library_permission !== null && $library_permission->value === 1) + if($library_permission === null) return false; - else if($library_permission !== null && $library_permission->value === 0) - return true; - else if($library_permission === null) - return true; else - return false; + return true; } if ($permission->name === 'RIGHT_MAX_DOWNLOAD') { @@ -75,6 +68,15 @@ class Model_Permission extends Model_Overwrite return true; } + if ($permission->name === 'RIGHT_MAX_DOWNLOAD_SPEED') { + /** @TODO IF (ENABLED) + * RETURN VALUE + * ELSE + * RETURN 0 + */ + return true; + } + if ($permission->name === 'RIGHT_MAX_WATCH') { /** @TODO IF (NUMBER_WATCH <= MAX_WATCH) // in last 24h * RETURN TRUE diff --git a/fuel/app/config/routes.php b/fuel/app/config/routes.php index b28656b..4873861 100755 --- a/fuel/app/config/routes.php +++ b/fuel/app/config/routes.php @@ -1,14 +1,16 @@ 'index', // The default route - '_404_' => 'error/404', // The main 404 route + '_root_' => 'index', // The default route + '_404_' => 'error/404', // The main 404 route - 'home(/:server_id)?'=> 'home/index', - 'movie/list' => 'movie/list', - 'library/:library_id' => 'library/index', - 'tvshow/:tvshow_id' => 'tvshow/index', - 'season/:season_id' => 'season/index', - 'episode/:episode_id' => 'episode/index', - 'movie/:movie_id' => 'movie/index', - 'settings/libraries/premissions/:library_id' => 'settings/libraries/permissions' + 'home(/:server_id)?' => 'home/index', + 'movie/list' => 'movie/list', + 'library/:library_id' => 'library/index', + 'tvshow/:tvshow_id' => 'tvshow/index', + 'season/:season_id' => 'season/index', + 'episode/:movie_id/download' => 'movie/download', + 'episode/:episode_id' => 'episode/index', + 'movie/:movie_id/download' => 'movie/download', + 'movie/:movie_id' => 'movie/index', + 'settings/libraries/premissions/:library_id' => 'settings/libraries/permissions' ); diff --git a/fuel/app/views/episode/index.php b/fuel/app/views/episode/index.php index abc93a2..90ce326 100644 --- a/fuel/app/views/episode/index.php +++ b/fuel/app/views/episode/index.php @@ -17,9 +17,11 @@
diff --git a/fuel/app/views/movie/index.php b/fuel/app/views/movie/index.php index ab430b7..986693d 100644 --- a/fuel/app/views/movie/index.php +++ b/fuel/app/views/movie/index.php @@ -5,8 +5,8 @@
- getLibrary())) : ?> - + getLibrary())) : ?> + @@ -298,9 +298,9 @@
-
+
-
+
getMetadata()['Role'] as $role) : ?>
diff --git a/fuel/app/views/player/index.php b/fuel/app/views/player/index.php index 311ef16..57aed47 100644 --- a/fuel/app/views/player/index.php +++ b/fuel/app/views/player/index.php @@ -39,7 +39,7 @@
-
+
getServer()->name; ?> > name; ?>
-
By default a user can do everything without restriction. Use -1 to unlimited and 0 to block.
+
+ By default a user can do everything without restriction. Use -1 to unlimited and 0 to block. +
+ Disable the permission, is like putting in unlimited mode. +
diff --git a/fuel/app/views/tvshow/index.php b/fuel/app/views/tvshow/index.php index a37a866..d735182 100644 --- a/fuel/app/views/tvshow/index.php +++ b/fuel/app/views/tvshow/index.php @@ -204,9 +204,9 @@
-
+
-
+
getMetadata()['Role'])) : ?> getMetadata()['Role'] as $role) : ?>