Files
Yuba/functions.php
2018-09-13 01:50:00 -07:00

170 lines
5.9 KiB
PHP
Executable File

<?php
// Classes
//////////////////////////////////////////
class ProgressBar {
protected static $time_start;
protected static $time_remain;
protected static $time_update;
protected static $total;
protected static $done;
protected static $message;
public static function display($message = null) {
$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();
}
$message = gmdate("H:i:s",self::$time_remain)." | ".$message;
}
if ($message) {
return "\n".$string."\n".$message;
} elseif (!strpos(__FILE__,".app")) {
return "\r\033[K\r".$string;
} else {
return "\n".$string;
}
}
public static function start($total, $message = null) {
self::$done = 0;
self::$total = $total;
self::$time_start = time();
self::$message = $message;
return $message."\n".self::display();
}
public static function next($message = null) {
self::$done++;
return self::display($message);
}
public static function finish() {
global $wopt_currstep;
$wopt_currstep++;
self::$done = 0;
return "\n";
}
}
// Functions
//////////////////////////////////////////
/*
function getParents($zpath, $pathname) {
$path = dirname($pathname);
$parts = explode("/",trim(substr($path,strlen(basename($zpath))),"/"));
foreach ($parts as $index => $part) {
$parents[] = array($part, md5($zpath."/".implode("/",array_slice($parts, 0, $index+1))));
}
return $parents;
}
*/
function stepString() {
global $wopt_steps;
global $wopt_currstep;
return "Step ".$wopt_currstep." of ".$wopt_steps;
}
function shortlabel($pathname, $max = 99) {
$basename = basename($pathname);
$suffix = "(...).".pathinfo($basename,PATHINFO_EXTENSION);
if (strlen($basename) > $max) {
$return = substr($basename, 0, ($max-strlen($suffix))).$suffix;
} else {
$return = $basename;
}
return $return;
}
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];
}
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;
default: throw new Exception(sprintf("Not a valid plist. %s is not a valid type", $this->name), 4);
}
}
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;
}
?>