/** * EpiApi::getRoute($route); * @name getRoute * @author Jaisen Mathai <*****@*****.**> * @param string $route * @method getRoute * @static method */ public function getRoute($route, $httpMethod) { foreach ($this->regexes as $ind => $regex) { if (preg_match($regex, $route, $arguments)) { array_shift($arguments); $def = $this->routes[$ind]; if ($httpMethod != $def['httpMethod']) { continue; } else { if (is_array($def['callback']) && method_exists($def['callback'][0], $def['callback'][1])) { if (Epi::getSetting('debug')) { getDebug()->addMessage(__CLASS__, sprintf('Matched %s : %s : %s : %s', $httpMethod, $this->route, json_encode($def['callback']), json_encode($arguments))); } return array('callback' => $def['callback'], 'args' => $arguments, 'postprocess' => true); } else { if (function_exists($def['callback'])) { if (Epi::getSetting('debug')) { getDebug()->addMessage(__CLASS__, sprintf('Matched %s : %s : %s : %s', $httpMethod, $this->route, json_encode($def['callback']), json_encode($arguments))); } return array('callback' => $def['callback'], 'args' => $arguments, 'postprocess' => true); } } } EpiException::raise(new EpiException('Could not call ' . json_encode($def) . " for route {$regex}")); } } EpiException::raise(new EpiException("Could not find route {$this->route} from {$_SERVER['REQUEST_URI']}")); }
public static function raise($exception) { $useExceptions = Epi::getSetting('exceptions'); if ($useExceptions) { throw new $exception($exception->getMessage(), $exception->getCode()); } else { echo sprintf("An error occurred and you have <strong>exceptions</strong> disabled so we're displaying the information.\n To turn exceptions on you should call: <em>Epi::setSetting('exceptions', true);</em>.\n <ul><li>File: %s</li><li>Line: %s</li><li>Message: %s</li><li>Stack trace: %s</li></ul>", $exception->getFile(), $exception->getLine(), $exception->getMessage(), nl2br($exception->getTraceAsString())); } }
/** * addRoute('/', 'function', 'GET'); * @name addRoute * @author Jaisen Mathai <*****@*****.**> * @param string $route * @param mixed $callback * @param mixed $method * @param string $callback */ private function addRoute($route, $callback, $method, $postprocess = false) { $this->routes[] = array('httpMethod' => $method, 'path' => $route, 'callback' => $callback, 'postprocess' => $postprocess); $this->regexes[] = "#^{$route}\$#"; if (Epi::getSetting('debug')) { getDebug()->addMessage(__CLASS__, sprintf('Found %s : %s : %s', $method, $route, json_encode($callback))); } }
/** * addRoute('/', 'function', 'GET'); * @name addRoute * @author Jaisen Mathai <*****@*****.**> * @param string $route * @param mixed $callback * @param mixed $method * @param string $callback */ private function addRoute($route, $callback, $method, $access, $postprocess = false) { /* if ( $access == "secure" ) { // user needs to be logged in if ( isset($_SESSION['sessionId']) ) { $this->routes[] = array('httpMethod' => $method, 'path' => $route, 'callback' => $callback, 'postprocess' => $postprocess); $this->regexes[]= "#^{$route}\$#"; if(Epi::getSetting('debug')) getDebug()->addMessage(__CLASS__, sprintf('Found %s : %s : %s', $method, $route, json_encode($callback))); } else { //return 401 http_response_code(401); trigger_error('Access is denied'); } } else if ( $access == "insecure" ) { // user can see this without being logged in $this->routes[] = array('httpMethod' => $method, 'path' => $route, 'callback' => $callback, 'postprocess' => $postprocess); $this->regexes[]= "#^{$route}\$#"; if(Epi::getSetting('debug')) getDebug()->addMessage(__CLASS__, sprintf('Found %s : %s : %s', $method, $route, json_encode($callback))); }*/ $this->routes[] = array('httpMethod' => $method, 'path' => $route, 'callback' => $callback, 'postprocess' => $postprocess, 'access' => $access); $this->regexes[] = "#^{$route}\$#"; if (Epi::getSetting('debug')) { getDebug()->addMessage(__CLASS__, sprintf('Found %s : %s : %s', $method, $route, json_encode($callback))); } }
private function prepare($sql, $params = array()) { try { $sth = $this->dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $sth->execute($params); return $sth; } catch (PDOException $e) { if (Epi::getSetting('showSql')) { EpiException::raise(new EpiDatabaseQueryException("Query error: {$e->getMessage()} - {$sql}")); } else { EpiException::raise(new EpiDatabaseQueryException("Query error: {$e->getMessage()}")); } return false; } }