Exemplo n.º 1
0
 public function load(Reader $reader)
 {
     $constants = $reader->read();
     foreach ($constants as $name => $value) {
         // Check name
         if (!Validate::isVariableName($name)) {
             throw new \Exception('Name of constant must be a valid variable name');
         }
         // Check if is already loaded
         if (array_key_exists($name, self::$_constants)) {
             // If is already defined => next
             if (defined($name)) {
                 Logger::getInstance()->debug('Constant : "' . $name . '" already defined');
                 continue;
             }
             Logger::getInstance()->debug('Constant : "' . $name . '" already load, was overloaded');
         }
         if (is_array($value)) {
             if (empty($value)) {
                 $value = null;
             } else {
                 throw new \Exception('Constant value cannot be an array');
             }
         }
         // Cast value
         if (is_string($value)) {
             $value = Tools::castValue($value);
         }
         // Add
         self::$_constants[$name] = $value;
     }
 }
Exemplo n.º 2
0
 public function load(Reader $reader)
 {
     $caches = $reader->read();
     foreach ($caches as $cacheName => $cacheValue) {
         // Check name
         if (!Validate::isVariableName($cacheName)) {
             throw new \Exception('Name of cache must be a valid variable name');
         }
         // Check options
         $params = array();
         foreach ($cacheValue as $name => $value) {
             // no use comment (for xml file)
             if ($name == 'comment') {
                 continue;
             }
             // cast
             if (is_string($value)) {
                 $value = Tools::castValue($value);
             }
             // add value into cache parameters
             $params[$name] = $value;
         }
         // check adaptater
         if (!isset($params['adaptater'])) {
             throw new \Exception('Miss adaptater parameter for cache : "' . $cacheName . '"');
         }
         // Add param name
         $params['name'] = $cacheName;
         // Add cache
         CacheManager::addCache($cacheName, CacheManager::factory($params['adaptater'], $params, 'framework\\cache\\adaptaters', 'framework\\cache\\IAdaptater'), true);
     }
 }
Exemplo n.º 3
0
 public function load(Reader $reader)
 {
     $security = $reader->read();
     foreach ($security as $type => $datas) {
         $securityData = array();
         if (isset($datas['autorun']) && is_string($datas['autorun'])) {
             $securityData['autorun'] = Tools::castValue($datas['autorun']);
         } elseif (!isset($datas['autorun'])) {
             $securityData['autorun'] = false;
         }
         //default value
         foreach ($datas as $name => $value) {
             if ($name == 'autorun' || $name == 'comment' || $name == 'form') {
                 continue;
             }
             if (is_string($value)) {
                 $value = Tools::castValue($value);
             }
             $securityData[$name] = $value;
         }
         //formulaires (for Form api)
         if (isset($datas['form'])) {
             $securityData = array();
             foreach ($datas['form'] as $formName => $formDatas) {
                 if (!Validate::isVariableName($formName)) {
                     throw new \Exception('Security form name must be a valid variable');
                 }
                 $form = new \stdClass();
                 $form->name = $formName;
                 if (isset($formDatas['protection'])) {
                     $protections = array();
                     foreach ($formDatas['protection'] as $protectionType => $protectionDatas) {
                         if (is_array($protectionDatas)) {
                             foreach ($protectionDatas as $optionName => $optionValue) {
                                 if ($optionName == 'comment') {
                                     continue;
                                 }
                                 if (is_string($optionValue)) {
                                     $protectionDatas[$optionName] = Tools::castValue($optionValue);
                                 }
                             }
                         }
                         if (is_string($value)) {
                             $value = Tools::castValue($value);
                         }
                         $protections[$protectionType] = $protectionDatas;
                     }
                 }
                 $form->protections = $protections;
                 $securityData[] = $form;
             }
         }
         SecurityManager::addSecurity($type, array('autorun' => $datas['autorun'], 'datas' => $securityData), true);
     }
 }
Exemplo n.º 4
0
 public function run()
 {
     $ip = Tools::getUserIp();
     $userAgent = Http::getServer('HTTP_USER_AGENT');
     //badcrawler detected
     if (Session::getInstance()->get(md5($ip . 'badcrawler'))) {
         Router::getInstance()->show403(true);
     }
     $this->_check($ip, $userAgent);
     Logger::getInstance()->debug('Sniffer security was run', 'security');
 }
Exemplo n.º 5
0
 public static function getVar($varName, $default = null)
 {
     if (!Validate::isVariableName($varName)) {
         throw new \Exception('language var name : "' . $varName . '"must be a valid variable');
     }
     if (!property_exists(self::$_languageVars, $varName)) {
         Logger::getInstance()->debug('Language var ' . $varName . ' is not setted');
         return $default;
     } else {
         return Tools::castValue((string) self::$_languageVars->{$varName});
     }
 }
Exemplo n.º 6
0
 public function generateSlug($lastSlug = null)
 {
     $manager = self::factoryManager('new', 'default', 'new');
     $exist = true;
     $salt = '';
     $i = 0;
     while ($exist && $i < 50) {
         $this->_slug = Tools::stringToUrl($this->_titre, '-', 'UTF-8', true) . $salt;
         $count = $manager->existsSlug($this->_slug, $lastSlug);
         $exist = $count >= 1 ? true : false;
         $salt = (string) $i;
         $i++;
     }
     return $this->_slug;
 }
Exemplo n.º 7
0
 public function purgeLogsDir($deleteRootLogsDir = true)
 {
     $dir = Tools::cleanScandir(self::$_logDir);
     foreach ($dir as &$f) {
         if (is_file(self::$_logDir . $f)) {
             unlink(self::$_logDir . $f);
         }
         if (is_dir(self::$_logDir . $f)) {
             Tools::deleteTreeDirectory(self::$_logDir . $f);
         }
     }
     if ($deleteRootLogsDir) {
         chmod(self::$_logDir, 0775);
         rmdir(self::$_logDir);
     }
 }
Exemplo n.º 8
0
 public function __construct($ident = false, $option = LOG_CONS, $facility = LOG_USER)
 {
     if ($ident != false) {
         if (!is_string($ident)) {
             throw new \Exception('ident parameter must be a string');
         }
     }
     switch ($facility) {
         case LOG_AUTH:
         case LOG_AUTHPRIV:
         case LOG_CRON:
         case LOG_DAEMON:
         case LOG_KERN:
         case LOG_LPR:
         case LOG_MAIL:
         case LOG_NEWS:
         case LOG_SYSLOG:
         case LOG_USER:
         case LOG_UUCP:
             break;
         case LOG_LOCAL0:
         case LOG_LOCAL1:
         case LOG_LOCAL2:
         case LOG_LOCAL3:
         case LOG_LOCAL4:
         case LOG_LOCAL5:
         case LOG_LOCAL6:
         case LOG_LOCAL7:
             if (Tools::isWindows()) {
                 throw new \Exception('This facility parameter isn\'t valid on your system');
             }
             break;
         default:
             throw new \Exception('This facility parameter is invalid');
     }
     switch ($option) {
         case LOG_CONS:
         case LOG_NDELAY:
         case LOG_ODELAY:
         case LOG_PERROR:
         case LOG_PID:
             break;
         default:
             throw new \Exception('This facility parameter is invalid');
     }
     openlog($ident, $option, $facility);
 }
Exemplo n.º 9
0
 public function loadFile($filename, Loader $loader = null, Reader $reader = null)
 {
     if (!file_exists($filename)) {
         throw new \Exception('File : "' . $filename . '" not exists');
     }
     if ($loader === null && $reader === null) {
         $ext = Tools::getFileExtension($filename);
     }
     //get reader by name of file
     if ($reader === null) {
         $reader = $this->_factory($ext, self::READER, $filename);
     }
     //get loader by name of file
     if ($loader === null) {
         $loader = $this->_factory(basename($filename, '.' . $ext), self::LOADER);
     }
     if ($reader && $loader) {
         $this->load($loader, $reader);
     }
 }
Exemplo n.º 10
0
 protected function _urlHaveRewriteRule($url)
 {
     foreach ($this->_rules as &$rule) {
         if (preg_match('#' . Tools::selectStringByDelimiter($rule, '^', '$') . '#', $url)) {
             return true;
         }
     }
     return false;
 }
Exemplo n.º 11
0
 public function load(Reader $reader)
 {
     $routes = $reader->read();
     foreach ($routes as $name => $datas) {
         // Check name
         if (!Validate::isVariableName($name)) {
             throw new \Exception('Route name must be a valid variable');
         }
         // Check controller info
         if (!isset($datas['controller'])) {
             throw new \Exception('Miss controller into route "' . $name . '"');
         }
         // create instance of route
         $route = new RouterRoute($name, $datas['controller']);
         // Optionnals parameters
         if (isset($datas['regex'])) {
             $route->setRegex(Tools::castValue($datas['regex']));
         }
         if (isset($datas['requireSsl'])) {
             $route->setRequireSsl(Tools::castValue($datas['requireSsl']));
         }
         if (isset($datas['requireAjax'])) {
             $route->setRequireAjax(Tools::castValue($datas['requireAjax']));
         }
         if (isset($datas['autoSetAjax'])) {
             $route->setAutoSetAjax(Tools::castValue($datas['autoSetAjax']));
         }
         if (isset($datas['requireHttpMethod'])) {
             $route->setRequireHttpMethod(Tools::castValue($datas['requireHttpMethod']));
         }
         if (isset($datas['httpResponseStatusCode'])) {
             $route->setHttpResponseStatusCode(Tools::castValue($datas['httpResponseStatusCode']));
         }
         if (isset($datas['httpProtocol'])) {
             $route->setHttpProtocol(Tools::castValue($datas['httpProtocol']));
         }
         if (isset($datas['rules'])) {
             if (is_array($datas['rules'])) {
                 if (isset($datas['rules']['rule']) && is_array($datas['rules']['rule'])) {
                     $datas['rules'] = $datas['rules']['rule'];
                 }
             }
             $route->setRules($datas['rules']);
         }
         if (isset($datas['methods'])) {
             if (is_array($datas['methods'])) {
                 $methods = $datas['methods'];
                 foreach ($methods as $method => $val) {
                     //no have parameters, replace wtih empty parameters list
                     if (is_int($method)) {
                         //TODO fix : replace methode into good order
                         unset($methods[$method]);
                         $methods[$val] = array();
                     }
                 }
                 $route->setMethods($methods);
             }
         }
         // Add into router
         Router::addRoute($route, true);
     }
 }
Exemplo n.º 12
0
 public function setImageHorizontalLine($horizontalLineCount, $horizontalLineColorType = 0, $horizontalLineColorValue = null, $horizontalLineThickness = null)
 {
     if (!is_int($horizontalLineCount) || $horizontalLineCount < 1) {
         throw new \Exception('horizontal line count invalid');
     }
     $this->_imageHorizontalLine = $horizontalLineCount;
     if (!is_int($horizontalLineColorType) || $horizontalLineColorType < 0 || $horizontalLineColorType > 3) {
         throw new \Exception('Horizontal line color type invalid');
     }
     $this->_imageHorizontalLineColorType = $horizontalLineColorType;
     if ($this->_imageHorizontalLineColorType == 0) {
         // all horizontal line have same color
         if (is_null($horizontalLineColorValue)) {
             $this->_imageHorizontalLineColorValue = array('R' => 0, 'G' => 0, 'B' => 0);
         } else {
             if (is_string($horizontalLineColorValue)) {
                 $rgb = explode('/', $horizontalLineColorValue);
             }
             $color = isset($rgb) && is_array($rgb) && count($rgb) >= 3 ? new Color($rgb[0], $rgb[1], $rgb[2]) : new Color($horizontalLineColorValue);
             $this->_imageHorizontalLineColorValue = $color->getColor();
         }
         if ($this->_imageHorizontalLineColorValue == $this->_imageBackgroundColor) {
             throw new \Exception('Horizontal line color value must be different to background color');
         }
     } else {
         // check validty of color value for color type 1
         if ($this->_imageHorizontalLineColorType == 1) {
             $colors = explode('-', $horizontalLineColorValue);
             if (!is_array($colors) || $this->_imageHorizontalLine > count($colors)) {
                 throw new \Exception('Invalid horizontal lines colors value, miss a color');
             }
         }
         for ($i = 0; $i < $this->_imageHorizontalLine; $i++) {
             if ($this->_imageHorizontalLineColorType == 1) {
                 if (is_string($colors[$i])) {
                     $rgb = explode('/', $colors[$i]);
                 }
                 $color = isset($rgb) && is_array($rgb) && count($rgb) >= 3 ? new Color($rgb[0], $rgb[1], $rgb[2]) : new Color($colors[$i]);
                 $this->_imageHorizontalLineColorValue[$i] = $color->getColor();
                 if ($this->_imageHorizontalLineColorValue[$i] == $this->_imageBackgroundColor) {
                     throw new \Exception('Horizontal lines color value must be different to background color');
                 }
             } elseif ($this->_imageHorizontalLineColorType == 2) {
                 // random colors
                 $r = Tools::generateInt(0, 255, $this->_imageBackgroundColor['R']);
                 $g = Tools::generateInt(0, 255, $this->_imageBackgroundColor['G']);
                 $b = Tools::generateInt(0, 255, $this->_imageBackgroundColor['B']);
                 $color = new Color($r, $g, $b);
                 $this->_imageHorizontalLineColorValue[$i] = $color->getColor();
             }
         }
     }
     if (!is_null($horizontalLineThickness) && !is_int($horizontalLineThickness) || $horizontalLineThickness > $this->_imageWidth) {
         throw new \Exception('Horizontal line Thickness value must be an integer or null, and inferior to image width');
     }
     $this->_imageHorizontalLineThickness = $horizontalLineThickness;
     // TODO imagedashedline ???
 }
Exemplo n.º 13
0
 public function setDsn($dsn, $check = true)
 {
     if (!is_string($dsn)) {
         throw new \Exception('Dsn must be a string');
     }
     if ($check) {
         extract(Tools::parseDsn($dsn));
         // check required infos
         if (!isset($driver)) {
             throw new \Exception('Miss driver type');
         }
         if (!isset($host)) {
             throw new \Exception('Miss server host type');
         }
         if (!isset($port)) {
             throw new \Exception('Miss server port type');
         }
         if (!isset($dbname)) {
             throw new \Exception('Miss server dbname type');
         }
         if (!isset($charset)) {
             throw new \Exception('Miss server charset type');
         }
     }
     $this->_dsn = $dsn;
     return $this;
 }
Exemplo n.º 14
0
 /**
  * Checks if the string is a well formed email address..
  *
  * @param string $string
  * @return boolean
  */
 public static function isEmail($email)
 {
     if (!is_string($email) || strlen($email) > 255) {
         return false;
     }
     if (Tools::phpVersionCompareTo('5.3.3') >= 0) {
         return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
     } else {
         return (bool) preg_match('`^(?!\\.)[.\\w!#$%&\'*+/=?^_\\`{|}~-]+@(?!-)[\\w-]+\\.\\w+$`i', $email);
     }
 }
Exemplo n.º 15
0
 public function minify($returnContent = true, $forceCacheUpdate = false)
 {
     // autoloading files
     foreach (Tools::cleanScandir($this->getPath()) as $file) {
         if (Validate::isFileExtension($this->_type, $file)) {
             $this->addFile($this->getPath() . $file);
         }
     }
     $this->_key = md5($this->_name . Router::getHost(true, Http::isHttps()) . $this->getPath());
     if ($this->_cacheExpired() || $forceCacheUpdate) {
         $this->_generateCache();
     }
     if ($returnContent) {
         return $this->getContent();
     }
 }
Exemplo n.º 16
0
 public function clearGroup($groupName)
 {
     $dirs = Tools::cleanScanDir($this->_path);
     foreach ($dirs as &$f) {
         if (is_file($this->_path . $f)) {
             //is not a lock
             if (stripos($f, $this->_prefix . $this->_prefixGroups . md5('lock')) == false) {
                 // find
                 if (stripos($f, $groupName) !== false) {
                     $file = new \SplFileObject($this->_path . $f, 'r');
                     $data = unserialize(base64_decode($file->fgets()));
                     $key = isset($data[1]) ? $data[1] : $f;
                     $this->delete($key);
                 }
             }
         }
     }
     Logger::getInstance()->debug('Cache cleared group : "' . $groupName . '"', 'cache' . $this->_name);
 }
Exemplo n.º 17
0
 protected function _generateUrl($flushUrlAfter = false, $flusUrlParameters = false)
 {
     if ($this->_rewrite) {
         $url = new Rewriting($this->_rewriteOptions['rewriteRuleFile']);
         $url->setCharset($this->_charset);
         if ($this->_file != null && $this->_rewriteOptions['keepUrlFile']) {
             $url->setUrlFile($this->_file, $this->_rewriteOptions['keepUrlFileExt']);
         }
         foreach ($this->_args as &$arg) {
             $url->setArgs($arg, $this->_rewriteOptions['notPutArgsKey'], false, $flushUrlAfter, $flusUrlParameters);
         }
         $this->_url = $url->getUrl($this->_rewriteOptions['checkRewriteRule']);
     } else {
         $argsLink = count($this->_args) > 0 ? '?' : '';
         foreach ($this->_args as &$arg) {
             foreach ($arg as $key => &$value) {
                 $argsLink .= Tools::stringToUrl($key, '_', $this->_charset) . '=' . Tools::stringToUrl($value, '_', $this->_charset) . '&amp;';
             }
         }
         $this->_url = $this->_file != null ? $this->_file . trim($argsLink, '&amp;') : trim($argsLink, '&amp;');
     }
 }
Exemplo n.º 18
0
 public function load(Reader $reader)
 {
     $databases = $reader->read();
     foreach ($databases as $name => $datas) {
         // Check name
         if (!Validate::isVariableName($name)) {
             throw new \Exception('Name of database must be a valid variable name');
         }
         // Check essential parameters
         if (!isset($datas['adaptater'])) {
             throw new \Exception('Miss adaptater config param for database : "' . $name . '"');
         }
         if (!isset($datas['type'])) {
             throw new \Exception('Miss type config param for database : "' . $name . '"');
         }
         if (!isset($datas['server'])) {
             throw new \Exception('Miss server config param for database : "' . $name . '"');
         }
         // Create database instance
         $database = new DatabaseManager($name, $datas['type'], DatabaseManager::factory($datas['adaptater'], $name, 'framework\\database\\adaptaters', 'framework\\database\\IAdaptater'));
         // Fetch servers
         foreach ($datas['server'] as $server) {
             // extract server informations
             extract($server);
             // extract dsn
             if (isset($dsn)) {
                 extract(Tools::parseDsn($dsn));
             }
             // check required infos
             if (!isset($type)) {
                 throw new \Exception('Miss server type');
             }
             if (!isset($dbuser)) {
                 throw new \Exception('Miss server dbuser type');
             }
             if (!isset($dbpassword)) {
                 throw new \Exception('Miss server dbpassword type');
             }
             if (!isset($driver)) {
                 throw new \Exception('Miss driver type');
             }
             if (!isset($host)) {
                 throw new \Exception('Miss server host type');
             }
             if (!isset($port)) {
                 throw new \Exception('Miss server port type');
             }
             if (!isset($dbname)) {
                 throw new \Exception('Miss server dbname type');
             }
             if (!isset($charset)) {
                 throw new \Exception('Miss server charset type');
             }
             // Check driver is supported by database adaptater
             if (!$database->isValidDriver($driver)) {
                 throw new \Exception('Invalid driver : "' . $driver . '", not supported database adaptater : "' . $datas['adaptater'] . '"');
             }
             // Create server instance
             $serverInstance = new Server($type, $dbuser, $dbpassword, $driver, $host, $port, $dbname, $charset);
             if (isset($dsn)) {
                 $serverInstance->setDsn($dsn, false);
             }
             // Add into servers list
             $database->addServer($serverInstance);
             //flush vars
             unset($type, $dbuser, $dbpassword, $driver, $host, $port, $dbname, $charset, $dsn, $serverInstance);
         }
         // Add database
         DatabaseManager::addDatabase($name, $database, true);
     }
 }
Exemplo n.º 19
0
 protected function _generateSecurity()
 {
     self::_checkState();
     return md5(Tools::getUserIp() . Http::getServer('HTTP_USER_AGENT'));
 }