source: trunk/Application/Core.php

Last change on this file was 10, checked in by chronos, 18 months ago
  • Fixed: More class type casting.
File size: 4.3 KB
Line 
1<?php
2
3$ConfigFileName = dirname(__FILE__).'/../Config/Config.php';
4if (file_exists($ConfigFileName)) include_once($ConfigFileName);
5
6include_once(dirname(__FILE__).'/../Packages/Common/PackageCommon.php');
7include_once(dirname(__FILE__).'/../Packages/FormManager/PackageFormManager.php');
8include_once(dirname(__FILE__).'/Version.php');
9include_once(dirname(__FILE__).'/BaseView.php');
10include_once(dirname(__FILE__).'/PageMissing.php');
11include_once(dirname(__FILE__).'/UpdateTrace.php');
12include_once(dirname(__FILE__).'/DefaultConfig.php');
13include_once(dirname(__FILE__).'/../Global.php');
14
15class Core extends System
16{
17 public bool $ShowPage;
18 public string $BaseURL;
19 public array $Config;
20 public array $HeadItems;
21 public array $Bars;
22 public array $PathItems;
23 public FormManager $FormManager;
24 public BaseView $BaseView;
25 public User $User;
26
27 function __construct()
28 {
29 parent::__construct();
30 $this->Pages = array();
31 $this->Bars = array();
32 $this->HeadItems = array();
33 $this->ShowPage = true;
34 $this->BaseURL = $_SERVER['SCRIPT_NAME'];
35 $BaseScriptName = '/index.php';
36 if (substr($this->BaseURL, -strlen($BaseScriptName), strlen($BaseScriptName)) == $BaseScriptName)
37 $this->BaseURL = substr($this->BaseURL, 0, -strlen($BaseScriptName));
38 $this->FormManager = new FormManager($this->Database);
39 }
40
41 function RunCommon()
42 {
43 global $Config, $DatabaseRevision;
44
45 session_start();
46
47 $ErrorHandler = new ErrorHandler();
48 $ErrorHandler->ShowError = $Config['Web']['ShowPHPError'];
49 $ErrorHandler->Start();
50
51 $this->Config = &$Config;
52
53 if (isset($this->Config['Database']))
54 {
55 $this->Database->Connect($Config['Database']['Host'], $Config['Database']['User'],
56 $Config['Database']['Password'], $Config['Database']['Database']);
57 $this->Database->Prefix = $Config['Database']['Prefix'];
58 $this->Database->charset($Config['Database']['Charset']);
59 $this->Database->ShowSQLError = $Config['Web']['ShowSQLError'];
60 $this->Database->ShowSQLQuery = $Config['Web']['ShowSQLQuery'];
61 }
62
63 // SQL injection hack protection
64 foreach ($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
65 foreach ($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
66
67 $this->RegisterPageBar('Top');
68 $this->RegisterPageBar('TopLeft');
69
70 $this->StartModules();
71 }
72
73 function StartModules(): void
74 {
75 $ModuleSetup = $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/ModuleSetup.php');
76 $ModuleSetup->Install();
77 $ModuleSetup->Start();
78 $this->ModuleManager->LoadModules();
79 $this->ModuleManager->LoadModule(dirname(__FILE__).'/../Packages/Common/Modules/ModuleAdmin.php');
80 if (file_exists($this->ModuleManager->FileName)) $this->ModuleManager->LoadState();
81 if (ModuleSetup::Cast($ModuleSetup)->CheckState())
82 {
83 $this->ModuleManager->StartAll(array(ModuleCondition::Enabled));
84 }
85 }
86
87 function Run(): void
88 {
89 $this->RunCommon();
90 if ($this->ShowPage)
91 {
92 $this->PathItems = ProcessURL();
93 $this->ShowPage();
94 }
95 }
96
97 function ShowPage()
98 {
99 $this->BaseView = new BaseView($this);
100
101 /* @var $Page Page */
102 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
103 if ($ClassName != '')
104 {
105 $Page = new $ClassName($this);
106 } else {
107 $Page = new PageMissing($this);
108 }
109 echo($this->BaseView->GetOutput($Page));
110 }
111
112 function RegisterPageBar($Name)
113 {
114 $this->Bars[$Name] = array();
115 }
116
117 function UnregisterPageBar($Name)
118 {
119 unset($this->Bars[$Name]);
120 }
121
122 function RegisterPageBarItem($BarName, $ItemName, $Callback)
123 {
124 $this->Bars[$BarName][$ItemName] = $Callback;
125 }
126
127 function SearchPage($PathItems, $Pages)
128 {
129 if (count($PathItems) > 0) $PathItem = $PathItems[0];
130 else $PathItem = '';
131 if (array_key_exists($PathItem, $Pages))
132 {
133 if (is_array($Pages[$PathItem]))
134 {
135 array_shift($PathItems);
136 return $this->SearchPage($PathItems, $Pages[$PathItem]);
137 } else return $Pages[$PathItem];
138 } else return '';
139 }
140
141 function Link($Target): string
142 {
143 return $this->BaseURL.$Target;
144 }
145
146 static function Cast(System $System): Core
147 {
148 if ($System instanceof Core)
149 {
150 return $System;
151 }
152 throw new Exception('Expected Core type but '.gettype($System));
153 }
154}
Note: See TracBrowser for help on using the repository browser.