Example #1
0
File: Cookie.php Project: fem/spof
 /**
  * Get the path where to store the cookie on the current domain.
  *
  * @internal
  *
  * @return string
  */
 private static function getCookiePath()
 {
     return Config::getDetail('server', 'path');
 }
Example #2
0
 /**
  * This function generates a new .css file from the existing files (if anything new happen to them or does not
  * exist). The name of the new file will be returned.
  *
  * @api
  *
  * @param array $files
  *
  * @return string|false false on error or nothing
  */
 public static function combine(array $files)
 {
     if (empty($files)) {
         return false;
     }
     $target = self::getTargetPath();
     $source = self::getSourcePath();
     // identify file combinations by hash
     $cssHash = md5(serialize($files));
     $targetfile = $target . $cssHash . '.css';
     // check if any source file was modified
     $needUpdate = false;
     if (file_exists($targetfile)) {
         $hashtime = filemtime($targetfile);
         foreach ($files as $file) {
             if ($hashtime < filemtime($file['name'])) {
                 $needUpdate = true;
                 break;
             }
         }
     } else {
         // file does not exist, so we need an update anyway
         $needUpdate = true;
     }
     // we can abort if no update is required
     if ($needUpdate === false) {
         return $cssHash;
     }
     // combine file contents
     $handle = fopen($targetfile, 'w+');
     foreach ($files as $file) {
         $content = file_get_contents($file['name']);
         if ($file['fixpaths']) {
             preg_match_all('/url\\(([^)]+)\\)/', $content, $matches, PREG_SET_ORDER);
             $replaces = [];
             $copy = [];
             foreach ($matches as $match) {
                 if (strpos($match[1], 'data') === 0) {
                     continue;
                 }
                 $filename = 'gen__' . md5($file['name'] . '-' . $match[1]) . preg_replace('/^[^.]+\\.(.+)$/', '.$1', $match[1]);
                 $replaces[$match[0]] = 'url(../img/' . $filename . ')';
                 $copy[dirname($file['name']) . '/' . $match[1]] = Application::$WEB_ROOT . 'img/' . $filename;
             }
             // replace usage in stylesheet and copy file to be accessible via web
             $content = str_replace(array_keys($replaces), $replaces, $content);
             foreach ($copy as $source => $target) {
                 try {
                     copy($source, $target);
                 } catch (\ErrorException $e) {
                     Logger::getInstance()->exception($e);
                 }
             }
         }
         fwrite($handle, self::minify($content));
     }
     // foreach file
     fclose($handle);
     // adjust file permissions for webserver
     chmod($targetfile, Config::getDetail('stylesheet', 'file_perms', self::$defaultConfig));
     return $cssHash;
 }
Example #3
0
 /**
  * This function generates a new .js file from the existing files (if anything new happen to them or does not
  * exist). The name of the new file will be returned.
  *
  * @api
  *
  * @param array $files
  *
  * @return string|false false on error or nothing
  */
 public static function combine(array $files)
 {
     if (empty($files)) {
         return false;
     }
     $target = self::getTargetPath();
     $source = self::getSourcePath();
     // identify file combinations by hash
     $jsHash = md5(serialize($files));
     $targetfile = $target . $jsHash . '.js';
     // check if any source file was modified
     $needUpdate = false;
     if (file_exists($targetfile)) {
         $hashtime = filemtime($targetfile);
         foreach ($files as $file) {
             if (substr($file['name'], 0, 1) === '/') {
                 $filename = $file['name'];
             } elseif ($file['relative']) {
                 // relative to public/js
                 $filename = $target . $file['name'] . '.js';
             } else {
                 // use javascript folder
                 $filename = $source . $file['name'] . '.js.tpl';
             }
             if ($hashtime < filemtime($filename)) {
                 $needUpdate = true;
                 break;
             }
         }
     } else {
         // file does not exist, so we need an update anyway
         $needUpdate = true;
     }
     // we can abort if no update is required
     if ($needUpdate === false) {
         return $jsHash;
     }
     // make sure, that the target directory exists
     if (!is_dir($target)) {
         FileUtil::makedir($target);
     }
     // combine file contents
     $content = '';
     foreach ($files as $file) {
         try {
             if (substr($file['name'], 0, 1) === '/') {
                 if (strpos($file['name'], '.tpl') > 0) {
                     $nextcontent = JavascriptTemplate::getInstance()->fetch($file['name']);
                 } else {
                     $nextcontent = file_get_contents($file['name']);
                 }
             } elseif ($file['relative']) {
                 $nextcontent = file_get_contents($target . $file['name'] . '.js');
             } else {
                 $nextcontent = JavascriptTemplate::getInstance()->fetch($source . $file['name'] . '.js.tpl');
             }
             // do not double minify
             if (strpos($file['name'], '.min') > 0 || strpos($file['name'], '.pack') > 0 || !method_exists('\\JShrink\\Minifier', 'minify')) {
                 $content .= $nextcontent . "\n";
             } else {
                 $content .= \JShrink\Minifier::minify($nextcontent) . "\n";
             }
         } catch (\ErrorException $exception) {
             Logger::getInstance()->exception($exception);
         }
     }
     // foreach file
     // write minified version
     $success = file_put_contents($targetfile, $content);
     // adjust file permissions for webserver
     if ($success) {
         chmod($targetfile, Config::getDetail('smarty', 'file_perms', self::$defaultConfig));
     }
     // foreach scssFiles
     return $jsHash;
 }
Example #4
0
File: Router.php Project: fem/spof
 /**
  * This function generates the htaccess ruleset based on the preset and the generated routes defined in the source
  * file. Generation is only done if relevant files got changed.
  *
  * @internal
  */
 public static function updateRules()
 {
     $target = self::getTargetFile();
     $source = self::getSourceFile();
     $preset = self::getPresetFile();
     // check if rulesset is outdated, before updating it
     if (file_exists($target)) {
         $target_time = filemtime($target);
         if (filemtime($source) < $target_time && filemtime($preset) < $target_time && filemtime(__FILE__) < $target_time) {
             if (empty(self::$additionalRoutes)) {
                 return;
             }
             $outdated = false;
             foreach (self::$additionalRoutes as $route) {
                 if (filemtime(Application::$FILE_ROOT . $route . '/routes.yml') > $target_time) {
                     $outdated = true;
                 }
             }
             if ($outdated) {
                 return;
             }
             return true;
         }
     }
     $routes = self::getRoutes();
     // if parse error occurred, stop here
     if (empty($routes)) {
         return;
     }
     $rules = fopen($target, 'w+');
     ob_start();
     /** @noinspection PhpIncludeInspection */
     require $preset;
     fwrite($rules, ob_get_clean());
     // write each rule
     $unfolded = self::expandAndSort($routes);
     foreach ($unfolded as $route) {
         fwrite($rules, 'RewriteRule ^' . self::getRegexPattern($route['pattern']) . '$ ' . self::getReplaceUrl($route['pattern'], $route['static']) . '&%{QUERY_STRING} [L,QSA]' . "\n");
     }
     // write 404 rule last
     fwrite($rules, 'RewriteRule ^.*$ ' . 'index.php?module=errors&show=show404&%{QUERY_STRING} [L,QSA]' . "\n");
     fclose($rules);
     error_log('@@' . Config::getDetail('router', 'file_perms', self::$defaultConfig));
     chmod($target, Config::getDetail('router', 'file_perms', self::$defaultConfig));
 }