#!/usr/bin/php "; die; } if (isset($argv[2]) && is_dir($argv[2])) { $dbprefix = chop($argv[2],"/"); } else { $dbprefix = "."; } $base = preg_replace("/[^A-Za-z0-9\.]/", "_", basename($zpath)); $dbfile = $dbprefix."/".$stamp."_".$base.".sqlite3"; if (file_exists($dbfile)) { echo "File \"".$dbfile."\" already exists!"; die; } // Banner echo "Yuba ".$version."\n"; echo "-----------------------------------------------\n"; $banner = $zpath." -> ".$dbfile; echo $banner."\n"; echo str_repeat("-", strlen($banner))."\n"; // Database $profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1"); $disks = shell_exec("diskutil list 2>&1"); $host = gethostname(); $dbo = new PDO("sqlite:".$dbfile); $dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $dbo->exec("CREATE TABLE IF NOT EXISTS \"".$base."\" ( id INTEGER PRIMARY KEY, parent INTEGER, path TEXT, ext TEXT, type TEXT, size INTEGER, hash TEXT, perms INTEGER, owner TEXT, inode INTEGER, mtime INTEGER, atime INTEGER, ctime INTEGER, mdls TEXT, tags TEXT, mediainfo TEXT, exif TEXT, thumb BLOB)"); $dbo->exec("CREATE TABLE IF NOT EXISTS _walkwalk ( version TEXT, host TEXT, profile TEXT, disks TEXT)"); $insert = "INSERT INTO _walkwalk VALUES (:version, :host, :profile, :disks)"; $stmt = $dbo->prepare($insert); $stmt->BindValue(":version",$version); $stmt->BindValue(":host",$host); $stmt->BindValue(":profile",$profile); $stmt->BindValue(":disks",$disks); $stmt->execute(); // Iterate $dir_iter = new RecursiveDirectoryIterator($zpath, RecursiveDirectoryIterator::SKIP_DOTS ); $iter = new RecursiveIteratorIterator($dir_iter, RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD ); // Insert foreach ($iter as $splFileInfo) { $path = $splFileInfo->getRealPath(); echo $path."\n"; $line = array(); $info = pathinfo($path); $select = "SELECT id from \"".$base."\" WHERE (path=\"".$info['dirname']."\")"; $slct = $dbo->query($select); $row = $slct->fetchObject(); if (@$row->id) { $line['parent'] = $row->id; } else { $line['parent'] = 0; } $line['path'] = $path; $line['ext'] = strtolower(@$info['extension']); $line['type'] = filetype($path); if ($line['type'] == "dir") { $line['size'] = @shell_exec("du -ks \"".$path."\"")*1024; } else { $line['size'] = filesize($path); } $line['hash'] = md5_file($path); $line['perms'] = fileperms($path); $line['owner'] = fileowner($path).":".filegroup($path); $line['inode'] = fileinode($path); $line['mtime'] = filemtime($path); $line['atime'] = fileatime($path); $line['ctime'] = filectime($path); $spotlight = shell_exec("mdls \"".$path."\" 2>&1"); if ($spotlight != $path.": could not find ".$path.".\n") { $line['mdls'] = $spotlight; if (strpos($spotlight, "kMDItemUserTags")) { $jlines = array_slice(explode("\n", @shell_exec("mdls -name kMDItemUserTags -raw \"".$path."\" 2>&1")), 1, -1); $colors = array(); foreach ($jlines as $jline) { $colors[] = chop($jline,","); } $line['tags'] = serialize($colors); } } $line['mediainfo'] = ""; $line['exif'] = ""; @exec("qlmanage -t -o ".$tmpdir." \"".$path."\" 2>&1"); $thumb = $tmpdir.basename($path).".png"; if (file_exists($thumb)) { $line['thumb'] = file_get_contents($thumb); } $insert = "INSERT INTO \"".$base."\" VALUES (:id, :parent, :path, :ext, :type, :size, :hash, :perms, :owner, :inode, :mtime, :atime, :ctime, :mdls, :tags, :mediainfo, :exif, :thumb)"; $stmt = null; $stmt = $dbo->prepare($insert); $stmt->BindValue(":parent",$line['parent']); $stmt->BindValue(":path",$line['path']); $stmt->BindValue(":ext",$line['ext']); $stmt->BindValue(":type",$line['type']); $stmt->BindValue(":size",$line['size']); $stmt->BindValue(":hash",$line['hash']); $stmt->BindValue(":perms",$line['perms']); $stmt->BindValue(":owner",$line['owner']); $stmt->BindValue(":inode",$line['inode']); $stmt->BindValue(":mtime",$line['mtime']); $stmt->BindValue(":atime",$line['atime']); $stmt->BindValue(":ctime",$line['ctime']); $stmt->BindValue(":mdls",$line['mdls']); $stmt->BindValue(":tags",@$line['tags']); $stmt->BindValue(":mediainfo",$line['mediainfo']); $stmt->BindValue(":exif",$line['exif']); $stmt->BindValue(":thumb",@$line['thumb']); $stmt->execute(); } $fbanner = "Finished in ".floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"])." seconds"; echo str_repeat("-", strlen($fbanner))."\n".$fbanner."\n"; ?>