From d9e152b444426677016e9ec9af1c4689ba965e48 Mon Sep 17 00:00:00 2001 From: denghongtao Date: Tue, 13 Sep 2022 07:31:36 +0000 Subject: [PATCH 1/3] Add api control. --- test/lib/controlapi.php | 265 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100755 test/lib/controlapi.php diff --git a/test/lib/controlapi.php b/test/lib/controlapi.php new file mode 100755 index 0000000000..51388f3ef4 --- /dev/null +++ b/test/lib/controlapi.php @@ -0,0 +1,265 @@ +run($argv[1], $entries); + } + + public $ztPath; + + /** + * Run. + * + * @param string $path + * @param string $module + * @access public + * @return bool + */ + public function run($path, $entries = '') + { + $independent = array(); + $strIndependent = ''; + + if($entries) + { + $results = $this->checkInput($path); + if($results) + { + echo 'true'; + } + else + { + echo 'false'; + } + + foreach($results as $result) + { + if(strpos($result['filePath'], $entries) != false) $independent[count($independent)] = $result; + } + + $strIndependent .= ' $array) + { + $strIndependent .= '$result->key' . $key . ' = new stdclass;' . ";\n"; + foreach($array as $field => $val) + { + $strIndependent .= '$result->key' . $key . '->' . $field . " = "; + $strIndependent .= $val . ";\n"; + } + $strIndependent .= "\n"; + } + file_put_contents($entries . '.php', $strIndependent); + + return $independent; + } + else + { + $results = $this->checkInput($path); + + if($results) + { + echo 'true'; + } + else + { + echo 'false'; + } + + $strResults = ''; + $strResults .= ' $array) + { + $strResults .= '$result->key' . $key . ' = new stdclass;' . ";\n"; + foreach($array as $field => $val) + { + $strResults .= '$result->key' . $key . '->' . $field . " = "; + $strResults .= $val . ";\n"; + } + $strResults .= "\n"; + } + file_put_contents('entries.php', $strResults); + return $results; + } + } + + /* + * Check user input. + * + * @param string $path + * @access public + * @return bool + */ + + public function checkInput($path = '') + { + if(empty($path)) return false; + + $path = $this->checkWebDir($path); + if(!$path) return false; + + $this->ztPath = $path; + $openRes = $this->checkOpen(); + return $openRes; + } + + /* + * Check zentao path. + * + * @param string $path + * @access public + * @return string + */ + public function checkWebDir($path = '') + { + $configPath = $path . DS . 'config' . DS . 'config.php'; + + $info = new SplFileInfo($configPath); + $realPath = $info->getRealPath(); + + if($realPath) return dirname(dirname($realPath)); + return ''; + } + + /** + * Get files under a directory recursive. + * + * @param string $dir + * @param array $exceptions + * @access private + * @return array + */ + public function readDir($dir, $exceptions = array()) + { + static $files = array(); + + if(!is_dir($dir)) return $files; + + $dir = realpath($dir) . DS; + $entries = scandir($dir); + + foreach($entries as $entry) + { + if($entry == '.' or $entry == '..' or $entry == '.svn') continue; + if(in_array($entry, $exceptions)) continue; + + $fullEntry = $dir . $entry; + if(is_file($fullEntry)) + { + $files[] = $dir . $entry; + } + else + { + $nextDir = $dir . $entry; + $this->readDir($nextDir); + } + } + return $files; + } + + public function checkOpen() + { + $apiFiles = $this->readDir($this->ztPath . DS . 'api' . DS . 'v1' . DS . 'entries' . DS); + $results = array(); + foreach ($apiFiles as $key => $filePath) + { + $fileContent = file($filePath); + $controls = array(); + foreach ($fileContent as $line => $code) + { + preg_match('/\$control\s+=\s\$this->loadController\([\'"]([a-z]+)[\'"],\s[\'"]([a-zA-Z0-9]+)[\'"]\);/', $code, $controlNames); + if(!empty($controlNames)) + { + $controls[] = $controlNames[1]; + continue; + } + if(!preg_match('/\$control->([a-z0-9]+)\((\$[a-z0-9]+,\s|\$this->param\([\'\"][a-z0-9]+[\'\"],\s*[\'\"]?[a-z0-9-_\s]*[\'\"]?\)[,]?\s*|[\'\"]?[0-9a-z]+[\'\"]?,\s)+\)/i', $code, $controlMethod)) continue; + + $res = preg_match_all('/(\$[a-z0-9]+[,\)])|([0-9]+[,\)])|((?[a-z0-9]+\()/i', $code, $execControls, PREG_PATTERN_ORDER); + if(!empty($execControls[0])) + { + $params = $execControls[0]; + $pramsLen = count($params) - 1; + $methodName = trim(trim($params[0], '->'), '('); + + $module = $controls[count($controls) - 1]; + $checkRes = $this->checkParamLen($module, $methodName, $pramsLen); + + if(!is_bool($checkRes)) + { + $results[] = array( + 'filePath' => $filePath, + 'line' => ++$line, + 'moduleName' => $module, + 'methodName' => $methodName, + 'apiCode' => $controlMethod[0], + 'controlCode' => $checkRes['lineCode'], + 'controlFile' => $checkRes['controlFile'], + 'status' => 'fail' + ); + } + elseif($checkRes) + { + $results[] = array( + 'filePath' => $filePath, + 'line' => ++$line, + 'moduleName' => $module, + 'methodName' => $methodName, + 'apiCode' => $controlMethod[0], + 'controlCode' => '', + 'controlFile' => '', + 'status' => 'success' + ); + } + } + } + } + return empty($results) ? true : $results; + } + + /* + * Check method params length. + * + * @param string $module + * @param string $method + * @param int $length + * @access public + * @return string|bool + */ + public function checkParamLen($module, $method, $length) + { + $controlFile = $this->ztPath . DS . 'module' . DS . $module . DS . 'control.php'; + $controlExtFile = $this->ztPath . DS . 'extension/max/' . $module . '/control.php'; + $controlFuncFile = $this->ztPath . DS . 'extension/max/' . $module . '/ext/control/' . $method . '.php'; + + $realFile = file_exists($controlFuncFile) ? $controlFuncFile : (file_exists($controlExtFile) ? $controlExtFile : $controlFile); + $controlContent = file_get_contents($realFile); + preg_match('/public\sfunction\s' . $method . '\((\$[a-z0-9]+(\s=\s[\'\"]?[a-z0-9-_,]*[\'\"]?)?,?\s?)*\)/i', $controlContent, $matches); + if(empty($matches)) return false; + + $paramLen = substr_count($matches[0], '$'); + if($paramLen != $length) + { + return array('lineCode' => $matches[0], 'controlFile' => $realFile); + } + else + { + return true; + } + } +} +$tester = new apiCheckModel(); From f2dc224448d290167c315b29e3d7649b730c2bd5 Mon Sep 17 00:00:00 2001 From: denghongtao Date: Tue, 13 Sep 2022 08:17:55 +0000 Subject: [PATCH 2/3] * Fix controlapi. --- test/lib/controlapi.php | 46 ++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/test/lib/controlapi.php b/test/lib/controlapi.php index 51388f3ef4..141e4ebdc8 100755 --- a/test/lib/controlapi.php +++ b/test/lib/controlapi.php @@ -8,13 +8,12 @@ class apiCheckModel * Eg: ztf run api.php $path $entries. * * @access public - * @return bool + * @return string */ public function __construct() { global $argv; - var_dump($argv); - $path = $argv[1]; + $path = $argv[1]; $entries = ''; if($argv[2]) $entries = $argv[2]; $this->run($argv[1], $entries); @@ -23,16 +22,16 @@ class apiCheckModel public $ztPath; /** - * Run. + * Run generate report. * * @param string $path - * @param string $module + * @param string $entries * @access public - * @return bool + * @return array */ public function run($path, $entries = '') { - $independent = array(); + $independent = array(); $strIndependent = ''; if($entries) @@ -80,7 +79,7 @@ class apiCheckModel echo 'false'; } - $strResults = ''; + $strResults = ''; $strResults .= ' $array) { @@ -102,7 +101,7 @@ class apiCheckModel * * @param string $path * @access public - * @return bool + * @return array */ public function checkInput($path = '') @@ -113,7 +112,7 @@ class apiCheckModel if(!$path) return false; $this->ztPath = $path; - $openRes = $this->checkOpen(); + $openRes = $this->checkOpen(); return $openRes; } @@ -122,14 +121,14 @@ class apiCheckModel * * @param string $path * @access public - * @return string + * @return void */ public function checkWebDir($path = '') { $configPath = $path . DS . 'config' . DS . 'config.php'; - $info = new SplFileInfo($configPath); - $realPath = $info->getRealPath(); + $info = new SplFileInfo($configPath); + $realPath = $info->getRealPath(); if($realPath) return dirname(dirname($realPath)); return ''; @@ -149,12 +148,12 @@ class apiCheckModel if(!is_dir($dir)) return $files; - $dir = realpath($dir) . DS; + $dir = realpath($dir) . DS; $entries = scandir($dir); foreach($entries as $entry) { - if($entry == '.' or $entry == '..' or $entry == '.svn') continue; + if(in_array($entry, array('.', '..', '.svn', '.git'))) continue; if(in_array($entry, $exceptions)) continue; $fullEntry = $dir . $entry; @@ -171,6 +170,12 @@ class apiCheckModel return $files; } + /** + * Matching control. + * + * @access public + * @return bool | array + */ public function checkOpen() { $apiFiles = $this->readDir($this->ztPath . DS . 'api' . DS . 'v1' . DS . 'entries' . DS); @@ -192,12 +197,11 @@ class apiCheckModel $res = preg_match_all('/(\$[a-z0-9]+[,\)])|([0-9]+[,\)])|((?[a-z0-9]+\()/i', $code, $execControls, PREG_PATTERN_ORDER); if(!empty($execControls[0])) { - $params = $execControls[0]; - $pramsLen = count($params) - 1; + $params = $execControls[0]; + $pramsLen = count($params) - 1; $methodName = trim(trim($params[0], '->'), '('); - - $module = $controls[count($controls) - 1]; - $checkRes = $this->checkParamLen($module, $methodName, $pramsLen); + $module = $controls[count($controls) - 1]; + $checkRes = $this->checkParamLen($module, $methodName, $pramsLen); if(!is_bool($checkRes)) { @@ -238,7 +242,7 @@ class apiCheckModel * @param string $method * @param int $length * @access public - * @return string|bool + * @return array | bool */ public function checkParamLen($module, $method, $length) { From 1de133a79b1fe965dd0a4cd5c0c54bda1bc130e6 Mon Sep 17 00:00:00 2001 From: denghongtao Date: Tue, 13 Sep 2022 08:28:16 +0000 Subject: [PATCH 3/3] * Fix notes. --- test/lib/controlapi.php | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/test/lib/controlapi.php b/test/lib/controlapi.php index 141e4ebdc8..ca160059b4 100755 --- a/test/lib/controlapi.php +++ b/test/lib/controlapi.php @@ -37,14 +37,7 @@ class apiCheckModel if($entries) { $results = $this->checkInput($path); - if($results) - { - echo 'true'; - } - else - { - echo 'false'; - } + echo $results ? 'true' : 'false' ; foreach($results as $result) { @@ -70,14 +63,7 @@ class apiCheckModel { $results = $this->checkInput($path); - if($results) - { - echo 'true'; - } - else - { - echo 'false'; - } + echo $results ? 'true' : 'false' ; $strResults = ''; $strResults .= '