PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/php/xuzhou58/xuzhou58.com/include/shopcar.class.php

http://jqbird.googlecode.com/
PHP | 211 lines | 178 code | 17 blank | 16 comment | 20 complexity | 685e8667b429a0aeacb5d3fc747fa448 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. define("DE_ItemEcode",'Shop_De_');//?????Cookie??,????????????!
  3. class MemberShops
  4. {
  5. var $OrdersId;
  6. var $productsId;
  7. //php5????PHP>=5.0
  8. function __construct()
  9. {
  10. $this->OrdersId = $this->getCookie("OrdersId");
  11. if(empty($this->OrdersId))
  12. {
  13. $this->OrdersId = $this->MakeOrders();
  14. }
  15. }
  16. //?????PHP<5.0
  17. function MemberShops()
  18. {
  19. $this->__construct();
  20. }
  21. //??????????
  22. function MakeOrders()
  23. {
  24. $this->OrdersId = 'S-P'.time().'RN'.mt_rand(100,999);
  25. $this->deCrypt($this->saveCookie("OrdersId",$this->OrdersId));
  26. return $this->OrdersId;
  27. }
  28. //???????????
  29. function addItem($id,$value)
  30. {
  31. $this->productsId = DE_ItemEcode.$id;
  32. $this->saveCookie($this->productsId,$value);
  33. }
  34. //??????????
  35. function delItem($id)
  36. {
  37. $this->productsId = DE_ItemEcode.$id;
  38. setcookie($this->productsId, "", time()-3600000,"/");
  39. }
  40. //???????
  41. function clearItem()
  42. {
  43. foreach($_COOKIE as $key => $vals)
  44. {
  45. if(ereg(DE_ItemEcode,$key))
  46. {
  47. setcookie($key, "", time()-3600000,"/");
  48. }
  49. }
  50. return 1;
  51. }
  52. //??????
  53. function getItems()
  54. {
  55. $Products = array();
  56. foreach($_COOKIE as $key => $vals)
  57. {
  58. if(ereg(DE_ItemEcode,$key) && ereg("[^_0-9a-z]",$key))
  59. {
  60. parse_str($this->deCrypt($vals), $arrays);
  61. $values = @array_values($arrays);
  62. if(!empty($values))
  63. {
  64. $arrays['price'] = sprintf("%01.2f", $arrays['price']);
  65. if($arrays['buynum'] < 1)
  66. {
  67. $arrays['buynum'] = 0;
  68. }
  69. $Products[$key] = $arrays;
  70. }
  71. }
  72. }
  73. unset($key,$vals,$values,$arrays);
  74. return $Products;
  75. }
  76. //????????
  77. function getOneItem($id)
  78. {
  79. $key = DE_ItemEcode.$id;
  80. if(!isset($_COOKIE[$key]) && empty($_COOKIE[$key]))
  81. {
  82. return '';
  83. }
  84. $itemValue = $_COOKIE[$key];
  85. parse_str($this->deCrypt($itemValue), $Products);
  86. unset($key,$itemValue);
  87. return $Products;
  88. }
  89. //??????????
  90. function cartCount()
  91. {
  92. $Products = $this->getItems();
  93. $itemsCount = count($Products);
  94. $i = 0;
  95. if($itemsCount > 0)
  96. {
  97. foreach($Products as $val)
  98. {
  99. $i = $i+$val['buynum'];
  100. }
  101. }
  102. unset($Products,$val,$itemsCount);
  103. return $i;
  104. }
  105. //??????????
  106. function priceCount()
  107. {
  108. $price = 0.00;
  109. foreach($_COOKIE as $key => $vals)
  110. {
  111. if(ereg(DE_ItemEcode,$key))
  112. {
  113. $Products = $this->getOneItem(str_replace(DE_ItemEcode,"",$key));
  114. if($Products['buynum'] > 0 && $Products['price'] > 0)
  115. {
  116. $price = $price + ($Products['price']*$Products['buynum']);
  117. }
  118. }
  119. }
  120. unset($key,$vals,$Products);
  121. return sprintf("%01.2f", $price);
  122. }
  123. //??????
  124. function enCrypt($txt)
  125. {
  126. srand((double)microtime() * 1000000);
  127. $encrypt_key = md5(rand(0, 32000));
  128. $ctr = 0;
  129. $tmp = '';
  130. for($i = 0; $i < strlen($txt); $i++)
  131. {
  132. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  133. $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  134. }
  135. return base64_encode($this->setKey($tmp));
  136. }
  137. //???????
  138. function deCrypt($txt)
  139. {
  140. $txt = $this->setKey(base64_decode($txt));
  141. $tmp = '';
  142. for ($i = 0; $i < strlen($txt); $i++)
  143. {
  144. $tmp .= $txt[$i] ^ $txt[++$i];
  145. }
  146. return $tmp;
  147. }
  148. //??????
  149. function setKey($txt)
  150. {
  151. global $cfg_cookie_encode;
  152. $encrypt_key = md5(strtolower($cfg_cookie_encode));
  153. $ctr = 0;
  154. $tmp = '';
  155. for($i = 0; $i < strlen($txt); $i++)
  156. {
  157. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  158. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  159. }
  160. return $tmp;
  161. }
  162. //?????
  163. function enCode($array)
  164. {
  165. $arrayenc = array();
  166. foreach($array as $key => $val)
  167. {
  168. $arrayenc[] = $key.'='.urlencode($val);
  169. }
  170. return implode('&', $arrayenc);
  171. }
  172. //?????_cookie
  173. function saveCookie($key,$value)
  174. {
  175. if(is_array($value))
  176. {
  177. $value = $this->enCrypt($this->enCode($value));
  178. }
  179. else
  180. {
  181. $value = $this->enCrypt($value);
  182. }
  183. setcookie($key,$value,time()+36000,'/');
  184. }
  185. //?????_cookie
  186. function getCookie($key)
  187. {
  188. if(isset($_COOKIE[$key]) && !empty($_COOKIE[$key]))
  189. {
  190. return $this->deCrypt($_COOKIE[$key]);
  191. }
  192. }
  193. }
  194. ?>