-
Notifications
You must be signed in to change notification settings - Fork 127
/
main.php
164 lines (134 loc) · 4.64 KB
/
main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
require_once 'global.php';
$smarty = new Smarty_setup();
/**
* 处理单个文件的静态检测
* 输入PHP文件
* @param string $path
*/
function load_file($path){
$cfg = new CFGGenerator() ;
$cfg->getFileSummary()->setPath($path);
$visitor = new MyVisitor() ;
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative) ;
$traverser = new PhpParser\NodeTraverser ;
$code = file_get_contents($path);
$stmts = $parser->parse($code) ;
$traverser->addVisitor($visitor) ;
$traverser->traverse($stmts) ;
$nodes = $visitor->getNodes() ;
$pEntryBlock = new BasicBlock() ;
$pEntryBlock->is_entry = true ;
//开始分析
$cfg->CFGBuilder($nodes, NULL, NULL, NULL) ;
}
/**
* 将结果集转为前端的模式
* @param ResultContext $resContext
*/
function convertResults($resContext){
$ret = array() ;
$resArr = $resContext->getResArr() ;
foreach($resArr as $record){
$item = array() ;
$record = $record->getRecord() ;
$item['type'] = $record['type'] ;
$item['node_path'] = $record['node_path'] ;
$item['var_path'] = $record['var_path'] ;
//整理node代码
$node = $record['node'] ;
$node_item = array() ;
if($node instanceof Symbol){
$node_start = $node->getValue()->getAttribute('startLine') ;
$node_end = $node->getValue()->getAttribute('endLine') ;
}else{
$node_start = $node->getAttribute('startLine') ;
$node_end = $node->getAttribute('endLine') ;
}
$node_item['line'] = $node_start . "|" . $node_end ;
$node_item['code'] = FileUtils::getCodeByLine($record['node_path'], $node_start, $node_end) ;
$item['node'] = $node_item ;
//整理var代码
$var = $record['var'] ;
$var_item = array() ;
if($var instanceof Symbol){
$var_start = $var->getValue()->getAttribute('startLine') ;
$var_end = $var->getValue()->getAttribute('endLine') ;
}elseif(is_string($var)){
$var_start = $node_start ;
$var_end = $node_end ;
}else{
$var_start = $var->getAttribute('startLine') ;
$var_end = $var->getAttribute('endLine') ;
}
$var_item['line'] = $var_start . "|" . $var_end ;
$var_item['code'] = FileUtils::getCodeByLine($record['var_path'], $var_start, $var_end) ;
$item['var'] = $var_item ;
array_push($ret, $item) ;
}
return $ret ;
}
if(!isset($_POST['path']) || !isset($_POST['type'])){
$smarty->display('index.html') ;
exit() ;
}
//1、从web ui中获取并加载项目工程
$project_path = $_POST['prj_path'] ; //扫描的工程路径
$scan_path = $_POST['path'] ; //扫描文件路径
$scan_type = $_POST['type'] ; //扫描的类型
$encoding = $_POST['encoding'] ; //CMS的编码 UTF-8 或者 GBK
if(CommonUtils::endsWith($project_path, "/")){
$last = count($project_path) - 2 ;
$project_path = substr($project_path, 0, $last) ;
}
if(CommonUtils::endsWith($scan_path, "/")){
$last = count($scan_path) - 2 ;
$scan_path = substr($scan_path, 0, $last) ;
}
$scan_type = $scanType = strtoupper($scan_type);
$encoding = strtoupper($encoding);
$project_path = str_replace(array('\\','//'), '/', $project_path);
$scan_path = str_replace(array('\\','//'), '/', $scan_path);
$fileName = str_replace('/', '_', $scan_path);
$fileName = str_replace(':', '_', $fileName);
$serialPath = CURR_PATH . "/data/resultConetxtSerialData/" . $fileName;
if (!is_file($serialPath)){
//创建文件
$fileHandler = fopen($serialPath, 'w');
fclose($fileHandler);
}
$results = null;
if(($serial_str = file_get_contents($serialPath)) != ''){
$results = unserialize($serial_str) ;
}else{
//3、初始化模块
$allFiles = FileUtils::getPHPfile($project_path);
$mainlFiles = FileUtils::mainFileFinder($scan_path);
$initModule = new InitModule() ;
$initModule->init($project_path, $allFiles) ;
//4、循环每个文件 进行分析工作
if(is_file($project_path)){
load_file($project_path) ;
}elseif (is_dir($project_path)){
$path_list = $mainlFiles;
foreach ($path_list as $path){
try{
load_file($path) ;
}catch(Exception $e){
continue ;
}
}
}else{
//请求不合法
echo "工程不存在!" ;
exit() ;
}
//5、处理results 序列化
$results = ResultContext::getInstance() ;
file_put_contents($serialPath, serialize($results)) ;
}
//6、传给template
$template_res = convertResults($results) ;
$smarty->assign('results',$template_res);
$smarty->display('content.html');
?>