Esempio n. 1
0
 public function __construct($_clean_url = null)
 {
     self::$repository = repository;
     $path = preg_replace("/^\\.\\//", "/", '/');
     $clean_url = $_clean_url !== null ? $_clean_url : $_SERVER['REQUEST_URI'];
     if (preg_match("#0x#", $clean_url)) {
         // error
     }
     $clean_url = preg_replace("#0x#Ui", "", $clean_url);
     $clean_url = preg_replace("#^https?://{$_SERVER['HTTP_HOST']}\\/#", '', $clean_url);
     $clean_url = preg_replace("#^{$path}#", '', $clean_url);
     $clean_url = urldecode($clean_url);
     preg_match("/^([^?]*)(\\?.*)?\$/", $clean_url, $url);
     self::$real_url_string = self::$url_string = $url[1];
     self::$real_url_array = self::$url_array = preg_split("[\\/]", preg_replace("/^\\/|\\/\$/", '', $url[1]), -1, PREG_SPLIT_NO_EMPTY);
     // HTTP_HOST is not secure and attacker can change it
     $domain = $_SERVER['HTTP_HOST'];
     self::$domain = preg_split("[\\.]", $domain);
     self::$sub_domain = self::$domain;
     array_pop(self::$sub_domain);
     array_pop(self::$sub_domain);
     if (isset(self::$real_url_array[0]) && self::$real_url_array[0] == 'home' || isset(self::$real_url_array[1]) && self::$real_url_array[1] == 'home') {
         \lib\error::page("home");
     }
     /**
      * user want control panel or CMS
      */
     if (defined('CMS') && constant('CMS')) {
         $myCP = constant('CMS') === true ? 'cp' : constant('CMS');
         router::set_storage('CMS', $myCP);
     } else {
         router::set_storage('CMS', 'cp');
     }
     /**
      * before router
      */
     if (self::$auto_repository) {
         // first get subdomain and if not exist get first url part as mysub
         $mysub = router::get_sub_domain();
         if (!$mysub) {
             $mysub = router::get_url(0);
             router::$sub_is_fake = true;
             // router::$sub_is_fake = $mysub? true: false;
             // router::set_storage('language', router::get_storage('defaultLanguage') );
         }
         if ($mysub) {
             // automatically set repository if folder of it exist
             $myaddons = array();
             $mysub_real = $mysub;
             $myloc = null;
             $mysub_valid = null;
             // check for account with specefic name
             if (\lib\router::get_storage('CMS')) {
                 $myaddons[\lib\router::get_storage('CMS')] = 'cp';
                 $myaddons['account'] = 'account';
             }
             // check this sub is exist in our data or not
             if (array_key_exists($mysub, $myaddons)) {
                 $mysub = $myaddons[$mysub];
                 $mysub_valid = true;
             }
             // set repository name
             $myrep = 'content_' . $mysub;
             // check content_aaa folder is exist in project or saloos addons folder
             if (is_dir(root . $myrep)) {
                 $myloc = false;
             } elseif ($mysub_valid && is_dir(addons . $myrep)) {
                 $myloc = addons;
             }
             // if folder exist
             if (!is_null($myloc)) {
                 // if url is fake, show it like subdomain and remove from url
                 if (router::$sub_is_fake) {
                     router::remove_url($mysub_real);
                     router::set_sub_domain($mysub_real);
                 }
                 // set repository to this folder
                 $myparam = array($myrep);
                 if ($myloc) {
                     array_push($myparam, $myloc);
                 }
                 // call function and pass param value to it
                 router::set_repository(...$myparam);
             }
         }
     }
     if (self::$auto_api) {
         // automatically allow api, if you wan't to desable it, only set a value
         $route = new router\route("/^api([^\\/]*)/", function ($reg) {
             router::remove_url($reg->url);
             router::set_storage('api', true);
         });
     }
     if (class_exists('\\cls\\route')) {
         $router = new \cls\route();
         $router->main = $this;
         if (method_exists($router, "_before")) {
             $router->_before();
         }
     }
     // like dev or com or ir or ...
     if (!defined('Tld')) {
         define('Tld', router::get_root_domain('tld'));
     }
     $this->check_router();
     /**
      * after router
      */
     if (class_exists('\\cls\\route')) {
         if (method_exists($router, "_after")) {
             $router->_after();
         }
     }
     // Define Project Constants *******************************************************************
     // declate some constant variable for better use in all part of app
     // like .dev or .com
     if (!defined('MainTld')) {
         // if enabling multi domain and set default tld define main tld
         if (\lib\utility\option::get('config', 'meta', 'multiDomain') && ($defaultTld = \lib\utility\option::get('config', 'meta', 'defaultTld'))) {
             define('MainTld', '.' . $defaultTld);
         } else {
             define('MainTld', Tld === 'dev' ? '.dev' : '.com');
         }
     }
     // like ermile
     if (!defined('Domain')) {
         define('Domain', router::get_root_domain('domain'));
     }
     // like account
     if (!defined('SubDomain')) {
         define('SubDomain', router::get_sub_domain());
     }
     // like  127.0.0.1
     if (!defined('ClientIP')) {
         define('ClientIP', router::get_clientIP());
     }
     // like ermile.com
     if (!defined('Service')) {
         define('Service', Domain . '.' . Tld);
     }
     // like test
     if (!defined('Module')) {
         define('Module', router::get_url(0));
     }
     // like https://ermile.com
     router::set_storage('url_site', Protocol . '://' . Domain . '.' . Tld . '/');
     // set MyAccount for use in all part of services
     if (!defined('AccountService')) {
         // if user want main account and set main account name
         if (\lib\utility\option::get('config', 'meta', 'useMainAccount') && \lib\utility\option::get('config', 'meta', 'mainAccount') === constant('MainService')) {
             define('AccountService', constant('MainService'));
         } else {
             define('AccountService', Domain);
         }
     }
     // check for account with specefic name
     if (!defined('MyAccount') && \lib\router::get_storage('CMS')) {
         // set MyAccount for use in all part of services
         define('MyAccount', 'account');
     }
     router::$base = Protocol . '://';
     if (router::$sub_is_fake) {
         router::$base .= Service . (SubDomain ? '/' . SubDomain : null);
     } else {
         router::$base .= SubDomain . '.' . Service;
     }
     if (count(explode('.', SubDomain)) > 1) {
         die("<p>Saloos only support one subdomain!</p>");
     } elseif (SubDomain === 'www') {
         header('Location: ' . router::get_storage('url_site'), true, 301);
     }
 }
Esempio n. 2
0
 public function __construct()
 {
     // check php version to upper than 5.6
     if (version_compare(phpversion(), '5.6', '<')) {
         die("<p>For using Saloos you must update php version to 5.6 or higher!</p>");
     }
     /**
      * in coming soon period show public_html/pages/coming/ folder
      * developer must set get parameter like site.com/dev=anyvalue
      * for disable this attribute turn off it from config.php in project root
      */
     if (defined('CommingSoon') && CommingSoon && isset($_GET['dev'])) {
         setcookie('preview', 'yes', time() + 30 * 24 * 60 * 60, '/', '.' . Service);
     } elseif (defined("CommingSoon") && CommingSoon && !isset($_COOKIE["preview"])) {
         header('Location: http://' . AccountService . MainTld . '/static/page/coming/', true, 302);
         exit;
     }
     /**
      * Localized Language, defaults to English.
      *
      * Change this to localize Saloos. A corresponding MO file for the chosen
      * language must be installed to content/languages. For example, install
      * fa_IR.mo to content/languages and set LANGUAGE to 'fa_IR' to enable Persian
      * language support.
      */
     if (router::get_storage('language')) {
         switch (Tld) {
             case 'ir':
                 router::set_storage('language', "fa_IR");
                 break;
             default:
                 break;
         }
         // do nothing
     } elseif (isset($_GET["lang"]) && router::get_repository_name() !== 'content') {
         router::set_storage('language', $_GET["lang"]);
     } elseif (isset($_COOKIE["lang"])) {
         router::set_storage('language', $_COOKIE["lang"]);
     } else {
         router::set_storage('language', router::get_storage('defaultLanguage'));
     }
     // save language preference for future page requests
     setcookie('lang', router::get_storage('language'), time() + 30 * 24 * 60 * 60, '/', '.' . Service);
     // use saloos php gettext function
     require_once lib . 'utility/gettext/gettext.inc';
     // gettext setup
     T_setlocale(LC_MESSAGES, router::get_storage('language'));
     // Set the text domain as 'messages'
     T_bindtextdomain('messages', root . 'includes/languages');
     T_bind_textdomain_codeset('messages', 'UTF-8');
     T_textdomain('messages');
     // check direction of language and set for rtl languages
     switch (router::get_storage('language')) {
         case 'fa_IR':
         case 'ar_SU':
             router::set_storage('direction', 'rtl');
             break;
         default:
             router::set_storage('direction', 'ltr');
             break;
     }
     /**
      * If DEBUG is TRUE you can see the full error description, If set to FALSE show userfriendly messages
      * change it from project config.php
      */
     if (!defined('DEBUG')) {
         define('DEBUG', false);
     }
     if (DEBUG) {
         ini_set('display_errors', 'On');
         ini_set('display_startup_errors', 'On');
         ini_set('error_reporting', 'E_ALL | E_STRICT');
         ini_set('track_errors', 'On');
         ini_set('display_errors', 1);
         error_reporting(E_ALL);
         //Setting for the PHP Error Handler
         // set_error_handler('\lib\error::myErrorHandler');
         //Setting for the PHP Exceptions Error Handler
         // set_exception_handler('\lib\error::myErrorHandler');
         //Setting for the PHP Fatal Error
         // register_shutdown_function('\lib\error::myErrorHandler');
     } else {
         error_reporting(0);
         ini_set('display_errors', 0);
     }
     // change header and remove php from it
     header("X-Powered-By: Saloos!");
     /**
      * A session is a way to store information (in variables) to be used across multiple pages.
      * Unlike a cookie, the information is not stored on the users computer.
      * access to session with this code: $_SESSION["test"]
      */
     if (is_string(Domain)) {
         session_name(Domain);
     }
     session_set_cookie_params(0, '/', '.' . Service, false, true);
     session_start();
 }