| Total Complexity | 77 | 
| Total Lines | 718 | 
| Duplicated Lines | 0 % | 
| Changes | 6 | ||
| Bugs | 0 | Features | 0 | 
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.
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 MySQL | ||
| 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 |         if (!self::isLoggedIn() && is_null(self::$Author)) { | ||
| 107 | GenericExceptionHandler::$enabled = false; | ||
| 108 | } | ||
| 109 | |||
| 110 | // Engine is ready. | ||
| 111 |         self::$Profiler->sample('Engine Initialisation'); | ||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * Setter for the Symphony Log and Error Handling system | ||
| 116 | * | ||
| 117 | * @since Symphony 2.6.0 | ||
| 118 | */ | ||
| 119 | public static function initialiseErrorHandler() | ||
| 120 |     { | ||
| 121 | // Initialise logging | ||
| 122 | self::initialiseLog(); | ||
| 123 | GenericExceptionHandler::initialise(self::Log()); | ||
| 124 | GenericErrorHandler::initialise(self::Log()); | ||
| 125 | } | ||
| 126 | |||
| 127 | /** | ||
| 128 | * Accessor for the Symphony instance, whether it be Frontend | ||
| 129 | * or Administration | ||
| 130 | * | ||
| 131 | * @since Symphony 2.2 | ||
| 132 | * @throws Exception | ||
| 133 | * @return Symphony | ||
| 134 | */ | ||
| 135 | public static function Engine() | ||
| 136 |     { | ||
| 137 |         if (class_exists('Administration', false)) { | ||
| 138 | return Administration::instance(); | ||
| 139 |         } elseif (class_exists('Frontend', false)) { | ||
| 140 | return Frontend::instance(); | ||
| 141 |         } else { | ||
| 142 |             throw new Exception(__('No suitable engine object found')); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | /** | ||
| 147 | * Setter for `$Configuration`. This function initialise the configuration | ||
| 148 | * object and populate its properties based on the given `$array`. Since | ||
| 149 | * Symphony 2.6.5, it will also set Symphony's date constants. | ||
| 150 | * | ||
| 151 | * @since Symphony 2.3 | ||
| 152 | * @param array $data | ||
| 153 | * An array of settings to be stored into the Configuration object | ||
| 154 | */ | ||
| 155 | public static function initialiseConfiguration(array $data = array()) | ||
| 156 |     { | ||
| 157 |         if (empty($data)) { | ||
| 158 | // Includes the existing CONFIG file and initialises the Configuration | ||
| 159 | // by setting the values with the setArray function. | ||
| 160 | include CONFIG; | ||
| 161 | |||
| 162 | $data = $settings; | ||
| 163 | } | ||
| 164 | |||
| 165 | self::$Configuration = new Configuration(true); | ||
| 166 | self::$Configuration->setArray($data); | ||
| 167 | |||
| 168 | // Set date format throughout the system | ||
| 169 |         $region = self::Configuration()->get('region'); | ||
| 170 |         define_safe('__SYM_DATE_FORMAT__', $region['date_format']); | ||
| 171 |         define_safe('__SYM_TIME_FORMAT__', $region['time_format']); | ||
| 172 |         define_safe('__SYM_DATETIME_FORMAT__', __SYM_DATE_FORMAT__ . $region['datetime_separator'] . __SYM_TIME_FORMAT__); | ||
| 173 | DateTimeObj::setSettings($region); | ||
| 174 | } | ||
| 175 | |||
| 176 | /** | ||
| 177 | * Accessor for the current `Configuration` instance. This contains | ||
| 178 | * representation of the the Symphony config file. | ||
| 179 | * | ||
| 180 | * @return Configuration | ||
| 181 | */ | ||
| 182 | public static function Configuration() | ||
| 183 |     { | ||
| 184 | return self::$Configuration; | ||
| 185 | } | ||
| 186 | |||
| 187 | /** | ||
| 188 | * Is XSRF enabled for this Symphony install? | ||
| 189 | * | ||
| 190 | * @since Symphony 2.4 | ||
| 191 | * @return boolean | ||
| 192 | */ | ||
| 193 | public static function isXSRFEnabled() | ||
| 194 |     { | ||
| 195 |         return self::Configuration()->get('enable_xsrf', 'symphony') === 'yes'; | ||
| 196 | } | ||
| 197 | |||
| 198 | /** | ||
| 199 | * Accessor for the current `Profiler` instance. | ||
| 200 | * | ||
| 201 | * @since Symphony 2.3 | ||
| 202 | * @return Profiler | ||
| 203 | */ | ||
| 204 | public static function Profiler() | ||
| 205 |     { | ||
| 206 | return self::$Profiler; | ||
| 207 | } | ||
| 208 | |||
| 209 | /** | ||
| 210 | * Setter for `$Log`. This function uses the configuration | ||
| 211 | * settings in the 'log' group in the Configuration to create an instance. Date | ||
| 212 | * formatting options are also retrieved from the configuration. | ||
| 213 | * | ||
| 214 | * @param string $filename (optional) | ||
| 215 | * The file to write the log to, if omitted this will default to `ACTIVITY_LOG` | ||
| 216 | * @throws Exception | ||
| 217 | * @return bool|void | ||
| 218 | */ | ||
| 219 | public static function initialiseLog($filename = null) | ||
| 220 |     { | ||
| 221 |         if (self::$Log instanceof Log && self::$Log->getLogPath() == $filename) { | ||
| 222 | return true; | ||
| 223 | } | ||
| 224 | |||
| 225 |         if (is_null($filename)) { | ||
| 226 | $filename = ACTIVITY_LOG; | ||
| 227 | } | ||
| 228 | |||
| 229 | self::$Log = new Log($filename); | ||
| 230 |         self::$Log->setArchive((self::Configuration()->get('archive', 'log') == '1' ? true : false)); | ||
| 231 |         self::$Log->setMaxSize(self::Configuration()->get('maxsize', 'log')); | ||
| 232 |         self::$Log->setFilter(self::Configuration()->get('filter', 'log')); | ||
| 233 | self::$Log->setDateTimeFormat(__SYM_DATETIME_FORMAT__); | ||
| 234 | |||
| 235 |         if (self::$Log->open(Log::APPEND, self::Configuration()->get('write_mode', 'file')) == '1') { | ||
| 236 |             self::$Log->initialise('Symphony Log'); | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | /** | ||
| 241 | * Accessor for the current `Log` instance | ||
| 242 | * | ||
| 243 | * @since Symphony 2.3 | ||
| 244 | * @return Log | ||
| 245 | */ | ||
| 246 | public static function Log() | ||
| 247 |     { | ||
| 248 | return self::$Log; | ||
| 249 | } | ||
| 250 | |||
| 251 | /** | ||
| 252 | * Setter for `$Cookie`. This will use PHP's parse_url | ||
| 253 | * function on the current URL to set a cookie using the cookie_prefix | ||
| 254 | * defined in the Symphony configuration. The cookie will last two | ||
| 255 | * weeks. | ||
| 256 | * | ||
| 257 | * This function also defines two constants, `__SYM_COOKIE_PATH__` | ||
| 258 | * and `__SYM_COOKIE_PREFIX__`. | ||
| 259 | * | ||
| 260 | * @deprecated Prior to Symphony 2.3.2, the constant `__SYM_COOKIE_PREFIX_` | ||
| 261 | * had a typo where it was missing the second underscore. Symphony will | ||
| 262 | * support both constants, `__SYM_COOKIE_PREFIX_` and `__SYM_COOKIE_PREFIX__` | ||
| 263 | * until Symphony 3.0 | ||
| 264 | */ | ||
| 265 | public static function initialiseCookie() | ||
| 266 |     { | ||
| 267 |         define_safe('__SYM_COOKIE_PATH__', DIRROOT === '' ? '/' : DIRROOT); | ||
| 268 |         define_safe('__SYM_COOKIE_PREFIX_', self::Configuration()->get('cookie_prefix', 'symphony')); | ||
| 269 |         define_safe('__SYM_COOKIE_PREFIX__', self::Configuration()->get('cookie_prefix', 'symphony')); | ||
| 270 | |||
| 271 | self::$Cookie = new Cookie(__SYM_COOKIE_PREFIX__, TWO_WEEKS, __SYM_COOKIE_PATH__); | ||
| 272 | } | ||
| 273 | |||
| 274 | /** | ||
| 275 | * Accessor for the current `$Cookie` instance. | ||
| 276 | * | ||
| 277 | * @since Symphony 2.5.0 | ||
| 278 | * @return Cookie | ||
| 279 | */ | ||
| 280 | public static function Cookie() | ||
| 281 |     { | ||
| 282 | return self::$Cookie; | ||
| 283 | } | ||
| 284 | |||
| 285 | /** | ||
| 286 | * Setter for `$ExtensionManager` using the current | ||
| 287 | * Symphony instance as the parent. If for some reason this fails, | ||
| 288 | * a Symphony Error page will be thrown | ||
| 289 | * @param Boolean $force (optional) | ||
| 290 | * When set to true, this function will always create a new | ||
| 291 | * instance of ExtensionManager, replacing self::$ExtensionManager. | ||
| 292 | */ | ||
| 293 | public static function initialiseExtensionManager($force=false) | ||
| 294 |     { | ||
| 295 |         if (!$force && self::$ExtensionManager instanceof ExtensionManager) { | ||
| 296 | return true; | ||
| 297 | } | ||
| 298 | |||
| 299 | self::$ExtensionManager = new ExtensionManager; | ||
| 300 | |||
| 301 |         if (!(self::$ExtensionManager instanceof ExtensionManager)) { | ||
| 302 |             self::throwCustomError(__('Error creating Symphony extension manager.')); | ||
| 303 | } | ||
| 304 | } | ||
| 305 | |||
| 306 | /** | ||
| 307 | * Accessor for the current `$ExtensionManager` instance. | ||
| 308 | * | ||
| 309 | * @since Symphony 2.2 | ||
| 310 | * @return ExtensionManager | ||
| 311 | */ | ||
| 312 | public static function ExtensionManager() | ||
| 313 |     { | ||
| 314 | return self::$ExtensionManager; | ||
| 315 | } | ||
| 316 | |||
| 317 | /** | ||
| 318 | * Setter for `$Database`, accepts a Database object. If `$database` | ||
| 319 | * is omitted, this function will set `$Database` to be of the `MySQL` | ||
| 320 | * class. | ||
| 321 | * | ||
| 322 | * @since Symphony 2.3 | ||
| 323 | * @param StdClass $database (optional) | ||
| 324 | * The class to handle all Database operations, if omitted this function | ||
| 325 | * will set `self::$Database` to be an instance of the `MySQL` class. | ||
| 326 | * @return boolean | ||
| 327 | * This function will always return true | ||
| 328 | */ | ||
| 329 | public static function setDatabase(StdClass $database = null) | ||
| 330 |     { | ||
| 331 |         if (self::Database()) { | ||
| 332 | return true; | ||
| 333 | } | ||
| 334 | |||
| 335 | self::$Database = !is_null($database) ? $database : new MySQL; | ||
| 336 | |||
| 337 | return true; | ||
| 338 | } | ||
| 339 | |||
| 340 | /** | ||
| 341 | * Accessor for the current `$Database` instance. | ||
| 342 | * | ||
| 343 | * @return MySQL | ||
| 344 | */ | ||
| 345 | public static function Database() | ||
| 346 |     { | ||
| 347 | return self::$Database; | ||
| 348 | } | ||
| 349 | |||
| 350 | /** | ||
| 351 | * This will initialise the Database class and attempt to create a connection | ||
| 352 | * using the connection details provided in the Symphony configuration. If any | ||
| 353 | * errors occur whilst doing so, a Symphony Error Page is displayed. | ||
| 354 | * | ||
| 355 | * @throws SymphonyErrorPage | ||
| 356 | * @return boolean | ||
| 357 | * This function will return true if the `$Database` was | ||
| 358 | * initialised successfully. | ||
| 359 | */ | ||
| 360 | public static function initialiseDatabase() | ||
| 361 |     { | ||
| 362 | self::setDatabase(); | ||
| 363 |         $details = self::Configuration()->get('database'); | ||
| 364 | |||
| 365 |         try { | ||
| 366 |             if (!self::Database()->connect($details['host'], $details['user'], $details['password'], $details['port'], $details['db'])) { | ||
| 367 | return false; | ||
| 368 | } | ||
| 369 | |||
| 370 |             if (!self::Database()->isConnected()) { | ||
| 371 | return false; | ||
| 372 | } | ||
| 373 | |||
| 374 | self::Database()->setPrefix($details['tbl_prefix']); | ||
| 375 | self::Database()->setCharacterEncoding(); | ||
| 376 | self::Database()->setCharacterSet(); | ||
| 377 |             self::Database()->setTimeZone(self::Configuration()->get('timezone', 'region')); | ||
| 378 | |||
| 379 |             if (isset($details['query_caching'])) { | ||
| 380 |                 if ($details['query_caching'] == 'off') { | ||
| 381 | self::Database()->disableCaching(); | ||
| 382 |                 } elseif ($details['query_caching'] == 'on') { | ||
| 383 | self::Database()->enableCaching(); | ||
| 384 | } | ||
| 385 | } | ||
| 386 | |||
| 387 |             if (isset($details['query_logging'])) { | ||
| 388 |                 if ($details['query_logging'] == 'off') { | ||
| 389 | self::Database()->disableLogging(); | ||
| 390 |                 } elseif ($details['query_logging'] == 'on') { | ||
| 391 | self::Database()->enableLogging(); | ||
| 392 | } | ||
| 393 | } | ||
| 394 |         } catch (DatabaseException $e) { | ||
| 395 | self::throwCustomError( | ||
| 396 | $e->getDatabaseErrorCode() . ': ' . $e->getDatabaseErrorMessage(), | ||
| 397 |                 __('Symphony Database Error'), | ||
| 398 | Page::HTTP_STATUS_ERROR, | ||
| 399 | 'database', | ||
| 400 | array( | ||
| 401 | 'error' => $e, | ||
| 402 |                     'message' => __('There was a problem whilst attempting to establish a database connection. Please check all connection information is correct.') . ' ' . __('The following error was returned:') | ||
| 403 | ) | ||
| 404 | ); | ||
| 405 | } | ||
| 406 | |||
| 407 | return true; | ||
| 408 | } | ||
| 409 | |||
| 410 | /** | ||
| 411 | * Accessor for the current `$Author` instance. | ||
| 412 | * | ||
| 413 | * @since Symphony 2.5.0 | ||
| 414 | * @return Author | ||
| 415 | */ | ||
| 416 | public static function Author() | ||
| 417 |     { | ||
| 418 | return self::$Author; | ||
| 419 | } | ||
| 420 | |||
| 421 | /** | ||
| 422 | * Attempts to log an Author in given a username and password. | ||
| 423 | * If the password is not hashed, it will be hashed using the sha1 | ||
| 424 | * algorithm. The username and password will be sanitized before | ||
| 425 | * being used to query the Database. If an Author is found, they | ||
| 426 | * will be logged in and the sanitized username and password (also hashed) | ||
| 427 | * will be saved as values in the `$Cookie`. | ||
| 428 | * | ||
| 429 | * @see toolkit.Cryptography#hash() | ||
| 430 | * @throws DatabaseException | ||
| 431 | * @param string $username | ||
| 432 | * The Author's username. This will be sanitized before use. | ||
| 433 | * @param string $password | ||
| 434 | * The Author's password. This will be sanitized and then hashed before use | ||
| 435 | * @param boolean $isHash | ||
| 436 | * If the password provided is already hashed, setting this parameter to | ||
| 437 | * true will stop it becoming rehashed. By default it is false. | ||
| 438 | * @return boolean | ||
| 439 | * true if the Author was logged in, false otherwise | ||
| 440 | */ | ||
| 441 | public static function login($username, $password, $isHash = false) | ||
| 442 |     { | ||
| 443 | $username = trim(self::Database()->cleanValue($username)); | ||
| 444 | $password = trim(self::Database()->cleanValue($password)); | ||
| 445 | |||
| 446 |         if (strlen($username) > 0 && strlen($password) > 0) { | ||
| 447 |             $author = AuthorManager::fetch('id', 'ASC', 1, null, sprintf( | ||
| 448 | "`username` = '%s'", | ||
| 449 | $username | ||
| 450 | )); | ||
| 451 | |||
| 452 |             if (!empty($author) && Cryptography::compare($password, current($author)->get('password'), $isHash)) { | ||
| 453 | self::$Author = current($author); | ||
| 454 | |||
| 455 | // Only migrate hashes if there is no update available as the update might change the tbl_authors table. | ||
| 456 |                 if (self::isUpgradeAvailable() === false && Cryptography::requiresMigration(self::$Author->get('password'))) { | ||
| 457 |                     self::$Author->set('password', Cryptography::hash($password)); | ||
| 458 | |||
| 459 |                     self::Database()->update(array('password' => self::$Author->get('password')), 'tbl_authors', sprintf( | ||
| 460 |                         " `id` = %d", self::$Author->get('id') | ||
| 461 | )); | ||
| 462 | } | ||
| 463 | |||
| 464 |                 self::$Cookie->set('username', $username); | ||
| 465 |                 self::$Cookie->set('pass', self::$Author->get('password')); | ||
| 466 | |||
| 467 | self::Database()->update(array( | ||
| 468 |                     'last_seen' => DateTimeObj::get('Y-m-d H:i:s')), | ||
| 469 | 'tbl_authors', | ||
| 470 |                     sprintf(" `id` = %d", self::$Author->get('id')) | ||
| 471 | ); | ||
| 472 | |||
| 473 | // Only set custom author language in the backend | ||
| 474 |                 if (class_exists('Administration', false)) { | ||
| 475 |                     Lang::set(self::$Author->get('language')); | ||
| 476 | } | ||
| 477 | |||
| 478 | return true; | ||
| 479 | } | ||
| 480 | } | ||
| 481 | |||
| 482 | return false; | ||
| 483 | } | ||
| 484 | |||
| 485 | /** | ||
| 486 | * Symphony allows Authors to login via the use of tokens instead of | ||
| 487 | * a username and password. A token is derived from concatenating the | ||
| 488 | * Author's username and password and applying the sha1 hash to | ||
| 489 | * it, from this, a portion of the hash is used as the token. This is a useful | ||
| 490 | * feature often used when setting up other Authors accounts or if an | ||
| 491 | * Author forgets their password. | ||
| 492 | * | ||
| 493 | * @param string $token | ||
| 494 | * The Author token, which is a portion of the hashed string concatenation | ||
| 495 | * of the Author's username and password | ||
| 496 | * @throws DatabaseException | ||
| 497 | * @return boolean | ||
| 498 | * true if the Author is logged in, false otherwise | ||
| 499 | */ | ||
| 500 | public static function loginFromToken($token) | ||
| 501 |     { | ||
| 502 | $token = self::Database()->cleanValue($token); | ||
| 503 | |||
| 504 |         if (strlen(trim($token)) == 0) { | ||
| 505 | return false; | ||
| 506 | } | ||
| 507 | |||
| 508 |         if (strlen($token) == 6 || strlen($token) == 16) { | ||
| 509 | $row = self::Database()->fetchRow(0, sprintf( | ||
| 510 | "SELECT `a`.`id`, `a`.`username`, `a`.`password` | ||
| 511 | FROM `tbl_authors` AS `a`, `tbl_forgotpass` AS `f` | ||
| 512 | WHERE `a`.`id` = `f`.`author_id` | ||
| 513 | AND `f`.`expiry` > '%s' | ||
| 514 | AND `f`.`token` = '%s' | ||
| 515 | LIMIT 1", | ||
| 516 |                 DateTimeObj::getGMT('c'), | ||
| 517 | $token | ||
| 518 | )); | ||
| 519 | |||
| 520 |             self::Database()->delete('tbl_forgotpass', sprintf(" `token` = '%s' ", $token)); | ||
| 521 |         } else { | ||
| 522 | $row = self::Database()->fetchRow(0, sprintf( | ||
| 523 | "SELECT `id`, `username`, `password` | ||
| 524 | FROM `tbl_authors` | ||
| 525 | WHERE SUBSTR(%s(CONCAT(`username`, `password`)), 1, 8) = '%s' | ||
| 526 | AND `auth_token_active` = 'yes' | ||
| 527 | LIMIT 1", | ||
| 528 | 'SHA1', | ||
| 529 | $token | ||
| 530 | )); | ||
| 531 | } | ||
| 532 | |||
| 533 |         if ($row) { | ||
| 534 | self::$Author = AuthorManager::fetchByID($row['id']); | ||
| 535 |             self::$Cookie->set('username', $row['username']); | ||
| 536 |             self::$Cookie->set('pass', $row['password']); | ||
| 537 |             self::Database()->update(array('last_seen' => DateTimeObj::getGMT('Y-m-d H:i:s')), 'tbl_authors', sprintf(" | ||
| 538 | `id` = %d", $row['id'] | ||
| 539 | )); | ||
| 540 | |||
| 541 | return true; | ||
| 542 | } | ||
| 543 | |||
| 544 | return false; | ||
| 545 | } | ||
| 546 | |||
| 547 | /** | ||
| 548 | * This function will destroy the currently logged in `$Author` | ||
| 549 | * session, essentially logging them out. | ||
| 550 | * | ||
| 551 | * @see core.Cookie#expire() | ||
| 552 | */ | ||
| 553 | public static function logout() | ||
| 554 |     { | ||
| 555 | self::$Cookie->expire(); | ||
| 556 | } | ||
| 557 | |||
| 558 | /** | ||
| 559 | * This function determines whether an there is a currently logged in | ||
| 560 | * Author for Symphony by using the `$Cookie`'s username | ||
| 561 | * and password. If the instance is not found, they will be logged | ||
| 562 | * in using the cookied credentials. | ||
| 563 | * | ||
| 564 | * @see login() | ||
| 565 | * @return boolean | ||
| 566 | */ | ||
| 567 | public static function isLoggedIn() | ||
| 568 |     { | ||
| 569 | // Check to see if Symphony exists, or if we already have an Author instance. | ||
| 570 |         if (is_null(self::$_instance) || self::$Author) { | ||
| 571 | return true; | ||
| 572 | } | ||
| 573 | |||
| 574 | // No author instance found, attempt to log in with the cookied credentials | ||
| 575 |         return self::login(self::$Cookie->get('username'), self::$Cookie->get('pass'), true); | ||
| 576 | } | ||
| 577 | |||
| 578 | /** | ||
| 579 | * Returns the most recent version found in the `/install/migrations` folder. | ||
| 580 | * Returns a version string to be used in `version_compare()` if an updater | ||
| 581 | * has been found. Returns `FALSE` otherwise. | ||
| 582 | * | ||
| 583 | * @since Symphony 2.3.1 | ||
| 584 | * @return string|boolean | ||
| 585 | */ | ||
| 586 | public static function getMigrationVersion() | ||
| 587 |     { | ||
| 588 |         if (self::isInstallerAvailable()) { | ||
| 589 | $migrations = scandir(DOCROOT . '/install/migrations'); | ||
| 590 | $migration_file = end($migrations); | ||
| 591 |             $migration_class = 'migration_' . str_replace('.', '', substr($migration_file, 0, -4)); | ||
| 592 | return call_user_func(array($migration_class, 'getVersion')); | ||
| 593 | } | ||
| 594 | |||
| 595 | return false; | ||
| 596 | } | ||
| 597 | |||
| 598 | /** | ||
| 599 | * Checks if an update is available and applicable for the current installation. | ||
| 600 | * | ||
| 601 | * @since Symphony 2.3.1 | ||
| 602 | * @return boolean | ||
| 603 | */ | ||
| 604 | public static function isUpgradeAvailable() | ||
| 605 |     { | ||
| 606 |         if (self::isInstallerAvailable()) { | ||
| 607 | $migration_version = self::getMigrationVersion(); | ||
| 608 |             $current_version = Symphony::Configuration()->get('version', 'symphony'); | ||
| 609 | |||
| 610 | return version_compare($current_version, $migration_version, '<'); | ||
| 611 | } | ||
| 612 | |||
| 613 | return false; | ||
| 614 | } | ||
| 615 | |||
| 616 | /** | ||
| 617 | * Checks if the installer/upgrader is available. | ||
| 618 | * | ||
| 619 | * @since Symphony 2.3.1 | ||
| 620 | * @return boolean | ||
| 621 | */ | ||
| 622 | public static function isInstallerAvailable() | ||
| 625 | } | ||
| 626 | |||
| 627 | /** | ||
| 628 | * A wrapper for throwing a new Symphony Error page. | ||
| 629 | * | ||
| 630 | * This methods sets the `GenericExceptionHandler::$enabled` value to `true`. | ||
| 631 | * | ||
| 632 | * @see core.SymphonyErrorPage | ||
| 633 | * @param string|XMLElement $message | ||
| 634 | * A description for this error, which can be provided as a string | ||
| 635 | * or as an XMLElement. | ||
| 636 | * @param string $heading | ||
| 637 | * A heading for the error page | ||
| 638 | * @param integer $status | ||
| 639 | * Properly sets the HTTP status code for the response. Defaults to | ||
| 640 | * `Page::HTTP_STATUS_ERROR`. Use `Page::HTTP_STATUS_XXX` to set this value. | ||
| 641 | * @param string $template | ||
| 642 | * A string for the error page template to use, defaults to 'generic'. This | ||
| 643 | * can be the name of any template file in the `TEMPLATES` directory. | ||
| 644 | * A template using the naming convention of `tpl.*.php`. | ||
| 645 | * @param array $additional | ||
| 646 | * Allows custom information to be passed to the Symphony Error Page | ||
| 647 | * that the template may want to expose, such as custom Headers etc. | ||
| 648 | * @throws SymphonyErrorPage | ||
| 649 | */ | ||
| 650 | public static function throwCustomError($message, $heading = 'Symphony Fatal Error', $status = Page::HTTP_STATUS_ERROR, $template = 'generic', array $additional = array()) | ||
| 651 |     { | ||
| 652 | GenericExceptionHandler::$enabled = true; | ||
| 653 | throw new SymphonyErrorPage($message, $heading, $template, $additional, $status); | ||
| 654 | } | ||
| 655 | |||
| 656 | /** | ||
| 657 | * Setter accepts a previous Throwable. Useful for determining the context | ||
| 658 | * of a current Throwable (ie. detecting recursion). | ||
| 659 | * | ||
| 660 | * @since Symphony 2.3.2 | ||
| 661 | * | ||
| 662 | * @since Symphony 2.7.0 | ||
| 663 | * This function works with both Exception and Throwable | ||
| 664 | * Supporting both PHP 5.6 and 7 forces use to not qualify the $e parameter | ||
| 665 | * | ||
| 666 | * @param Throwable $ex | ||
| 667 | */ | ||
| 668 | public static function setException($ex) | ||
| 669 |     { | ||
| 670 | self::$exception = $ex; | ||
| 671 | } | ||
| 672 | |||
| 673 | /** | ||
| 674 | * Accessor for `self::$exception`. | ||
| 675 | * | ||
| 676 | * @since Symphony 2.3.2 | ||
| 677 | * @return Throwable|null | ||
| 678 | */ | ||
| 679 | public static function getException() | ||
| 680 |     { | ||
| 681 | return self::$exception; | ||
| 682 | } | ||
| 683 | |||
| 684 | /** | ||
| 685 | * Returns the page namespace based on the current URL. | ||
| 686 | * A few examples: | ||
| 687 | * | ||
| 688 | * /login | ||
| 689 | * /publish | ||
| 690 | * /blueprints/datasources | ||
| 691 | * [...] | ||
| 692 | * /extension/$extension_name/$page_name | ||
| 693 | * | ||
| 694 | * This method is especially useful in couple with the translation function. | ||
| 695 | * | ||
| 696 | * @see toolkit#__() | ||
| 697 | * @return string | ||
| 698 | * The page namespace, without any action string (e.g. "new", "saved") or | ||
| 699 | * any value that depends upon the single setup (e.g. the section handle in | ||
| 700 | * /publish/$handle) | ||
| 701 | */ | ||
| 702 | public static function getPageNamespace() | ||
| 731 | } | ||
| 732 | } | ||
| 733 | |||
| 734 | /** | ||
| 735 | * The `SymphonyErrorPageHandler` extends the `GenericExceptionHandler` | ||
| 736 | * to allow the template for the exception to be provided from the `TEMPLATES` | ||
| 737 | * directory | ||
| 738 | */ | ||
| 739 | class SymphonyErrorPageHandler extends GenericExceptionHandler | ||
| 740 | { | ||
| 741 | /** | ||
| 742 | * The render function will take a `SymphonyErrorPage` exception and | ||
| 743 | * output a HTML page. This function first checks to see if their is a custom | ||
| 744 | * template for this exception otherwise it reverts to using the default | ||
| 745 | * `usererror.generic.php` | ||
| 746 | * | ||
| 747 | * @param Throwable $e | ||
| 748 | * The Throwable object | ||
| 749 | * @return string | ||
| 750 | * An HTML string | ||
| 751 | */ | ||
| 752 | public static function render($e) | ||
| 753 |     { | ||
| 754 | // Validate the type, resolve to a 404 if not valid | ||
| 755 |         if (!static::isValidThrowable($e)) { | ||
| 756 | $e = new FrontendPageNotFoundException(); | ||
| 996 |