예제 #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;
 }
예제 #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)
 {
     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']));
     }
 }
예제 #3
0
 /**
  * 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']));
         }
     }
 }