season.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?
  2. require 'config.php';
  3. # True if the provided season folder is being monitored
  4. function isMonitored($season_path) {
  5. if (file_exists(dirname($season_path) . '/skip')) {
  6. return false;
  7. }
  8. if (file_exists($season_path . '/season_done')) {
  9. return false;
  10. }
  11. $season = 0;
  12. if (preg_match('/^Season\s+(\d+)$/i', basename($season_path), $matches)) {
  13. $season = $matches[1];
  14. }
  15. if (!$season) {
  16. return false;
  17. }
  18. return true;
  19. }
  20. # Get the season search status, including the URL (if any) for the provided path
  21. function seasonSearch($season_path) {
  22. $retval = isMonitored($season_path);
  23. if ($retval) {
  24. $file = $season_path . '/url';
  25. if (is_readable($file)) {
  26. $retval = trim(file_get_contents($file));
  27. }
  28. }
  29. return $retval;
  30. }
  31. # Find all the season in a provided series folder and determine which are being monitored
  32. function findSeasons($series) {
  33. $retval = array();
  34. # Check for the skip file
  35. $skip = false;
  36. if (file_exists($path . '/skip')) {
  37. $skip = true;
  38. }
  39. $path = seriesPath($series);
  40. $dir = opendir($path);
  41. if ($dir === FALSE) {
  42. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  43. }
  44. while (false !== ($season = readdir($dir))) {
  45. # Skip junk
  46. if (isJunk($season)) {
  47. continue;
  48. }
  49. # We only care about directories
  50. $season_path = $path . '/' . $season;
  51. if (!is_dir($season_path)) {
  52. continue;
  53. }
  54. # Record the season number and search status
  55. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  56. $retval[ $matches[1] ] = seasonSearch($season_path);
  57. }
  58. }
  59. closedir($dir);
  60. # Sort numerically; the directory listing typically returns a lexicographic order
  61. ksort($retval, SORT_NUMERIC);
  62. return $retval;
  63. }
  64. # Save the search status for all seasons in a series
  65. function saveSeasons($series, $data, $series_last, $seasons_last) {
  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 = seriesPath($series) . '/Season ' . $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. $url = $data[ 'url_' . $season ];
  86. $url_path = $season_path . '/url';
  87. if ($url) {
  88. if (isMonitored($season_path)) {
  89. file_put_contents($url_path, $url . "\n");
  90. }
  91. } else {
  92. if ($seasons_last[ $season ] && isMonitored($season_path)) {
  93. if (file_exists($url_path)) {
  94. unlink($url_path);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. # Add the specified season folder
  101. function addSeason($series, $season) {
  102. # Ensure the series exists
  103. if (!seriesExists($series)) {
  104. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  105. }
  106. # Ensure the season does not exist
  107. $season_path = seriesPath($series) . '/Season ' . intval($season);
  108. if (file_exists($season_path)) {
  109. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  110. }
  111. # Create the directory
  112. mkdir($season_path);
  113. }
  114. # Remove the specified season folder, if possible (i.e. if empty)
  115. function delSeason($series, $season) {
  116. # Ensure the series exists
  117. if (!seriesExists($series)) {
  118. die('Invalid series: ' . htmlspecialchars($series) . "\n");
  119. }
  120. # Ensure the season exists
  121. $season_path = seriesPath($series) . '/Season ' . intval($season);
  122. if (!file_exists($season_path)) {
  123. die('Invalid season: ' . htmlspecialchars($season) . "\n");
  124. }
  125. # Remove the directory (or fail silently)
  126. @rmdir($season_path);
  127. }
  128. ?>