public function cron() { $this->load->model('tool/nitro'); $this->model_tool_nitro->loadCore(); if (!$this->model_tool_nitro->from_cron_url()) { return; } if (!getNitroPersistence('CRON.Remote.Delete')) { return; } $tasks = array(); $now = time(); if (getNitroPersistence('CRON.Remote.Delete')) { $period = getNitroPersistence('PageCache.ExpireTime'); $period = !empty($period) ? $period : NITRO_PAGECACHE_TIME; $tasks[] = '- Delete files older than ' . date('Y-m-d H:i:s', $now - $period); cleanNitroCacheFolders('index.html', $period); } if (getNitroPersistence('CRON.Remote.SendEmail')) { $subject = 'NitroPack Remote CRON job'; $message = 'Time of execution: ' . date('Y-m-d H:i:s', $now) . PHP_EOL . PHP_EOL; $message .= 'Executed tasks: ' . PHP_EOL . implode(PHP_EOL, $tasks) . PHP_EOL . PHP_EOL; sendNitroMail(getOpenCartSetting('config_email'), $subject, $message); } }
private function rackspace_upload_file($data) { $response =& $this->request->post['last']; $rackspace =& $data['rackspace']; $containers = array('js' => NULL, 'image' => NULL, 'css' => NULL); $mimeTypes = $this->generateUpToDateMimeArray('http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types'); $source = $data['file']['realpath']; $destination = $data['file']['file']; $ext = strtolower(pathinfo($source, PATHINFO_EXTENSION)); $container = null; if (getNitroPersistence('CDNRackspace.SyncCSS') && in_array($ext, unserialize(NITRO_EXTENSIONS_CSS))) { $container = $rackspace->Container(getNitroPersistence('CDNRackspace.CSSContainer')); } if (getNitroPersistence('CDNRackspace.SyncJavaScript') && in_array($ext, unserialize(NITRO_EXTENSIONS_JS))) { $container = $rackspace->Container(getNitroPersistence('CDNRackspace.JavaScriptContainer')); } if (getNitroPersistence('CDNRackspace.SyncImages') && in_array($ext, unserialize(NITRO_EXTENSIONS_IMG))) { $container = $rackspace->Container(getNitroPersistence('CDNRackspace.ImagesContainer')); } if (empty($container)) { return; } $obj = $container->DataObject(); $upload_response = $obj->Create(array('name' => $destination, 'content_type' => $mimeTypes[$ext]), $source); if ($upload_response->errno() != 0) { throw new Exception('Could not upload ' . $destination); } }
function nitro_minify_css($styles) { if (defined('HTTP_CATALOG') || getNitroPersistence('Mini.CSSExtract')) { return $styles; } else { require_once DIR_SYSTEM . 'nitro/core/minify_functions.php'; return optimizeCSS($styles); } }
public function query($sql) { require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/config.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/core.php'); $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['DBCache']['Enabled']) && $nitroPersistence['Nitro']['DBCache']['Enabled'] == 'yes') { $nitro_matches = array(); $nitro_match = false; // Product COUNT Queries if (!empty($nitroPersistence['Nitro']['DBCache']['ProductCountQueries']) && $nitroPersistence['Nitro']['DBCache']['ProductCountQueries'] == 'yes') { $nitro_match = preg_match('~SELECT.*COUNT\\(.*FROM.*[^0-9a-zA-Z_]' . DB_PREFIX . '(product)([\\s]|$)~i', $sql, $nitro_matches); } // Category COUNT Queries if (!$nitro_match && !empty($nitroPersistence['Nitro']['DBCache']['CategoryCountQueries']) && $nitroPersistence['Nitro']['DBCache']['CategoryCountQueries'] == 'yes') { $nitro_match = preg_match('~SELECT.*COUNT\\(.*FROM.*[^0-9a-zA-Z_]' . DB_PREFIX . '(category)([\\s]|$)~i', $sql, $nitro_matches); } // Category Queries if (!$nitro_match && !empty($nitroPersistence['Nitro']['DBCache']['CategoryQueries']) && $nitroPersistence['Nitro']['DBCache']['CategoryQueries'] == 'yes') { $nitro_match = preg_match('~SELECT.*FROM.*[^0-9a-zA-Z_]' . DB_PREFIX . '(category)([\\s]|$)~i', $sql, $nitro_matches); } // SEO URLs Queries if (!$nitro_match && !empty($nitroPersistence['Nitro']['DBCache']['SeoUrls']) && $nitroPersistence['Nitro']['DBCache']['SeoUrls'] == 'yes') { $nitro_match = preg_match('~SELECT.*FROM.*[^0-9a-zA-Z_]' . DB_PREFIX . '(url_alias)([\\s]|$)~i', $sql, $nitro_matches); } // Search Queries if (!$nitro_match && !empty($nitroPersistence['Nitro']['DBCache']['Search']) && $nitroPersistence['Nitro']['DBCache']['Search'] == 'yes') { $nitro_match = preg_match('~SELECT.*WHERE.*(LIKE|MATCH)~i', $sql, $nitro_matches); if ($nitro_match) { $nitro_match = false; if (!empty($nitroPersistence['Nitro']['DBCache']['SearchKeywords'])) { $nitro_keywords = explode(",", $nitroPersistence['Nitro']['DBCache']['SearchKeywords']); foreach ($nitro_keywords as $nitro_keyword) { if (stripos(trim($nitro_keyword), $sql) !== FALSE) { $nitro_match = true; break; } } } } } if ($nitro_match) { require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/dbcache.php'); $nitro_cache_selector = strtolower($nitro_matches[1]) . '.' . md5($sql); // category.6ef5cee93ce985fe9de730a1d837455d $nitro_result = getNitroDBCache($nitro_cache_selector); if ($nitro_result !== FALSE) { return $nitro_result; } } } if (!empty($nitro_cache_selector)) { $nitro_db_result = $this->driver->query($sql); setNitroDBCache($nitro_cache_selector, $nitro_db_result); return $nitro_db_result; } return $this->driver->query($sql); }
public function set($key, $value) { $this->delete($key); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/config.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/core.php'); $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && (empty($nitroPersistence['Nitro']['OpenCartCache']['Enabled']) || $nitroPersistence['Nitro']['OpenCartCache']['Enabled'] == 'no')) { return; } $nitro_expire = !empty($nitroPersistence['Nitro']['OpenCartCache']['ExpireTime']) ? (int) $nitroPersistence['Nitro']['OpenCartCache']['ExpireTime'] : 0; $this->expire = $nitro_expire; $file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\\._-]/i', '', $key) . '.' . (time() + $this->expire); $handle = fopen($file, 'w'); fwrite($handle, serialize($value)); fclose($handle); }
public function save($file, $quality = 90) { require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/config.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/core.php'); $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['ImageCache']['OverrideCompression']) && $nitroPersistence['Nitro']['ImageCache']['OverrideCompression'] == 'yes') { $nitro_quality = !empty($nitroPersistence['Nitro']['ImageCache']['JPEGCompression']) ? (int) $nitroPersistence['Nitro']['ImageCache']['JPEGCompression'] : $quality; $quality = $nitro_quality < 0 ? 0 : ($nitro_quality > 100 ? 100 : $nitro_quality); } $info = pathinfo($file); $extension = strtolower($info['extension']); if (is_resource($this->image)) { if ($extension == 'jpeg' || $extension == 'jpg') { imagejpeg($this->image, $file, $quality); } elseif ($extension == 'png') { imagepng($this->image, $file); } elseif ($extension == 'gif') { imagegif($this->image, $file); } imagedestroy($this->image); } }
public function output() { if ($this->output) { if ($this->level) { $ouput = $this->compress($this->output, $this->level); } else { $ouput = $this->output; } if (!headers_sent()) { foreach ($this->headers as $header) { header($header, true); } } echo $ouput; require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/config.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/core.php'); $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && $nitroPersistence['Nitro']['PageCache']['Enabled'] == 'yes') { require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/bottom.php'); } } }
function setNitroDBCache($cache, $data, $expire = 3600) { $data = serialize($data); $file = NITRO_DBCACHE_FOLDER . $cache . '.nitro'; $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['DBCache']['CacheDepo'])) { if ($nitroPersistence['Nitro']['DBCache']['CacheDepo'] == 'hdd') { if (!is_dir(NITRO_DBCACHE_FOLDER)) { if (!mkdir(NITRO_DBCACHE_FOLDER)) { return false; } } if (is_writeable(NITRO_DBCACHE_FOLDER)) { if (file_put_contents($file, $data)) { return true; } } } else { setRamCache($file, $data, $nitroPersistence['Nitro']['DBCache']['CacheDepo']); } } return false; }
if (isset($_SERVER['PATH_TRANSLATED'])) { $_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF']))); } } if (!isset($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1); if (isset($_SERVER['QUERY_STRING'])) { $_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; } } if (!isset($_SERVER['HTTP_HOST'])) { $_SERVER['HTTP_HOST'] = getenv('HTTP_HOST'); } require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/config.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/core.php'); $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && $nitroPersistence['Nitro']['PageCache']['Enabled'] == 'yes') { require_once VQMod::modCheck(DIR_SYSTEM . 'nitro/core/top.php'); } // Helper require_once VQMod::modCheck(DIR_SYSTEM . 'helper/json.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'helper/utf8.php'); // Engine require_once VQMod::modCheck(DIR_SYSTEM . 'engine/action.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'engine/controller.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'engine/front.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'engine/loader.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'engine/model.php'); require_once VQMod::modCheck(DIR_SYSTEM . 'engine/registry.php'); // Common require_once VQMod::modCheck(DIR_SYSTEM . 'library/cache.php');
if (!empty($data)) { $urls = json_decode($data); $total = count($urls); $currency = strtoupper(getOpenCartSetting('config_currency')); $language = getOpenCartSetting('config_language'); foreach ($urls as $target) { $ch = curl_init($target); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_USERAGENT, 'Nitro-Precache'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $headers = array('Nitro-Precache: 1', 'Cache-Control: no-cache', 'Pragma: no-cache', 'Connection: keep-alive'); $cookie = 'currency=' . $currency . '; language=' . $language . ';'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_COOKIE, $cookie); curl_exec($ch); curl_close($ch); } $precache_progress .= ' Precached ' . $total . ' pages'; } else { $precache_progress .= ' Pre-cache data not found. URL: ' . $url . PHP_EOL; } $tasks[] = $precache_progress; } if (getNitroPersistence('CRON.Local.SendEmail')) { $subject = 'NitroPack Local CRON job'; $message = 'Time of execution: ' . date('Y-m-d H:i:s', $now) . PHP_EOL . PHP_EOL; $message .= 'Executed tasks: ' . PHP_EOL . implode(PHP_EOL, $tasks) . PHP_EOL . PHP_EOL; sendNitroMail(getOpenCartSetting('config_email'), $subject, $message); }
public function clearProductCache($product_id) { $this->loadCore(); if (!getNitroPersistence('PageCache.ClearCacheOnProductEdit')) { return; } initNitroProductCacheDb(); $cachefile_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "nitro_product_cache WHERE product_id='" . $product_id . "'"); foreach ($cachefile_query->rows as $cachefile) { $file = $cachefile['cachefile']; if (file_exists($file) && is_writable($file)) { unlink($file); } $filegz = $file . '.gz'; if (file_exists($filegz) && is_writable($filegz)) { unlink($filegz); } } $this->db->query("DELETE FROM " . DB_PREFIX . "nitro_product_cache WHERE product_id='" . $product_id . "'"); $this->clearDBCache(); }
function minifyHTML($html) { require_once NITRO_FOLDER . 'lib' . DS . 'minifier' . DS . 'HTMLMin.php'; $htmlMinifier = new Nitro_Minify_HTML($html, array('jsCleanComments' => false, 'keepHTMLComments' => getNitroPersistence('Mini.HTMLComments'))); $html = $htmlMinifier->process(); return $html; }
function combine($type, $files, $excludes) { if (!in_array($type, array('css', 'js'))) { return $files; } //extract local fylesystem path clean_file_paths($files, $excludes); global $registry; $oc_root = dirname(DIR_APPLICATION); $cache = NULL; $cachefile = NULL; $filename = NULL; if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } $cachefile = $oc_root . DS . 'assets' . DS . $type . DS . getSSLCachePrefix() . $type . '-combined.cache'; if (!file_exists($cachefile)) { touch($cachefile); file_put_contents($cachefile, json_encode(array())); } $cache = json_decode(file_get_contents($cachefile), true); $comboHash = ''; $excludedFiles = array(); $includedFiles = 0; foreach ($files as $hash => $file) { if (!is_excluded_file($file, $excludes)) { $comboHash .= $hash; $includedFiles++; } else { $excludedFiles[$hash] = $file; } } $comboHash = md5($comboHash); $target = '/assets/' . $type . '/' . getSSLCachePrefix() . 'nitro-combined-' . $comboHash . '.' . $type; $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS); $recache = false; foreach ($files as $hash => $file) { if (!is_excluded_file($file, $excludes)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS); if (!empty($cache[$comboHash][$filename])) { if (!is_url($file) && $cache[$comboHash][$filename] != filemtime($filename)) { $recache = true; break; } } else { $recache = true; break; } } else { continue; } } $combinedContent = ''; if ($recache || !file_exists($targetAbsolutePath)) { if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == '1')) { $webshopUrl = $registry->get('config')->get('config_ssl'); } else { $webshopUrl = $registry->get('config')->get('config_url'); } $webshopUrl = rtrim(preg_replace('~^https?\\:~i', '', $webshopUrl), '/'); $counter = 0; foreach ($files as $hash => $file) { if (!is_excluded_file($file, $excludes)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $file), DS); $content = is_url($file) ? fetchRemoteContent($file) : file_get_contents($filename); $urlToCurrentDir = is_url($file) ? dirname($file) : $webshopUrl . str_replace($oc_root, '', dirname('/' . trim($filename, '/'))); if (!empty($content)) { if ($type == 'js' && substr($content, -1) == ')') { $content .= ';'; } if ($type == 'css') { $content = preg_replace('/(url\\()(?![\'\\"]?(?:(?:https?\\:\\/\\/)|(?:data\\:)|(?:\\/)))([\'\\"]?)\\/?(?!\\/)/', '$1$2' . $urlToCurrentDir . '/', $content); } $combinedContent .= ($counter > 0 ? PHP_EOL : '') . $content; unset($content); $counter++; } $cache[$comboHash][$filename] = is_url($file) ? 0 : filemtime($filename); } } if ($type == 'css') { /* pull imports to the top and include their content if possible */ $imports = array(); preg_match_all('/\\@import[^\\;]*\\;/', $combinedContent, $imports); if (!empty($imports)) { $imports = array_reverse($imports[0]); foreach ($imports as $import) { $importUrl = preg_replace('/[^\'"\\(]*(?:[\'"\\(\\s]?)*(.*?)(?:[\'"\\)]).*/', '$1', $import); if (getNitroPersistence('Mini.CSSFetchImport')) { $tmpImportContent = fetchRemoteContent($importUrl); } if (!empty($tmpImportContent)) { $combinedContent = $tmpImportContent . str_replace($import, '', $combinedContent); } else { $combinedContent = $import . str_replace($import, '', $combinedContent); } } } } file_put_contents($targetAbsolutePath, $combinedContent); } file_put_contents($cachefile, json_encode($cache)); if ($includedFiles > 0) { return array_merge($excludedFiles, array(md5($target) => trim($target, '/'))); } else { return $excludedFiles; } }
protected function index() { $this->data['title'] = $this->document->getTitle(); if (isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $server = $this->config->get('config_ssl'); } else { $server = $this->config->get('config_url'); } $this->data['base'] = $server; $this->data['description'] = $this->document->getDescription(); $this->data['keywords'] = $this->document->getKeywords(); $this->data['links'] = $this->document->getLinks(); $this->data['styles'] = $this->document->getStyles(); require_once DIR_SYSTEM . 'nitro/config.php'; require_once DIR_SYSTEM . 'nitro/core/core.php'; $nitroPersistence = getNitroPersistence(); $nitro_has_ftp_to_cdn = !empty($nitroPersistence['Nitro']['CDNStandardFTP']['Enabled']) && $nitroPersistence['Nitro']['CDNStandardFTP']['Enabled'] == 'yes'; if ($nitro_has_ftp_to_cdn) { $nitro_ftp_persistence = getFTPPersistence(); } else { $nitro_ftp_persistence = array(); } $nitro_amazon_persistence = getAmazonPersistence(); $nitro_rackspace_persistence = getRackspacePersistence(); $nitro_has_css_minification = !empty($nitroPersistence['Nitro']['Mini']['Enabled']) && $nitroPersistence['Nitro']['Mini']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['Mini']['CSS']) && $nitroPersistence['Nitro']['Mini']['CSS'] == 'yes'; $nitro_amazon_condition = !empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNAmazon']['Enabled']) && $nitroPersistence['Nitro']['CDNAmazon']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNAmazon']['ServeCSS']) && $nitroPersistence['Nitro']['CDNAmazon']['ServeCSS'] == 'yes'; $nitro_rackspace_condition = !empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNRackspace']['Enabled']) && $nitroPersistence['Nitro']['CDNRackspace']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNRackspace']['ServeCSS']) && $nitroPersistence['Nitro']['CDNRackspace']['ServeCSS'] == 'yes'; $nitro_css_minify_condition = $nitro_has_css_minification && $nitro_has_ftp_to_cdn || !$nitro_has_css_minification; if (($nitro_css_minify_condition || $nitro_amazon_condition || $nitro_rackspace_condition) && !(function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl())) { foreach ($this->data['styles'] as $nitro_index => $nitro_value) { $nitro_relative = stripos($nitro_value['href'], 'http://') === 0 ? false : (stripos($nitro_value['href'], 'https://') === 0 ? false : true); if ($nitro_css_minify_condition && $nitro_relative && !empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNStandard']['Enabled']) && $nitroPersistence['Nitro']['CDNStandard']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNStandard']['ServeCSS']) && $nitroPersistence['Nitro']['CDNStandard']['ServeCSS'] == 'yes') { $nitro_has_ftp_persistence = false; if ($nitro_has_ftp_to_cdn) { $nitro_check_ftp_value = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_has_ftp_persistence = in_array($nitro_check_ftp_value, $nitro_ftp_persistence); } else { $nitro_has_ftp_persistence = true; } if ($nitro_has_ftp_persistence && !empty($nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl'], strlen($nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl'] : $nitroPersistence['Nitro']['CDNStandard']['CSSHttpsUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } else { if ($nitro_has_ftp_persistence && !empty($nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl'])) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl'], strlen($nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl'] : $nitroPersistence['Nitro']['CDNStandard']['CSSHttpUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } } } else { if ($nitro_amazon_condition && $nitro_relative) { $nitro_check_amazon_value = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_has_amazon_persistence = in_array($nitro_check_amazon_value, $nitro_amazon_persistence); if ($nitro_has_amazon_persistence && !empty($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'], strlen($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'] : $nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } else { if ($nitro_has_amazon_persistence && !empty($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'])) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'], strlen($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'] : $nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } } } else { if ($nitro_rackspace_condition && $nitro_relative) { $nitro_check_rackspace_value = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_has_rackspace_persistence = in_array($nitro_check_rackspace_value, $nitro_rackspace_persistence); if ($nitro_has_rackspace_persistence && !empty($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl'], strlen($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl'] : $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpsUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } else { if ($nitro_has_rackspace_persistence && !empty($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl'])) { $nitro_href = substr($nitro_value['href'], 0, 1) == '/' ? substr($nitro_value['href'], 1) : $nitro_value['href']; $nitro_url = substr($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl'], strlen($nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl'] : $nitroPersistence['Nitro']['CDNRackspace']['CSSHttpUrl'] . '/'; $this->data['styles'][$nitro_index]['href'] = $nitro_url . $nitro_href; } } } } } } } $this->data['scripts'] = $this->document->getScripts(); $nitro_has_js_minification = !empty($nitroPersistence['Nitro']['Mini']['Enabled']) && $nitroPersistence['Nitro']['Mini']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['Mini']['JS']) && $nitroPersistence['Nitro']['Mini']['JS'] == 'yes'; $nitro_js_minify_condition = $nitro_has_js_minification && $nitro_has_ftp_to_cdn || !$nitro_has_js_minification; $nitro_rackspace_condition = !empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNRackspace']['Enabled']) && $nitroPersistence['Nitro']['CDNRackspace']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNRackspace']['ServeJavaScript']) && $nitroPersistence['Nitro']['CDNRackspace']['ServeJavaScript'] == 'yes'; if (($nitro_js_minify_condition || $nitro_amazon_condition || $nitro_rackspace_condition) && !(function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl())) { foreach ($this->data['scripts'] as $nitro_index => $nitro_value) { $nitro_relative = stripos($nitro_value, 'http://') === 0 ? false : (stripos($nitro_value, 'https://') === 0 ? false : true); if ($nitro_js_minify_condition && $nitro_relative && !empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNStandard']['Enabled']) && $nitroPersistence['Nitro']['CDNStandard']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['CDNStandard']['ServeJavaScript']) && $nitroPersistence['Nitro']['CDNStandard']['ServeJavaScript'] == 'yes') { $nitro_has_ftp_persistence = false; if ($nitro_has_ftp_to_cdn) { $nitro_check_ftp_value = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_has_ftp_persistence = in_array($nitro_check_ftp_value, $nitro_ftp_persistence); } else { $nitro_has_ftp_persistence = true; } if ($nitro_has_ftp_persistence && !empty($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl'], strlen($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl'] : $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpsUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } else { if ($nitro_has_ftp_persistence && !empty($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl'])) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl'], strlen($nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl'] : $nitroPersistence['Nitro']['CDNStandard']['JavaScriptHttpUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } } } else { if ($nitro_amazon_condition && $nitro_relative) { $nitro_check_amazon_value = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_has_amazon_persistence = in_array($nitro_check_amazon_value, $nitro_amazon_persistence); if ($nitro_has_amazon_persistence && !empty($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'], strlen($nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'] : $nitroPersistence['Nitro']['CDNAmazon']['HttpsUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } else { if ($nitro_has_amazon_persistence && !empty($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'])) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'], strlen($nitroPersistence['Nitro']['CDNAmazon']['HttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'] : $nitroPersistence['Nitro']['CDNAmazon']['HttpUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } } } else { if ($nitro_rackspace_condition && $nitro_relative) { $nitro_check_rackspace_value = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_has_rackspace_persistence = in_array($nitro_check_rackspace_value, $nitro_rackspace_persistence); if ($nitro_has_rackspace_persistence && !empty($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl']) && isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl'], strlen($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl'] : $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpsUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } else { if ($nitro_has_rackspace_persistence && !empty($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl'])) { $nitro_href = substr($nitro_value, 0, 1) == '/' ? substr($nitro_value, 1) : $nitro_value; $nitro_url = substr($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl'], strlen($nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl']) - 1, 1) == '/' ? $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl'] : $nitroPersistence['Nitro']['CDNRackspace']['JavaScriptHttpUrl'] . '/'; $this->data['scripts'][$nitro_index] = $nitro_url . $nitro_href; } } } } } } } $this->data['lang'] = $this->language->get('code'); $this->data['direction'] = $this->language->get('direction'); $this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8'); $this->data['name'] = $this->config->get('config_name'); if ($this->config->get('config_icon') && file_exists(DIR_IMAGE . $this->config->get('config_icon'))) { $this->data['icon'] = $server . 'image/' . $this->config->get('config_icon'); } else { $this->data['icon'] = ''; } if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) { $this->data['logo'] = $server . 'image/' . $this->config->get('config_logo'); } else { $this->data['logo'] = ''; } $this->language->load('common/header'); $this->data['og_url'] = (isset($this->request->server['HTTPS']) ? HTTPS_SERVER : HTTP_SERVER) . substr($this->request->server['REQUEST_URI'], 1, strlen($this->request->server['REQUEST_URI']) - 1); $this->data['og_image'] = $this->document->getOgImage(); $this->data['text_home'] = $this->language->get('text_home'); $this->data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0); $this->data['text_shopping_cart'] = $this->language->get('text_shopping_cart'); $this->data['text_search'] = $this->language->get('text_search'); $this->data['text_welcome'] = sprintf($this->language->get('text_welcome'), $this->url->link('account/login', '', 'SSL'), $this->url->link('account/register', '', 'SSL')); $this->data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', 'SSL'), $this->customer->getFirstName(), $this->url->link('account/logout', '', 'SSL')); $this->data['text_account'] = $this->language->get('text_account'); $this->data['text_checkout'] = $this->language->get('text_checkout'); $this->data['text_page'] = $this->language->get('text_page'); $this->data['home'] = $this->url->link('common/home'); $this->data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL'); $this->data['logged'] = $this->customer->isLogged(); $this->data['account'] = $this->url->link('account/account', '', 'SSL'); $this->data['shopping_cart'] = $this->url->link('checkout/cart'); $this->data['checkout'] = $this->url->link('checkout/checkout', '', 'SSL'); // Daniel's robot detector $status = true; if (isset($this->request->server['HTTP_USER_AGENT'])) { $robots = explode("\n", trim($this->config->get('config_robots'))); foreach ($robots as $robot) { if ($robot && strpos($this->request->server['HTTP_USER_AGENT'], trim($robot)) !== false) { $status = false; break; } } } // A dirty hack to try to set a cookie for the multi-store feature $this->load->model('setting/store'); $this->data['stores'] = array(); if ($this->config->get('config_shared') && $status) { $this->data['stores'][] = $server . 'catalog/view/javascript/crossdomain.php?session_id=' . $this->session->getId(); $stores = $this->model_setting_store->getStores(); foreach ($stores as $store) { $this->data['stores'][] = $store['url'] . 'catalog/view/javascript/crossdomain.php?session_id=' . $this->session->getId(); } } // Search if (isset($this->request->get['search'])) { $this->data['search'] = $this->request->get['search']; } else { $this->data['search'] = ''; } // Menu if (isset($this->request->get['path'])) { $parts = explode('_', (string) $this->request->get['path']); } else { $parts = array(); } $this->load->model('catalog/category'); $this->load->model('catalog/product'); $this->data['categories'] = array(); $categories = $this->model_catalog_category->getCategories(0); foreach ($categories as $category) { if ($category['top']) { // Level 2 $children_data = array(); $children = $this->model_catalog_category->getCategories($category['category_id']); foreach ($children as $child) { //Будем вычислять кол-во товаров в категориях только если это кол-во надо показывать if ($this->config->get('config_product_count')) { $data = array('filter_category_id' => $child['category_id'], 'filter_sub_category' => true); //Optmization //$product_total = $this->model_catalog_product->getTotalProducts($data); $product_total = 0; } $children_data[] = array('name' => $child['name'], 'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])); } // Level 1 $this->data['categories'][] = array('name' => $category['name'], 'children' => $children_data, 'active' => in_array($category['category_id'], $parts), 'column' => $category['column'] ? $category['column'] : 1, 'href' => $this->url->link('product/category', 'path=' . $category['category_id'])); } } $this->children = array('module/language', 'module/currency', 'module/cart'); if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/header.tpl')) { $this->template = $this->config->get('config_template') . '/template/common/header.tpl'; } else { $this->template = 'default/template/common/header.tpl'; } $this->render(); }
<?php if (isNitroEnabled() && getNitroPersistence('PageCache.Enabled')) { if (isset($_GET['tracking'])) { setcookie('tracking', $_GET['tracking'], time() + 3600 * 24 * 1000, '/'); } require_once DIR_SYSTEM . 'nitro/core/top.php'; }
function getGooglePageSpeedReport($setting = null) { if (file_exists(NITRO_FOLDER . 'data/googlepagespeed.tpl')) { $data = file_get_contents(NITRO_FOLDER . 'data/googlepagespeed.tpl'); $data = base64_decode($data); //$data = json_decode($data,true); $returnData = $data; } else { // Fetch the report and save it $catalogURL = defined('HTTPS_CATALOG') && HTTPS_CATALOG != '' ? HTTPS_CATALOG : defined('HTTP_CATALOG') && HTTP_CATALOG != '' ? HTTP_CATALOG : defined('HTTPS_SERVER') && HTTPS_SERVER != '' ? HTTPS_SERVER : HTTP_SERVER; $catalogURL = $catalogURL . '#red' . rand(1000, 99999); $persistence = getNitroPersistence(); $key = !empty($persistence['Nitro']['GooglePageSpeedApiKey']) ? $persistence['Nitro']['GooglePageSpeedApiKey'] : 'AIzaSyCxptR6CbHYrHkFfsO_XN3nkf6FjoQp2Mg'; $url = "https://www.googleapis.com/pagespeedonline/v1/runPagespeed?url=" . $catalogURL . "&key=" . $key; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); if (!empty($output)) { $returnData = $output; setGooglePageSpeedReport($returnData); } else { $returnData = false; } } if (!empty($setting)) { if (!empty($returnData['NitroCache'][$setting])) { $returnData = $returnData['NitroCache'][$setting]; } else { $returnData = false; } } return $returnData; }
<?php $connection = new \OpenCloud\Rackspace(RACKSPACE_US, array('username' => getNitroPersistence('CDNRackspace.Username'), 'apiKey' => getNitroPersistence('CDNRackspace.APIKey'))); $objstore = $connection->ObjectStore('cloudFiles', getNitroPersistence('CDNRackspace.ServerRegion'), "publicURL");
<?php if (getNitroPersistence('Enabled') && getNitroPersistence('ImageCache.OverrideCompression')) { $nitro_quality = getNitroPersistence('ImageCache.JPEGCompression'); $nitro_quality = !empty($nitro_quality) ? $nitro_quality : $quality; $quality = $nitro_quality < 0 ? 0 : ($nitro_quality > 100 ? 100 : $nitro_quality); }
private function amazon_upload_file($data) { $response =& $this->request->post['last']; $s3 =& $data['s3']; $buckets = $s3->listBuckets(); $cssjs_browser_cache = 0; $images_browser_cache = 0; if (getNitroPersistence('BrowserCache.Enabled') && getNitroPersistence('BrowserCache.Headers.Pages.Expires')) { if (getNitroPersistence('BrowserCache.CSSJS.Period')) { switch (getNitroPersistence('BrowserCache.CSSJS.Period')) { case '1 week': $cssjs_browser_cache = 7 * 24 * 3600; break; case '1 month': $cssjs_browser_cache = 30 * 24 * 3600; break; case '6 months': $cssjs_browser_cache = 6 * 30 * 24 * 3600; break; case '1 year': $cssjs_browser_cache = 365 * 24 * 3600; break; default: $cssjs_browser_cache = 0; } } if (getNitroPersistence('BrowserCache.Images.Period')) { switch (getNitroPersistence('BrowserCache.Images.Period')) { case '1 week': $images_browser_cache = 7 * 24 * 3600; break; case '1 month': $images_browser_cache = 30 * 24 * 3600; break; case '6 months': $images_browser_cache = 6 * 30 * 24 * 3600; break; case '1 year': $images_browser_cache = 365 * 24 * 3600; break; default: $images_browser_cache = 0; } } } $source = $data['file']['realpath']; $destination = $data['file']['file']; $ext = strtolower(pathinfo($source, PATHINFO_EXTENSION)); if (getNitroPersistence('CDNAmazon.SyncCSS') && in_array($ext, unserialize(NITRO_EXTENSIONS_CSS))) { $bucket = getNitroPersistence('CDNAmazon.CSSBucket'); if (is_array($buckets) && in_array($bucket, $buckets)) { $compress = getNitroPersistence('Compress.CSS') && getNitroPersistence('Compress.CSSLevel') ? (int) getNitroPersistence('Compress.CSSLevel') : false; $cache_time = $cssjs_browser_cache; } else { throw new Exception('The CSS bucket does not exist. Please create it.'); } } if (getNitroPersistence('CDNAmazon.SyncJavaScript') && in_array($ext, unserialize(NITRO_EXTENSIONS_JS))) { $bucket = getNitroPersistence('CDNAmazon.JavaScriptBucket'); if (is_array($buckets) && in_array($bucket, $buckets)) { $compress = getNitroPersistence('Compress.JS') && getNitroPersistence('Compress.JSLevel') ? (int) getNitroPersistence('Compress.JSLevel') : false; $cache_time = $cssjs_browser_cache; } else { throw new Exception('The JS bucket does not exist. Please create it.'); } } if (getNitroPersistence('CDNAmazon.SyncImages') && in_array($ext, unserialize(NITRO_EXTENSIONS_IMG))) { $bucket = getNitroPersistence('CDNAmazon.ImageBucket'); if (is_array($buckets) && in_array($bucket, $buckets)) { $compress = false; $cache_time = $images_browser_cache; } else { throw new Exception('The images bucket does not exist. Please create it.'); } } if (empty($bucket)) { return; } $req = new S3Request('HEAD', $bucket, $destination); $res = $req->getResponse(); $to_upload = $res->code != 200 || $res->code == 200 && ((!empty($compress) xor !empty($res->headers['x-amz-meta-compressed'])) || (!empty($cache_time) xor !empty($res->headers['x-amz-meta-expires-time'])) || !empty($cache_time) && !empty($res->headers['x-amz-meta-expires-time']) && (int) $cache_time != $res->headers['x-amz-meta-expires-time']); if ($to_upload) { $headers = array(); $meta_headers = array(); if (!empty($cache_time)) { $headers['Expires'] = gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $cache_time); $meta_headers['expires-time'] = $cache_time; } switch ($ext) { case 'css': $headers['Content-Type'] = 'text/css'; break; case 'js': $headers['Content-Type'] = 'text/javascript'; break; } if (!empty($compress) && is_readable($source) && !empty($headers['Content-Type'])) { $headers['Content-Encoding'] = 'gzip'; $meta_headers['compressed'] = '1'; $contents = gzencode(file_get_contents($source), $compress); $temp_file = tempnam(sys_get_temp_dir(), 'Nitro'); $temp_handle = fopen($temp_file, "w"); if (empty($temp_handle) || !fwrite($temp_handle, $contents)) { throw new Exception('There was a problem writing to ' . $temp_file); } fclose($temp_handle); $source = $temp_file; } if ($s3->putObject($s3->inputFile($source), $bucket, $destination, S3::ACL_PUBLIC_READ, $meta_headers, $headers)) { if (!empty($temp_file)) { unlink($temp_file); unset($temp_file); } } else { throw new Exception('Could not upload ' . $destination); } } }
public function applyNitroCacheHTCompressionRules($post) { $this->loadConfig(); $this->loadCore(); $pers = getNitroPersistence(); $htaccessFileContent = $this->getHtaccessFileContent(); $old_content = $this->extractNitrocodeCompressFromHtaccessFile($htaccessFileContent); if ((string) $old_content != '') { $newHtaccessFileContent = str_replace($old_content, '', $htaccessFileContent); } else { $newHtaccessFileContent = $htaccessFileContent; } $this->setHtaccessFileContent($newHtaccessFileContent); if (empty($post['Nitro']['Compress']['Enabled']) || $post['Nitro']['Compress']['Enabled'] == 'no') { return false; } $htrules = '# STARTCOMPRESSNITRO' . PHP_EOL; if (!empty($post['Nitro']['Compress']['CSS']) && $post['Nitro']['Compress']['CSS'] == 'yes') { $htrules .= '' . PHP_EOL; $htrules .= 'RewriteCond %{SCRIPT_FILENAME} !-d' . PHP_EOL; $htrules .= 'RewriteRule ^(\\/?((catalog)|(assets)).+)\\.css$ assets/style.php?l=4&p=$1 [NC,L]' . PHP_EOL; } if (!empty($post['Nitro']['Compress']['JS']) && $post['Nitro']['Compress']['JS'] == 'yes') { $htrules .= '' . PHP_EOL; $htrules .= 'RewriteCond %{SCRIPT_FILENAME} !-d' . PHP_EOL; $htrules .= 'RewriteRule ^(\\/?((catalog)|(assets)).+)\\.js$ assets/script.php?l=4&p=$1 [NC,L]' . PHP_EOL; } $htrules .= PHP_EOL . '# ENDCOMPRESSNITRO' . PHP_EOL; $newHtaccessFileContent = $htrules . $this->getHtaccessFileContent(); return $this->setHtaccessFileContent($newHtaccessFileContent); }
function extractHardcodedResources($content) { if (!isNitroEnabled() || !getNitroPersistence('Mini.Enabled')) { return $content; } $settings = getNitroPersistence(); $cssExclude = array(); $jsExclude = array(); $jsLineExclude = array(); $cssExtractCheckPassed = false; $jsExtractCheckPassed = false; require_once DIR_SYSTEM . 'nitro' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'core.php'; require_once DIR_SYSTEM . 'nitro' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'cdn.php'; if (getNitroPersistence('Mini.CSSExtract')) { $cssExtractCheckPassed = true; if (getNitroPersistence('Mini.CSSExclude')) { $cssExclude = trim(getNitroPersistence('Mini.CSSExclude'), "\n\r "); $cssExclude = explode("\n", $cssExclude); foreach ($cssExclude as $k => $stylename) { $stylename = trim($stylename, "\n\r "); if (!empty($stylename)) { $cssExclude[$k] = $stylename; } } } $extractedCSSFiles = array(); $current_pos = 0; $html_end = strlen($content) - 1; while ($html_end !== false && $current_pos < $html_end) { $next_css = strpos($content, 'stylesheet', $current_pos); $tag_start = $next_css; if ($next_css !== false) { //go left to check if we are in a link tag $i = $next_css; $isTagStartFound = false; while ($i > 0 && !$isTagStartFound) { if ($content[$i - 1] == '<') { if (substr($content, $i, 4) != '?php') { $isTagStartFound = true; } else { $i--; } } else { $i--; } } $tag_start = $i - 1; $tag = ''; while ($i < $next_css && !in_array($content[$i], array(' ', "\n", "\r"))) { $tag .= $content[$i]; $i++; } if (strtolower($tag) == 'link') { //see if we are not in a comment block $c = $i; $commentStartFound = false; $commentEndFound = false; while ($c > 0 && !$commentStartFound) { if ($content[$c] == '>') { if (substr($content, $c - 2, 3) == '-->') { $commentEndFound = true; break; } } if ($content[$c] == '<') { if (substr($content, $c, 4) == '<!--') { $commentStartFound = true; } } $c--; } $weAreInComment = $commentStartFound && !$commentEndFound; //find the href while ($i < $html_end) { if ($content[$i] == 'h' && substr($content, $i, 5) == 'href=') { $i += 6; break; } $i++; } $css_src = ''; while ($i < $html_end && $content[$i] != '\'' && $content[$i] != '"') { $css_src .= $content[$i]; $i++; } if (strpos($css_src, '<?php') !== false || nitroIsIgnoredUrl($css_src, $cssExclude) || $weAreInComment) { //skip this css if its location is dynamically generated $current_pos = $next_css + 1; continue; } $extractedCSSFiles[] = $css_src; //cut the css link $i = $tag_start; $tag_end = $tag_start; $isTagEndFound = false; while ($i < $html_end && !$isTagEndFound) { if ($content[$i] == '>' && $content[$i - 1] != '?') { //if we are not in php closing tag $isTagEndFound = true; } $tag_end = $i; $i++; } $content = substr($content, 0, $tag_start) . substr($content, $tag_end + 1); $html_end = strlen($content) - 1; } else { $current_pos = $next_css + 1; continue; } } else { break; } $current_pos = $tag_start + 1; } //minify and combine the newly extracted css resources //and then put them in the header $minCSS = optimizeCSS(generateCSSMinificatorStyles($extractedCSSFiles)); $new_css_include = ''; foreach ($minCSS as $css_file) { $new_css_include .= '<link rel="' . $css_file['rel'] . '" type="text/css" href="' . $css_file['href'] . '" media="' . $css_file['media'] . '" />'; } if (!empty($new_css_include)) { $base_start = strpos($content, '<base'); if ($base_start === false) { $base_start = strpos($content, '<head'); } $i = $base_start; $base_end = 0; while ($i < $html_end && !$base_end) { if ($content[$i] == '>' && $content[$i - 1] != '?') { $base_end = $i; break; } $i++; } $content = substr($content, 0, $base_end + 1) . $new_css_include . substr($content, $base_end + 1); } } if (getNitroPersistence('Mini.JSExtract')) { $jsExtractCheckPassed = true; if (getNitroPersistence('Mini.JSExclude')) { $jsExclude = trim(getNitroPersistence('Mini.JSExclude'), "\n\r "); $jsExclude = explode("\n", $jsExclude); foreach ($jsExclude as $k => $script) { $script = trim($script, "\n\r "); if (!empty($script)) { $jsExclude[$k] = $script; } } } if (getNitroPersistence('Mini.JSExcludeInline')) { $jsLineExclude = trim(getNitroPersistence('Mini.JSExcludeInline'), "\n\r "); $jsLineExclude = explode("\n", $jsLineExclude); foreach ($jsLineExclude as $k => $script) { $script = trim($script, "\n\r "); if (!empty($script)) { $jsLineExclude[$k] = $script; } } } if (NITRO_DEFAULT_EXCLUDES) { $jsLineExclude[] = 'lazy'; if (stripos(getOpenCartSetting('config_template'), 'journal') !== false) { $jsLineExclude[] = 'function display'; $jsLineExclude[] = 'journal'; $jsLineExclude[] = 'Journal'; } $jsLineExclude[] = 'jcarousel'; $jsLineExclude[] = 'ddslick'; } $extractedJSFiles = array(); $current_pos = 0; $html_end = strlen($content) - 1; while ($html_end !== false && $current_pos < $html_end) { $next_js = strpos($content, '<script', $current_pos); $tag_start = $next_js; if ($next_js !== false) { //go left to check if we are in a script tag $i = $next_js; $tag_start = $i; $tag = 'script'; while ($i < $next_js && !in_array($content[$i], array(' ', "\n", "\r"))) { $tag .= $content[$i]; $i++; } if (strtolower($tag) == 'script') { //see if we are not in a comment block $c = $i; $commentStartFound = false; $commentEndFound = false; while ($c > 0 && !$commentStartFound) { if ($content[$c] == '>') { if (substr($content, $c - 2, 3) == '-->') { $commentEndFound = true; break; } } if ($content[$c] == '<') { if (substr($content, $c, 4) == '<!--') { $commentStartFound = true; } } $c--; } $weAreInComment = $commentStartFound && !$commentEndFound; //find the src $src_start = $i; $isSrcStartFound = false; while ($i < $html_end && !$isSrcStartFound) { if ($content[$i] == 's' && substr($content, $i, 4) == 'src=') { $isSrcStartFound = true; $src_start = $i; break; } else { if ($content[$i] == '>' && $content[$i - 1] != '?') { //we have reached the closing char of the script tag break; } } $i++; } $i = $src_start + 5; $js_src = ''; if ($isSrcStartFound) { while ($i < $html_end && $content[$i] != '\'' && $content[$i] != '"') { $js_src .= $content[$i]; $i++; } } $js_src = trim($js_src); if (!$isSrcStartFound && !$weAreInComment) { //inline javascript $type_start = false; $end_of_tag = false; $i = $tag_start; while ($i < $html_end && !$type_start && !$end_of_tag) { if ($content[$i] == 't') { if (substr($content, $i, 5) == 'type=') { $type_start = $i + 6; break; } } else { if ($content[$i] == '>' && $content[$i - 1] != '?') { $end_of_tag = $i; break; } } $i++; } if ($type_start) { $i = $type_start; $script_type = ''; while ($i < $html_end && $content[$i] != '\'' && $content[$i] != '"') { $script_type .= $content[$i]; $i++; } if ($script_type == 'text/javascript') { while ($i < $html_end && !$end_of_tag) { if ($content[$i] == '>' && $content[$i - 1] != '?') { $end_of_tag = $i; break; } $i++; } } } if ($end_of_tag) { $script_end = strpos($content, '</script', $end_of_tag); $code = substr($content, $end_of_tag + 1, $script_end - ($end_of_tag + 1)); foreach ($jsLineExclude as $excludeCode) { if (stripos($code, $excludeCode) !== false) { //skip this js $current_pos = $next_js + 1; continue 2; } } $new_js_file = createTempScript($code); $tag_end = $tag_start; $i = $tag_start; $isTagEndFound = false; $passedThroughClosingScriptTag = false; while ($i < $html_end && !$isTagEndFound) { if ($content[$i] == '>' && $content[$i - 1] != '?') { //if we are not in php closing tag if ($passedThroughClosingScriptTag) { $isTagEndFound = true; } } else { if ($content[$i] == '<') { if (substr($content, $i, 8) == '</script') { $passedThroughClosingScriptTag = true; } } } $tag_end = $i; $i++; } $content = substr($content, 0, $tag_start) . substr($content, $tag_end + 1); $extractedJSFiles[] = $new_js_file; $html_end = strlen($content) - 1; } } if (strpos($js_src, '<?php') !== false || nitroIsIgnoredUrl($js_src, $jsExclude) || $weAreInComment || !$isSrcStartFound) { //skip this js if its location is dynamically generated, is excluded, is in comment or is inline $current_pos = $next_js + 1; continue; } $extractedJSFiles[] = $js_src; //cut the js link from html $i = $tag_start; $tag_end = $tag_start; $isTagEndFound = false; $passedThroughClosingScriptTag = false; while ($i < $html_end && !$isTagEndFound) { if ($content[$i] == '>' && $content[$i - 1] != '?') { //if we are not in php closing tag if ($passedThroughClosingScriptTag) { $isTagEndFound = true; } } else { if ($content[$i] == '<') { if (substr($content, $i, 8) == '</script') { $passedThroughClosingScriptTag = true; } } } $tag_end = $i; $i++; } $content = substr($content, 0, $tag_start) . substr($content, $tag_end + 1); $html_end = strlen($content) - 1; } else { $current_pos = $next_js + 1; continue; } } else { break; } $current_pos = $tag_start; } //minify and combine the extracted js //and put it at the end $minJS = optimizeJS(generateJSMinificatorScripts($extractedJSFiles)); $new_js_include = ''; $use_defer = getNitroPersistence('Mini.JSDefer'); foreach ($minJS as $js_file) { $new_js_include .= '<script type="text/javascript" src="' . $js_file . '"' . ($use_defer ? ' defer' : '') . '></script>'; } if (!empty($new_js_include)) { $position = getNitroPersistence('Mini.JSPosition'); if ($position == 'bottom') { $move_pos = strpos($content, '</body'); if ($move_pos === false) { $move_pos = strpos($content, '</html'); } } else { $base_start = strpos($content, '<base'); if ($base_start === false) { $base_start = strpos($content, '<head'); } $i = $base_start; $base_end = 0; while ($i < $html_end && !$base_end) { if ($content[$i] == '>' && $content[$i - 1] != '?') { $base_end = $i; break; } $i++; } $move_pos = $base_end + 1; } if ($move_pos !== false) { $content = substr($content, 0, $move_pos) . $new_js_include . substr($content, $move_pos); } else { $content .= $new_js_include; } } } return $content; }
function nitro_resolve_cdn($path, $real_url) { if (!empty($real_url)) { $real_url = nitro_clean_path(rtrim($real_url, '/'), null, '/'); } if (getNitroPersistence('CDNStandard.GenericURL')) { $real_url = nitro_clean_path(rtrim(getNitroPersistence('CDNStandard.GenericURL'), '/'), null, '/'); } else { if (empty($real_url)) { $real_url = ''; } } if (stripos($path, 'http') === 0 || stripos($path, '//') === 0) { return $path; } $path = ltrim($path, '/'); if (areWeInIgnoredUrl() && getNitroPersistence('PageCache.Enabled')) { return $real_url . $path; } $type = nitro_get_file_type($path); $cdn_persistence = ''; $cdn_http = ''; $cdn_https = ''; if (getNitroPersistence('CDNStandard.Enabled')) { $cdn_persistence = '3'; switch ($type) { case 'image': if (getNitroPersistence('CDNStandard.ServeImages')) { $cdn_http = getNitroPersistence('CDNStandard.ImagesHttpUrl'); $cdn_https = getNitroPersistence('CDNStandard.ImagesHttpsUrl'); } break; case 'js': if (getNitroPersistence('CDNStandard.ServeJavaScript')) { $cdn_http = getNitroPersistence('CDNStandard.JavaScriptHttpUrl'); $cdn_https = getNitroPersistence('CDNStandard.JavaScriptHttpsUrl'); } break; case 'css': if (getNitroPersistence('CDNStandard.ServeCSS')) { $cdn_http = getNitroPersistence('CDNStandard.CSSHttpUrl'); $cdn_https = getNitroPersistence('CDNStandard.CSSHttpsUrl'); } break; } } elseif (getNitroPersistence('CDNAmazon.Enabled')) { $cdn_persistence = '1'; switch ($type) { case 'image': if (getNitroPersistence('CDNAmazon.ServeImages')) { $cdn_http = getNitroPersistence('CDNAmazon.ImageHttpUrl'); $cdn_https = getNitroPersistence('CDNAmazon.ImageHttpsUrl'); } break; case 'js': if (getNitroPersistence('CDNAmazon.ServeJavaScript')) { $cdn_http = getNitroPersistence('CDNAmazon.JavaScriptHttpUrl'); $cdn_https = getNitroPersistence('CDNAmazon.JavaScriptHttpsUrl'); } break; case 'css': if (getNitroPersistence('CDNAmazon.ServeCSS')) { $cdn_http = getNitroPersistence('CDNAmazon.CSSHttpUrl'); $cdn_https = getNitroPersistence('CDNAmazon.CSSHttpsUrl'); } break; } } elseif (getNitroPersistence('CDNRackspace.Enabled')) { $cdn_persistence = '2'; switch ($type) { case 'image': if (getNitroPersistence('CDNRackspace.ServeImages')) { $cdn_http = getNitroPersistence('CDNRackspace.ImagesHttpUrl'); $cdn_https = getNitroPersistence('CDNRackspace.ImagesHttpsUrl'); } break; case 'js': if (getNitroPersistence('CDNRackspace.ServeJavaScript')) { $cdn_http = getNitroPersistence('CDNRackspace.JavaScriptHttpUrl'); $cdn_https = getNitroPersistence('CDNRackspace.JavaScriptHttpsUrl'); } break; case 'css': if (getNitroPersistence('CDNRackspace.ServeCSS')) { $cdn_http = getNitroPersistence('CDNRackspace.CSSHttpUrl'); $cdn_https = getNitroPersistence('CDNRackspace.CSSHttpsUrl'); } break; } } if (getSSLCachePrefix() == '-1' && !empty($cdn_https)) { // We have SSL $cdn_url = $cdn_https; } else { $cdn_url = $cdn_http; } if (empty($cdn_persistence) || empty($cdn_url)) { return $real_url . $path; } $cdn_url = nitro_clean_path(rtrim($cdn_url, '/'), null, '/'); global $db; $exists = $db->query("SELECT * FROM " . DB_PREFIX . "nitro_cdn_files WHERE file='" . $db->escape($path) . "' AND uploaded=1 AND cdn=" . $cdn_persistence . " LIMIT 0,1"); if ($exists->num_rows) { return $cdn_url . $path; } else { return $real_url . $path; } }
private function get_clean_root() { $root = implode('/', explodeTrim('/', getNitroPersistence('CDNStandardFTP.Root'))); return !empty($root) ? '/' . $root . '/' : '/'; }
public function getScripts() { if (defined('HTTP_CATALOG')) { return $this->scripts; } $oc_root = dirname(DIR_APPLICATION); $cache = NULL; $cachefile = NULL; $filename = NULL; if (!defined('DS')) { define('DS', DIRECTORY_SEPARATOR); } //load NitroCache require_once $oc_root . DS . 'system' . DS . 'nitro' . DS . 'config.php'; require_once $oc_root . DS . 'system' . DS . 'nitro' . DS . 'core' . DS . 'core.php'; $nitroSettings = getNitroPersistence(); if (empty($nitroSettings['Nitro']['Enabled']) || $nitroSettings['Nitro']['Enabled'] != 'yes' || empty($nitroSettings['Nitro']['Mini']['Enabled']) || empty($nitroSettings['Nitro']['Mini']['JS']) || $nitroSettings['Nitro']['Mini']['Enabled'] != 'yes' || $nitroSettings['Nitro']['Mini']['JS'] != 'yes' || function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl()) { return $this->scripts; } if (!file_exists($oc_root . DS . 'assets')) { mkdir($oc_root . DS . 'assets'); } if (!file_exists($oc_root . DS . 'assets' . DS . 'script')) { mkdir($oc_root . DS . 'assets' . DS . 'script'); } $jsExclude = array(); if (!empty($nitroSettings['Nitro']['Mini']['JSExclude'])) { $jsExclude = explode("\n", $nitroSettings['Nitro']['Mini']['JSExclude']); foreach ($jsExclude as $k => $scriptname) { $jsExclude[$k] = trim($scriptname, "\n\r "); } } //extract local fylesystem path foreach ($this->scripts as $hash => $script) { $url_info = parse_url($script); if (!empty($url_info['path'])) { $f = trim($url_info['path'], '/'); if (file_exists($oc_root . DS . $f)) { $this->scripts[$hash] = $f; } else { if (empty($nitroSettings['Nitro']['Mini']['JSExclude'])) { $nitroSettings['Nitro']['Mini']['JSExclude'] = ''; } $jsExclude[] = basename($style['href']); } } else { if (empty($nitroSettings['Nitro']['Mini']['JSExclude'])) { $nitroSettings['Nitro']['Mini']['JSExclude'] = ''; } $jsExclude[] = basename($style['href']); } } if (!empty($nitroSettings['Nitro']['Mini']['JSCombine']) && $nitroSettings['Nitro']['Mini']['JSCombine'] == 'yes') { $cachefile = $oc_root . DS . 'assets' . DS . 'script' . DS . getSSLCachePrefix() . 'scripts-combined.cache'; if (!file_exists($cachefile)) { touch($cachefile); file_put_contents($cachefile, json_encode(array())); } $cache = json_decode(file_get_contents($cachefile), true); $comboHash = ''; $excludedScripts = array(); $includedScripts = 0; foreach ($this->scripts as $hash => $script) { if (!in_array(trim(basename($script), "\r\n"), $jsExclude)) { $comboHash .= $hash; $includedScripts++; } else { $excludedScripts[$hash] = $script; } } $comboHash = md5($comboHash); $target = '/assets/script/' . getSSLCachePrefix() . 'nitro-combined-' . $comboHash . '.js'; $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS); $recache = false; foreach ($this->scripts as $hash => $script) { if (!in_array(trim(basename($script), "\r\n"), $jsExclude)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $script), DS); if (!empty($cache[$comboHash][$filename])) { if ($cache[$comboHash][$filename] != filemtime($filename)) { $recache = true; break; } } else { $recache = true; break; } } } $minifiedCombined = ''; if ($recache || !file_exists($targetAbsolutePath)) { include_once $oc_root . DS . 'system' . DS . 'nitro' . DS . 'lib' . DS . 'minifier' . DS . 'JSMin.php'; $counter = 0; foreach ($this->scripts as $hash => $script) { if (!in_array(trim(basename($script), "\r\n"), $jsExclude)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $script), DS); $scriptSrc = file_get_contents($filename); //minify $scriptSrc = JSMin::minify($scriptSrc); if (substr($scriptSrc, -1) == ')') { $scriptSrc .= ';'; } $minifiedCombined .= ($counter > 0 ? PHP_EOL : '') . $scriptSrc; unset($scriptSrc); $cache[$comboHash][$filename] = filemtime($filename); $counter++; } } file_put_contents($targetAbsolutePath, $minifiedCombined); } file_put_contents($cachefile, json_encode($cache)); if ($includedScripts > 0) { return array_merge($excludedScripts, array(md5($target) => trim($target, '/'))); } else { return $excludedScripts; } } else { $cachefile = $oc_root . DS . 'assets' . DS . 'script' . DS . getSSLCachePrefix() . 'scripts.cache'; if (!file_exists($cachefile)) { touch($cachefile); file_put_contents($cachefile, json_encode(array())); } $cache = json_decode(file_get_contents($cachefile), true); include_once $oc_root . DS . 'system' . DS . 'nitro' . DS . 'lib' . DS . 'minifier' . DS . 'JSMin.php'; foreach ($this->scripts as $hash => $script) { $recache = false; if (!in_array(trim(basename($script), "\r\n"), $jsExclude)) { $filename = $oc_root . DS . trim(str_replace('/', DS, $script), DS); $basefilename = basename($script, '.js'); $target = '/assets/script/' . getSSLCachePrefix() . 'nitro-mini-' . $basefilename . '.js'; $targetAbsolutePath = $oc_root . DS . trim(str_replace('/', DS, $target), DS); if (!empty($cache[$filename])) { if ($cache[$filename] != filemtime($filename)) { $recache = true; } } else { $recache = true; } if ($recache || !file_exists($targetAbsolutePath)) { touch($targetAbsolutePath); $scriptSrc = file_get_contents($filename); //minify $scriptSrc = JSMin::minify($scriptSrc); file_put_contents($targetAbsolutePath, $scriptSrc); $cache[$filename] = filemtime($filename); } $this->scripts[$hash] = trim($target, '/'); } } file_put_contents($cachefile, json_encode($cache)); return $this->scripts; } }
function applyCloudFlareFix() { if (getNitroPersistence('CDNCloudFlare.Enabled')) { if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; } } }
public function applyHtaccessCompressionRules() { $this->loadCore(); $this->restoreHtaccessCompress(); if (!getNitroPersistence('Enabled') || !getNitroPersistence('Compress.Enabled')) { return false; } if (getNitroPersistence('BrowserCache.Enabled') && getNitroPersistence('BrowserCache.CSSJS.Period') != 'no-cache') { switch (getNitroPersistence('BrowserCache.CSSJS.Period')) { case '1 week': $browser_cache = 7 * 24 * 3600; break; case '1 month': $browser_cache = 30 * 24 * 3600; break; case '6 months': $browser_cache = 6 * 30 * 24 * 3600; break; case '1 year': $browser_cache = 365 * 24 * 3600; break; default: $browser_cache = 0; } } $htrules = '# STARTCOMPRESSNITRO' . PHP_EOL; $htrules = 'RewriteEngine On' . PHP_EOL; if (getNitroPersistence('Compress.CSS') && (int) getNitroPersistence('Compress.CSSLevel') > 0) { $htrules .= PHP_EOL; //$htrules .= '<IfModule mod_rewrite.c>'.PHP_EOL; $htrules .= 'RewriteCond %{SCRIPT_FILENAME} !-d' . PHP_EOL; $htrules .= 'RewriteRule ^(\\/?((catalog)|(assets)).+)\\.css$ assets/style.php?l=' . getNitroPersistence('Compress.CSSLevel') . '&p=$1' . (!empty($browser_cache) ? '&c=' . $browser_cache : '') . ' [NC,L]' . PHP_EOL; //$htrules .= '</IfModule>'.PHP_EOL; } if (getNitroPersistence('Compress.JS')) { $htrules .= PHP_EOL; //$htrules .= '<IfModule mod_rewrite.c>'.PHP_EOL; $htrules .= 'RewriteCond %{SCRIPT_FILENAME} !-d' . PHP_EOL; $htrules .= 'RewriteRule ^(\\/?((catalog)|(assets)).+)\\.js$ assets/script.php?l=' . getNitroPersistence('Compress.CSSLevel') . '&p=$1' . (!empty($browser_cache) ? '&c=' . $browser_cache : '') . ' [NC,L]' . PHP_EOL; //$htrules .= '</IfModule>'.PHP_EOL; } $htrules .= 'AddType image/svg+xml .svg' . PHP_EOL; $htrules .= 'AddOutputFilterByType DEFLATE image/svg+xml' . PHP_EOL; $htrules .= '# ENDCOMPRESSNITRO' . PHP_EOL; $newHtaccessFileContent = $htrules . $this->getHtaccessFileContent(); return $this->setHtaccessFileContent($newHtaccessFileContent); }
<?php if (isNitroEnabled() && (getNitroPersistence('Smush.Method') && getNitroPersistence('Smush.Method') == 'local') && getNitroPersistence('Smush.OnDemand')) { if (!empty($new_image) && file_exists(DIR_IMAGE . $new_image)) { loadNitroLib('NitroSmush/NitroSmush'); $smusher = new NitroSmush(); global $registry; $log = $registry->get('log'); try { $filename = DIR_IMAGE . $new_image; $res = $smusher->smush($filename); if (!empty($res['errors']) && NITRO_DEBUG_MODE == 1) { $log->write("[NitroSmush: {$filename}] => " . var_export($res, true)); } } catch (Exception $e) { set_time_limit(30); } } }
public function resize($filename, $width, $height, $type = "") { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); require_once DIR_SYSTEM . 'nitro/config.php'; require_once DIR_SYSTEM . 'nitro/core/core.php'; $nitroPersistence = getNitroPersistence(); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['ImageCache']['OverrideCompression']) && $nitroPersistence['Nitro']['ImageCache']['OverrideCompression'] == 'yes') { } if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height, $type); $image->save(DIR_IMAGE . $new_image); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['PageCache']['Enabled']) && $nitroPersistence['Nitro']['PageCache']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['SmushIt']['OnDemand']) && $nitroPersistence['Nitro']['SmushIt']['OnDemand'] == 'yes' && !(function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl())) { include_once dirname(DIR_SYSTEM) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'smushit.php'; try { $file = new SmushIt(DIR_IMAGE . $new_image, SmushIt::LOCAL_ORIGIN); // And finaly, replace original file by the compressed version // Sometimes, Smush.it convert files. We don't want that to happen. $src = pathinfo($file->source, PATHINFO_EXTENSION); $dst = pathinfo($file->destination, PATHINFO_EXTENSION); if ($src == $dst and copy($file->destination, DIR_IMAGE . $new_image)) { // Success ! //echo 'Smushed File: '.$source.'<br>'; } } catch (Exception $e) { set_time_limit(30); } } } else { copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); if (!empty($nitroPersistence['Nitro']['Enabled']) && $nitroPersistence['Nitro']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['PageCache']['Enabled']) && $nitroPersistence['Nitro']['PageCache']['Enabled'] == 'yes' && !empty($nitroPersistence['Nitro']['SmushIt']['OnDemand']) && $nitroPersistence['Nitro']['SmushIt']['OnDemand'] == 'yes' && !(function_exists('areWeInIgnoredUrl') && areWeInIgnoredUrl())) { include_once dirname(DIR_SYSTEM) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'smushit.php'; try { $file = new SmushIt(DIR_IMAGE . $new_image, SmushIt::LOCAL_ORIGIN); // And finaly, replace original file by the compressed version // Sometimes, Smush.it convert files. We don't want that to happen. $src = pathinfo($file->source, PATHINFO_EXTENSION); $dst = pathinfo($file->destination, PATHINFO_EXTENSION); if ($src == $dst and copy($file->destination, DIR_IMAGE . $new_image)) { // Success ! //echo 'Smushed File: '.$source.'<br>'; } } catch (Exception $e) { set_time_limit(30); } } } } return $this->getImageUrl($new_image); }
public function cdn() { if ($this->request->server['REQUEST_METHOD'] != 'POST') { $this->response->setOutput(''); return; } $this->load->model('tool/nitro'); $this->model_tool_nitro->loadCore(); set_error_handler(create_function('$severity, $message, $file, $line', 'throw new Exception($message . " in file " . $file . " on line " . $line . ". Debug backtrace: <pre>" . print_r(debug_backtrace(), true) . "</pre>");')); // Save config if (!empty($this->request->post['config']['Nitro'])) { $nitro_persistence = getNitroPersistence(); foreach ($this->request->post['config']['Nitro'] as $key => $config_vals) { $nitro_persistence['Nitro'][$key] = $config_vals; } setNitroPersistence($nitro_persistence); } if (empty($this->request->post['last'])) { $this->request->post['last'] = array('response_type' => 'error', 'percent' => 0, 'message' => 'CDN type not valid.'); } $response =& $this->request->post['last']; try { if (!empty($this->request->post['cdn'])) { switch ($this->request->post['cdn']) { case 'amazon': $this->load->model('tool/nitro_amazon'); $this->model_tool_nitro_amazon->upload(); break; case 'rackspace': $this->load->model('tool/nitro_rackspace'); $this->model_tool_nitro_rackspace->upload(); break; case 'ftp': $this->load->model('tool/nitro_ftp'); $this->model_tool_nitro_ftp->upload(); break; } } } catch (Exception $e) { $response['percent'] = 0; $response['response_type'] = 'error'; $response['message'] = $e->getMessage(); } restore_error_handler(); $this->response->setOutput(json_encode($response)); }