This repository was archived by the owner on Dec 6, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcgi_request.inc
More file actions
406 lines (360 loc) · 9.19 KB
/
Copy pathcgi_request.inc
File metadata and controls
406 lines (360 loc) · 9.19 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
<?php
/**
* CGI リクエストクラス
*
* CGIリクエストをEUCに正しく変換して突っ返す
* 他言語だとインスタンス必須だが、PHPではいらないかも
* XXX バッファリングによる性能向上は後日マターって感じで
*
* @package magicweapon
* @access public
* @author Michiaki Wuzawa <wuzawa@m-fr.net>
* @create yyyy/mm/dd
* @version $Id$
**/
//require_once('debug_util.inc');
//
class cgi_request {
//
public function __construct()
{
$this->init();
}
//
public function init()
{
// デフォルトをここにしておく
$this->set_hi_priority_post();
// 文字コードが未指定なら、デフォルトUTF-8で設定しておく
if (false === defined('MW_INPUT_ENCODING')) {
define('MW_INPUT_ENCODING', 'UTF-8');
}
//
if ('' === mb_internal_encoding()) {
mb_internal_encoding('UTF-8');
}
}
/**
* 非推奨なので気にしないように
*/
public function set_priority($s) { $this->priority_ = $s; }
public function get_priority() { return $this->priority_; }
/**
* GetもPostも見に行くが、Post情報を優先させる
* @access public
*/
public function set_hi_priority_post() { $this->priority_ = "pg"; }
/**
* GetもPostも見に行くが、Get情報を優先させる
* @access public
*/
public function set_hi_priority_get() { $this->priority_ = "gp"; }
/**
* Getのみを見に行く
* @access public
*/
public function set_only_get() { $this->priority_ = "g"; }
/**
* Postのみを見に行く
* @access public
*/
public function set_only_post() { $this->priority_ = "p"; }
/**
*
* 全部の名前取得:PostとGet
*
*/
public function get_all_names()
{
$p_key = array_keys($_POST);
$g_key = array_keys($_GET);
// 配列を足しこむ
$ret = array_unique(array_merge($p_key, $g_key));
//
return $ret;
}
/**
* 下請けの下請け。[配列 | 単変数]を配列に設定する
* XXX これってUTIL行きなのか?
* @access protected
* @param &array $base 値を入れ込むべき要素
* @param array $add 足したい配列
*/
protected function _add_array(&$base, $add)
{
if (is_array($add)) {
foreach ($add as $wk) {
array_push($base, $wk);
}
} else {
array_push($base, $add);
}
}
/**
* 下請け関数。Get情報を取得
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return arrayまたはstring 結果(の配列)。値が無い場合はnullを返す
*/
public function _get_get($key) {
if (isset($_GET[$key])) {
return $_GET[$key];
}
return null;
}
/**
* 下請け関数。Post情報を取得
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return arrayまたはstring 結果(の配列)。値が無い場合はnullを返す
*/
public function _get_post($key) {
if (isset($_POST[$key])) {
return $_POST[$key];
}
return null;
}
/*
* 指定されたキーの存在チェック
*/
public function is_exist($key)
{
// キー指定がなければ突っ返す
if ("" == $key) {
return false;
}
// 判定開始
$flg = false;
$length = strlen($this->priority_);
for($i = 0; $i < $length; $i ++) {
$itr = $this->priority_[$i];
// pならpost情報を取得
if ("p" == $itr) {
$wk = $this->_get_post($key);
}
// gならget情報を取得
if ("g" == $itr) {
$wk = $this->_get_get($key);
}
// データがあったらtrueに
if (!(is_null($wk))) {
$flg = true;
}
}
//
return $flg;
}
/**
* GetもしくはPostから情報を検索、結果を配列にして返す
* XXX データは全て文字コードをEUCに直してから返す
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return array(string) 結果の配列。ない場合は空配列を返す
*/
public function find_array($key = "")
{
$ret = array();
// キー指定がなければ突っ返す
if ("" == $key) {
return $ret;
}
// 判定
$length = strlen($this->priority_);
for($i = 0; $i < $length; $i ++) {
$itr = $this->priority_[$i];
// pならpost情報を取得
if ("p" == $itr) {
$wk = $this->_get_post($key);
}
// gならget情報を取得
if ("g" == $itr) {
$wk = $this->_get_get($key);
}
// データがあったっぽいなら
if (!(is_null($wk))) {
//debug_util::dump_string_put($wk);
// 足しこむ
$this->_add_array($ret, $wk);
}
}
// 文字コード置換
$eret = array();
foreach ($ret as $buf) {
//debug_util::dump_string_put($buf);
// XXX magic_quote対策追加
if (1 === get_magic_quotes_gpc()) {
//print "magic_quote<br>\n";
$buf = stripslashes($buf);
}
//print "befor buf $buf<br>";
//debug_util::dump_string_put($buf);
// 絵文字処理用(一旦ダミーメソッド)
$buf = $this->interruption_emoji($buf);
//print "befor buf $buf<br>";
//debug_util::dump_string_put($buf);
// 文字コード変換
$buf = mb_convert_encoding($buf, mb_internal_encoding(), MW_INPUT_ENCODING);
//debug_util::dump_string_put($buf);
//print "\n";
//var_dump($buf);
array_push($eret, $buf);
}
//
return $eret;
}
// 単数用ラッパー
/**
* GetもしくはPostから情報を検索、結果を単値で返す
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return string 結果。ない場合はから文字を返す
*/
public function find($key = "")
{
$ret = $this->find_array($key);
if (0 == count($ret)) {
return "";
}
//
return $ret[0];
}
/**
* Cookieから情報を検索、結果を配列にして返す
*
* XXX とりあえず文字コード置換はかけない
*
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return array(string) 結果の配列。ない場合は空配列を返す
*/
public function find_cookie_array($key = "")
{
$ret = array();
// キー指定がなければ突っ返す
if ("" == $key) {
return $ret;
}
// データが無ければ突っ返す
if (!(isset($_COOKIE[$key]))) {
return $ret;
}
// データ取得
$wk = $_COOKIE[$key];
// 配列ではない(単値)の場合
if (!(is_array($wk))) {
// 配列に
$ret = array($wk);
} else {
// そのまんま
$ret = $wk;
}
// magic_quoteがoffなら
if (0 === get_magic_quotes_gpc()) {
return $ret;
}
// onなら
$ret2 = array();
foreach ($ret as $buf) {
// XXX magic_quote対策追加
$ret2[] = stripslashes($buf);
}
//
return $ret2;
}
// 単数用ラッパー
/**
* Cookieから情報を検索、結果を返す
* @access public
* @param string $key 検索用のkey。name=valueのname部分に相当
* @return string 結果。ない場合はから文字を返す
*/
public function find_cookie($key = "")
{
$ret = $this->find_cookie_array($key);
if (0 == count($ret)) {
return "";
}
//
return $ret[0];
}
/*
XXX 作る?
get_path_info()
*/
/**
* 1画面に複数遷移先があるときの判断を一発で
* XXX type="image"が未考慮
*
* 使う側のイメージとしては
* // デフォルトは配列のtop
* $btn_name_array = ('rss_list_button', 'rss_result_button');
* $btn_name = $req->choice($btn_name_array);
*
* // デフォルトを明示的に指定
* $btn_name_array = ('rss_list_button', 'rss_result_button');
* $btn_name = $req->choice($btn_name_array, 'no_choice');
*
* @access public
* @param array $names チェック対象のname一覧
* @param string $default 対象がすべてNGだった時のデフォルト
* @return array(string) 結果の配列。ない場合は空配列を返す
*/
public function choice($names, $default = "")
{
// デフォルト設定。ない場合は「配列の先頭」を選択
$ret = $default;
if ("" == $ret) {
$ret = $names[0];
}
// チェック
foreach ($names as $name) {
// 見つかったら
if ("" != $this->find($name)) {
// 設定して抜け出す
$ret = $name;
break;
}
}
//
return $ret;
}
/**
* uploadファイルの取得
*
* @access public
* @param string $name FORMで指定したname
* @param boolean $encode_flg trueなら文字codeをencodeする。defaultはfalse
* @return string ファイルの中身。エラーの場合空文字列
*/
public function get_file($name, $encode_flg = false)
{
// 一応チェック
if (false === is_uploaded_file($_FILES[$name]['tmp_name'])) {
return "";
}
// XXX move_uploaded_file はしない。tmpの場所指定とか面倒だし
// XXX あとで別途「get_file_to_file」とかつくるかも
// 中身をとりだす
$s = file_get_contents($_FILES[$name]['tmp_name']);
// もしencode必要ならする
if (true === $encode_flg) {
$s = mb_convert_encoding($s, mb_internal_encoding(), MW_INPUT_ENCODING);
}
//
return $s;
}
/**
* 絵文字用のポリモフィズム用ダミーメソッド
*
* @access public
* @param string $s データ
* @return string 出力
*/
public function interruption_emoji($s)
{
//
return $s;
}
//private:
private $priority_;
} // end of class