Exemplo n.º 1
0
 /**
  * Get an instance of a preexisting Connection or a new instance.
  *
  * @param  array $group
  * @return \HMC\Database\Connection
  */
 public static function get($group = false)
 {
     // Determining if exists or it's not empty, then use default group defined in config
     $group = !$group ? array('type' => \HMC\Config::DATABASE_TYPE(), 'host' => \HMC\Config::DATABASE_HOST(), 'name' => \HMC\Config::DATABASE_NAME(), 'user' => \HMC\Config::DATABASE_USER(), 'pass' => \HMC\Config::DATABASE_PASS()) : $group;
     // Group information
     $type = $group['type'];
     $host = $group['host'];
     $name = $group['name'];
     $user = $group['user'];
     $pass = $group['pass'];
     // ID for database based on the group information
     $id = "{$type}.{$host}.{$name}.{$user}.{$pass}";
     // Checking if the same
     if (isset(self::$instances[$id])) {
         return self::$instances[$id];
     }
     try {
         // I've run into problem where
         // SET NAMES "UTF8" not working on some hostings.
         // Specifiying charset in DSN fixes the charset problem perfectly!
         $instance = new Connection("{$type}:host={$host};dbname={$name};charset=utf8", $user, $pass);
         $instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         // Setting Database into $instances to avoid duplication
         self::$instances[$id] = $instance;
         return $instance;
     } catch (PDOException $e) {
         $msg = Logger::buildExceptionMessage($e);
         \HMC\Error::showError(501, $msg);
     }
 }
Exemplo n.º 2
0
 /**
  * saves error message from exception
  * @param  numeric $number  error number
  * @param  string $message the error
  * @param  string $file    file originated from
  * @param  numeric $line   line number
  */
 public static function errorHandler($number, $message, $file, $line)
 {
     if (Config::SITE_ENVIRONMENT() == 'development') {
         Error::showError(500, '(' . $number . ') ' . $file . ':' . $line . ' => ' . $message);
         die;
     }
     $msg = "{$message} in {$file} on line {$line}";
     if (self::$logger != null) {
         self::error($number . $msg);
     } else {
         if ($number !== E_NOTICE && $number < 2048) {
             self::errorMessage($msg);
             self::customErrorMsg();
         }
     }
     return 0;
 }
Exemplo n.º 3
0
 /**
  * Runs the callback for the given request
  */
 public static function dispatch()
 {
     $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
     $method = $_SERVER['REQUEST_METHOD'];
     $searches = array_keys(static::$patterns);
     $replaces = array_values(static::$patterns);
     self::$routes = str_replace('//', '/', self::$routes);
     $found_route = false;
     // parse query parameters
     $query = '';
     $q_arr = array();
     if (strpos($uri, '&') > 0) {
         $query = substr($uri, strpos($uri, '&') + 1);
         $uri = substr($uri, 0, strpos($uri, '&'));
         $q_arr = explode('&', $query);
         foreach ($q_arr as $q) {
             $qobj = explode('=', $q);
             $q_arr[] = array($qobj[0] => $qobj[1]);
             if (!isset($_GET[$qobj[0]])) {
                 $_GET[$qobj[0]] = $qobj[1];
             }
         }
     }
     // check if route is defined without regex
     if (in_array($uri, self::$routes)) {
         $route_pos = array_keys(self::$routes, $uri);
         // foreach route position
         foreach ($route_pos as $route) {
             if (self::$methods[$route] == $method || self::$methods[$route] == 'ANY') {
                 $found_route = true;
                 //if route is not an object
                 if (!is_object(self::$callbacks[$route])) {
                     //call object controller and method
                     self::invokeObject(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 } else {
                     //call closure
                     call_user_func(self::$callbacks[$route]);
                     if (self::$halts) {
                         return;
                     }
                 }
             }
         }
         // end foreach
     } else {
         // check if defined with regex
         $pos = 0;
         // foreach routes
         foreach (self::$routes as $route) {
             $route = str_replace('//', '/', $route);
             if (strpos($route, ':') !== false) {
                 $route = str_replace($searches, $replaces, $route);
             }
             if (preg_match('#^' . $route . '$#', $uri, $matched)) {
                 if (self::$methods[$pos] == $method || self::$methods[$pos] == 'ANY') {
                     $found_route = true;
                     //remove $matched[0] as [1] is the first parameter.
                     array_shift($matched);
                     if (!is_object(self::$callbacks[$pos])) {
                         //call object controller and method
                         self::invokeObject(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     } else {
                         //call closure
                         call_user_func_array(self::$callbacks[$pos], $matched);
                         if (self::$halts) {
                             return;
                         }
                     }
                 }
             }
             $pos++;
         }
         // end foreach
     }
     if (self::$fallback) {
         //call the auto dispatch method
         $found_route = self::autoDispatch();
     }
     // run the error callback if the route was not found
     if (!$found_route) {
         //first check if we have a satisfiable static route.
         if (count(self::$static_routes) > 0) {
             $test_sr = str_replace(Config::SITE_PATH() . '/', '', $uri);
             foreach (self::$static_routes as $sr) {
                 if (file_exists($sr . $test_sr)) {
                     $ext = \HMC\Document\Document::getExtension($test_sr);
                     switch ($ext) {
                         case 'html':
                         case 'htm':
                             require $sr . $test_sr;
                             break;
                         default:
                             header("Location: {$sr}{$test_sr}");
                     }
                     die;
                 } else {
                     //for security you cannot access php files directly so
                     //if there's no extension we test for a php file with that name.
                     if (file_exists($sr . $test_sr . '.php')) {
                         require $sr . $test_sr . '.php';
                         die;
                     }
                 }
             }
         }
         if (!self::$errorCallback) {
             self::$errorCallback = function () {
                 Error::showError(404);
             };
         }
         if (!is_object(self::$errorCallback)) {
             //call object controller and method
             self::invokeObject(self::$errorCallback, null, 'No routes found.');
             if (self::$halts) {
                 return;
             }
         } else {
             call_user_func(self::$errorCallback);
             if (self::$halts) {
                 return;
             }
         }
     }
 }
Exemplo n.º 4
0
 /**
  * load a 404 page with the error message
  */
 public function index()
 {
     Error::showError(404);
 }