source: trunk/Application/Core.php@ 8

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