diff --git a/Yuba.php b/Yuba.php index 6ece6ec..96c66fe 100755 --- a/Yuba.php +++ b/Yuba.php @@ -2,26 +2,79 @@ "; - die; +if (!$zpath) { echo "Usage: walk "; 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"; -echo date("F jS, Y h:i:s", time())."\n"; -$banner = "Walking ".$zpath." to ".$dbfile; +$banner = $zpath." -> ".$dbfile; echo $banner."\n"; echo str_repeat("-", strlen($banner))."\n"; -die; + +// 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 @@ -31,44 +84,81 @@ $iter = new RecursiveIteratorIterator($dir_iter, RecursiveIteratorIterator::CATCH_GET_CHILD ); -$i = 1; -foreach ($iter as $splFileInfo) { - $key[$splFileInfo->getRealPath()] = $i; - $i++; - } +// Insert -$line = array(); -foreach ($key as $path => $id) { +foreach ($iter as $splFileInfo) { + $path = $splFileInfo->getRealPath(); echo $path."\n"; + $line = array(); $info = pathinfo($path); - $line[$id]['parent'] = @$key[$info['dirname']]; - $line[$id]['path'] = $path; - if (is_dir($path)) { - $line[$id]['isdir'] = 1; + $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[$id]['isdir'] = 0; + $line['parent'] = 0; } - $line[$id]['size'] = filesize($path); - $hash = md5_file($path); - $line[$id]['hash'] = $hash; - $line[$id]['perms'] = fileperms($path); - $line[$id]['owner'] = fileowner($path).":".filegroup($path); - $line[$id]['type'] = filetype($path); - $line[$id]['inode'] = fileinode($path); - $line[$id]['mtime'] = filemtime($path); - $line[$id]['atime'] = fileatime($path); - $line[$id]['ctime'] = filectime($path); + $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[$id]['mdls'] = $spotlight; + $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); + } } - $thumb = "/tmp/".$hash.".png"; - exec("qlmanage -t -o \"".$thumb."\" \"".$path."\""); + $line['mediainfo'] = ""; + $line['exif'] = ""; + @exec("qlmanage -t -o ".$tmpdir." \"".$path."\" 2>&1"); + $thumb = $tmpdir.basename($path).".png"; if (file_exists($thumb)) { - $line[$id]['thumb'] = file_get_contents($blob); + $line['thumb'] = file_get_contents($thumb); } - } -print_r($line); + $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"; ?> \ No newline at end of file