質問編集履歴

2

セキュリティの関わる問題なので削除させていただきました。

2018/06/18 09:35

投稿

amaguri
amaguri

スコア227

test CHANGED
@@ -1 +1 @@
1
- cakephpにてEXPLAINファイルを解析してクエ種類とデータベース名を抽出すコード実際何をしているかがわかりせん
1
+ セキュティの関わ問題なの削除させていただきした。
test CHANGED
@@ -1,245 +1 @@
1
- 下記のコードの解読をしているのですが意味が全くわかわずわかる方いらっしゃいましたら教えてください。
2
-
3
-
4
-
5
- ```php
6
-
7
- //EXPLAINファイルを解析してクエリ種類とデータベース名を抽出
8
-
9
- function CheckExplainParce($u_userid,$u_id){
10
-
11
- $cnt=0;
12
-
13
- $results=array();
14
-
15
- $cur_db="default";
1
+ セキュリティの関わる問題なので削除させていただきました。
16
-
17
- $sql_query="";
18
-
19
- $sql_query_no=0;
20
-
21
- $exp_file=DIR_RESULT."/${u_userid}/${u_id}.exp";
22
-
23
-
24
-
25
- if ( !($fp=fopen($exp_file,"r")) ){ return $results; }
26
-
27
- while(!feof($fp)){
28
-
29
- $data = fgets($fp, 10240);
30
-
31
- $data=str_replace(array("\r\n","\n","\r"), '', $data);
32
-
33
- if ( eregi("^use ",$data) ){
34
-
35
- list($dummy,$cur_db)=split("[ ;]",$data);
36
-
37
- }
38
-
39
-
40
-
41
- //クエリ
42
-
43
- if ( eregi("^explain ",$data) ){
44
-
45
- $sql_query=substr($data,8);
46
-
47
- //$this->log("QUERY=[$sql_query]",LOG_DEBUG);
48
-
49
- $sql_query_no++;
50
-
51
- }
52
-
53
-
54
-
55
- // トークン部分の解析
56
-
57
- //
58
-
59
- // 【解析仕様】
60
-
61
- // ・トークンは『ABSTRACT SYNTAX TREE』行と『STAGE DEPENDENCIES』行の間にある
62
-
63
- // ・データベース名及びテーブル名(以降 オブジェクト部)は次のターゲットトークンの次の行以降にインデントされて定義されている
64
-
65
- // → TOK_TABNAME、TOK_TABTYPE、TOK_ALTERTABLE、TOK_TABTYPE、TOK_SHOW
66
-
67
- // ・オブジェクト部には『データベース名行 + テーブル名行』または『テーブル名行』のパターンがある
68
-
69
- // ・オブジェクト部の範囲はターゲットトークンのインデントと同じまたは浅い行の前行までとする
70
-
71
- // ・オブジェクト部に別のターゲットトークンがあった場合、オブジェクト部はその前行までとし、別のターゲットトークン以降を新しいオブジェクト部として処理する
72
-
73
- //
74
-
75
- if (eregi("^ABSTRACT SYNTAX TREE", $data) and !feof($fp)) {
76
-
77
- $data = fgets($fp, 10240);
78
-
79
- $data = str_replace(array("\r\n","\n","\r"), '', $data);
80
-
81
-
82
-
83
- $sql_type="";
84
-
85
-
86
-
87
- while (!feof($fp)) {
88
-
89
- if (eregi("^STAGE DEPENDENCIES", $data)) { break; }
90
-
91
-
92
-
93
- $sql_type = CommonComponent::CheckTOK2SQLtype(trim($data), $sql_type);
94
-
95
-
96
-
97
- if (eregi("^TOK_TABNAME|^TOK_TABTYPE|^TOK_ALTERTABLE|^TOK_TABTYPE|^TOK_SHOW|^TOK_CREATEDATABASE|^TOK_DROPDATABASE", trim($data))) {
98
-
99
- $tok_indent = strlen($data) - strlen(ltrim($data));
100
-
101
- $token = trim($data);
102
-
103
-
104
-
105
- $data = fgets($fp, 10240);
106
-
107
- $data = str_replace(array("\r\n","\n","\r"), '', $data);
108
-
109
-
110
-
111
- $objects = array();
112
-
113
-
114
-
115
- while (!feof($fp)) {
116
-
117
- if (eregi("^TOK_TABNAME|^TOK_TABTYPE|^TOK_ALTERTABLE|^TOK_TABTYPE|^TOK_SHOW|^TOK_CREATEDATABASE|^TOK_DROPDATABASE", trim($data))) { break; }
118
-
119
- if ($tok_indent >= (strlen($data) - strlen(ltrim($data)))) { break; }
120
-
121
-
122
-
123
- if (!eregi("^TOK_|.", trim($data))) {
124
-
125
- $objects[] = trim($data);
126
-
127
- }
128
-
129
- $data = fgets($fp, 10240);
130
-
131
- $data=str_replace(array("\r\n","\n","\r"), '', $data);
132
-
133
- }
134
-
135
-
136
-
137
- if (count($objects) >= 2) {
138
-
139
- $results[$cnt]->no = $sql_query_no;
140
-
141
- $results[$cnt]->type = $sql_type;
142
-
143
- $results[$cnt]->db = $objects[0];
144
-
145
- $results[$cnt]->tbl = $objects[1];
146
-
147
- $cnt++;
148
-
149
- } else if (count($objects) == 1) {
150
-
151
- $results[$cnt]->no = $sql_query_no;
152
-
153
- $results[$cnt]->type = $sql_type;
154
-
155
- if (eregi("^TOK_SHOW|^TOK_CREATEDATABASE|^TOK_DROPDATABASE", $token)) {
156
-
157
- $results[$cnt]->db = $objects[0];
158
-
159
- $results[$cnt]->tbl = "";
160
-
161
- } else {
162
-
163
- $results[$cnt]->db = $cur_db;
164
-
165
- $results[$cnt]->tbl = $objects[0];
166
-
167
- }
168
-
169
- $cnt++;
170
-
171
- } else if (count($objects) == 0 and eregi("^TOK_SHOW", $token)) {
172
-
173
- $results[$cnt]->no = $sql_query_no;
174
-
175
- $results[$cnt]->type = $sql_type;
176
-
177
- $results[$cnt]->db = $cur_db;
178
-
179
- $results[$cnt]->tbl = "";
180
-
181
- $cnt++;
182
-
183
- }
184
-
185
- } else {
186
-
187
- $data = fgets($fp, 10240);
188
-
189
- $data = str_replace(array("\r\n","\n","\r"), '', $data);
190
-
191
- }
192
-
193
- }
194
-
195
-
196
-
197
- $sql_query = "";
198
-
199
- }
200
-
201
- }
202
-
203
- fclose($fp);
204
-
205
- return $results;
206
-
207
- }
208
-
209
- ```
210
-
211
-
212
-
213
- ```php
214
-
215
- //トークン名からクエリに変換
216
-
217
- function CheckTOK2SQLtype($data,$sql_type){
218
-
219
- if ( $data == "TOK_DESCTABLE" ){ return "desc"; }
220
-
221
- if ( $data == "TOK_CREATETABLE" ){ return "create table"; }
222
-
223
- if ( $data == "TOK_TABREF" ){ return "select"; }
224
-
225
- if ( $data == "TOK_INSERT" ){ return "insert"; }
226
-
227
- if ( $data == "TOK_DROPTABLE" ){ return "drop table"; }
228
-
229
- if ( eregi("^TOK_SHOW",$data) ){ return "show"; }
230
-
231
- if ( eregi("^TOK_ALTER",$data) ){ return "alter table"; }
232
-
233
-
234
-
235
- if ( $data == "TOK_CREATEDATABASE" ){ return "create database"; }
236
-
237
- if ( $data == "TOK_DROPDATABASE" ){ return "drop database"; }
238
-
239
-
240
-
241
- return $sql_type;
242
-
243
- }
244
-
245
- ```

1

追加

2018/06/18 09:35

投稿

amaguri
amaguri

スコア227

test CHANGED
File without changes
test CHANGED
@@ -120,7 +120,7 @@
120
120
 
121
121
 
122
122
 
123
- if (!eregi("^TOK_|\.", trim($data))) {
123
+ if (!eregi("^TOK_|.", trim($data))) {
124
124
 
125
125
  $objects[] = trim($data);
126
126
 
@@ -207,3 +207,39 @@
207
207
  }
208
208
 
209
209
  ```
210
+
211
+
212
+
213
+ ```php
214
+
215
+ //トークン名からクエリに変換
216
+
217
+ function CheckTOK2SQLtype($data,$sql_type){
218
+
219
+ if ( $data == "TOK_DESCTABLE" ){ return "desc"; }
220
+
221
+ if ( $data == "TOK_CREATETABLE" ){ return "create table"; }
222
+
223
+ if ( $data == "TOK_TABREF" ){ return "select"; }
224
+
225
+ if ( $data == "TOK_INSERT" ){ return "insert"; }
226
+
227
+ if ( $data == "TOK_DROPTABLE" ){ return "drop table"; }
228
+
229
+ if ( eregi("^TOK_SHOW",$data) ){ return "show"; }
230
+
231
+ if ( eregi("^TOK_ALTER",$data) ){ return "alter table"; }
232
+
233
+
234
+
235
+ if ( $data == "TOK_CREATEDATABASE" ){ return "create database"; }
236
+
237
+ if ( $data == "TOK_DROPDATABASE" ){ return "drop database"; }
238
+
239
+
240
+
241
+ return $sql_type;
242
+
243
+ }
244
+
245
+ ```