-
Notifications
You must be signed in to change notification settings - Fork 32
/
command.php
178 lines (167 loc) · 5.37 KB
/
command.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
header('Content-type:text/plain;charset=utf-8');
error_reporting(0);
if($argc == 1){
error('invalid command');
}else{
$command = $argv[1];
$filename = $argv[0];
unset($argv[0],$argv[1]);
$argv = array_values($argv);
if(!function_exists($command)){
error('wrong command name');
}else{
$command($argv);
}
}
//连接数据库
function connect_db(){
error_reporting(0);
$settings = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'Thinkphp.sublime-settings');
$settings = json_decode($settings, true);
$current_database = file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'current_database');
$config = $settings['database'][$current_database];
if(!$config)
exit('current_database config dosen\'t exsist');
$argv = array(
0=>$config['DB_HOST'].':'.$config['DB_PORT'],
1=>$config['DB_USER'],
2=>$config['DB_PWD'],
3=>$config['DB_NAME'],
4=>$config['DB_PREFIX']
);
$conn = mysql_connect($argv[0],$argv[1],$argv[2]);
if(!$conn && (strpos('localhost', $argv[0])!= -1)){
$conn = mysql_connect(str_replace('localhost', '127.0.0.1', $argv[0]),$argv[1],$argv[2]);
}
if(!$conn)
exit('can\'t connect database');
mysql_select_db($argv[3],$conn);
mysql_set_charset('UTF8', $conn);
return $argv;
}
function show_colums($argv){
$config = connect_db();
$table = $config[4].parse_name($argv[0]);
$result = mysql_query("SHOW FULL COLUMNS FROM {$table}");
if(!$result){
error(mysql_error());
}else{
while($row = mysql_fetch_assoc($result)){
$res[] = $row;
}
success('query success',$res);
}
mysql_close();
}
function find_php_defination($argv){
$word = $argv[0];
$path = realpath(dirname(__FILE__));
$path .= DIRECTORY_SEPARATOR.'phpruntime'.DIRECTORY_SEPARATOR;
chdir($path);
$list = glob('*.php');
$to_search = "function {$word} (";
foreach ($list as $key => $value) {
$content = file_get_contents($path.$value);
$pos_search = strpos($content, $to_search);
if($pos_search !== false){
$comment = get_comment($pos_search, $content);
if($comment){
success('found it!', $comment);
break;
}
}
}
error('didn\'t find it');
}
function get_comment($pos,$content){
$content_length = strlen($content);
$end_pos = strripos($content, '*/', $pos - $content_length);
$start_pos = strripos($content, '/**', $end_pos - $content_length);
return substr($content, $start_pos, $end_pos - $start_pos + 2);
}
function query($argv = ''){
error_reporting(7);
$table_queryer_file = __DIR__.DIRECTORY_SEPARATOR.'ThinkPHP-Queryer';
$content = file_get_contents($table_queryer_file);
$sep = '##########################################################################';
$arr_content = split($sep, $content);
$sql = trim($arr_content[0]);
connect_db($argv);
$result = mysql_query($sql);
$rows = array();
if($result){
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
if($i == 0)
$header = array_keys($row);
$i++;
$rows[] = array_values($row);
}
}
require __DIR__.'/table.php';
$in = array(
'header'=>$header? $header : array(),
'rows'=>$rows
);
if($rows){
$table = new table($in);
$output = $table->render(0);
}
if(!$output)
$output = 'no results!';
exit($output);
}
//----------------------下面是通用函数-------------------------
function error($msg,$data=''){
exit(json_encode(array('status'=>0,'info'=>$msg,'data'=>$data)));
}
function success($msg,$data=''){
exit(json_encode(array('status'=>1,'info'=>$msg,'data'=>$data)));
}
/**
+----------------------------------------------------------
* 字符串命名风格转换
* type
* =0 将Java风格转换为C的风格
* =1 将C风格转换为Java的风格
+----------------------------------------------------------
* @access protected
+----------------------------------------------------------
* @param string $name 字符串
* @param integer $type 转换类型
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
function parse_name($name, $type=0) {
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
function make_request($url, $param = array(), $httpMethod = 'GET') {
$oCurl = curl_init();
if (stripos($url, "https://") !== FALSE) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
}
if ($httpMethod == 'GET') {
curl_setopt($oCurl, CURLOPT_URL, $url . "?" . http_build_query($param));
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
} else {
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, 1);
curl_setopt($oCurl, CURLOPT_POSTFIELDS, http_build_query($param));
}
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return FALSE;
}
}