Compare commits

...

11 Commits

Author SHA1 Message Date
cmp167
d178d52aa6 Add cmpSysExecTest 2024-03-08 02:34:00 +03:00
cmp167
2f7c7b676c Fix debug mesg 2024-03-08 00:46:04 +03:00
cmp167
3ee13c2670 Accept empty array for decodeJSON 2023-12-31 07:43:52 +12:00
cmp167
1a837f46dd Add cmpTraitInit and fix git-func 2023-09-10 09:49:34 +12:00
cmp167
6ee83b4b82 Fix maxFileSize 2023-09-10 04:07:23 +12:00
cmp167
42bba9d1be Fix @array[key] 2023-09-10 01:11:00 +12:00
cmp167
8f803e6c06 Fix warn 2023-09-06 03:11:28 +12:00
cmp167
fe2e8b11a3 Add cmpSysExecEventXXX 2023-09-06 02:51:19 +12:00
cmp167
8301ee83d3 Add cmpShellEsc 2023-08-23 21:00:54 +12:00
cmp167
b08e3df595 Merge remote-tracking branch 'refs/remotes/origin/master' 2023-07-28 18:14:16 +12:00
cmp167
cb579dc369 Fix retval type 2023-07-28 18:05:15 +12:00

View File

@@ -8,6 +8,7 @@
var $gitCommit = "";
var $gitComment = "";
var $maxFileSize = 104856;
function toArray($obj) {
$re = array();
@@ -145,11 +146,11 @@
function readFile($file) {
return @file_get_contents(
$file , // string
false , // bool $use_include_path
null , // ?resource $context
0 , // int $offset
$this->parm["maxFileSize"] , // ?int $length
$file , // string
false , // bool $use_include_path
null , // ?resource $context
0 , // int $offset
$this->maxFileSize , // ?int $length
);
}
@@ -163,8 +164,12 @@
$obj = $this->decodeJSON($raw, 1);
if(is_array($obj)) {
return $obj;
}
if(!$obj) {
$this->e("Invalid JSON '$file'");
$this->e("Invalid JSON file '$file'");
return false;
}
@@ -193,16 +198,28 @@
$this->appendConf($obj);
if(@$this->parm["timezone"]) {
if(isset($this->parm["timezone"]) && $this->parm["timezone"]) {
$this->d("Set timezone '" . $this->parm["timezone"] . "'");
date_default_timezone_set($this->parm["timezone"]);
}
if(!@$this->parm["maxFileSize"])
$this->parm["maxFileSize"] = 1048576;
$this->d("Set maxFileSize '" . $this->parm["maxFileSize"] . "'");
do {
if(!isset($this->parm["maxFileSize"]))
break;
if(is_array(@$this->parm["php_ini"])) {
if(!$this->parm["maxFileSize"])
break;
if(!is_numeric($this->parm["maxFileSize"])) {
$this->d("Not numeric maxFileSize");
break;
}
$this->maxFileSize = 1*$this->parm["maxFileSize"];
$this->d("Set maxFileSize '" . $this->maxFileSize . "'");
} while(0);
if(isset($this->parm["php_ini"]) && is_array($this->parm["php_ini"])) {
foreach($this->parm["php_ini"] as $k => $v) {
ini_set($k, $v);
}
@@ -249,8 +266,7 @@
return $a;
}
function cmpShellEscArr($cmaRaw) {
function cmpShellEscStr($str) {
$ttr = array(
"\t" => "\\\t", "\$" => "\\\$",
"\\" => "\\\\", "\"" => "\\\"",
@@ -265,30 +281,241 @@
"?" => "\\?" , "!" => "\\!"
);
do {
if(!is_array($str))
break;
if(!isset($str[0]))
return null;
if($str[0] != "asis") {
$this->d("Unknown modifier '".$str[0]."'");
return null;
}
if(!isset($str[1]) || !$str[1]) {
$this->d("Invalid string '".$str[1]."'");
return null;
}
return $str[1];
} while(0);
return strtr($str, $ttr);
}
function cmpShellEscArr($cmaRaw) {
$cmaEsc = array();
for($i = 0; $i < count($cmaRaw); $i++) {
if(@$cmaRaw[$i][0] == "asis") {
$cmaEsc[] = $cmaRaw[$i][1];
continue;
$str = $this->cmpShellEscStr($cmaRaw[$i]);
if(!$str) {
return null;
}
$cmaEsc[] = strtr($cmaRaw[$i], $ttr);
$cmaEsc[] = $str;
}
return $cmaEsc;
}
function cmpShellEscStr($cmaRaw) {
$cmaEsc = $this->cmpShellEscArr($cmaRaw);
function cmpShellEsc($cmaRaw) {
if(is_array($cmaRaw)) {
$cmaEsc = $this->cmpShellEscArr($cmaRaw);
return join(" ", $cmaEsc);
}
return join(" ", $cmaEsc);
return $this->cmpShellEscStr($cmaRaw);
}
function cmpSysExecEventString(&$prs, &$buf, &$opt) {
$fs = "/\n/";
if(isset($opt["eventStringDelimer"]) && $opt["eventStringDelimer"])
$fs = $opt["eventStringDelimer"];
$a = preg_split($fs, $buf);
if(!$a) {
$this->d("Can't split buffer");
return false;
}
$c = count($a) - 1;
if($c < 1)
return true;
for($i = 0; $i < $c; $i++) {
if(!isset($opt["silent"]) || !$opt["silent"])
$this->d($prs["streamCurrent"] . ": " . $a[$i]);
if($prs["streamCurrent"] == "stdout") {
if(isset($opt["eventStringStdOut"]) && $opt["eventStringStdOut"]) {
($opt["eventStringStdOut"]["call"])($a[$i], $prs);
}
}
else if($prs["streamCurrent"] == "stderr") {
if(isset($opt["eventStringStdErr"]) && $opt["eventStringStdErr"]) {
($opt["eventStringStdErr"]["call"])($a[$i], $prs);
}
}
}
// $this->d("Stream tail: " . $a[$c]);
$buf = $a[$c];
return true;
}
function cmpSysExecEventListenner(&$ppsAll, &$opt) {
$prs = array();
while(!feof($ppsAll[1])) {
$read = [ $ppsAll[1], $ppsAll[2] ];
$write = null;
$except = null;
// $this->d("Add stream event listenner");
$cnt = stream_select(
$read,
$write,
$except,
1, // seconds
0 // microseconds
);
if(false === $cnt) {
$this->d("stream_select err");
return false;
}
// $this->d("Streams event count : " . $sel);
if(!$cnt) {
continue;
}
$out = stream_get_contents($ppsAll[1]);
if($out) {
$prs["streamCurrent"] = "stdout";
if(!$this->cmpSysExecEventString($prs, $out, $opt))
return false;
}
$err = stream_get_contents($ppsAll[2]);
if($err) {
$prs["streamCurrent"] = "stderr";
if(!$this->cmpSysExecEventString($prs, $err, $opt))
return false;
}
// while
}
return true;
}
function cmpSysExecEventSwo(&$ppsAll, &$opt) {
do {
if(!isset($opt["stdInEventAdd"]) || !$opt["stdInEventAdd"])
break;
if(!isset($opt["stdInEventAdd"]["call"])) {
$this->d("Unset callable stdInEventAdd");
break;
}
if(!is_callable($opt["stdInEventAdd"]["call"])) {
$this->d("Invalid callable stdInEventAdd");
break;
}
if(!isset($opt["silent"]) || !$opt["silent"])
$this->d("Call stdInEventAdd");
($opt["stdInEventAdd"]["call"]) (
$ppsAll[0],
$opt["stdInEventAdd"]
);
} while(0);
do {
if(!isset($opt["stdOutEventAdd"]) || !$opt["stdOutEventAdd"])
break;
if(!isset($opt["stdOutEventAdd"]["call"])) {
$this->d("Unset callable stdOutEventAdd");
break;
}
if(!is_callable($opt["stdOutEventAdd"]["call"])) {
$this->d("Invalid callable stdOutEventAdd");
break;
}
if(!isset($opt["silent"]) || !$opt["silent"])
$this->d("Call stdOutEventAdd");
($opt["stdOutEventAdd" ]["call"]) (
$ppsAll[1],
$opt["stdOutEventAdd"]
);
} while(0);
do {
if(!isset($opt["stdErrEventAdd"]) || !$opt["stdErrEventAdd"])
break;
if(!isset($opt["stdErrEventAdd"]["call"])) {
$this->d("Unset callable stdErrEventAdd");
break;
}
if(!is_callable($opt["stdErrEventAdd"]["call"])) {
$this->d("Invalid callable stdErrEventAdd");
break;
}
if(!isset($opt["silent"]) || !$opt["silent"])
$this->d("Call stdErrEventAdd");
($opt["stdErrEventAdd"]["call"]) (
$ppsAll[2],
$opt["stdErrEventAdd"]
);
} while(0);
do {
if(!isset($opt["eventWait"]) || !$opt["eventWait"])
break;
if(!isset($opt["eventWait"]["call"])) {
$this->d("Unset callable eventWait");
break;
}
if(!is_callable($opt["eventWait"]["call"])) {
$this->d("Invalid callable eventWait");
break;
}
if(!isset($opt["silent"]) || !$opt["silent"])
$this->d("Call eventWait");
($opt["eventWait"]["call"])();
} while(0);
return true;
}
function cmpSysExec($cmaRaw, $opt = []) {
$cmdStr = $this->cmpShellEscStr($cmaRaw);
$cmdStr = $this->cmpShellEsc($cmaRaw);
if(!@$opt["silent"]) {
if(!isset($opt["silent"]) || !$opt["silent"]) {
$this->d("cmpSysExec: " . $cmdStr);
}
@@ -300,7 +527,7 @@
$workDir = "/tmp";
if(@$opt["workDir"])
if(isset($opt["workDir"]) && $opt["workDir"])
$workDir = $opt["workDir"];
$envAll = null;
@@ -320,103 +547,52 @@
$blk = $opt["blocking"];
}
stream_set_blocking($ppsAll[1], $blk);
stream_set_blocking($ppsAll[2], $blk);
$retVal = 255;
$out = "";
$err = "";
if($blk) {
if(@$opt["stdin"]) {
if(isset($opt["eventString"]) && $opt["eventString"]) {
stream_set_blocking($ppsAll[1], 0);
stream_set_blocking($ppsAll[2], 0);
if(isset($opt["stdin"]) && $opt["stdin"]) {
fwrite($ppsAll[0], $opt["stdin"]);
fclose($ppsAll[0]);
}
$this->cmpSysExecEventListenner($ppsAll, $opt);
}
else if($blk) {
stream_set_blocking($ppsAll[1], $blk);
stream_set_blocking($ppsAll[2], $blk);
if(isset($opt["stdin"]) && $opt["stdin"]) {
fwrite($ppsAll[0], $opt["stdin"]);
fclose($ppsAll[0]);
}
$out = stream_get_contents($ppsAll[1], 1048576);
$err = stream_get_contents($ppsAll[2], 1048576);
fclose($ppsAll[1]);
fclose($ppsAll[2]);
}
else {
stream_set_blocking($ppsAll[1], $blk);
stream_set_blocking($ppsAll[2], $blk);
// $this->d("Nonblock");
do {
if(!@$opt["stdInEventAdd"])
break;
$this->cmpSysExecEventSwo($ppsAll, $opt);
if(!is_callable(@$opt["stdInEventAdd"]["call"])) {
$this->d("Invalid callable stdInEventAdd");
break;
}
if(!@$opt["silent"])
$this->d("Call stdInEventAdd");
($opt["stdInEventAdd"]["call"]) (
$ppsAll[0],
$opt["stdInEventAdd"]
);
} while(0);
do {
if(!@$opt["stdOutEventAdd"])
break;
if(!is_callable(@$opt["stdOutEventAdd"]["call"])) {
$this->d("Invalid callable stdOutEventAdd");
break;
}
if(!@$opt["silent"])
$this->d("Call stdOutEventAdd");
($opt["stdOutEventAdd" ]["call"]) (
$ppsAll[1],
$opt["stdOutEventAdd"]
);
} while(0);
do {
if(!@$opt["stdErrEventAdd"])
break;
if(!is_callable(@$opt["stdErrEventAdd"]["call"])) {
$this->d("Invalid callable stdErrEventAdd");
break;
}
if(!@$opt["silent"])
$this->d("Call stdErrEventAdd");
($opt["stdErrEventAdd"]["call"]) (
$ppsAll[2],
$opt["stdErrEventAdd"]
);
} while(0);
do {
if(!@$opt["eventWait"])
break;
if(!@$opt["silent"])
$this->d("Call eventWait");
if(!is_callable(@$opt["eventWait"]["call"])) {
$this->d("Invalid callable eventWait");
break;
}
($opt["eventWait"]["call"])();
} while(0);
}
// $this->d("Close all pipe");
fclose($ppsAll[1]);
fclose($ppsAll[2]);
$retVal = proc_close($prcRes);
if(!@$opt["return"] || @$opt["return"] == "full") {
if(!isset($opt["return"]) || !$opt["return"] || $opt["return"] == "full") {
return array(
"retval" => $retVal,
"stdout" => $out,
@@ -426,12 +602,12 @@
$val = 0;
if(@$opt["retval"])
if(isset($opt["retval"]) && $opt["retval"])
$val = $opt["retval"];
if(!@$opt["noerror"]) {
if(!isset($opt["noerror"]) || !$opt["noerror"]) {
if($retVal !== $val) {
if(!@$opt["silent"]) {
if(!isset($opt["silent"]) || !$opt["silent"]) {
$this->d($err);
$this->d("Return code: " . $retVal);
}
@@ -444,77 +620,163 @@
}
}
if(isset($opt["return"]) && $opt["return"]) {
switch(@$opt["return"]) {
case "retval":
return 1*$retVal;
switch(@$opt["return"]) {
case "retval":
return $retVal;
case "outstr":
return $out;
case "outstr":
return $out;
case "outarr":
default:
return explode("\n", $out);
case "outarr":
default:
return explode("\n", $out);
}
}
return explode("\n", $out);
}
function cmpSysExecTest() {
$a = array(
"ping" ,
"-c" ,
"4" ,
"127.0.0.1" ,
);
$this->d("Launch");
$this->d($a);
function getGitStatus() {
$ret = $this->fgExec("git status");
// $this->d(join("\n", $ret));
$out = trim($ret["output"], "\n \t\r");
if(!$out) {
$this->d($ret["stderr"][0]);
return false;
}
$lin = explode("\n", $out);
$cnt = count($lin);
if($lin[$cnt-1] == "nothing to commit, working tree clean") {
$this->gitStatus = "treeClean";
$this->d("Git tree clean");
} else {
$this->gitStatus = "treeModified";
$this->d("Last string: " . $lin[$cnt-1]);
}
return;
$this->cmpSysExec($a, array(
"silent" => false,
"eventString" => true,
"eventStringStdOut" => array("call" => [$this, "d"])
));
}
function getGitLog1() {
$ret = $this->fgExec("git log -1");
// $this->d(join("\n", $ret));
function getGitStatus($dir = ".") {
$a = array(
"git", "status", "--porcelain"
);
$out = trim($ret["output"], "\n \t\r");
$gitStatus = $this->cmpSysExec($a, ["workDir" => $dir]);
if(!$out) {
$this->d($ret["stderr"][0]);
return false;
if($gitStatus["retval"]) {
$this->d("GIT have errors");
$this->d($gitStatus["stderr"]);
return ;
}
$lin = explode("\n", $out);
$cnt = count($lin);
if(!$gitStatus["stdout"]) {
return null;
}
$wrd = explode(" ", $lin[0]);
$this->gitCommit = $wrd[1];
$this->w("GIT have changes");
$this->gitComment = $lin[$cnt-1];
$arr = explode("\n", $gitStatus["stdout"]);
$ret = array();
$wrd = explode(" ", $lin[2]);
array_shift($wrd);
$this->gitTimeStamp = strtotime(join(" ", $wrd));
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = trim($arr[$i], "\n \r\t");
return;
if(!$arr[$i])
continue;
$ff = explode(" ", $arr[$i], 2);
$this->d("flag=" . $ff[0] . " file=" . $ff[1]);
$ret[] = $ff;
}
// $this->d($ret);
return $ret;
}
function getGitLog1($dir = ".") {
// https://git-scm.com/docs/pretty-formats
$frmReq = array(
array("commitHash", "H" ),
array("commitDate", "ci"),
array("commitTStm", "ct"),
array("authorMail", "ae")
);
$frmArr = array();
for($i = 0; $i < count($frmReq); $i++)
$frmArr[] = "%" . $frmReq[$i][1];
$frmStr = join("%n", $frmArr);
$a = array(
"git", "log", "--pretty=format:$frmStr", "-1"
);
$gitCommit = $this->cmpSysExec($a, ["workDir" => $dir]);
if($gitCommit["retval"]) {
$this->d("GIT have errors");
$this->d($gitCommit["stderr"]);
return null;
}
$b = explode("\n", $gitCommit["stdout"]);
$frmRet = array();
for($i = 0; $i < count($frmReq); $i++)
$frmRet[ $frmReq[$i][0] ] = $b[$i];
return $frmRet;
}
function cmpTraitInit($obj, $lvl = 0) {
$listTrait = class_uses($obj);
foreach($listTrait as $trait) {
$this->d("Used trait $trait");
do {
$traitRoot = OUT_PROJ_DIR . "/" . $trait;
$traitGit = $traitRoot . "/.git";
if(!is_dir($traitGit))
break;
$ret = $this->getGitStatus($traitRoot);
$ret = $this->getGitLog1($traitRoot);
$this->d($ret);
} while(0);
$this->cmpTraitInit($trait, $lvl+1);
$initMeth = $trait . "Init";
if(!method_exists($this, $initMeth)) {
// $this->d("No method $initMeth");
continue;
}
$parm = null;
if(isset($this->parm[$trait])) {
// $this->d("Found parm");
$parm = $this->parm[$trait];
}
$this->swoSelfCall(array("func" => $initMeth, "parm" => $parm));
}
return true;
}
function isMD5($str) {
if(preg_match("/^[0-9a-f]{32}$/", $str))
return true;