예제 #1
0
 public static function run()
 {
     $time = microtime(true);
     Engine::instance()->add_data(array("working" => "✓"));
     echo Engine::instance()->process(new Template("layout2.htm"), new Layout("layout.htm"));
     echo "Generated in " . (microtime(true) - $time) . " milliseconds.";
 }
예제 #2
0
 public static function run()
 {
     $time = microtime(true);
     Engine::instance()->add_rule(new Rule('translate', 'translate:(\\w+),(\\w+)', '<?php \\Application::translate(\'$1\', \'$2\'); ?>'));
     Engine::instance()->add_data(array("working" => "&#10003;", "working_array" => array("check" => "&#10003;"), "varif" => true, "varnot" => false, "varcondition" => 4, "varlist" => array(array("item" => 1), array("item" => 2), array("item" => 3), array("item" => 4), array("item" => 5))));
     echo Engine::instance()->process(new Template("test.htm"));
     echo "Generated in " . (microtime(true) - $time) . " milliseconds.";
 }
예제 #3
0
 /**
  * Return the content of the view
  *
  * @return string
  */
 public function content()
 {
     $config = Application\Application::get_instance()->get_config();
     if (!is_null($config)) {
         if (SessionManager::is_logged_in()) {
             $user_array = array();
             $apine_user = SessionManager::get_user();
             $user_array['id'] = $apine_user->get_id();
             $user_array['username'] = $apine_user->get_username();
             $user_array['password'] = $apine_user->get_password();
             $user_array['type'] = $apine_user->get_type();
             $user_array['email'] = $apine_user->get_email_address();
             $user_array['register_date'] = $apine_user->get_register_date();
             $user_array['groups'] = array();
             $properties = $apine_user->get_property_all();
             if (is_array($properties)) {
                 foreach ($properties as $name => $value) {
                     $user_array["property_" . $name] = $value->get_value();
                 }
             }
             foreach ($apine_user->get_group() as $group) {
                 $user_array['group_' . $group->get_id()] = true;
                 $user_array['groups'][$group->get_id()] = $group->get_id();
             }
         } else {
             $user_array = false;
         }
     } else {
         $user_array = false;
     }
     Engine::instance()->add_data(array('apine_user' => $user_array, 'apine_application_https' => Application\Application::get_instance()->get_use_https(), 'apine_application_mode' => Application\Application::get_instance()->get_mode(), 'apine_application_secure' => Application\Application::get_instance()->get_secure_session(), 'apine_view_metatags' => $this->_metatags, 'apine_view_scripts' => $this->_scripts, 'apine_view_stylesheets' => $this->_styles, "apine_view_title" => $this->_title));
     Engine::instance()->add_data($this->_params->get_all());
     $this->content = Engine::instance()->process($this->_view, $this->_layout);
     return $this->content;
 }
예제 #4
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());
         }
     }
 }