/** * Funkcja wywoływana na zakończenie skryptu PHP * w przypadku gdy skrypt kończy się błędem uruchamia funkcje powiadamiajaca o błędzie */ public static function shutdownFunction() { $error = error_get_last(); if (!empty($error)) { MK_Error::handler(isset($error['type']) ? $error['type'] : null, isset($error['message']) ? $error['message'] : null, isset($error['file']) ? $error['file'] : null, isset($error['line']) ? $error['line'] : null); } }
/** * Rozbudowany raport błędu dla MK_Exception i MK_Db_Exception. * Zapisanie zdarzenia w pliku tekstowym i wysłanie do logs.madkom.pl (dla developer:false) * * try { * // code * } catch (MK_Db_Exception $e) { * //MK_Error::setMoreTraceIgnorePath(array('Spirb->loadModule')); * die($e->getExtendedMessage()); * } catch (MK_Exception $e) { * die($e->getExtendedMessage()); * } * * @param bool $detailedLog * * @return string */ public function getExtendedMessage($detailedLog = true) { $retArray = array('success' => false, 'message' => $this->getMessage()); $_file = $this->getFile(); $_line = strval($this->getLine()); $_trace = MK_Error::getExtendedTrace($this); $debugMsg = $dbError = ''; if (MK_Db_PDO_Singleton::isInstance()) { $mkDb = new MK_Db_PDO(); $mkDb->transFail(); $dbError = $mkDb->getErrorMsg(); } if ($detailedLog == true) { if (empty($dbError)) { $debugMsg = MK_Error::fromException($retArray['message'], $_file, $_line, $_trace); } else { $debugMsg = MK_Error::fromDataBase($dbError, $_file, $_line, $_trace); } } else { $debugMsg = empty($dbError) ? $retArray['message'] : $dbError; } $retArray['debug'] = MK_DEBUG === true ? '<pre>' . $debugMsg . '</pre>' : ''; if (MK::isAjaxExecution(true)) { return json_encode($retArray); } return $retArray[MK_DEBUG === true ? 'debug' : 'message']; }
/** * * Singleton pattern for database connection * * @return PDO * @access public * @static */ public static function getInstance() { if (!self::isInstance()) { $dsn = 'pgsql:host=' . DB_HOST . ';port=' . DB_PORT . ';dbname=' . DB_NAME; try { self::$_singleton = new PDO($dsn, DB_USER, DB_PASS); // PDO::ATTR_DEFAULT_FETCH_MODE: Set default fetch mode. Description of modes is available in PDOStatement::fetch() documentation. // PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set // PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set // PDO::FETCH_BOUND: returns TRUE and assigns the values of the columns in your result set to the PHP variables to which they were bound with the PDOStatement::bindColumn() method // PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column. // PDO::FETCH_INTO: updates an existing instance of the requested class, mapping the columns of the result set to named properties in the class // PDO::FETCH_LAZY: combines PDO::FETCH_BOTH and PDO::FETCH_OBJ, creating the object variable names as they are accessed // PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0 // PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set self::$_singleton->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // PDO::ATTR_CASE: Force column names to a specific case. // PDO::CASE_LOWER: Force column names to lower case. // PDO::CASE_NATURAL: Leave column names as returned by the database driver. // PDO::CASE_UPPER: Force column names to upper case. self::$_singleton->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL); // PDO::ATTR_ERRMODE: Error reporting. // PDO::ERRMODE_SILENT: Just set error codes. // PDO::ERRMODE_WARNING: Raise E_WARNING. // PDO::ERRMODE_EXCEPTION: Throw exceptions. self::$_singleton->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); ///@TODO ogarnąć DEBUG //self::$_singleton->debugDumpParams(DB_DEBUG); } catch (PDOException $e) { $debugMsg = $e->getMessage() . "\n" . str_replace(DB_PASS, '*HIDDEN*', $e->getTraceAsString()); MK_Error::fromDataBase($debugMsg, $e->getFile(), strval($e->getLine())); $retArray = array('success' => false, 'message' => 'Nie udało się połączyć z bazą danych'); if (MK_DEBUG === true) { $retArray['debug'] = $debugMsg; } if (MK::isAjaxExecution(true)) { die(json_encode($retArray)); } exit(MK_DEBUG === true ? $retArray['debug'] : $retArray['message']); } } return self::$_singleton; }
if (MK_DEBUG || MK_IS_CLI) { error_reporting(E_ALL | E_STRICT); ini_set('display_errors', 'on'); set_time_limit(0); umask(0); // Resetowanie maski uprawnien } else { // #ErrorHandling error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE); ini_set('display_errors', 'off'); // Ustawiamy własną funkcję do obsługi błędów, jeżeli nie wywołujemy aplikacji z konsoli set_error_handler('MK_Error::handler', error_reporting()); register_shutdown_function('MK::shutdownFunction'); } if (MK_ERROR_JS_ENABLED) { MK_Error::fromJavaScript(); } // Nadpisanie php.ini ini_set("memory_limit", "512M"); ini_set("max_execution_time", "600"); ini_set("default_socket_timeout", "600"); // #SessionHandling ini_set('session.entropy_length', 16); ini_set('session.entropy_file', '/dev/urandom'); ini_set('session.hash_function', 1); ini_set('session.hash_bits_per_character', 6); ini_set('session.save_handler', SESSION_SAVE_HANDLER); ini_set('session.gc_maxlifetime', 0); ini_set('session.gc_probability', 0); ini_set('session.cookie_lifetime', 0); ini_set('session.cache_expire', 480);
/** * Ustawia dane użytkownika z sesji * * @param array $userData */ public static function setUserData(array $userData) { self::$userData = $userData; }