forked from arzzcom/iModule.core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathiModule.class.php
More file actions
2809 lines (2456 loc) · 103 KB
/
iModule.class.php
File metadata and controls
2809 lines (2456 loc) · 103 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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* 이 파일은 iModule 의 일부입니다. (https://www.imodules.io)
*
* iModule core class 로 모든 사이트 레이아웃 및 모듈, 위젯, 애드온은 이 class 를 통해 호출된다.
* 이 class 는 index.php 파일에 의해 선언되며 iModule과 관련된 모든 파일에서 $IM 변수로 접근할 수 있다.
*
* @file /classes/iModule.class.php
* @author Arzz ([email protected])
* @license MIT License
* @version 3.0.0
* @modified 2019. 11. 26.
*/
class iModule {
/**
* iModule 실행모드
*/
private $mode = null;
/**
* DB 관련 변수정의
*
* @private DB $DB DB에 접속하고 데이터를 처리하기 위한 DB class (@see /classes/DB.class.php)
* @private string[] $table DB 테이블 별칭 및 원 테이블명을 정의하기 위한 변수
*/
private $DB;
private $table;
/**
* 사이트 주소에 의해 정의되는 사이트설정변수
* http://$domain/$language/$menu/$view/$idx
*/
public $domain;
public $language;
public $menu;
public $page;
public $view;
public $idx;
public $container = null;
public $indexUrl = null;
/**
* DB접근을 줄이기 위해 DB에서 불러온 데이터를 저장할 변수를 정의한다.
*
* @public object[] $sites : 사이트 설정값
* @public object[] $siteLinks : 사이트 링크값
* @public object[] $siteDefaultLanguages : 사이트 기본 언어셋
* @public object[] $menus : 사이트별 모든 메뉴설정값
* @public object[] $pages : 사이트별 특정 메뉴에 해당하는 모든 페이지설정값
* @public object[] $modules : 불러온 모듈 클래스
* @public object[] $plugins : 불러온 플러그인 클래스
*/
public $sites = array();
public $siteLinks = array();
public $siteDefaultLanguages = array();
public $menus = array();
public $pages = array();
public $sitemap = array();
public $modules = array();
public $plugins = array();
/**
* 언어셋을 정의한다.
*
* @private object $lang 현재 사이트주소에서 설정된 언어셋
* @private object $oLang package.json 에 의해 정의된 기본 언어셋
*/
private $lang = null;
private $oLang = null;
/**
* 각 기능별 core class 를 정의한다.
*
* @public Event $Event 이벤트처리를 위한 Event class (@see /classes/Event.class.php)
* @public Plugin $Plugin plugin을 정의하고 호출하기 위한 Plugin class (@see /classes/Plugin.class.php)
* @public Module $Module module을 정의하고 호출하기 위한 Module class (@see /classes/Module.class.php)
* @public Cache $Cache 캐싱처리를 위한 Cache class (@see /classes/Cache.class.php)
*/
public $Event;
public $Plugin;
public $Module;
public $Cache;
private $initTime = 0;
private $timezone; // server timezone
/**
* 사이트 설정변수
* 현재 접속한 사이트주소에 따라 접근한 사이트관련 정보들을 정의한다.
*
* @public object $site 현재 사이트에 관련된 모든 RAW 정보
* @public boolean $useTemplet 사이트템플릿 사용여부
* @private string $siteTitle 웹브라우저에 표시되는 사이트제목
* @private string $siteDescription SEO를 위한 META 태그에 정의될 사이트소개
* @private string $canonical SEO를 위한 현재 페이지에 접근할 수 있는 유니크한 사이트주소 (필수 GET 변수만 남겨둔 페이지 URL)
* @private string $robots SEO를 위한 검색로봇 색인규칙
* @private string $viewTitle META 태그를 위한 뷰페이지 제목 (각 모듈이나 애드온에서 페이지별로 변경할 수 있다.)
* @private string $viewDescription META 태그를 위한 뷰페이지 설명 (각 모듈이나 애드온에서 페이지별로 변경할 수 있다.)
* @private string $viewImage META 태그를 위한 뷰페이지 이미지 (각 모듈이나 애드온에서 페이지별로 변경할 수 있다.)
*/
public $site;
public $useTemplet = true;
private $siteTitle = null;
private $siteDescription = null;
private $canonical = null;
private $robots = null;
private $viewport = 'user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, width=device-width';
private $viewTitle = null;
private $viewDescription = null;
private $viewImage = null;
private $siteHeaders = array();
private $siteBodys = array();
private $siteTemplet = null;
private $javascriptLanguages = array();
private $webFonts = array('moimz'); // Moimz 폰트아이콘은 기본적으로 포함된다.
private $webFontDefault = null;
/**
* class 선언
*/
function __construct($mode=null) {
global $_CONFIGS;
$this->mode = $mode;
/**
* 페이지 로딩시간을 구하기 위한 최초 마이크로타임을 기록한다.
*/
$this->initTime = $this->getMicroTime();
/**
* 접속한 사이트주소 및 사이트변수 정의
*/
$this->site = null;
$this->domain = isset($_SERVER['HTTP_HOST']) == true ? strtolower($_SERVER['HTTP_HOST']) : '';
$this->language = Request('_language');
$this->menu = Request('_menu') == null ? 'index' : preg_replace('/[^a-zA-Z_0-9]/','',Request('_menu'));
$this->page = Request('_page') == null ? null : preg_replace('/[^a-zA-Z_0-9]/','',Request('_page'));
$this->view = Request('_view') == null ? null : Request('_view');
$this->idx = Request('_idx') == null || is_array(Request('_idx')) == true ? null : Request('_idx');
if ($mode !== 'SAFETY') {
/**
* cache처리를 위한 클래스를 정의한다.
*/
$this->Cache = new Cache($this);
/**
* iModule 이 설치되어 있다면 각 기능별 core class 를 호출한다.
* 이 클래스는 인스톨러에서도 사용되기 인스톨러에서 호출되면 에러가 발생하는 기능 class 를 비활성화 한다.
*/
if ($_CONFIGS->installed === true) {
$this->Event = new Event($this);
$this->Plugin = new Plugin($this);
$this->Module = new Module($this);
}
}
/**
* iModule core 에서 사용하는 DB 테이블 별칭 정의
* @see package.json 의 databases 참고
*/
$this->table = new stdClass();
$this->table->site = 'site_table';
$this->table->sitemap = 'sitemap_table';
$this->table->article = 'article_table';
/**
* 타임존 설정
* @todo 언젠가 사용할 예정
*/
$this->timezone = 'Asia/Seoul';
date_default_timezone_set($this->timezone);
/**
* 기본 사이트 자바스크립트 호출
*
* moment.js : 시간포맷을 위한 자바스크립트 라이브러리
* jquery.1.11.2.min.js : jQuery
* default.js : 기본 iModule 자바스크립트 라이브러리
*/
$this->addHeadResource('script',__IM_DIR__.'/scripts/moment.js');
$this->addHeadResource('script',__IM_DIR__.'/scripts/jquery.js');
$this->addHeadResource('script',__IM_DIR__.'/scripts/jquery.extend.js');
$this->addHeadResource('script',__IM_DIR__.'/scripts/common.js');
}
/**
* iModule 실행모드를 변경한다.
*
* @param string $mode
* @return iModule $this
*/
function setMode($mode) {
$this->mode = $mode;
return $this;
}
/**
* 인스톨과정에서 iModule core 클래스 정의가 필요할 경우 iModule core 를 정의한다.
*
* @return null
*/
function init() {
global $_CONFIGS;
$_CONFIGS->key = isset($_CONFIGS->key) == true ? $_CONFIGS->key : FileReadLine(__IM_PATH__.'/configs/key.config.php',1);
$_CONFIGS->db = isset($_CONFIGS->db) == true ? $_CONFIGS->db : json_decode(Decoder(FileReadLine(__IM_PATH__.'/configs/db.config.php',1)));
$this->Event = new Event($this); // ./classes/Event.class.php
$this->Plugin = new Plugin($this); // ./classes/Plugin.class.php
$this->Module = new Module($this); // ./classes/Module.class.php
$this->Cache = new Cache($this); // ./classes/Cache.class.php
}
/**
* 정상적으로 사이트에 접속시, 현재 접속한 사이트의 기본 URL을 구하고 사이트에 설정된 메뉴들을 저장한다.
*
* @param boolean $is_sitemap 사이트메뉴를 초기화할지 여부
*/
function initSites($is_sitemap=true) {
/**
* 모든 사이트의 RAW 데이터를 저장한다.
*/
$this->sites = $this->db()->select($this->table->site)->orderBy('sort','asc')->get();
/**
* @todo is_ssl 컬럼이 is_https 컬럼으로 변경됨에 따른 수정사항 (차후 제거필요)
*/
if (count($this->sites) > 0 && isset($this->sites[0]->is_https) == false) {
$this->db()->rawQuery("ALTER TABLE `im_site_table` CHANGE `is_ssl` `is_https` ENUM('TRUE','FALSE') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'FALSE' COMMENT 'HTTPS접속여부'");
return $this->initSites($is_sitemap);
}
$check = $this->db()->select($this->table->sitemap)->getOne();
if ($check != null && isset($check->permission) == false) {
$this->db()->rawQuery("ALTER TABLE `im_sitemap_table` ADD `permission` VARCHAR(255) NOT NULL DEFAULT 'true' COMMENT '권한' AFTER `context`;");
}
/**
* 현재 접속한 도메인에 해당하는 사이트가 없을 경우, 유사한 사이트를 찾는다.
*/
if ($this->db()->select($this->table->site)->where('domain',$this->domain)->has() == false) {
$isAlias = false;
for ($i=0, $loop=count($this->sites);$i<$loop;$i++) {
if ($this->sites[$i]->alias == '') continue;
/**
* 현재 접속한 도메인을 alias 로 가지고 있는 사이트를 탐색한다.
*/
$domains = explode(',',$this->sites[$i]->alias);
for ($j=0, $loopj=count($domains);$j<$loopj;$j++) {
if ($domains[$j] == $this->domain) {
$this->domain = $this->sites[$i]->domain;
$isAlias = true;
break;
}
if (preg_match('/\*\./',$domains[$j]) == true) {
$aliasToken = explode('.',$domains[$j]);
$domainToken = explode('.',$this->domain);
$isMatch = true;
while (count($aliasToken) > 0) {
$token = array_pop($aliasToken);
if ($token != '*' && $token != array_pop($domainToken)) {
$isMatch = false;
}
}
if ($isMatch == true) {
$this->domain = $this->sites[$i]->domain;
$isAlias = true;
break;
}
}
}
}
/**
* 전체 사이트 정보를 참고해도 현재 접속한 도메인의 사이트를 찾을 수 없을 경우 에러메세지를 출력한다.
*/
if ($isAlias == false) {
$this->printError('SITE_NOT_FOUND');
}
}
/**
* 언어설정값이 유효한지 확인한다.
*/
if ($this->language === null) {
/**
* 언어셋이 지정되지 않았을 경우 기본언어셋을 검색한다. 만약 찾을 수 없다면 에러메세지를 출력한다.
*/
$site = $this->db()->select($this->table->site)->where('domain',$this->domain)->where('is_default','TRUE')->getOne();
if ($site == null) $this->printError('LANGUAGE_NOT_FOUND');
$this->siteDefaultLanguages[$this->domain] = $site->language;
$this->language = $site->language;
} else {
/**
* 언어셋이 지정되었고, 해당 언어셋이 현재 사이트에서 사용중인지 확인한다. 만약 사용중인 언어셋이 아니라면 기본언어셋을 사용한다.
*/
$site = $this->db()->select($this->table->site)->where('domain',$this->domain)->where('language',$this->language)->getOne();
if ($site == null) {
$site = $this->db()->select($this->table->site)->where('domain',$this->domain)->where('is_default','TRUE')->getOne();
/**
* 기본 언어셋이 없을 경우 에러메세지를 출력한다.
*/
if ($site == null) $this->printError('LANGUAGE_NOT_FOUND');
}
}
/**
* 특수한 경우가 아닌 경우 사이트유효성 검사에 따라 확인된 URL로 이동한다.
*/
if (defined('__IM_SITE__') == true) {
if (($site->is_https == 'TRUE' && IsHttps() == false) || $_SERVER['HTTP_HOST'] != $site->domain || $this->language != $site->language) {
$redirectUrl = ($site->is_https == 'TRUE' ? 'https://' : 'http://').$site->domain;
if (defined('__IM_CONTAINER__') == true || isset($_SERVER['REDIRECT_URL']) == true) {
$redirectUrl.= preg_replace('/^\/'.$this->language.'/','/'.$site->language,$_SERVER['REDIRECT_URL']);
} else {
$redirectUrl.= __IM_DIR__;
if ($this->getDefaultLanguage($site->domain) != $site->language || $this->menu != 'index' || $this->page != null || $this->idx != null) {
$redirectUrl.= '/'.$site->language;
if ($this->menu != 'index' || $this->page != null || $this->idx != null) $redirectUrl.= '/'.$this->menu;
if ($this->page != null || $this->idx != null) $redirectUrl.= '/'.$this->page;
if ($this->idx != null) $redirectUrl.= '/'.$this->idx;
}
}
$redirectUrl.= $this->getQueryString();
header("HTTP/1.1 301 Moved Permanently");
header("location:".$redirectUrl);
exit;
}
}
if ($is_sitemap == true) {
/**
* 사이트에서 사용중인 1차메뉴 및 2차메뉴를 저장한다.
*/
$groups = array();
for ($i=0, $loop=count($this->sites);$i<$loop;$i++) {
$this->menus[$this->sites[$i]->domain.'@'.$this->sites[$i]->language] = array();
$this->pages[$this->sites[$i]->domain.'@'.$this->sites[$i]->language] = array();
$groups[$this->sites[$i]->domain.'@'.$this->sites[$i]->language] = array();
$sitemap = null;
/**
* 사이트 구성모듈이 있는 경우 해당 모듈을 통해 사이트맵을 가져온다.
*/
if (strpos($this->sites[$i]->templet,'#') === 0) {
$temp = explode('.',substr($this->sites[$i]->templet,1));
if ($this->getModule()->isSitemap($temp[0]) == true) {
$mModule = $this->getModule($temp[0]);
$sitemap = method_exists($mModule,'getSitemap') == true ? $mModule->getSitemap($this->sites[$i]->domain,$this->sites[$i]->language) : null;
}
}
$sitemap = $sitemap != null ? $sitemap : $this->db()->select($this->table->sitemap)->where('domain',$this->sites[$i]->domain)->where('language',$this->sites[$i]->language)->orderBy('sort','asc')->get();
for ($j=0, $loopj=count($sitemap);$j<$loopj;$j++) {
$sitemap[$j]->permission = isset($sitemap[$j]->permission) == true ? $this->parsePermissionString($sitemap[$j]->permission) : true;
$sitemap[$j]->is_hide = isset($sitemap[$j]->is_hide) == true && $sitemap[$j]->is_hide == 'TRUE';
$sitemap[$j]->is_footer = isset($sitemap[$j]->is_footer) == true && $sitemap[$j]->is_footer == 'TRUE';
$sitemap[$j]->header = json_decode($sitemap[$j]->header);
$sitemap[$j]->header = $sitemap[$j]->header == null ? json_decode('{"type":"NONE"}') : $sitemap[$j]->header;
$sitemap[$j]->footer = json_decode($sitemap[$j]->footer);
$sitemap[$j]->footer = $sitemap[$j]->footer == null ? json_decode('{"type":"NONE"}') : $sitemap[$j]->footer;
$sitemap[$j]->context = isset($sitemap[$j]->context) == true && $sitemap[$j]->context ? json_decode($sitemap[$j]->context) : null;
$sitemap[$j]->description = isset($sitemap[$j]->description) == true && $sitemap[$j]->description ? $sitemap[$j]->description : null;
if ($sitemap[$j]->type == 'MODULE') $sitemap[$j]->context->config = isset($sitemap[$j]->context->config) == true ? $sitemap[$j]->context->config : null;
if (isset($this->pages[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]) == false) {
$this->pages[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu] = array();
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu] = null;
}
if ($sitemap[$j]->page == '') {
$this->menus[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][] = $sitemap[$j];
} else {
if ($sitemap[$j]->type == 'GROUPSTART') {
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu] = new stdClass();
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]->code = substr($sitemap[$j]->page,1);
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]->title = $sitemap[$j]->title;
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]->icon = $sitemap[$j]->icon;
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]->pages = array();
continue;
}
if ($sitemap[$j]->type == 'GROUPEND') {
$groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu] = null;
continue;
}
if ($groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu] != null) $groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu]->pages[] = $sitemap[$j];
$sitemap[$j]->group = $groups[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu];
$this->pages[$sitemap[$j]->domain.'@'.$sitemap[$j]->language][$sitemap[$j]->menu][] = $sitemap[$j];
}
}
}
} else {
/**
* 사이트에서 사용중인 1차메뉴 및 2차메뉴를 저장한다.
*/
for ($i=0, $loop=count($this->sites);$i<$loop;$i++) {
$this->menus[$this->sites[$i]->domain.'@'.$this->sites[$i]->language] = array();
$this->pages[$this->sites[$i]->domain.'@'.$this->sites[$i]->language] = array();
}
}
}
/**
* DB클래스를 반환한다.
*
* @param string $code DB코드 (기본값 : default)
* @param string $prefix DB 테이블 앞에 고정적으로 사용되는 PREFIX 명 (정의되지 않을 경우 init.config.php 에서 정의된 __IM_DB_PREFIX__ 상수값을 사용한다.
* @return DB $DB
*/
function db($code='default',$prefix=null) {
if ($this->DB == null) $this->DB = new DB($this);
$prefix = $prefix === null ? __IM_DB_PREFIX__ : $prefix;
return $this->DB->get($code,$prefix);
}
/**
* Cache 클래스를 반환한다.
*
* @return Cache $cache
*/
function cache() {
return $this->Cache;
}
/**
* class 외부에서 DB 테이블 별칭으로 실제 테이블명을 가져온다.
*
* @param string $table DB 테이블 별칭
* @return string $tableName 실제 DB 테이블명
*/
function getTable($table) {
return $this->table->$table;
}
/**
* 코어이름을 반환한다.
*/
function getName() {
return 'core';
}
/**
* 언어셋파일에 정의된 코드를 이용하여 사이트에 설정된 언어별로 텍스트를 반환한다.
* 코드에 해당하는 문자열이 없을 경우 1차적으로 package.json 에 정의된 기본언어셋의 텍스트를 반환하고, 기본언어셋 텍스트도 없을 경우에는 코드를 그대로 반환한다.
*
* @param string $code 언어코드
* @param string $replacement 일치하는 언어코드가 없을 경우 반환될 메세지 (기본값 : null, $code 반환)
* @return string $language 실제 언어셋 텍스트
*/
function getText($code,$replacement=null) {
if ($this->lang == null) {
$package = json_decode(file_get_contents(__IM_PATH__.'/package.json'));
if (file_exists(__IM_PATH__.'/languages/'.$this->language.'.json') == true) {
$this->lang = json_decode(file_get_contents(__IM_PATH__.'/languages/'.$this->language.'.json'));
if ($this->language != $package->language) {
$this->oLang = json_decode(file_get_contents(__IM_PATH__.'/languages/'.$package->language.'.json'));
}
} else {
$this->lang = json_decode(file_get_contents(__IM_PATH__.'/languages/'.$package->language.'.json'));
$this->oLang = null;
}
}
$returnString = null;
$temp = explode('/',$code);
$string = $this->lang;
$oString = $this->oLang;
for ($i=0, $loop=count($temp);$i<$loop;$i++) {
if (isset($string->{$temp[$i]}) == true) {
$string = $string->{$temp[$i]};
} else {
$string = null;
break;
}
}
if ($string != null) {
$returnString = $string;
} elseif ($this->oLang != null) {
if ($string == null && $this->oLang != null) {
$string = $this->oLang;
for ($i=0, $loop=count($temp);$i<$loop;$i++) {
if (isset($string->{$temp[$i]}) == true) {
$string = $string->{$temp[$i]};
} else {
$string = null;
break;
}
}
}
if ($string != null) $returnString = $string;
}
$this->fireEvent('afterGetText','core',$code,$returnString);
if ($returnString == null) return $replacement === null ? $code : $replacement;
else return $returnString;
}
/**
* 상황에 맞게 에러코드를 반환한다.
*
* @param string $code 에러코드
* @param object $value(옵션) 에러와 관련된 데이터
* @param string $message(옵션) 변환된 에러메세지
*/
function getErrorText($code,$value=null,$message=null,$isRawData=false) {
if (is_object($code) == true) {
$message = $code->message;
$description = $code->description;
$type = $code->type;
} else {
$message = '';
if ($message == null) {
$message = $this->getText('error/'.$code,$code);
}
if ($message == $code) {
$message = $this->getText('error/UNKNOWN');
$description = $code;
$type = 'MAIN';
} else {
$description = null;
switch ($code) {
case 'PHP_ERROR' :
$description = 'File : '.$value['file'].'<br>Line : '.$value['line'].'<br><br>';
$description.= nl2br(str_replace(array('<','>'),array('<','>'),$value['message']));
$type = 'MAIN';
break;
case 'NOT_FOUND_PAGE' :
$description = GetString($value ? $value : $this->getUrl(),'replace');
$type = 'BACK';
break;
case 'REQUIRED_LOGIN' :
$type = 'LOGIN';
break;
default :
if ($value != null && is_string($value) == true) $description = $value;
$type = 'BACK';
}
$description = strlen($description) == 0 ? null : $description;
}
}
if ($isRawData === true) {
$data = new stdClass();
$data->message = $message;
$data->description = $description;
$data->type = $type;
return $data;
}
return $message.($description !== null ? ' ('.$description.')' : '');
}
/**
* 모듈 클래스를 불러온다.
* 이미 모듈 클래스가 선언되어 있다면 선언되어 있는 모듈클래스를 반환한다. (중복선언하지 않음)
*
* @param string $module(옵션) 모듈이름 (/modules 내부의 해당모듈의 폴더명)
* @param boolean $isForceLoad(옵션) 설치가 되지 않은 모듈이라도 강제로 모듈클래스를 호출할지 여부
* @param boolean $isClone(옵션) 메모리상에 있는 모듈클래스가 아닌 새로운 모듈클래스를 호출할지 여부
* @return object $module 모듈클래스
*/
function getModule($module=null,$isForceLoad=false,$isClone=false) {
if ($module == null) return $this->Module;
/**
* 항상 새로운 모듈 클래스를 반환할 경우
*/
if ($isClone == true) {
/**
* 모듈코어 클래스를 새로 선언하고, 모듈코어 클래스에서 모듈 클래스를 불러온다.
*/
$class = new Module($this);
$mModule = $class->load($module,$isForceLoad);
/**
* 모듈클래스를 호출하지 못했을 경우, 에러메세지를 출력한다.
*/
if ($mModule === false) $this->printError('LOAD_MODULE_FAIL : '.$module);
return $mModule;
}
/**
* 선언되어 있는 해당 모듈 클래스가 없을 경우, 새로 선언한다.
*/
if (isset($this->modules[$module]) == false) {
/**
* 모듈코어 클래스를 새로 선언하고, 모듈코어 클래스에서 모듈 클래스를 불러온다.
*/
$class = new Module($this);
$this->modules[$module] = $class->load($module,$isForceLoad);
}
/**
* 모듈클래스를 호출하지 못했을 경우, 에러메세지를 출력한다.
*/
if ($this->modules[$module] === false) $this->printError('LOAD_MODULE_FAIL : '.$module);
return $this->modules[$module];
}
/**
* 플러그인 클래스를 불러온다.
* 이미 플러그인 클래스가 선언되어 있다면 선언되어 있는 플러그인클래스를 반환한다. (중복선언하지 않음)
*
* @param string $plugin(옵션) 플러그인이름 (/plugins 내부의 해당모듈의 폴더명)
* @param boolean $isForceLoad(옵션) 설치가 되지 않은 플러그인이라도 강제로 플러그인클래스를 호출할지 여부
* @return object $plugin 플러그인 클래스
*/
function getPlugin($plugin=null,$isForceLoad=false) {
if ($plugin == null) return $this->Plugin;
/**
* 선언되어 있는 해당 플러그인 클래스가 없을 경우, 새로 선언한다.
*/
if (isset($this->plugins[$plugin]) == false) {
/**
* 모듈코어 클래스를 새로 선언하고, 모듈코어 클래스에서 모듈 클래스를 불러온다.
*/
$class = new Plugin($this);
$this->plugins[$plugin] = $class->load($plugin,$isForceLoad);
}
/**
* 모듈클래스를 호출하지 못했을 경우, 에러메세지를 출력한다.
*/
if ($this->plugins[$plugin] === false) $this->printError('LOAD_PLUGIN_FAIL : '.$plugin);
return $this->plugins[$plugin];
}
/**
* 위젯 클래스를 불러온다.
* 위젯은 하나의 페이지에 중복으로 사용할 수 있으므로, 무조건 새로운 클래스를 정의하여 반환한다.
*
* @param string $widget 위젯명 (/widgets 내부의 해당위젯의 폴더명)
* @return object $widget
*/
function getWidget($widget) {
$class = new Widget($this);
return $class->load($widget);
}
/**
* iModule 코어의 상대경로를 가져온다.
*
* @param string $dir
*/
function getDir() {
return __IM_DIR__;
}
/**
* iModule 코어의 절대경로를 가져온다.
*
* @param string $path
*/
function getPath() {
return __IM_PATH__;
}
/**
* 템플릿 객체를 가져온다.
*
* @param object $caller 템플릿을 요청하는 클래스 (iModule, Module, Widget)
* @param string $templet 템플릿명
* @return Templet $templet 템플릿 객체
*/
function getTemplet($caller,$templet) {
$class = new Templet($this);
return $class->load($caller,$templet);
}
/**
* 전체 템플릿목록을 가져온다.
*
* @param object $caller 템플릿목록을 요청하는 클래스 (iModule, Module, Plugin, Widget)
* @return Templet[] $templets 템플릿목록
*/
function getTemplets($caller) {
$class = new Templet($this);
return $class->getTemplets($caller);
}
/**
* 함수가 호출될 시점의 microtime 을 구한다.
*
* @return double $microtime
*/
function getMicroTime() {
$microtimestmp = explode(" ",microtime());
return $microtimestmp[0]+$microtimestmp[1];
}
/**
* iModule 이 선언되고 나서 함수가 호출되는 시점까지의 수행시간을 구한다.
*
* @return double $loadtime
*/
function getLoadTime() {
return sprintf('%0.5f',$this->getMicroTime() - $this->initTime);
}
/**
* 모든 첨부파일이 저장되는 절대경로를 반환한다.
*
* @return string $attachment_path
* @see /modules/ModuleAttachment.class.php
* @tode 첨부파일 저장되는 경로를 변경할 수 있는 설정값 추가
*/
function getAttachmentPath() {
global $_CONFIGS;
if (isset($_CONFIGS->attachment) == true && isset($_CONFIGS->attachment->path) == true) return $_CONFIGS->attachment->path;
return __IM_PATH__.'/attachments';
}
/**
* 모든 첨부파일이 저장되는 상대경로를 반환한다.
*
* @return string $attachment_dir
* @see /modules/ModuleAttachment.class.php
* @tode 첨부파일 저장되는 경로를 변경할 수 있는 설정값 추가
*/
function getAttachmentDir() {
global $_CONFIGS;
if (isset($_CONFIGS->attachment) == true && isset($_CONFIGS->attachment->dir) == true) return $_CONFIGS->attachment->dir;
return __IM_DIR__.'/attachments';
}
/**
* 현재 접속한 프로토콜(HTTP or HTTPS)를 포함한 Host정보를 구한다.
*
* @param boolean $isDir true : iModule 이 설치된 디렉토리 경로를 포함한다.
*/
function getHost($isDir=false) {
$url = IsHttps() == true ? 'https://' : 'http://';
$url.= $this->domain;
if ($isDir == true) $url.= __IM_DIR__;
return $url;
}
/**
* 메뉴 URL 을 구한다.
* 모든 파라매터값은 옵션이며 입력되지 않거나, NULL 일 경우 현재 접속한 페이지의 정보를 사용한다.
* 즉, 모든 파라매터값이 없는 상태로 호출하면 현재 페이지의 URL 을 구할 수 있다.
* 파라매터값을 false 로 설정하면 하위주소를 무시한다. $page 값이 false 일 경우 1차 메뉴주소까지만 반환한다.
*
* @param string $menu 1차 메뉴
* @param string $page 2차 메뉴
* @param string $view 모듈별 페이지종류 (목록페이지 또는 글쓰기페이지 등 : 모듈별로 사용되는 값이 다르다.)
* @param string $idx 모듈별 고유값 (게시물번호 또는 회원아이디 등 : 모듈별로 사용되는 값이 다르다.)
* @param boolean $isFullUrl true : 도메인을 포함한 전체 URL / false : 도메인을 제외한 URL(기본)
* @param string $domain 현재 접속한 도메인이 아닌 다른 사이트로 연결하고자 할 경우 해당 사이트의 도메인
* @param string $language 현재 접속한 언어설정이 아닌 다른 언어의 사이트로 연결하고자 할 경우 해당 언어셋 코드
* @return string $url;
*/
function getUrl($menu=null,$page=null,$view=null,$idx=null,$isFullUrl=false,$domain=null,$language=null) {
if ($this->container != null) {
$container = explode('/',$this->container);
$module = $container[0];
$container = $container[1];
if (defined('__IM_CONTAINER_POPUP__') == true) $container = '@'.$container;
return $this->getModuleUrl($module,$container,$view,$idx,$isFullUrl,$domain,$language);
}
/**
* 전달된 값이 없거나, NULL 일 경우 현재 페이지의 값으로 설정한다.
*/
$menu = $menu === null ? $this->menu : $menu;
$page = $page === null && $menu == $this->menu ? $this->page : $page;
$view = $view === null && $menu == $this->menu && $page == $this->page ? $this->view : $view;
$idx = $idx === null && $menu == $this->menu && $page == $this->page && $view == $this->view ? $this->idx : $idx;
/**
* $domain 의 값이 * 일 경우 현재 사이트의 도메인으로 설정한다.
*/
$domain = $domain == '*' ? $this->site->domain : $domain;
$context = $menu === null || $menu === false ? null : ($page === null || $page === false ? $this->getMenus($menu,$domain,$language) : $this->getPages($menu,$page,$domain,$language));
if ($context != null && isset($context->type) == true && $context->type == 'LINK') return $context->context->link.'#IM'.$context->context->target;
/**
* $isFullUrl 값이 true 이거나, 설정된 도메인이 현재 사이트의 도메인과 다를 경우 전체 URL 을 생성한다.
*/
if ($isFullUrl == true || ($domain != null && $domain !== $this->site->domain)) {
$domain = $domain == null ? $_SERVER['HTTP_HOST'] : $domain;
$check = $this->db()->select($this->table->site)->where('domain',$domain)->getOne();
if ($check == null) {
$url = IsHttps() == true ? 'https://' : 'http://';
$url.= $domain.__IM_DIR__;
} else {
$url = $check->is_https == 'TRUE' ? 'https://' : 'http://';
$url.= $domain.__IM_DIR__;
}
} else {
$url = __IM_DIR__;
}
/**
* 각각의 파라매터값이 false 가 아닐때까지 하위메뉴 주소를 만들고 반환한다.
*/
if ($language === false) return ($url ? $url : '/');
$url.= '/'.($language == null ? $this->language : $language);
if ($menu === null || $menu === false) return $url;
$url.= '/'.$menu;
if ($page === null || $page === false) return $url;
$url.= '/'.$page;
if ($view === null || $view === false) return $url;
$url.= '/'.$view;
if ($idx === null || $idx === false) return $url;
$url.= '/'.$idx;
return $url;
}
/**
* 특정 모듈의 특정 컨텍스트를 사용하도록 설정된 페이지 URL를 반환한다.
*
* @param string $module 모듈명
* @param string $context 컨텍스트명
* @param string[] $extacts 반드시 일치해야하는 컨텍스트 옵션
* @param string[] $options 반드시 일치할 필요는 없는 컨텍스트 옵션
* @param boolean $isSameDomain 현재 도메인 우선모드 (기본값 : false, true 일 경우 같은 도메인일 경우 우선, false 일 경우 $options 설정값에 우선)
* @param boolean $isFullUrl 전체경로여부
* @return string $url
*/
function getContextUrl($module,$context,$exacts=array(),$options=array(),$isSameDomain=false,$isFullUrl=false) {
$matches = $this->getContextPage($module,$context);
/**
* 설정과 일치하는 페이지가 없을 경우, NULL 을 반환한다.
*/
if (count($matches) == 0) return null;
/**
* 메뉴권한을 확인한다.
*/
$filters = array();
foreach ($matches as $match) {
if (isset($match->permission) == false || $this->parsePermissionString($match->permission) == true) {
$filters[] = $match;
}
}
$matches = $filters;
/**
* 반드시 일치해야하는 설정값을 가진 페이지를 탐색한다.
*/
$filters = array();
foreach ($matches as $match) {
$is_matched = true;
foreach ($exacts as $key=>$value) {
if (isset($match->context->configs->$key) == false || $match->context->configs->$key != $value) {
$is_matched = false;
break;
}
}
if ($is_matched == true) $filters[] = $match;
}
$matches = $filters;
/**
* 일치하는 페이지가 없을 경우 NULL 을 반환하고, 설정과 일치하는 페이지가 유일할 경우 해당 페이지를 반환한다.
*/
if (count($matches) == 0) return null;
if (count($matches) == 1) return $this->getUrl(false,false,false,false,$isFullUrl,$matches[0]->domain,$matches[0]->language).'/'.$matches[0]->url;
/**
* 설정과 일치하는 페이지가 2개 이상일 경우, $options 설정이나, $isSameDomain 설정에 따라 최대한 일치하는 페이지를 재탐색한다.
*/
$filters = array();
/**
* 같은 도메인 우선일 경우
*/
if ($isSameDomain == true) {
foreach ($matches as $match) {
if ($match->domain == $this->site->domain && $match->language == $this->language) $filters[] = $match;
}
if (count($filters) == 0) {
foreach ($matches as $match) {
if ($match->domain == $this->site->domain) $filters[] = $match;
}
}
/**
* 같은 도메인에 설정과 일치하는 페이지가 유일할 경우, 해당 페이지를 반환한다.
*/
if (count($filters) == 1) return $this->getUrl(false,false,false,false,$isFullUrl,$filters[0]->domain,$filters[0]->language).'/'.$filters[0]->url;
}
if (count($filters) > 0) $matches = $filters;
/**
* $options 설정과 최대한 많이 일치하는 페이지를 재탐색한다.
*/
$matchCount = 0;
$filters = array();
foreach ($matches as $match) {
/**
* 일치하는 $options 설정값 갯수
*/
$count = 0;
foreach ($options as $key=>$value) {
if (isset($match->context->configs->$key) == true && $match->context->configs->$key == $value) $count++;
}
if ($count > $matchCount) {
$filters = array($match);
$matchCount = $count;
} elseif ($count == $matchCount) {
$filters[] = $match;
}
}
if (count($filters) == 0) return null;
if (count($filters) == 1) return $this->getUrl(false,false,false,false,$isFullUrl,$filters[0]->domain,$filters[0]->language).'/'.$filters[0]->url;
foreach ($filters as $match) {
if ($match->domain == $this->site->domain && $match->language == $this->language) return $this->getUrl(false,false,false,false,$isFullUrl,$match->domain,$match->language).'/'.$match->url;
}
return $this->getUrl(false,false,false,false,$isFullUrl,$filters[0]->domain,$filters[0]->language).'/'.$filters[0]->url;
}
/**
* 특정 모듈의 특정 컨텍스트를 사용하도록 설정된 페이지를 반환한다.
*
* @param string $module 모듈명
* @param string $context 컨텍스트명
* @return object $matches 조건과 일치하는 사이트맵 페이지 객체
*/
function getContextPage($module,$context) {
$values = (object)get_defined_vars();
$matches = array();
$pages = $this->db()->select($this->table->sitemap,'domain,language,menu,page,permission,context')->where('type','MODULE')->where('context','{"module":"'.$module.'"%','LIKE')->get();
foreach ($pages as $page) {
$page->context = json_decode($page->context);
if ($context != $page->context->context) continue;
$page->url = $page->menu.'/'.$page->page;
$matches[] = $page;
}
/**
* 이벤트를 발생시켜 사이트맵 구조를 가지고 있는 다른 모듈로부터도 페이지 객체를 얻는다.
*/
$this->fireEvent('afterGetContextPage','core','context',$values,$matches);
return $matches;
}
/**
* 명령을 처리할 주소를 반환한다.
*
* @param string $module 모듈이름
* @param string $action 명령코드
* @param string[] $params 전달할 변수
* @param boolean $isFullUrl true : 도메인을 포함한 전체 URL / false : 도메인을 제외한 URL(기본)
*/
function getProcessUrl($module,$action,$params=array(),$isFullUrl=false) {
$queryStrings = array();
foreach ($params as $key=>$value) $queryStrings[] = $key.'='.urlencode($value);
if ($isFullUrl == true) {
$url = IsHttps() == true ? 'https://' : 'http://';
$url.= $_SERVER['HTTP_HOST'].__IM_DIR__;
} else {
$url = '';