/** * Works exactly like debug() except uses var_dump() in place of print_r(). * * @param mixed $var, $var, $var... * @return array|string */ function dump() { if (\titon\core\Config::get('debug') > 0) { $vars = func_get_args(); $calledFrom = debug_backtrace(); echo '<div class="TitonDebug">'; echo '<b>' . trim(str_replace(ROOT, '', $calledFrom[0]['file'])) . '</b> (' . $calledFrom[0]['line'] . ')'; if (!empty($vars)) { foreach ($vars as $var) { echo '<pre>'; var_dump($var); echo '</pre>'; } } echo '</div>'; } }
/** * Check to see if a locale is supported, if not default to en_US (or custom default). * * @access public * @return string * @static */ public static function getLocale() { $locale = Config::get('Locale.current'); if (in_array($locale, self::$__locales)) { return strtolower($locale); } return strtolower(Config::get('Locale.default')); }
/** * Get the currently used locale for the application. * * @access public * @return string * @static */ public static function locale() { return Config::get('Locale.current') ?: Config::get('Locale.default'); }
/** * Initialize the session by applying all ini settings depending on security level. * * @access public * @return void */ public function initialize() { if ($this->__started == true) { return; } if (!in_array($this->storage, array(self::STORAGE_PHP, self::STORAGE_CACHE, self::STORAGE_DATABASE))) { $this->storage = self::STORAGE_PHP; } if (!in_array($this->security, array(self::SECURITY_LOW, self::SECURITY_MEDIUM, self::SECURITY_HIGH))) { $this->security = self::SECURITY_MEDIUM; } // Ini Settings ini_set('session.name', Config::get('App.name') . '[Session]'); ini_set('session.use_trans_sid', false); ini_set('url_rewriter.tags', ''); ini_set('session.use_cookies', true); ini_set('session.use_only_cookies', true); ini_set('session.auto_start', true); if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') { ini_set('session.cookie_secure', true); } // Security Settings switch ($this->security) { case self::SECURITY_HIGH: case self::SECURITY_MEDIUM: default: $timeout = $this->security == self::SECURITY_HIGH ? 10 : 25; $lifetime = 60 * $timeout; // seconds * length = minutes ini_set('session.referer_check', $this->__host); ini_set('session.cookie_domain', $this->__host); ini_set('session.cookie_lifetime', $lifetime); break; case self::SECURITY_LOW: $timeout = 45; ini_set('session.cookie_domain', $this->__host); ini_set('session.cookie_lifetime', 0); break; } // Storage Settings switch ($this->storage) { case self::STORAGE_CACHE: break; case self::STORAGE_DATABASE: break; case self::STORAGE_PHP: default: break; } // Start Session if (headers_sent()) { $_SESSION = array(); } else { session_start(); } $this->__id = session_id(); $this->__started = true; // Store settings if ($this->check('Security') == false) { $this->set('Security', array('time' => $this->__time, 'host' => $this->__host, 'agent' => $this->__agent, 'storage' => $this->storage, 'inactivity' => $this->inactivity)); } return $this->__started; }
/** * Initialize the environment by applying the configuration. * Load the environment variables into the class; strtolower() all keys first. * * @access public * @return void * @static */ public static function initialize() { $current = static::$__environments[static::detect()]; foreach ($current as $key => $value) { Config::set($key, $value); } }
/** * Encrypt a cookies value to offer more security and protection. * * @access private * @param string $value * @return string */ private function __encrypt($value) { if (empty($value) || $this->encrypt === false) { return $value; } if (is_array($value)) { $value = serialize($value); } $value = base64_encode(Config::get('App.salt') . $value); $length = strlen($value); $encrypted = ''; for ($i = 0; $i < $length; ++$i) { $encrypted .= ';' . ord(substr($value, $i, 1)); } $encrypted = trim($encrypted, ';'); return $encrypted; }
/** * Initialize the error/exception/debug handling depending on environment. * * @access public * @return void * @static */ public static function initialize() { if (!Config::check('debug')) { static::errorReporting(static::ERRORS_ON); } ini_set('log_errors', true); ini_set('report_memleaks', true); ini_set('error_log', TEMP . Logger::ERROR_LOG); set_error_handler('\\titon\\log\\Debugger::error', E_ALL | E_STRICT); set_exception_handler(array(new Exception(), 'log')); }
/** * Stop the benchmarking process by logging the micro seconds and memory usage and then outputting the results. * * @access public * @param string $slug * @param boolean $log * @return string|mixed * @static */ public static function stop($slug = 'benchmark', $log = self::DONT_LOG) { if (Config::get('debug') > 0) { if (empty(static::$__benchmarks[$slug])) { return false; } static::$__benchmarks[$slug] = array('endTime' => microtime(true), 'endMemory' => memory_get_usage()) + static::$__benchmarks[$slug]; if ($log === static::DO_LOG) { Logger::debug(static::display($slug)); } return static::$__benchmarks[$slug]; } }
/** * Writes a message to the error or debug log, depending on the threat level. * Additionally, it will send you an email with the error message if Debug.email is defined. * * @access public * @param string $message * @param int $level * @return void * @static */ public static function write($message, $level = 0) { if (!empty($message)) { switch ($level) { case static::CRITICAL: $type = 'Critical'; break; case static::ALERT: $type = 'Alert'; break; case static::WARNING: $type = 'Warning'; break; case static::NOTICE: $type = 'Notice'; break; case static::INFO: $type = 'Info'; break; case static::DEBUG: $type = 'Debug'; break; default: $type = 'Internal'; break; } if ($level == static::DEBUG) { $file = static::DEBUG_LOG; } else { $file = static::ERROR_LOG; $message = '[' . $type . '] ' . $message; } $log = fopen(TEMP . $file, 'ab'); fwrite($log, $message . "\n"); fclose($log); if ($level >= static::WARNING) { if ($email = Config::get('Debug.email')) { mail($email, '[Titon Error] ' . $type, $message); } } } }