public function preprocessUtil($content, $file) { if (substr($file, -5) == '.scss') { $config = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions(); $cp = defined('CACHE_DIR') ? CACHE_DIR : 'pc'; $host = @$config['util']['host']; $path = PUBLIC_PATH . '/img'; $d = array(basename($file) => md5(file_get_contents($file))); if (!function_exists('recursive_scss')) { function recursive_scss($path, $dir, &$d) { $cur = $path . ($dir ? '/' . $dir : ''); foreach (scandir($cur) as $fn) { if ($fn == '.' || $fn == '..') { continue; } if (is_dir($cur . '/' . $fn) && !in_array($fn, array('font'))) { recursive_scss($path, $dir . ($dir ? '/' : '') . $fn, $d); } else { if (preg_match('/\\.(' . ($dir == 'sprites' ? 'png|' : '') . 'scss)/i', $fn)) { $d[$dir . ($dir ? '/' : '') . $fn] = md5(file_get_contents($cur . '/' . $fn)); } } } } } recursive_scss($path, '', $d); if ($d) { $res = file_get_contents($host . '/x/scss/ch/get/host/' . $_SERVER['HTTP_HOST'] . '/file/' . basename($file)); if ($res) { if (!class_exists('Zip')) { require 'Zkernel/Other/Lib/ekernel/lib/Zip.php'; } $zip = new Zip(); $res = json_decode($res, true); $cnt = 0; foreach ($d as $k => $v) { if ($v != @$res[$k]) { $zip->addFile(file_get_contents($path . '/' . $k), $k); $cnt++; } } if ($cnt) { $data = urlencode($zip->getZipData()); $context = stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-Type: multipart/form-data' . "\r\n" . 'Content-Length: ' . strlen($data) . "\r\n", 'content' => $data))); $res = file_get_contents($host . '/x/scss/ch/set/host/' . $_SERVER['HTTP_HOST'] . '/file/' . basename($file), false, $context); if ($res) { $res = json_decode($res, true); file_put_contents(PUBLIC_PATH . '/' . $cp . '/css/temp.zip', urldecode($res['data'])); require 'Zkernel/Other/Lib/ekernel/lib/Unzip.php'; $zip = new Unzip(); $zip->extract(PUBLIC_PATH . '/' . $cp . '/css/temp.zip', PUBLIC_PATH . '/' . $cp . '/css'); unlink(PUBLIC_PATH . '/' . $cp . '/css/temp.zip'); $nfn = str_replace('.scss', '.css', basename($file)); $content = @file_get_contents(PUBLIC_PATH . '/' . $cp . '/css/' . $nfn); unlink(PUBLIC_PATH . '/' . $cp . '/css/' . $nfn); } } } } } return $content; }
$zip = new Zip(); $zip->setZipFile("ZipExample.zip"); $zip->setComment("Example Zip file.\nCreated on " . date('l jS \\of F Y h:i:s A')); $zip->addFile("Hello World!", "hello.txt"); @($handle = opendir($fileDir)); if ($handle) { /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) { if (strpos($file, ".html") !== false) { $pathData = pathinfo($fileDir . $file); $fileName = $pathData['filename']; $zip->addFile(file_get_contents($fileDir . $file), $file, filectime($fileDir . $file)); } } } $zip->finalize(); // as we are not using getZipData or getZipFile, we need to call finalize ourselves. $zip->setZipFile("ZipExample2.zip"); ?> <html> <head> <title>Zip Test</title> </head> <body> <h1>Zip Test</h1> <p>Zip files saved, length is <?php echo strlen($zip->getZipData()); ?> bytes.</p> </body> </html>
public function preprocess_util($content, $file) { if (substr($file, -5) == '.scss') { $config = application::get_instance()->config->util; $path = PATH_ROOT . '/img'; $d = array(basename($file) => md5(file_get_contents($file))); if (!function_exists('recursive_scss')) { function recursive_scss($path, $dir, &$d) { $cur = $path . ($dir ? '/' . $dir : ''); foreach (scandir($cur) as $fn) { if ($fn == '.' || $fn == '..') { continue; } if (is_dir($cur . '/' . $fn) && !in_array($fn, array('font'))) { recursive_scss($path, $dir . ($dir ? '/' : '') . $fn, $d); } else { if (preg_match('/\\.(' . ($dir == 'sprites' ? 'png|' : '') . 'scss)/i', $fn)) { $d[$dir . ($dir ? '/' : '') . $fn] = md5(file_get_contents($cur . '/' . $fn)); } } } } } recursive_scss($path, '', $d); if ($d) { $res = file_get_contents($config->host . '/x/scss/ch/get/host/' . $_SERVER['HTTP_HOST'] . '/ip/' . $_SERVER['REMOTE_ADDR'] . '/file/' . basename($file)); if ($res) { if (!class_exists('Zip')) { require PATH_ROOT . '/' . DIR_LIBRARY . '/lib/Zip.php'; } $zip = new Zip(); $res = json_decode($res, true); foreach ($d as $k => $v) { if ($v != @$res[$k]) { $zip->addFile(file_get_contents($path . '/' . $k), $k); } } $data = urlencode($zip->getZipData()); $context = stream_context_create(array('http' => array('method' => 'POST', 'header' => 'Content-Type: multipart/form-data' . "\r\n" . 'Content-Length: ' . strlen($data) . "\r\n", 'content' => $data))); $res = file_get_contents($config->host . '/x/scss/ch/set/host/' . $_SERVER['HTTP_HOST'] . '/ip/' . $_SERVER['REMOTE_ADDR'] . '/file/' . basename($file), false, $context); if ($res) { $res = json_decode($res, true); file_put_contents(PATH_ROOT . '/' . DIR_CACHE . '/css/temp.zip', urldecode($res['data'])); require PATH_ROOT . '/' . DIR_LIBRARY . '/lib/Unzip.php'; $zip = new Unzip(); $zip->extract(PATH_ROOT . '/' . DIR_CACHE . '/css/temp.zip', PATH_ROOT . '/' . DIR_CACHE . '/css'); unlink(PATH_ROOT . '/' . DIR_CACHE . '/css/temp.zip'); $nfn = str_replace('.scss', '.css', basename($file)); $content = @file_get_contents(PATH_ROOT . '/' . DIR_CACHE . '/css/' . $nfn); unlink(PATH_ROOT . '/' . DIR_CACHE . '/css/' . $nfn); } } } } return $content; }
public function preprocess_scss($content, $file, $options = array()) { $opt = $this->_scss; if ($options && !is_array($options)) { $options = array(); } if ($options) { $this->_check_options($options); $opt = array_merge($opt, $options); } // process scss file content $res = $this->_preprocess_scss_content($content, $file); // find all images from images_dir $images = array(); $images_dir = trim(dirname($file) . '/' . rtrim($opt['images_dir'], './'), '/'); $this->_preprocess_scss_image($images_dir, '', $images); // zip scss file and all images $zip = new Zip(); $zip->addFile($res, 'sass/style.scss'); if ($images) { foreach ($images as $el) { $zip->addFile($this->_read_file(ltrim($images_dir . '/', '/') . $el), 'images/' . $el); } } // preparing options for remote call $opt['file'] = basename($file); $opt['images_dir'] = '__cache_dir__'; // cleaning gen images cache $this->_cache_purge('images/' . $opt['file']); // call api $result = $this->scss($zip->getZipData(), $opt); $content = $result['content']; // unzip images and replace their names in content if ($result['images']) { $dir = $this->_path_root . '/' . $this->_cache_dir . '/images/' . $opt['file']; $this->_cache_save('images/' . $opt['file'], 'temp', $result['images'], 'zip'); try { $zip = new Unzip(); $zip->extract($dir . '/temp.zip', $dir); $zip->close(); unlink($dir . '/temp.zip'); } catch (\Exception $ex) { } $content = str_replace('__cache_dir__', '/' . $this->_cache_dir . '/images/' . $opt['file'], $content); } return $content; }