Ejemplo n.º 1
0
 /**
  * Send this response object to output.
  */
 public function send()
 {
     $e = $this->getException();
     $tpd = $this->template_data;
     // variable $tpd is accessible in each template file
     if (is_null($e) || $e instanceof NoticeException || $e instanceof WarningException) {
         $templates_path = sprintf("%s/", Config::getAbsoluteFolderPath(Config::KEY_DIR_APP_TEMPLATES));
         // include Master header template
         if (!empty($templates_path) && is_file($templates_path . self::HEADER_TEMPLATE_FILE)) {
             include $templates_path . self::HEADER_TEMPLATE_FILE;
         }
         // make exception box
         if (!is_null($e)) {
             echo $this->getExceptionBox();
         }
         // make content (only for null or Notice exception)
         if ((is_null($e) || $e instanceof NoticeException) && !empty($this->template_file) && is_file($templates_path . $this->template_file)) {
             include $templates_path . $this->template_file;
         }
         // include Master footer template
         if (!empty($templates_path) && is_file($templates_path . self::FOOTER_TEMPLATE_FILE)) {
             include $templates_path . self::FOOTER_TEMPLATE_FILE;
         }
     } else {
         System::redirect(Config::get(Config::KEY_SITE_FQDN) . Config::get(Config::KEY_SHUTDOWN_PAGE));
     }
 }
Ejemplo n.º 2
0
 /**
  * Save exception into databse.
  *
  * @param Phoenix\Core\Database $db
  *            database object
  * @param \Exception $e
  *            exception object
  * @return void
  */
 public static function saveToDatabase(Database $db, Exception $e)
 {
     try {
         $r = InternalLogDao::insertRecord($db, get_class($e), $e->getCode(), $e->getTraceAsString(), $e->getMessage());
         if ($r != 1) {
             throw new WarningException(FrameworkExceptions::W_DB_INVALID_SQL_ACTION);
         }
     } catch (WarningException $ex) {
         self::saveToFile(new FailureException(FrameworkExceptions::F_LOGGER_UNABLE_SAVE_WARNING));
         self::saveToFile($e);
         System::redirect(Config::get(Config::KEY_SITE_FQDN) . Config::get(Config::KEY_SHUTDOWN_PAGE));
     }
 }
Ejemplo n.º 3
0
 /**
  * Creates current Request object.
  *
  * @return Phoenix\Http\Request
  */
 public static function createRequest()
 {
     // prepare Url of the request.
     $url = new Url();
     $url->setScheme(!empty($_SERVER["HTTPS"]) && strcasecmp($_SERVER["HTTPS"], "off") ? "https" : "http");
     $url->setUser(isset($_SERVER["PHP_AUTH_USER"]) ? $_SERVER["PHP_AUTH_USER"] : "");
     $url->setPassword(isset($_SERVER["PHP_AUTH_PW"]) ? $_SERVER["PHP_AUTH_PW"] : "");
     // host & port
     if ((isset($_SERVER[$tmp = "HTTP_HOST"]) || isset($_SERVER[$tmp = "SERVER_NAME"])) && preg_match("/^([a-z0-9_.-]+|\\[[a-f0-9:]+\\])(:\\d+)?\\z/i", $_SERVER[$tmp], $pair)) {
         $url->setHost(strtolower($pair[1]));
         if (isset($pair[2])) {
             $url->setPort(substr($pair[2], 1));
         } elseif (isset($_SERVER["SERVER_PORT"])) {
             $url->setPort($_SERVER["SERVER_PORT"]);
         }
     }
     // path & query
     $requestUrl = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "/";
     $requestUrl = preg_replace(array_keys(self::$urlFilters["url"]), array_values(self::$urlFilters["url"]), $requestUrl);
     $tmp = explode("?", $requestUrl, 2);
     $path = Url::unescape($tmp[0], "%/?#");
     $path = Strings::fixEncoding(preg_replace(array_keys(self::$urlFilters["path"]), array_values(self::$urlFilters["path"]), $path));
     $url->setPath($path);
     // detect script path
     $lpath = strtolower($path);
     $script = isset($_SERVER["SCRIPT_NAME"]) ? strtolower($_SERVER["SCRIPT_NAME"]) : "";
     if ($lpath !== $script) {
         $max = min(strlen($lpath), strlen($script));
         for ($i = 0; $i < $max && $lpath[$i] === $script[$i]; $i++) {
         }
         $path = $i ? substr($path, 0, strrpos($path, "/", $i - strlen($path) - 1) + 1) : "/";
     }
     $url->setPath($path);
     // GET, POST, COOKIE
     $useFilter = !in_array(ini_get("filter.default"), array("", "unsafe_raw")) || ini_get("filter.default_flags");
     $query = $useFilter ? filter_input_array(INPUT_GET, FILTER_UNSAFE_RAW) : (empty($_GET) ? array() : $_GET);
     $post = $useFilter ? filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW) : (empty($_POST) ? array() : $_POST);
     $cookies = $useFilter ? filter_input_array(INPUT_COOKIE, FILTER_UNSAFE_RAW) : (empty($_COOKIE) ? array() : $_COOKIE);
     if (get_magic_quotes_gpc()) {
         $query = Strings::stripslashes($query, $useFilter);
         $post = Strings::stripslashes($post, $useFilter);
         $cookies = Strings::stripslashes($cookies, $useFilter);
     }
     // remove invalid characters
     $reChars = '/^[' . self::CHARS . ']*+\\z/u';
     if (!self::$binary) {
         $list = array(&$query, &$post, &$cookies);
         while (list($key, $val) = each($list)) {
             foreach ($val as $k => $v) {
                 if (is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
                     unset($list[$key][$k]);
                 } elseif (is_array($v)) {
                     $list[$key][$k] = $v;
                     $list[] =& $list[$key][$k];
                 } else {
                     $list[$key][$k] = (string) preg_replace('/[^' . self::CHARS . ']+/u', "", $v);
                 }
             }
         }
         unset($list, $key, $val, $k, $v);
     }
     $url->setQuery($query);
     // FILES
     $files = array();
     if (!empty($_FILES)) {
         foreach ($_FILES as $k => $v) {
             if (!self::$binary && is_string($k) && (!preg_match($reChars, $k) || preg_last_error())) {
                 continue;
             }
             $files[$k] = self::rebuildFiles($_FILES[$k]);
         }
     }
     // HEADERS
     if (function_exists("apache_request_headers")) {
         $headers = apache_request_headers();
     } else {
         $headers = array();
         foreach ($_SERVER as $k => $v) {
             if (strncmp($k, "HTTP_", 5) == 0) {
                 $k = substr($k, 5);
             } elseif (strncmp($k, "CONTENT_", 8)) {
                 continue;
             }
             $headers[strtr($k, "_", "-")] = $v;
         }
     }
     $remoteAddr = isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : NULL;
     $remoteHost = isset($_SERVER["REMOTE_HOST"]) ? $_SERVER["REMOTE_HOST"] : NULL;
     // proxy
     foreach (self::$proxies as $proxy) {
         if (System::ipMatch($remoteAddr, $proxy)) {
             if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
                 $remoteAddr = trim(current(explode(",", $_SERVER["HTTP_X_FORWARDED_FOR"])));
             }
             if (isset($_SERVER["HTTP_X_FORWARDED_HOST"])) {
                 $remoteHost = trim(current(explode(",", $_SERVER["HTTP_X_FORWARDED_HOST"])));
             }
             break;
         }
     }
     $method = isset($_SERVER["REQUEST_METHOD"]) ? $_SERVER["REQUEST_METHOD"] : NULL;
     if ($method === "POST" && isset($_SERVER["HTTP_X_HTTP_METHOD_OVERRIDE"]) && preg_match("/^[A-Z]+\\z/", $_SERVER["HTTP_X_HTTP_METHOD_OVERRIDE"])) {
         $method = $_SERVER["HTTP_X_HTTP_METHOD_OVERRIDE"];
     }
     return new Request($url, $method, $post, $files, $cookies, $headers, $remoteAddr, $remoteHost);
 }
Ejemplo n.º 4
0
 /**
  * Perform action on this view.
  *
  * @throws Phoenix\Exceptions\WarningException
  * @return void
  */
 private function performViewAction()
 {
     /*
      * ($this->response == null) means that cotroller does not throw any exception and everything is ok
      *
      * ($this->response == Response && $this->response->getException() == NoticeException) means that controller
      * throws NoticeException
      *
      * it is possible to create new response with content only in situations mentioned above
      */
     $action_name = $this->request->getUrl()->getQueryParameter(self::URL_GET_ACTION);
     if (is_null($this->response) || $this->response instanceof Response && $this->response->getException() instanceof NoticeException) {
         if (System::isCallable($this->view, $action_name)) {
             $old_exception = $this->response instanceof Response && $this->response->getException() instanceof NoticeException ? $this->response->getException() : null;
             $this->view->{$action_name}();
             $this->response = $this->view->getResponse();
             $this->response->setException($old_exception);
         } else {
             throw new WarningException(FrameworkExceptions::W_ROUTER_INVALID_ACTION, json_encode($this->request));
         }
     }
 }
Ejemplo n.º 5
0
 /**
  * Resolve proxy request.
  *
  * @todo cache
  * @todo file download + condition
  * @throws Phoenix\Exceptions\WarningException
  */
 private function performProxyRequest()
 {
     $token = $this->request->getUrl()->getQueryParameter(FrontController::URL_GET_TOKEN);
     // @todo load from cache
     // load from db
     $proxy_item = ProxyDao::getProxyItemByValidToken($this->db, $token);
     if ($proxy_item == Database::EMPTY_RESULT) {
         throw new WarningException(FrameworkExceptions::W_INVALID_TOKEN);
     }
     $proxy_item = $proxy_item[0];
     // detect type of request
     if (is_null($proxy_item->getRoute()) && is_null($proxy_item->getAction()) && !is_null($proxy_item->getData())) {
         // external link to redirect on (data=url)
         System::redirect($proxy_item->getData());
     } else {
         if (!is_null($proxy_item->getRoute()) && !is_null($proxy_item->getAction())) {
             $config_route = Config::get(Config::KEY_APP_PROXY_FILE_ROUTE);
             $config_action = Config::get(Config::KEY_APP_PROXY_FILE_ACTION);
             if (!empty($config_route) && !empty($config_action) && $proxy_item->getRoute() == $config_route && $proxy_item->getAction() == $config_action && !is_null($proxy_item->getData())) {
                 // @todo file download
             } else {
                 // internal rewrite link to app (data=query string part of url saved as json)
                 $_GET = array();
                 $_GET[FrontController::URL_GET_ROUTE] = $proxy_item->getRoute();
                 $_GET[FrontController::URL_GET_ACTION] = $proxy_item->getAction();
                 $_GET[FrontController::URL_GET_FORMAT] = ${$this}->response_format;
                 if (!is_null($proxy_item->getData())) {
                     // decode json data and put into GET
                     $_GET = array_merge($_GET, json_decode($proxy_item->getData(), true));
                 }
                 $this->request = RequestFactory::createRequest();
                 $this->performFrontControllerRequest();
                 return;
             }
         } else {
             throw new WarningException(FrameworkExceptions::W_INVALID_TOKEN);
         }
     }
 }