/** * Ejecuta el Cron mediante el metodo indicado * Este es utilizado por el metodo forward del CronController y de uso interno al modulo * @param string $cron * @param string $method */ public function executeCron($cron, $method = "index") { $dir = PATHAPP . 'source/crons/' . $cron . '.php'; //Analiza si existe el archivo if (file_exists($dir)) { require $dir; $dir = explode("/", $cron); $class = $dir[count($dir) - 1]; $cron = new $class(); //Analiza si existe el metodo indicado if (method_exists($cron, $method)) { $cron->{$method}($this->cronRequest, $this->cronResponse); } else { Error::general_error('Cron Controller Error', 'The Cron Controller ' . $cron . ' dont implement the method ' . $method . '()'); } } else { Error::general_error('Cron Controller Error', 'The Cron Controller ' . $cron . ' dont exist'); } }
/** * Ejecuta el controlador que mapeo anteriormente. Segun su definicion en la configuracion se ejecutara al estilo REST * o mediante nombre de funciones * @param array $controller_esp * @param string $uriapp */ protected function executeController($controller_esp, $uriapp = NULL) { $dir = $this->buildDir($controller_esp); $class = $this->buildClass($controller_esp); if (!class_exists($class)) { //Si la clase no existe intento cargarla if (file_exists($dir)) { require_once $dir; } else { //Avisa que el archivo no existe Error::general_error('Controller Error', 'The controller ' . $controller_esp['class'] . ' dont exists'); } } $controller = new $class(); //Agrego los parametros URI $uri_params = UrlUri::uriParams($controller_esp['url'], $uriapp); $dinamic_method = $uri_params['dinamic']; $method = $uri_params['method']; $controller->setUriParams($uri_params['params']); //Analizo si hay parametros en la configuracion if (isset($controller_esp['properties'])) { $this->app->dependenciesEngine->injectProperties($controller, $controller_esp['properties']); } //Saca el metodo HTPP y en base a eso hace una llamada al metodo correspondiente $methodHttp = $_SERVER['REQUEST_METHOD']; if ($dinamic_method) { if (method_exists($controller, $method)) { $controller->{$method}($this->httpRequest, $this->httpResponse); } else { Error::general_error('HTTP Method Error', "The HTTP method {$method} is not supported"); } } else { switch ($methodHttp) { case 'GET': $controller->doGet($this->httpRequest, $this->httpResponse); break; case 'POST': $controller->doPost($this->httpRequest, $this->httpResponse); break; case 'UPDATE': $controller->doUpdate($this->httpRequest, $this->httpResponse); break; case 'DELETE': $controller->doDelete($this->httpRequest, $this->httpResponse); break; case 'HEAD': $controller->doHead($this->httpRequest, $this->httpResponse); break; case 'TRACE': $controller->doTrace($this->httpRequest, $this->httpResponse); break; case 'URI': $controller->doUri($this->httpRequest, $this->httpResponse); break; case "OPTIONS": $controller->doOptions($this->httpRequest, $this->httpResponse); break; case 'CONNECT': $controller->doConnect($this->httpRequest, $this->httpResponse); break; default: Error::general_error('HTTP Method Error', "The HTTP method {$methodHttp} is not supported"); } } }
/** * Setea el codigo del header HTTP * @param int $code * @param string $text */ public static function setEstadoHeader($code = 200, $text = '') { //Arreglo con todos los codigos y su respectivo texto $states = array(100 => 'Continue', 101 => 'Switching Protocols', 103 => 'Checkpoint', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 306 => 'Switch Proxy', 307 => 'Temporary Redirect', 308 => 'Resume Incomplete', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Long', 415 => 'Unsupported Media Type', 416 => 'Requested Range Not Satisfiable', 417 => 'Expectation Failed', 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 505 => 'HTTP Version Not Supported', 511 => 'Network Authentication Required'); //Me fijo que el codigo no sea un string if ($code == '' or !is_numeric($code)) { Enola\Error::general_error('Error Estado HTTP', 'El codigo de estado debe ser numerico'); } //Veo si se paso o no texto y si no, le asigo el del codigo if (isset($states[$code]) and $text == '') { $text = $states[$code]; } //Me fijo que el texto no este vacio if ($text == '') { Enola\Error::general_error('Error Estado HTTP', 'No status text available. Please check your status code number or supply your own message text.'); } //Cargo el protocolo $server_protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : FALSE; //Segun el protocolo modifico el header HTTP if (substr(php_sapi_name(), 0, 3) == 'cgi') { header("Status: {$code} {$text}", TRUE); } elseif ($server_protocol == 'HTTP/1.1' or $server_protocol == 'HTTP/1.0') { header($server_protocol . " {$code} {$text}", TRUE, $code); } else { header("HTTP/1.1 {$code} {$text}", TRUE, $code); } }
/** * Si corresponde: * Finaliza el calculo del tiempo de respuesta e imprime el resultado */ public function displayPerformance() { if ($this->performance != NULL && $this->showPerformance) { $this->performance->terminate(); $mensaje = 'The execution time of the APP is: ' . $this->performance->elapsed() . ' seconds'; $titulo = 'Performance'; //Muestra la informacion al usuario Error::display_information($titulo, $mensaje); } }
/** * Devuelve un array con los valores del archivo de configuracion * @param string $name * @param boolean $cache * @return array */ public function readConfigurationFile($name, $cache = TRUE) { //Lee archivo de configuracion principal donde se encuentra toda la configuracion de variables, filtros, controladores, etc. $config = NULL; if ($this->cacheConfigFiles && $cache && $this->app->cache != NULL) { //Si esta en produccion y se encuentra en cache lo cargo $config = $this->app->getAttribute('C_' . $name); } if ($config == NULL) { //Cargo la configuracion y guardo en cache si corresponde $config = $this->readFile($name); if ($this->cacheConfigFiles && $cache && $this->app->cache != NULL) { $this->app->setAttribute('C_' . $name, $config); } } if (!is_array($config)) { //Arma una respuesta de error de configuracion. \Enola\Error::general_error('Configuration Error', 'The configuration file ' . $name . ' is not available or is misspelled'); //Cierra la aplicacion exit; } return $config; }
/** * Ejecuta un componente que se indique mediante su nombre. Le pasa los parametros que correspondan. * En base a si se le pasa la accion o no ejecuta la misma y luego el render o solo en render de componente * Este metodo esta abstracto a si el componente se esta ejecutando via URL o no. * @param string $name * @param array $parameters * @param string url */ public function executeComponent($name, $parameters = NULL, $action = NULL) { $components = $this->app->context->getComponentsDefinition(); $component = NULL; if (isset($components[$name])) { $comp = $components[$name]; $dir = $this->buildDir($comp, 'components'); $class = $this->buildClass($comp); if (!class_exists($class)) { //Si la clase no existe intento cargarla if (file_exists($dir)) { require_once $dir; } else { //Avisa que el archivo no existe Error::general_error('Component Error', 'The component ' . $comp['class'] . ' dont exists'); } } $component = new $class(); //Analizo si hay parametros en la configuracion if (isset($comp['properties'])) { $this->app->dependenciesEngine->injectProperties($component, $comp['properties']); } } if ($component != NULL) { //Analiza si existe el metodo render if (method_exists($component, 'rendering')) { if ($action != NULL) { if (method_exists($component, $action)) { $component->{$action}($this->request, $this->response, $parameters); } else { Error::general_error('Component Error', 'The component ' . $name . ' dont implement the action ' . $action . '()'); } } return $component->rendering($this->request, $this->response, $parameters); } else { Error::general_error('Component Error', 'The component ' . $name . ' dont implement the method rendering()'); } } else { Error::general_error('Component Error', "The component {$name} dont exists"); } }
/** * Setea el driver indicado * @param string $store */ public function setCacheStore($store = "Default") { if ($store == "Default") { $store = self::$config['defaultStore']; } $config = self::$config['stores'][$store]; switch ($config['driver']) { case 'file': $this->store = new CacheFileSystem($config['folder']); break; case 'database': $this->store = new CacheDataBase($config['connection'], $config['table']); break; case 'apc': $this->store = new CacheApc(); break; case 'memcached': $this->store = new CacheMemCache($this->prefix, $config["servers"]); break; default: \Enola\Error::general_error("Cache Configuration", "Driver specified unsupported"); break; } }
/** * Realiza una comprobacion de identidad * Analiza que no se este suplantando la identidad del verdadero usuario */ private function checkIdentity() { if (isset($_SESSION['REMOTE_ADDR']) && isset($_SESSION['HTTP_USER_AGENT'])) { if ($_SESSION['REMOTE_ADDR'] != $_SERVER['REMOTE_ADDR'] || $_SESSION['HTTP_USER_AGENT'] != $_SERVER['HTTP_USER_AGENT']) { Error::general_error('Session - Identity', 'There are a proble with the Sesion identity'); } } else { $_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; $_SESSION['HTTP_USER_AGENT'] = $_SERVER['HTTP_USER_AGENT']; } }