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 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; } }
function generateNameOfCacheFile() { if (!empty($GLOBALS['nitro.pagecache.file'])) { return $GLOBALS['nitro.pagecache.file']; } nitroEnableSession(); if (empty($_SESSION['language']) && empty($_SESSION['currency'])) { $db = NitroDb::getInstance(); $db->query("SET NAMES 'utf8'"); $db->query("SET CHARACTER SET utf8"); $db->query("SET CHARACTER_SET_CONNECTION=utf8"); $db->query("SET SQL_MODE = ''"); // In, when the site is opened for first time // Store if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == '1')) { $store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`ssl`, 'www.', '') = '" . $db->escape('https://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'"); } else { $store_query = $db->query("SELECT * FROM " . DB_PREFIX . "store WHERE REPLACE(`url`, 'www.', '') = '" . $db->escape('http://' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . rtrim(dirname($_SERVER['PHP_SELF']), '/.\\') . '/') . "'"); } $store_id = 0; if ($store_query->num_rows) { $result = $store_query->row; $store_id = (int) $result['store_id']; } $GLOBALS['nitro.store_id'] = $store_id; $resource = $db->query("SELECT * FROM " . DB_PREFIX . "setting WHERE (`key`='config_language' OR `key`='config_currency') AND `store_id` = '" . $store_id . "'"); if ($resource->num_rows) { $data = array(); $config_language = 0; $config_currency = 0; foreach ($resource->rows as $result) { if (!empty($result['key']) && $result['key'] == 'config_language') { $config_language = strtolower($result['value']); } if (!empty($result['key']) && $result['key'] == 'config_currency') { $config_currency = strtolower($result['value']); } } if (isset($_SESSION)) { $_SESSION['language'] = $config_language; $_SESSION['currency'] = $config_currency; } } } $filename = getFullURL(); $filename = str_replace(array('/', '?', ':', ';', '=', '&', '&', '.', '--', '%', '~', '-amp-'), '-', $filename); $default_language = !empty($_COOKIE['language']) ? $_COOKIE['language'] : '0'; $default_currency = !empty($_COOKIE['currency']) ? $_COOKIE['currency'] : '0'; $language = strtolower(!empty($_SESSION['language']) && is_string($_SESSION['language']) ? $_SESSION['language'] : $default_language); $currency = strtolower(!empty($_SESSION['currency']) && is_string($_SESSION['currency']) ? $_SESSION['currency'] : $default_currency); if (NITRO_DEBUG_MODE) { $cached_filename = $filename . '-' . $language . '-' . $currency . '.html'; } else { $cached_filename = md5($filename . '-' . $language . '-' . $currency) . '.html'; } if (mobileCheck()) { $cached_filename = 'mobile-' . $cached_filename; } $GLOBALS['nitro.pagecache.file'] = getSSLCachePrefix() . $cached_filename; return $GLOBALS['nitro.pagecache.file']; }
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; } }
function generateNameOfCacheFile() { if (empty($_SESSION['language']) && empty($_SESSION['currency'])) { // In, when the site is opened for first time if ($link = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD)) { if (mysql_select_db(DB_DATABASE, $link)) { mysql_query("SET NAMES 'utf8'", $link); mysql_query("SET CHARACTER SET utf8", $link); mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $link); mysql_query("SET SQL_MODE = ''", $link); $resource = mysql_query("SELECT * FROM " . DB_PREFIX . "setting WHERE `key`='config_language' OR `key`='config_currency'", $link); if ($resource && is_resource($resource)) { $data = array(); $config_language = 0; $config_currency = 0; while ($result = mysql_fetch_assoc($resource)) { if (!empty($result['key']) && $result['key'] == 'config_language') { $config_language = strtolower($result['value']); } if (!empty($result['key']) && $result['key'] == 'config_currency') { $config_currency = strtolower($result['value']); } } if (isset($_SESSION)) { $_SESSION['language'] = $config_language; $_SESSION['currency'] = $config_currency; } } mysql_close($link); } } } $filename = getFullURL(); $filename = str_replace(array('/', '?', ':', ';', '=', '&', '&', '.', '--', '%', '~', '-amp-'), '-', $filename); $language = strtolower(!empty($_SESSION['language']) ? $_SESSION['language'] : '0'); $currency = strtolower(!empty($_SESSION['currency']) ? $_SESSION['currency'] : '0'); if (NITRO_MODE) { //if > 0 than we are in debug mode $cached_filename = $filename . '-' . $language . '-' . $currency . '.html'; } else { $cached_filename = md5($filename . '-' . $language . '-' . $currency) . '.html'; } return getSSLCachePrefix() . $cached_filename; }