tv.php 8.1 KB

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