示例#1
0
 /**
  * Set Json File
  *
  * @param string|array $a_json
  * @return string
  */
 public function set_json_file($a_json)
 {
     $options = 0;
     $get = Request::get();
     if (isset($get['json_pretty'])) {
         $options |= JSON_PRETTY_PRINT;
     }
     if (is_string($a_json)) {
         // Verify if valid json array
         //$result = json_decode($a_json);
         json_decode($a_json);
         if (json_last_error() === JSON_ERROR_NONE) {
             $this->_json_file = $a_json;
             $return = $a_json;
         } else {
             $return = null;
         }
     } else {
         if (is_object($a_json)) {
             $this->_json_file = json_encode($a_json, $options);
             $return = $this->_json_file;
         } else {
             if (is_array($a_json)) {
                 $this->_json_file = json_encode($a_json, $options);
                 $return = $this->_json_file;
             } else {
                 $return = null;
             }
         }
     }
     return $return;
 }
示例#2
0
 /**
  * Construct the Controller
  */
 public function __construct()
 {
     if (Request::is_api_call() || Request::is_ajax()) {
         $this->_view = new JSONView();
     } else {
         $this->_view = new HTMLView();
     }
 }
 /**
  * Instantiation of the strategy
  */
 private function __construct()
 {
     if (Apine\Core\Request::is_api_call()) {
         $this->strategy = new APISession();
     } else {
         $this->strategy = new WebSession();
     }
 }
示例#4
0
 /**
  * 
  * {@inheritDoc}
  * @see ApineRouterInterface::route()
  */
 public function route($request)
 {
     try {
         $args = explode("/", $request);
         array_shift($args);
         $controller = $args[0];
         array_shift($args);
         // Add post arguments to args array
         $args = array_merge($args, Request::get_request_params());
         if (self::check_route($request)) {
             $route = new Route($controller, strtolower(Request::get_request_type()), $args);
         }
         if (!isset($route)) {
             throw new GenericException("Route \"{$controller}\" not Found", 404);
         }
         return $route;
     } catch (Exception $e) {
         throw new GenericException($e->getMessage(), $e->getCode(), $e);
     }
 }
示例#5
0
 /**
  * Set HTTP Response Code Header 
  * 
  * @param integer $code
  * @return integer
  */
 public final function set_response_code($code)
 {
     if ($code !== NULL) {
         switch ($code) {
             case 100:
                 $text = 'Continue';
                 break;
             case 101:
                 $text = 'Switching Protocols';
                 break;
             case 200:
                 $text = 'OK';
                 break;
             case 201:
                 $text = 'Created';
                 break;
             case 202:
                 $text = 'Accepted';
                 break;
             case 203:
                 $text = 'Non-Authoritative Information';
                 break;
             case 204:
                 $text = 'No Content';
                 break;
             case 205:
                 $text = 'Reset Content';
                 break;
             case 206:
                 $text = 'Partial Content';
                 break;
             case 300:
                 $text = 'Multiple Choices';
                 break;
             case 301:
                 $text = 'Moved Permanently';
                 break;
             case 302:
                 $text = 'Moved Temporarily';
                 break;
             case 303:
                 $text = 'See Other';
                 break;
             case 304:
                 $text = 'Not Modified';
                 break;
             case 305:
                 $text = 'Use Proxy';
                 break;
             case 400:
                 $text = 'Bad Request';
                 break;
             case 401:
                 $text = 'Unauthorized';
                 break;
             case 402:
                 $text = 'Payment Required';
                 break;
             case 403:
                 $text = 'Forbidden';
                 break;
             case 404:
                 $text = 'Not Found';
                 break;
             case 405:
                 $text = 'Method Not Allowed';
                 break;
             case 406:
                 $text = 'Not Acceptable';
                 break;
             case 407:
                 $text = 'Proxy Authentication Required';
                 break;
             case 408:
                 $text = 'Request Time-out';
                 break;
             case 409:
                 $text = 'Conflict';
                 break;
             case 410:
                 $text = 'Gone';
                 break;
             case 411:
                 $text = 'Length Required';
                 break;
             case 412:
                 $text = 'Precondition Failed';
                 break;
             case 413:
                 $text = 'Request Entity Too Large';
                 break;
             case 414:
                 $text = 'Request-URI Too Large';
                 break;
             case 415:
                 $text = 'Unsupported Media Type';
                 break;
             case 418:
                 $text = 'I\'m a teapot';
                 break;
             case 500:
                 $text = 'Internal Server Error';
                 break;
             case 501:
                 $text = 'Not Implemented';
                 break;
             case 502:
                 $text = 'Bad Gateway';
                 break;
             case 503:
                 $text = 'Service Unavailable';
                 break;
             case 504:
                 $text = 'Gateway Time-out';
                 break;
             case 505:
                 $text = 'HTTP Version not supported';
                 break;
             default:
                 exit('Unknown http status code "' . htmlentities($code) . '"');
                 break;
         }
         $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
         $this->set_header_rule($protocol . ' ' . $code . ' ' . $text);
         $GLOBALS['http_response_code'] = $code;
     } else {
         $code = isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200;
     }
     return $code;
 }
示例#6
0
/**
 * Redirect to another end point of the application
 * using a full query string
 * 
 * @param string $a_request
 * @param integer $a_protocol
 * @return Apine\MVC\RedirectionView
 */
function apine_internal_redirect($a_request, $a_protocol = APINE_PROTOCOL_DEFAULT)
{
    $new_view = new RedirectionView();
    $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
    if (!empty(Request::get()['request']) && $a_request == Request::get()['request']) {
        $new_view->set_header_rule($protocol . ' 302 Moved Temporarily');
    }
    // Remove Trailing slash
    $request = trim($a_request, '/');
    $new_view->set_header_rule('Location: ' . URLHelper::path($request, $a_protocol));
    return $new_view;
}
 /**
  * Test Database Connection
  *
  * @param array $params
  * @throws GenericException
  */
 public function test_database($params)
 {
     try {
         if (Request::is_ajax()) {
             $body = json_decode(Request::get_request_body());
             $database = new Database($body->type, $body->host, $body->name, $body->user, $body->pass, $body->char);
         } else {
             throw new GenericException('Invalid Request', 400);
         }
     } catch (DatabaseException $e) {
         $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
         header($protocol . ' 404 Not Found');
     }
 }
示例#8
0
 /**
  * Append a path to the current absolute path
  * 
  * @param string $base
  * 			Base url
  * @param string $path
  *        String to append
  * @param integer $protocol
  *        Protocol to append to the path
  * @return string
  */
 private static function write_url($base, $path, $protocol)
 {
     if (isset(Request::get()['language'])) {
         if (Request::get()['language'] == Translator::language()->code || Request::get()['language'] == Translator::language()->code_short) {
             $language = Request::get()['language'];
         } else {
             $language = Translator::language()->code_short;
         }
         return self::protocol($protocol) . $base . '/' . $language . '/' . $path;
     } else {
         return self::protocol($protocol) . $base . '/' . $path;
     }
 }
示例#9
0
 /**
  * Detect the best language according to language parameter in request
  *
  * @return Translation
  */
 private static function request_best()
 {
     $request_get = Request::get();
     $return = null;
     if (isset($request_get['language'])) {
         $directory = new TranslationDirectory();
         $return = $directory->is_exist_language($request_get['language']);
     }
     return $return;
 }
示例#10
0
 /**
  * Log a user in
  * 
  * Look up in database for a matching row with a username and a
  * password
  *
  * @param string $a_user_name
  *        Username of the user
  * @param string $a_password
  *        Password of the user
  * @return boolean
  */
 public function login($a_user_name, $a_password)
 {
     if (!$this->is_logged_in()) {
         if (Apine\User\Factory\UserFactory::is_name_exist($a_user_name) || Apine\User\Factory\UserFactory::is_email_exist($a_user_name)) {
             $encode_pass = Apine\Core\Encryption::hash_password($a_password);
         } else {
             return false;
         }
         $user_id = Apine\User\Factory\UserFactory::authentication($a_user_name, $encode_pass);
         $request_server = Apine\Core\Request::server();
         if ($user_id) {
             $referer = isset($request_server['REMOTE_ADDR']) ? $request_server['REMOTE_ADDR'] : '';
             $agent = isset($request_server['HTTP_USER_AGENT']) ? $request_server['HTTP_USER_AGENT'] : '';
             $creation_time = time();
             $new_user_token = new Apine\User\UserToken();
             $new_user_token->set_user($user_id);
             $new_user_token->set_token(Apine\Core\Encryption::hash_api_user_token($a_user_name, $a_password, $creation_time));
             $new_user_token->set_origin($referer . $agent);
             $new_user_token->set_creation_date($creation_time);
             $new_user_token->save();
             $this->token = $new_user_token;
             $this->set_session_type($this->token->get_user()->get_type());
             $this->logged_in = true;
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
 /**
  * Error view generation
  *
  * @param string|integer $a_code
  * @param string $a_message
  * @param Exception $a_exception
  * @return MVC\View
  */
 public function custom($a_code, $a_message, Exception $a_exception = null)
 {
     $this->_view->set_param('code', $a_code);
     $this->_view->set_param('message', $a_message);
     if (Core\Request::is_api_call() || Core\Request::is_ajax()) {
         $this->_view->set_param('request', Core\Request::get()['request']);
     } else {
         $this->_view->set_title($a_message);
         $this->_view->set_view('error');
     }
     if ($a_exception !== null && !is_array($a_exception)) {
         $this->_view->set_param('file', $a_exception->getFile());
         $this->_view->set_param('line', $a_exception->getLine());
         if (Application\Application::get_instance()->get_mode() === APINE_MODE_DEVELOPMENT) {
             $this->_view->set_param('trace', $a_exception->getTraceAsString());
         }
     }
     if ($this->is_http_code($a_code)) {
         $this->_view->set_response_code($a_code);
     } else {
         $this->_view->set_response_code(500);
     }
     return $this->_view;
 }
示例#12
0
 /**
  * Run the application
  *
  * @param int $a_runtime Runtime mode
  */
 public function run($a_runtime = APINE_RUNTIME_HYBRID)
 {
     if ($a_runtime !== APINE_RUNTIME_HYBRID && $a_runtime !== APINE_RUNTIME_API && $a_runtime !== APINE_RUNTIME_APP) {
         $a_runtime = APINE_RUNTIME_HYBRID;
     }
     if ($this->use_composer && !strstr($this->apine_folder, 'vendor/youmy001')) {
         require_once 'vendor/autoload.php';
     }
     /**
      * Main Execution
      */
     try {
         // Make sure application runs with a valid execution mode
         if ($this->mode !== APINE_MODE_DEVELOPMENT && $this->mode !== APINE_MODE_PRODUCTION) {
             throw new GenericException('Invalid Execution Mode \\"' . $this->mode . '"', 418);
         }
         if (!file_exists('.htaccess') || !file_exists('config.ini')) {
             $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
             header($protocol . ' 503 Service Unavailable');
             die("Critical Error : Framework Installation Not Completed");
         }
         if (!Request::is_api_call() && (!empty(Request::get()['request']) && Request::get()['request'] != '/')) {
             $request = Request::get()['request'];
         } else {
             if (!Request::is_api_call()) {
                 $request = '/index';
             } else {
                 $request = Request::get()['request'];
             }
         }
         // Verify is the protocol is allowed
         if (Request::is_https() && !$this->use_https) {
             apine_internal_redirect($request, APINE_PROTOCOL_HTTP);
         }
         if (is_null($this->config)) {
             $this->config = new Config('config.ini');
         }
         // Find a timezone for the user
         // using geoip library and its local database
         if (function_exists('geoip_open')) {
             $gi = geoip_open($this->apine_folder . "/GeoLiteCity.dat", GEOIP_STANDARD);
             $record = GeoIP_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
             //$record = geoip_record_by_addr($gi, "24.230.215.89");
             //var_dump($record);
             if (isset($record)) {
                 $timezone = get_time_zone($record->country_code, $record->region != '' ? $record->region : 0);
             } else {
                 if (!is_null($this->config->get('dateformat', 'timezone'))) {
                     $timezone = $this->config->get('dateformat', 'timezone');
                 } else {
                     $timezone = 'America/New_York';
                 }
             }
             date_default_timezone_set($timezone);
         } else {
             if (!is_null($this->config->get('dateformat', 'timezone'))) {
                 date_default_timezone_set($this->config->get('dateformat', 'timezone'));
             }
         }
         // If a user is logged in; redirect to the allowed protocol
         // Secure session only work when Use HTTPS is set to "yes"
         if (SessionManager::is_logged_in()) {
             if ($this->secure_session) {
                 if (!Request::is_https() && $this->use_https) {
                     die(apine_internal_redirect($request, APINE_PROTOCOL_HTTPS)->draw());
                 } else {
                     if (Request::is_https() && !$this->use_https) {
                         die(apine_internal_redirect($request, APINE_PROTOCOL_HTTP)->draw());
                     }
                 }
             } else {
                 if (Request::is_https()) {
                     die(apine_internal_redirect($request, APINE_PROTOCOL_HTTP)->draw());
                 }
             }
         }
         unset($request);
         if (!Request::is_api_call()) {
             if ($a_runtime == APINE_RUNTIME_API) {
                 throw new GenericException('Web Application calls are not implemented', 501);
             }
             Engine::instance()->add_rule(new Rule('apine_data_loop', 'loopdata', '<?php foreach ($this->data as $element): $this->wrap($element); ?>'));
             Engine::instance()->add_rule(new Rule('apine_config', 'apine_config:(\\w+),(\\w+)', '<?php echo \\Apine\\Application\\Application::get_instance()->get_config()->get(\'$1\',\'$2\');?>'));
             Engine::instance()->add_rule(new Rule('apine_translate', 'apine_translate:(\\w+),(\\w+)', '<?php echo \\Apine\\Application\\Translator::get_instance()->translate(\'$1\',\'$2\');?>'));
             Engine::instance()->add_rule(new Rule('apine_format_date', 'apine_format_date:(\\w+),(\\w+)', '<?php echo \\Apine\\Application\\Translator::get_instance()->translation()->get_locale()->format_date("$1", Apine\\Application\\Translator::get_instance()->translation()->get_locale()->$2());?>'));
             Engine::instance()->add_rule(new Rule('apine_format_date_array', 'apine_format_date:(\\w+)\\[(\\w+)\\],(\\w+)', '<?php echo \\Apine\\Application\\Translator::get_instance()->translation()->get_locale()->format_date($this->data[\'$1\'][\'$2\'], Apine\\Application\\Translator::get_instance()->translation()->get_locale()->$3());?>'));
             Engine::instance()->add_rule(new Rule('apine_language', 'apine_language:(code|short|name)', '<?php switch("$1"){case "code": echo Apine\\Application\\Translator::get_instance()->translation()->get("language","code");break;case "short": echo Apine\\Application\\Translator::get_instance()->translation()->get("language","shortcode");break;case "name": echo Apine\\Application\\Translator::get_instance()->translation()->get("language","name");break;}?>'));
             Engine::instance()->add_rule(new Rule('apine_execution', 'apine_execution_time', '<?php echo apine_execution_time();?>'));
             Engine::instance()->add_rule(new Rule('apine_version', 'apine_version:(framework|application)', '<?php echo \\Apine\\Application\\Application::get_instance()->get_version()->$1();?>'));
             Engine::instance()->add_rule(new Rule('apine_url', 'apine_url_(path|resource):(([^\\/\\s]+\\/)?([^\\{\\}]*))', '<?php echo \\Apine\\MVC\\URLHelper::get_instance()->$1("$2");?>'));
             Engine::instance()->add_rule(new Rule('apine_url_secure', 'apine_url_(path|resource)_secure:(([^\\/\\s]+\\/)?([^\\{\\}]*))', '<?php echo Apine\\MVC\\URLHelper::get_instance()->$1("$2", APINE_PROTOCOL_HTTPS);?>'));
             Engine::instance()->add_rule(new Rule('apine_view_apply_meta', 'apine_apply_meta', '<?php echo Apine\\MVC\\HTMLView::apply_meta($data["apine_view_metatags"]);?>'));
             Engine::instance()->add_rule(new Rule('apine_view_apply_scripts', 'apine_apply_scripts', '<?php echo Apine\\MVC\\HTMLView::apply_scripts($data["apine_view_scripts"]);?>'));
             Engine::instance()->add_rule(new Rule('apine_view_apply_stylesheets', 'apine_apply_stylesheets', '<?php echo Apine\\MVC\\HTMLView::apply_stylesheets($data["apine_view_stylesheets"]);?>'));
             Engine::instance()->add_rule(new Rule('apine_user_has_group', 'if:apine_user\\[groups\\]==([0-9]+)', '<?php if (\\Apine\\Session\\SessionManager::get_user()->has_group($1)) : ?>'));
             Engine::instance()->add_rule(new Rule('apine_user_group', 'apine_user\\[groups\\]\\[([0-9]+)\\]', '<?php echo (\\Apine\\Session\\SessionManager::get_user()->has_group($1)) : \\Apine\\Session\\SessionManager::get_user()->get_group()->get_item($1)->get_name() : ""; ?>'));
             if (!empty(Request::get()['request']) && Request::get()['request'] != '/') {
                 $request = Request::get()['request'];
             } else {
                 $request = '/index';
             }
             $router = new WebRouter($this->routes_path, $this->routes_type);
         } else {
             if ($a_runtime == APINE_RUNTIME_APP) {
                 throw new GenericException('RESTful API calls are not implemented', 501);
             }
             $request = Request::get()['request'];
             $router = new APIRouter();
         }
         // Fetch and execute the route
         $route = $router->route($request);
         $view = $router->execute($route->controller, $route->action, $route->args);
         // Draw the output is a view is returned
         if (!is_null($view) && is_a($view, 'Apine\\MVC\\View')) {
             $view->draw();
         } else {
             throw new GenericException('Empty Apine View', 488);
         }
     } catch (GenericException $e) {
         // Handle application errors
         try {
             $error = new Controllers\ErrorController();
             if ($this->mode == APINE_MODE_PRODUCTION) {
                 if ($error_name = $error->method_for_code($e->getCode())) {
                     $view = $error->{$error_name}();
                 } else {
                     $view = $error->server();
                 }
             } else {
                 $view = $error->custom($e->getCode(), $e->getMessage(), $e);
             }
             $view->draw();
         } catch (Exception $e2) {
             var_dump($e2->getTraceAsString());
             $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
             header($protocol . ' 500 Internal Server Error');
             die("Critical Error : " . $e->getMessage());
         }
     } catch (Exception $e) {
         // Handle PHP exceptions
         try {
             $error = new Controllers\ErrorController();
             $view = $error->custom(500, $e->getMessage(), $e);
             $view->draw();
         } catch (Exception $e2) {
             $protocol = isset(Request::server()['SERVER_PROTOCOL']) ? Request::server()['SERVER_PROTOCOL'] : 'HTTP/1.0';
             header($protocol . ' 500 Internal Server Error');
             die("Critical Error : " . $e->getMessage());
         }
     }
 }
示例#13
0
     if (count($args) > 1) {
         $controller = $args[0];
         array_shift($args);
         $action = $args[0];
         array_shift($args);
     } else {
         $controller = $args[0];
         array_shift($args);
         $action = "index";
     }
     // Add post arguments to args array
     if (Request::get_request_type() != "GET") {
         $args = array_merge($args, Request::post());
     }
     if (!empty(Request::files())) {
         $args = array_merge($args, array("uploads" => Request::files()));
     }
     $maj_controller = ucfirst($controller) . 'Controller';
     print $maj_controller;
     if (class_exists('Apine\\Controllers\\System\\' . $maj_controller) && method_exists('Apine\\Controllers\\System\\' . $maj_controller, $action)) {
         $return = 'Apine\\Controllers\\System\\' . $maj_controller;
         $controller = new $return();
         $view = $controller->{$action}($args);
     } else {
         throw new GenericException('Not Found', 404);
     }
 }
 // Draw the output
 if (!is_null($view) && is_a($view, 'Apine\\MVC\\View')) {
     $view->draw();
 }
示例#14
0
 /**
  * 
  * {@inheritDoc}
  * @see ApineRouterInterface::route()
  */
 public final function route($request)
 {
     $route_found = false;
     $vanilla_route_found = self::check_route($request);
     if (!$vanilla_route_found && file_exists($this->routes_file)) {
         switch ($this->routes_type) {
             case APINE_ROUTES_JSON:
                 $file_request = $this->json_route($request);
                 break;
             case APINE_ROUTES_XML:
                 $file_request = $this->xml_route($request);
                 break;
             default:
                 $file_request = null;
         }
         if ($file_request !== $request) {
             $route_found = true;
             $request = $file_request;
         }
     }
     $args = explode("/", $request);
     array_shift($args);
     if (count($args) > 1) {
         $controller = $args[0];
         array_shift($args);
         $action = $args[0];
         array_shift($args);
     } else {
         if (count($args) > 0) {
             $controller = $args[0];
             array_shift($args);
             $action = "index";
         } else {
             $controller = null;
             $action = null;
         }
     }
     // Add post arguments to args array
     $args = array_merge($args, Request::get_request_params());
     try {
         if ($this->check_route($request)) {
             $route = new Route($controller, $action, $args);
         }
         if (!isset($route)) {
             if ($route_found) {
                 throw new GenericException("Reference Found but Action not Accessible for Route \"{$controller}\"", 410);
             } else {
                 throw new GenericException("Route \"{$controller}\" not Found", 404);
             }
         }
         return $route;
     } catch (Exception $e) {
         throw new GenericException($e->getMessage(), $e->getCode(), $e);
     }
 }