Beispiel #1
0
 /**
  * Setup and initialize the class
  *
  * @param array $setting Global setting for imager
  * @param string $type Specify the image handler
  *
  * @throws Exception\ImageHandlerTypeUnknown
  * @throws Exception\ImageHandlerClassNotFound
  *
  * @return bool Return true when succeed. false when fail
  */
 public static function setup(array $setting, $type = '')
 {
     if (!$type) {
         if (extension_loaded('gd')) {
             $type = 'GD';
         } elseif (extension_loaded('gmagick')) {
             $type = 'Gmagick';
         } elseif (extension_loaded('imagick')) {
             $type = 'Imagick';
         } else {
             throw new Exception\ImageHandlerTypeUnknown($type);
         }
     }
     $className = static::getOperator($type);
     if (!class_exists($className)) {
         throw new Exception\ImageHandlerClassNotFound($className);
     }
     static::$handlerClassName = $className;
     static::$setting = array('MemoryLimit' => (Ini::getBytes('memory_limit') - memory_get_peak_usage()) * 0.9, 'Font' => isset($setting['Font']) && is_readable($setting['Font']) ? $setting['Font'] : static::$setting['Font'], 'FontSize' => isset($setting['FontSize']) && $setting['FontSize'] > 0 ? $setting['FontSize'] : static::$setting['FontSize']);
     return true;
 }
Beispiel #2
0
 /**
  * Constructor
  *
  * @param array $cfg Array of core configuration
  * @param array $common Array of common configuration
  * @param Framework $facula The framework itself
  */
 public function __construct(array &$cfg, array $common, Framework $facula)
 {
     $cp = new ConfigParser($cfg, static::$defaultSetting);
     // General settings
     $this->configs = array('Cache' => $cp->get('CacheTemplate'), 'Compress' => $cp->get('CompressOutput'), 'Renew' => $cp->get('ForceRenew'), 'CacheTTL' => $cp->get('CacheMaxLifeTime'), 'Charset' => strtoupper($cp->get('Charset', isset($common['Charset']) ? $common['Charset'] : null)), 'AspTags' => Ini::has('asp_tags') ? Ini::getBool('asp_tags') : false, 'CacheBegin' => $common['BootTime'], 'BootTime' => $common['BootTime'], 'BootVersion' => $common['BootVersion']);
     // Use custom render
     if (!$cp->isEmpty('Render')) {
         if (!class_exists($cp->get('Render'))) {
             new Error('RENDER_CLASS_NOTFOUND', array($cp->get('Render')), 'ERROR');
             return;
         }
         if (!class_implements($cp->get('Render'), static::$operatorsImpl['Render'])) {
             new Error('RENDER_INTERFACE_INVALID', array($cp->get('Render'), static::$operatorsImpl['Render']), 'ERROR');
             return;
         }
         $this->configs['Render'] = $cp->get('Render');
     }
     // Use custom compiler
     if (!$cp->isEmpty('Compiler')) {
         if (!class_exists($cp->get('Compiler'))) {
             new Error('COMPILER_CLASS_NOTFOUND', array($cp->get('Compiler')), 'ERROR');
             return;
         }
         if (!class_implements($cp->get('Compiler'), static::$operatorsImpl['Compiler'])) {
             new Error('COMPILER_INTERFACE_INVALID', array($cp->get('Compiler'), static::$operatorsImpl['Compiler']), 'ERROR');
             return;
         }
         $this->configs['Compiler'] = $cp->get('Compiler');
     }
     // TemplatePool
     if (!$cp->isEmpty('TemplatePool') && is_dir($cp->get('TemplatePool'))) {
         $this->configs['TplPool'] = PathParser::get($cp->get('TemplatePool'));
     } else {
         new Error('PATH_TEMPLATEPOOL_NOTFOUND', array(), 'ERROR');
         return;
     }
     // CompiledTemplate
     if (!$cp->isEmpty('CompiledTemplate') && is_dir($cp->get('CompiledTemplate'))) {
         $this->configs['Compiled'] = PathParser::get($cp->get('CompiledTemplate'));
     } else {
         new Error('PATH_COMPILEDTEMPLATE_NOTFOUND', array(), 'ERROR');
         return;
     }
     // Check if cache path has set
     if ($this->configs['Cache']) {
         if (!$cp->isEmpty('CachePath') && is_dir($cp->get('CachePath'))) {
             $this->configs['Cached'] = PathParser::get($cp->get('CachePath'));
         } else {
             new Error('PATH_CACHEDTEMPLATE_NOTFOUND', array(), 'ERROR');
             return;
         }
     }
     $this->pool['SupportedLanguages'] = array();
     if ($this->loadFileMap() && isset(static::$fileMap['Lang'])) {
         $this->pool['SupportedLanguages'] = array_keys(static::$fileMap['Lang']);
     }
     $this->assigned['RootURL'] = $facula->request->getClientInfo('rootURL');
     $this->assigned['HostURIFormatted'] = $facula->request->getClientInfo('hostURIFormatted');
     $this->assigned['AbsRootURL'] = $facula->request->getClientInfo('absRootURL');
     $this->assigned['AbsRootFormatted'] = $facula->request->getClientInfo('absRootFormatted');
     $this->assigned['_BOOT_VERSION'] = $this->configs['BootVersion'];
     $this->assigned['_BOOT_TIME'] = $this->configs['BootTime'];
 }
Beispiel #3
0
 /**
  * Constructor
  *
  * @param array $cfg Array of core configuration
  * @param array $common Array of common configuration
  * @param Framework $facula The framework itself
  */
 public function __construct(array &$cfg, array $common, Framework $facula)
 {
     global $_SERVER;
     $cp = new ConfigParser($cfg, static::$defaultSetting);
     if (function_exists('get_magic_quotes_gpc')) {
         $this->configs['AutoMagicQuotes'] = get_magic_quotes_gpc();
     }
     if ($cp->get('DenyExternalSubmit')) {
         $this->configs['NoExtSubmit'] = true;
     } else {
         $this->configs['NoExtSubmit'] = false;
     }
     if (!$cp->isEmpty('MaxDataSize')) {
         // Give memory_limit * 0.6 because we needs memory to run other stuffs, so memory
         // cannot be 100%ly use for handle request data;
         $this->configs['MaxDataSize'] = min($cp->get('MaxDataSize'), Ini::getBytes('post_max_size'), Ini::getBytes('memory_limit') * 0.6);
     } else {
         $this->configs['MaxDataSize'] = min(Ini::getBytes('post_max_size'), Ini::getBytes('memory_limit') * 0.6);
     }
     // CDN or approved proxy servers
     if ($cp->isEmpty('TrustedProxies') || !defined('FILTER_FLAG_IPV4')) {
         $this->configs['TrustedProxies'] = array();
     } else {
         $proxyIPTemp = array();
         if (defined('AF_INET6') && defined('FILTER_FLAG_IPV6')) {
             $this->configs['TPVerifyFlags'] = FILTER_FLAG_IPV4 + FILTER_FLAG_IPV6;
         } else {
             $this->configs['TPVerifyFlags'] = FILTER_FLAG_IPV4;
         }
         foreach ($cp->get('TrustedProxies') as $proxy) {
             $proxyIPRange = explode('-', $proxy, 2);
             foreach ($proxyIPRange as $proxyIP) {
                 if (!filter_var($proxyIP, FILTER_VALIDATE_IP, $this->configs['TPVerifyFlags'])) {
                     new Error('PROXYADDR_INVALID', array($proxyIP), 'ERROR');
                     return;
                     break 2;
                 }
             }
             if (isset($proxyIPRange[1])) {
                 $proxyIPTemp[0] = inet_pton($proxyIPRange[0]);
                 $proxyIPTemp[1] = inet_pton($proxyIPRange[1]);
                 if ($proxyIPTemp[0] < $proxyIPTemp[1]) {
                     $this->configs['TrustedProxies'][$proxyIPTemp[0]] = $proxyIPTemp[1];
                 } elseif ($proxyIPTemp[0] > $proxyIPTemp[1]) {
                     $this->configs['TrustedProxies'][$proxyIPTemp[1]] = $proxyIPTemp[0];
                 } else {
                     $this->configs['TrustedProxies'][$proxyIPTemp[0]] = false;
                 }
                 continue;
             }
             $this->configs['TrustedProxies'][inet_pton($proxyIPRange[0])] = false;
         }
         // Proxy header, like `X-Forward-For` etc
         if ($cp->isEmpty('TrustedProxyIPHeaders')) {
             new Error('PROXYXFF_HEADER_MUST_BE_DEFINED', array(implode(', ', array_keys(static::$allowedXFFHeaders))), 'ERROR');
             return;
         }
         $currentXFFPriories = count($cp->get('TrustedProxyIPHeaders'));
         foreach ($cp->get('TrustedProxyIPHeaders') as $xffHeader) {
             if (!isset(static::$allowedXFFHeaders[$xffHeader])) {
                 new Error('PROXYXFF_HEADER_INVALID', array($xffHeader, implode(', ', array_keys(static::$allowedXFFHeaders))), 'ERROR');
                 return;
             }
             $this->configs['TrustedXFFHeader'][static::$allowedXFFHeaders[$xffHeader]] = $currentXFFPriories--;
         }
     }
     // We can handle up to 512 elements in _GET + _POST + _COOKIE + SERVER array
     $this->configs['MaxRequestBlocks'] = $cp->get('MaxRequestBlocks');
     // How long of the data we can handle.
     $this->configs['MaxHeaderSize'] = $cp->get('MaxHeaderSize');
     $this->configs['CookiePrefix'] = !empty($common['CookiePrefix']) ? $common['CookiePrefix'] : '';
     // Get environment variables
     // Get current absolute root, only work for HTTP request
     if (!empty($_SERVER['REQUEST_METHOD'])) {
         // Get current path
         if (!empty($common['SitePath'])) {
             $this->requestInfo['rootURL'] = $common['SitePath'];
         } elseif (!empty($_SERVER['SCRIPT_NAME'])) {
             $this->requestInfo['rootURL'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
         } else {
             new Error('UNKNOWN_SITE_PATH', array(), 'ERROR');
             return;
         }
         // Get current host & port
         if (!empty($common['SiteHost'])) {
             $httpHostSplited = explode(':', $common['SiteHost'], 2);
             $hostName = !empty($httpHostSplited[0]) ? trim($httpHostSplited[0]) : 'localhost';
             $hostPort = !empty($httpHostSplited[1]) ? trim($httpHostSplited[1]) : '80';
             if (empty($common['SiteScheme'])) {
                 $hostScheme = $this->isHTTPS() ? 'https' : 'http';
             } else {
                 $hostScheme = strtolower(trim($common['SiteScheme']));
             }
         } elseif (!empty($_SERVER['SERVER_NAME']) && !empty($_SERVER['SERVER_PORT'])) {
             $hostName = $_SERVER['SERVER_NAME'];
             $hostPort = $_SERVER['SERVER_PORT'];
             $hostScheme = $this->isHTTPS() ? 'https' : 'http';
         } elseif (!empty($_SERVER['HTTP_HOST'])) {
             $httpHostSplited = explode(':', $_SERVER['HTTP_HOST'], 2);
             $hostName = !empty($httpHostSplited[0]) ? trim($httpHostSplited[0]) : 'localhost';
             $hostPort = !empty($httpHostSplited[1]) ? trim($httpHostSplited[1]) : '80';
             $hostScheme = $this->isHTTPS() ? 'https' : 'http';
         } else {
             new Error('UNKNOWN_SITE_HOST', array(), 'ERROR');
             return;
         }
         $this->requestInfo['expectedHost'] = $hostName;
         $this->requestInfo['expectedPort'] = (int) $hostPort;
         $this->requestInfo['hostURIFormatted'] = '%s//' . $this->requestInfo['expectedHost'] . '%s';
         $this->requestInfo['absRootFormatted'] = $this->requestInfo['hostURIFormatted'] . $this->requestInfo['rootURL'];
         $this->requestInfo['absRootURL'] = sprintf($this->requestInfo['absRootFormatted'], $hostScheme . ':', $hostScheme === 'https' ? $this->requestInfo['expectedPort'] === 443 ? '' : ':' . $this->requestInfo['expectedPort'] : ($this->requestInfo['expectedPort'] === 80 ? '' : ':' . $this->requestInfo['expectedPort']));
     }
 }
 public function testSet()
 {
     Ini::set($this->originalSettingKey, 'The new test value');
     $this->assertSame('The new test value', Ini::getStr($this->originalSettingKey));
 }
 /**
  * Constructor
  *
  * @param array $cfg Array of core configuration
  * @param array $common Array of common configuration
  * @param \Facula\Framework $facula The framework itself
  *
  * @return void
  */
 public function __construct(&$cfg, $common, $facula)
 {
     global $_SERVER;
     if (function_exists('get_magic_quotes_gpc')) {
         $this->configs['AutoMagicQuotes'] = get_magic_quotes_gpc();
     }
     if (isset($cfg['DenyExternalSubmit']) && $cfg['DenyExternalSubmit']) {
         $this->configs['NoExtSubmit'] = true;
     } else {
         $this->configs['NoExtSubmit'] = false;
     }
     if (isset($cfg['MaxDataSize'])) {
         // give memory_limit * 0.6 because we needs memory to run other stuffs, so memory
         // cannot be 100%ly use for handle request data;
         $this->configs['MaxDataSize'] = min((int) $cfg['MaxDataSize'], Ini::getBytes('post_max_size'), Ini::getBytes('memory_limit') * 0.6);
     } else {
         $this->configs['MaxDataSize'] = min(Ini::getBytes('post_max_size'), Ini::getBytes('memory_limit') * 0.6);
     }
     // CDN or approved proxy servers
     if (isset($cfg['TrustedProxies']) && is_array($cfg['TrustedProxies'])) {
         $proxyIPRange = $proxyIPTemp = array();
         if (defined('AF_INET6')) {
             $this->configs['TPVerifyFlags'] = FILTER_FLAG_IPV4 + FILTER_FLAG_IPV6;
         } else {
             $this->configs['TPVerifyFlags'] = FILTER_FLAG_IPV4;
         }
         foreach ($cfg['TrustedProxies'] as $proxy) {
             $proxyIPRange = explode('-', $proxy, 2);
             foreach ($proxyIPRange as $proxyIP) {
                 if (!filter_var($proxyIP, FILTER_VALIDATE_IP, $this->configs['TPVerifyFlags'])) {
                     new Error('PROXYADDR_INVALID', array($proxyIP), 'ERROR');
                     return false;
                     break 2;
                 }
             }
             if (isset($proxyIPRange[1])) {
                 $proxyIPTemp[0] = inet_pton($proxyIPRange[0]);
                 $proxyIPTemp[1] = inet_pton($proxyIPRange[1]);
                 if ($proxyIPTemp[0] < $proxyIPTemp[1]) {
                     $this->configs['TrustedProxies'][$proxyIPTemp[0]] = $proxyIPTemp[1];
                 } elseif ($proxyIPTemp[0] > $proxyIPTemp[1]) {
                     $this->configs['TrustedProxies'][$proxyIPTemp[1]] = $proxyIPTemp[0];
                 } else {
                     $this->configs['TrustedProxies'][$proxyIPTemp[0]] = false;
                 }
             } else {
                 $this->configs['TrustedProxies'][inet_pton($proxyIPRange[0])] = false;
             }
         }
     } else {
         $this->configs['TrustedProxies'] = array();
     }
     // We can handler up to 512 elements in _GET + _POST + _COOKIE + SERVER array
     $this->configs['MaxRequestBlocks'] = isset($cfg['MaxRequestBlocks']) ? (int) $cfg['MaxRequestBlocks'] : 512;
     // How long of the data we can handle.
     $this->configs['MaxHeaderSize'] = isset($cfg['MaxHeaderSize']) ? (int) $cfg['MaxHeaderSize'] : 5120;
     $this->configs['CookiePrefix'] = isset($common['CookiePrefix'][0]) ? $common['CookiePrefix'] : '';
     // Get environment variables
     // Get current root
     if (isset($_SERVER['SCRIPT_NAME'])) {
         if (isset($common['SiteRootURL'][0])) {
             $this->requestInfo['rootURL'] = $common['SiteRootURL'];
         } else {
             $this->requestInfo['rootURL'] = substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/'));
         }
         // Get current absolute root
         if (isset($_SERVER['SERVER_NAME']) && isset($_SERVER['SERVER_PORT'])) {
             $this->requestInfo['hostURIFormated'] = '%s//' . $_SERVER['SERVER_NAME'] . '%s';
             $this->requestInfo['absRootFormated'] = $this->requestInfo['hostURIFormated'] . $this->requestInfo['rootURL'];
             $this->requestInfo['absRootURL'] = sprintf($this->requestInfo['absRootFormated'], $this->isHTTPS() ? 'https:' : 'http:', $this->isHTTPS() ? $_SERVER['SERVER_PORT'] == '443' ? '' : ':' . $_SERVER['SERVER_PORT'] : ($_SERVER['SERVER_PORT'] == '80' ? '' : ':' . $_SERVER['SERVER_PORT']));
         }
     }
 }