series.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. # Look for all the exists files
  52. foreach ($EXISTS_FILES as $name) {
  53. $flags[ $name ] = false;
  54. if (file_exists($path . '/' . $name)) {
  55. $flags[ $name ] = true;
  56. }
  57. }
  58. # Read all the content files
  59. foreach ($CONTENT_FILES as $name) {
  60. $flags[ $name ] = false;
  61. $file = $path . '/' . $name;
  62. if (is_readable($file)) {
  63. $flags[ $name ] = trim(file_get_contents($file));
  64. }
  65. }
  66. # Read the TVDB IDs from the *.webloc file
  67. $webloc = findWebloc($path);
  68. if ($webloc !== false) {
  69. $flags = array_merge($flags, readWebloc($webloc));
  70. }
  71. return $flags;
  72. }
  73. # Parse and save all of the serires-level exists and content files
  74. function saveFlags($series, $data, $series_last, $seasons_last) {
  75. global $EXISTS_FILES;
  76. global $CONTENT_FILES;
  77. # Ensure the series exists
  78. if (!seriesExists($series)) {
  79. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  80. }
  81. $series_path = seriesPath($series);
  82. # Special handling for "skip"
  83. {
  84. $path = $series_path . '/skip';
  85. if ($data['skip']) {
  86. if (!file_exists($path)) {
  87. touch($path);
  88. }
  89. } else {
  90. if (file_exists($path)) {
  91. unlink($path);
  92. }
  93. }
  94. }
  95. # Ignore everything else if we are now or were just in "skip" mode
  96. if ($series_last['skip'] || $data['skip']) {
  97. return;
  98. }
  99. # Exists files
  100. foreach ($EXISTS_FILES as $file) {
  101. $path = $series_path . '/' . $file;
  102. if ($data[ $file ]) {
  103. if (!file_exists($path)) {
  104. touch($path);
  105. }
  106. } else {
  107. if (file_exists($path)) {
  108. unlink($path);
  109. }
  110. }
  111. }
  112. # Content files
  113. foreach ($CONTENT_FILES as $file) {
  114. $path = $series_path . '/' . $file;
  115. if ($data[ $file ]) {
  116. file_put_contents($path, $data[ $file ]);
  117. } else {
  118. if (file_exists($path)) {
  119. unlink($path);
  120. }
  121. }
  122. }
  123. }
  124. # Add a series as identified by TVDB URL or ID
  125. function addSeries($str) {
  126. global $TVDB_LANG_ID;
  127. $id = false;
  128. $lid = false;
  129. $series = false;
  130. # Accept URLs or raw IDs
  131. if (preg_match('/^\d+$/', $str)) {
  132. $id = intval($str);
  133. } else {
  134. $result = parseTVDBURL($str);
  135. $id = $result['tvdb-id'];
  136. $lid = $result['tvdb-lid'];
  137. }
  138. # Ensure we got something useful
  139. if (!$id) {
  140. die('Invalid ID or URL: ' . htmlspecialchars($str) . "\n");
  141. }
  142. if (!$lid) {
  143. $lid = $TVDB_LANG_ID;
  144. }
  145. # Find the show name
  146. $series = getTVDBTitle($id, $lid);
  147. # Ensure the title is reasonable
  148. if (!$series) {
  149. die('No such TVDB series: ' . htmlspecialchars($id) . "\n");
  150. }
  151. # Clean the title for filesystem use
  152. $series = cleanSeries($series);
  153. if (seriesExists($series)) {
  154. die('Series already exists: ' . htmlspecialchars($series) . "\n");
  155. }
  156. # Return the series name or FALSE on failure
  157. $retval = false;
  158. # If all is well, create the folder and webloc file
  159. $series_path = seriesPath($series);
  160. if (@mkdir($series_path)) {
  161. writeWebloc(TVDBURL($id, $lid), $series_path . '/' . $series . '.webloc');
  162. addSeason($series, 1);
  163. $retval = $series;
  164. }
  165. return $retval;
  166. }
  167. ?>