scan.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #!/usr/local/bin/php
  2. <?php
  3. # Debug
  4. $DEBUG = false;
  5. if ($_ENV['DEBUG']) {
  6. $DEBUG = true;
  7. }
  8. # Includes
  9. require_once 'opendrive.php';
  10. # Command-line parameters
  11. global $argc;
  12. global $argv;
  13. $SUB_DIR = '';
  14. if (($argc > 1) && (strlen($argv[1]) > 0)) {
  15. $SUB_DIR = trim($argv[1]);
  16. }
  17. # Wait DELAY_DAYS before considering a file
  18. $DELAY_DAYS = 30;
  19. if (isset($_ENV['DELAY_DAYS'])) {
  20. $DELAY_DAYS = $_ENV['DELAY_DAYS'];
  21. }
  22. # Allow local-only scanning
  23. $REMOTE = true;
  24. if (isset($_ENV['REMOTE'])) {
  25. if (!$_ENV['REMOTE']) {
  26. $REMOTE = false;
  27. }
  28. }
  29. # Delete only if specified
  30. $DELETE = false;
  31. if ($_ENV['DELETE']) {
  32. $DELETE = true;
  33. }
  34. # Build our local base path
  35. $VIDEO_DIR=$_ENV['HOME'] . '/bin/video';
  36. if (isset($_ENV['VIDEO_DIR'])) {
  37. $VIDEO_DIR = $_ENV['VIDEO_DIR'];
  38. }
  39. $BASE_LOCAL='';
  40. if (isset($_ENV['BASE_LOCAL'])) {
  41. $BASE_LOCAL = $_ENV['BASE_LOCAL'];
  42. } else {
  43. $BASE_LOCAL = trim(shell_exec($VIDEO_DIR . '/mediaPath'));
  44. }
  45. # Allow usage with absolute local paths
  46. if (substr($SUB_DIR, 0, 1) == '/') {
  47. $SUB_DIR = preg_replace('/^' . preg_quote($BASE_LOCAL, '/') . '\//', '', $SUB_DIR);
  48. }
  49. # Usage checks
  50. if (strlen($SUB_DIR) < 1 || $DELAY_DAYS < 1) {
  51. die('Usage: ' . $argv[0] . " sub_directory\n");
  52. }
  53. # Sanity checks
  54. $LOCAL = $BASE_LOCAL . '/' . $SUB_DIR;
  55. if (!file_exists($LOCAL) || !is_dir($LOCAL)) {
  56. die('Invalid local directory: ' . $LOCAL . "\n");
  57. }
  58. # Open the DB connection
  59. $dbh = dbOpen();
  60. # Look for files in the DB that no longer exist or have changed types
  61. $delete = $dbh->prepare('DELETE FROM files WHERE base = :base AND path = :path');
  62. $missing = $dbh->prepare('SELECT base, path, type FROM files WHERE path LIKE :subdir');
  63. $missing->execute(array(':subdir' => $SUB_DIR . '/%'));
  64. while ($row = $missing->fetch(PDO::FETCH_ASSOC)) {
  65. $trigger = false;
  66. $file = $row['base'] . '/' . $row['path'];
  67. if ($row['type'] == 'folder') {
  68. # Ensure the path exists and is a folder
  69. if (!is_dir($file)) {
  70. echo 'Missing folder: ' . $file . "\n";
  71. $trigger = true;
  72. }
  73. } else if ($row['type'] == 'ignored') {
  74. # Silently delete missing ignored files of any type
  75. if (!file_exists($file)) {
  76. if ($DEBUG) {
  77. echo 'Missing ignored file: ' . $file . "\n";
  78. }
  79. $trigger = true;
  80. }
  81. } else {
  82. # Ensure the path exists and is a regular file
  83. if (!is_file($file)) {
  84. echo 'Missing file: ' . $file . "\n";
  85. $trigger = true;
  86. }
  87. }
  88. # Delete if global DELETE is enabled
  89. if ($DELETE && $trigger) {
  90. $delete->execute(array(':base' => $row['base'], ':path' => $row['path']));
  91. }
  92. unset($trigger);
  93. unset($file);
  94. }
  95. unset($delete);
  96. # Grab the file list -- limit files by mtime, but include all directories
  97. $FIND=tempnam(sys_get_temp_dir(), 'scanLocal-find');
  98. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  99. ' -type f -mtime +' . escapeshellarg($DELAY_DAYS) . ' > ' . escapeshellarg($FIND));
  100. exec('cd ' . escapeshellarg($BASE_LOCAL) . ' && find ' . escapeshellarg($SUB_DIR) .
  101. ' -type d >> ' . escapeshellarg($FIND));
  102. # Sort and injest
  103. exec('cat ' . escapeshellarg($FIND) . ' | sort ', $FILES);
  104. unlink($FIND);
  105. unset($FIND);
  106. # Loop through all the files we found
  107. $select = $dbh->prepare('SELECT base, path, type FROM files WHERE base = :base AND path = :path');
  108. $insert = $dbh->prepare('INSERT INTO files (base, path, type, mtime) VALUES (:base, :path, :type, now())');
  109. $check = $dbh->prepare('SELECT type, size, EXTRACT(EPOCH FROM mtime) AS mtime FROM files WHERE base = :base AND path = :path');
  110. $set_mtime = $dbh->prepare('UPDATE files SET mtime = now() WHERE base = :base AND path = :path');
  111. $set_size = $dbh->prepare('UPDATE files SET size = :size WHERE base = :base AND path = :path');
  112. $set_type = $dbh->prepare('UPDATE files SET type = :type WHERE base = :base AND path = :path');
  113. $check_hash = $dbh->prepare('SELECT hash, EXTRACT(EPOCH FROM hash_time) AS hash_time ' .
  114. 'FROM files WHERE base = :base AND path = :path AND ' .
  115. '(hash_time IS NULL OR EXTRACT(EPOCH FROM hash_time) < :mtime)');
  116. $set_hash = $dbh->prepare('UPDATE files SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
  117. foreach ($FILES as $file) {
  118. if ($DEBUG) {
  119. echo 'Scanning: ' . $file . "\n";
  120. }
  121. # Construct the absolute path
  122. $path = $BASE_LOCAL . '/' . $file;
  123. # Pick a file type
  124. $parts = pathinfo($path);
  125. $EXT = strtolower($parts['extension']);
  126. $NAME = strtolower($parts['filename']);
  127. unset($parts);
  128. $TYPE = 'other';
  129. if (preg_match('/\/\.git(\/.*)?$/', $path)) {
  130. $TYPE = 'ignored';
  131. } else if (preg_match('/\/\._/', $path)) {
  132. $TYPE = 'ignored';
  133. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  134. $TYPE = 'ignored';
  135. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || $EXT == 'ds_store' ||
  136. preg_match('/^\.smbdelete/', $NAME) || preg_match('/\.mkv\.\w+$/', $file)) {
  137. $TYPE = 'ignored';
  138. } else if (preg_match('/^iTunes\/Album Artwork\/Cache/', $file)) {
  139. $TYPE = 'ignored';
  140. } else if (is_dir($path)) {
  141. $TYPE = 'folder';
  142. if ($EXT == '' && $NAME == 'cmd') {
  143. $TYPE = 'ignored';
  144. }
  145. } else if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  146. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  147. $TYPE = 'video';
  148. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  149. $EXT == 'm4p' || $EXT == 'wav') {
  150. $TYPE = 'audio';
  151. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  152. $TYPE = 'book';
  153. } else if ($EXT == 'jpg' || $EXT == 'png') {
  154. $TYPE = 'image';
  155. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  156. $TYPE = 'archive';
  157. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  158. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  159. $TYPE = 'database';
  160. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  161. $TYPE = 'metadata';
  162. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  163. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  164. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  165. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  166. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  167. $TYPE = 'metadata';
  168. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  169. $EXT == 'bup' || $EXT == 'ifo') {
  170. $TYPE = 'metadata';
  171. } else if ($EXT == '' && preg_match('/\.sparsebundle\/bands\/\w+$/i', $file)) {
  172. $TYPE = 'disk';
  173. }
  174. if ($TYPE == 'other') {
  175. die('Unknown file type: ' . $path . ': ' . $NAME . '|' . $EXT . "\n");
  176. }
  177. # Add missing paths
  178. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  179. if (!$select->fetch(PDO::FETCH_ASSOC)) {
  180. if ($DEBUG) {
  181. echo 'Adding: ' . $path . ': ' . $TYPE . "\n";
  182. }
  183. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  184. }
  185. # Grab the DB entry
  186. $check->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  187. $row = $check->fetch(PDO::FETCH_ASSOC);
  188. if (!$row || !is_array($row)) {
  189. die('Unable to fetch entry for: ' . $file . "\n");
  190. }
  191. # Check the file type
  192. if ($row['type'] != $TYPE) {
  193. if ($DEBUG) {
  194. echo 'Updating type for: ' . $file . "\n";
  195. }
  196. $set_type->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  197. }
  198. # Skip everything else for 'ignored' files
  199. if ($TYPE == 'ignored') {
  200. continue;
  201. }
  202. # Grab the local file stats
  203. $stat = @stat($path);
  204. if ($stat === false) {
  205. echo 'Unable to stat: ' . $path . "\n";
  206. continue;
  207. }
  208. # Check the file mtime
  209. if ($row['mtime'] < $stat['mtime']) {
  210. if ($DEBUG) {
  211. echo 'Updating mtime for: ' . $file . "\n";
  212. }
  213. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  214. }
  215. # Check the file size
  216. if ($row['size'] != $stat['size']) {
  217. if ($DEBUG) {
  218. echo 'Updating size for: ' . $file . "\n";
  219. }
  220. $set_size->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':size' => $stat['size']));
  221. }
  222. # Update hashes as needed
  223. if ($TYPE != 'folder') {
  224. $check_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':mtime' => $mtime));
  225. $row = $check_hash->fetch(PDO::FETCH_ASSOC);
  226. if ($row) {
  227. if ($DEBUG) {
  228. echo 'Adding hash: ' . $path . "\n";
  229. }
  230. $hash = md5_file($path, false);
  231. if (strlen($hash) == 32) {
  232. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':hash' => $hash));
  233. } else {
  234. echo 'Unable to hash: ' . $path . "\n";
  235. continue;
  236. }
  237. unset($hash);
  238. }
  239. unset($row);
  240. }
  241. unset($mtime);
  242. unset($NAME);
  243. unset($EXT);
  244. unset($TYPE);
  245. unset($path);
  246. }
  247. unset($FILES);
  248. unset($select);
  249. unset($insert);
  250. unset($check);
  251. unset($set_mtime);
  252. unset($set_size);
  253. unset($check_hash);
  254. unset($set_hash);
  255. # Update priorities
  256. $paths = $dbh->prepare('SELECT path, priority FROM paths');
  257. $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE path LIKE :path');
  258. $paths->execute();
  259. while ($row = $paths->fetch(PDO::FETCH_ASSOC)) {
  260. $priority->execute(array(':priority' => $row['priority'], ':path' => $row['path'] . '/%'));
  261. if ($DEBUG) {
  262. echo 'Updated ' . $priority->rowCount() . ' rows from ' . $row['path'] . ' with priority ' . $row['priority'] . "\n";
  263. }
  264. }
  265. unset($priority);
  266. unset($paths);
  267. # If remote operations are enabled
  268. if ($REMOTE) {
  269. # Log in to OpenDrive
  270. require_once 'opendrive.php';
  271. $session = login();
  272. # Validate the existence, type and size of all files we think on the remote system
  273. $select = $dbh->prepare('SELECT base, path, type, size FROM files WHERE remote_mtime IS NOT NULL AND path LIKE :subdir');
  274. $clear = $dbh->prepare('UPDATE files SET remote_mtime = NULL, remote_hash = NULL WHERE base = :base AND path = :path');
  275. $select->execute(array(':subdir' => $SUB_DIR . '/%'));
  276. while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
  277. $trigger = false;
  278. if ($DEBUG > 2) {
  279. echo 'Checking remote: ' . $row['path'] . "\n";
  280. }
  281. if ($row['type'] == 'ignored') {
  282. continue;
  283. } else if ($row['type'] == 'folder') {
  284. if (!folderID($session, $row['path'])) {
  285. echo 'Missing remote folder: ' . $row['path'] . "\n";
  286. $trigger = true;
  287. }
  288. } else {
  289. $data = fileInfo($session, $row['path']);
  290. if (!$data) {
  291. echo 'Missing remote file: ' . $row['path'] . "\n";
  292. $trigger = true;
  293. } else if ($data['Size'] != $row['size']) {
  294. echo 'Wrong size remote file: ' . $row['path'] . ' (' . $data['size'] . '/' . $row['Size'] . ")\n";
  295. $trigger = true;
  296. }
  297. unset($data);
  298. }
  299. if ($DELETE && $trigger) {
  300. $clear->execute(array(':base' => $row['base'], ':path' => $row['path']));
  301. }
  302. unset($trigger);
  303. }
  304. unset($select);
  305. unset($clear);
  306. # Logout of OpenDrive
  307. logout($session);
  308. unset($session);
  309. }
  310. # Cleanup
  311. unset($dbh);
  312. ?>