season.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. # Find all the season in a provided series folder and determine which are being monitored
  35. function findSeasons($series) {
  36. $retval = array();
  37. $path = seriesPath($series);
  38. $dir = opendir($path);
  39. if ($dir === FALSE) {
  40. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  41. }
  42. while (false !== ($season = readdir($dir))) {
  43. # Skip junk
  44. if (isJunk($season)) {
  45. continue;
  46. }
  47. # We only care about directories
  48. $season_path = $path . '/' . $season;
  49. if (!is_dir($season_path)) {
  50. continue;
  51. }
  52. # Record the season number and search status
  53. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  54. $season_num = $matches[1];
  55. $retval[ $season_num ] = isMonitored($series, $season_num);
  56. }
  57. }
  58. closedir($dir);
  59. # Sort numerically; the directory listing typically returns a lexicographic order
  60. ksort($retval, SORT_NUMERIC);
  61. return $retval;
  62. }
  63. # Save the search status for all seasons in a series
  64. function saveSeasons($series, $data, $series_last, $seasons_last) {
  65. die_if_not_authenticated();
  66. # Do nothing if we are or just were in "skip" mode
  67. if ($data['skip'] || $series_last['skip']) {
  68. return;
  69. }
  70. # For each season
  71. $seasons = findSeasons($series);
  72. foreach ($seasons as $season => $status) {
  73. $season_path = seasonPath($series, $season);
  74. $monitored = $data[ 'season_' . $season ];
  75. $monitored_path = $season_path . '/season_done';
  76. if ($monitored) {
  77. if (file_exists($monitored_path)) {
  78. unlink($monitored_path);
  79. }
  80. } else {
  81. if (!file_exists($monitored_path)) {
  82. touch($monitored_path);
  83. }
  84. }
  85. }
  86. # Force a cache update
  87. clearCache();
  88. }
  89. # Add the specified season folder
  90. function addSeason($series, $season) {
  91. die_if_not_authenticated();
  92. # Cheap validation
  93. $season = intval($season);
  94. # Ensure the series exists
  95. if (!seriesExists($series)) {
  96. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  97. }
  98. # Ensure the season does not exist
  99. $season_path = seasonPath($series, $season);
  100. if (file_exists($season_path)) {
  101. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  102. }
  103. # Ensure the season number is listed on TVDB
  104. $flags = readFlags($series);
  105. if (!$flags['tvdb-id']) {
  106. die('No TVDB ID for series: ' . htmlspecialchars($series) . "\n");
  107. }
  108. $seasons = getTVDBSeasons($flags['tvdb-id'], $flags['tvdb-lid']);
  109. if (!$seasons[ $season ]) {
  110. die('Season not listed in TheTVDB: ' . htmlspecialchars($season) . "\n");
  111. }
  112. # Create the directory
  113. @mkdir($season_path);
  114. # Force a cache update
  115. clearCache();
  116. }
  117. # Remove the specified season folder, if possible (i.e. if empty)
  118. function delSeason($series, $season) {
  119. die_if_not_authenticated();
  120. # Ensure the series exists
  121. if (!seriesExists($series)) {
  122. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  123. }
  124. # Ensure the season exists
  125. if (!seasonExists($series, $season)) {
  126. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  127. }
  128. # Remove the directory (or fail silently)
  129. @rmdir(seasonPath($series, $season));
  130. # Force a cache update
  131. clearCache();
  132. }
  133. ?>