/** * Event onAfterRender * * @access public * @param null * @return null */ public function onAfterRender() { // Don't do anything if MageBridge is not enabled if ($this->isEnabled() == false) { return false; } if (JRequest::getCmd('option') == 'com_zoo') { $body = JResponse::getBody(); // Check for Magento CMS-tags if (preg_match('/\\{\\{([^}]+)\\}\\}/', $body)) { // Get system variables $bridge = MageBridgeModelBridge::getInstance(); // Include the MageBridge register $key = md5(var_export($body, true)) . ':' . JRequest::getCmd('option') . ':' . $row->id; $text = MageBridgeEncryptionHelper::base64_encode($body); // Conditionally load CSS if ($this->getParams()->get('load_css') == 1 || $this->getParams()->get('load_js') == 1) { $bridge->register('headers'); } // Build the bridge $segment_id = $bridge->register('filter', $key, $text); $bridge->build(); // Load CSS if needed if ($this->getParams()->get('load_css') == 1) { $bridge->setHeaders('css'); } // Load JavaScript if needed if ($this->getParams()->get('load_js') == 1) { $bridge->setHeaders('js'); } // Get the result from the bridge $result = $bridge->getSegmentData($segment_id); $result = MageBridgeEncryptionHelper::base64_decode($result); // Only replace the original if the new content exists if (!empty($result)) { $body = $result; } } if (!empty($body)) { JResponse::setBody($body); } } }
/** * Event onContentPrepare * * @param string $context * @param object $row * @param JRegistry $params * @param mixed $page * * @return bool */ public function onContentPrepare($context, $row, $params, $page) { // Do not continue if not enabled if ($this->isEnabled() == false) { return false; } // Check for Magento CMS-tags if (!empty($row->text) && preg_match('/{{([^}]+)}}/', $row->text)) { // Get system variables $bridge = MageBridgeModelBridge::getInstance(); // Include the MageBridge register $option = JFactory::getApplication()->input->getCmd('option'); $key = md5(var_export($row, true)) . ':' . $option; $text = MageBridgeEncryptionHelper::base64_encode($row->text); // Conditionally load CSS if ($this->params->get('load_css') == 1 || $this->params->get('load_js') == 1) { $bridge->register('headers'); } // Build the bridge $segment_id = $bridge->register('filter', $key, $text); $bridge->build(); // Load CSS if needed if ($this->params->get('load_css') == 1) { $bridge->setHeaders('css'); } // Load JavaScript if needed if ($this->params->get('load_js') == 1) { $bridge->setHeaders('js'); } // Get the result from the bridge $result = $bridge->getSegmentData($segment_id); $result = MageBridgeEncryptionHelper::base64_decode($result); // Only replace the original if the new content exists if (!empty($result)) { $row->text = $result; } } }
public static function decrypt($data) { // Don't do anything with empty data $data = trim($data); if (empty($data) || is_string($data) == false && is_numeric($data) == false) { return null; } // Detect data that is not encrypted if (strstr($data, '|=|') == false) { return $data; } $array = explode('|=|', $data); $encrypted = MageBridgeEncryptionHelper::base64_decode($array[0], true); $key = MageBridgeEncryptionHelper::getSaltKey($array[1]); $iv = substr($key, 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB)); try { $decrypted = mcrypt_cfb(MCRYPT_CAST_256, $key, $encrypted, MCRYPT_DECRYPT, $iv); $decrypted = trim($decrypted); return $decrypted; } catch (Exception $e) { Mage::getSingleton('magebridge/debug')->error("Error while decrypting: " . $e->getMessage()); return null; } }
public static function filterContent($content) { // Allow to disable this filtering if (MagebridgeModelConfig::load('filter_content') == 0) { return $content; } // Get common variables $bridge = MageBridgeModelBridge::getInstance(); // Convert all remaining Magento links to Joomla! links $content = str_replace($bridge->getMagentoUrl() . 'index.php/', $bridge->getJoomlaBridgeUrl(), $content); $content = str_replace($bridge->getMagentoUrl() . 'magebridge.php/', $bridge->getJoomlaBridgeUrl(), $content); // Implement a very dirty hack because PayPal converts URLs "&" to "and" $current = MageBridgeUrlHelper::current(); if (strstr($current, 'paypal') && strstr($current, 'redirect')) { // Try to find the distorted URLs $matches = array(); if (preg_match_all('/([^\\"\']+)com_magebridgeand([^\\"\']+)/', $content, $matches)) { foreach ($matches[0] as $match) { // Replace the wrong "and" words with "&" again $url = str_replace('com_magebridgeand', 'com_magebridge&', $match); $url = str_replace('rootand', 'root&', $url); // Replace the wrong URL with its correction $content = str_replace($match, $url, $content); } } } // Replace all uenc-URLs from Magento with URLs parsed through JRoute $matches = array(); $replaced = array(); if (preg_match_all('/\\/uenc\\/([a-zA-Z0-9\\-\\_\\,]+)/', $content, $matches)) { foreach ($matches[1] as $match) { // Decode the match $original_url = MageBridgeEncryptionHelper::base64_decode($match); $url = $original_url; $url = MageBridgeUrlHelper::stripUrl($url); // Convert the non-SEF URL to a SEF URL if (preg_match('/^index.php\\?option=com_magebridge/', $url)) { // Parse the URL but do NOT turn it into SEF because of Mage_Core_Controller_Varien_Action::_isUrlInternal() $url = MageBridgeHelper::filterUrl(str_replace('/', urldecode('/'), $url), false); $url = $bridge->getJoomlaBridgeSefUrl($url); } else { if (!preg_match('/^(http|https)/', $url)) { $url = $bridge->getJoomlaBridgeSefUrl($url); } $url = preg_replace('/\\?SID=([a-zA-Z0-9\\-\\_]{12,42})/', '', $url); } // Extra check on HTTPS if (JURI::getInstance()->isSSL() == true) { $url = str_replace('http://', 'https://', $url); } else { $url = str_replace('https://', 'http://', $url); } // Replace the URL in the content if ($original_url != $url && $original_url . '/' != $url && !in_array($match, $replaced)) { MageBridgeModelDebug::getInstance()->notice('Translating uenc-URL from ' . $original_url . ' to ' . $url); $base64_url = MageBridgeEncryptionHelper::base64_encode($url); $content = str_replace($match, $base64_url, $content); $replaced[] = $match; } } } // Match all URLs and filter them $matches = array(); if (preg_match_all('/index.php\\?option=com_magebridge([^\'\\"\\<]+)([\'\\"\\<]{1})/', $content, $matches)) { for ($i = 0; $i < count($matches[0]); $i++) { $oldurl = 'index.php?option=com_magebridge' . $matches[1][$i]; $end = $matches[2][$i]; $newurl = MageBridgeHelper::filterUrl($oldurl); if (!empty($newurl)) { $content = str_replace($oldurl . $end, $newurl . $end, $content); } } } // Clean-up left-overs $content = str_replace('?___SID=U', '', $content); $content = str_replace('?___SID=S', '', $content); $content = preg_replace('/\\?___store=([a-zA-Z0-9]+)/', '', $content); $content = preg_replace('/\\?SID=([a-zA-Z0-9\\-\\_]{12,42})/', '', $content); // Remove double-slashes //$basedir = preg_replace('/^([\/]?)(.*)([\/]?)$/', '\2', JURI::base(true)); //$content = str_replace(JURI::base().$basedir, JURI::base(), $content); $content = str_replace(JURI::base() . '/', JURI::base(), $content); // Adjust wrong media-URLs if (JURI::getInstance()->isSSL() == true) { $non_https = preg_replace('/^https:/', 'http:', $bridge->getMagentoUrl()); $https = preg_replace('/^http:/', 'https:', $bridge->getMagentoUrl()); $content = str_replace($non_https, $https, $content); } // Adjust incorrect URLs with parameters starting with & if (preg_match_all('/(\'|\\")(http|https):\\/\\/([^\\&\\?\'\\"]+)\\&/', $content, $matches)) { foreach ($matches[0] as $index => $match) { $content = str_replace($matches[3][$index] . '&', $matches[3][$index] . '?', $content); } } return $content; }
/** * Decrypt data after encryption * * @param string $data * @return mixed */ public static function decrypt($data) { // Don't do anything with empty data $data = trim($data); if (empty($data) || is_string($data) == false && is_numeric($data) == false) { return null; } // Detect data that is not encrypted $data = urldecode($data); if (strstr($data, '|=|') == false) { return $data; } $array = explode('|=|', $data); $encrypted = MageBridgeEncryptionHelper::base64_decode($array[0], true); $key = MageBridgeEncryptionHelper::getSaltedKey($array[1]); // PHP 5.5 version if (version_compare(PHP_VERSION, '5.5.0') >= 0) { try { $td = mcrypt_module_open(MCRYPT_CAST_256, '', 'ecb', ''); $iv = substr($key, 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB)); mcrypt_generic_init($td, $key, $iv); $decrypted = mdecrypt_generic($td, $encrypted); $decrypted = trim($decrypted); return $decrypted; } catch (Exception $e) { Mage::getSingleton('magebridge/debug')->error("Error while decrypting: " . $e->getMessage()); return null; } } else { try { $iv = substr($key, 0, mcrypt_get_iv_size(MCRYPT_CAST_256, MCRYPT_MODE_CFB)); $decrypted = @mcrypt_cfb(MCRYPT_CAST_256, $key, $encrypted, MCRYPT_DECRYPT, $iv); $decrypted = trim($decrypted); return $decrypted; } catch (Exception $e) { Mage::getSingleton('magebridge/debug')->error("Error while decrypting: " . $e->getMessage()); return null; } } }
public function getHttpReferer() { if (preg_match('/\\/(uenc|referer)\\/([a-zA-Z0-9\\,]+)/', JURI::current(), $match)) { $this->_http_referer = MageBridgeEncryptionHelper::base64_decode($match[2]); // If this is a MageBridge page, use it only if its not a customer-page, or homepage } else { if (preg_match('/\\/customer\\/account\\//', JURI::current()) == false && preg_match('/\\/persistent\\/index/', JURI::current()) == false && preg_match('/\\/checkout\\/cart/', JURI::current()) == false && JURI::current() != MageBridge::getBridge()->getJoomlaBridgeUrl()) { $this->_http_referer = JURI::getInstance()->toString(); } else { if (empty($this->_http_referer)) { $session = JFactory::getSession(); $this->_http_referer = $session->get('magebridge.http_referer'); } } } //if (empty($this->_http_referer) && isset($_SERVER['HTTP_REFERER'])) { // $this->_http_referer = $_SERVER['HTTP_REFERER']; //} return $this->_http_referer; }
public function decode($block_data) { $block_data = MageBridgeEncryptionHelper::base64_decode($block_data); return $block_data; }
/** * Event onAfterRender * * @access public * @param null * @return null */ public function onAfterRender() { // Don't do anything if MageBridge is not enabled if ($this->isEnabled() == false) { return false; } if (JFactory::getApplication()->input->getCmd('option') == 'com_zoo') { $body = JResponse::getBody(); // Check for Magento CMS-tags if (preg_match('/\\{\\{([^}]+)\\}\\}/', $body) || preg_match('/\\{mb([^}]+)\\}/', $body)) { // Get system variables $bridge = MageBridgeModelBridge::getInstance(); $register = MageBridgeModelRegister::getInstance(); // Detect the request-tag if (preg_match_all('/\\{mbrequest url="([^\\"]+)"\\}/', $body, $matches)) { foreach ($matches[0] as $matchIndex => $match) { $url = $matches[1][$matchIndex]; MageBridgeUrlHelper::setRequest($url); $body = str_replace($match, '', $body); } } // Detect block-names if (preg_match_all('/\\{mbblock name="([^\\"]+)"\\}/', $body, $matches)) { foreach ($matches[0] as $matchIndex => $match) { $block_name = $matches[1][$matchIndex]; $register->add('block', $block_name); } } // Include the MageBridge register $key = md5(var_export($body, true)) . ':' . JFactory::getApplication()->input->getCmd('option'); $text = MageBridgeEncryptionHelper::base64_encode($body); // Conditionally load CSS if ($this->params->get('load_css') == 1 || $this->params->get('load_js') == 1) { $bridge->register('headers'); } // Build the bridge $segment_id = $bridge->register('filter', $key, $text); $bridge->build(); // Load CSS if needed if ($this->params->get('load_css') == 1) { $bridge->setHeaders('css'); } // Load JavaScript if needed if ($this->params->get('load_js') == 1) { $bridge->setHeaders('js'); } // Get the result from the bridge $result = $bridge->getSegmentData($segment_id); $result = MageBridgeEncryptionHelper::base64_decode($result); // Only replace the original if the new content exists if (!empty($result)) { $body = $result; } // Detect block-names if (preg_match_all('/\\{mbblock name="([^\\"]+)"\\}/', $body, $matches)) { foreach ($matches[0] as $matchIndex => $match) { $block_name = $matches[1][$matchIndex]; $block = $bridge->getBlock($block_name); $body = str_replace($match, $block, $body); } } } if (!empty($body)) { JResponse::setBody($body); } } }
/** * Method to load the JavaScript headers * * @param array $headers * @return null */ public function loadJs($headers) { // Dot not load if this is not the right document-class $document = JFactory::getDocument(); if ($document->getType() != 'html') { return false; } // Check whether all scripts are disabled $disable_js = MagebridgeModelConfig::load('disable_js_mage'); if ($disable_js == 'all') { return false; } // Check whether the bridge is offline $offline = MageBridge::getBridge()->isOffline(); if ($offline == true) { return false; } // Initialize the internal array $this->_scripts = array(); // Get system variables $bridge = MageBridge::getBridge(); $html = "<script type=\"text/javascript\">\n" . "//<![CDATA[\n" . "var BLANK_URL = '" . $this->getBaseJsUrl() . "blank.html';\n" . "var BLANK_IMG = '" . $this->getBaseJsUrl() . "spacer.gif';\n" . "//]]>\n" . "</script>\n"; $document->addCustomTag($html); // Load Prototype if ($this->loadPrototype() == true) { $this->_has_prototype = true; } // Loop through all the header-items fetched from Magento if (!empty($headers['items'])) { $jslist = array(); $jstags = array(); foreach ($headers['items'] as $item) { if ($item['type'] == 'skin_js' || $item['type'] == 'js') { if (MageBridgeHelper::jsIsDisabled($item['name']) == true) { continue; } $this->_stylesheets[] = $item['name']; $this->_scripts[] = $item['name']; if (empty($item['name'])) { continue; } // If this is a skin-script, construct the tag but add it later to the HTML-header if ($item['type'] == 'skin_js') { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $jstags[] = $tag; continue; } // If this is a conditional script, construct the tag but add it later to the HTML-header if (!empty($item['if'])) { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $tag = '<!--[if ' . $item['if'] . ' ]>' . "\n" . $tag . '<![endif]-->' . "\n"; $jstags[] = $tag; continue; } // Detect Prototype if (strstr($item['path'], 'prototype') || strstr($item['path'], 'scriptaculous')) { $this->_has_prototype = true; // Load an optimized Prototype/script.acul.us version if (MagebridgeModelConfig::load('use_protoaculous') == 1 || MagebridgeModelConfig::load('use_protoculous') == 1) { $skip_scripts = array('prototype/prototype.js', 'scriptaculous/builder.js', 'scriptaculous/effects.js', 'scriptaculous/dragdrop.js', 'scriptaculous/controls.js', 'scriptaculous/slider.js'); if (in_array($item['name'], $skip_scripts)) { continue; } } // Skip these, if the Google API is already loaded if (MagebridgeModelConfig::load('use_google_api') == 1) { if (preg_match('/prototype.js$/', $item['name'])) { continue; } if (preg_match('/scriptaculous.js$/', $item['name'])) { continue; } } } // Detect jQuery and replace it if (preg_match('/jquery-([0-9]+)\\.([0-9]+)\\.([0-9]+)/', $item['path']) || preg_match('/jquery.js$/', $item['path']) || preg_match('/jquery.min.js$/', $item['path'])) { if (MagebridgeModelConfig::load('replace_jquery') == 1) { MageBridgeTemplateHelper::load('jquery'); continue; } } // Detect the translation script if (strstr($item['name'], 'translate.js')) { $translate = true; } // Load this script through JS merging or not if (MagebridgeModelConfig::load('merge_js') == 1) { $jslist[] = $item['name']; } else { if (MagebridgeModelConfig::load('merge_js') == 2 && !empty($headers['merge_js'])) { // Don't do anything here yet } else { if (!preg_match('/^http/', $item['path'])) { $item['path'] = $bridge->getMagentoUrl() . $item['path']; } $item['path'] = $this->convertUrl($item['path']); $tag = '<script type="text/javascript" src="' . $item['path'] . '"></script>' . "\n"; $jstags[] = $tag; } } } } if (MagebridgeModelConfig::load('merge_js') == 2 && !empty($headers['merge_js'])) { $this->addScript($headers['merge_js']); } else { if (!empty($jslist)) { $this->addScript($this->getBaseJsUrl() . 'index.php?c=auto&f=,' . implode(',', $jslist)); } } if (!empty($jstags)) { foreach ($jstags as $tag) { if (!empty($tag)) { $document->addCustomTag($tag); } } } } // Load some extra JavaScript tags if (isset($headers['custom'])) { foreach ($headers['custom'] as $custom) { $custom = MageBridgeEncryptionHelper::base64_decode($custom); $custom = preg_replace('/Mage.Cookies.domain([^;]+)\\;/m', 'Mage.Cookies.domain = null;', $custom); $document->addCustomTag($custom); } } else { if (isset($translate) && $translate == true) { $html = '<script type="text/javascript">var Translator = new Translate([]);</script>'; $document->addCustomTag($html); } } return; }
/** * Proxy uploads * * @return array */ public function upload() { // Don't do anything outside of the MageBridge component if ($this->app->input->getCmd('option') != 'com_magebridge') { return array(); } // Define some variables $tmpFiles = array(); // Automatically handle file uploads if (!empty($_FILES)) { foreach ($_FILES as $name => $file) { if (empty($file['tmp_name']) || empty($file['name'])) { continue; } // Detect file upload problems $errorMessage = null; switch ($file['error']) { case 1: case 2: $errorMessage = JText::sprintf('Upload of %s exceeded the maximum size [%d]', $file['name'], $file['error']); break; case 3: case 4: case 6: case 7: case 8: $errorMessage = JText::sprintf('Error when uploading file %s [%d]', $file['name'], $file['error']); break; } // @todo: Why re-upload file to Joomla? Why not directly to Magento using tmp file? // Move the uploaded file to the Joomla tmp-directory if (is_readable($file['tmp_name'])) { // Upload the specific file jimport('joomla.filesystem.file'); $tmpFile = $this->getUploadPath() . '/' . $file['name']; JFile::upload($file['tmp_name'], $tmpFile); // Check if the file is there if (!is_file($tmpFile) || !is_readable($tmpFile)) { $errorMessage = JText::sprintf('Unable to read uploaded file %s', $tmpFile); } else { if (!filesize($tmpFile) > 0) { $errorMessage = JText::sprintf('Uploaded file %s is empty', $tmpFile); } else { $file['tmp_name'] = $tmpFile; $tmpFiles[$name] = $file; continue; } } } else { $errorMessage = JText::sprintf('Uploaded file %s is not readable', $file['tmp_name']); } // Handle errors if (!empty($errorMessage)) { // See if we can redirect back to the same old page $request = JFactory::getApplication()->input->getString('request'); if (preg_match('/\\/uenc\\/([a-zA-Z0-9\\,\\-\\_]+)/', $request, $uenc)) { $page = MageBridgeEncryptionHelper::base64_decode($uenc[1]); if (!empty($uenc) && !empty($page)) { // Remove the old file $this->cleanup($tmpFiles); // Redirect to the old page $this->app->redirect($page, $errorMessage, 'error'); $this->app->close(); return array(); } } // If no redirect could be given, do not handle this at all, but just set an error $this->app->enqueueMessage($errorMessage, 'error'); } } } return $tmpFiles; }
/** * Helper-method to get the HTTP Referer to send to Magento * * @return string */ public function getHttpReferer() { // If this is a non-MageBridge page, use it if (JFactory::getApplication()->input->getCmd('option') != 'com_magebridge') { $referer = JURI::getInstance()->toString(); // If the referer is set on the URL, use it also } elseif (preg_match('/\\/(uenc|referer)\\/([a-zA-Z0-9\\,\\_\\-]+)/', JURI::current(), $match)) { $referer = MageBridgeEncryptionHelper::base64_decode($match[2]); // If this is the MageBridge page checkout/cart/updatePost, return to the checkout } else { if (preg_match('/\\/checkout\\/cart\\/([a-zA-Z0-9]+)Post/', JURI::current()) == true) { $referer = MageBridgeUrlHelper::route('checkout/cart'); // If this is a MageBridge page, use it only if its not a customer-page, or homepage } else { if (preg_match('/\\/customer\\/account\\//', JURI::current()) == false && preg_match('/\\/persistent\\/index/', JURI::current()) == false && preg_match('/\\/review\\/product\\/post/', JURI::current()) == false && preg_match('/\\/remove\\/item/', JURI::current()) == false && preg_match('/\\/newsletter\\/subscriber/', JURI::current()) == false && preg_match('/\\/checkout\\/cart/', JURI::current()) == false && $this->isAjax() == false && JURI::current() != $this->getJoomlaBridgeUrl()) { $referer = JURI::getInstance()->toString(); } } } // Load the stored referer from the session if (empty($referer)) { $session = JFactory::getSession(); $referer = $session->get('magebridge.http_referer'); } // Use the default referer if (empty($this->_http_referer)) { if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != JURI::current()) { $referer = $_SERVER['HTTP_REFERER']; } } $this->_http_referer = $referer; return $this->_http_referer; }