/** * Method to correct the request URI depending on what is asked * * @param array $data * @return array */ private function doCorrectRequest($data) { // If the data do not include any visual elements, setting the Request URI is not needed $url_needed = false; foreach ($data as $index => $segment) { if ($index == 'breadcrumbs' || $index == 'headers' || $segment['name'] == 'block') { $url_needed = true; break; } } // Reset the Request URI if it is not needed if ($url_needed == false && isset($data['meta']['arguments']['request_uri'])) { // Change the META-data $data['meta']['arguments']['request_uri'] = '/'; // Correct the helper so it returns the new value MageBridgeUrlHelper::setRequest('/'); // Reset the proxy to make sure 404s or other status codes are not preventing us from building the bridge again MageBridgeModelProxy::getInstance()->reset(); } return $data; }
/** * Download a specific package using the MageBridge Proxy (CURL-based) * * @param string $url * @param string $target * @return string */ public static function downloadPackage($url, $target = false) { $app = JFactory::getApplication(); // Open the remote server socket for reading $proxy = MageBridgeModelProxy::getInstance(); $data = $proxy->getRemote($url, null, 'get', false); if (empty($data)) { JError::raiseWarning(42, JText::_('REMOTE_DOWNLOAD_FAILED') . ', ' . $error); return false; } // Set the target path if not given $config = JFactory::getConfig(); if (!$target) { $target = $config->get('tmp_path') . '/' . JInstallerHelper::getFilenameFromURL($url); } else { $target = $config->get('tmp_path') . '/' . basename($target); } // Write received data to file file_put_contents($target, $data); // Return the name of the downloaded package return basename($target); }
protected function fetchContent($label, $url, $params) { // Initialize the proxy $proxy = MageBridgeModelProxy::getInstance(); $proxy->setAllowRedirects(false); $content = $proxy->getRemote($url, $params, 'post'); // Detect proxy errors $proxy_error = $proxy->getProxyError(); if (!empty($proxy_error)) { die('ERROR: Proxy error: ' . $proxy_error); } // Detect the HTTP status $http_status = $proxy->getHttpStatus(); if ($http_status != 200) { die('ERROR: Encountered a HTTP Status ' . $http_status); } // Parse the content if (!empty($content)) { // Detect HTML-page if (preg_match('/\\<\\/html\\>$/', $content)) { die('ERROR: Data contains HTML not JSON'); } $data = json_decode($content, true); if (!empty($data)) { if (array_key_exists('meta', $data)) { return 'SUCCESS: ' . $label; } die('ERROR: JSON response contains unknown data: ' . var_export($data, true)); } else { die('ERROR: Failed to decode JSON'); } } else { die('ERROR: Empty content'); } }
public function build() { $application = JFactory::getApplication(); $register = MageBridgeModelRegister::getInstance(); $proxy = MageBridgeModelProxy::getInstance(); // Load cached data into the register $register->loadCache(); // Exit immediately if the bridge is set offline if ($this->isOffline()) { MageBridgeModelDebug::getInstance()->error('Bridge is set offline'); return $register->getRegister(); } // Exit immediately if the api_user and api_key are not configured yet if (strlen(MagebridgeModelConfig::load('api_user')) == 0 && strlen(MagebridgeModelConfig::load('api_key')) == 0) { MageBridgeModelDebug::getInstance()->error('No API user or no API key'); return $register->getRegister(); } // Exit if the proxy doesn't work (after 10 proxy-requests) if ($proxy->getCount() > 10) { MageBridgeModelDebug::getInstance()->notice('Too many requests'); return $register->getRegister(); } // Only continue if we have no data yet, or when we're dealing with a new (or empty) register if (count($register->getPendingRegister()) > 0) { // Allow modification before we build the bridge MageBridgeModelDebug::beforeBuild(); //MageBridgeModelDebug::getInstance()->trace('Backtrace', debug_backtrace()); MageBridgeModelDebug::getInstance()->notice('Building bridge for ' . count($register->getPendingRegister()) . ' items'); // Extra debugging options if (!defined('MAGEBRIDGE_MODULEHELPER_OVERRIDE')) { MageBridgeModelDebug::getInstance()->warning('Modulehelper override not active'); } foreach ($register->getPendingRegister() as $segment) { switch ($segment['type']) { case 'api': MageBridgeModelDebug::getInstance()->notice('Pending Segment API resource: ' . $segment['name']); break; case 'block': MageBridgeModelDebug::getInstance()->notice('Pending Segment block: ' . $segment['name']); break; default: $name = isset($segment['name']) ? $segment['name'] : null; $type = isset($segment['type']) ? $segment['type'] : null; if (empty($name)) { MageBridgeModelDebug::getInstance()->notice('Pending Segment: ' . $type); } else { MageBridgeModelDebug::getInstance()->notice('Pending Segment: ' . $type . '/' . $name); } break; } } // Initialize proxy-settings if ($application->isSite() && JRequest::getCmd('option') != 'com_magebridge') { $proxy->setAllowRedirects(false); } else { if ($application->isAdmin() && (JRequest::getCmd('option') != 'com_magebridge' || JRequest::getCmd('view') != 'root')) { $proxy->setAllowRedirects(false); } } // Allow others to hook into this event $this->beforeBuild(); // Get the proxy and push the registry through the proxy //MageBridgeModelDebug::getInstance()->trace( 'Register', $register->getPendingRegister()); MageBridgeModelDebug::getInstance()->notice('HTTP Referer: ' . $this->getHttpReferer()); // Build the bridge through the proxy $data = $proxy->build($register->getPendingRegister()); //MageBridgeModelDebug::getInstance()->trace( 'Bridge-data', $data ); // Set the API-state flag $this->setApiState($proxy->getState()); // Exit, if the result is empty if (empty($data) || !is_array($data)) { return $register->getRegister(); } // Merge the new data with the already existing register $register->merge($data); if (isset($data['meta']['data']['state'])) { $this->setApiState($data['meta']['data']['state']); } if (isset($data['meta']['data']['extra'])) { $this->setApiExtra($data['meta']['data']['extra']); } if (isset($data['meta']['data']['api_session'])) { $this->setApiSession($data['meta']['data']['api_session']); } if (isset($data['meta']['data']['magento_config'])) { $this->setMageConfig($data['meta']['data']['magento_config']); } // Allow others to hook into this event $this->afterBuild(); // Fire all Magento events defined in the incoming bridge-data $this->setEvents(); } //MageBridgeModelDebug::getInstance()->trace('Register data', $register->getRegister()); //MageBridgeModelDebug::getInstance()->trace('Function stack', xdebug_get_function_stack()); MageBridgeModelDebug::getInstance()->getBridgeData(); return $register->getRegister(); }
public function displayResult($tpl) { // Fetch configuration data $url = MagebridgeModelConfig::load('url') . 'magebridge.php'; $host = MagebridgeModelConfig::load('host'); // Do basic resolving on the host if it is not an IP-address if (preg_match('/^([0-9\\.]+)$/', $host) == false) { $host = preg_replace('/\\:[0-9]+$/', '', $host); if (gethostbyname($host) == $host) { die('ERROR: Failed to resolve hostname "' . $host . '" in DNS'); } } // Try to open a socket to port 80 if (@fsockopen($host, 80, $errno, $errmsg, 5) == false) { die('ERROR: Failed to open a connection to host "' . $host . '" on port 80. Perhaps a firewall is in the way?'); } // Try to open a socket to port 443 //if (@fsockopen($host, 443, $errno, $errmsg, 5) == false) { // die('ERROR: Failed to open a socket to host "'.$host.'" on port 443. Perhaps a firewall is in the way?'); //} // Initialize the proxy $proxy = MageBridgeModelProxy::getInstance(); $proxy->setAllowRedirects(false); $content = $proxy->getRemote($url, array('mbtest' => '1'), 'post'); // Detect proxy errors $proxy_error = $proxy->getProxyError(); if (!empty($proxy_error)) { die('ERROR: Proxy error: ' . $proxy_error); } // Detect the HTTP status $http_status = $proxy->getHttpStatus(); if ($http_status != 200) { die('ERROR: Encountered a HTTP Status ' . $http_status); } // Parse the content if (!empty($content)) { // Detect HTML-page if (preg_match('/\\<\\/html\\>$/', $content)) { die('ERROR: Data contains HTML not JSON'); } $data = json_decode($content, true); if (!empty($data)) { if (array_key_exists('mbtest', $data)) { die('SUCCESS: Magento-side of the bridge is available'); } die('ERROR: JSON response contains unknown data: ' . var_export($data, true)); } else { die('ERROR: Failed to decode JSON'); } } else { die('ERROR: Empty content'); } }