Files
Yuba/Yuba.php

1235 lines
34 KiB
PHP
Raw Normal View History

2017-05-03 00:39:48 -07:00
<?php
2017-06-20 02:05:52 -07:00
// Yuba
// //
//////////////////////////////////////////
2019-04-27 00:48:25 -07:00
$version = "0.7.10.3";
2017-06-20 02:05:52 -07:00
2019-04-27 00:48:25 -07:00
ini_set('memory_limit', '10240M');
2017-05-25 15:38:19 -07:00
date_default_timezone_set("America/Los_Angeles");
2018-08-08 23:12:47 -07:00
// Includes & Prefs
//////////////////////////////////////////
require("functions.php");
require("filetypes.php");
2018-09-16 15:22:15 -07:00
$wopt_noprofile = 0;
$wopt_steps = 10;
2018-09-13 01:50:00 -07:00
$wopt_currstep = 1;
2018-08-08 23:12:47 -07:00
2019-04-09 01:13:49 -07:00
$parser = new plistParser();
2018-08-08 23:12:47 -07:00
$p = unserialize(file_get_contents("prefs.php"));
2017-06-20 02:05:52 -07:00
// Path & application variables
2017-05-20 20:03:50 -07:00
//////////////////////////////////////////
2017-06-20 02:05:52 -07:00
2018-09-02 11:21:24 -07:00
$stamp = date("Y-m-d_H-i-s", time());
2018-08-08 23:12:47 -07:00
if (!isset($argv[1])) { echo "Input error"; die; }
2017-06-20 02:05:52 -07:00
$zpath = realpath(@$argv[1]);
2018-08-08 23:12:47 -07:00
if (@$argv[2]) { $bdest = realpath($argv[2]); } else { $bdest = realpath($p['bdest']); }
if (!is_dir($zpath) | !is_dir($bdest)) { echo "Filepath error"; die; }
2017-06-20 02:05:52 -07:00
// Check for bundle
if ($zpath == "/") { $blabel = "root"; } else { $blabel = preg_replace("/[^A-Za-z0-9\.]/", "_", basename($zpath)); }
2019-04-09 01:13:49 -07:00
if (is_writable($zpath)) { echo "Warning: source is writeable\n"; }
2017-06-20 02:05:52 -07:00
$bpath = chop($bdest,"/")."/".substr(crc32($zpath),0,3)."_".$blabel.".bundle";
if (!is_dir($bpath)) { mkdir($bpath); }
if (!is_dir($bpath."/thumbs")) { mkdir($bpath."/thumbs"); }
2018-09-14 13:50:27 -07:00
if (!is_dir($bpath."/contents")) { mkdir($bpath."/contents"); }
2017-06-20 02:05:52 -07:00
2017-06-16 00:14:32 -07:00
// Treat these directories as files
2018-08-08 23:12:47 -07:00
$p['bundles'] = array( "app",
2017-06-16 00:14:32 -07:00
"bundle",
"sparsebundle",
"photoslibrary",
"aplibrary",
"apvault",
"abbu",
"calendar",
"framework",
"plugin",
"kext",
"rtfd"
);
2018-08-08 23:12:47 -07:00
foreach ($p['bundles'] as $bundle) {
$p['nodescend'][] = "*.".$bundle;
2017-06-16 00:14:32 -07:00
}
2017-05-25 15:38:19 -07:00
// Ignore matching files and directories
2018-08-08 23:12:47 -07:00
$p['ignore'] = array( ".DS_Store",
2019-04-27 00:48:25 -07:00
".Trashes",
2017-06-16 00:14:32 -07:00
".DocumentRevisions-V100",
".Spotlight-V100",
".TemporaryItems",
".apdisk",
".com.apple.timemachine.donotpresent",
".fseventsd",
".metadata-never-index",
".neofinder.abemeda.volinfo.xml"
);
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
// Metadata tools
2018-08-08 23:12:47 -07:00
$bin_gfi = __DIR__."/bin/GetFileInfo";
$bin_mediainfo = __DIR__."/bin/mediainfo";
$bin_exiftool = __DIR__."/bin/exiftool";
$bin_ffmpeg = __DIR__."/bin/ffmpeg";
$bin_qlthumb = __DIR__."/bin/ql-thumbnail";
2017-06-20 02:05:52 -07:00
2018-09-01 21:30:02 -07:00
// Logfile
2018-09-16 15:22:15 -07:00
$messages_log_file = $bpath."/".$stamp."_messages.log";
2018-09-02 11:21:24 -07:00
$error_log_file = $bpath."/".$stamp."_error.log";
2018-09-01 21:30:02 -07:00
error_reporting(E_ALL);
ini_set("display_errors", TRUE);
ini_set("log_errors", TRUE);
2018-09-02 11:21:24 -07:00
ini_set("error_log", $error_log_file);
2018-09-01 21:30:02 -07:00
2018-08-08 23:12:47 -07:00
// Banner
2017-06-20 02:05:52 -07:00
//////////////////////////////////////////
2018-08-08 23:12:47 -07:00
$banner = "Yuba: ".$zpath." -> ".$bpath;
2018-09-16 15:22:15 -07:00
echo msg($banner."\n".str_repeat("-", strlen($banner)));
2017-05-22 23:37:36 -07:00
// Disk info
2017-06-20 02:05:52 -07:00
//////////////////////////////////////////
2017-05-20 20:03:50 -07:00
2018-09-16 15:22:15 -07:00
echo msg("Gathering system info...");
2018-08-08 23:12:47 -07:00
2019-04-09 01:13:49 -07:00
if (substr($zpath, 0, 9) != "/Volumes/") {
$zbase = "/";
} else {
$zparts = explode("/", $zpath);
$zbase = "/Volumes/".$zparts[2];
}
2017-05-20 20:03:50 -07:00
$host = gethostname();
2018-09-16 15:22:15 -07:00
$disks = shell_exec("diskutil list -plist 2>&1");
2019-04-09 01:13:49 -07:00
$diskutil = shell_exec("diskutil info -plist ".$zbase." 2>&1");
$diskutil_parsed = $parser->parseString(utf8_for_xml($diskutil));
//print_r($diskutil_parsed);
$vdisks = shell_exec("hdiutil info -plist 2>&1");
$vdisks_parsed = $parser->parseString(utf8_for_xml($vdisks));
//print_r($vdisks_parsed);
2018-09-01 21:30:02 -07:00
$df = shell_exec("df 2>&1");
2019-04-09 01:13:49 -07:00
// old method, new method is to parse plist
//$df_volume = trim(shell_exec("df ".escapeshellarg($zpath)." | tail -n 1 | rev | cut -d' ' -f1 | rev"));
//$df_device = trim(shell_exec("df ".escapeshellarg($zpath)." | tail -n 1 | cut -d' ' -f1"));
$df_volume = $diskutil_parsed['MountPoint'];
$df_device = "/dev/".$diskutil_parsed['ParentWholeDisk'];
2019-04-27 00:48:25 -07:00
$mdutil = shell_exec("mdutil -sv ".$df_volume);
2018-09-01 21:30:02 -07:00
if (strpos($mdutil,"disabled")) {
2018-09-16 15:22:15 -07:00
echo msg("Warning: spotlight indexing is disabled");
2018-09-02 11:21:24 -07:00
$p['spotlight'] = false;
2018-09-01 21:30:02 -07:00
}
2017-05-22 23:37:36 -07:00
2018-09-16 15:22:15 -07:00
/////////////////////////////////////////////////////////////////////////////////////////////
// rewrite below to use diskutil info -plist
//
//
//
2019-04-09 01:13:49 -07:00
$olddiskutil = shell_exec("diskutil info ".$zbase." 2>&1");
2018-09-14 13:50:27 -07:00
$getdetail = array( "Volume Name",
2017-05-22 23:37:36 -07:00
"Protocol",
"Volume UUID",
"Device Location",
"Volume Total Space",
"Volume Available Space",
"Level Type"
);
2018-09-14 13:50:27 -07:00
foreach ($getdetail as $detail) {
2019-04-09 01:13:49 -07:00
preg_match("/(".$detail.":)(.*)(\n)/",$olddiskutil,$matches);
2017-05-22 23:37:36 -07:00
if (isset($matches[2])) {
2018-09-14 13:50:27 -07:00
if (substr($detail, -5, 5) == "Space") {
2017-05-22 23:37:36 -07:00
$pieces = explode(" ", trim($matches[2]));
$summary = $pieces[0]." ".$pieces[1];
2018-09-14 13:50:27 -07:00
$details[$detail] = $summary;
2017-05-22 23:37:36 -07:00
} else {
2018-09-14 13:50:27 -07:00
$details[$detail] = trim($matches[2]);
2017-05-22 23:37:36 -07:00
}
}
}
2018-09-14 13:50:27 -07:00
$dstring = serialize($details);
2017-05-22 23:37:36 -07:00
if ($zpath == "/") {
$type = "Startup disk";
2018-09-14 13:50:27 -07:00
} elseif (strtolower($zpath) == strtolower("/Volumes/".$details["Volume Name"])) {
if ($details["Protocol"] == "Disk Image") {
2017-05-22 23:37:36 -07:00
$type = "Disk image";
} else {
$type = "External disk";
}
} else {
$type = "Folder";
}
2018-09-16 15:22:15 -07:00
//
//
//
/////////////////////////////////////////////////////////////////////////////////////////////
2017-05-22 23:37:36 -07:00
2019-04-09 01:13:49 -07:00
if ($type == "Disk image") {
$hdiutil = shell_exec("hdiutil imageinfo -plist ".$df_device." 2>&1");
foreach ($vdisks_parsed['images'] as $id => $disk) {
if ($disk['system-entities'][0]['dev-entry'] == $df_device) {
$image_file = $disk['image-path'];
}
}
} else {
$hdiutil = false;
$image_file = false;
}
2018-08-08 23:12:47 -07:00
if ($wopt_noprofile) {
$profile = "disabled";
} else {
2019-04-09 01:13:49 -07:00
echo msg("system_profiler");
2018-08-08 23:12:47 -07:00
$profile = shell_exec("system_profiler SPHardwareDataType SPStorageDataType SPThunderboltDataType SPUSBDataType 2>&1");
}
2017-06-16 00:14:32 -07:00
$qlmanage = shell_exec("qlmanage -m 2>&1");
$sysvers = shell_exec("sw_vers 2>&1");
2017-05-22 23:37:36 -07:00
// Database
2017-06-20 02:05:52 -07:00
//////////////////////////////////////////
2017-05-20 20:03:50 -07:00
2018-09-16 15:22:15 -07:00
echo msg("Building database...");
2018-08-08 23:12:47 -07:00
2017-06-22 01:09:03 -07:00
$dbo = new PDO("sqlite:".$bpath."/".$stamp.".sqlite3");
2017-05-20 20:03:50 -07:00
$dbo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2018-09-16 15:22:15 -07:00
$dbo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
2017-05-20 20:03:50 -07:00
2017-06-18 03:17:42 -07:00
$dbo->query("PRAGMA page_size = 4096");
$dbo->query("PRAGMA cache_size = 10000");
$dbo->query("PRAGMA locking_mode = EXCLUSIVE");
$dbo->query("PRAGMA synchronous = NORMAL");
$dbo->query("PRAGMA journal_mode = WAL");
2017-06-16 00:14:32 -07:00
2018-09-02 11:21:24 -07:00
$dbo->exec("CREATE TABLE _skim (
2017-06-20 02:05:52 -07:00
version TEXT,
opts TEXT,
host TEXT,
uid INTEGER,
zpath TEXT,
bpath TEXT,
type TEXT,
passed_file INTEGER,
passed_dir INTEGER,
passed_link INTEGER,
passed_total INTEGER,
nodescended INTEGER,
ignored INTEGER,
dupes INTEGER,
2018-09-14 13:50:27 -07:00
details TEXT,
2017-06-20 02:05:52 -07:00
qlmanage TEXT,
sysvers TEXT,
disks TEXT,
2019-04-09 01:13:49 -07:00
diskutil TEXT,
vdisks TEXT,
hdiutil TEXT,
image_file TEXT,
df TEXT,
df_device TEXT,
df_volume TEXT,
mdutil TEXT,
2017-06-20 02:05:52 -07:00
profile TEXT,
status TEXT
)");
$dbo->exec("CREATE TABLE family (
pid TEXT,
fid TEXT,
children TEXT
)");
2017-06-22 01:09:03 -07:00
2017-05-25 15:38:19 -07:00
$dbo->exec("CREATE TABLE files (
2017-06-20 02:05:52 -07:00
pid TEXT,
fid TEXT,
2017-06-22 01:09:03 -07:00
Pathname TEXT,
Path TEXT,
Filename TEXT,
Extension TEXT,
Type TEXT,
2018-09-05 00:26:26 -07:00
Size INTEGER,
2017-06-22 01:09:03 -07:00
Inode INTEGER,
Perms INTEGER,
Owner TEXT,
ATime INTEGER,
MTime INTEGER,
2018-09-02 11:21:24 -07:00
CTime INTEGER,
2017-06-22 01:09:03 -07:00
LinkTarget TEXT,
RealPath TEXT,
stat TEXT,
items INTEGER,
newest INTEGER,
2019-04-27 00:48:25 -07:00
fkind TEXT,
2017-06-22 01:09:03 -07:00
gfi_type TEXT,
gfi_attr TEXT,
gfi_created TEXT,
has_exif INTEGER,
has_mediainfo INTEGER,
has_hash INTEGER,
thumb_filename TEXT,
thumb_width INTEGER,
thumb_height INTEGER,
2018-09-03 02:53:01 -07:00
contents_filename TEXT
2017-05-25 15:38:19 -07:00
)");
2017-05-22 23:37:36 -07:00
2019-04-09 01:13:49 -07:00
$stmt = $dbo->prepare("INSERT INTO _skim VALUES (:version, :opts, :host, :uid, :zpath, :bpath, :type, :passed_file, :passed_dir, :passed_link, :passed_total, :nodescended, :ignored, :dupes, :details, :qlmanage, :sysvers, :disks, :diskutil, :vdisks, :hdiutil, :image_file, :df, :df_device, :df_volume, :mdutil, :profile, :status)");
2017-06-20 02:05:52 -07:00
$stmt->BindValue(":version",$version);
2018-08-08 23:12:47 -07:00
$stmt->BindValue(":opts",serialize($p));
2017-05-20 20:03:50 -07:00
$stmt->BindValue(":host",$host);
2017-06-20 02:05:52 -07:00
$stmt->BindValue(":uid",posix_getuid());
2017-05-22 23:37:36 -07:00
$stmt->BindValue(":zpath",$zpath);
2017-06-20 02:05:52 -07:00
$stmt->BindValue(":bpath",$bpath);
2017-05-22 23:37:36 -07:00
$stmt->BindValue(":type",$type);
2018-09-14 13:50:27 -07:00
$stmt->BindValue(":details",$dstring);
2017-06-16 00:14:32 -07:00
$stmt->BindValue(":qlmanage",$qlmanage);
$stmt->BindValue(":sysvers",$sysvers);
2019-04-09 01:13:49 -07:00
$stmt->BindValue(":disks",$disks);
2017-05-22 23:37:36 -07:00
$stmt->BindValue(":diskutil",$diskutil);
2019-04-09 01:13:49 -07:00
$stmt->BindValue(":vdisks",$vdisks);
2018-09-16 15:22:15 -07:00
$stmt->BindValue(":hdiutil",$hdiutil);
2019-04-09 01:13:49 -07:00
$stmt->BindValue(":image_file",$image_file);
2018-09-01 21:30:02 -07:00
$stmt->BindValue(":df",$df);
$stmt->BindValue(":df_device",$df_device);
$stmt->BindValue(":df_volume",$df_volume);
$stmt->BindValue(":mdutil",$mdutil);
2017-05-22 23:37:36 -07:00
$stmt->BindValue(":profile",$profile);
2017-05-25 15:38:19 -07:00
$stmt->BindValue(":status","aborted");
2017-05-20 20:03:50 -07:00
$stmt->execute();
2017-06-20 02:05:52 -07:00
// Iterator
//////////////////////////////////////////
2017-05-03 00:39:48 -07:00
2018-08-08 23:12:47 -07:00
$first_run = 1;
$passed_file = $passed_dir = $passed_link = $passed_total = $nodescended = $ignored = 0;
2017-05-25 15:38:19 -07:00
$files = new RecursiveIteratorIterator(
new RecursiveCallbackFilterIterator(
new RecursiveDirectoryIterator(
2017-06-22 01:09:03 -07:00
$zpath,
2017-05-25 15:38:19 -07:00
RecursiveDirectoryIterator::SKIP_DOTS
),
2018-08-08 23:12:47 -07:00
function ($current, $key, $iterator) use ($p) {
global $nodescended, $ignored, $passed_file, $passed_dir, $passed_link, $passed_total, $first_run;
2017-05-25 15:38:19 -07:00
$clean = true;
2017-06-22 01:09:03 -07:00
// identify ignore files
2018-08-08 23:12:47 -07:00
if (is_array($p['ignore'])) {
foreach ($p['ignore'] as $wildcard) {
2017-05-25 15:38:19 -07:00
if (fnmatch($wildcard, $current->getFilename())) {
$clean = false;
2018-08-08 23:12:47 -07:00
if ($first_run) { $ignored++; }
2017-05-25 15:38:19 -07:00
}
}
}
2017-06-22 01:09:03 -07:00
// identify nodescend dirs
2018-08-08 23:12:47 -07:00
if (is_array($p['nodescend'])) {
foreach ($p['nodescend'] as $wildcard) {
2017-05-25 15:38:19 -07:00
if (fnmatch($wildcard, $current->getPath())) {
$clean = false;
2018-08-08 23:12:47 -07:00
if ($first_run) { $nodescended++; }
2017-05-25 15:38:19 -07:00
}
}
}
2017-06-22 01:09:03 -07:00
//tally stats
2018-08-08 23:12:47 -07:00
if ($clean && $first_run) {
2017-06-20 02:05:52 -07:00
if ($current->getType() == "file") {
$passed_file++;
} elseif ($current->getType() == "dir") {
$passed_dir++;
} elseif ($current->getType() == "link") {
$passed_link++;
}
2018-08-08 23:12:47 -07:00
$passed_total++;
2017-06-20 02:05:52 -07:00
}
2017-05-25 15:38:19 -07:00
return $clean;
}
),
RecursiveIteratorIterator::SELF_FIRST,
RecursiveIteratorIterator::CATCH_GET_CHILD
);
2018-08-08 23:12:47 -07:00
// Tally
2017-06-20 02:05:52 -07:00
//////////////////////////////////////////
2017-05-25 15:38:19 -07:00
2018-09-16 15:22:15 -07:00
echo msg("Counting files...");
2018-09-03 02:53:01 -07:00
2018-08-08 23:12:47 -07:00
foreach ($files as $null) { }
$first_run = 0;
2017-05-25 15:38:19 -07:00
2018-09-02 11:21:24 -07:00
if (!$passed_total) {
2018-09-16 15:22:15 -07:00
echo msg("Nothing was found, exiting");
2018-09-02 11:21:24 -07:00
die;
}
2018-09-16 15:22:15 -07:00
echo msg("Total files: ".$passed_total."");
2017-06-16 00:14:32 -07:00
2017-06-22 01:09:03 -07:00
// Pool DB
//////////////////////////////////////////
$dbp = new PDO("sqlite:".$bpath."/pool.sqlite3");
2018-09-14 13:50:27 -07:00
2017-06-22 01:09:03 -07:00
$dbp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
2018-09-14 13:50:27 -07:00
$dbp->query("PRAGMA page_size = 4096");
$dbp->query("PRAGMA cache_size = 10000");
$dbp->query("PRAGMA locking_mode = EXCLUSIVE");
$dbp->query("PRAGMA synchronous = NORMAL");
$dbp->query("PRAGMA journal_mode = WAL");
2017-06-22 01:09:03 -07:00
$dbp->exec("CREATE TABLE IF NOT EXISTS md5 (fid TEXT, hash TEXT)");
$dbp->exec("CREATE TABLE IF NOT EXISTS exiftool (fid TEXT, tags TEXT)");
$dbp->exec("CREATE TABLE IF NOT EXISTS mediainfo (fid TEXT, info TEXT)");
$dbp->exec("CREATE TABLE IF NOT EXISTS thumbs (fid TEXT, created INTEGER, relative_path TEXT, width INTEGER, height INTEGER, tool TEXT)");
2018-09-01 21:30:02 -07:00
$dbp->exec("CREATE TABLE IF NOT EXISTS contents (fid TEXT, created INTEGER, relative_path TEXT)");
2017-06-22 01:09:03 -07:00
2017-06-20 02:05:52 -07:00
// Prescan
//////////////////////////////////////////
2017-05-25 15:38:19 -07:00
2018-09-03 03:40:31 -07:00
$i = 0;
2017-06-20 02:05:52 -07:00
$family = array();
2018-09-03 03:40:31 -07:00
$fids = array();
$noread = array();
2018-08-08 23:12:47 -07:00
2018-09-13 01:50:00 -07:00
echo ProgressBar::start($passed_total,"Prescan (".stepString().")");
2018-08-08 23:12:47 -07:00
2017-05-25 15:38:19 -07:00
foreach ($files as $splFileInfo) {
$path = $splFileInfo->getPath();
2018-09-03 03:40:31 -07:00
$pathname = $splFileInfo->getPathname();
$shellpath = escapeshellarg($splFileInfo->getPathname());
$realpath = $splFileInfo->getRealPath();
2017-06-20 02:05:52 -07:00
$key = md5($pathname);
$pkey = md5($path);
if (array_key_exists($key, $family)) {
2018-09-16 15:22:15 -07:00
echo msg("Duplicate key on ".$pathname.""); die;
2017-05-20 20:03:50 -07:00
}
2017-06-16 00:14:32 -07:00
2017-06-20 02:05:52 -07:00
$family[$key] = array();
// Path-agnostic Unique File ID (to prevent redundant hashes and thumbs)
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
if ($splFileInfo->getType() != "dir" && $splFileInfo->getType() != "link") {
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
$fid = md5($splFileInfo->getSize().$splFileInfo->getMtime().$splFileInfo->getBasename());
$dx[$fid][] = $pathname;
$fx[] = array($fid, $pathname);
$family[$key]['fid'] = $fid;
2017-05-03 00:39:48 -07:00
}
2018-09-14 13:50:27 -07:00
// capture stat before values change
2017-06-20 02:05:52 -07:00
2018-09-14 13:50:27 -07:00
/*
2018-09-03 03:40:31 -07:00
if ($splFileInfo->getType() != "link") {
$stx[$i] = array( $splFileInfo->getATime(),
$splFileInfo->getMTime(),
$splFileInfo->getCTime() );
}
2018-09-14 13:50:27 -07:00
*/
2017-05-25 15:38:19 -07:00
2018-09-16 15:22:15 -07:00
//$sty[$i] = "bypass";
2018-09-14 13:50:27 -07:00
$sty[$i] = statToArray(shell_exec("stat -s ".$shellpath." 2>&1"));
2018-09-03 03:40:31 -07:00
// Check file can be read
2017-05-25 15:38:19 -07:00
2018-09-03 03:40:31 -07:00
if ($realpath && !is_readable($realpath)) {
$noread[] = $realpath;
}
2017-06-20 02:05:52 -07:00
// Children
2017-06-18 03:17:42 -07:00
2018-09-03 03:40:31 -07:00
//$family[$pkey]['children'][] = $key;
$family[$pkey]['children'][] = $i+1;
2017-06-18 03:17:42 -07:00
2018-09-13 01:50:00 -07:00
echo ProgressBar::next(true);
2018-09-03 03:40:31 -07:00
$i++;
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
}
2017-05-25 15:38:19 -07:00
2018-08-08 23:12:47 -07:00
echo ProgressBar::finish();
2017-05-25 15:38:19 -07:00
2018-09-03 03:40:31 -07:00
// Thow permissions error
if (count($noread)) {
2018-09-16 15:22:15 -07:00
echo msg("Current user (".posix_getuid().") does not have read access to the following files:");
2018-09-03 03:40:31 -07:00
foreach ($noread as $file) {
echo $file."\n";
}
if ($p['readability']) {
2018-09-16 15:22:15 -07:00
echo msg("Exiting...");
2018-09-03 03:40:31 -07:00
die;
}
}
2017-06-20 02:05:52 -07:00
// Debug record of duplicate FIDs
2018-08-08 23:12:47 -07:00
$dupes = array_filter($dx, function($a) { return count($a) > 1; });
$dxo = var_export($dupes, true);
2017-06-20 02:05:52 -07:00
if (strlen($dxo)) {
file_put_contents($bpath."/".$stamp."_dupes.txt",$dxo);
$dupecount = count($dupes,COUNT_RECURSIVE) - count($dupes);
2018-08-08 23:12:47 -07:00
echo floor(($dupecount/$passed_total)*100)." percent of files look like duplicates\n";
2017-06-20 02:05:52 -07:00
}
// Write family to DB
$message = "Writing family to DB: ";
$message .= $passed_file." files, ";
$message .= $passed_dir." dirs, ";
$message .= $nodescended." bundles, ";
$message .= $passed_link." links, ";
$message .= $ignored." ignored, ";
$message .= ($dupecount ? $dupecount : 0)." dupes";
2018-08-08 23:12:47 -07:00
echo ProgressBar::start(count($family),$message);
2017-06-20 02:05:52 -07:00
foreach ($family as $key => $item) {
$stmt = $dbo->prepare("INSERT INTO family VALUES (:pid, :fid, :children)");
$stmt->BindValue(":pid",$key);
if (@$item['fid']) {
$stmt->BindValue(":fid",$item['fid']);
2017-05-22 23:37:36 -07:00
}
2017-06-20 02:05:52 -07:00
if (@$item['children'] && is_array(@$item['children'])) {
$stmt->BindValue(":children",serialize($item['children']));
2017-05-03 00:39:48 -07:00
}
2017-05-25 15:38:19 -07:00
$stmt->execute();
2017-06-20 02:05:52 -07:00
2018-08-08 23:12:47 -07:00
echo ProgressBar::next();
2017-06-20 02:05:52 -07:00
}
echo ProgressBar::finish();
2019-04-09 01:13:49 -07:00
// create an index for family db
$dbo->exec("CREATE INDEX family_index ON family (pid)");
2017-06-20 02:05:52 -07:00
unset($dx, $dxo, $dupes);
// stats
2018-09-02 11:21:24 -07:00
$stmt = "UPDATE _skim SET ";
2017-06-20 02:05:52 -07:00
$stmt .= "passed_file=".$passed_file.", ";
$stmt .= "passed_dir=".$passed_dir.", ";
$stmt .= "passed_link=".$passed_link.", ";
2018-08-08 23:12:47 -07:00
$stmt .= "passed_total=".$passed_total.", ";
2017-06-20 02:05:52 -07:00
$stmt .= "nodescended=".$nodescended.", ";
$stmt .= "ignored=".$ignored.", ";
2017-06-22 01:09:03 -07:00
$stmt .= "dupes=".($dupecount ? $dupecount : 0);
2017-06-20 02:05:52 -07:00
$dbo->exec($stmt);
2018-09-13 01:50:00 -07:00
$wopt_currstep++;
2017-06-20 02:05:52 -07:00
// Thumbnails
//////////////////////////////////////////
2018-08-08 23:12:47 -07:00
if ($p['thumbs']) {
2017-06-20 02:05:52 -07:00
2018-09-13 01:50:00 -07:00
echo ProgressBar::start(count($fx),"Generating thumbnails (".stepString().")");
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
foreach ($fx as $array) {
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
$fid = $array[0];
$pathname = $array[1];
$ext = pathinfo($pathname,PATHINFO_EXTENSION);
$tpath = $bpath."/thumbs/".substr($fid, 0, 2);
2018-08-08 23:12:47 -07:00
if (!is_dir($tpath)) { mkdir($tpath); }
2017-06-20 02:05:52 -07:00
$tfile = $tpath."/".$fid.".jpg";
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
// HACK for ql-thumbnail bug
$t_skip = array("emlx");
if (count($t_skip) && in_array($ext, $t_skip)) {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Skipping ".shortlabel($pathname));
2017-06-20 02:05:52 -07:00
continue;
}
2017-05-25 15:38:19 -07:00
2017-06-22 01:09:03 -07:00
// if no thumb file, then poll database
2017-06-20 02:05:52 -07:00
if (file_exists($tfile)) {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Thumb file found for ".shortlabel($pathname));
2017-06-22 01:09:03 -07:00
continue;
} elseif ($dbp->query("SELECT EXISTS(SELECT 1 FROM thumbs WHERE fid='".$fid."')")->fetch()[0]) {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Thumb record found for ".shortlabel($pathname));
2017-06-20 02:05:52 -07:00
continue;
2017-05-25 15:38:19 -07:00
} else {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Generating thumb for ".shortlabel($pathname));
2017-05-25 15:38:19 -07:00
}
2017-06-22 01:09:03 -07:00
$stmt = $dbp->prepare("INSERT INTO thumbs VALUES (:fid, :created, :relative_path, :width, :height, :tool)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":created",time());
2017-06-18 03:17:42 -07:00
2017-06-20 02:05:52 -07:00
$shellpath = escapeshellarg($pathname);
// first try to make a thumb with external tools
$cmd = null;
2018-08-08 23:12:47 -07:00
if (in_array($ext, $t_files['sips'])) {
//$cmd = $bin_tv." ".$shellpath." -o ".$tfile."[Q=90,optimize_coding] --size=".$p['thumb_size'];
$cmd = "sips -s format jpeg -s formatOptions 80 --resampleHeightWidthMax ".$p['thumb_size']." ".$shellpath." --out ".$tfile;
$stmt->BindValue(":tool","sips");
2017-06-20 02:05:52 -07:00
} elseif (in_array($ext, $t_files['ffmpeg'])) {
2018-08-08 23:12:47 -07:00
//$cmd = $bin_tf." -i ".$shellpath." -o ".$tfile." -s ".$p['thumb_size']." -c jpg -q 8.5";
$cmd = $bin_ffmpeg." -ss $(( $(".$bin_mediainfo." --Inform='Video;%Duration%' ".$shellpath.") / 10000 )) -i ".$shellpath." -vframes 1 -filter:v scale='400:-1' -q:v 3 ".$tfile;
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":tool","ffmpeg");
2017-06-20 02:05:52 -07:00
}
if ($cmd) { shell_exec($cmd." 2>&1"); }
// if those tools failed, try quicklook
2018-08-08 23:12:47 -07:00
if (!@filesize($tfile)) {
//$cmd = $bin_qlthumb." ".$shellpath." ".$tfile." public.jpeg-2000 ".$p['thumb_size']." ".$p['thumb_size']." .8";
$cmd = $bin_qlthumb." ".$shellpath." ".$tfile." public.jpeg ".$p['thumb_size']." ".$p['thumb_size']." .8";
2017-06-20 02:05:52 -07:00
shell_exec($cmd." 2>&1");
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":tool","quicklook");
2017-06-20 02:05:52 -07:00
}
// success, move thumb into the bundle
2018-08-08 23:12:47 -07:00
if (file_exists($tfile) && @filesize($tfile)) {
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":relative_path",substr($tfile, strlen($bpath)));
list($width, $height) = getimagesize($tfile);
$stmt->BindValue(":width",$width);
$stmt->BindValue(":height",$height);
2017-06-20 02:05:52 -07:00
}
2017-05-25 15:38:19 -07:00
2017-06-22 01:09:03 -07:00
$stmt->execute();
2017-06-18 03:17:42 -07:00
}
2017-05-25 15:38:19 -07:00
2017-06-20 02:05:52 -07:00
echo ProgressBar::finish();
}
2018-09-14 13:50:27 -07:00
// Contents
//////////////////////////////////////////
if ($p['contents']) {
// should be rewritten to check against filemtimes
echo ProgressBar::start(count($fx),"Gathering contents (".stepString().")");
foreach ($fx as $array) {
$fid = $array[0];
$pathname = $array[1];
$ext = pathinfo($pathname,PATHINFO_EXTENSION);
$cpath = $bpath."/contents/".substr($fid, 0, 2);
$cfile = $cpath."/".$fid.".zip";
if (in_array($ext, $c_files)) {
if (!is_dir($cpath)) { mkdir($cpath); }
if (!file_exists($cfile) && filesize($pathname) < 25000) {
2018-09-16 15:22:15 -07:00
msg("Zipping ".$pathname);
$zip = new ZipArchive();
$zip->open($cfile, ZipArchive::CREATE);
$zip->addfile($pathname,basename($pathname));
$zip->close();
2018-09-14 13:50:27 -07:00
$stmt = $dbp->prepare("INSERT INTO contents VALUES (:fid, :created, :relative_path)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":created",time());
$stmt->BindValue(":relative_path",substr($cfile, strlen($bpath)));
$stmt->execute();
2018-09-16 15:22:15 -07:00
2018-09-14 13:50:27 -07:00
}
}
echo ProgressBar::next(true);
}
echo ProgressBar::finish();
}
2017-06-22 01:09:03 -07:00
// External metadata
//////////////////////////////////////////
2018-08-08 23:12:47 -07:00
if ($p['meta']) {
2017-06-22 01:09:03 -07:00
2018-09-13 01:50:00 -07:00
echo ProgressBar::start(count($fx),"Collecting external metadata (".stepString().")");
2017-06-22 01:09:03 -07:00
foreach ($fx as $array) {
$fid = $array[0];
$pathname = $array[1];
$shellpath = escapeshellarg($pathname);
$ext = pathinfo($pathname,PATHINFO_EXTENSION);
2018-09-03 03:40:31 -07:00
$found = 1;
2017-06-22 01:09:03 -07:00
if (!in_array($ext, $e_files) && !in_array($ext, $m_files)) {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Not a media file: ".shortlabel($pathname));
2017-06-22 01:09:03 -07:00
continue;
}
2018-09-13 01:50:00 -07:00
2017-06-22 01:09:03 -07:00
if (in_array($ext, $e_files)) {
$check = $dbp->query("SELECT EXISTS(SELECT 1 FROM exiftool WHERE fid='".$fid."')")->fetch()[0];
if (!$check) {
2018-09-13 01:50:00 -07:00
$arrstring = shell_exec($bin_exiftool." -php ".$shellpath);
// $rawexif = eval("return ".`$bin_exiftool -php $shellpath`);
// do an addtl check below to prevent "PHP Parse error: syntax error, unexpected end of file, expecting ';'"
if (substr($arrstring,0,5) == "Array") {
$rawexif = eval("return ".$arrstring);
$stmt = $dbp->prepare("INSERT INTO exiftool VALUES (:fid, :tags)");
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":tags",serialize($rawexif[0]));
$stmt->execute();
$found = 0;
}
2017-06-22 01:09:03 -07:00
}
}
2018-09-13 01:50:00 -07:00
2017-06-22 01:09:03 -07:00
if (in_array($ext, $m_files)) {
$check = $dbp->query("SELECT EXISTS(SELECT 1 FROM mediainfo WHERE fid='".$fid."')")->fetch()[0];
if (!$check) {
$stmt = $dbp->prepare("INSERT INTO mediainfo VALUES (:fid, :info)");
$stmt->BindValue(":fid",$fid);
2018-09-03 02:53:01 -07:00
//$stmt->BindValue(":info",serialize(parseMediaInfo(shell_exec($bin_mediainfo." --Output=OLDXML ".$shellpath." 2>&1"))));
$stmt->BindValue(":info",shell_exec($bin_mediainfo." --Output=OLDXML ".$shellpath." 2>&1"));
2017-06-22 01:09:03 -07:00
$stmt->execute();
2018-09-03 03:40:31 -07:00
$found = 0;
2017-06-22 01:09:03 -07:00
}
}
2018-09-03 03:40:31 -07:00
if ($found) {
echo ProgressBar::next("Metadata found: ".shortlabel($pathname));
} else {
echo ProgressBar::next("Collecting metadata: ".shortlabel($pathname));
}
2017-06-22 01:09:03 -07:00
}
echo ProgressBar::finish();
}
2017-06-20 02:05:52 -07:00
// Hashes
//////////////////////////////////////////
2018-08-08 23:12:47 -07:00
if ($p['hash']) {
2017-05-25 15:38:19 -07:00
2018-08-08 23:12:47 -07:00
if ($p['hash_limit']) {
$message = "Generating hashes for files under ".$p['hash_limit']."GB";
2017-06-20 02:05:52 -07:00
} else {
$message = "Generating hashes for all files";
}
2017-06-18 03:17:42 -07:00
2018-09-13 01:50:00 -07:00
echo ProgressBar::start(count($fx),$message." (".stepString().")");
2017-06-20 02:05:52 -07:00
foreach ($fx as $array) {
$fid = $array[0];
$pathname = $array[1];
$size = filesize($pathname);
2018-08-08 23:12:47 -07:00
$limit = $p['hash_limit']*1000000000;
2017-06-22 01:09:03 -07:00
$check = $dbp->query("SELECT EXISTS(SELECT 1 FROM md5 WHERE fid='".$fid."')")->fetch()[0];
2017-06-20 02:05:52 -07:00
if ($check) {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Hash already exists: ".shortlabel($pathname));
} elseif ($p['hash_limit'] && ($size > $limit)) {
echo ProgressBar::next("Too big to hash: ".shortlabel($pathname)." (".human_filesize($size).")");
2017-06-20 02:05:52 -07:00
} else {
2018-08-08 23:12:47 -07:00
echo ProgressBar::next("Generating hash: ".shortlabel($pathname));
2017-06-22 01:09:03 -07:00
$stmt = $dbp->prepare("INSERT INTO md5 VALUES (:fid, :hash)");
2017-06-20 02:05:52 -07:00
$stmt->BindValue(":fid",$fid);
$stmt->BindValue(":hash",md5_file($pathname));
$stmt->execute();
2017-06-18 03:17:42 -07:00
}
2017-05-25 15:38:19 -07:00
}
2017-06-20 02:05:52 -07:00
echo ProgressBar::finish();
2017-05-25 15:38:19 -07:00
2017-05-20 20:03:50 -07:00
}
2018-09-16 15:22:15 -07:00
// Pool Indices
//////////////////////////////////////////
// We are done with the Pool DB, make sure there are indices
$dbp->exec("CREATE INDEX IF NOT EXISTS contents_index ON contents (fid)");
$dbp->exec("CREATE INDEX IF NOT EXISTS exiftool_index ON exiftool (fid)");
$dbp->exec("CREATE INDEX IF NOT EXISTS md5_index ON md5 (fid)");
$dbp->exec("CREATE INDEX IF NOT EXISTS mediainfo_index ON mediainfo (fid)");
$dbp->exec("CREATE INDEX IF NOT EXISTS thumbs_index ON thumbs (fid)");
2018-09-02 11:21:24 -07:00
// Spotlight
//////////////////////////////////////////
$mb['i'] = array( "PixelWidth",
"PixelHeight",
"Latitude",
"Longitude",
"DurationSeconds",
"UseCount",
"FSInvisible",
"NumberOfPages",
"PageHeight",
2019-04-27 00:48:25 -07:00
"PageWidth",
"TotalBitRate" );
2018-09-02 11:21:24 -07:00
$mb['t'] = array( "Title",
"ContentType",
"Creator",
"FSCreatorCode",
"Kind",
"FSTypeCode" );
$mb['a'] = array( "UserTags",
"WhereFroms",
"EncodingApplications" );
$mb['d'] = array( "DateAdded",
"LastUsedDate",
"ContentModificationDate",
"ContentCreationDate" );
$ibuild[] = ":pid, :spotlight";
$cbuild[] = "pid TEXT, spotlight TEXT";
foreach (array_merge($mb['i'],$mb['d']) as $item) {
$cbuild[] = $item." INTEGER";
$ibuild[] = ":".$item;
}
foreach (array_merge($mb['t'],$mb['a']) as $item) {
$cbuild[] = $item." TEXT";
$ibuild[] = ":".$item;
}
$p['spotlight'] = 1;
if ($p['spotlight']) {
2018-09-13 01:50:00 -07:00
echo ProgressBar::start($passed_total,"Spotlight (".stepString().")");
2018-09-02 11:21:24 -07:00
$dbo->exec("CREATE TABLE mdls (".implode(",",$cbuild).")");
foreach ($files as $splFileInfo) {
2018-09-13 01:50:00 -07:00
$path = $splFileInfo->getPathname();
2018-09-16 15:22:15 -07:00
msg($path);
2018-09-13 01:50:00 -07:00
$pid = md5($path);
$shellpath = escapeshellarg($path);
2018-09-02 11:21:24 -07:00
$mdls = shell_exec("mdls -plist - ".$shellpath." 2>&1");
2018-09-16 15:22:15 -07:00
if (substr_count(@$mdls,"\n") > 1) {
$spotlight = $parser->parseString(utf8_for_xml($mdls));
} else {
$spotlight = array();
}
2018-09-02 11:21:24 -07:00
$stmt = $dbo->prepare("INSERT INTO mdls VALUES (".implode(",",$ibuild).")");
foreach ($mb as $key => $list) {
foreach ($list as $item) {
if (@$spotlight["kMDItem".$item]) {
$stmt->BindValue(":pid",$pid);
$stmt->BindValue(":spotlight",$mdls);
switch($key) {
case "i":
case "t":
$stmt->BindValue(":".$item,$spotlight["kMDItem".$item]);
break;
case "a":
$stmt->BindValue(":".$item,serialize($spotlight["kMDItem".$item]));
break;
case "d":
$stmt->BindValue(":".$item,strtotime($spotlight["kMDItem".$item]));
break;
}
}
}
}
$stmt->execute();
2018-09-13 01:50:00 -07:00
echo ProgressBar::next(true);
2018-09-02 11:21:24 -07:00
}
echo ProgressBar::finish();
}
2017-06-20 02:05:52 -07:00
// Files
//////////////////////////////////////////
2017-06-18 03:17:42 -07:00
2018-09-01 21:30:02 -07:00
$j = 0;
2018-09-13 01:50:00 -07:00
echo ProgressBar::start($passed_total, "Skimming");
2017-06-22 01:09:03 -07:00
foreach ($files as $splFileInfo) {
// DB
2019-04-27 00:48:25 -07:00
$stmt = $dbo->prepare("INSERT INTO files VALUES (:pid, :fid, :Pathname, :Path, :Filename, :Extension, :Type, :Size, :Inode, :Perms, :Owner, :ATime, :MTime, :CTime, :LinkTarget, :RealPath, :stat, :items, :newest, :fkind, :gfi_type, :gfi_attr, :gfi_created, :has_exif, :has_mediainfo, :has_hash, :thumb_filename, :thumb_width, :thumb_height, :contents_filename)");
2017-06-22 01:09:03 -07:00
// Identify dir, file, link or bundle dir
$type = $splFileInfo->getType();
if ($type == "dir") {
2018-08-08 23:12:47 -07:00
foreach ($p['bundles'] as $bundle) {
2017-06-22 01:09:03 -07:00
$check = ".".$bundle;
if (substr($splFileInfo->getFilename(), -(strlen($check)), strlen($check)) == $check) { $type = "bundle"; }
}
}
$stmt->BindValue(":Type",$type);
// Path basics
$pathname = $splFileInfo->getPathname();
$path = $splFileInfo->getPath();
$filename = $splFileInfo->getFilename();
$extension = $splFileInfo->getExtension();
$shellpath = escapeshellarg($pathname);
$stmt->BindValue(":Pathname",$pathname);
$stmt->BindValue(":Path",$path);
$stmt->BindValue(":Filename",$filename);
$stmt->BindValue(":Extension",$extension);
2018-09-05 00:26:26 -07:00
//stat
2018-09-14 13:50:27 -07:00
$stmt->BindValue(":stat",serialize($sty[$j]));
2018-09-01 21:30:02 -07:00
2017-06-22 01:09:03 -07:00
if ($type == "link") {
$stmt->BindValue(":LinkTarget",$splFileInfo->getLinkTarget());
$stmt->BindValue(":RealPath",$splFileInfo->getRealPath());
} else {
$stmt->BindValue(":Inode",$splFileInfo->getInode());
$stmt->BindValue(":Perms",$splFileInfo->getPerms());
$stmt->BindValue(":Owner",$splFileInfo->getOwner().":".$splFileInfo->getGroup());
2018-09-14 13:50:27 -07:00
$stmt->BindValue(":ATime",$splFileInfo->getATime());
$stmt->BindValue(":MTime",$splFileInfo->getMTime());
$stmt->BindValue(":CTime",$splFileInfo->getCTime());
2017-06-22 01:09:03 -07:00
}
// ------------------------------------------------ //
// Generate PID and FID
$pid = md5($pathname);
$stmt->BindValue(":pid",$pid);
if ($type == "file") {
$fid = md5($splFileInfo->getSize().$splFileInfo->getMtime().$splFileInfo->getBasename());
$stmt->BindValue(":fid",$fid);
}
// Size
if ($type == "dir" || $type == "bundle") {
$size = trim(shell_exec("du -ks ".$shellpath." | cut -f1"))*1024;
} elseif ($type == "file") {
$size = $splFileInfo->getSize();
} else {
$size = null;
}
$stmt->BindValue(":Size",@$size);
// ------------------------------------------------ //
// Items
if ($type == "dir" || $type == "bundle" ) {
2018-09-16 15:22:15 -07:00
// below commented out because it was causing -1 on dirs beginning with a dot
//$items = chop(@shell_exec("find ".$shellpath." \( ! -regex '.*/\..*' \) | wc -l 2>&1"))-1;
// below should be rewritten to use $wopt_ignore files
$items = chop(@shell_exec("find ".$shellpath." \( ! -regex '.*/\.DS_Store' \) | wc -l 2>&1"))-1;
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":items",@$items);
}
// ------------------------------------------------ //
2017-05-25 15:38:19 -07:00
2017-06-22 01:09:03 -07:00
// Newest
if ($type == "dir") {
$newest = @filemtime(chop(shell_exec("find ".$shellpath." -type f -not -path '*/\.*' -print0 | xargs -0 stat -f \"%m %N\" | sort -rn 2>&1 | head -1 | cut -f2- -d\" \"")));
$stmt->BindValue(":newest",@$newest);
}
// ------------------------------------------------ //
// GetFileInfo
$gfiparts = explode("\n", chop(shell_exec($bin_gfi." -P ".$shellpath." 2>&1")));
if (is_array($gfiparts)) {
foreach ($gfiparts as $line) {
list($label, $value) = explode(": ", $line);
$gfi[$label] = isset($value) ? trim($value,"\"") : null;
}
}
$writegfitype = @$gfi['type'].":".@$gfi['creator'];
if ($writegfitype == "\\0\\0\\0\\0:\\0\\0\\0\\0" || $writegfitype == ":") { $writegfitype = null; }
$stmt->BindValue(":gfi_type",$writegfitype);
$stmt->BindValue(":gfi_attr",@$gfi['attributes']);
2019-04-09 01:13:49 -07:00
$stmt->BindValue(":gfi_created",strtotime(@$gfi['created']));
2019-04-27 00:48:25 -07:00
// ------------------------------------------------ //
// Kind
unset($fkind);
if ($type == "file") {
$fkind = trim(shell_exec("file -b -p ".$shellpath." | cut -f1 -d,"));
$stmt->BindValue(":fkind",@$fkind);
}
2017-06-22 01:09:03 -07:00
// ------------------------------------------------ //
// Pool
if ($type == "file") {
unset($fetch_exif, $fetch_media, $fetch_hash, $fetch_thumb, $yes_exif, $yes_media, $yes_hash);
2018-09-14 13:50:27 -07:00
$yes_exif = $dbp->query("SELECT rowid FROM exiftool WHERE fid='".$fid."'")->fetch()[0];
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":has_exif",$yes_exif);
2018-09-14 13:50:27 -07:00
$yes_media = $dbp->query("SELECT rowid FROM mediainfo WHERE fid='".$fid."'")->fetch()[0];
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":has_mediainfo",$yes_media);
2018-09-14 13:50:27 -07:00
$yes_hash = $dbp->query("SELECT rowid FROM md5 WHERE fid='".$fid."'")->fetch()[0];
2017-06-22 01:09:03 -07:00
$stmt->BindValue(":has_hash",$yes_hash);
2018-09-14 13:50:27 -07:00
$yes_contents = $dbp->query("SELECT relative_path FROM contents WHERE fid='".$fid."'")->fetch()[0];
$stmt->BindValue(":contents_filename",$yes_contents);
2018-09-02 11:21:24 -07:00
2017-06-22 01:09:03 -07:00
$fetch_thumb = $dbp->query("SELECT * FROM thumbs WHERE fid='".$fid."'")->fetch();
if (@$fetch_thumb['relative_path']) {
$stmt->BindValue(":thumb_filename",$fetch_thumb['relative_path']);
$stmt->BindValue(":thumb_width",$fetch_thumb['width']);
$stmt->BindValue(":thumb_height",$fetch_thumb['height']);
} else {
$stmt->BindValue(":thumb_filename",null);
}
}
2018-09-02 11:21:24 -07:00
// ------------------------------------------------ //
2017-06-22 01:09:03 -07:00
// Write to DB
$stmt->execute();
// Double check stat for file against pre-run value
2018-09-03 03:40:31 -07:00
if ($p['verify_stat'] && $type != "link") {
2017-06-22 01:09:03 -07:00
2018-09-14 13:50:27 -07:00
$restat = statToArray(shell_exec("stat -s ".$shellpath." 2>&1"));
2017-06-22 01:09:03 -07:00
$message = array();
2018-09-14 13:50:27 -07:00
if ($sty[$j]['st_atime'] != $restat['st_atime']) {
if ($p['fixatimes'] && $type != "link" && is_writable($pathname)) {
exec("touch -at `date -r ".$sty[$j]['st_atime']." +%Y%m%d%H%M.%S` ".$shellpath." 2>&1");
$message[] = "atime (fix)";
} else {
$message[] = "atime";
}
2017-06-22 01:09:03 -07:00
}
2018-09-14 13:50:27 -07:00
if ($sty[$j]['st_mtime'] != $restat['st_mtime']) {
$message[] = "mtime";
2017-06-22 01:09:03 -07:00
}
2018-09-14 13:50:27 -07:00
if ($sty[$j]['st_ctime'] != $restat['st_ctime']) {
$message[] = "ctime";
2017-06-22 01:09:03 -07:00
}
2018-09-16 15:22:15 -07:00
if (count($message)) { echo msg("FILE = ".$filename."; CHANGE = ".implode(", ", $message).""); }
2017-06-22 01:09:03 -07:00
}
2018-09-16 15:22:15 -07:00
echo ProgressBar::next($pathname);
2018-09-01 21:30:02 -07:00
$j++;
2017-06-22 01:09:03 -07:00
}
echo ProgressBar::finish();
2017-05-25 15:38:19 -07:00
2018-09-14 13:50:27 -07:00
// Milk
//////////////////////////////////////////
2019-04-27 00:48:25 -07:00
$milk['t*DocTitle'] = ["e^Title","k^Title","m^Track_name"];
2018-09-16 15:22:15 -07:00
$milk['t*Format'] = ["m^Format","e^Compression","e^MIMEType"];
2019-04-27 00:48:25 -07:00
$milk['t*Dimensions'] = ["k^PixelWidth.k^PixelHeight","e^PixelWidth.e^PixelHeight","m^SkimDims","k^SkimPageDims"];
2018-09-16 15:22:15 -07:00
$milk['s*Seconds'] = ["k^DurationSeconds","e^Duration","m^Duration"];
$milk['d*DateTime'] = ["e^DateTimeOriginal","m^EncodedDate","e^CreateDate","e^MediaCreateDate","k^ContentCreationDate"];
$milk['t*Origin'] = ["e^CameraModelName","e^Producer","e^CreatorTool","e^WriterName","e^Software","e^Encoder","k^Creator"];
$milk['t*GPS'] = ["k^Latitude.k^Longitude","e^GPSPosition"];
2019-04-27 00:48:25 -07:00
$milk['t*Author'] = ["e^Author","e^Artist","e^Creator","e^By-line","k^Copyright"];
2018-09-16 15:22:15 -07:00
2019-04-27 00:48:25 -07:00
$milk['i*Tracks'] = ["m^SkimTrackCount","k^NumberOfPages"];
2018-09-16 15:22:15 -07:00
$milk['t*Writer'] = ["m^Writing_application.m^Writing_library"];
2019-04-27 00:48:25 -07:00
$milk['t*Bitrate'] = ["m^Overall_bit_rate","e^AvgBitrate","k^TotalBitRate"];
2018-09-16 15:22:15 -07:00
2019-04-27 00:48:25 -07:00
//$milk['i*Orientation'] = ["e^Orientation"];
//$milk['t*Profile'] = ["e^Profile"];
//$milk['i*BitDepth'] = ["e^BitDepth"];
//$milk['t*LensType'] = ["e^LensType"];
//$milk['t*FocalLength'] = ["e^FocalLength"];
//$milk['t*Aperture'] = ["e^Aperture"];
//$milk['t*LightSource'] = ["e^LightSource"];
//$milk['t*WhiteBalance'] = ["e^WhiteBalance"];
2018-09-16 15:22:15 -07:00
$delimiter = ",";
2019-04-27 00:48:25 -07:00
$display_delimiter = " x ";
2018-09-14 13:50:27 -07:00
// Build DB
2018-09-16 15:22:15 -07:00
$cbuild = $ibuild = array();
foreach (array_keys($milk) as $name) {
list($kind,$item) = explode("*",$name);
switch ($kind) {
case "t":
case "d":
case "s":
$cbuild[] = $item." TEXT";
break;
case "i":
$cbuild[] = $item." INTEGER";
break;
2018-09-14 13:50:27 -07:00
}
2018-09-16 15:22:15 -07:00
$ibuild[] = ":".$item;
2018-09-14 13:50:27 -07:00
}
$dbo->exec("CREATE TABLE milk (".implode(",",$cbuild).")");
2018-09-16 15:22:15 -07:00
$countrows = @reset($dbo->query("SELECT max(rowid) FROM files")->fetch());
echo msg("Milking ".$countrows." rows");
echo ProgressBar::start($countrows, "Milk");
$loop = $dbo->query("SELECT rowid, * FROM files");
while ($row_a = $loop->fetch()) {
$stmt = $dbo->prepare("INSERT INTO milk VALUES (".implode(",",$ibuild).")");
$row_b = @$dbo->query("SELECT * FROM mdls WHERE (rowid='".$row_a['rowid']."')")->fetch();
if (count(@$row_b) > 1) {
$m['k'] = $row_b;
2019-04-27 00:48:25 -07:00
//custom values
if ($m['k']['PageWidth'] && $m['k']['PageHeight']) {
$m['k']['SkimPageDims'] = round($m['k']['PageWidth']/72,2)."in".$display_delimiter.round($m['k']['PageHeight']/72,2)."in";
}
2018-09-16 15:22:15 -07:00
} else {
$m['k'] = null;
}
if (isset($row_a['has_exif'])) {
$row_c = $dbp->query("SELECT * FROM exiftool WHERE (rowid='".$row_a['has_exif']."')")->fetch();
$m['e'] = unserialize($row_c['tags']);
} else {
$m['e'] = null;
}
if (isset($row_a['has_mediainfo'])) {
$row_d = $dbp->query("SELECT * FROM mediainfo WHERE (rowid='".$row_a['has_mediainfo']."')")->fetch();
$decoded = @json_decode(json_encode(simplexml_load_string($row_d['info'])),true);
$m['m'] = $decoded['File']['track'][0];
//custom values
$m['m']['SkimTrackCount'] = @count($decoded['File']['track']);
2019-04-09 01:13:49 -07:00
foreach (@$decoded['File']['track'] as $track) {
2018-09-16 15:22:15 -07:00
if (!@$m['m']['SkimDims'] && @$track['Width'] && @$track['Height']) {
2019-04-27 00:48:25 -07:00
$m['m']['SkimDims'] = sanitize($track['Width'],"i").$display_delimiter.sanitize($track['Height'],"i");
2018-09-16 15:22:15 -07:00
}
}
// do seconds fix here
} else {
$m['m'] = null;
}
// M*I*L*K baby
foreach ($milk as $value => $weighted) {
list($type,$name) = explode("*",$value);
$found = 0;
foreach ($weighted as $dindex) {
// concatenante 2 values
if (!$found && strpos($dindex, ".")) {
$parts = explode(".",$dindex);
$out = array();
foreach ($parts as $part) {
list($kind,$item) = explode("^",$part);
if (@$m[$kind][$item]) {
$out[] = sanitize($m[$kind][$item],$type);
}
}
if (count($out)) {
$stmt->BindValue(":".$name,implode($delimiter,$out));
$found = 1;
}
} elseif (!$found) {
// find a single value
list($kind,$item) = explode("^",$dindex);
if (@$m[$kind][$item]) {
$stmt->BindValue(":".$name,sanitize($m[$kind][$item],$type));
$found = 1;
}
}
}
}
echo ProgressBar::next(true);
$stmt->execute();
}
2018-09-14 13:50:27 -07:00
2018-09-16 15:22:15 -07:00
echo ProgressBar::finish();
2018-09-14 13:50:27 -07:00
2017-06-20 02:05:52 -07:00
// Cleanup
//////////////////////////////////////////
2018-09-16 15:22:15 -07:00
echo msg("");
2017-06-20 02:05:52 -07:00
2018-09-02 11:21:24 -07:00
if (file_exists($error_log_file)) { echo file_get_contents($error_log_file); }
2017-06-20 02:05:52 -07:00
$seconds = floor($time = microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"]);
2018-09-02 11:21:24 -07:00
$dbo->exec("UPDATE _skim SET status='completed_in_".$seconds."'");
2018-09-05 00:26:26 -07:00
// rsync
if ($p['rsync_dest']) {
2018-09-16 15:22:15 -07:00
echo msg("rsync...");
2018-09-05 00:26:26 -07:00
$command = "rsync -avv -e ssh ".$bpath." ".$p['rsync_dest'];
$count = trim(shell_exec("find ".escapeshellarg($bpath)." | wc -l"));
2018-09-14 13:50:27 -07:00
echo ProgressBar::start($count,$p['rsync_dest']);
2018-09-05 00:26:26 -07:00
$pipe = popen($command, "r");
2018-09-14 13:50:27 -07:00
while(fgets($pipe, 2048)) { echo ProgressBar::next(true); }
2018-09-05 00:26:26 -07:00
pclose($pipe);
echo ProgressBar::finish();
}
2019-04-09 01:13:49 -07:00
$done = "Finished ".$zpath." in ".$seconds." seconds";
echo msg($done); ncenter($done);
2017-05-25 15:38:19 -07:00
2017-06-22 01:09:03 -07:00
unset($dbo, $dbp, $files, $family, $fx);
2017-05-03 00:39:48 -07:00
?>