Complex classes like Symphony often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Symphony, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | abstract class Symphony implements Singleton |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * An instance of the Symphony class, either `Administration` or `Frontend`. |
||
| 17 | * @var Symphony |
||
| 18 | */ |
||
| 19 | protected static $_instance = null; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * An instance of the Profiler class |
||
| 23 | * @var Profiler |
||
| 24 | */ |
||
| 25 | protected static $Profiler = null; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * An instance of the `Configuration` class |
||
| 29 | * @var Configuration |
||
| 30 | */ |
||
| 31 | private static $Configuration = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An instance of the `Database` class |
||
| 35 | * @var Database |
||
| 36 | */ |
||
| 37 | private static $Database = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * An instance of the `ExtensionManager` class |
||
| 41 | * @var ExtensionManager |
||
| 42 | */ |
||
| 43 | private static $ExtensionManager = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * An instance of the `Log` class |
||
| 47 | * @var Log |
||
| 48 | */ |
||
| 49 | private static $Log = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The current page namespace, used for translations |
||
| 53 | * @since Symphony 2.3 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | private static $namespace = false; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * An instance of the Cookie class |
||
| 60 | * @var Cookie |
||
| 61 | */ |
||
| 62 | public static $Cookie = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * An instance of the currently logged in Author |
||
| 66 | * @var Author |
||
| 67 | */ |
||
| 68 | public static $Author = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * A previous exception that has been fired. Defaults to null. |
||
| 72 | * @since Symphony 2.3.2 |
||
| 73 | * @var Exception |
||
| 74 | */ |
||
| 75 | private static $exception = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The Symphony constructor initialises the class variables of Symphony. At present |
||
| 79 | * constructor has a couple of responsibilities: |
||
| 80 | * - Start a profiler instance |
||
| 81 | * - If magic quotes are enabled, clean `$_SERVER`, `$_COOKIE`, `$_GET`, `$_POST` and the `$_REQUEST` arrays. |
||
| 82 | * - Initialise the correct Language for the currently logged in Author. |
||
| 83 | * - Start the session and adjust the error handling if the user is logged in |
||
| 84 | * |
||
| 85 | * The `$_REQUEST` array has been added in 2.7.0 |
||
| 86 | */ |
||
| 87 | protected function __construct() |
||
| 88 | { |
||
| 89 | self::$Profiler = Profiler::instance(); |
||
| 90 | |||
| 91 | if (get_magic_quotes_gpc()) { |
||
| 92 | General::cleanArray($_SERVER); |
||
| 93 | General::cleanArray($_COOKIE); |
||
| 94 | General::cleanArray($_GET); |
||
| 95 | General::cleanArray($_POST); |
||
| 96 | General::cleanArray($_REQUEST); |
||
| 97 | } |
||
| 98 | |||
| 99 | // Initialize language management |
||
| 100 | Lang::initialize(); |
||
| 101 | Lang::set(self::$Configuration->get('lang', 'symphony')); |
||
|
|
|||
| 102 | |||
| 103 | self::initialiseCookie(); |
||
| 104 | |||
| 105 | // If the user is not a logged in Author, turn off the verbose error messages. |
||
| 106 | GenericExceptionHandler::$enabled = self::isLoggedIn() && !is_null(self::$Author); |
||
| 107 | |||
| 108 | // Engine is ready. |
||
| 109 | self::$Profiler->sample('Engine Initialisation'); |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Setter for the Symphony Log and Error Handling system |
||
| 114 | * |
||
| 115 | * @since Symphony 2.6.0 |
||
| 116 | */ |
||
| 117 | public static function initialiseErrorHandler() |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Accessor for the Symphony instance, whether it be Frontend |
||
| 127 | * or Administration |
||
| 128 | * |
||
| 129 | * @since Symphony 2.2 |
||
| 130 | * @throws Exception |
||
| 131 | * @return Symphony |
||
| 132 | */ |
||
| 133 | public static function Engine() |
||
| 134 | { |
||
| 135 | if (class_exists('Administration', false)) { |
||
| 136 | return Administration::instance(); |
||
| 137 | } elseif (class_exists('Frontend', false)) { |
||
| 138 | return Frontend::instance(); |
||
| 139 | } else { |
||
| 140 | throw new Exception(__('No suitable engine object found')); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Setter for `$Configuration`. This function initialise the configuration |
||
| 146 | * object and populate its properties based on the given `$array`. Since |
||
| 147 | * Symphony 2.6.5, it will also set Symphony's date constants. |
||
| 148 | * |
||
| 149 | * @since Symphony 2.3 |
||
| 150 | * @param array $data |
||
| 151 | * An array of settings to be stored into the Configuration object |
||
| 152 | */ |
||
| 153 | public static function initialiseConfiguration(array $data = array()) |
||
| 154 | { |
||
| 155 | if (empty($data)) { |
||
| 156 | // Includes the existing CONFIG file and initialises the Configuration |
||
| 157 | // by setting the values with the setArray function. |
||
| 158 | include CONFIG; |
||
| 159 | |||
| 160 | $data = $settings; |
||
| 161 | } |
||
| 162 | |||
| 163 | self::$Configuration = new Configuration(true); |
||
| 164 | self::$Configuration->setArray($data); |
||
| 165 | |||
| 166 | // Set date format throughout the system |
||
| 167 | $region = self::Configuration()->get('region'); |
||
| 168 | define_safe('__SYM_DATE_FORMAT__', $region['date_format']); |
||
| 169 | define_safe('__SYM_TIME_FORMAT__', $region['time_format']); |
||
| 170 | define_safe('__SYM_DATETIME_FORMAT__', __SYM_DATE_FORMAT__ . $region['datetime_separator'] . __SYM_TIME_FORMAT__); |
||
| 171 | DateTimeObj::setSettings($region); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Accessor for the current `Configuration` instance. This contains |
||
| 176 | * representation of the the Symphony config file. |
||
| 177 | * |
||
| 178 | * @return Configuration |
||
| 179 | */ |
||
| 180 | public static function Configuration() |
||
| 181 | { |
||
| 182 | return self::$Configuration; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Is XSRF enabled for this Symphony install? |
||
| 187 | * |
||
| 188 | * @since Symphony 2.4 |
||
| 189 | * @return boolean |
||
| 190 | */ |
||
| 191 | public static function isXSRFEnabled() |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Accessor for the current `Profiler` instance. |
||
| 198 | * |
||
| 199 | * @since Symphony 2.3 |
||
| 200 | * @return Profiler |
||
| 201 | */ |
||
| 202 | public static function Profiler() |
||
| 203 | { |
||
| 204 | return self::$Profiler; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Setter for `$Log`. This function uses the configuration |
||
| 209 | * settings in the 'log' group in the Configuration to create an instance. Date |
||
| 210 | * formatting options are also retrieved from the configuration. |
||
| 211 | * |
||
| 212 | * @param string $filename (optional) |
||
| 213 | * The file to write the log to, if omitted this will default to `ACTIVITY_LOG` |
||
| 214 | * @throws Exception |
||
| 215 | * @return bool|void |
||
| 216 | */ |
||
| 217 | public static function initialiseLog($filename = null) |
||
| 218 | { |
||
| 219 | if (self::$Log instanceof Log && self::$Log->getLogPath() == $filename) { |
||
| 220 | return true; |
||
| 221 | } |
||
| 222 | |||
| 223 | if (is_null($filename)) { |
||
| 224 | $filename = ACTIVITY_LOG; |
||
| 225 | } |
||
| 226 | |||
| 227 | self::$Log = new Log($filename); |
||
| 228 | self::$Log->setArchive((self::Configuration()->get('archive', 'log') == '1' ? true : false)); |
||
| 229 | self::$Log->setMaxSize(self::Configuration()->get('maxsize', 'log')); |
||
| 230 | self::$Log->setFilter(self::Configuration()->get('filter', 'log')); |
||
| 231 | self::$Log->setDateTimeFormat(__SYM_DATETIME_FORMAT__); |
||
| 232 | |||
| 233 | if (self::$Log->open(Log::APPEND, self::Configuration()->get('write_mode', 'file')) == '1') { |
||
| 234 | self::$Log->initialise('Symphony Log'); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Accessor for the current `Log` instance |
||
| 240 | * |
||
| 241 | * @since Symphony 2.3 |
||
| 242 | * @return Log |
||
| 243 | */ |
||
| 244 | public static function Log() |
||
| 245 | { |
||
| 246 | return self::$Log; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Setter for `$Cookie`. This will use PHP's parse_url |
||
| 251 | * function on the current URL to set a cookie using the cookie_prefix |
||
| 252 | * defined in the Symphony configuration. The cookie will last two |
||
| 253 | * weeks. |
||
| 254 | * |
||
| 255 | * This function also defines two constants, `__SYM_COOKIE_PATH__` |
||
| 256 | * and `__SYM_COOKIE_PREFIX__`. |
||
| 257 | */ |
||
| 258 | public static function initialiseCookie() |
||
| 259 | { |
||
| 260 | define_safe('__SYM_COOKIE_PATH__', DIRROOT === '' ? '/' : DIRROOT); |
||
| 261 | define_safe('__SYM_COOKIE_PREFIX__', self::Configuration()->get('cookie_prefix', 'symphony')); |
||
| 262 | |||
| 263 | self::$Cookie = new Cookie(__SYM_COOKIE_PREFIX__, TWO_WEEKS, __SYM_COOKIE_PATH__); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Accessor for the current `$Cookie` instance. |
||
| 268 | * |
||
| 269 | * @since Symphony 2.5.0 |
||
| 270 | * @return Cookie |
||
| 271 | */ |
||
| 272 | public static function Cookie() |
||
| 273 | { |
||
| 274 | return self::$Cookie; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Setter for `$ExtensionManager` using the current |
||
| 279 | * Symphony instance as the parent. If for some reason this fails, |
||
| 280 | * a Symphony Error page will be thrown |
||
| 281 | * @param Boolean $force (optional) |
||
| 282 | * When set to true, this function will always create a new |
||
| 283 | * instance of ExtensionManager, replacing self::$ExtensionManager. |
||
| 284 | */ |
||
| 285 | public static function initialiseExtensionManager($force=false) |
||
| 286 | { |
||
| 287 | if (!$force && self::$ExtensionManager instanceof ExtensionManager) { |
||
| 288 | return true; |
||
| 289 | } |
||
| 290 | |||
| 291 | self::$ExtensionManager = new ExtensionManager; |
||
| 292 | |||
| 293 | if (!(self::$ExtensionManager instanceof ExtensionManager)) { |
||
| 294 | self::throwCustomError(__('Error creating Symphony extension manager.')); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Accessor for the current `$ExtensionManager` instance. |
||
| 300 | * |
||
| 301 | * @since Symphony 2.2 |
||
| 302 | * @return ExtensionManager |
||
| 303 | */ |
||
| 304 | public static function ExtensionManager() |
||
| 305 | { |
||
| 306 | return self::$ExtensionManager; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Setter for `$Database`, accepts a Database object. If `$database` |
||
| 311 | * is omitted, this function will set `$Database` to be of the `MySQL` |
||
| 312 | * class. |
||
| 313 | * |
||
| 314 | * @deprecated @since Symphony 3.0.0 - This function now does nothing |
||
| 315 | * @since Symphony 2.3 |
||
| 316 | * @param StdClass $database (optional) |
||
| 317 | * The class to handle all Database operations, if omitted this function |
||
| 318 | * will set `self::$Database` to be an instance of the `MySQL` class. |
||
| 319 | * @return boolean |
||
| 320 | * This function will always return true |
||
| 321 | */ |
||
| 322 | public static function setDatabase(StdClass $database = null) |
||
| 323 | { |
||
| 324 | return true; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Accessor for the current `$Database` instance. |
||
| 329 | * |
||
| 330 | * @return Database |
||
| 331 | */ |
||
| 332 | public static function Database() |
||
| 333 | { |
||
| 334 | return self::$Database; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * This will initialise the Database class and attempt to create a connection |
||
| 339 | * using the connection details provided in the Symphony configuration. If any |
||
| 340 | * errors occur whilst doing so, a Symphony Error Page is displayed. |
||
| 341 | * |
||
| 342 | * @throws SymphonyErrorPage |
||
| 343 | * @return boolean |
||
| 344 | * This function will return true if the `$Database` was |
||
| 345 | * initialised successfully. |
||
| 346 | */ |
||
| 347 | public static function initialiseDatabase() |
||
| 348 | { |
||
| 349 | $details = self::Configuration()->get('database'); |
||
| 350 | self::$Database = new Database($details); |
||
| 351 | |||
| 352 | try { |
||
| 353 | self::Database()->connect(); |
||
| 354 | |||
| 355 | if (!self::Database()->isConnected()) { |
||
| 356 | return false; |
||
| 357 | } |
||
| 358 | |||
| 359 | self::Database()->setTimeZone(self::Configuration()->get('timezone', 'region')); |
||
| 360 | } catch (DatabaseException $e) { |
||
| 361 | self::throwCustomError( |
||
| 362 | $e->getDatabaseErrorCode() . ': ' . $e->getDatabaseErrorMessage(), |
||
| 363 | __('Symphony Database Error'), |
||
| 364 | Page::HTTP_STATUS_ERROR, |
||
| 365 | 'database', |
||
| 366 | array( |
||
| 367 | 'error' => $e, |
||
| 368 | 'message' => __('There was a problem whilst attempting to establish a database connection. Please check all connection information is correct.') . ' ' . __('The following error was returned:') |
||
| 369 | ) |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | return true; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Accessor for the current `$Author` instance. |
||
| 378 | * |
||
| 379 | * @since Symphony 2.5.0 |
||
| 380 | * @return Author |
||
| 381 | */ |
||
| 382 | public static function Author() |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Attempts to log an Author in given a username and password. |
||
| 389 | * If the password is not hashed, it will be hashed using the sha1 |
||
| 390 | * algorithm. The username and password will be sanitized before |
||
| 391 | * being used to query the Database. If an Author is found, they |
||
| 392 | * will be logged in and the sanitized username and password (also hashed) |
||
| 393 | * will be saved as values in the `$Cookie`. |
||
| 394 | * |
||
| 395 | * @see toolkit.Cryptography#hash() |
||
| 396 | * @throws DatabaseException |
||
| 397 | * @param string $username |
||
| 398 | * The Author's username. This will be sanitized before use. |
||
| 399 | * @param string $password |
||
| 400 | * The Author's password. This will be sanitized and then hashed before use |
||
| 401 | * @param boolean $isHash |
||
| 402 | * If the password provided is already hashed, setting this parameter to |
||
| 403 | * true will stop it becoming rehashed. By default it is false. |
||
| 404 | * @return boolean |
||
| 405 | * true if the Author was logged in, false otherwise |
||
| 406 | */ |
||
| 407 | public static function login($username, $password, $isHash = false) |
||
| 408 | { |
||
| 409 | $username = trim($username); |
||
| 410 | $password = trim($password); |
||
| 411 | |||
| 412 | if (strlen($username) > 0 && strlen($password) > 0) { |
||
| 413 | $author = (new AuthorManager) |
||
| 414 | ->select() |
||
| 415 | ->username($username) |
||
| 416 | ->limit(1) |
||
| 417 | ->execute() |
||
| 418 | ->next(); |
||
| 419 | |||
| 420 | if ($author && Cryptography::compare($password, $author->get('password'), $isHash)) { |
||
| 421 | self::$Author = $author; |
||
| 422 | |||
| 423 | // Only migrate hashes if there is no update available as the update might change the tbl_authors table. |
||
| 424 | // Also, only upgrade if the password is clear text. |
||
| 425 | if (!self::isUpgradeAvailable() && !$isHash && Cryptography::requiresMigration(self::$Author->get('password'))) { |
||
| 426 | self::$Author->set('password', Cryptography::hash($password)); |
||
| 427 | |||
| 428 | self::Database() |
||
| 429 | ->update('tbl_authors') |
||
| 430 | ->set(['password' => self::$Author->get('password')]) |
||
| 431 | ->where(['id' => self::$Author->get('id')]) |
||
| 432 | ->execute(); |
||
| 433 | } |
||
| 434 | |||
| 435 | self::$Cookie->set('username', $username); |
||
| 436 | self::$Cookie->set('pass', self::$Author->get('password')); |
||
| 437 | |||
| 438 | self::Database() |
||
| 439 | ->update('tbl_authors') |
||
| 440 | ->set(['last_seen' => DateTimeObj::get('Y-m-d H:i:s')]) |
||
| 441 | ->where(['id' => self::$Author->get('id')]) |
||
| 442 | ->execute(); |
||
| 443 | |||
| 444 | // Only set custom author language in the backend |
||
| 445 | if (class_exists('Administration', false)) { |
||
| 446 | Lang::set(self::$Author->get('language')); |
||
| 447 | } |
||
| 448 | |||
| 449 | return true; |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | return false; |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Symphony allows Authors to login via the use of tokens instead of |
||
| 458 | * a username and password. |
||
| 459 | * A token is a random string of characters. |
||
| 460 | * This is a useful feature often used when setting up other Authors accounts or |
||
| 461 | * if an Author forgets their password. |
||
| 462 | * |
||
| 463 | * @param string $token |
||
| 464 | * The Author token |
||
| 465 | * @throws DatabaseException |
||
| 466 | * @return boolean |
||
| 467 | * true if the Author is logged in, false otherwise |
||
| 468 | */ |
||
| 469 | public static function loginFromToken($token) |
||
| 470 | { |
||
| 471 | $token = trim($token); |
||
| 472 | |||
| 473 | if (strlen($token) === 0) { |
||
| 474 | return false; |
||
| 475 | } |
||
| 476 | |||
| 477 | $am = new AuthorManager; |
||
| 478 | // Try with the password reset |
||
| 479 | $rowByResetPass = $am->fetchByPasswordResetToken($token); |
||
| 480 | if ($rowByResetPass) { |
||
| 481 | $row = $rowByResetPass; |
||
| 482 | // consume the token |
||
| 483 | self::Database() |
||
| 484 | ->delete('tbl_forgotpass') |
||
| 485 | ->where(['token' => $token]) |
||
| 486 | ->execute(); |
||
| 487 | } else { |
||
| 488 | // Fallback to auth token |
||
| 489 | $row = $am->fetchByAuthToken($token); |
||
| 490 | } |
||
| 491 | |||
| 492 | if ($row) { |
||
| 493 | self::$Author = $row; |
||
| 494 | self::$Cookie->set('username', $row['username']); |
||
| 495 | self::$Cookie->set('pass', $row['password']); |
||
| 496 | return self::Database() |
||
| 497 | ->update('tbl_authors') |
||
| 498 | ->set(['last_seen' => DateTimeObj::get('Y-m-d H:i:s')]) |
||
| 499 | ->where(['id' => $row['id']]) |
||
| 500 | ->execute() |
||
| 501 | ->success(); |
||
| 502 | } |
||
| 503 | |||
| 504 | return false; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * This function will destroy the currently logged in `$Author` |
||
| 509 | * session, essentially logging them out. |
||
| 510 | * |
||
| 511 | * @see core.Cookie#expire() |
||
| 512 | */ |
||
| 513 | public static function logout() |
||
| 514 | { |
||
| 515 | self::$Cookie->expire(); |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * This function determines whether an there is a currently logged in |
||
| 520 | * Author for Symphony by using the `$Cookie`'s username |
||
| 521 | * and password. If the instance is not found, they will be logged |
||
| 522 | * in using the cookied credentials. |
||
| 523 | * |
||
| 524 | * @see login() |
||
| 525 | * @return boolean |
||
| 526 | */ |
||
| 527 | public static function isLoggedIn() |
||
| 528 | { |
||
| 529 | // Check to see if we already have an Author instance. |
||
| 530 | if (self::$Author) { |
||
| 531 | return true; |
||
| 532 | } |
||
| 533 | |||
| 534 | // No author instance found, attempt to log in with the cookied credentials |
||
| 535 | return self::login(self::$Cookie->get('username'), self::$Cookie->get('pass'), true); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Returns the most recent version found in the `/install/migrations` folder. |
||
| 540 | * Returns a semver version string if an updater |
||
| 541 | * has been found. |
||
| 542 | * Returns `false` otherwise. |
||
| 543 | * |
||
| 544 | * @since Symphony 2.3.1 |
||
| 545 | * @return string|boolean |
||
| 546 | */ |
||
| 547 | public static function getMigrationVersion() |
||
| 548 | { |
||
| 549 | if (self::isInstallerAvailable() && class_exists('Updater')) { |
||
| 550 | $migrations = Updater::getAvailableMigrations(); |
||
| 551 | $m = end($migrations); |
||
| 552 | if (!$m) { |
||
| 553 | return false; |
||
| 554 | } |
||
| 555 | return $m->getVersion(); |
||
| 556 | } |
||
| 557 | |||
| 558 | return false; |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Checks if an update is available and applicable for the current installation. |
||
| 563 | * |
||
| 564 | * @since Symphony 2.3.1 |
||
| 565 | * @return boolean |
||
| 566 | */ |
||
| 567 | public static function isUpgradeAvailable() |
||
| 568 | { |
||
| 569 | if (self::isInstallerAvailable()) { |
||
| 570 | $migration_version = self::getMigrationVersion(); |
||
| 571 | $current_version = Symphony::Configuration()->get('version', 'symphony'); |
||
| 572 | |||
| 573 | return \Composer\Semver\Comparator::lessThan($current_version, $migration_version); |
||
| 574 | } |
||
| 575 | |||
| 576 | return false; |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Checks if the installer/updater is available. |
||
| 581 | * |
||
| 582 | * @since Symphony 2.3.1 |
||
| 583 | * @return boolean |
||
| 584 | */ |
||
| 585 | public static function isInstallerAvailable() |
||
| 586 | { |
||
| 587 | return file_exists(DOCROOT . '/install/index.php'); |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * A wrapper for throwing a new Symphony Error page. |
||
| 592 | * |
||
| 593 | * @see core.SymphonyErrorPage |
||
| 594 | * @param string|XMLElement $message |
||
| 595 | * A description for this error, which can be provided as a string |
||
| 596 | * or as an XMLElement. |
||
| 597 | * @param string $heading |
||
| 598 | * A heading for the error page |
||
| 599 | * @param integer $status |
||
| 600 | * Properly sets the HTTP status code for the response. Defaults to |
||
| 601 | * `Page::HTTP_STATUS_ERROR`. Use `Page::HTTP_STATUS_XXX` to set this value. |
||
| 602 | * @param string $template |
||
| 603 | * A string for the error page template to use, defaults to 'generic'. This |
||
| 604 | * can be the name of any template file in the `TEMPLATES` directory. |
||
| 605 | * A template using the naming convention of `tpl.*.php`. |
||
| 606 | * @param array $additional |
||
| 607 | * Allows custom information to be passed to the Symphony Error Page |
||
| 608 | * that the template may want to expose, such as custom Headers etc. |
||
| 609 | * @throws SymphonyErrorPage |
||
| 610 | */ |
||
| 611 | public static function throwCustomError($message, $heading = 'Symphony Fatal Error', $status = Page::HTTP_STATUS_ERROR, $template = 'generic', array $additional = array()) |
||
| 612 | { |
||
| 613 | throw new SymphonyErrorPage($message, $heading, $template, $additional, $status); |
||
| 614 | } |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Setter accepts a previous Throwable. Useful for determining the context |
||
| 618 | * of a current Throwable (ie. detecting recursion). |
||
| 619 | * |
||
| 620 | * @since Symphony 2.3.2 |
||
| 621 | * |
||
| 622 | * @since Symphony 2.7.0 |
||
| 623 | * This function works with both Exception and Throwable |
||
| 624 | * Supporting both PHP 5.6 and 7 forces use to not qualify the $e parameter |
||
| 625 | * |
||
| 626 | * @param Throwable $ex |
||
| 627 | */ |
||
| 628 | public static function setException($ex) |
||
| 629 | { |
||
| 630 | self::$exception = $ex; |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Accessor for `self::$exception`. |
||
| 635 | * |
||
| 636 | * @since Symphony 2.3.2 |
||
| 637 | * @return Throwable|null |
||
| 638 | */ |
||
| 639 | public static function getException() |
||
| 640 | { |
||
| 641 | return self::$exception; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Returns the page namespace based on the current URL. |
||
| 646 | * A few examples: |
||
| 647 | * |
||
| 648 | * /login |
||
| 649 | * /publish |
||
| 650 | * /blueprints/datasources |
||
| 651 | * [...] |
||
| 652 | * /extension/$extension_name/$page_name |
||
| 653 | * |
||
| 654 | * This method is especially useful in couple with the translation function. |
||
| 655 | * |
||
| 656 | * @see toolkit#__() |
||
| 657 | * @return string |
||
| 658 | * The page namespace, without any action string (e.g. "new", "saved") or |
||
| 659 | * any value that depends upon the single setup (e.g. the section handle in |
||
| 660 | * /publish/$handle) |
||
| 661 | */ |
||
| 662 | public static function getPageNamespace() |
||
| 663 | { |
||
| 664 | if (self::$namespace !== false) { |
||
| 665 | return self::$namespace; |
||
| 666 | } |
||
| 667 | |||
| 668 | $page = getCurrentPage(); |
||
| 669 | |||
| 670 | if (!is_null($page)) { |
||
| 671 | $page = trim($page, '/'); |
||
| 672 | } |
||
| 673 | |||
| 674 | if (substr($page, 0, 7) == 'publish') { |
||
| 675 | self::$namespace = '/publish'; |
||
| 676 | } elseif (empty($page) && isset($_REQUEST['mode'])) { |
||
| 677 | self::$namespace = '/login'; |
||
| 678 | } elseif (empty($page)) { |
||
| 679 | self::$namespace = null; |
||
| 680 | } else { |
||
| 681 | $bits = explode('/', $page); |
||
| 682 | |||
| 683 | if ($bits[0] == 'extension') { |
||
| 684 | self::$namespace = sprintf('/%s/%s/%s', $bits[0], $bits[1], $bits[2]); |
||
| 685 | } else { |
||
| 686 | self::$namespace = sprintf('/%s/%s', $bits[0], isset($bits[1]) ? $bits[1] : ''); |
||
| 687 | } |
||
| 688 | } |
||
| 689 | |||
| 690 | return self::$namespace; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * The `SymphonyErrorPageHandler` extends the `GenericExceptionHandler` |
||
| 696 | * to allow the template for the exception to be provided from the `TEMPLATES` |
||
| 697 | * directory |
||
| 698 | */ |
||
| 699 | class SymphonyErrorPageHandler extends GenericExceptionHandler |
||
| 700 | { |
||
| 701 | /** |
||
| 702 | * The render function will take a `SymphonyErrorPage` exception and |
||
| 703 | * output a HTML page. This function first checks to see if the `GenericExceptionHandler` |
||
| 704 | * is enabled and pass control to it if not. After that, the method checks if there is a custom |
||
| 705 | * template for this exception otherwise it reverts to using the default |
||
| 706 | * `usererror.generic.php`. If the template is not found, it will call |
||
| 707 | * `GenericExceptionHandler::render()`. |
||
| 708 | * |
||
| 709 | * @param Throwable $e |
||
| 710 | * The Throwable object |
||
| 711 | * @return string |
||
| 712 | * An HTML string |
||
| 713 | */ |
||
| 714 | public static function render($e) |
||
| 730 | } |
||
| 731 | } |
||
| 732 | |||
| 733 | /** |
||
| 734 | * `SymphonyErrorPage` extends the default `Exception` class. All |
||
| 735 | * of these exceptions will halt execution immediately and return the |
||
| 736 | * exception as a HTML page. By default the HTML template is `usererror.generic.php` |
||
| 737 | * from the `TEMPLATES` directory. |
||
| 738 | */ |
||
| 739 | |||
| 740 | class SymphonyErrorPage extends Exception |
||
| 741 | { |
||
| 957 |