scan.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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');
  63. $missing->execute();
  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. # Construct the absolute path
  119. $path = $BASE_LOCAL . '/' . $file;
  120. # Pick a file type
  121. $parts = pathinfo($path);
  122. $EXT = strtolower($parts['extension']);
  123. $NAME = strtolower($parts['filename']);
  124. unset($parts);
  125. $TYPE = 'other';
  126. if (preg_match('/\/\.git(\/.*)?$/', $path)) {
  127. $TYPE = 'ignored';
  128. } else if (preg_match('/\/\._/', $path)) {
  129. $TYPE = 'ignored';
  130. } else if ($EXT == 'lastfindrecode' || $NAME == 'placeholder' || $EXT == 'plexignore') {
  131. $TYPE = 'ignored';
  132. } else if ($EXT == 'tmp' || $EXT == 'gitignore' || $EXT == 'ds_store' ||
  133. preg_match('/^\.smbdelete/', $NAME) || preg_match('/\.mkv\.\w+$/', $file)) {
  134. $TYPE = 'ignored';
  135. } else if (preg_match('/^iTunes\/Album Artwork\/Cache/', $file)) {
  136. $TYPE = 'ignored';
  137. } else if (is_dir($path)) {
  138. $TYPE = 'folder';
  139. if ($EXT == '' && $NAME == 'cmd') {
  140. $TYPE = 'ignored';
  141. }
  142. } else if ($EXT == 'm4v' || $EXT == 'mkv' || $EXT == 'mp4' || $EXT == 'mov' ||
  143. $EXT == 'vob' || $EXT == 'iso' || $EXT == 'avi') {
  144. $TYPE = 'video';
  145. } else if ($EXT == 'mp3' || $EXT == 'aac' || $EXT == 'm4a' || $EXT == 'm4b' ||
  146. $EXT == 'm4p' || $EXT == 'wav') {
  147. $TYPE = 'audio';
  148. } else if ($EXT == 'epub' || $EXT == 'pdf') {
  149. $TYPE = 'book';
  150. } else if ($EXT == 'jpg' || $EXT == 'png') {
  151. $TYPE = 'image';
  152. } else if ($EXT == 'gz' || $EXT == 'zip' || $EXT == 'xz') {
  153. $TYPE = 'archive';
  154. } else if ($EXT == 'itc' || $EXT == 'itl' || $EXT == 'strings' || $EXT == 'itdb' ||
  155. $EXT == 'plist' || $EXT == 'ipa' || $EXT == 'ini') {
  156. $TYPE = 'database';
  157. } else if ($EXT == 'clip' || $EXT == 'riff' || $EXT == 'nfo') {
  158. $TYPE = 'metadata';
  159. } else if ($EXT == 'webloc' || $NAME == 'skip' || $NAME == 'season_done' ||
  160. $NAME == 'more_number_formats' || $NAME == 'no_quality_checks' ||
  161. $NAME == 'filler' || $NAME == 'search_name' || $EXT == 'disabled' ||
  162. $NAME == 'must_match' || $EXT == 'fakeshow' || $EXT == 'filler' ||
  163. $NAME == 'excludes' || $NAME == 'search_by_date' || $EXT == 'twopart') {
  164. $TYPE = 'metadata';
  165. } else if ($EXT == 'fake' || $EXT == 'txt' || $EXT == 'json' ||
  166. $EXT == 'bup' || $EXT == 'ifo') {
  167. $TYPE = 'metadata';
  168. }
  169. if ($TYPE == 'other') {
  170. die('Unknown file type: ' . $path . ': ' . $NAME . '|' . $EXT . "\n");
  171. }
  172. # Add missing paths
  173. $select->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  174. if (!$select->fetch(PDO::FETCH_ASSOC)) {
  175. if ($DEBUG) {
  176. echo 'Adding: ' . $path . ': ' . $TYPE . "\n";
  177. }
  178. $insert->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  179. }
  180. # Grab the DB entry
  181. $check->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  182. $row = $check->fetch(PDO::FETCH_ASSOC);
  183. if (!$row || !is_array($row)) {
  184. die('Unable to fetch entry for: ' . $file . "\n");
  185. }
  186. # Check the file type
  187. if ($row['type'] != $TYPE) {
  188. if ($DEBUG) {
  189. echo 'Updating type for: ' . $file . "\n";
  190. }
  191. $set_type->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':type' => $TYPE));
  192. }
  193. # Skip everything else for 'ignored' files
  194. if ($TYPE == 'ignored') {
  195. continue;
  196. }
  197. # Check the file mtime
  198. $mtime = trim(shell_exec('stat -c "%Y" ' . escapeshellarg($path)));
  199. if ($row['mtime'] < $mtime) {
  200. if ($DEBUG) {
  201. echo 'Updating mtime for: ' . $file . "\n";
  202. }
  203. $set_mtime->execute(array(':base' => $BASE_LOCAL, ':path' => $file));
  204. }
  205. # Check the file size
  206. $size = trim(shell_exec('stat -c "%s" ' . escapeshellarg($path)));
  207. if ($row['size'] != $size) {
  208. if ($DEBUG) {
  209. echo 'Updating size for: ' . $file . "\n";
  210. }
  211. $set_size->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':size' => $size));
  212. }
  213. unset($size);
  214. # Update hashes as needed
  215. if ($TYPE != 'folder') {
  216. $check_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':mtime' => $mtime));
  217. $row = $check_hash->fetch(PDO::FETCH_ASSOC);
  218. if ($row) {
  219. if ($DEBUG) {
  220. echo 'Adding hash: ' . $path . "\n";
  221. }
  222. $hash = trim(shell_exec('md5sum ' . escapeshellarg($path) . ' | cut -d " " -f 1'));
  223. if (strlen($hash) == 32) {
  224. $set_hash->execute(array(':base' => $BASE_LOCAL, ':path' => $file, ':hash' => $hash));
  225. } else {
  226. die('Invalid hash (' . $hash . ') for file: ' . $path . "\n");
  227. }
  228. unset($hash);
  229. }
  230. unset($row);
  231. }
  232. unset($mtime);
  233. unset($NAME);
  234. unset($EXT);
  235. unset($TYPE);
  236. unset($path);
  237. }
  238. unset($FILES);
  239. unset($select);
  240. unset($insert);
  241. unset($check);
  242. unset($set_mtime);
  243. unset($set_size);
  244. unset($check_hash);
  245. unset($set_hash);
  246. # Update priorities
  247. $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE path LIKE :path');
  248. $priority->execute(array(':priority' => 100, ':path' => 'Movies/%'));
  249. $priority->execute(array(':priority' => 50, ':path' => 'iTunes/%'));
  250. $priority->execute(array(':priority' => -50, ':path' => 'Backups/%'));
  251. $priority->execute(array(':priority' => -100, ':path' => 'TV/%'));
  252. unset($priority);
  253. # If remote operations are enabled
  254. if ($REMOTE) {
  255. # Log in to OpenDrive
  256. require_once 'opendrive.php';
  257. $session = login();
  258. # Validate the existence, type and size of all files we think on the remote system
  259. $select = $dbh->prepare('SELECT base, path, type, size FROM files WHERE remote_mtime IS NOT NULL AND path LIKE :subdir');
  260. $clear = $dbh->prepare('UPDATE files SET remote_mtime = NULL, remote_hash = NULL WHERE base = :base AND path = :path');
  261. $select->execute(array(':subdir' => $SUB_DIR . '/%'));
  262. while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
  263. $trigger = false;
  264. if ($DEBUG > 2) {
  265. echo 'Checking remote: ' . $row['path'] . "\n";
  266. }
  267. if ($row['type'] == 'ignored') {
  268. continue;
  269. } else if ($row['type'] == 'folder') {
  270. if (!folderID($session, $row['path'])) {
  271. echo 'Missing remote folder: ' . $row['path'] . "\n";
  272. $trigger = true;
  273. }
  274. } else {
  275. $data = fileInfo($session, $row['path']);
  276. if (!$data) {
  277. echo 'Missing remote file: ' . $row['path'] . "\n";
  278. $trigger = true;
  279. } else if ($data['Size'] != $row['size']) {
  280. echo 'Wrong size remote file: ' . $row['path'] . ' (' . $data['size'] . '/' . $row['Size'] . ")\n";
  281. $trigger = true;
  282. }
  283. unset($data);
  284. }
  285. if ($DELETE && $trigger) {
  286. $clear->execute(array(':base' => $row['base'], ':path' => $row['path']));
  287. }
  288. unset($trigger);
  289. }
  290. unset($select);
  291. unset($clear);
  292. # Logout of OpenDrive
  293. logout($session);
  294. unset($session);
  295. }
  296. # Cleanup
  297. unset($dbh);
  298. ?>