374 lines
8.8 KiB
PHP
Executable File
374 lines
8.8 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
// Yuba
|
|
//////////////////////////////////////////
|
|
$version = "0.2.1";
|
|
$thumb_size = "256";
|
|
$thumb_factor = $thumb_size/128;
|
|
|
|
// Path arguments
|
|
|
|
$zpath = realpath($argv[1]);
|
|
if (!is_dir($zpath)) { echo "Usage: walk <path> <dbfile>"; die; }
|
|
if (isset($argv[2]) && is_dir($argv[2])) {
|
|
$dbprefix = realpath($argv[2]);
|
|
} else {
|
|
$dbprefix = ".";
|
|
}
|
|
|
|
// File checks
|
|
|
|
date_default_timezone_set("America/Los_Angeles");
|
|
$time_start = microtime(true);
|
|
$stamp = date("Y-m-d_H-i-s", time());
|
|
$tmpdir = "/tmp/WalkWalk_".$stamp."/";
|
|
if (!is_dir($tmpdir)) { mkdir($tmpdir); }
|
|
$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";
|
|
|
|
// Disk info
|
|
|
|
$host = gethostname();
|
|
$disks = shell_exec("diskutil list 2>&1");
|
|
|
|
if (substr($zpath, 0, 9) != "/Volumes/") {
|
|
$zbase = "/";
|
|
} else {
|
|
$zparts = explode("/", $zpath);
|
|
$zbase = "/Volumes/".$zparts[2];
|
|
}
|
|
|
|
$diskutil = shell_exec("diskutil info ".$zbase." 2>&1");
|
|
$getstats = array( "Volume Name",
|
|
"Protocol",
|
|
"Volume UUID",
|
|
"Device Location",
|
|
"Volume Total Space",
|
|
"Volume Available Space",
|
|
"Level Type"
|
|
);
|
|
foreach ($getstats as $stat) {
|
|
preg_match("/(".$stat.":)(.*)(\n)/",$diskutil,$matches);
|
|
if (isset($matches[2])) {
|
|
if (substr($stat, -5, 5) == "Space") {
|
|
$pieces = explode(" ", trim($matches[2]));
|
|
$summary = $pieces[0]." ".$pieces[1];
|
|
$stats[$stat] = $summary;
|
|
} else {
|
|
$stats[$stat] = trim($matches[2]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$dstats = serialize($stats);
|
|
if ($zpath == "/") {
|
|
$type = "Startup disk";
|
|
} elseif (strtolower($zpath) == strtolower("/Volumes/".$stats["Volume Name"])) {
|
|
if ($stats["Protocol"] == "Disk Image") {
|
|
$type = "Disk image";
|
|
} else {
|
|
$type = "External disk";
|
|
}
|
|
} else {
|
|
$type = "Folder";
|
|
}
|
|
|
|
$profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1");
|
|
|
|
// Database
|
|
|
|
$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,
|
|
exiftool TEXT,
|
|
tinfo TEXT)");
|
|
|
|
$dbo->exec("CREATE TABLE IF NOT EXISTS thumbs (
|
|
id INTEGER PRIMARY KEY,
|
|
thumb BLOB)");
|
|
|
|
$dbo->exec("CREATE TABLE IF NOT EXISTS _walkwalk (
|
|
version TEXT,
|
|
host TEXT,
|
|
zpath TEXT,
|
|
type TEXT,
|
|
stats TEXT,
|
|
diskutil TEXT,
|
|
disks TEXT,
|
|
profile TEXT)");
|
|
|
|
$insert = "INSERT INTO _walkwalk VALUES (:version, :host, :zpath, :type, :stats, :diskutil, :disks, :profile)";
|
|
$stmt = $dbo->prepare($insert);
|
|
$stmt->BindValue(":version",$version." (".posix_getuid().")");
|
|
$stmt->BindValue(":host",$host);
|
|
$stmt->BindValue(":zpath",$zpath);
|
|
$stmt->BindValue(":type",$type);
|
|
$stmt->BindValue(":stats",$dstats);
|
|
$stmt->BindValue(":diskutil",$diskutil);
|
|
$stmt->BindValue(":disks",$disks);
|
|
$stmt->BindValue(":profile",$profile);
|
|
$stmt->execute();
|
|
|
|
// Iterate
|
|
|
|
$dir_iter = new RecursiveDirectoryIterator($zpath,
|
|
RecursiveDirectoryIterator::SKIP_DOTS
|
|
);
|
|
$iter = new RecursiveIteratorIterator($dir_iter,
|
|
RecursiveIteratorIterator::SELF_FIRST,
|
|
RecursiveIteratorIterator::CATCH_GET_CHILD
|
|
);
|
|
|
|
// Check perms
|
|
|
|
if (posix_getuid()) {
|
|
|
|
echo "You are not root. Checking file readability: ";
|
|
|
|
$oops = 0;
|
|
foreach ($iter as $splFileInfo) {
|
|
$path = $splFileInfo->getRealPath();
|
|
if (!is_readable($path)) {
|
|
$oops = 1;
|
|
echo "x";
|
|
} else {
|
|
echo ".";
|
|
}
|
|
}
|
|
|
|
echo "\n\n";
|
|
|
|
if ($oops) {
|
|
echo "Some files could not be read. Continue? (Y/n)";
|
|
$line = trim(fgets(fopen("php://stdin","r")));
|
|
$line = $line ?: "y";
|
|
if($line != "y"){
|
|
echo "Exiting!\n"; die;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Filetypes
|
|
|
|
$m_files = array( "mkv",
|
|
"ogg",
|
|
"avi",
|
|
"wav",
|
|
"mpeg",
|
|
"mpg",
|
|
"vob",
|
|
"mp4",
|
|
"m2v",
|
|
"mp3",
|
|
"asf",
|
|
"wma",
|
|
"wmv",
|
|
"qt",
|
|
"mov",
|
|
"rm",
|
|
"ifo",
|
|
"ac3",
|
|
"dts",
|
|
"aac",
|
|
"ape",
|
|
"flac",
|
|
"aiff",
|
|
"m2ts" );
|
|
|
|
$e_files = array( "ai",
|
|
"aiff",
|
|
"ape",
|
|
"asf",
|
|
"avi",
|
|
"bmp",
|
|
"divx",
|
|
"dng",
|
|
"doc",
|
|
"docx",
|
|
"eps",
|
|
"epub",
|
|
"exe",
|
|
"exif",
|
|
"fla",
|
|
"flac",
|
|
"flv",
|
|
"gif",
|
|
"icc",
|
|
"iso",
|
|
"jpg",
|
|
"jpeg",
|
|
"m2ts",
|
|
"m4a",
|
|
"m4b",
|
|
"m4v",
|
|
"mkv",
|
|
"mobi",
|
|
"azw",
|
|
"azw3",
|
|
"mov",
|
|
"qt",
|
|
"mp3",
|
|
"mp4",
|
|
"mpeg",
|
|
"mpg",
|
|
"m2v",
|
|
"nef",
|
|
"numbers",
|
|
"ogg",
|
|
"pages",
|
|
"pdf",
|
|
"pict",
|
|
"png",
|
|
"ppm",
|
|
"ppt",
|
|
"psd",
|
|
"psb",
|
|
"qif",
|
|
"raw",
|
|
"rtf",
|
|
"sr2",
|
|
"srf",
|
|
"svg",
|
|
"swf",
|
|
"tiff",
|
|
"tif",
|
|
"torrent",
|
|
"vcf",
|
|
"vob",
|
|
"wav",
|
|
"webm",
|
|
"wma",
|
|
"wmv",
|
|
"xls",
|
|
"xlsx",
|
|
"xmp",
|
|
"zip" );
|
|
|
|
// Insert
|
|
|
|
foreach ($iter as $splFileInfo) {
|
|
$path = $splFileInfo->getRealPath();
|
|
echo $path.": ";
|
|
$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);
|
|
}
|
|
echo floor($line['size']/1024)."k; ";
|
|
$line['hash'] = md5_file($path);
|
|
echo "hashed; ";
|
|
$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);
|
|
echo "perms; ";
|
|
$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);
|
|
}
|
|
}
|
|
echo "mdls; ";
|
|
$thumb = $tmpdir.basename($path).".png";
|
|
@exec("qlmanage -t -f ".$thumb_factor." -o ".$tmpdir." \"".$path."\" 2>&1");
|
|
if (!file_exists($thumb) && (in_array($line['ext'], $m_files) | in_array($line['ext'], $e_files))) {
|
|
@exec("ffmpegthumbnailer -i \"".$path."\" -o \"".$thumb."\" -s ".$thumb_size." -c png 2>&1");
|
|
echo "ffmpeg-";
|
|
}
|
|
if (file_exists($thumb) && filesize($thumb)) {
|
|
$line['thumb'] = file_get_contents($thumb);
|
|
$line['tinfo'] = serialize(getimagesize($thumb));
|
|
}
|
|
echo "thumb; ";
|
|
if ($line['ext'] && in_array($line['ext'], $m_files)) {
|
|
$line['mediainfo'] = shell_exec("mediainfo \"".$path."\" 2>&1");
|
|
}
|
|
echo "media; ";
|
|
if ($line['ext'] && in_array($line['ext'], $e_files)) {
|
|
$line['exiftool'] = shell_exec("exiftool \"".$path."\" 2>&1");
|
|
}
|
|
echo "exif ";
|
|
$insert = "INSERT INTO \"".$base."\" VALUES (:id, :parent, :path, :ext, :type, :size, :hash, :perms, :owner, :inode, :mtime, :atime, :ctime, :mdls, :tags, :mediainfo, :exiftool, :tinfo)";
|
|
$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(":exiftool",@$line['exiftool']);
|
|
$stmt->BindValue(":tinfo",@$line['tinfo']);
|
|
$stmt->execute();
|
|
echo "-> sql";
|
|
$insert = "INSERT INTO thumbs VALUES (:id, :thumb)";
|
|
$stmt = $dbo->prepare($insert);
|
|
$stmt->BindValue(":thumb",@$line['thumb']);
|
|
$stmt->execute();
|
|
if (@$line['thumb']) {
|
|
echo " (thumb)";
|
|
}
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
$fbanner = "Finished in ".floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"])." seconds";
|
|
echo str_repeat("-", strlen($fbanner))."\n".$fbanner."\n";
|
|
|
|
?>
|