1
0

scan.php 11 KB

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