public static function hasPrototypeJs()
 {
     return MageBridgeModelBridgeHeaders::getInstance()->hasProtoType();
 }
Example #2
0
 /**
  * /**
  * Method to determine which JavaScript to use and which not
  *
  * @access private
  *
  * @param null
  *
  * @return null
  */
 private function handleJavaScript()
 {
     // Get MageBridge variables
     $disableJsMootools = $this->loadConfig('disable_js_mootools');
     $disableJsFootools = $this->loadConfig('disable_js_footools');
     $disableJsFrototype = $this->loadConfig('disable_js_frototype');
     $disable_js_jquery = $this->loadConfig('disable_js_jquery');
     $disable_js_prototype = $this->loadConfig('disable_js_prototype');
     $disable_js_custom = $this->loadConfig('disable_js_custom');
     $disable_js_all = $this->loadConfig('disable_js_all');
     $magento_js = MageBridgeModelBridgeHeaders::getInstance()->getScripts();
     $uri = JURI::getInstance();
     $foo_script = JURI::root(true) . '/media/com_magebridge/js/foo.js';
     $footools_script = JURI::root(true) . '/media/com_magebridge/js/footools.min.js';
     $frototype_script = JURI::root(true) . '/media/com_magebridge/js/frototype.min.js';
     $base_url = $this->getBaseUrl();
     $base_js_url = $this->getBaseJsUrl();
     // Parse the disable_js_custom string into an array
     $disable_js_custom = explode(',', $disable_js_custom);
     foreach ($disable_js_custom as $index => $script) {
         $script = trim($script);
         if (!empty($script)) {
             $disable_js_custom[$index] = $script;
         }
     }
     // Fetch the body
     $body = JResponse::getBody();
     // Determine whether ProtoType is loaded
     $has_prototype = MageBridgeTemplateHelper::hasPrototypeJs();
     if ($has_prototype == false) {
         if (stristr($body, '/js/protoaculous') || stristr($body, '/js/protoculous') || stristr($body, '/prototype.js')) {
             $has_prototype = true;
         }
     }
     // Load the whitelist
     $whitelist = JFactory::getConfig()->get('magebridge.script.whitelist');
     if (!is_array($whitelist)) {
         $whitelist = array();
     }
     // Add some items to the whitelist
     if ($disable_js_jquery == false) {
         $whitelist[] = 'media/system/js/calendar.js';
         $whitelist[] = 'media/system/js/calendar-setup.js';
         $whitelist[] = '/com_jce/';
         $whitelist[] = '/footools.js';
         $whitelist[] = 'www.googleadservices.com';
         $whitelist[] = 'media/jui/js';
         $whitelist[] = 'protostar/js/template.js';
     }
     // Load the blacklist
     $blacklist = JFactory::getConfig()->get('magebridge.script.blacklist');
     // Only parse the body, if MageBridge has loaded the ProtoType library and only if configured
     if ($has_prototype == true && ($disable_js_all > 0 || $disableJsMootools == 1 || !empty($disable_js_custom))) {
         // Disable MooTools (and caption) and replace it with FooTools
         if ($disableJsMootools == 1 && $disableJsFootools == 0) {
             $this->console[] = 'MageBridge removed MooTools core and replaced it with FooTools';
             $footools_tag = '<script type="text/javascript" src="' . $footools_script . '"></script>';
             $body = preg_replace('/\\<script/', $footools_tag . "\n" . '<script ', $body, 1);
         }
         // Find all script tags
         preg_match_all('/\\<script([^<]+)\\>\\<\\/script\\>/', $body, $tags);
         $commented = array();
         foreach ($tags[0] as $tag) {
             // Filter the src="" attribute
             preg_match('/src=([\\"\']{1})([^\\"\']+)/', $tag, $src);
             if (is_array($src) && !empty($src[2])) {
                 $script = $src[2];
             } else {
                 continue;
             }
             // Load the whitelist
             if (!empty($whitelist) && is_array($whitelist)) {
                 $match = false;
                 foreach ($whitelist as $w) {
                     if (stristr($script, $w)) {
                         $match = true;
                         break;
                     }
                 }
                 if ($match == true) {
                     continue;
                 }
             }
             // If this looks like a jQuery script, skip it
             if (stristr($script, 'jquery') && $disable_js_jquery == 0) {
                 continue;
             }
             // If this looks like a ProtoType script, skip it
             if ((stristr($script, 'scriptaculous') || stristr($script, 'prototype')) && $disable_js_prototype == 0) {
                 continue;
             }
             // If this looks like a MageBridge script, skip it
             if (stristr($script, 'com_magebridge')) {
                 continue;
             }
             // Skip URLs that seem to belong to Magento
             if (!empty($base_url) && (strstr($script, 'http://' . $base_url) || strstr($script, 'https://' . $base_url))) {
                 continue;
             } else {
                 if (!empty($base_js_url) && (strstr($script, 'http://' . $base_js_url) || strstr($script, 'https://' . $base_js_url))) {
                     continue;
                     // Skip Magento frontend scripts
                 } else {
                     if (preg_match('/\\/skin\\/frontend\\//', $script)) {
                         continue;
                     } else {
                         // Do some more complex tests
                         $skip = false;
                         // Loop through the whitelist
                         if (!empty($magento_js)) {
                             foreach ($magento_js as $js) {
                                 if (strstr($script, $js)) {
                                     $skip = true;
                                     break;
                                 }
                             }
                         }
                         if ($skip == true) {
                             continue;
                         }
                         // Loop through the known Magento scripts
                         if (!empty($magento_js)) {
                             foreach ($magento_js as $js) {
                                 if (strstr($script, $js)) {
                                     $skip = true;
                                     break;
                                 }
                             }
                         }
                         if ($skip == true) {
                             continue;
                         }
                     }
                 }
             }
             // Decide whether to remove this script by default
             if ($disable_js_all == 1 || $disable_js_all == 3) {
                 $remove = true;
             } else {
                 $remove = false;
             }
             // Load the blacklist
             if (!empty($blacklist) && is_array($blacklist)) {
                 foreach ($blacklist as $b) {
                     if (preg_match('/' . str_replace('/', '\\/', $js) . '$/', $b)) {
                         $remove = true;
                         break;
                     }
                 }
             }
             // Scan for exceptions
             if ($disable_js_all > 1 && !empty($disable_js_custom)) {
                 foreach ($disable_js_custom as $js) {
                     if (preg_match('/' . str_replace('/', '\\/', $js) . '$/', $script)) {
                         $remove = $disable_js_all == 2 ? true : false;
                         break;
                     }
                 }
                 // Disable MooTools
             } else {
                 if ($disableJsMootools == 1) {
                     $mootools_scripts = array('media/system/js/modal.js', 'media/system/js/validate.js', 'beez_20/javascript/hide.js', 'md_stylechanger.js', 'media/com_finder/js/autocompleter.js');
                     if (MageBridgeHelper::isJoomla25()) {
                         $mootools_scripts[] = 'media/system/js/caption.js';
                     }
                     if (preg_match('/mootools/', $script)) {
                         $remove = true;
                     }
                     foreach ($mootools_scripts as $js) {
                         if (preg_match('/' . str_replace('/', '\\/', $js) . '$/', $script)) {
                             $remove = true;
                         }
                     }
                 }
             }
             // Remove this script
             if ($remove) {
                 // Decide how to remove the scripts
                 $filter = $this->getParam('filter_js', 'foo');
                 // Remove the script entirely from the page
                 if ($filter == 'remove') {
                     $body = str_replace($tag . "\n", '', $body);
                     $body = str_replace($tag, '', $body);
                     // Comment the tag
                 } else {
                     if ($filter == 'comment') {
                         if (!in_array($tag, $commented)) {
                             $commented[] = $tag;
                             $body = str_replace($tag, '<!-- MB: ' . $tag . ' -->', $body);
                         }
                         // Replace the script with the foo-script
                     } else {
                         $this->console[] = 'MageBridge removed ' . $script;
                         $body = str_replace($script, $foo_script, $body);
                     }
                 }
             }
         }
         // Log to the JavaScript Console
         if (MagebridgeModelDebug::isDebug() == true && $this->loadConfig('debug_console') == 1) {
             $console = '';
             foreach ($this->console as $c) {
                 $console .= 'console.warn("' . $c . '");';
             }
             $script = "<script type=\"text/javascript\">\n" . $console . "\n</script>";
             $body = str_replace('<head>', '<head>' . $script, $body);
         }
         // Set the body
         JResponse::setBody($body);
     } else {
         // Add FrotoType to the page
         if ($disableJsFrototype == 0) {
             $body = JResponse::getBody();
             $frototype_tag = '<script type="text/javascript" src="' . $frototype_script . '"></script>';
             $body = preg_replace('/\\<script/', $frototype_tag . "\n" . '<script ', $body, 1);
             JResponse::setBody($body);
         }
     }
 }
 public function setHeaders($type = null)
 {
     return MageBridgeModelBridgeHeaders::getInstance()->setHeaders($type);
 }
 public static function loadPrototype()
 {
     return MageBridgeModelBridgeHeaders::loadPrototype();
 }