season.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?
  2. require 'config.php';
  3. $DONE_FILE = 'season_done';
  4. $RECODE_FILE = '.lastFindRecode';
  5. function seasonExists($series, $season) {
  6. return is_dir(seasonPath($series, $season));
  7. }
  8. function seasonPath($series, $season) {
  9. return seriesPath($series) . '/Season ' . intval($season);
  10. }
  11. # True if the provided season folder is being monitored
  12. function isMonitored($series, $season) {
  13. global $DONE_FILE;
  14. # Season 0 is never monitored
  15. if (!$season) {
  16. return false;
  17. }
  18. # Imaginary series are not monitored
  19. if (!seriesExists($series)) {
  20. return false;
  21. }
  22. # Imaginary seasons are not monitored
  23. if (!seasonExists($series, $season)) {
  24. return false;
  25. }
  26. # Series marked "skip" are not monitored
  27. if (file_exists(seriesPath($series) . '/skip')) {
  28. return false;
  29. }
  30. # Seasons marked done are not monitored
  31. if (file_exists(seasonPath($series, $season) . '/' . $DONE_FILE)) {
  32. return false;
  33. }
  34. # The default behavior is to search
  35. return true;
  36. }
  37. # Find all the season in a provided series folder and determine which are being monitored
  38. function findSeasons($series) {
  39. $retval = array();
  40. $path = seriesPath($series);
  41. $dir = opendir($path);
  42. if ($dir === FALSE) {
  43. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  44. }
  45. while (false !== ($season = readdir($dir))) {
  46. # Skip junk
  47. if (isJunk($season)) {
  48. continue;
  49. }
  50. # We only care about directories
  51. $season_path = $path . '/' . $season;
  52. if (!is_dir($season_path)) {
  53. continue;
  54. }
  55. # Record the season number and search status
  56. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  57. $season_num = $matches[1];
  58. $retval[ $season_num ] = isMonitored($series, $season_num);
  59. }
  60. }
  61. closedir($dir);
  62. # Sort numerically; the directory listing typically returns a lexicographic order
  63. ksort($retval, SORT_NUMERIC);
  64. return $retval;
  65. }
  66. # Save the search status for all seasons in a series
  67. function saveSeasons($series, $data, $series_last, $seasons_last) {
  68. die_if_not_authenticated();
  69. global $DONE_FILE;
  70. # Do nothing if we are or just were in "skip" mode
  71. if ($data['skip'] || $series_last['skip']) {
  72. return;
  73. }
  74. # For each season
  75. $seasons = findSeasons($series);
  76. foreach ($seasons as $season => $status) {
  77. $season_path = seasonPath($series, $season);
  78. $monitored = $data[ 'season_' . $season ];
  79. $monitored_path = $season_path . '/' . $DONE_FILE;
  80. if ($monitored) {
  81. if (file_exists($monitored_path)) {
  82. unlink($monitored_path);
  83. }
  84. } else {
  85. if (!file_exists($monitored_path)) {
  86. touch($monitored_path);
  87. }
  88. }
  89. }
  90. # Force a cache update
  91. clearCache();
  92. }
  93. # Add the specified season folder
  94. function addSeason($series, $season) {
  95. die_if_not_authenticated();
  96. # Cheap validation
  97. $season = intval($season);
  98. # Ensure the series exists
  99. if (!seriesExists($series)) {
  100. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  101. }
  102. # Ensure the season does not exist
  103. $season_path = seasonPath($series, $season);
  104. if (file_exists($season_path)) {
  105. die(htmlspecialchars($series). ': Invalid season: ' . htmlspecialchars($season) . "\n");
  106. }
  107. # Ensure the season number is listed on TVDB
  108. $flags = readFlags($series);
  109. if (!$flags['tvdb-id']) {
  110. die('No TVDB ID for series: ' . htmlspecialchars($series) . "\n");
  111. }
  112. $seasons = getTVDBSeasons($flags['tvdb-id'], $flags['tvdb-lid']);
  113. if (!$seasons[ $season ]) {
  114. die(htmlspecialchars($series) . ': Season not listed in TheTVDB: ' . htmlspecialchars($season) . "\n");
  115. }
  116. # Create the directory
  117. @mkdir($season_path);
  118. # Force a cache update
  119. clearCache();
  120. }
  121. # Remove the specified season folder, if possible (i.e. if empty)
  122. function delSeason($series, $season) {
  123. die_if_not_authenticated();
  124. global $DONE_FILE;
  125. global $RECODE_FILE;
  126. # Ensure the series exists
  127. if (!seriesExists($series)) {
  128. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  129. }
  130. # Ensure the season exists
  131. if (!seasonExists($series, $season)) {
  132. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  133. }
  134. # Drop known files from the season folder
  135. $path = seasonPath($series, $season);
  136. @unlink($path . '/' . $RECODE_FILE);
  137. @unlink($path . '/' . $DONE_FILE);
  138. # Remove the directory (or fail silently)
  139. @rmdir($path);
  140. # Force a cache update
  141. clearCache();
  142. }
  143. ?>