Exemple #1
0
 function replace_cdn_script_tags(Wa72\HtmlPageDom\HtmlPageCrawler &$c)
 {
     //Find all cdn scripts
     $nodes = $c->filter('script');
     $nodes->each(function (Wa72\HtmlPageDom\HtmlPageCrawler &$node) use($c) {
         /* @var $node \Wa72\HtmlPageDom\HtmlPageCrawler */
         $href = $node->getAttribute('src');
         if (!file_exists(THEME . '/' . $href)) {
             //Check if this is an external link
             if (filter_var($href, FILTER_VALIDATE_URL) === false && Mvc_Functions::startsWith($href, '//')) {
                 //Determine its download location by hashing the url
                 $hash = md5($href);
                 $filename = "static/js/downloaded/{$hash}.js";
                 $file_loc = THEME . "/" . $filename;
                 if (!file_exists($file_loc) && isset($_GET['downloadjs'])) {
                     //Download the link
                     //						echo "$href <br>";
                     if (Mvc_Functions::startsWith($href, '//')) {
                         $href = 'http:' . $href;
                     }
                     $content = file_get_contents($href);
                     debug($href);
                     debug($content);
                     if (empty($content)) {
                         return;
                     }
                     if (!is_dir(dirname($file_loc))) {
                         mkdir(dirname($file_loc));
                     }
                     file_put_contents($file_loc, $content);
                     $c->filter('script[src="' . $href . '"]')->setAttribute('src', $filename);
                 } else {
                     if (file_exists($file_loc)) {
                         $c->filter('script[src="' . $href . '"]')->setAttribute('src', $filename);
                     }
                 }
             }
         }
     });
 }