Files
Yuba/Yuba.php

164 lines
4.9 KiB
PHP
Raw Normal View History

2017-05-03 00:39:48 -07:00
#!/usr/bin/php
<?php
// Yuba
2017-05-20 20:03:50 -07:00
//////////////////////////////////////////
2017-05-03 00:39:48 -07:00
2017-05-20 20:03:50 -07:00
// File checks
$version = "0.1.1";
2017-05-03 00:39:48 -07:00
date_default_timezone_set("America/Los_Angeles");
2017-05-20 20:03:50 -07:00
$time_start = microtime(true);
$stamp = date("Y-m-d_H-i-s", time());
$tmpdir = "/tmp/WalkWalk_".$stamp."/";
if (!is_dir($tmpdir)) { mkdir($tmpdir); }
2017-05-03 00:39:48 -07:00
$zpath = realpath($argv[1]);
2017-05-20 20:03:50 -07:00
if (!$zpath) { echo "Usage: walk <path> <dbfile>"; die; }
if (isset($argv[2]) && is_dir($argv[2])) {
$dbprefix = chop($argv[2],"/");
} else {
$dbprefix = ".";
2017-05-03 00:39:48 -07:00
}
2017-05-20 20:03:50 -07:00
$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
2017-05-03 00:39:48 -07:00
echo "Yuba ".$version."\n";
echo "-----------------------------------------------\n";
2017-05-20 20:03:50 -07:00
$banner = $zpath." -> ".$dbfile;
2017-05-03 00:39:48 -07:00
echo $banner."\n";
echo str_repeat("-", strlen($banner))."\n";
2017-05-20 20:03:50 -07:00
// 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
2017-05-03 00:39:48 -07:00
$dir_iter = new RecursiveDirectoryIterator($zpath,
RecursiveDirectoryIterator::SKIP_DOTS
);
$iter = new RecursiveIteratorIterator($dir_iter,
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);
2017-05-20 20:03:50 -07:00
// Insert
2017-05-03 00:39:48 -07:00
2017-05-20 20:03:50 -07:00
foreach ($iter as $splFileInfo) {
$path = $splFileInfo->getRealPath();
2017-05-03 00:39:48 -07:00
echo $path."\n";
2017-05-20 20:03:50 -07:00
$line = array();
2017-05-03 00:39:48 -07:00
$info = pathinfo($path);
2017-05-20 20:03:50 -07:00
$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;
2017-05-03 00:39:48 -07:00
} else {
2017-05-20 20:03:50 -07:00
$line['size'] = filesize($path);
2017-05-03 00:39:48 -07:00
}
2017-05-20 20:03:50 -07:00
$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);
2017-05-03 00:39:48 -07:00
$spotlight = shell_exec("mdls \"".$path."\" 2>&1");
if ($spotlight != $path.": could not find ".$path.".\n") {
2017-05-20 20:03:50 -07:00
$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);
}
2017-05-03 00:39:48 -07:00
}
2017-05-20 20:03:50 -07:00
$line['mediainfo'] = "";
$line['exif'] = "";
@exec("qlmanage -t -o ".$tmpdir." \"".$path."\" 2>&1");
$thumb = $tmpdir.basename($path).".png";
2017-05-03 00:39:48 -07:00
if (file_exists($thumb)) {
2017-05-20 20:03:50 -07:00
$line['thumb'] = file_get_contents($thumb);
2017-05-03 00:39:48 -07:00
}
2017-05-20 20:03:50 -07:00
$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";
2017-05-03 00:39:48 -07:00
?>