scan.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. }
  172. if ($TYPE == 'other') {
  173. die('Unknown file type: ' . $path . ': ' . $NAME . '|' . $EXT . "\n");
  174. }
  175. # Add missing paths
  176. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  177. if (!$select->fetch(PDO::FETCH_ASSOC)) {
  178. if ($DEBUG) {
  179. echo 'Adding: ' . $path . ': ' . $TYPE . "\n";
  180. }
  181. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  182. }
  183. # Grab the DB entry
  184. $check->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  185. $row = $check->fetch(PDO::FETCH_ASSOC);
  186. if (!$row || !is_array($row)) {
  187. die('Unable to fetch entry for: ' . $file . "\n");
  188. }
  189. # Check the file type
  190. if ($row['type'] != $TYPE) {
  191. if ($DEBUG) {
  192. echo 'Updating type for: ' . $file . "\n";
  193. }
  194. $set_type->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  195. }
  196. # Skip everything else for 'ignored' files
  197. if ($TYPE == 'ignored') {
  198. continue;
  199. }
  200. # Grab the local file stats
  201. $stat = @stat($path);
  202. if ($stat === false) {
  203. echo 'Unable to stat: ' . $path . "\n";
  204. continue;
  205. }
  206. # Check the file mtime
  207. if ($row['mtime'] < $stat['mtime']) {
  208. if ($DEBUG) {
  209. echo 'Updating mtime for: ' . $file . "\n";
  210. }
  211. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  212. }
  213. # Check the file size
  214. if ($row['size'] != $stat['size']) {
  215. if ($DEBUG) {
  216. echo 'Updating size for: ' . $file . "\n";
  217. }
  218. $set_size->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':size' => $stat['size']));
  219. }
  220. # Update hashes as needed
  221. if ($TYPE != 'folder') {
  222. $check_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':mtime' => $mtime));
  223. $row = $check_hash->fetch(PDO::FETCH_ASSOC);
  224. if ($row) {
  225. if ($DEBUG) {
  226. echo 'Adding hash: ' . $path . "\n";
  227. }
  228. $hash = md5_file($path, false);
  229. if (strlen($hash) == 32) {
  230. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':hash' => $hash));
  231. } else {
  232. echo 'Unable to hash: ' . $path . "\n";
  233. continue;
  234. }
  235. unset($hash);
  236. }
  237. unset($row);
  238. }
  239. unset($mtime);
  240. unset($NAME);
  241. unset($EXT);
  242. unset($TYPE);
  243. unset($path);
  244. }
  245. unset($FILES);
  246. unset($select);
  247. unset($insert);
  248. unset($check);
  249. unset($set_mtime);
  250. unset($set_size);
  251. unset($check_hash);
  252. unset($set_hash);
  253. # Update priorities
  254. $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE path LIKE :path');
  255. $priority->execute(array(':priority' => 250, ':path' => 'Pictures/%'));
  256. $priority->execute(array(':priority' => 100, ':path' => 'Movies/%'));
  257. $priority->execute(array(':priority' => 50, ':path' => 'iTunes/%'));
  258. $priority->execute(array(':priority' => -50, ':path' => 'Backups/%'));
  259. $priority->execute(array(':priority' => -100, ':path' => 'TV/%'));
  260. unset($priority);
  261. # If remote operations are enabled
  262. if ($REMOTE) {
  263. # Log in to OpenDrive
  264. require_once 'opendrive.php';
  265. $session = login();
  266. # Validate the existence, type and size of all files we think on the remote system
  267. $select = $dbh->prepare('SELECT base, path, type, size FROM files WHERE remote_mtime IS NOT NULL AND path LIKE :subdir');
  268. $clear = $dbh->prepare('UPDATE files SET remote_mtime = NULL, remote_hash = NULL WHERE base = :base AND path = :path');
  269. $select->execute(array(':subdir' => $SUB_DIR . '/%'));
  270. while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
  271. $trigger = false;
  272. if ($DEBUG > 2) {
  273. echo 'Checking remote: ' . $row['path'] . "\n";
  274. }
  275. if ($row['type'] == 'ignored') {
  276. continue;
  277. } else if ($row['type'] == 'folder') {
  278. if (!folderID($session, $row['path'])) {
  279. echo 'Missing remote folder: ' . $row['path'] . "\n";
  280. $trigger = true;
  281. }
  282. } else {
  283. $data = fileInfo($session, $row['path']);
  284. if (!$data) {
  285. echo 'Missing remote file: ' . $row['path'] . "\n";
  286. $trigger = true;
  287. } else if ($data['Size'] != $row['size']) {
  288. echo 'Wrong size remote file: ' . $row['path'] . ' (' . $data['size'] . '/' . $row['Size'] . ")\n";
  289. $trigger = true;
  290. }
  291. unset($data);
  292. }
  293. if ($DELETE && $trigger) {
  294. $clear->execute(array(':base' => $row['base'], ':path' => $row['path']));
  295. }
  296. unset($trigger);
  297. }
  298. unset($select);
  299. unset($clear);
  300. # Logout of OpenDrive
  301. logout($session);
  302. unset($session);
  303. }
  304. # Cleanup
  305. unset($dbh);
  306. ?>