source: trunk/Application/Core.php

Last change on this file was 95, checked in by chronos, 4 years ago
  • Modified: Updated Common package.
  • Added: Explicit types for better type checking.
  • Fixed: Support for php 8.0.
File size: 3.9 KB
RevLine 
[64]1<?php
2
[69]3$ConfigFileName = dirname(__FILE__).'/../Config/Config.php';
[92]4if (file_exists($ConfigFileName)) include_once($ConfigFileName);
[69]5
6include_once(dirname(__FILE__).'/../Global.php');
7include_once(dirname(__FILE__).'/Version.php');
[95]8include_once(dirname(__FILE__).'/BaseView.php');
[69]9include_once(dirname(__FILE__).'/UpdateTrace.php');
10include_once(dirname(__FILE__).'/DefaultConfig.php');
11
[95]12class Core extends System
[64]13{
[95]14 public array $Pages;
15 public bool $ShowPage;
16 public string $BaseURL;
17 public bool $UseSession;
[67]18
[69]19 function __construct()
[64]20 {
[69]21 parent::__construct();
22 $this->Pages = array();
[75]23 $this->UseSession = true;
[69]24 $this->ShowPage = true;
25 $this->BaseURL = $_SERVER['SCRIPT_NAME'];
[92]26 if (substr($this->BaseURL, -10, 10) == '/index.php')
[69]27 $this->BaseURL = substr($this->BaseURL, 0, -10);
28 }
29
[95]30 function RunCommon(): void
[69]31 {
[74]32 global $Config, $DatabaseRevision, $WithoutSessionStart;
[64]33
[92]34 if ($this->UseSession) session_start();
[64]35
36 $ErrorHandler = new ErrorHandler();
37 $ErrorHandler->ShowError = $Config['Web']['ShowError'];
[84]38 //$ErrorHandler->Start();
[64]39
[95]40 try
41 {
[78]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
[64]52 // SQL injection hack protection
[92]53 foreach ($_POST as $Index => $Item) $_POST[$Index] = addslashes($Item);
54 foreach ($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item);
[64]55
[69]56 $this->Config = &$Config;
[67]57
[95]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())
[69]70 {
[95]71 $this->ModuleManager->StartAll(array(ModuleCondition::Enabled));
[69]72 }
[64]73 }
[69]74
[95]75 function Run(): void
[69]76 {
77 $this->RunCommon();
[92]78 if ($this->ShowPage)
[69]79 {
80 $this->PathItems = ProcessURL();
81 $this->ShowPage();
82 }
83 }
84
[95]85 static function Cast(System $System): Core
[69]86 {
[95]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 {
[69]96 $this->BaseView = new BaseView($this);
97
98 /* @var $Page Page */
99 $ClassName = $this->SearchPage($this->PathItems, $this->Pages);
[92]100 if ($ClassName != '')
[69]101 {
102 $Page = new $ClassName($this);
103 } else {
104 $Page = new PageMissing($this);
105 }
106 echo($this->BaseView->GetOutput($Page));
107 }
108
[95]109 function SearchPage(array $PathItems, array $Pages): string
[69]110 {
[92]111 if (count($PathItems) > 0) $PathItem = $PathItems[0];
[69]112 else $PathItem = '';
[92]113 if (array_key_exists($PathItem, $Pages))
[69]114 {
[92]115 if (is_array($Pages[$PathItem]))
[69]116 {
117 array_shift($PathItems);
[92]118 return $this->SearchPage($PathItems, $Pages[$PathItem]);
119 } else return $Pages[$PathItem];
120 } else return '';
[69]121 }
122
[95]123 function Link(string $Target): string
[69]124 {
[92]125 return $this->BaseURL.$Target;
[69]126 }
[67]127}
[69]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
[95]138 function Show(): string
[69]139 {
140 Header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');
[92]141 return '<h3 align="center">'.T('Required page not found');
[69]142 }
143}
Note: See TracBrowser for help on using the repository browser.