series.php 5.5 KB

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