tv.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?
  2. # Config
  3. $MEDIA_PATH = '/mnt/media';
  4. $TV_PATH = $MEDIA_PATH . '/TV';
  5. $EXISTS_FILES = array('no_quality_checks', 'more_number_formats', 'skip');
  6. $CONTENT_FILES = array('must_match', 'search_name');
  7. $TVDB_URL = 'http://thetvdb.com/?tab=series';
  8. # True if the input path is junk -- self-links, OS X noise, etc.
  9. function isJunk($path) {
  10. $path = basename($path);
  11. # Ignore certain fixed paths
  12. foreach (array('.', '..', '.DS_Store') as $value) {
  13. if ($path == $value) {
  14. return true;
  15. }
  16. }
  17. # Ignore ._ paths
  18. if (preg_match('/^\.\_/', $path)) {
  19. return true;
  20. }
  21. # Otherwise the path is good
  22. return false;
  23. }
  24. function isMonitored($season_path) {
  25. if (file_exists(dirname($season_path) . '/skip')) {
  26. return false;
  27. }
  28. if (file_exists($season_path . '/season_done')) {
  29. return false;
  30. }
  31. $season = 0;
  32. if (preg_match('/^Season\s+(\d+)$/i', basename($season_path), $matches)) {
  33. $season = $matches[1];
  34. }
  35. if (!$season) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. function findSeasons($path) {
  41. $retval = array();
  42. # Check for the skip file
  43. $skip = false;
  44. if (file_exists($path . '/skip')) {
  45. $skip = true;
  46. }
  47. $dir = opendir($path);
  48. if ($dir === FALSE) {
  49. die('Unable to opendir(): ' . $path . "\n");
  50. }
  51. while (false !== ($season = readdir($dir))) {
  52. # Skip junk
  53. if (isJunk($show)) {
  54. continue;
  55. }
  56. # We only care about directories
  57. $season_path = $path . '/' . $season;
  58. if (!is_dir($season_path)) {
  59. continue;
  60. }
  61. # Record season numbers and monitored status
  62. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  63. $retval[ $matches[1] ] = isMonitored($season_path);
  64. }
  65. }
  66. closedir($dir);
  67. return $retval;
  68. }
  69. # Find all shows under the given path
  70. function allShows($base) {
  71. $retval = array();
  72. # Look for series folders
  73. $tv_dir = opendir($base);
  74. if ($tv_dir === FALSE) {
  75. die('Unable to opendir(): ' . $path . "\n");
  76. }
  77. while (false !== ($show = readdir($tv_dir))) {
  78. # Skip junk
  79. if (isJunk($show)) {
  80. continue;
  81. }
  82. # We only care about directories
  83. $show_path = $base . '/' . $show;
  84. if (!is_dir($show_path)) {
  85. continue;
  86. }
  87. # Record the show title and look for season folders
  88. $retval[ $show ] = findSeasons($show_path);
  89. }
  90. closedir($tv_dir);
  91. return $retval;
  92. }
  93. # Print a DL of all shows and note the available and monitored seasons
  94. function printAllShows() {
  95. global $TV_PATH;
  96. global $MEDIA_PATH;
  97. $shows = allShows($TV_PATH);
  98. echo "<dl>\n";
  99. foreach ($shows as $show => $seasons) {
  100. echo '<dt><a href="?show=' . urlencode($show) . '">' . htmlspecialchars($show) . "</a></dt>\n";
  101. foreach ($seasons as $season => $monitored) {
  102. echo '<dd>Season ' . htmlspecialchars($season);
  103. if ($monitored) {
  104. echo ' (monitored)';
  105. }
  106. echo "</dd>\n";
  107. }
  108. }
  109. echo "</dl>\n";
  110. }
  111. function findWebloc($path) {
  112. $retval = false;
  113. $dir = opendir($path);
  114. if ($dir === FALSE) {
  115. die('Unable to opendir(): ' . $path . "\n");
  116. }
  117. while (false !== ($name = readdir($dir))) {
  118. # Skip junk
  119. if (isJunk($name)) {
  120. continue;
  121. }
  122. # We only care about *.webloc files
  123. if (!preg_match('/\.webloc$/', $name)) {
  124. continue;
  125. }
  126. # The file must be readable to be useful
  127. $file = $path . '/' . $name;
  128. if (is_readable($file)) {
  129. $retval = $file;
  130. last;
  131. }
  132. }
  133. closedir($dir);
  134. return $retval;
  135. }
  136. function readWebloc($file) {
  137. global $TVDB_URL;
  138. $retval = array(
  139. 'tvdb-id' => false,
  140. 'tvdb-lid' => false,
  141. 'url' => false,
  142. );
  143. # Read and parse the file
  144. $str = trim(file_get_contents($file));
  145. if (preg_match('/[\?\&]id=(\d+)/', $str, $matches)) {
  146. $retval['tvdb-id'] = $matches[1];
  147. }
  148. if (preg_match('/[\?\&]lid=(\d+)/', $str, $matches)) {
  149. $retval['tvdb-lid'] = $matches[1];
  150. }
  151. # Construct a URL
  152. if ($retval['tvdb-id'] !== false) {
  153. $retval['url'] = $TVDB_URL . '&id=' . $retval['tvdb-id'];
  154. if ($retval['tvdb-lid'] !== false) {
  155. $retval['url'] .= '&lid=' . $retval['tvdb-lid'];
  156. }
  157. }
  158. return $retval;
  159. }
  160. function readFlags($path) {
  161. global $EXISTS_FILES;
  162. global $CONTENT_FILES;
  163. $flags = array();
  164. # Look for all the exists files
  165. foreach ($EXISTS_FILES as $name) {
  166. $flags[ $name ] = false;
  167. if (file_exists($path . '/' . $name)) {
  168. $flags[ $name ] = true;
  169. }
  170. }
  171. # Read all the content files
  172. foreach ($CONTENT_FILES as $name) {
  173. $flags[ $name ] = false;
  174. $file = $path . '/' . $name;
  175. if (is_readable($file)) {
  176. $flags[ $name ] = trim(file_get_contents($file));
  177. }
  178. }
  179. # Read the TVDB IDs from the *.webloc file
  180. $webloc = findWebloc($path);
  181. if ($webloc !== false) {
  182. $flags = array_merge($flags, readWebloc($webloc));
  183. }
  184. return $flags;
  185. }
  186. function printShow($show) {
  187. global $TV_PATH;
  188. # Construct our show path and make sure it's reasonable
  189. $path = $TV_PATH . '/' . $show;
  190. if (!is_dir($path)) {
  191. die('Unknown show: ' . $show);
  192. }
  193. # Check the flags
  194. $flags = readFlags($path);
  195. # Check the seasons
  196. $seasons = findSeasons($path);
  197. # Print
  198. echo '<pre>';
  199. echo $show . "\n";
  200. print_r($flags);
  201. echo "\nSeasons\n";
  202. print_r($seasons);
  203. echo '</pre>';
  204. }
  205. #=========================================================================================
  206. # Send our headers early
  207. header('Content-type: text/html; charset=utf-8');
  208. # Did the user request a specific show?
  209. $show = false;
  210. if (isset($_REQUEST['show'])) {
  211. # This is not a great filter, but it should make the string safe for use as a quoted path
  212. $show = preg_replace('/[\0\n\r]/', '', $_REQUEST['show']);
  213. $show = basename($show);
  214. }
  215. #=========================================================================================
  216. # Generic XHTML 1.1 header
  217. print <<<ENDOLA
  218. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  219. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  220. <html xmlns="http://www.w3.org/1999/xhtml">
  221. <head>
  222. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  223. <title>UberZach TV</title>
  224. </head>
  225. <body>
  226. ENDOLA;
  227. if ($show === false) {
  228. printAllShows();
  229. } else {
  230. printShow($show);
  231. }
  232. # Generic XHTML 1.1 footer
  233. print <<<ENDOLA
  234. </body>
  235. </html>
  236. ENDOLA;
  237. ?>