国内流行的内容管理系统(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.

241 lines
6.7KB

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