series.php 5.3 KB

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