Beispiel #1
0
 /**
  * Show an error message to the user and log the error if needed.
  * Current 404 errors are not logged. This function ends execution.
  * @param $errNum int the HTTP error code to return, typically 404 for missing page or 500.
  * @param $info string (optional) with the message to log, not displayed in production.
  */
 public static function showError($errNum, $info = null)
 {
     Language::load('Errors');
     ob_get_clean();
     $defError = Language::tr('500_default_error');
     switch ($errNum) {
         case 404:
             Error::error404();
             die;
             break;
         case 500:
         default:
             break;
     }
     if ($info != null) {
         Logger::error('[' . $errNum . '] ' . strip_tags($info));
     }
     $data['title'] = Language::tr('500_title');
     $data['error'] = $info != null ? Config::SITE_ENVIRONMENT() == 'development' ? $defError . '<br/>' . $info : $defError : $defError;
     View::addHeader("HTTP/1.0 500 Internal Server Error");
     View::renderTemplate('header', $data);
     View::render('error/500', $data);
     View::renderTemplate('footer', $data);
     die;
 }
Beispiel #2
0
 public static function init($configFile)
 {
     $opts = null;
     $apcEnabled = (bool) ini_get('apc.enabled');
     if ($apcEnabled && apc_exists('hmcsoftmvc-config')) {
         if (file_exists($configFile)) {
             $lastModTime = filemtime($configFile);
             $lastConfTime = 0;
             if (apc_exists('hmcsoftmvc-config-updated')) {
                 $lastConfTime = apc_fetch('hmcsoftmvc-config-updated');
             }
             if ($lastConfTime < $lastModTime) {
                 $opts = json_decode(file_get_contents($configFile), true);
                 apc_store('hmcsoftmvc-config', $opts);
                 apc_store('hmcsoftmvc-config-updated', $lastModTime);
             } else {
                 $opts = apc_fetch('hmcsoftmvc-config');
             }
         }
     } else {
         if (is_readable($configFile)) {
             $opts = json_decode(file_get_contents($configFile), true);
         }
     }
     $defaults = self::get_defaults();
     if ($opts !== null) {
         self::$options = (array) self::merge_defaults($defaults, (array) $opts);
     }
     self::$options = Hooks::run('config', self::$options);
     if (self::$options['SITE']['LANGUAGE'] == null) {
         //try to figure if we support the user's language.
         $userlangs = explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
         $foundLang = false;
         foreach (self::$options['SITE']['LANGUAGES'] as $ourLang) {
             if (!$foundLang) {
                 foreach ($userlangs as $ulang) {
                     if (!$foundLang && $ulang === $ourLang) {
                         self::$options['SITE']['LANGUAGE'] = $ourLang;
                         $foundLang = true;
                     }
                 }
             }
         }
         if (!$foundLang) {
             self::$options['SITE']['LANGUAGE'] = self::$options['SITE']['DEFAULTLANG'];
         }
     }
     if (isset(self::$options['SITE'])) {
         if (isset(self::$options['ENVIRONMENT'])) {
             if (self::$options['SITE']['ENVIRONMENT'] == 'development') {
                 error_reporting(E_NONE);
             } else {
                 error_reporting(E_NONE);
             }
         }
         //set timezone
         if (isset(self::$options['SITE']['TIMEZONE'])) {
             date_default_timezone_set(self::$options['SITE']['TIMEZONE']);
         }
     }
     //turn on output buffering
     ob_start();
     //turn on custom error handling
     if (isset(self::$options['LOG'])) {
         \HMC\Logger::init(self::$options['LOG']);
         if (isset(self::$options['LOG']['EXCEPTIONS'])) {
             set_exception_handler(self::$options['LOG']['EXCEPTIONS']);
         } else {
             set_exception_handler('HMC\\Logger::exceptionHandler');
         }
         if (isset(self::$options['LOG']['ERRORS'])) {
             set_error_handler(self::$options['LOG']['ERRORS']);
         } else {
             set_error_handler('HMC\\Logger::errorHandler');
         }
     }
     //start sessions
     if (isset(self::$options['SESSION'])) {
         Session::init(self::$options['SESSION']);
     } else {
         Session::init();
     }
     return self::$options;
 }
Beispiel #3
0
 /**
  * Create the table in the database.
  * @param $onlyNew (optional) - only create the table if its not already there.
  * @return true if the table was created or already exists, false otherwise.
  */
 public function create($onlyNew = false)
 {
     $compiled = $this->compile();
     if ($compiled === null) {
         return false;
     }
     $sql = "CREATE TABLE ";
     if ($onlyNew) {
         $sql .= "IF NOT EXISTS ";
     }
     $sql .= $compiled . ';';
     if ($this->db === null) {
         return false;
     }
     try {
         $this->db->exec($sql);
         $ret = $this->db->errorCode();
         if ($ret == '00000') {
             return true;
         } else {
             $info = $this->db->errorInfo();
             \HMC\Logger::error('SQLSTATE[' . $ret . '] ' . $info[0] . $info[1] . ' ' . $info[2]);
             return false;
         }
     } catch (\Exception $e) {
         \HMC\Logger::error($sql);
         \HMC\Logger::error(\HMC\Logger::buildExceptionMessage($e));
         return false;
     }
 }