series.php 4.5 KB

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