1
0

scan.php 10 KB

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