1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @author Pierre-Henry Soria <[email protected]> |
4
|
|
|
* @copyright (c) 2012-2019, Pierre-Henry Soria. All Rights Reserved. |
5
|
|
|
* @license GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory. |
6
|
|
|
* @link http://ph7cms.com |
7
|
|
|
* @package PH7 / App / Core |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PH7; |
11
|
|
|
|
12
|
|
|
defined('PH7') or exit('Restricted access'); |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use PH7\App\Includes\Classes\Loader\Autoloader as AppLoader; |
16
|
|
|
use PH7\Framework\Config\Config; |
17
|
|
|
use PH7\Framework\Config\FileNotFoundException; |
18
|
|
|
use PH7\Framework\Core\Kernel; |
19
|
|
|
use PH7\Framework\Error\CException as Except; |
20
|
|
|
use PH7\Framework\File\Import; |
|
|
|
|
21
|
|
|
use PH7\Framework\Loader\Autoloader as FrameworkLoader; |
22
|
|
|
use PH7\Framework\Mvc\Router\FrontController; |
23
|
|
|
use PH7\Framework\Navigation\Browser; |
24
|
|
|
use PH7\Framework\Registry\Registry; |
25
|
|
|
use PH7\Framework\Server\Environment as Env; |
26
|
|
|
use PH7\Framework\Server\Server; |
27
|
|
|
|
28
|
|
|
/*** Begin Loading Files ***/ |
29
|
|
|
require 'configs/constants.php'; |
30
|
|
|
require 'includes/helpers/misc.php'; |
31
|
|
|
|
32
|
|
|
class Bootstrap |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Bootstrap $oInstance |
36
|
|
|
*/ |
37
|
|
|
private static $oInstance = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set constructor/cloning to private since it's a singleton class. |
41
|
|
|
*/ |
42
|
|
|
private function __construct() {} |
43
|
|
|
private function __clone() {} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get instance of class. |
47
|
|
|
* |
48
|
|
|
* @return Bootstrap Returns the instance class or create initial instance of the class. |
49
|
|
|
*/ |
50
|
|
|
public static function getInstance() |
51
|
|
|
{ |
52
|
|
|
return null === static::$oInstance ? static::$oInstance = new static : static::$oInstance; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set a default timezone if it is not already configured in environment. |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function setTimezoneIfNotSet() |
61
|
|
|
{ |
62
|
|
|
if (!ini_get('date.timezone')) { |
63
|
|
|
ini_set('date.timezone', PH7_DEFAULT_TIMEZONE); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initialize the app, load the files and launch the main FrontController router. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
* |
72
|
|
|
* @throws Exception |
73
|
|
|
* @throws Except\PH7Exception |
74
|
|
|
* @throws Except\UserException |
75
|
|
|
* @throws FileNotFoundException |
76
|
|
|
*/ |
77
|
|
|
public function run() |
78
|
|
|
{ |
79
|
|
|
try { |
80
|
|
|
$this->loadInitFiles(); |
81
|
|
|
|
82
|
|
|
//** Temporary code. In the near future, pH7CMS will be usable without mod_rewrite |
83
|
|
|
if (!Server::isRewriteMod()) { |
|
|
|
|
84
|
|
|
$this->notRewriteModEnabledError(); |
85
|
|
|
exit; |
|
|
|
|
86
|
|
|
} //*/ |
87
|
|
|
|
88
|
|
|
// Enable client browser cache |
89
|
|
|
(new Browser)->cache(); |
90
|
|
|
|
91
|
|
|
new Server; // Start Server |
92
|
|
|
|
93
|
|
|
$this->startPageBenchmark(); |
94
|
|
|
//Framework\Compress\Compress::setZlipCompression(); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// Initialize the FrontController, we are asking the front controller to process the HTTP request |
97
|
|
|
FrontController::getInstance()->runRouter(); |
98
|
|
|
/** TODO: When pH7CMS will support PHP 7.1 |
99
|
|
|
} catch (FileNotFoundException | Except\UserException $oE) { |
100
|
|
|
//*/ |
101
|
|
|
} catch (FileNotFoundException $oE) { |
102
|
|
|
echo $oE->getMessage(); |
103
|
|
|
} catch (Except\UserException $oE) { |
104
|
|
|
echo $oE->getMessage(); // Simple User Error with Exception |
105
|
|
|
} catch (Except\PH7Exception $oE) { |
106
|
|
|
Except\PH7Exception::launch($oE); |
107
|
|
|
} catch (Exception $oE) { |
108
|
|
|
Except\PH7Exception::launch($oE); |
109
|
|
|
} finally { |
110
|
|
|
$this->closeAppSession(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Load all necessary files for running the app. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
private function loadInitFiles() |
120
|
|
|
{ |
121
|
|
|
// Load Framework Classes |
122
|
|
|
require PH7_PATH_FRAMEWORK . 'Loader/Autoloader.php'; |
123
|
|
|
FrameworkLoader::getInstance()->init(); |
124
|
|
|
|
125
|
|
|
/** Loading configuration files environments **/ |
126
|
|
|
// For All environment |
127
|
|
|
Import::file(PH7_PATH_APP . 'configs/environment/all.env'); |
128
|
|
|
// Specific to the current environment |
129
|
|
|
Import::file(PH7_PATH_APP . 'configs/environment/' . Env::getFileName(Config::getInstance()->values['mode']['environment'])); |
130
|
|
|
|
131
|
|
|
// Load Class ~/protected/app/includes/classes/* |
132
|
|
|
Import::pH7App('includes.classes.Loader.Autoloader'); |
133
|
|
|
AppLoader::getInstance()->init(); |
134
|
|
|
|
135
|
|
|
// Load Debug class |
136
|
|
|
Import::pH7FwkClass('Error.Debug'); |
137
|
|
|
|
138
|
|
|
// Load String Class |
139
|
|
|
Import::pH7FwkClass('Str.Str'); |
140
|
|
|
|
141
|
|
|
/* Structure/General.class.php functions are not currently used */ |
142
|
|
|
// Import::pH7FwkClass('Structure.General'); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Initialize the benchmark time. It is calculated in Framework\Layout\Html\Design::stat() |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
private function startPageBenchmark() |
151
|
|
|
{ |
152
|
|
|
Registry::getInstance()->start_time = microtime(true); |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* If sessions status are enabled, writes session data and ends session. |
157
|
|
|
* |
158
|
|
|
* @return void |
159
|
|
|
*/ |
160
|
|
|
private function closeAppSession() |
161
|
|
|
{ |
162
|
|
|
if (session_status() === PHP_SESSION_ACTIVE) { |
163
|
|
|
session_write_close(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Display an error message if the Apache mod_rewrite is not enabled. |
169
|
|
|
* |
170
|
|
|
* @return void HTML output. |
171
|
|
|
*/ |
172
|
|
|
private function notRewriteModEnabledError() |
173
|
|
|
{ |
174
|
|
|
$sMsg = '<p class="warning"><a href="' . Kernel::SOFTWARE_WEBSITE . '">pH7CMS</a> requires Apache "mod_rewrite".</p> |
175
|
|
|
<p>Firstly, please <strong>make sure the ".htaccess" file has been uploaded to the root directory where pH7CMS is installed</strong>. If not, use your FTP client (such as Filezilla) and upload it again from pH7CMS unziped package and try again.<br /> |
176
|
|
|
Secondly, please <strong>make sure "mod_rewrite" is correctly installed</strong>.<br /> Click <a href="http://ph7cms.com/doc/en/how-to-install-rewrite-module" target="_blank" rel="noopener">here</a> if you want to get more information on how to install the rewrite module.<br /><br /> |
177
|
|
|
After that, please <a href="' . PH7_URL_ROOT . '">retry</a>.</p>'; |
178
|
|
|
|
179
|
|
|
echo html_body("Apache's mod_rewrite is required", $sMsg); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.