This repository was archived by the owner on Dec 6, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcontroller.inc
More file actions
566 lines (497 loc) · 15.8 KB
/
Copy pathcontroller.inc
File metadata and controls
566 lines (497 loc) · 15.8 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
/**
* コントローラクラス
*
* MVCにおけるCを担当するクラス
*
* @package magicweapon
* @link http://www.grid-works-guild.net/MagicWeapon/ MagicWeapon
* @access public
* @author Michiaki Wuzawa <wuzawa@m-fr.net>
* @create $LastChangedDate$
* @version $Revision$
**/
require_once('cgi_request.inc');
require_once('cgi_response.inc');
require_once('load_manager.inc');
require_once('security.inc');
require_once('multimap.inc');
require_once('mw_config.inc');
require_once('mobile_info.inc');
require_once('user_agent_check.inc');
require_once('config_extension.inc');
require_once('di.inc');
require_once('mw_set_ini_encoding.inc');
class controller {
//public:
// configインスタンスのアクセサ
protected function set_config_obj($o) { $this->cfobj_ = $o; }
public function get_config_obj() { return $this->cfobj_; }
//
protected function set_cgi_request_obj($o) { $this->reqobj_ = $o; }
public function get_cgi_request_obj() { return $this->reqobj_; }
//
protected function set_cgi_response_obj($o) { $this->resobj_ = $o; }
public function get_cgi_response_obj() { return $this->resobj_; }
//
protected function set_bag_obj($o) { $this->bag_ = $o; }
public function get_bag_obj() { return $this->bag_; }
//
protected function set_mobile_obj($o) { $this->mobobj_ = $o; }
public function get_mobile_obj() { return $this->mobobj_; }
//
protected function set_output_encoding_obj($o)
{ $this->output_encoding_obj_ = $o; }
public function get_output_encoding_obj()
{ return $this->output_encoding_obj_; }
//
public function set_base_path($s) { $this->bp_ = $s; }
public function get_base_path() { return $this->bp_; }
public function __construct()
{
// バッファリング開始
//ob_clean(); // XXX エラーを引き起こす可能性があるので一端コメントアウト
ob_start();
//
$this->init();
}
/**
* 初期化処理
*
* @access public
*/
public function init()
{
$this->set_config_obj(new config_extension);
$this->set_mobile_obj(new mobile_info);
$this->set_base_path("");
}
/**
* インスタンス生成
*
* request & response をはじめとしてさまざまなインスタンスを生成
* bag系など
*
* @access public
* @return boolean 何かあったらfalse
*/
public function make_instancees()
{
// 鞄インスタンス生成
// XXX 準備いらないし直で
$this->set_bag_obj( new multimap() );
// 内部エンコード設定
$inside_encoding_type = $this->get_config_obj()->find('inside_encoding_type');
if ('' === $inside_encoding_type) {
$inside_encoding_type = 'UTF-8';
}
mw_set_ini_encoding($inside_encoding_type);
// 入出力エンコード設定
$output_encoding_type = $this->get_config_obj()->find('output_encoding_type');
if ('' === $output_encoding_type) {
$output_encoding_type = 'UTF-8';
}
switch(strtolower($output_encoding_type)) {
case 'utf-8':
case 'utf-8-mobile#docomo':
case 'utf-8-mobile#kddi-a':
case 'utf-8-mobile#kddi-b':
case 'utf-8-mobile#softbank':
$e = 'utf-8';
break;
case 'euc-jp':
case 'eucjp-win':
case 'cp51932':
$e = 'euc-jp';
break;
case 'sjis':
case 'sjis-win':
case 'cp932':
case 'sjis-mac':
case 'sjis-mobile#docomo':
case 'sjis-mobile#kddi':
case 'sjis-mobile#softbank':
$e = 'Shift_JIS';
break;
case 'jis':
case 'jis-ms':
case 'cp50220':
case 'cp50220raw':
case 'cp50221':
case 'cp50222':
case 'iso-2022-jp':
case 'iso-2022-jp-ms':
$e = 'ISO-2022-JP';
break;
case 'auto':
if (true === $this->get_mobile_obj()->is_mobile()) {
$e = 'Shift_JIS';
$output_encoding_type = 'sjis-win';
} else {
$e = 'utf-8';
$output_encoding_type = 'utf-8';
}
break;
default:
throw new ErrorException('encode type invalid!!');
break;
}
// 定数の設定
define("MW_INPUT_ENCODING", $output_encoding_type);
define("MW_OUTPUT_ENCODING", $output_encoding_type);
define("MW_OUTPUT_CONTENT_TYPE_ENCODING", $e);
// CGI REQUESTインスタンスの生成
$req = $this->get_instance('cgi_request', 'cgi_request.inc:cgi_request');
// 一応「Post Get」順とする
// XXX 後でコンフィグではじく?
$req->set_hi_priority_post();
//
$this->set_cgi_request_obj($req);
// CGI RESPONSEインスタンスの生成
$res = new cgi_response;
// 出力のデフォルトを「HTML」にしておく + 文字コード指定
$res->set_content_html(MW_OUTPUT_CONTENT_TYPE_ENCODING);
//
$this->set_cgi_response_obj($res);
// テンプレートエンジンインスタンス
// XXX 場合によっては邪魔な可能性もあるけど、まぁ、大抵使うでしょ
$teobj = $this->get_instance('template_engine', 'secure_conv.inc:secure_conv');
// テンプレートエンジンごとの設定
// XXX 外に切り出すかねぇ…Smarty以外の追加もあるだろうしねぇ…
// Smarty系列の設定
if (true === is_subclass_of($teobj, 'Smarty')) {
$teobj->compile_dir = $this->get_config_obj()->find('smarty_compile_dir');
$teobj->template_dir = $this->get_config_obj()->find('template_dir');
$teobj->config_dir = $this->get_config_obj()->find('smarty_config_dir');
}
//var_dump($teobj);
$this->get_bag_obj()->insert('conv', $teobj);
//
return true;
}
/**
* configファイル設定
*
* configファイル名を設定:その後必要な処理を走らせる
*
* @access public
* @param string $fn configファイル名
* @return boolean 何かあったらfalse
*/
public function set_config($fn)
{
// まずはlocalなコンフィグを取り込む
$bp = $this->get_base_path();
if ("" != $bp) {
$this->get_config_obj()->set_base_path($bp);
}
$this->get_config_obj()->set_file($fn);
//var_dump($this->get_config_obj());
////////////////////////////////
// コンフィグからの設定各種
////////////////////////////////
// common_include_dirの解決っていうかset_ini
foreach ($this->get_config_obj()->find_array('common_include_dir') as $path) {
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
//print $path . "\n";
//print get_include_path() . "<BR>\n";
}
// DIコンテナ設定を取り込んでおく
$s = $this->get_config_obj()->find('di_config');
if ('' !== $s) {
di::set_config($s);
}
// data_clump関連の設定
require_once('data_clump_manager.inc');
//
data_clump_manager::set_config($this->get_config_obj()->find('data_clump_config'));
//
return true;
}
/**
* 入り口チェック
*
* あらゆる処理の前に走るメソッド
* ・user-agent-arrow または user-agent-denny が設定されていれば機種チェック
*
*
*
* @access public
* @return boolean 問題があればfalse
*/
public function entrance_check()
{
$ret = true;
// 機種判定
// XXX 両方設定されているケースはとりあえず未考慮
$afn = $this->get_config_obj()->find('user-agent-arrow');
$dfn = $this->get_config_obj()->find('user-agent-denny');
//var_dump($afn);
//var_dump($dfn);
if ( ("" !== $afn) || ("" !== $dfn) ) {
$obj = new user_agent_check;
$obj->set_mobile_obj($this->get_mobile_obj());
$fn = "";
if ("" !== $afn) {
$fn = $afn;
$obj->set_arrow_string(file_get_contents($fn));
} else {
$fn = $dfn;
$obj->set_denny_string(file_get_contents($fn));
}
//var_dump($fn);
// 判定
$ret = $obj->is_permission();
// XXX アクセス不可な時の処理
if (false === $ret) {
$uri = $this->get_config_obj()->find('user-agent-disapprove_uri');
if ('' !== $uri) {
// XXX ちぃとベタ
header('Location:' . $uri);
exit;
}
// else
// XXX …これもかなりベタ orz
echo 'your user-agent is denny.';
exit;
}
}
//var_dump($ret);
//var_dump( $this->get_config_obj()->find('only_mobile') );
if ('on' === $this->get_config_obj()->find('only_mobile')) {
$mm = $this->get_mobile_obj();
//var_dump($mm);
//
if (false === $mm->is_mobile()) {
// XXX ちぃとベタ
header('Location:' . $this->get_config_obj()->find('no_mobile_uri'));
exit;
}
// uidチェック
$id = $mm->get_sid();
//var_dump($id);
if ('' === $id) {
// XXX ちぃとベタ
header('Location:' . $this->get_config_obj()->find('no_uid_uri'));
exit;
}
}
//
return $ret;
}
/**
* 出口チェック
*
* あらゆる処理の後に走るメソッド
* 継承先で必要なら実装してください
*
* @access public
* @return boolean 問題があればfalse
*/
public function exit_check() { return true; }
/**
* 入力データパース&デコード
*
* PHP的にはやることないなぁ
*
* @access public
* @return boolean 問題があればfalse
*/
public function parse_decode()
{
//
return true;
}
/**
* Model割り当て部分メイン
*
* @access public
* @param string $cstring コマンド名
* @param int $recursive_counter 再帰用カウンタ
* @return base_model_inc base_model系インスタンス
*/
public function run_main($cstring, $recursive_counter)
{
//print "run_main c = $cstring\n";
//var_dump($this->mapdata_);
//var_dump($recursive_counter);
//exit;
// ダイレクトに「一文字めアンダースコア」なcstring呼び出したときNG
if ( (0 === $recursive_counter)&&('_' === $cstring[0]) ) {
throw new ErrorException("\"" . $cstring . "\" is invalid command(2)!!");
}
// 探してみる。無ければエラーで突っ返す
if (is_null($model_data = $this->mapdata_->find($cstring))) {
throw new ErrorException("\"" . $cstring . "\" is invalid command!!");
}
//var_dump($model_data);
//print "include is " . $model_data->get_include_filename() . "<BR>\n";
//print "class is " . $model_data->get_classname() . "<BR>\n";
// include処理
// XXX load_fileのルーチンだとmodelのエラーが出てこない…
require_once($model_data->get_include_filename());
// オブジェクト作成
$classname = $model_data->get_classname();
$model_ins = new $classname;
// 必要な情報の設定
// XXX 基本的にreferer渡しなので情報は共有できるはず
$model_ins->set_config($this->get_config_obj());
$model_ins->set_cgi_request($this->get_cgi_request_obj());
$model_ins->set_cgi_response($this->get_cgi_response_obj());
$model_ins->set_my_name($cstring);
$model_ins->set_bag_obj($this->get_bag_obj());
$model_ins->set_mobile($this->get_mobile_obj());
//
$model_ins->set_bag('recursive_counter', $recursive_counter);
//print "\nbag \n";
//var_dump($this->get_bag_obj());
// mapファイルに設定があってかつそれが上書きされてなきゃ設定する〜
//print "Trap 1\n";
//var_dump($model_data->get_template_filename());
//var_dump($model_ins->is_tmp_filename());
if (("" !== $model_data->get_template_filename())&&(false === $model_ins->is_tmp_filename())) {
//print "Trap 2\n";
$model_ins->set_tmp_filename($model_data->get_template_filename());
}
if (("" !== $model_data->get_error_template_filename())&&(false === $model_ins->is_error_tmp_filename())) {
//print "Trap 3\n";
$model_ins->set_error_tmp_filename($model_data->get_error_template_filename());
}
// インスタンスにキック
$r = $model_ins->initialize();
// 明示的にinitializeでfalseが帰ってきたときだけ、一切の処理をしない
if (false !== $r) {
if ($model_ins->is_recursive_on()) {
// 再帰的読み込みっぽいので、引き続きcall
$m_ins = $this->run_main($model_ins->get_recursive_command(), $recursive_counter + 1);
} else {
$model_ins->execute(); // ここでBodyが作られてる…はず!!
if ($model_ins->is_recursive_on()) {
// 再帰的読み込みっぽいので、引き続きcall
$m_ins = $this->run_main($model_ins->get_recursive_command(), $recursive_counter + 1);
// 戻ってきた値を横取りして抱え込む
// XXX base64とか増えたら処理ふやさんとなぁ
$model_ins->set_body($m_ins->get_body());
}
}
// 終了処理
$model_ins->finalize();
}
//var_dump($model_ins);
// で、復帰
// XXX インスタンスそのものを復帰させる。後でbase64とかでてくるから
//print "\nbag \n";
//var_dump($this->get_bag_obj());
return $model_ins;
}
/**
* メイン処理
*
* 必要な処理を必要な順番でcallする
*
* @access public
*/
public function run()
{
// 例外処理捕捉開始
try {
// インスタンス生成
if (!($this->make_instancees())) {
throw new ErrorException("make re instance error!!");
}
// 入り口チェック
if (!($this->entrance_check())) {
throw new ErrorException("entrance ceck error!!");
}
// 入力データパース&デコード
if (!($this->parse_decode())) {
throw new ErrorException("parse or decode ceck error!!");
}
// マッピング
// マッピングファイルの取得
$map_file = $this->get_config_obj()->find('mapping_file_path');
//print $map_file . "\n";
// マッピングファイルの解体
//$this->mapdata_ = new config;
//$this->mapdata_->set_file($map_file);
$this->mapdata_ = new mw_config;
$this->mapdata_->set_file($map_file);
// Controller メイン
// スイッチ文字列の取得
// XXX cも可変にしたほうがいいんだろうなぁ…
$cstring = $this->get_cgi_request_obj()->find("c");
if ("" == $cstring) {
// XXX どうする? 一応indexに張るけど
$cstring = "index";
}
// 指定されたmodelを叩く
// XXX return値、stringでいいのか???
// XXX よくないからインスタンスげとしてみた
//print "cstring is $cstring \n";
$model_obj = $this->run_main($cstring, 0);
// View処理
if (true === $model_obj->is_direct_output_on()) {
//
$this->get_cgi_response_obj()->set_nocache();
$this->get_cgi_response_obj()->put();
//
echo $model_obj->get_body();
} else {
// XXX ちと過去との互換性用に、すこしだけごにょった処理〜
if ('' != $this->get_config_obj()->find('view_class')) {
// こっちが「古い遺産」用の、後で消したいコード
$vobj = $this->get_instance('view_class', 'view.inc:view');
} else {
// こっちが本道
$vobj = $this->get_instance('view', 'view.inc:view');
}
//
$vobj->set_body($model_obj->get_body());
$vobj->set_res_obj($this->get_cgi_response_obj());
$vobj->set_model_obj($model_obj);
$vobj->output();
}
// XXX ここでよいか?
ob_end_flush();
} catch (Exception $e) {
// XXX
print "get exception!!!\n";
print security::sanitize_html($e->getMessage()) . "\n";
//print $e->getCode() . "\n";
//print $e->getFile() . "\n";
//print $e->getLine() . "\n";
//print_r($e->getTrace());
//print $e->getTraceAsString() . "\n";
}
// うちとこの終了処理
// XXX tryの中にいれる?
$this->exit_check();
}
// DIで吸収してみた〜
protected function get_instance($name, $default)
{
// View処理
$v = $this->get_config_obj()->find($name);
if ("" !== $v) {
//
$awk = explode(":", $v);
$inc = trim($awk[0]);
$class = trim($awk[1]);
//
require_once($inc);
$obj = new $class;
} else {
$obj = di::create($name, $default);
}
//
return $obj;
}
//private:
private $bp_; // base_path(基底になるパス)
private $cfobj_; // サイト固有のconfig。configクラスインスタンス
private $mobobj_; // mobile_infoクラスのインスタンス
private $reqobj_; // CGI REQUEST インスタンス
private $resobj_; // CGI RESPONSE インスタンス
private $mapdata_; // マッピングファイル用:configインスタンス
private $bag_obj_; // model持ち回り用鞄インスタンス
} // end of class