1
0

scan.php 11 KB

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