Files
Yuba/functions.php

293 lines
9.4 KiB
PHP
Raw Normal View History

2018-08-08 23:12:47 -07:00
<?php
2019-06-11 02:00:18 -07:00
// Classes & Functions
2018-09-01 21:30:02 -07:00
//////////////////////////////////////////
class ProgressBar {
2018-09-13 01:50:00 -07:00
protected static $time_start;
protected static $time_remain;
protected static $time_update;
protected static $total;
protected static $done;
2019-06-10 22:04:25 -07:00
protected static $noline;
2018-09-13 01:50:00 -07:00
protected static $message;
2018-09-01 21:30:02 -07:00
public static function display($message = null) {
2018-09-13 01:50:00 -07:00
$progress = self::$done/self::$total;
$string = "PROGRESS:".round($progress*100,2);
if ($message === true) {
$message = self::$message;
}
if ($message) {
if (time()-self::$time_update) { // only update the remaining time every 1 seconds
$seconds = time() - self::$time_start;
self::$time_remain = floor($seconds/$progress)-$seconds;
self::$time_update = time();
}
2018-09-14 13:50:27 -07:00
$message = gmdate("H:i:s",self::$time_remain)." ".$message;
2018-09-13 01:50:00 -07:00
}
2018-09-01 21:30:02 -07:00
if ($message) {
2018-09-02 11:21:24 -07:00
return "\n".$string."\n".$message;
2018-09-01 21:30:02 -07:00
} elseif (!strpos(__FILE__,".app")) {
2019-06-10 22:04:25 -07:00
// if script is run outside app bundle, attempt to print progress inline
2018-09-01 21:30:02 -07:00
return "\r\033[K\r".$string;
2019-06-10 22:04:25 -07:00
} elseif (self::$noline) {
return $string;
2018-09-01 21:30:02 -07:00
} else {
return "\n".$string;
}
2018-09-13 01:50:00 -07:00
2018-09-01 21:30:02 -07:00
}
2019-06-10 22:04:25 -07:00
public static function start($total, $message = null, $noline = false) {
self::$noline = $noline;
2018-09-13 01:50:00 -07:00
self::$done = 0;
2018-09-01 21:30:02 -07:00
self::$total = $total;
2018-09-13 01:50:00 -07:00
self::$time_start = time();
self::$message = $message;
2018-09-16 15:22:15 -07:00
return msg($message).self::display();
2018-09-01 21:30:02 -07:00
}
public static function next($message = null) {
self::$done++;
2018-09-16 15:22:15 -07:00
if (is_string($message)) { msg($message); }
2018-09-01 21:30:02 -07:00
return self::display($message);
2018-09-16 15:22:15 -07:00
}
2018-09-01 21:30:02 -07:00
2019-06-12 23:26:54 -07:00
public static function finish($clear = null) {
2018-09-13 01:50:00 -07:00
global $wopt_currstep;
$wopt_currstep++;
2018-09-01 21:30:02 -07:00
self::$done = 0;
2019-06-12 23:26:54 -07:00
if ($clear) {
return "\nREFRESH\n";
} else {
return "\n".str_repeat("*",99)."\n";
}
2018-09-01 21:30:02 -07:00
}
}
2018-08-08 23:12:47 -07:00
// Functions
//////////////////////////////////////////
2019-10-19 05:54:29 -07:00
function globext($array) {
$globparts = array();
foreach ($array as $ext) {
$globstring = "";
$parts = str_split($ext);
foreach ($parts as $char) {
$globstring .= "[".$char.strtoupper($char)."]";
}
$globparts[] = $globstring;
}
return "{".implode(",",$globparts)."}";
}
function globstring($array,$base = "*") {
if (!is_array($base)) {
return $base.".".globext($array);
} else {
$globparts = array();
foreach ($base as $file) {
$globparts[] = $file;
$globparts[] = strtoupper($file);
$globparts[] = ucfirst($file);
}
return "{".implode(",",$globparts)."}".".".globext($array);
}
}
2019-10-09 04:13:49 -07:00
function revise_prefs($array) { // bad practice
2019-10-09 04:07:46 -07:00
$prefs_file = "/Users/".get_current_user()."/Library/Preferences/org.profiteroles.Yuba.php";
$p = unserialize(file_get_contents($prefs_file));
foreach ($array as $key => $value) { $p[$key] = "value"; }
2019-10-09 04:18:29 -07:00
file_put_contents($prefs_file,serialize($p));
2019-10-09 04:07:46 -07:00
}
2019-06-10 16:32:09 -07:00
function prettysize($size) {
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
2019-05-22 04:02:22 -07:00
function alert($string, $title = "Warning") {
echo "\nALERT:".$title."|".$string."\n";
}
2019-06-02 02:50:28 -07:00
function notification($string) {
echo "\nNOTIFICATION:".$string."\n";
}
2019-05-22 04:02:22 -07:00
function askMulti($string, $buttons) {
$buttonstring = "buttons {\\\"".implode("\\\", \\\"",$buttons)."\\\"} default button ".count($buttons);
$result = exec("osascript -e \"display dialog \\\"".$string."\\\" ".$buttonstring."\" | cut -f2 -d':'");
return array_search($result,$buttons);
}
2019-10-15 18:32:21 -07:00
function msg($string, $extra=true) {
2018-09-16 15:22:15 -07:00
global $messages_log_file;
$logstring = "[".date('Y-m-d h:i:s')."] ".$string."\n";
file_put_contents($messages_log_file, $logstring, FILE_APPEND);
2019-10-15 18:32:21 -07:00
if ($extra) { file_put_contents("/tmp/yuba/debug.log", $logstring, FILE_APPEND); }
2018-09-16 15:22:15 -07:00
return $string."\n";
}
2019-10-09 04:07:46 -07:00
function dm($string) {
global $p; if (!$p['debug']) { return; } // bad practice
if (strpos($string,"\n") === false) {
$logstring = "[".date('Y-m-d h:i:s')."] ".$string."\n";
} else {
$logstring = "[".date('Y-m-d h:i:s')."]\n".$string."\n".str_repeat("-",33)."\n";
}
file_put_contents("/tmp/yuba/debug.log", $logstring, FILE_APPEND);
}
2018-09-16 15:22:15 -07:00
function timeToSeconds($val) {
if (!is_numeric($val) && strpos($val,":") === false) {
$val = str_replace(" s","",$val);
$val = str_replace(" h ",":",$val);
$val = str_replace(" min",":00",$val);
}
if (!is_numeric($val)) {
$sec = 0;
foreach (array_reverse(explode(':', $val)) as $k => $v) $sec += pow(60, $k) * $v;
$val = $sec;
}
return number_format($val,2,".","");
}
function sanitize($val,$type) {
switch($type) {
case "t":
return $val;
break;
case "i":
return filter_var($val,FILTER_SANITIZE_NUMBER_FLOAT);
break;
case "s":
return timeToSeconds($val);
break;
case "d":
if ($formatted = @strtotime($val)) {
return $formatted;
} else {
return $val;
}
break;
}
}
function is_serial($string) {
return (@unserialize($string) !== false || $string == 'b:0;');
}
2018-09-14 13:50:27 -07:00
function statToArray($stat) {
foreach (explode(" ",$stat) as $part) {
$value = explode("=",$part);
$out[$value[0]] = $value[1];
}
return $out;
}
2018-09-13 01:50:00 -07:00
function stepString() {
global $wopt_steps;
global $wopt_currstep;
return "Step ".$wopt_currstep." of ".$wopt_steps;
2018-09-05 00:26:26 -07:00
}
2018-09-13 01:50:00 -07:00
function shortlabel($pathname, $max = 99) {
2018-08-08 23:12:47 -07:00
$basename = basename($pathname);
$suffix = "(...).".pathinfo($basename,PATHINFO_EXTENSION);
if (strlen($basename) > $max) {
$return = substr($basename, 0, ($max-strlen($suffix))).$suffix;
} else {
$return = $basename;
}
2019-10-19 05:54:29 -07:00
return str_replace("\r","",$return); // filter out weird carriage returns in Icon filenames
2018-08-08 23:12:47 -07:00
}
function human_filesize($bytes, $decimals = 2) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
2018-09-16 15:22:15 -07:00
function utf8_for_xml($string) {
return preg_replace ('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $string);
}
2018-08-08 23:12:47 -07:00
class plistParser extends XMLReader {
public function parseString($string) { $this->XML($string); return $this->process(); }
private function process() {
$this->read();
if($this->nodeType !== XMLReader::DOC_TYPE || $this->name !== "plist") { throw new Exception(sprintf("Error parsing plist. nodeType: %d -- Name: %s", $this->nodeType, $this->name), 2); }
if(!$this->next("plist") || $this->nodeType !== XMLReader::ELEMENT || $this->name !== "plist") { throw new Exception(sprintf("Error parsing plist. nodeType: %d -- Name: %s", $this->nodeType, $this->name), 3); }
$plist = array(); while($this->read()) { if($this->nodeType == XMLReader::ELEMENT) { $plist[] = $this->parse_node(); } }
if(count($plist) == 1 && $plist[0]) { return $plist[0]; } else { return $plist; }
}
private function parse_node() {
if($this->nodeType !== XMLReader::ELEMENT) return;
switch($this->name) {
case 'data': return base64_decode($this->getNodeText()); break;
case 'real': return floatval($this->getNodeText()); break;
case 'string': return $this->getNodeText(); break;
case 'integer': return intval($this->getNodeText()); break;
case 'date': return $this->getNodeText(); break;
case 'true': return true; break;
case 'false': return false; break;
case 'array': return $this->parse_array(); break;
case 'dict': return $this->parse_dict(); break;
2019-05-22 00:59:01 -07:00
// why the can't this plist parser handle the plist generated from an hdiutil list with no dimgs attached?
2019-04-27 00:48:25 -07:00
//default: throw new Exception(sprintf("Not a valid plist. %s is not a valid type", $this->name), 4);
}
2018-08-08 23:12:47 -07:00
}
private function parse_dict() {
$array = array(); $this->nextOfType(XMLReader::ELEMENT);
do { if($this->nodeType !== XMLReader::ELEMENT || $this->name !== "key") { if(!$this->next("key")) { return $array; } } $key = $this->getNodeText(); $this->nextOfType(XMLReader::ELEMENT); $array[$key] = $this->parse_node(); $this->nextOfType(XMLReader::ELEMENT, XMLReader::END_ELEMENT); }
while($this->nodeType && !$this->isNodeOfTypeName(XMLReader::END_ELEMENT, "dict")); return $array;
}
private function parse_array() {
$array = array(); $this->nextOfType(XMLReader::ELEMENT);
do { $array[] = $this->parse_node(); $this->nextOfType(XMLReader::ELEMENT, XMLReader::END_ELEMENT); }
while($this->nodeType && !$this->isNodeOfTypeName(XMLReader::END_ELEMENT, "array")); return $array;
}
private function getNodeText() { $string = $this->readString(); $this->nextOfType(XMLReader::END_ELEMENT); return $string; }
private function nextOfType() { $types = func_get_args(); $this->read(); while($this->nodeType && !(in_array($this->nodeType, $types))) { $this->read(); } }
private function isNodeOfTypeName($type, $name) { return $this->nodeType === $type && $this->name === $name; }
}
function parseMediaInfo ($xml) {
$xml = simplexml_load_string($xml);
$data = array();
$data['version'] = (string) $xml['version'];
foreach ($xml->File->track as $track) {
$trackType = strtolower($track['type']);
$trackId = isset($track['streamid']) ? $track['streamid'] : 1;
$trackId = (string)$trackId;
$trackData = [];
foreach ($track as $rawKey => $rawVal) {
$key = strtolower($rawKey);
$val = (string)$rawVal;
if ($key == 'stream_identifier') { continue; }
if (!array_key_exists($key, $trackData)) {
$trackData[$key] = array($val);
} elseif (!in_array($val, $trackData[$key])) {
$trackData[$key][] = $val;
}
}
if ($trackType == 'general') {
$data['file']['general'] = $trackData;
} else {
$data['file'][$trackType][$trackId] = $trackData;
}
}
return $data;
}
?>