diff --git a/src/sacy/fragment-cache.php b/src/sacy/fragment-cache.php index b5897b5..dee52e6 100644 --- a/src/sacy/fragment-cache.php +++ b/src/sacy/fragment-cache.php @@ -31,8 +31,11 @@ function get($key){ function set($key, $value){ $p = $this->key2file($key); - if (!@mkdir(dirname($p), 0755, true)){ - throw new Exception("Failed to create fragment cache dir: $p"); + $dir = dirname($p); + if (!is_dir($dir)) { + if (!@mkdir($dir, 0755, true)){ + throw new Exception("Failed to create fragment cache dir: $p"); + } } return @file_put_contents($p, $value); } diff --git a/src/sacy/sacy.php b/src/sacy/sacy.php index 4eb3b64..17b9057 100644 --- a/src/sacy/sacy.php +++ b/src/sacy/sacy.php @@ -102,12 +102,11 @@ private function urlToFile($ref){ if (isset($u['query'])) return false; $ref = $u['path']; - $path = array($_SERVER['DOCUMENT_ROOT']); + $path = array($this->_cfg->getDocumentRoot()); if ($ref[0] != '/') $path[] = $_SERVER['PHP_SELF']; $path[] = $ref; return realpath(implode(DIRECTORY_SEPARATOR, $path)); - } @@ -223,6 +222,11 @@ public function __construct($params = null){ $this->setParams($params); } + public function getDocumentRoot() + { + return realpath(str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME'])); + } + public function getDebugMode(){ if ($this->params['debug_toggle'] === false) return 0; @@ -555,7 +559,7 @@ function writeHeader($fh, $work_units){ fwrite($fh, "/*\nsacy javascript cache dump \n\n"); fwrite($fh, "This dump has been created from the following files:\n"); foreach($work_units as $file){ - fprintf($fh, " - %s\n", str_replace($_SERVER['DOCUMENT_ROOT'], '', $file['file'])); + fprintf($fh, " - %s\n", str_replace($this->getConfig()->getDocumentRoot(), '', $file['file'])); } fwrite($fh, "*/\n\n"); } @@ -595,7 +599,7 @@ function getOutput($work_unit){ function processFile($fh, $work_unit){ if ($this->getConfig()->get('write_headers')) - fprintf($fh, "\n/* %s */\n", str_replace($_SERVER['DOCUMENT_ROOT'], '', $work_unit['file'])); + fprintf($fh, "\n/* %s */\n", str_replace($this->getConfig()->getDocumentRoot(), '', $work_unit['file'])); fwrite($fh, $this->getOutput($work_unit)); } @@ -631,7 +635,7 @@ function writeHeader($fh, $work_units){ fwrite($fh, "/*\nsacy css cache dump \n\n"); fwrite($fh, "This dump has been created from the following files:\n"); foreach($work_units as $file){ - fprintf($fh, " - %s\n", str_replace($_SERVER['DOCUMENT_ROOT'], '', $file['file'])); + fprintf($fh, " - %s\n", str_replace($this->getConfig()->getDocumentRoot(), '', $file['file'])); } fwrite($fh, "*/\n\n"); } @@ -648,7 +652,7 @@ function processFile($fh, $work_unit){ $content = \Minify_CSS_UriRewriter::rewrite( $content, dirname($work_unit['file']), - $_SERVER['DOCUMENT_ROOT'], + $this->getConfig()->getDocumentRoot(), array(), true ); @@ -660,7 +664,7 @@ function processFile($fh, $work_unit){ ); }else{ if ($this->getConfig()->get('write_headers')) - fprintf($fh, "\n/* %s */\n", str_replace($_SERVER['DOCUMENT_ROOT'], '', $work_unit['file'])); + fprintf($fh, "\n/* %s */\n", str_replace($this->getConfig()->getDocumentRoot(), '', $work_unit['file'])); fwrite($fh, $this->getOutput($work_unit)); } @@ -728,12 +732,13 @@ function getOutput($work_unit){ return \Minify_CSS_UriRewriter::rewrite( $css, dirname($source_file), - $_SERVER['DOCUMENT_ROOT'], + $this->getConfig()->getDocumentRoot(), array() ); }else{ return \Minify_CSS::minify($css, array( - 'currentDir' => dirname($source_file) + 'currentDir' => dirname($source_file), + 'docRoot' => $this->getConfig()->getDocumentRoot() )); } }