season.php 4.3 KB

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