private function _getAssetPath($image)
 {
     $url = $this->_getImageUrl($image);
     if (stripos($url, "/assets/") === 0) {
         $file = new Kwf_Assets_Dependency_File(substr($url, 8));
         return $file->getAbsoluteFileName();
     } else {
         throw new Kwf_Exception("Path does not include '/assets/'. Not implemented yet.");
     }
 }
 public function getContentsPacked()
 {
     $cacheId = 'scss-v2-' . $this->getIdentifier();
     $ret = Kwf_Assets_ContentsCache::getInstance()->load($cacheId);
     if ($ret !== false) {
         return $ret;
     }
     $fileName = $this->getAbsoluteFileName();
     static $loadPath;
     if (!isset($loadPath)) {
         $loadPath = array();
         foreach (glob(realpath(VENDOR_PATH) . '/*/*') as $p) {
             $bowerMain = null;
             $mainExt = null;
             if (file_exists($p . '/bower.json')) {
                 $bower = json_decode(file_get_contents($p . '/bower.json'));
                 if (isset($bower->main) && is_string($bower->main)) {
                     $bowerMain = $bower->main;
                     $mainExt = substr($bowerMain, -5);
                 }
             }
             if ($mainExt == '.scss' || $mainExt == '.sass') {
                 $mainDir = substr($bowerMain, 0, strrpos($bowerMain, '/'));
                 $loadPath[] = $p . '/' . $mainDir;
             } else {
                 if (file_exists($p . '/scss')) {
                     $loadPath[] = $p . '/scss';
                 }
             }
         }
         $loadPath[] = './scss';
         if (KWF_PATH == '..') {
             $loadPath[] = substr(getcwd(), 0, strrpos(getcwd(), '/')) . '/sass/Kwf/stylesheets';
         } else {
             $loadPath[] = KWF_PATH . '/sass/Kwf/stylesheets';
         }
         $loadPath[] = 'temp/scss-generated';
         $loadPath = escapeshellarg(implode(PATH_SEPARATOR, $loadPath));
     }
     $buildFile = 'temp/scss-' . str_replace(array('\\', ':', '/', '.', '-'), '_', $this->getFileNameWithType());
     if (!is_dir('temp/scss-generated')) {
         mkdir('temp/scss-generated');
     }
     if ($this->_config) {
         $config = "\$config: " . self::_generateScssConfig($this->_config) . ";\n";
         file_put_contents('temp/scss-generated/_config.scss', $config);
     } else {
         if (file_exists('temp/scss-generated/_config.scss')) {
             unlink('temp/scss-generated/_config.scss');
         }
     }
     if (substr($fileName, 0, 2) == './') {
         $fileName = str_replace(DIRECTORY_SEPARATOR, '/', getcwd()) . substr($fileName, 1);
     }
     $wrapperContents = "";
     $wrapperContents .= "@import \"config/global-settings\";\n";
     $wrapperContents .= "@import \"{$fileName}\";\n";
     $wrapperFile = tempnam('temp', 'scw');
     file_put_contents($wrapperFile, $wrapperContents);
     chmod($wrapperFile, 0777);
     $bin = Kwf_Config::getValue('server.nodeSassBinary');
     if (!$bin) {
         $bin = getcwd() . "/" . VENDOR_PATH . "/bin/node " . getcwd() . '/node_modules/node-sass/bin/node-sass';
     } else {
         $p = json_decode(file_get_contents(getcwd() . '/node_modules/node-sass/package.json'), true);
         $bin = str_replace('%version%', $p['version'], $bin);
         unset($p);
     }
     $cmd = "{$bin} --include-path {$loadPath} --output-style compressed ";
     $cmd .= " --source-map " . escapeshellarg($buildFile . '.map');
     $cmd .= " " . escapeshellarg($wrapperFile) . " " . escapeshellarg($buildFile);
     $cmd .= " 2>&1";
     $out = array();
     exec($cmd, $out, $retVal);
     if ($retVal) {
         throw new Kwf_Exception("compiling sass failed: " . implode("\n", $out));
     }
     $map = json_decode(file_get_contents("{$buildFile}.map"));
     $sources = array();
     $masterFiles = array_values($this->_configMasterFiles);
     foreach ($map->sources as $k => $i) {
         //sources are relative to cache/sass, strip that
         if (substr($i, 0, 15) == 'scss-generated/' || substr($i, 0, 3) == 'scw') {
             $f = substr($this->getFileNameWithType(), 0, -5) . '/temp/' . $i;
             $map->{'_x_org_koala-framework_sourcesContent'}[$k] = file_get_contents('temp/' . $i);
             $masterFiles[] = 'temp/' . $i;
         } else {
             if (substr($i, 0, 3) != '../') {
                 throw new Kwf_Exception('source doesn\'t start with ../: ' . $i);
             }
             $i = substr($i, 3);
             $f = self::getPathWithTypeByFileName($this->_providerList, getcwd() . '/' . $i);
             if (!$f) {
                 throw new Kwf_Exception("Can't find path for '" . getcwd() . '/' . $i . "'");
             }
             $dep = new Kwf_Assets_Dependency_File($this->_providerList, $i);
             $masterFiles[] = $dep->getAbsoluteFileName();
         }
         $sources[$k] = '/assets/' . $f;
         if (substr($f, 0, 16) == 'web/scss/config/') {
             $masterFiles[] = KWF_PATH . '/sass/Kwf/stylesheets/config/' . substr($f, 16);
         } else {
             if (substr($f, 0, 32) == 'kwf/sass/Kwf/stylesheets/config/') {
                 $masterFiles[] = 'scss/config/' . substr($f, 32);
             }
         }
     }
     $map->{'_x_org_koala-framework_masterFiles'} = $masterFiles;
     $map->sources = $sources;
     $map->file = $buildFile;
     file_put_contents("{$buildFile}.map", json_encode($map));
     unlink($wrapperFile);
     if ($this->_config) {
         unlink('temp/scss-generated/_config.scss');
     }
     $ret = file_get_contents($buildFile);
     $ret = str_replace("@charset \"UTF-8\";\n", '', $ret);
     //remove charset, no need to adjust sourcemap as sourcemap doesn't include that (bug in libsass)
     $ret = str_replace(chr(0xef) . chr(0xbb) . chr(0xbf), '', $ret);
     //remove byte order mark
     $ret = preg_replace("#/\\*\\# sourceMappingURL=.* \\*/#", '', $ret);
     $map = new Kwf_SourceMaps_SourceMap(file_get_contents("{$buildFile}.map"), $ret);
     $map->setMimeType('text/css');
     unlink($buildFile);
     unlink("{$buildFile}.map");
     Kwf_Assets_ContentsCache::getInstance()->save($map, $cacheId);
     return $map;
 }
 public function getFileContents($file, $language = null)
 {
     $ret = array();
     if (substr($file, -4) == '.gif') {
         $ret['mimeType'] = 'image/gif';
     } else {
         if (substr($file, -4) == '.png') {
             $ret['mimeType'] = 'image/png';
         } else {
             if (substr($file, -4) == '.jpg') {
                 $ret['mimeType'] = 'image/jpeg';
             } else {
                 if (substr($file, -4) == '.mp4') {
                     $ret['mimeType'] = 'video/mp4';
                 } else {
                     if (substr($file, -5) == '.webm') {
                         $ret['mimeType'] = 'video/webm';
                     } else {
                         if (substr($file, -4) == '.css' || substr($file, -5) == '.scss') {
                             $ret['mimeType'] = 'text/css; charset=utf-8';
                             if (!Kwf_Assets_Dispatcher::allowSourceAccess()) {
                                 throw new Kwf_Exception_AccessDenied();
                             }
                         } else {
                             if (substr($file, -3) == '.js') {
                                 $ret['mimeType'] = 'text/javascript; charset=utf-8';
                                 if (!Kwf_Assets_Dispatcher::allowSourceAccess()) {
                                     throw new Kwf_Exception_AccessDenied();
                                 }
                             } else {
                                 if (substr($file, -4) == '.swf') {
                                     $ret['mimeType'] = 'application/flash';
                                 } else {
                                     if (substr($file, -4) == '.ico') {
                                         $ret['mimeType'] = 'image/x-icon';
                                     } else {
                                         if (substr($file, -5) == '.html') {
                                             $ret['mimeType'] = 'text/html; charset=utf-8';
                                         } else {
                                             if (substr($file, -4) == '.otf') {
                                                 // für Schriften
                                                 $ret['mimeType'] = 'application/octet-stream';
                                             } else {
                                                 if (substr($file, -4) == '.eot') {
                                                     // für Schriften
                                                     $ret['mimeType'] = 'application/vnd.ms-fontobject';
                                                 } else {
                                                     if (substr($file, -4) == '.svg') {
                                                         // für Schriften
                                                         $ret['mimeType'] = 'image/svg+xml';
                                                     } else {
                                                         if (substr($file, -4) == '.ttf') {
                                                             // für Schriften
                                                             $ret['mimeType'] = 'application/octet-stream';
                                                         } else {
                                                             if (substr($file, -5) == '.woff') {
                                                                 // für Schriften
                                                                 $ret['mimeType'] = 'application/font-woff';
                                                             } else {
                                                                 if (substr($file, -6) == '.woff2') {
                                                                     // für Schriften
                                                                     $ret['mimeType'] = 'application/font-woff2';
                                                                 } else {
                                                                     if (substr($file, -4) == '.htc') {
                                                                         // für ie css3
                                                                         $ret['mimeType'] = 'text/x-component';
                                                                     } else {
                                                                         if (substr($file, -4) == '.pdf') {
                                                                             $ret['mimeType'] = 'application/pdf';
                                                                         } else {
                                                                             throw new Kwf_Assets_NotFoundException("Invalid filetype ({$file})");
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (substr($ret['mimeType'], 0, 5) == 'text/') {
         $ret['mtime'] = time();
         $file = new Kwf_Assets_Dependency_File($file);
         if (!$file->getAbsoluteFileName() || !file_exists($file->getAbsoluteFileName())) {
             throw new Kwf_Exception_NotFound();
         }
         $ret['contents'] = $file->getContents(null);
     } else {
         $fx = substr($file, 0, strpos($file, '/'));
         if (substr($fx, 0, 3) == 'fx_') {
             $cache = Kwf_Assets_Cache::getInstance();
             $cacheId = 'fileContents' . str_replace(array('/', '.', '-', ':'), array('_', '_', '_', '_'), $file);
             if (!($cacheData = $cache->load($cacheId))) {
                 if (substr($ret['mimeType'], 0, 6) != 'image/') {
                     throw new Kwf_Exception("Fx is only possible for images");
                 }
                 $im = new Imagick();
                 if (substr($file, -4) == '.ico') {
                     $im->setFormat('ico');
                 }
                 //required because imagick can't autodetect ico format
                 $file = new Kwf_Assets_Dependency_File(substr($file, strpos($file, '/') + 1));
                 $im->readImage($file->getAbsoluteFileName());
                 $fx = explode('_', substr($fx, 3));
                 foreach ($fx as $i) {
                     $params = array();
                     if (($pos = strpos($i, '-')) !== false) {
                         $params = explode('-', substr($i, $pos + 1));
                         $i = substr($i, 0, $pos);
                     }
                     call_user_func(array('Kwf_Assets_Effects', $i), $im, $params);
                 }
                 $cacheData['mtime'] = $file->getMTime();
                 $cacheData['contents'] = $im->getImagesBlob();
                 $im->destroy();
                 $cache->save($cacheData, $cacheId);
             }
             $ret['contents'] = $cacheData['contents'];
             $ret['mtime'] = time();
         } else {
             $ret['mtime'] = time();
             $file = new Kwf_Assets_Dependency_File($file);
             if (!file_exists($file->getAbsoluteFileName())) {
                 throw new Kwf_Exception_NotFound();
             }
             $ret['contents'] = $file->getContents(null);
         }
     }
     return $ret;
 }
 public function getFilename()
 {
     $d = $this->_getIconAndType();
     $file = new Kwf_Assets_Dependency_File(Kwf_Assets_ProviderList_Default::getInstance(), $d['type'] . '/' . $d['icon']);
     return $file->getAbsoluteFileName();
 }
 public function getFilename()
 {
     $d = $this->_getIconAndType();
     $file = new Kwf_Assets_Dependency_File($d['type'] . '/' . $d['icon']);
     return $file->getAbsoluteFileName();
 }