source: trunk/Application/Core.php

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