tv.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. # True if the provided season folder is being monitored
  25. function isMonitored($season_path) {
  26. if (file_exists(dirname($season_path) . '/skip')) {
  27. return false;
  28. }
  29. if (file_exists($season_path . '/season_done')) {
  30. return false;
  31. }
  32. $season = 0;
  33. if (preg_match('/^Season\s+(\d+)$/i', basename($season_path), $matches)) {
  34. $season = $matches[1];
  35. }
  36. if (!$season) {
  37. return false;
  38. }
  39. return true;
  40. }
  41. # Find all the season in a provided series folder and determine which are being monitored
  42. function findSeasons($path) {
  43. $retval = array();
  44. # Check for the skip file
  45. $skip = false;
  46. if (file_exists($path . '/skip')) {
  47. $skip = true;
  48. }
  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($show)) {
  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 season numbers and monitored status
  64. if (preg_match('/Season\s+(\d+)/i', $season, $matches)) {
  65. $retval[ $matches[1] ] = isMonitored($season_path);
  66. }
  67. }
  68. closedir($dir);
  69. # Sort numerically; the directory listing typically returns a lexicographic order
  70. ksort($retval, SORT_NUMERIC);
  71. return $retval;
  72. }
  73. # Find all shows under the given path
  74. function allShows($base) {
  75. $retval = array();
  76. # Look for series folders
  77. $tv_dir = opendir($base);
  78. if ($tv_dir === FALSE) {
  79. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  80. }
  81. while (false !== ($show = readdir($tv_dir))) {
  82. # Skip junk
  83. if (isJunk($show)) {
  84. continue;
  85. }
  86. # We only care about directories
  87. $show_path = $base . '/' . $show;
  88. if (!is_dir($show_path)) {
  89. continue;
  90. }
  91. # Record the show title and look for season folders
  92. $retval[ $show ] = findSeasons($show_path);
  93. }
  94. closedir($tv_dir);
  95. return $retval;
  96. }
  97. # Find the *.webloc file, if in, in the provided folder
  98. function findWebloc($path) {
  99. $retval = false;
  100. $dir = opendir($path);
  101. if ($dir === FALSE) {
  102. die('Unable to opendir(): ' . htmlspecialchars($path) . "\n");
  103. }
  104. while (false !== ($name = readdir($dir))) {
  105. # Skip junk
  106. if (isJunk($name)) {
  107. continue;
  108. }
  109. # We only care about *.webloc files
  110. if (!preg_match('/\.webloc$/', $name)) {
  111. continue;
  112. }
  113. # The file must be readable to be useful
  114. $file = $path . '/' . $name;
  115. if (is_readable($file)) {
  116. $retval = $file;
  117. last;
  118. }
  119. }
  120. closedir($dir);
  121. return $retval;
  122. }
  123. # Read and parse the provided *.webloc file
  124. function readWebloc($file) {
  125. global $TVDB_URL;
  126. $retval = array(
  127. 'tvdb-id' => false,
  128. 'tvdb-lid' => false,
  129. 'url' => false,
  130. );
  131. # Read and parse the file
  132. $str = trim(file_get_contents($file));
  133. if (preg_match('/[\?\&]id=(\d+)/', $str, $matches)) {
  134. $retval['tvdb-id'] = $matches[1];
  135. }
  136. if (preg_match('/[\?\&]lid=(\d+)/', $str, $matches)) {
  137. $retval['tvdb-lid'] = $matches[1];
  138. }
  139. # Construct a URL
  140. if ($retval['tvdb-id'] !== false) {
  141. $retval['url'] = $TVDB_URL . '&id=' . $retval['tvdb-id'];
  142. if ($retval['tvdb-lid'] !== false) {
  143. $retval['url'] .= '&lid=' . $retval['tvdb-lid'];
  144. }
  145. }
  146. return $retval;
  147. }
  148. # Read and parse all of the series-level exists, content, and *.webloc files
  149. function readFlags($path) {
  150. global $EXISTS_FILES;
  151. global $CONTENT_FILES;
  152. $flags = array();
  153. # Look for all the exists files
  154. foreach ($EXISTS_FILES as $name) {
  155. $flags[ $name ] = false;
  156. if (file_exists($path . '/' . $name)) {
  157. $flags[ $name ] = true;
  158. }
  159. }
  160. # Read all the content files
  161. foreach ($CONTENT_FILES as $name) {
  162. $flags[ $name ] = false;
  163. $file = $path . '/' . $name;
  164. if (is_readable($file)) {
  165. $flags[ $name ] = trim(file_get_contents($file));
  166. }
  167. }
  168. # Read the TVDB IDs from the *.webloc file
  169. $webloc = findWebloc($path);
  170. if ($webloc !== false) {
  171. $flags = array_merge($flags, readWebloc($webloc));
  172. }
  173. return $flags;
  174. }
  175. function printShow($show) {
  176. global $TV_PATH;
  177. global $EXISTS_FILES;
  178. global $CONTENT_FILES;
  179. # Construct our show path and make sure it's reasonable
  180. $path = $TV_PATH . '/' . $show;
  181. if (!is_dir($path)) {
  182. die('Unknown show: ' . htmlspecialchars($show));
  183. }
  184. # Check the flags
  185. $flags = readFlags($path);
  186. # Check the seasons
  187. $seasons = findSeasons($path);
  188. # Header
  189. echo '<h1>' . htmlspecialchars($show) . '</h1>';
  190. echo '<form action="' . $SERVER['PHP_SELF'] . '" method="post">';
  191. # Exists and content flags
  192. echo '<h2>Search Parameters</h2>';
  193. echo '<p>';
  194. foreach ($EXISTS_FILES as $file) {
  195. echo '<label><input value="1" type="checkbox" name="' . htmlspecialchars($file) . '" ';
  196. if ($flags[ $file ]) {
  197. echo 'checked="checked"';
  198. }
  199. echo '/> ' . htmlspecialchars($file) . '</label><br/>';
  200. }
  201. foreach ($CONTENT_FILES as $file) {
  202. echo '<label><input type="text" name="' . htmlspecialchars($file) . '" ';
  203. if ($flags[ $file ]) {
  204. echo 'value="' . htmlspecialchars($flags[ $file ]) . '"';
  205. }
  206. echo '/> ' . htmlspecialchars($file) . '</label><br/>';
  207. }
  208. echo '</p>';
  209. # TVDB URL
  210. echo '<h2>The TVDB</h2>';
  211. echo '<p><a href="' . htmlspecialchars($flags['url']) . '">' . htmlspecialchars($flags['url']) . '</a></p>';
  212. # Seasons
  213. echo '<h2>Seasons</h2>';
  214. echo '<p>';
  215. foreach ($seasons as $season => $monitored) {
  216. echo '<label>Season ' . htmlspecialchars($season) . ' ';
  217. echo '<input type="checkbox" value="1" name="season_' . htmlspecialchars($season) . '" ';
  218. if ($monitored) {
  219. echo 'checked="checked"';
  220. }
  221. echo '/></label><br/>';
  222. }
  223. echo '</p>';
  224. # Footer
  225. echo '</form>';
  226. }
  227. # Print a DL of all shows and note the available and monitored seasons
  228. function printAllShows() {
  229. global $TV_PATH;
  230. global $MEDIA_PATH;
  231. $shows = allShows($TV_PATH);
  232. echo "<dl>\n";
  233. foreach ($shows as $show => $seasons) {
  234. echo '<dt><a href="?show=' . urlencode($show) . '">' . htmlspecialchars($show) . "</a></dt>\n";
  235. foreach ($seasons as $season => $monitored) {
  236. echo '<dd>Season ' . htmlspecialchars($season);
  237. if ($monitored) {
  238. echo ' (monitored)';
  239. }
  240. echo "</dd>\n";
  241. }
  242. }
  243. echo "</dl>\n";
  244. }
  245. #=========================================================================================
  246. # Send our headers early
  247. header('Content-type: text/html; charset=utf-8');
  248. # Did the user request a specific show?
  249. $show = false;
  250. if (isset($_REQUEST['show'])) {
  251. # This is not a great filter, but it should make the string safe for use as a quoted path
  252. $show = preg_replace('/[\0\n\r]/', '', $_REQUEST['show']);
  253. $show = basename($show);
  254. }
  255. #=========================================================================================
  256. # Generic XHTML 1.1 header
  257. print <<<ENDOLA
  258. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  259. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  260. <html xmlns="http://www.w3.org/1999/xhtml">
  261. <head>
  262. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  263. <title>UberZach TV</title>
  264. </head>
  265. <body>
  266. ENDOLA;
  267. if ($show === false) {
  268. printAllShows();
  269. } else {
  270. printShow($show);
  271. }
  272. # Generic XHTML 1.1 footer
  273. print <<<ENDOLA
  274. </body>
  275. </html>
  276. ENDOLA;
  277. ?>