public static function writeToFile($to_file, $data, $type, $overrideGZ = false) { if (!$to_file) { return false; } $to_file = JBETOLO_CACHE_DIR . '/' . str_replace(JBETOLO_CACHE_DIR . '/', '', $to_file); if (JFile::exists($to_file)) { return true; } if ($type == 'css' || $type == 'js') { $minify = JBETOLO_IS_MINIFY && plgSystemJBetolo::param($type . '_minify'); $exclMinify = plgSystemJBetolo::param('minify_exclude'); if (is_array($data)) { if ($minify && $exclMinify && count($exclMinify) > 0) { $exclMinify = explode(',', $exclMinify); foreach ($data as $d => $content) { if (jbetoloFileHelper::isFileExcluded($content['file'], $exclMinify)) { $data[$d] = $content['content']; } else { $data[$d] = jbetoloFileHelper::minify($type, $content['content']); } } $data = implode("\n", $data); } else { $data = jbetoloHelper::getArrayValues($data, 'content'); $data = implode("\n", $data); if ($minify) { $data = jbetoloFileHelper::minify($type, $data); } } } else { if ($minify) { $data = jbetoloFileHelper::minify($type, $data); } else { $data = jbetoloHelper::eatWhiteSpace($data); } } } if (JBETOLO_IS_GZ && (plgSystemJBetolo::param($type . '_gzip') || $overrideGZ)) { $data = gzencode($data); JFile::write($to_file, $data); } else { JFile::write($to_file, $data); } return true; }
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); }