series.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?
  2. require 'config.php';
  3. function seriesExists($series) {
  4. return is_dir(seriesPath($series));
  5. }
  6. function seriesPath($series) {
  7. global $TV_PATH;
  8. return $TV_PATH . '/' . $series;
  9. }
  10. # Find all series and seasons under a given path
  11. function allSeriesSeasons($base, $use_cache = true) {
  12. global $CACHE_AGE;
  13. global $CACHE_FILE;
  14. $retval = array();
  15. # Use the cache file if it exists, is fresh and contains at least 1 series
  16. if ($use_cache && file_exists($CACHE_FILE)) {
  17. if (filemtime($CACHE_FILE) > time() - $CACHE_AGE) {
  18. $retval = @unserialize(@file_get_contents($CACHE_FILE));
  19. if (is_array($retval) && count($retval) > 0) {
  20. return $retval;
  21. }
  22. }
  23. }
  24. # Read live data from the disk
  25. $all_series = allSeries($base);
  26. foreach ($all_series as $series) {
  27. $retval[ $series ] = findSeasons($series);
  28. }
  29. # Always update the cache if we read from disk
  30. $temp = @tempnam(dirname($CACHE_FILE), 'tv_web.cache');
  31. if ($temp) {
  32. file_put_contents($temp, serialize($retval));
  33. rename($temp, $CACHE_FILE);
  34. }
  35. return $retval;
  36. }
  37. # Find all series under the given path
  38. function allSeries($base) {
  39. $retval = array();
  40. # Look for series folders
  41. $tv_dir = opendir($base);
  42. if ($tv_dir === FALSE) {
  43. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  44. }
  45. while (false !== ($series = readdir($tv_dir))) {
  46. # Skip junk
  47. if (isJunk($series)) {
  48. continue;
  49. }
  50. # Ensure the series is reasonable
  51. if (!seriesExists($series)) {
  52. continue;
  53. }
  54. $retval[] = $series;
  55. }
  56. closedir($tv_dir);
  57. return $retval;
  58. }
  59. # Read and parse all of the series-level exists, content, and *.webloc files
  60. function readFlags($series) {
  61. global $EXISTS_FILES;
  62. global $CONTENT_FILES;
  63. $flags = array();
  64. # Ensure the series exists
  65. if (!seriesExists($series)) {
  66. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  67. }
  68. $path = seriesPath($series);
  69. # Find the most recent mtime in the series directory
  70. $flags['mtime'] = filemtime($path);
  71. $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
  72. foreach($objects as $name => $object) {
  73. # Skip junk
  74. if (isJunk($name)) {
  75. continue;
  76. }
  77. # Track the most recent mtime
  78. $mtime = filemtime($name);
  79. if ($mtime > $flags['mtime']) {
  80. $flags['mtime'] = $mtime;
  81. }
  82. }
  83. # Look for all the exists files
  84. foreach ($EXISTS_FILES as $name) {
  85. $flags[ $name ] = false;
  86. if (file_exists($path . '/' . $name)) {
  87. $flags[ $name ] = true;
  88. }
  89. }
  90. # Read all the content files
  91. foreach ($CONTENT_FILES as $name) {
  92. $flags[ $name ] = false;
  93. $file = $path . '/' . $name;
  94. if (is_readable($file)) {
  95. $flags[ $name ] = trim(file_get_contents($file));
  96. }
  97. }
  98. # Read the TVDB IDs from the *.webloc file
  99. $webloc = findWebloc($path);
  100. if ($webloc !== false) {
  101. $flags = array_merge($flags, readWebloc($webloc));
  102. }
  103. return $flags;
  104. }
  105. # Parse and save all of the serires-level exists and content files
  106. function saveFlags($series, $data, $series_last, $seasons_last) {
  107. die_if_not_authenticated();
  108. global $EXISTS_FILES;
  109. global $CONTENT_FILES;
  110. # Ensure the series exists
  111. if (!seriesExists($series)) {
  112. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  113. }
  114. $series_path = seriesPath($series);
  115. # Special handling for "skip"
  116. {
  117. $path = $series_path . '/skip';
  118. if ($data['skip']) {
  119. if (!file_exists($path)) {
  120. touch($path);
  121. }
  122. } else {
  123. if (file_exists($path)) {
  124. unlink($path);
  125. }
  126. }
  127. }
  128. # Ignore everything else if we are now or were just in "skip" mode
  129. if ($series_last['skip'] || $data['skip']) {
  130. return;
  131. }
  132. # Exists files
  133. foreach ($EXISTS_FILES as $file) {
  134. $path = $series_path . '/' . $file;
  135. if ($data[ $file ]) {
  136. if (!file_exists($path)) {
  137. touch($path);
  138. }
  139. } else {
  140. if (file_exists($path)) {
  141. unlink($path);
  142. }
  143. }
  144. }
  145. # Content files
  146. foreach ($CONTENT_FILES as $file) {
  147. $path = $series_path . '/' . $file;
  148. if ($data[ $file ]) {
  149. file_put_contents($path, $data[ $file ]);
  150. } else {
  151. if (file_exists($path)) {
  152. unlink($path);
  153. }
  154. }
  155. }
  156. # Force a cache update
  157. clearCache();
  158. }
  159. # Add a series as identified by TVDB URL or ID
  160. function addSeries($str) {
  161. die_if_not_authenticated();
  162. global $TVDB_LANG_ID;
  163. $id = false;
  164. $lid = false;
  165. $series = false;
  166. # Accept URLs or raw IDs
  167. if (preg_match('/^\d+$/', $str)) {
  168. $id = intval($str);
  169. } else {
  170. $result = parseTVDBURL($str);
  171. $id = $result['tvdb-id'];
  172. $lid = $result['tvdb-lid'];
  173. }
  174. # Ensure we got something useful
  175. if (!$id) {
  176. die('Invalid ID or URL: ' . htmlspecialchars($str) . "\n");
  177. }
  178. if (!$lid) {
  179. $lid = $TVDB_LANG_ID;
  180. }
  181. # Find the show name
  182. $series = getTVDBTitle($id, $lid);
  183. # Ensure the title is reasonable
  184. if (!$series) {
  185. die('No such TVDB series: ' . htmlspecialchars($id) . "\n");
  186. }
  187. # Clean the title for filesystem use
  188. $series = cleanSeries($series);
  189. if (seriesExists($series)) {
  190. die('Series already exists: ' . htmlspecialchars($series) . "\n");
  191. }
  192. # Return the series name or FALSE on failure
  193. $retval = false;
  194. # If all is well, create the folder and webloc file
  195. $series_path = seriesPath($series);
  196. if (@mkdir($series_path)) {
  197. writeWebloc(TVDBURL($id, $lid), $series_path . '/' . $series . '.webloc');
  198. addSeason($series, 1);
  199. $retval = $series;
  200. }
  201. return $retval;
  202. }
  203. ?>