PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/SnS_connector/func.php

https://github.com/gobsInternetTechnologyGmbH/private_sales
PHP | 296 lines | 198 code | 49 blank | 49 comment | 44 complexity | fc0cfbf50c65a0ccea51fe3aaadbf616 MD5 | raw file
  1. <?php
  2. /* vim: set ts=4 sw=4 sts=4 et: */
  3. /*****************************************************************************\
  4. +-----------------------------------------------------------------------------+
  5. | X-Cart |
  6. | Copyright (c) 2001-2011 Ruslan R. Fazlyev <rrf@x-cart.com> |
  7. | All rights reserved. |
  8. +-----------------------------------------------------------------------------+
  9. | PLEASE READ THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
  10. | FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE |
  11. | AT THE FOLLOWING URL: http://www.x-cart.com/license.php |
  12. | |
  13. | THIS AGREEMENT EXPRESSES THE TERMS AND CONDITIONS ON WHICH YOU MAY USE |
  14. | THIS SOFTWARE PROGRAM AND ASSOCIATED DOCUMENTATION THAT RUSLAN R. |
  15. | FAZLYEV (hereinafter referred to as "THE AUTHOR") IS FURNISHING OR MAKING |
  16. | AVAILABLE TO YOU WITH THIS AGREEMENT (COLLECTIVELY, THE "SOFTWARE"). |
  17. | PLEASE REVIEW THE TERMS AND CONDITIONS OF THIS LICENSE AGREEMENT |
  18. | CAREFULLY BEFORE INSTALLING OR USING THE SOFTWARE. BY INSTALLING, |
  19. | COPYING OR OTHERWISE USING THE SOFTWARE, YOU AND YOUR COMPANY |
  20. | (COLLECTIVELY, "YOU") ARE ACCEPTING AND AGREEING TO THE TERMS OF THIS |
  21. | LICENSE AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY THIS |
  22. | AGREEMENT, DO NOT INSTALL OR USE THE SOFTWARE. VARIOUS COPYRIGHTS AND |
  23. | OTHER INTELLECTUAL PROPERTY RIGHTS PROTECT THE SOFTWARE. THIS |
  24. | AGREEMENT IS A LICENSE AGREEMENT THAT GIVES YOU LIMITED RIGHTS TO USE |
  25. | THE SOFTWARE AND NOT AN AGREEMENT FOR SALE OR FOR TRANSFER OF TITLE.|
  26. | THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT. |
  27. | |
  28. | The Initial Developer of the Original Code is Ruslan R. Fazlyev |
  29. | Portions created by Ruslan R. Fazlyev are Copyright (C) 2001-2011 |
  30. | Ruslan R. Fazlyev. All Rights Reserved. |
  31. +-----------------------------------------------------------------------------+
  32. \*****************************************************************************/
  33. /**
  34. * Functions for the SnS connector module
  35. *
  36. * @category X-Cart
  37. * @package X-Cart
  38. * @subpackage Modules
  39. * @author Ruslan R. Fazlyev <rrf@x-cart.com>
  40. * @copyright Copyright (c) 2001-2011 Ruslan R. Fazlyev <rrf@x-cart.com>
  41. * @license http://www.x-cart.com/license.php X-Cart license agreement
  42. * @version $Id: func.php,v 1.49.2.1 2011/01/10 13:12:02 ferz Exp $
  43. * @link http://www.x-cart.com/
  44. * @see ____file_see____
  45. */
  46. if ( !defined('XCART_START') ) { header("Location: ../../"); die("Access denied"); }
  47. /**
  48. * Functions for SnS connector module
  49. */
  50. define('PERSONALIZE_CLIENT_ID', 'personal_client_id');
  51. function func_generate_sns_action($action, $param = false, $is_pure = false)
  52. {
  53. global $sql_tbl, $config, $active_modules, $http_location, $sns_ext_actions;
  54. x_load(
  55. 'http',
  56. 'user',
  57. 'category'
  58. );
  59. $actions = array("AddToCart","DeleteFromCart","CartChanged","FeaturedProductSelected","BestsellerSelected","Order",'Register','Login','FillContactForm','SiteSearch','AdvancedSearch','ViewLegalInfo','ViewHelp','AddToWishList','WriteReview');
  60. if (empty($action) || empty($config['SnS_connector']['sns_collector_path_url_http']) || (!in_array($action, $actions) && !isset($sns_ext_actions[$action])))
  61. return false;
  62. $host = @parse_url($config['SnS_connector']['sns_collector_path_url_http']."/event.".$config['SnS_connector']['sns_script_extension']);
  63. if (empty($host['host']) || empty($host['path']))
  64. return false;
  65. $ts = XC_TIME;
  66. $post = array();
  67. $chain = array($action);
  68. foreach ($chain as $action) {
  69. $cpost = '';
  70. switch ($action) {
  71. case 'AddToCart':
  72. case 'DeleteFromCart':
  73. if ($param === false) {
  74. global $productid;
  75. }
  76. else {
  77. $productid = $param;
  78. }
  79. $tmp = func_sns_select_product($productid);
  80. if (empty($tmp))
  81. return false;
  82. if ($is_pure) {
  83. $cpost = $tmp;
  84. break;
  85. }
  86. $post[] = "name=$action&".$tmp;
  87. $action = 'CartChanged';
  88. /* FALL-THROUGH */
  89. case 'CartChanged':
  90. global $cart;
  91. $cpost = "itemsCount=".((empty($cart['products']) || !is_array($cart['products'])) ? 0 : intval(@count($cart['products'])))."&total=".price_format($cart['total_cost']);
  92. break;
  93. case 'BestsellerSelected':
  94. case 'AddToWishList':
  95. case 'FeaturedProductSelected':
  96. if ($param === false) {
  97. global $productid;
  98. }
  99. else {
  100. $productid = $param;
  101. }
  102. $cpost = func_sns_select_product($productid);
  103. if (empty($cpost))
  104. return false;
  105. break;
  106. case 'Order':
  107. if ($param === false) {
  108. global $orderid;
  109. }
  110. else {
  111. $orderid = $param;
  112. }
  113. x_load('order');
  114. $order = func_order_data($orderid);
  115. if (empty($order) || !in_array($order['order']['status'], array("P","C")) || (empty($order['products']) && empty($order['giftcerts'])))
  116. return false;
  117. $ts = $order['order']['date']-$config["Appearance"]["timezone_offset"];
  118. if (!empty($order['products'])) {
  119. foreach ($order['products'] as $i) {
  120. $tmp = func_sns_select_product($i['productid']);
  121. if (!empty($tmp)) {
  122. $cpost[] = "profile_id=".urlencode($order['userinfo']['login'])."&orderId=$orderid&total=".round($i['amount']*$i['price'], 2)."&billing_country=".urlencode($order['userinfo']['b_countryname'])."&billing_city=".urlencode($order['userinfo']['b_city'])."&billing_company=".urlencode($order['userinfo']['company'])."&billing_fax=".urlencode($order['userinfo']['fax'])."&billing_phone=".urlencode($order['userinfo']['phone'])."&billing_address=".urlencode($order['userinfo']['b_address'])."&billing_state=".urlencode($order['userinfo']['b_statename'])."&billing_zipcode=".urlencode($order['userinfo']['b_zipcode'])."&billing_firstname=".urlencode($order['userinfo']['firstname'])."&billing_lastname=".urlencode($order['userinfo']['lastname'])."&email=".urlencode($order['userinfo']['email'])."&quantity=$i[amount]&".$tmp;
  123. }
  124. }
  125. }
  126. if (!empty($order['giftcerts'])) {
  127. foreach ($order['giftcerts'] as $i) {
  128. $cpost[] = "profile_id=".urlencode($order['userinfo']['login'])."&orderId=$orderid&total=".$i['amount']."&billing_country=".urlencode($order['userinfo']['b_countryname'])."&billing_city=".urlencode($order['userinfo']['b_city'])."&billing_company=".urlencode($order['userinfo']['company'])."&billing_fax=".urlencode($order['userinfo']['fax'])."&billing_phone=".urlencode($order['userinfo']['phone'])."&billing_address=".urlencode($order['userinfo']['b_address'])."&billing_state=".urlencode($order['userinfo']['b_statename'])."&billing_zipcode=".urlencode($order['userinfo']['b_zipcode'])."&billing_firstname=".urlencode($order['userinfo']['firstname'])."&billing_lastname=".urlencode($order['userinfo']['lastname'])."&email=".urlencode($order['userinfo']['email'])."&quantity=1&productId=$i[gcid]&productName=GIFT CERTIFICATE&categoryName=";
  129. }
  130. }
  131. break;
  132. case 'Register':
  133. global $uname, $usertype;
  134. $userinfo = func_userinfo($uname, $usertype);
  135. /* FALL-THROUGH */
  136. case 'Login':
  137. if (!isset($userinfo)) {
  138. global $logged_userid, $login_type;
  139. $userinfo = func_userinfo($logged_userid, $login_type);
  140. }
  141. $cpost = "profile_id=$userinfo[login]&billing_country=".urlencode($userinfo['b_country'])."&billing_city=".urlencode($userinfo['b_city'])."&billing_company=".urlencode($userinfo['company'])."&billing_fax=".urlencode($userinfo['fax'])."&billing_phone=".urlencode($userinfo['phone'])."&billing_address=".urlencode($userinfo['b_address'])."&billing_state=".urlencode($userinfo['b_state'])."&billing_zipcode=".urlencode($userinfo['b_zipcode'])."&billing_firstname=".urlencode($userinfo['firstname'])."&billing_lastname=".urlencode($userinfo['lastname'])."&email=".urlencode($userinfo['email']);
  142. break;
  143. case 'FillContactForm':
  144. global $contact, $body;
  145. $cpost = "billing_country=".urlencode($contact['b_country'])."&billing_city=".urlencode($contact['b_city'])."&billing_company=".urlencode($contact['company'])."&billing_fax=".urlencode($contact['fax'])."&billing_phone=".urlencode($contact['phone'])."&billing_address=".urlencode($contact['b_address'])."&billing_state=".urlencode($contact['b_state'])."&billing_zipcode=".urlencode($contact['b_zipcode'])."&billing_firstname=".urlencode($contact['firstname'])."&billing_lastname=".urlencode($contact['lastname'])."&email=".urlencode($contact['email'])."&enquiry=".urlencode($body);
  146. break;
  147. case 'SiteSearch':
  148. global $posted_data;
  149. $cpost = "searchPhrase=".urlencode($posted_data['substring']);
  150. break;
  151. case 'AdvancedSearch':
  152. global $posted_data;
  153. $post[] = "name=SiteSearch&searchPhrase=".urlencode($posted_data['substring']);
  154. $cat = '';
  155. if (!empty($posted_data['categoryid'])) {
  156. $cat = func_get_category_path($posted_data['categoryid'], 'category', true);
  157. }
  158. $cpost = "searchPhrase=".urlencode($posted_data['substring'])."&categoryName=".urlencode($cat);
  159. break;
  160. case 'ViewLegalInfo':
  161. if ($param === false) {
  162. global $section;
  163. $cpost = "pageName=".urlencode($section);
  164. }
  165. else {
  166. $cpost = "pageName=".urlencode($param);
  167. }
  168. break;
  169. case 'ViewHelp':
  170. global $current_location, $REQUEST_URI;
  171. $tmp = @parse_url($current_location);
  172. $cpost = "pageUrl=".urlencode($tmp['scheme']."://".$tmp['host'].$REQUEST_URI);
  173. break;
  174. case 'WriteReview':
  175. global $review_message, $productid;
  176. $cpost = func_sns_select_product($productid);
  177. if (empty($cpost))
  178. return false;
  179. $cpost .= "&reviewText=".urlencode(stripslashes($review_message));
  180. break;
  181. default:
  182. if (isset($sns_ext_actions[$action]) && !empty($sns_ext_actions[$action]) && function_exists($sns_ext_actions[$action])) {
  183. if (!$sns_ext_actions[$action]($cpost, $param))
  184. continue;
  185. } else {
  186. continue;
  187. }
  188. }
  189. if (is_array($cpost)) {
  190. foreach ($cpost as $cp) {
  191. $post[] = "name=$action&".$cp;
  192. }
  193. }
  194. else {
  195. $post[] = "name=$action&".$cpost;
  196. }
  197. }
  198. if (empty($post))
  199. return false;
  200. $static_post = "clientId=".func_get_sns_client_id()."&sessionId=$_COOKIE[personal_session_id]&timestamp=".$ts."&shopDisplayName=".urlencode($config['SnS_connector']['sns_shop_display_name'])."&passphrase=".urlencode($config['SnS_connector']['sns_passphrase'])."&site=".urlencode($http_location);
  201. foreach ($post as $k => $v) {
  202. if (empty($v)) {
  203. unset($post[$k]);
  204. continue;
  205. }
  206. $post[$k] = $static_post."&".urlencode("actions[$k]")."=".urlencode($v);
  207. }
  208. list($head, $res) = func_http_post_request($host['host'], $host['path'], implode("&",$post));
  209. return (strpos($head['ERROR'],"200") !== false && strpos($res,"External event registered") !== false);
  210. }
  211. function func_sns_select_product($productid)
  212. {
  213. global $sql_tbl;
  214. x_load('category');
  215. $tmp = func_query_first("SELECT $sql_tbl[products].product, $sql_tbl[categories].categoryid FROM $sql_tbl[products], $sql_tbl[products_categories], $sql_tbl[categories] WHERE $sql_tbl[products].productid = '$productid' AND $sql_tbl[products].productid = $sql_tbl[products_categories].productid AND $sql_tbl[products_categories].main = 'Y' AND $sql_tbl[products_categories].categoryid = $sql_tbl[categories].categoryid");
  216. if (empty($tmp))
  217. return false;
  218. $cats = func_get_category_path($posted_data['categoryid'], 'category', true);
  219. return "productId=$productid&productName=".urlencode($tmp['product'])."&categoryName=".urlencode($cats);
  220. }
  221. function func_get_sns_client_id()
  222. {
  223. $client_id = (int)$_COOKIE[constant('PERSONALIZE_CLIENT_ID')];
  224. if (!empty($client_id)) {
  225. return $client_id;
  226. }
  227. $remote_addr = $_SERVER['REMOTE_ADDR'];
  228. $forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'];
  229. if (!empty($forwarded_for)) {
  230. $remote_addr = substr($forwarded_for.", ".$remote_addr, 0, 255);
  231. }
  232. $accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  233. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  234. return func_xor(func_xor(crc32($remote_addr),crc32($accept_language)), crc32($user_agent));
  235. }
  236. function func_sns_exec_actions(&$sns_actions)
  237. {
  238. if (empty($sns_actions))
  239. return false;
  240. foreach ($sns_actions as $a => $v) {
  241. foreach ($v as $v2) {
  242. func_generate_sns_action($a, $v2);
  243. }
  244. }
  245. $sns_actions = array();
  246. }
  247. ?>