| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- #!/usr/local/bin/php
- <?php
- # Debug
- $DEBUG = false;
- if ($_ENV['DEBUG']) {
- $DEBUG = true;
- }
- # Includes
- require_once 'opendrive.php';
- # Command-line parameters
- global $argc;
- global $argv;
- $LIMIT_PATH = '';
- if (($argc > 1) && (strlen($argv[1]) > 0)) {
- $LIMIT_PATH = trim($argv[1]);
- }
- # Allow local-only scanning
- $REMOTE = true;
- if (isset($_ENV['REMOTE'])) {
- if (!$_ENV['REMOTE']) {
- $REMOTE = false;
- }
- }
- # Delete only if specified
- $DELETE = false;
- if ($_ENV['DELETE']) {
- $DELETE = true;
- }
- # Open the DB connection
- $dbh = dbOpen();
- # Find our scan paths
- $paths = NULL;
- $priority = $dbh->prepare('UPDATE files SET priority = :priority WHERE base = :base AND path LIKE :path');
- $scan = $dbh->prepare('UPDATE paths SET last_scan = now() WHERE base = :base AND path = :path');
- if (!$LIMIT_PATH) {
- $paths = $dbh->prepare('SELECT base, path, priority, min_age FROM paths WHERE last_scan < now() - scan_age');
- } else {
- $paths = $dbh->prepare('SELECT base, path, priority, min_age FROM paths WHERE last_scan < now() - scan_age' .
- ' AND path LIKE :path');
- $paths->execute(array(':path' => $LIMIT_PATH));
- }
- while ($pathsRow = $paths->fetch(PDO::FETCH_ASSOC)) {
- # Grab our globals
- $BASE = $pathsRow['base'];
- $PATH = $pathsRow['path'];
- $PATH_LIKE = $PATH . '/%';
- $MIN_AGE = $pathsRow['min_age'];
- $LOCAL = $BASE . '/' . $PATH;
- # Sanity check
- if (!@is_dir($LOCAL) | !@is_readable($LOCAL)) {
- die('Invalid local directory: ' . $LOCAL . "\n");
- }
- # Look for files in the DB that no longer exist or have changed types
- $delete = $dbh->prepare('DELETE FROM files WHERE base = :base AND path = :path');
- $missing = $dbh->prepare('SELECT path, type FROM files WHERE base = :base AND path LIKE :path');
- $missing->execute(array(':base' => $BASE, ':path' => $PATH_LIKE));
- while ($row = $missing->fetch(PDO::FETCH_ASSOC)) {
- $trigger = false;
- $file = $BASE . '/' . $row['path'];
- if ($row['type'] == 'folder') {
- # Ensure the path exists and is a folder
- if (!is_dir($file)) {
- echo 'Missing folder: ' . $file . "\n";
- $trigger = true;
- }
- } else if ($row['type'] == 'ignored') {
- # Silently delete missing ignored files of any type
- if (!file_exists($file)) {
- if ($DEBUG) {
- echo 'Missing ignored file: ' . $file . "\n";
- }
- $trigger = true;
- }
- } else {
- # Ensure the path exists and is a regular file
- if (!is_file($file)) {
- echo 'Missing file: ' . $file . "\n";
- $trigger = true;
- }
- }
- # Delete if global DELETE is enabled
- if ($DELETE && $trigger) {
- $delete->execute(array(':base' => $BASE, ':path' => $row['path']));
- }
- unset($trigger);
- unset($file);
- }
- unset($row);
- unset($missing);
- unset($delete);
- # Grab the file list
- $FILES = array();
- $compAge = time() - ($MIN_AGE * 86400);
- $fsh = NULL;
- try {
- $fsh = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator(
- $LOCAL,
- FilesystemIterator::KEY_AS_PATHNAME |
- FilesystemIterator::CURRENT_AS_FILEINFO |
- FilesystemIterator::SKIP_DOTS |
- FilesystemIterator::UNIX_PATHS
- ),
- RecursiveIteratorIterator::SELF_FIRST
- );
- } catch (Exception $e) {
- die('Invalid filesystem path: ' . $LOCAL . "\n");
- }
- foreach ($fsh as $path => $file) {
- # Ignore anything that isn't a file and isn't a directory
- if (!$file->isDir() && !$file->isFile()) {
- continue;
- }
- # Ignore file less than $MIN_AGE old
- if ($file->isFile() && $file->getMTime() > $compAge) {
- continue;
- }
- # Keep everything else
- $FILES[] = $file;
- unset($path);
- unset($file);
- }
- unset($compAge);
- unset($fsh);
- # Loop through all the files we found
- $select = $dbh->prepare('SELECT base, path, type FROM files WHERE base = :base AND path = :path');
- $insert = $dbh->prepare('INSERT INTO files (base, path, type, mtime) VALUES (:base, :path, :type, now())');
- $check = $dbh->prepare('SELECT type, size, EXTRACT(EPOCH FROM mtime) AS mtime FROM files WHERE base = :base AND path = :path');
- $set_mtime = $dbh->prepare('UPDATE files SET mtime = now() WHERE base = :base AND path = :path');
- $set_size = $dbh->prepare('UPDATE files SET size = :size WHERE base = :base AND path = :path');
- $set_type = $dbh->prepare('UPDATE files SET type = :type WHERE base = :base AND path = :path');
- $check_hash = $dbh->prepare('SELECT hash, EXTRACT(EPOCH FROM hash_time) AS hash_time ' .
- 'FROM files WHERE base = :base AND path = :path AND ' .
- '(hash_time IS NULL OR EXTRACT(EPOCH FROM hash_time) < :mtime)');
- $set_hash = $dbh->prepare('UPDATE files SET hash = :hash, hash_time = now() WHERE base = :base AND path = :path');
- foreach ($FILES as $file) {
- # Construct the absolute path
- $path = $file->getPathname();
- $shortPath = substr($path, strlen($BASE) + 1);
- if ($DEBUG) {
- echo 'Scanning: ' . $path . "\n";
- }
- # Pick a file type
- $ext = $file->getExtension();
- $name = strtolower($file->getBasename('.' . $ext));
- $ext = strtolower($ext);
- $type = 'other';
- if (preg_match('/\/\.git(\/.*)?$/', $shortPath)) {
- $type = 'ignored';
- } else if (preg_match('/\/\._/', $shortPath)) {
- $type = 'ignored';
- } else if ($ext == 'lastfindrecode' || $name == 'placeholder' || $ext == 'plexignore') {
- $type = 'ignored';
- } else if ($ext == 'tmp' || $ext == 'gitignore' || $ext == 'ds_store' ||
- preg_match('/^\.smbdelete/', $name) || preg_match('/\.mkv\.\w+$/', $shortPath)) {
- $type = 'ignored';
- } else if (preg_match('/^iTunes\/Album Artwork\/Cache/', $shortPath) || $shortPath == 'iTunes/sentinel') {
- $type = 'ignored';
- } else if ($file->isDir()) {
- $type = 'folder';
- if ($ext == '' && $name == 'cmd') {
- $type = 'ignored';
- }
- } else if ($ext == 'm4v' || $ext == 'mkv' || $ext == 'mp4' || $ext == 'mov' ||
- $ext == 'vob' || $ext == 'iso' || $ext == 'avi') {
- $type = 'video';
- } else if ($ext == 'mp3' || $ext == 'aac' || $ext == 'm4a' || $ext == 'm4b' ||
- $ext == 'm4p' || $ext == 'wav') {
- $type = 'audio';
- } else if ($ext == 'epub' || $ext == 'pdf') {
- $type = 'book';
- } else if ($ext == 'jpg' || $ext == 'png') {
- $type = 'image';
- } else if ($ext == 'gz' || $ext == 'zip' || $ext == 'xz') {
- $type = 'archive';
- } else if ($ext == 'itc' || $ext == 'itl' || $ext == 'strings' || $ext == 'itdb' ||
- $ext == 'xml' || $ext == 'plist' || $ext == 'ipa' || $ext == 'ini') {
- $type = 'database';
- } else if ($ext == 'clip' || $ext == 'riff' || $ext == 'nfo') {
- $type = 'metadata';
- } else if ($ext == 'webloc' || $name == 'skip' || $name == 'season_done' ||
- $name == 'more_number_formats' || $name == 'no_quality_checks' ||
- $name == 'filler' || $name == 'search_name' || $ext == 'disabled' ||
- $name == 'must_match' || $ext == 'fakeshow' || $ext == 'filler' ||
- $name == 'excludes' || $name == 'search_by_date' || $ext == 'twopart') {
- $type = 'metadata';
- } else if ($ext == 'fake' || $ext == 'txt' || $ext == 'json' ||
- $ext == 'bup' || $ext == 'ifo') {
- $type = 'metadata';
- } else if (preg_match('/\.sparsebundle\//i', $shortPath)) {
- if ($ext == 'bckup' || $ext == 'plist') {
- $type = 'disk';
- } else if (preg_match('/\.sparsebundle\/bands\/\w+$/i', $shortPath)) {
- $type = 'disk';
- }
- }
- if ($type == 'other') {
- die('Unknown file type: ' . $path . ': ' . $name . '|' . $ext . "\n");
- }
- # Add missing paths
- $select->execute(array(':base' => $BASE, ':path' => $shortPath));
- if (!$select->fetch(PDO::FETCH_ASSOC)) {
- if ($DEBUG) {
- echo 'Adding: ' . $path . ': ' . $type . "\n";
- }
- $insert->execute(array(':base' => $BASE, ':path' => $shortPath, ':type' => $type));
- }
- # Grab the DB entry
- $check->execute(array(':base' => $BASE, ':path' => $shortPath));
- $row = $check->fetch(PDO::FETCH_ASSOC);
- if (!$row || !is_array($row)) {
- die('Unable to fetch entry for: ' . $path . "\n");
- }
- # Check the file type
- if ($row['type'] != $type) {
- if ($DEBUG) {
- echo 'Updating type for: ' . $path . "\n";
- }
- $set_type->execute(array(':base' => $BASE, ':path' => $shortPath, ':type' => $type));
- }
- # Skip everything else for 'ignored' files
- if ($type == 'ignored') {
- continue;
- }
- # Check the file mtime
- if ($row['mtime'] < $file->getMTime()) {
- if ($DEBUG) {
- echo 'Updating mtime for: ' . $file . "\n";
- }
- $set_mtime->execute(array(':base' => $BASE, ':path' => $shortPath));
- }
- # Check the file size
- if ($row['size'] != $file->getSize()) {
- if ($DEBUG) {
- echo 'Updating size for: ' . $file . "\n";
- }
- $set_size->execute(array(':base' => $BASE, ':path' => $shortPath, ':size' => $file->getSize()));
- }
- # Update hashes as needed
- if ($type != 'folder') {
- $check_hash->execute(array(':base' => $BASE, ':path' => $shortPath, ':mtime' => $mtime));
- $row = $check_hash->fetch(PDO::FETCH_ASSOC);
- if ($row) {
- if ($DEBUG) {
- echo 'Adding hash: ' . $path . "\n";
- }
- $hash = md5_file($path, false);
- if (strlen($hash) == 32) {
- $set_hash->execute(array(':base' => $BASE, ':path' => $shortPath, ':hash' => $hash));
- } else {
- echo 'Unable to hash: ' . $path . "\n";
- continue;
- }
- unset($hash);
- }
- unset($row);
- }
- unset($mtime);
- unset($name);
- unset($ext);
- unset($type);
- unset($shortPath);
- unset($path);
- }
- unset($FILES);
- unset($select);
- unset($insert);
- unset($check);
- unset($set_mtime);
- unset($set_size);
- unset($check_hash);
- unset($set_hash);
- # Update priorities
- $priority->execute(array(':priority' => $pathsRow['priority'], ':base' => $BASE, ':path' => $PATH_LIKE));
- if ($DEBUG) {
- echo 'Updated ' . $priority->rowCount() . ' rows from ' . $PATH . ' with priority ' . $pathsRow['priority'] . "\n";
- }
-
- # Update the scan time
- $scan->execute(array(':base' => $BASE, ':path' => $PATH));
- unset($BASE);
- unset($PATH);
- unset($PATH_LIKE);
- unset($MIN_AGE);
- unset($LOCAL);
- }
- unset($priority);
- unset($scan);
- unset($pathsRow);
- unset($paths);
- # If remote operations are enabled
- if ($REMOTE) {
- # Log in to OpenDrive
- require_once 'opendrive.php';
- $session = login();
- # Find our scan paths
- $paths = NULL;
- $scan = $dbh->prepare('UPDATE paths SET remote_last_scan = now() WHERE base = :base AND path = :path');
- if (!$LIMIT_PATH) {
- $paths = $dbh->prepare('SELECT base, path, priority, min_age FROM paths' .
- ' WHERE remote_last_scan < now() - remote_scan_age');
- } else {
- $paths = $dbh->prepare('SELECT base, path, priority, min_age FROM paths' .
- ' WHERE remote_last_scan < now() - remote_scan_age' .
- ' AND path LIKE :path');
- $paths->execute(array(':path' => $LIMIT_PATH));
- }
- while ($pathsRow = $paths->fetch(PDO::FETCH_ASSOC)) {
- # Grab our globals
- $BASE = $pathsRow['base'];
- $PATH = $pathsRow['path'];
- $PATH_LIKE = $PATH . '/%';
- if ($DEBUG) {
- echo 'Remote scan for: ' . $PATH . "\n";
- }
- # Validate the existence, type and size of all files we think on the remote system
- $select = $dbh->prepare('SELECT path, type, size FROM files WHERE remote_mtime IS NOT NULL AND path LIKE :path');
- $clear = $dbh->prepare('UPDATE files SET remote_mtime = NULL, remote_hash = NULL WHERE base = :base AND path = :path');
- $select->execute(array(':path' => $PATH_LIKE));
- while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
- $trigger = false;
- if ($DEBUG > 2) {
- echo 'Checking remote: ' . $row['path'] . "\n";
- }
- if ($row['type'] == 'ignored') {
- continue;
- } else if ($row['type'] == 'folder') {
- if (!folderID($session, $row['path'])) {
- echo 'Missing remote folder: ' . $row['path'] . "\n";
- $trigger = true;
- }
- } else {
- $data = fileInfo($session, $row['path']);
- if (!$data) {
- echo 'Missing remote file: ' . $row['path'] . "\n";
- $trigger = true;
- } else if ($data['Size'] != $row['size']) {
- echo 'Wrong size remote file: ' . $row['path'] . ' (' . $data['size'] . '/' . $row['Size'] . ")\n";
- $trigger = true;
- }
- unset($data);
- }
- if ($DELETE && $trigger) {
- $clear->execute(array(':base' => $BASE, ':path' => $row['path']));
- }
- unset($trigger);
- }
- unset($select);
- unset($clear);
- unset($row);
- # Update the remote scan time
- $scan->execute(array(':base' => $BASE, ':path' => $PATH));
- # Cleanup
- unset($PATH);
- unset($PATH_LIKE);
- }
- # Logout of OpenDrive
- logout($session);
- unset($session);
- }
- unset($scan);
- unset($pathsRow);
- unset($paths);
- # Cleanup
- unset($dbh);
- ?>
|