public static function get_http($url, $headers = array()) { static $_error2 = FALSE; PKHelper::DebugLogger('START: PKHelper::get_http(' . $url . ',' . print_r($headers, true) . ')'); // class: Context is not loaded when using piwik.php proxy on prestashop 1.4 if (class_exists('Context', FALSE)) { $lng = strtolower(isset(Context::getContext()->language->iso_code) ? Context::getContext()->language->iso_code : 'en'); } else { $lng = 'en'; } $timeout = 5; // should go in module conf if (self::$httpAuthUsername == "" || self::$httpAuthUsername === false) { self::$httpAuthUsername = Configuration::get(PKHelper::CPREFIX . 'PAUTHUSR'); } if (self::$httpAuthPassword == "" || self::$httpAuthPassword === false) { self::$httpAuthPassword = Configuration::get(PKHelper::CPREFIX . 'PAUTHPWD'); } $httpauth_usr = self::$httpAuthUsername; $httpauth_pwd = self::$httpAuthPassword; $use_cURL = (bool) Configuration::get(PKHelper::CPREFIX . 'USE_CURL'); if ($use_cURL === FALSE) { PKHelper::DebugLogger('Using \'file_get_contents\' to fetch remote'); $httpauth = ""; if (!empty($httpauth_usr) && !is_null($httpauth_usr) && $httpauth_usr !== false && (!empty($httpauth_pwd) && !is_null($httpauth_pwd) && $httpauth_pwd !== false)) { $httpauth = "Authorization: Basic " . base64_encode("{$httpauth_usr}:{$httpauth_pwd}") . "\r\n"; } $options = array('http' => array('user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : PKHelper::FAKEUSERAGENT, 'method' => "GET", 'timeout' => $timeout, 'header' => (!empty($headers) ? implode('', $headers) : "Accept-language: {$lng}\r\n") . $httpauth)); $context = stream_context_create($options); PKHelper::DebugLogger('Calling: ' . $url . (!empty($httpauth) ? "\n\t- With Http auth" : "")); $result = @file_get_contents($url, false, $context); if ($result === FALSE) { $http_response = ""; if (isset($http_response_header) && is_array($http_response_header)) { foreach ($http_response_header as $value) { if (preg_match("/^HTTP\\/.*/i", $value)) { $http_response = ':' . $value; } } } PKHelper::DebugLogger('request returned ERROR: http response: ' . $http_response); if (isset($http_response_header)) { PKHelper::DebugLogger('$http_response_header: ' . print_r($http_response_header, true)); } if (!$_error2) { self::$error = sprintf(self::l('Unable to connect to api%s'), " {$http_response}"); self::$errors[] = self::$error; $_error2 = TRUE; PKHelper::DebugLogger('Last error message: ' . self::$error); } } else { PKHelper::DebugLogger('request returned OK'); } PKHelper::DebugLogger('END: PKHelper::get_http(): OK'); return $result; } else { PKHelper::DebugLogger('Using \'cURL\' to fetch remote'); try { $ch = curl_init(); PKHelper::DebugLogger("\t: \$ch = curl_init()"); curl_setopt($ch, CURLOPT_URL, $url); PKHelper::DebugLogger("\t: curl_setopt(\$ch, CURLOPT_URL, {$url})"); !empty($headers) ? curl_setopt($ch, CURLOPT_HTTPHEADER, $headers) : curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept-language: {$lng}\r\n")); PKHelper::DebugLogger("\t: curl_setopt(\$ch, CURLOPT_HTTPHEADER, array(...))"); curl_setopt($ch, CURLOPT_USERAGENT, isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : PKHelper::FAKEUSERAGENT); if (!empty($httpauth_usr) && !is_null($httpauth_usr) && $httpauth_usr !== false && (!empty($httpauth_pwd) && !is_null($httpauth_pwd) && $httpauth_pwd !== false)) { curl_setopt($ch, CURLOPT_USERPWD, $httpauth_usr . ":" . $httpauth_pwd); } curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HTTPGET, 1); // just to be safe curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FAILONERROR, true); if (($return = curl_exec($ch)) === false) { if (!$_error2) { self::$error = curl_error($ch); self::$errors[] = self::$error; $_error2 = TRUE; } $return = false; } curl_close($ch); PKHelper::DebugLogger('END: PKHelper::get_http(): OK'); return $return; } catch (Exception $ex) { self::$errors[] = $ex->getMessage(); PKHelper::DebugLogger('Exception: ' . $ex->getMessage()); PKHelper::DebugLogger('END: PKHelper::get_http(): ERROR'); return false; } } }
/** * Method used when making ajax calls to Piwik API, * this method outputs json data. * * NOTE: only methods defiend in "PKHelper::$acp" can be called */ private function __pkapicall() { $apiMethod = Tools::getValue('pkapicall'); if (method_exists('PKHelper', $apiMethod) && isset(PKHelper::$acp[$apiMethod])) { $required = PKHelper::$acp[$apiMethod]['required']; // $optional = PKHelper::$acp[$apiMethod]['optional']; $order = PKHelper::$acp[$apiMethod]['order']; foreach ($required as $requiredOption) { if (!Tools::getIsset($requiredOption)) { PKHelper::DebugLogger("__pkapicall():\n\t- Required parameter \"" . $requiredOption . '" is missing'); die(Tools::jsonEncode(array('error' => true, 'message' => sprintf($this->l('Required parameter "%s" is missing'), $requiredOption)))); } } foreach ($order as &$value) { if (Tools::getIsset($value)) { $value = Tools::getValue($value); } else { $value = NULL; } } if (Tools::getIsset('httpUser')) { PKHelper::$httpAuthUsername = Tools::getValue('httpUser'); } if (Tools::getIsset('httpPasswd')) { PKHelper::$httpAuthPassword = Tools::getValue('httpPasswd'); } if (Tools::getIsset('piwikhost')) { PKHelper::$piwikHost = Tools::getValue('piwikhost'); } PKHelper::DebugLogger("__pkapicall():\n\t- Call PKHelper::" . $apiMethod); $result = call_user_func_array(array('PKHelper', $apiMethod), $order); if ($result === FALSE) { $lastError = ""; if (!empty(PKHelper::$errors)) { $lastError = "\n" . PKHelper::$error; } die(Tools::jsonEncode(array('error' => TRUE, 'message' => sprintf($this->l('Unknown error occurred%s'), $lastError)))); } else { PKHelper::DebugLogger("__pkapicall():\n\t- All good"); if (is_array($result) && isset($result[0])) { $message = $result; } else { if (is_object($result)) { $message = $result; } else { $message = is_string($result) && !is_bool($result) ? $result : (is_array($result) ? implode(', ', $result) : TRUE); } } if (is_bool($message)) { die(Tools::jsonEncode(array('error' => FALSE, 'message' => $this->l('Successfully Updated')))); } else { die(Tools::jsonEncode(array('error' => FALSE, 'message' => $message))); } } } else { die(Tools::jsonEncode(array('error' => true, 'message' => sprintf($this->l('Method "%s" dos not exists in class PKHelper'), $apiMethod)))); } }