* @copyright 2007-2011 PrestaShop SA : 6 rue lacepede, 75005 PARIS * @version Release: $Revision: 7541 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA **/ /** * @author Nans Pellicari - Prestashop * @version 1.3 */ class PrestashopStats { /** * @var array of urls to use for using method */ private static $arr_ps_stats_url; /** * @var string url of the shop */ private static $site_url; /** * Constructor save all necessaries infos to make transaction with tracker page. * @param string $site_url to identify the shop */ public function __construct($site_url) { if (self::$arr_ps_stats_url === NULL) { self::$arr_ps_stats_url = array(); self::$arr_ps_stats_url['actSubscription'] = 'http://www.prestashop.com/modules/tracker_twenga.php?act_subscription=1'; self::$arr_ps_stats_url['validateSubscription'] = 'http://www.prestashop.com/modules/tracker_twenga.php?validate_subscription=1'; self::$arr_ps_stats_url['cancelOrder'] = 'http://www.prestashop.com/modules/tracker_twenga.php?cancel_order=1'; self::$arr_ps_stats_url['validateOrder'] = 'http://www.prestashop.com/modules/tracker_twenga.php?validate_order=1'; self::$site_url = $site_url; } } /** * Build url for curl use with good params and good encode. * @param string $url url which defined the method to use for the tracker. * @param array $params params to passed for the tracker. */ private static function buildUrlToQuery($url, $params = array()) { $params['url'] = self::$site_url; $params['module'] = 'twenga'; if (Configuration::get('PS_TWENGA_KEY') !== false && Configuration::get('PS_TWENGA_KEY') !== '') $params['key'] = Configuration::get('PS_TWENGA_KEY'); $str_params = http_build_query($params); $str_url = $url.(($str_params !== '') ? '&'.$str_params : ''); return $str_url; } /** * Use cURL to connect with the tracker. * @param string $query_string * @throws Exception if error occurred. * @return array with the statut code and the response return from curl connection. */ private static function executeQuery($query_string) { $defaultParams = array( CURLOPT_HEADER => TRUE, CURLOPT_RETURNTRANSFER => TRUE, CURLINFO_HEADER_OUT => TRUE, CURLOPT_HTTPAUTH => CURLAUTH_BASIC, ); $session = curl_init($query_string); curl_setopt_array($session, $defaultParams); $response = curl_exec($session); $response = explode("\r\n\r\n", $response); $header = $response[0]; $response = $response[1]; $status_code = (int)curl_getinfo($session, CURLINFO_HTTP_CODE); if ($status_code === 0) throw new Exception('CURL Error: '.curl_error($session)); curl_close($session); return array('status_code' => $status_code, 'response' => $response); } public function actSubscription () { $str = self::buildUrlToQuery(self::$arr_ps_stats_url[__FUNCTION__]); $return = $this->executeQuery($str); if (trim($return['response']) !== '' && Validate::isMd5($return['response'])) Configuration::updateValue('PS_TWENGA_KEY', $return['response']); } public function validateSubscription () { $str = self::buildUrlToQuery(self::$arr_ps_stats_url[__FUNCTION__]); $this->executeQuery($str); } public function cancelOrder () { $str = self::buildUrlToQuery(self::$arr_ps_stats_url[__FUNCTION__]); $this->executeQuery($str); } public function validateOrder ($amount_ht, $amount_ttc) { $params = array('order_amount_ttc' => (float)$amount_ttc, 'order_amount_ht' => (float)$amount_ht); $str = self::buildUrlToQuery(self::$arr_ps_stats_url[__FUNCTION__], $params); $this->executeQuery($str); } }