示例#1
0
 /**
  * Store an encrypted cookie
  *
  * @param string $cookieName
  * @param mixed  $cookieValue
  * @param int    $expiry default stores just for the browser session
  */
 public static function set($cookieName, $cookieValue, $expiry = 0)
 {
     if (isset($_COOKIE['synsec'])) {
         $synsec = $_COOKIE['synsec'];
     } else {
         $synsec = Tools::randomString('12');
     }
     if ((!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != 'on') && (!isset($_SERVER['HTTP_X_FORWARDED_PROTO']) || $_SERVER['HTTP_X_FORWARDED_PROTO'] != 'https')) {
         $ssl = false;
     } else {
         $ssl = true;
     }
     setcookie('synsec', $synsec, time() + 60 * 60 * 24 * 30, '/', $_SERVER['HTTP_HOST'], $ssl, true);
     $synsec .= 'synErgy' . self::$token;
     /* Open the cipher */
     $td = mcrypt_module_open('rijndael-256', '', 'ofb', '');
     /* Create the IV and determine the keysize length, use MCRYPT_RAND
      * on Windows instead */
     $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
     $ks = mcrypt_enc_get_key_size($td);
     /* Create key */
     $key = substr(md5($synsec), 0, $ks);
     /* Intialize encryption */
     mcrypt_generic_init($td, $key, $iv);
     /* Encrypt data */
     $encrypted = mcrypt_generic($td, serialize($cookieValue));
     # Store our secure cookie
     setcookie($cookieName, trim(base64_encode($iv . '|' . $encrypted)), $expiry, '/', $_SERVER['HTTP_HOST'], $ssl, true);
     /* Terminate encryption handler */
     mcrypt_generic_deinit($td);
 }
示例#2
0
 /**
  * Save a cached file of the output
  *
  * @param $content
  */
 protected function writeCacheFile($content)
 {
     $dir = $this->temp_dir . DIRECTORY_SEPARATOR . 'synergy';
     Logger::debug('Synergy cache dir: ' . $dir);
     if (!is_dir($dir)) {
         Tools::mkdir($dir);
     }
     $file = $dir . DIRECTORY_SEPARATOR . md5($this->request->getUri()) . '.syn';
     $fh = fopen($file, 'w');
     fputs($fh, $content, strlen($content));
     @fclose($fh);
     if (!$this->isDev && $this->useGzip()) {
         Logger::info('Compressing response');
         $zp = gzopen($file . '.gz', 'w9');
         gzwrite($zp, $content);
         gzclose($zp);
         // remove gzip file if it's bigger than the unzipped file
         if (filesize($file . '.gz') > filesize($file)) {
             unlink($file . '.gz');
         }
     }
 }
示例#3
0
 /**
  * Prepares the cache folder for Smarty
  *
  * @return void
  */
 private function initSmartyCache()
 {
     if (!is_dir($this->cacheDir)) {
         Tools::mkdir($this->cacheDir, true);
     }
     // compiled templates dir
     $path = $this->cacheDir . DIRECTORY_SEPARATOR . 'templates_c' . DIRECTORY_SEPARATOR;
     if (!is_dir($path)) {
         Tools::mkdir($path, false);
     }
     $this->loader->setCompileDir($path);
     // cache dir
     $path = $this->cacheDir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
     if (!is_dir($path)) {
         Tools::mkdir($path, false);
     }
     $this->loader->setCacheDir($path);
     // configs dir
     $path = $this->cacheDir . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR;
     if (!is_dir($path)) {
         Tools::mkdir($path, false);
     }
     $this->loader->setConfigDir($path);
 }
示例#4
0
 /**
  * Remove the entire template cache dir
  *
  * @return void
  */
 public function emptyCacheDir()
 {
     if (isset($this->cacheDir) && is_dir($this->cacheDir)) {
         Tools::removeDir($this->cacheDir);
     }
 }
示例#5
0
 /**
  * @param $filename
  *
  * @throws InvalidArgumentException
  */
 public function setFilename($filename)
 {
     // close any open file resource before changing the filename
     $this->closeFH();
     $filename = trim($filename);
     // check the filename is valid before setting
     if (is_string($filename) && substr($filename, 0, 1) == DIRECTORY_SEPARATOR) {
         // split out the parts of the filename
         $parts = pathinfo($filename);
         // clean the filename
         $filename = $parts['dirname'] . DIRECTORY_SEPARATOR . preg_replace("/[^A-Za-z0-9+]/", '_', $parts['filename']);
         if (isset($parts['extension']) && strlen($parts['extension']) > 0) {
             $filename .= '.' . $parts['extension'];
         }
         // test the dir
         if (!is_dir($parts['dirname']) && !is_writable(dirname($parts['dirname']))) {
             throw new InvalidArgumentException("filename must be an absolute filename in a writeable directory : {$filename}");
         } else {
             if (!Tools::mkdir($parts['dirname'], true)) {
                 throw new InvalidArgumentException("filename must be an absolute filename in a writeable directory : {$filename}");
             }
         }
         // Test an existing file is writable
         if (file_exists($filename) && !is_writable($filename)) {
             $processUser = posix_getpwuid(posix_geteuid());
             throw new InvalidArgumentException('logfile must be writeable by user: '******'name']);
         } else {
             if (!file_exists($filename)) {
                 touch($filename);
             }
         }
         $this->filename = $filename;
     } else {
         throw new InvalidArgumentException("filename must be an absolute filename in a writeable directory : {$filename}");
     }
 }
示例#6
0
 /**
  * directory where the app data lives
  *
  * @param string $dir directory where the app data live
  *
  * @return void
  * @throws InvalidArgumentException
  */
 public function setAppDir($dir)
 {
     if (!is_dir($dir) && !Tools::mkdir($dir, true)) {
         throw new InvalidArgumentException(sprintf("Invalid App directory, %s", $dir));
     } else {
         if (!is_readable($dir)) {
             throw new InvalidArgumentException(sprintf("App Directory %s not readable", $dir));
         } else {
             $this->app_dir = $dir;
             $classLoader = new SplClassLoader($dir);
             $classLoader->register();
         }
     }
 }