| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- <?
- require 'config.php';
- function seriesExists($series) {
- return is_dir(seriesPath($series));
- }
- function seriesPath($series) {
- global $TV_PATH;
- return $TV_PATH . '/' . $series;
- }
- # Find all series and seasons under a given path
- function allSeriesSeasons($base, $use_cache = true) {
- global $CACHE_AGE;
- global $CACHE_FILE;
- $retval = array();
- # Look for a cache file (stale or otherwise)
- if ($use_cache) {
- $stale = false;
- $file = $CACHE_FILE;
- if (!file_exists($file)) {
- $file = staleCacheName($CACHE_FILE);
- $stale = true;
- }
- if (file_exists($file)) {
- if (filemtime($file) + $CACHE_AGE < time()) {
- $stale = true;
- }
- $retval = @unserialize(@file_get_contents($file));
- }
- if (is_array($retval) && count($retval) > 0) {
- if ($stale) {
- $retval['***Stale data - Click to force a refresh'] = array();
- }
- return $retval;
- }
- }
- # Read live data from the disk
- $all_series = allSeries($base);
- foreach ($all_series as $series) {
- $retval[ $series ] = findSeasons($series);
- }
- # Always update the cache if we read from disk
- $temp = @tempnam(dirname($CACHE_FILE), 'tv_web.cache');
- if ($temp) {
- if (dirname($temp) != dirname($CACHE_FILE)) {
- unlink($temp);
- $temp = false;
- }
- }
- if ($temp) {
- file_put_contents($temp, serialize($retval));
- rename($temp, $CACHE_FILE);
- }
- return $retval;
- }
- # Find all series under the given path
- function allSeries($base) {
- $retval = array();
- # Look for series folders
- $tv_dir = opendir($base);
- if ($tv_dir === FALSE) {
- die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
- }
- while (false !== ($series = readdir($tv_dir))) {
- # Skip junk
- if (isJunk($series)) {
- continue;
- }
- # Ensure the series is reasonable
- if (!seriesExists($series)) {
- continue;
- }
- $retval[] = $series;
- }
- closedir($tv_dir);
-
- return $retval;
- }
- # Read and parse all of the series-level exists, content, and *.webloc files
- function readFlags($series) {
- global $EXISTS_FILES;
- global $CONTENT_FILES;
- $flags = array();
- # Ensure the series exists
- if (!seriesExists($series)) {
- die('Invalid series: ' . htmlspecialchars($series) . "\n");
- }
- $path = seriesPath($series);
- # Find the most recent mtime in the series directory
- $flags['mtime'] = filemtime($path);
- $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
- try {
- foreach($objects as $name => $object) {
- # Skip junk
- if (isJunk($name)) {
- continue;
- }
- # Track the most recent mtime
- $mtime = filemtime($name);
- if ($mtime > $flags['mtime']) {
- $flags['mtime'] = $mtime;
- }
- }
- } catch (UnexpectedValueException $e) {
- }
- # Look for all the exists files
- foreach ($EXISTS_FILES as $name) {
- $flags[ $name ] = false;
- if (file_exists($path . '/' . $name)) {
- $flags[ $name ] = true;
- }
- }
- # Read all the content files
- foreach ($CONTENT_FILES as $name) {
- $flags[ $name ] = false;
- $file = $path . '/' . $name;
- if (is_readable($file)) {
- $flags[ $name ] = trim(file_get_contents($file));
- }
- }
- # Read the TVDB IDs from the *.webloc file
- $webloc = findWebloc($path);
- if ($webloc !== false) {
- $flags = array_merge($flags, readWebloc($webloc));
- }
- return $flags;
- }
- # Parse and save all of the serires-level exists and content files
- function saveFlags($series, $data, $series_last, $seasons_last) {
- die_if_not_authenticated();
- global $EXISTS_FILES;
- global $CONTENT_FILES;
- # Ensure the series exists
- if (!seriesExists($series)) {
- die('Invalid series: ' . htmlspecialchars($series) . "\n");
- }
- $series_path = seriesPath($series);
- # Special handling for "skip"
- {
- $path = $series_path . '/skip';
- if ($data['skip']) {
- if (!file_exists($path)) {
- touch($path);
- }
- } else {
- if (file_exists($path)) {
- unlink($path);
- }
- }
- }
- # Ignore everything else if we are now or were just in "skip" mode
- if ($series_last['skip'] || $data['skip']) {
- return;
- }
-
- # Exists files
- foreach ($EXISTS_FILES as $file) {
- $path = $series_path . '/' . $file;
- if ($data[ $file ]) {
- if (!file_exists($path)) {
- touch($path);
- }
- } else {
- if (file_exists($path)) {
- unlink($path);
- }
- }
- }
- # Content files
- foreach ($CONTENT_FILES as $file) {
- $path = $series_path . '/' . $file;
- $data[ $file ] = trim($data[ $file ]);
- if ($data[ $file ]) {
- file_put_contents($path, $data[ $file ] . "\n");
- } else {
- if (file_exists($path)) {
- unlink($path);
- }
- }
- }
- # Force a cache update
- clearCache();
- }
- # Add a series as identified by TVDB URL or ID
- function addSeries($str) {
- die_if_not_authenticated();
- global $TVDB_LANG_ID;
- $id = false;
- $lid = false;
- $series = false;
- # Accept URLs or raw IDs
- if (preg_match('/^\d+$/', $str)) {
- $id = intval($str);
- } else {
- $result = parseTVDBURL($str);
- $id = $result['tvdb-id'];
- $lid = $result['tvdb-lid'];
- }
- # Ensure we got something useful
- if (!$id) {
- die('Invalid ID or URL: ' . htmlspecialchars($str) . "\n");
- }
- if (!$lid) {
- $lid = $TVDB_LANG_ID;
- }
- # Find the show name
- $series = getTVDBTitle($id, $lid);
- # Ensure the title is reasonable
- if (!$series) {
- die('No such TVDB series: ' . htmlspecialchars($id) . "\n");
- }
- # Clean the title for filesystem use
- $series = cleanSeries($series);
- if (seriesExists($series)) {
- die('Series already exists: ' . htmlspecialchars($series) . "\n");
- }
- # Return the series name or FALSE on failure
- $retval = false;
- # If all is well, create the folder and webloc file
- $series_path = seriesPath($series);
- if (@mkdir($series_path)) {
- writeWebloc(TVDBURL($id, $lid), $series_path . '/' . $series . '.webloc');
- addSeason($series, 1);
- $retval = $series;
- }
- return $retval;
- }
- ?>
|