国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
6.9KB

  1. <?php
  2. define("DE_ItemEcode",'Shop_De_');//识别购物车Cookie前缀,非开发人员请不要随意更改!
  3. /**
  4. * 购物车类
  5. *
  6. * @version $Id: shopcar.class.php 2 20:58 2010年7月7日Z tianya $
  7. * @package DedeCMS.Libraries
  8. * @copyright Copyright (c) 2020, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  10. * @link https://www.dedebiz.com
  11. */
  12. // ------------------------------------------------------------------------
  13. /**
  14. * 会员购物车类
  15. *
  16. * @package MemberShops
  17. * @subpackage DedeCMS.Libraries
  18. * @link http://www.dedecms.com
  19. */
  20. class MemberShops
  21. {
  22. var $OrdersId;
  23. var $productsId;
  24. function __construct()
  25. {
  26. $this->OrdersId = $this->getCookie("OrdersId");
  27. if(empty($this->OrdersId))
  28. {
  29. $this->OrdersId = $this->MakeOrders();
  30. }
  31. }
  32. function MemberShops()
  33. {
  34. $this->__construct();
  35. }
  36. /**
  37. * 创建一个专有订单编号
  38. *
  39. * @return string
  40. */
  41. function MakeOrders()
  42. {
  43. $this->OrdersId = 'S-P'.time().'RN'.mt_rand(100,999);
  44. $this->deCrypt($this->saveCookie("OrdersId",$this->OrdersId));
  45. return $this->OrdersId;
  46. }
  47. /**
  48. * 添加一个商品编号及信息
  49. *
  50. * @param string $id 购物车ID
  51. * @param string $value 值
  52. * @return void
  53. */
  54. function addItem($id, $value)
  55. {
  56. $this->productsId = DE_ItemEcode.$id;
  57. $this->saveCookie($this->productsId,$value);
  58. }
  59. /**
  60. * 删去一个带编号的商品
  61. *
  62. * @param string $id 购物车ID
  63. * @return void
  64. */
  65. function delItem($id)
  66. {
  67. $this->productsId = DE_ItemEcode.$id;
  68. setcookie($this->productsId, "", time()-3600000,"/");
  69. }
  70. /**
  71. * 清空购物车商品
  72. *
  73. * @return string
  74. */
  75. function clearItem()
  76. {
  77. foreach($_COOKIE as $key => $vals)
  78. {
  79. if(preg_match('/'.DE_ItemEcode.'/', $key))
  80. {
  81. setcookie($key, "", time()-3600000,"/");
  82. }
  83. }
  84. return 1;
  85. }
  86. /**
  87. * 得到订单记录
  88. *
  89. * @return array
  90. */
  91. function getItems()
  92. {
  93. $Products = array();
  94. foreach($_COOKIE as $key => $vals)
  95. {
  96. if(preg_match("#".DE_ItemEcode."#", $key) && preg_match("#[^_0-9a-z]#", $key))
  97. {
  98. parse_str($this->deCrypt($vals), $arrays);
  99. $values = @array_values($arrays);
  100. if(!empty($values))
  101. {
  102. $arrays['price'] = sprintf("%01.2f", $arrays['price']);
  103. if($arrays['buynum'] < 1)
  104. {
  105. $arrays['buynum'] = 0;
  106. }
  107. $Products[$key] = $arrays;
  108. }
  109. }
  110. }
  111. unset($key,$vals,$values,$arrays);
  112. return $Products;
  113. }
  114. /**
  115. * 得到指定商品信息
  116. *
  117. * @param string $id 购物车ID
  118. * @return array
  119. */
  120. function getOneItem($id)
  121. {
  122. $key = DE_ItemEcode.$id;
  123. if(!isset($_COOKIE[$key]) && empty($_COOKIE[$key]))
  124. {
  125. return '';
  126. }
  127. $itemValue = $_COOKIE[$key];
  128. parse_str($this->deCrypt($itemValue), $Products);
  129. unset($key,$itemValue);
  130. return $Products;
  131. }
  132. /**
  133. * 获得购物车中的商品数
  134. *
  135. * @return int
  136. */
  137. function cartCount()
  138. {
  139. $Products = $this->getItems();
  140. $itemsCount = count($Products);
  141. $i = 0;
  142. if($itemsCount > 0)
  143. {
  144. foreach($Products as $val)
  145. {
  146. $i = $i+$val['buynum'];
  147. }
  148. }
  149. unset($Products,$val,$itemsCount);
  150. return $i;
  151. }
  152. /**
  153. * 获得购物车中的总金额
  154. *
  155. * @return string
  156. */
  157. function priceCount()
  158. {
  159. $price = 0.00;
  160. foreach($_COOKIE as $key => $vals)
  161. {
  162. if(preg_match("/".DE_ItemEcode."/", $key))
  163. {
  164. $Products = $this->getOneItem(str_replace(DE_ItemEcode,"",$key));
  165. if($Products['buynum'] > 0 && $Products['price'] > 0)
  166. {
  167. $price = $price + ($Products['price']*$Products['buynum']);
  168. }
  169. }
  170. }
  171. unset($key,$vals,$Products);
  172. return sprintf("%01.2f", $price);
  173. }
  174. //加密接口字符
  175. function enCrypt($txt)
  176. {
  177. return $this->mchStrCode($txt);
  178. }
  179. //解密接口字符串
  180. function deCrypt($txt)
  181. {
  182. return $this->mchStrCode($txt,'DECODE');
  183. }
  184. function mchStrCode($string, $operation = 'ENCODE')
  185. {
  186. $key_length = 4;
  187. $expiry = 0;
  188. $key = md5($GLOBALS['cfg_cookie_encode']);
  189. $fixedkey = md5($key);
  190. $egiskeys = md5(substr($fixedkey, 16, 16));
  191. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  192. $keys = md5(substr($runtokey, 0, 16) . substr($fixedkey, 0, 16) . substr($runtokey, 16) . substr($fixedkey, 16));
  193. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16) . $string : base64_decode(substr($string, $key_length));
  194. $i = 0; $result = '';
  195. $string_length = strlen($string);
  196. for ($i = 0; $i < $string_length; $i++){
  197. $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
  198. }
  199. if($operation == 'ENCODE') {
  200. return $runtokey . str_replace('=', '', base64_encode($result));
  201. } else {
  202. if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
  203. return substr($result, 26);
  204. } else {
  205. return '';
  206. }
  207. }
  208. }
  209. //串行化数组
  210. function enCode($array)
  211. {
  212. $arrayenc = array();
  213. foreach($array as $key => $val)
  214. {
  215. $arrayenc[] = $key.'='.urlencode($val);
  216. }
  217. return implode('&', $arrayenc);
  218. }
  219. //创建加密的_cookie
  220. function saveCookie($key,$value)
  221. {
  222. if(is_array($value))
  223. {
  224. $value = $this->enCrypt($this->enCode($value));
  225. }
  226. else
  227. {
  228. $value = $this->enCrypt($value);
  229. }
  230. setcookie($key,$value,time()+36000,'/');
  231. }
  232. //获得解密的_cookie
  233. function getCookie($key)
  234. {
  235. if(isset($_COOKIE[$key]) && !empty($_COOKIE[$key]))
  236. {
  237. return $this->deCrypt($_COOKIE[$key]);
  238. }
  239. }
  240. }