public static function sendFile($type, $file_name) { isset($file_name) && $file_name != '' || die('jbetolo: sendFile: no file to send.'); if ($type != 'htaccess') { jimport('joomla.plugin.plugin'); JPluginHelper::importPlugin('system', 'jbetolo'); $is_gz = JRequest::getBool('gz', false); $is_minify = JRequest::getBool('min', false); $to_file = JBETOLO_CACHE_DIR . $file_name; $age = JRequest::getInt('ag', plgSystemJBetolo::param('cache-age')); } else { list($is_gz, $age, $type, $file_name) = explode('_', $file_name); $to_file = JBETOLO_CACHE_DIR . $file_name; $is_gz = (bool) $is_gz; } if (file_exists($to_file)) { self::doSendFile($type, $to_file, $is_gz, $age); } else { die('Restricted access'); } }
public static function parseHTML($html, $tags = 'link|script') { $pat = '<(' . $tags . ')([^>]+)(?:/\\s*>|>(.*)</(' . $tags . ')>)'; $pat = '#(<\\!--\\s*\\[if[^\\]]+?\\]\\s*>\\s*)?(?:' . $pat . '\\s*)+(?(1)<\\!\\[endif\\]-->)#Uism'; $values = array(); if (preg_match_all($pat, $html, $m)) { $htmls = $m[0]; $conds = $m[1]; $tags = $m[2]; $attrs = $m[3]; $texts = $m[4]; $merge = array('css' => plgSystemJBetolo::param('css_merge'), 'js' => plgSystemJBetolo::param('js_merge')); $gzip = array('css' => JBETOLO_IS_GZ && plgSystemJBetolo::param('css_gzip'), 'js' => JBETOLO_IS_GZ && plgSystemJBetolo::param('js_gzip')); $gzip_excluded = array('css' => $gzip && plgSystemJBetolo::param('css_gzip_excluded'), 'js' => $gzip && plgSystemJBetolo::param('js_gzip_excluded')); $minify = array('css' => JBETOLO_IS_MINIFY && plgSystemJBetolo::param('css_minify'), 'js' => JBETOLO_IS_MINIFY && plgSystemJBetolo::param('js_minify')); $minify_excluded = array('css' => $minify && plgSystemJBetolo::param('css_minify_excluded'), 'js' => $minify && plgSystemJBetolo::param('js_minify_excluded')); // collect resources to be excluded from merging $merge_exclude = array(); foreach ($merge as $type => $m) { if ($m) { $merge_exclude[$type] = plgSystemJBetolo::param($type . '_merge_exclude'); if (isset($merge_exclude[$type]) && $merge_exclude[$type]) { $merge_exclude[$type] = explode(',', $merge_exclude[$type]); } else { $merge_exclude[$type] = array(); } // Gzip operates at file level, therefore if a file is indicated to be non-gzipped // and gzipping of merged file is enabled then we need to exclude it // (the analogus doesn't apply for minify as merged file can contain a mix of // minified and non-minified code) $abs_excl = plgSystemJBetolo::param('gzip_exclude'); if ($abs_excl && $gzip) { $abs_excl = explode(',', $abs_excl); $merge_exclude[$type] = array_merge($merge_exclude[$type], $abs_excl); } } } foreach ($merge_exclude as $type => $m) { foreach ($m as $i => $file) { $merge_exclude[$type][$i] = jbetoloFileHelper::normalizeCall($file, false, false); } } foreach (array('link', 'script') as $tag) { $included = plgSystemJBetolo::param(($tag == 'link' ? 'css' : 'js') . '_include'); if (!empty($included)) { $included = explode(',', $included); foreach ($included as $incl) { $conds[] = $texts[] = ''; $tags[] = $tag; $attrs[] = ' ' . ($tag == 'link' ? 'href' : 'src') . '="' . $incl . '" ' . ($tag == 'link' ? 'rel="stylesheet"' : ' type="text/javascript"'); } } } foreach ($attrs as $i => $attr) { $aggr = $tags[$i]; $value = array('_html' => $htmls[$i], '_text' => $texts[$i], '_tag' => $tags[$i], '_iecond' => !empty($conds[$i])); if (preg_match_all("#([^=\\s]+)=[\"\\']([^\"\\']+)[\"\\']+#Uim", $attr, $m, PREG_PATTERN_ORDER)) { $value = array_merge($value, array_combine($m[1], $m[2])); } $value['_inline'] = !empty($value['_text']); if (!$value['_inline']) { if ($value['_tag'] == 'script') { $value['_type'] = $aggr = 'js'; $value['_src'] = jbetoloFileHelper::normalizeCall($value['src'], false, false, true, 'js'); $value['_excluded'] = jbetoloFileHelper::isFileExcluded($value['_src'], $merge_exclude['js']); } else { if ($value['_tag'] == 'link' && $value['rel'] == 'stylesheet' || $value['_tag'] == 'style') { $value['_type'] = $aggr = 'css'; if ($value['_tag'] == 'link') { $value['_href'] = jbetoloFileHelper::normalizeCall($value['href'], false, false, true, 'css'); $value['_excluded'] = jbetoloFileHelper::isFileExcluded($value['_href'], $merge_exclude['css']); if (!isset($value['media'])) { $value['media'] = 'screen'; } } } } } if ($value['_iecond']) { $value['_excluded'] = true; } if (!isset($values[$aggr])) { $values[$aggr] = array(); } $values[$aggr][] = $value; } } return $values; }
private function parseBody($body, $type) { $merge = self::param($type . '_merge'); $gzip = JBETOLO_IS_GZ && self::param($type . '_gzip'); $gzip_excluded = $gzip && self::param($type . '_gzip_excluded'); $minify = JBETOLO_IS_MINIFY && self::param($type . '_minify'); $minify_excluded = $minify && self::param($type . '_minify_excluded'); if (!$merge && !$gzip_excluded && !$minify_excluded) { return; } $head_only = (bool) self::param($type . '_scan_headonly', true); if ($head_only) { list($body, ) = explode('</head>', $body); list(, $body) = explode('<head>', $body); } // absolutely included resources are appended to body $included = plgSystemJBetolo::param($type . '_include', ''); $included = trim($included); if ($included) { $included = @explode(',', $included); foreach ($included as $i => $include) { $include = jbetoloFileHelper::normalizeCall($include); if ($type == 'js') { $included[$i] = "<script type=\"text/javascript\" src=\"" . $include . "\"></script>\n"; } else { if ($type == 'css') { $included[$i] = "<link rel=\"stylesheet\" href=\"" . $include . "\" type=\"text/css\" media=\"screen\" />\n"; } } } $included = implode('', $included); $body = str_ireplace('</title>', '</title>' . $included, $body); } $excluded = $comments = $conds = $excludedSrcs = array(); // find and consider IE conditionals as excluded from merging preg_match_all(self::$conditionalTagScript[$type], $body, $condTags); $condTags = $condTags[0]; self::$conditionalTags = implode('', $condTags); preg_match_all(self::$conditionalSrcScript[$type], self::$conditionalTags, $matches); foreach ($matches[0] as $c => $conditional) { $conds[] = jbetoloFileHelper::normalizeCall($matches[1][$c]); $excludedSrcs[] = $conds[$c]; } // find and exclude commented resources from merging preg_match_all(self::$commentedTagScript[$type], $body, $matches); if (!empty($matches[0]) && !empty($condTags)) { foreach ($matches[0] as $m => $match) { if (in_array($match, $condTags)) { unset($matches[0][$m]); } } } $matches = implode('', $matches[0]); preg_match_all(self::$conditionalSrcScript[$type], $matches, $matches); foreach ($matches[0] as $c => $conditional) { $comments[] = jbetoloFileHelper::normalizeCall($matches[1][$c]); $excludedSrcs[] = $comments[$c]; } // collect resources to be excluded from merging if ($merge) { $excluded = self::param($type . '_merge_exclude'); if (isset($excluded) && $excluded) { $excluded = @explode(',', $excluded); } else { $excluded = array(); } $excluded = array_merge($excluded, self::$predefinedExclude[$type], $excludedSrcs); // Gzip operates at file level, therefore if a file is indicated to be non-gzipped // and gzipping of merged file is enabled then we need to exclude it // (the analogus doesn't apply for minify as merged file can contain a mix of // minified and non-minified code) $abs_excl = self::param('gzip_exclude'); if ($abs_excl && $gzip) { $abs_excl = explode(',', $abs_excl); $excluded = array_merge($excluded, $abs_excl); } } // find all resources preg_match_all(self::$tagRegex[$type], $body, $matches); $tags = $matches[0]; preg_match_all(self::$srcRegex[$type], implode('', $tags), $matches); if (count($matches[0]) != count($tags)) { // Due to incorrect syntax some tags has not found corresponding source entry and will be discarded $n = count($matches[1]); $d = 0; foreach ($tags as $s => $src) { $si = $s - $d; if ($si < $n) { if (strpos($tags[$s], $matches[1][$si]) === false) { unset($tags[$s]); $d++; } } else { unset($tags[$s]); } } $tags = array_filter($tags); $tags = array_values($tags); } $excludedSrcs = $_excludedSrcs = $srcs = $indexes = $srcsIndexes = array(); // prepare required input for the merging by processing each found resource // 1. separate the excluded ones by considering the choosen merging method // 2. if css identify and assign correct media type // 3. if resource is not locally available no further processing $deleteSrcs = self::param('delete'); if ($deleteSrcs) { $deleteSrcs = explode(',', $deleteSrcs); } foreach ($matches[1] as $s => $src) { $indexes[] = array('src' => $src, 'tag' => $tags[$s], 'srci' => ''); $src = jbetoloFileHelper::normalizeCall($src, false, false, true, $type); if ($src) { $asDynamic = jbetoloFileHelper::isSkippedAsDynamic($src); if ($merge) { $shouldIgnore = jbetoloFileHelper::isFileExcluded($src, $excluded); } else { $shouldIgnore = true; } if ($type == 'css') { $attr = jbetoloHelper::extractAttributes($tags[$s]); $indexes[$s]['attr'] = $attr; } if (!$shouldIgnore && !$asDynamic) { $srcs[] = $src; $indexes[$s]['srci'] = count($srcs) - 1; $srcsIndexes[$src] = $indexes[$s]; } else { $isDeleted = false; // is deleted if ($deleteSrcs) { foreach ($deleteSrcs as $d) { if ($d == $matches[1][$s]) { $isDeleted = true; break; } } } if (!$isDeleted) { $excludedSrcs[$src] = array('src' => $src, 'tag' => $tags[$s], 'dynamic' => $asDynamic); $_excludedSrcs[] = $src; $tags[$s] = JBETOLO_EMPTYTAG; } } } else { // external url or resource not found physically on the server $isDeleted = false; // is deleted if ($deleteSrcs) { foreach ($deleteSrcs as $d) { if ($d == $matches[1][$s]) { $isDeleted = true; break; } } } // is left untouched if (!$isDeleted) { $tags[$s] = JBETOLO_EMPTYTAG; } } } // resources to be deleted are removed from found ones if ($deleteSrcs) { foreach ($deleteSrcs as $d) { $_d = jbetoloFileHelper::normalizeCall($d); if ($_d !== false) { $d = $_d; } $f = jbetoloFileHelper::fileInArray($d, $srcs); if ($f) { unset($srcsIndexes[$srcs[$f[0]]]); unset($srcs[$f[0]]); } } } if ($type == 'js') { if (self::param('js_jquery_migrate_plugin', 0)) { $srcs[] = JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY_MIGRATE_PLUGIN; $srcsIndexes[JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY_MIGRATE_PLUGIN] = array('src' => JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY_MIGRATE_PLUGIN, 'tag' => '', 'srci' => ''); } if (self::param('add_local_jquery', 0)) { $srcs[] = JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY; $srcsIndexes[JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY] = array('src' => JBETOLO_PATH . 'jbetolo/assets/jquery/' . JBETOLO_JQUERY, 'tag' => '', 'srci' => ''); self::param('js_jquery', JBETOLO_JQUERY, 'set'); if (self::param('add_local_jquery_ui', 0)) { $srcs[] = JBETOLO_PATH . 'jbetolo/assets/jquery-ui/js/' . JBETOLO_JQUERY_UI; $srcsIndexes[JBETOLO_PATH . 'jbetolo/assets/jquery-ui/js/' . JBETOLO_JQUERY_UI] = array('src' => JBETOLO_PATH . 'jbetolo/assets/jquery-ui/js/' . JBETOLO_JQUERY_UI, 'tag' => '', 'srci' => ''); } } jbetoloJS::setJqueryFile($srcs, $_excludedSrcs); } else { if ($type == 'css') { if (self::param('add_local_jquery_ui_css', 0)) { $srcs[] = JBETOLO_PATH . 'jbetolo/assets/jquery-ui/css/' . JBETOLO_JQUERY_UI_CSS; $srcsIndexes[JBETOLO_PATH . 'jbetolo/assets/jquery-ui/css/' . JBETOLO_JQUERY_UI_CSS] = array('src' => JBETOLO_PATH . 'jbetolo/assets/jquery-ui/css/' . JBETOLO_JQUERY_UI_CSS, 'tag' => '', 'srci' => ''); } } } // apply merging ordering $orderedSrcs = jbetoloFileHelper::customOrder($srcsIndexes, $type, $srcs); $orderedSrcs = jbetoloHelper::getArrayValues($orderedSrcs, 'src'); $orderedExcludedSrcs = jbetoloFileHelper::customOrder($excludedSrcs, $type, $_excludedSrcs); return array($orderedSrcs, $orderedExcludedSrcs, $tags, $conds, $comments, $indexes); }