Ejemplo n.º 1
0
 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);
     }
 }
Ejemplo n.º 2
0
    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);
}
Ejemplo n.º 3
0
function sendNitroMail($to, $subject, $message)
{
    require_once realpath(DIR_SYSTEM . 'library/mail.php');
    $mail = new Mail();
    $mail->protocol = getOpenCartSetting('config_mail_protocol');
    $mail->parameter = getOpenCartSetting('config_mail_parameter');
    $mail->hostname = getOpenCartSetting('config_smtp_host');
    $mail->username = getOpenCartSetting('config_smtp_username');
    $mail->password = getOpenCartSetting('config_smtp_password');
    $mail->port = getOpenCartSetting('config_smtp_port');
    $mail->timeout = getOpenCartSetting('config_smtp_timeout');
    $mail->setTo($to);
    $mail->setFrom(getOpenCartSetting('config_email'));
    $mail->setSender(getOpenCartSetting('config_name'));
    $mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
    $mail->setText(html_entity_decode($message, ENT_QUOTES, 'UTF-8'));
    $mail->send();
}
Ejemplo n.º 4
0
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;
}