国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

242 рядки
6.8KB

  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. *
  17. * @package MemberShops
  18. * @subpackage DedeBIZ.Libraries
  19. * @link https://www.dedebiz.com
  20. */
  21. class MemberShops
  22. {
  23. var $OrdersId;
  24. var $productsId;
  25. function __construct()
  26. {
  27. $this->OrdersId = $this->getCookie("OrdersId");
  28. if (empty($this->OrdersId)) {
  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. if (preg_match('/'.DE_ItemEcode.'/', $key)) {
  79. setcookie($key, "", time() - 3600000, "/");
  80. }
  81. }
  82. return 1;
  83. }
  84. /**
  85. * 得到订单记录
  86. *
  87. * @return array
  88. */
  89. function getItems()
  90. {
  91. $Products = array();
  92. foreach ($_COOKIE as $key => $vals) {
  93. if (preg_match("#".DE_ItemEcode."#", $key) && preg_match("#[^_0-9a-z]#", $key)) {
  94. parse_str($this->deCrypt($vals), $arrays);
  95. $values = @array_values($arrays);
  96. if (!empty($values)) {
  97. $arrays['price'] = sprintf("%01.2f", $arrays['price']);
  98. if ($arrays['buynum'] < 1) {
  99. $arrays['buynum'] = 0;
  100. }
  101. $Products[$key] = $arrays;
  102. }
  103. }
  104. }
  105. unset($key, $vals, $values, $arrays);
  106. return $Products;
  107. }
  108. /**
  109. * 得到指定商品信息
  110. *
  111. * @param string $id 购物车ID
  112. * @return array
  113. */
  114. function getOneItem($id)
  115. {
  116. $key = DE_ItemEcode.$id;
  117. if (!isset($_COOKIE[$key]) && empty($_COOKIE[$key])) {
  118. return '';
  119. }
  120. $itemValue = $_COOKIE[$key];
  121. parse_str($this->deCrypt($itemValue), $Products);
  122. unset($key, $itemValue);
  123. return $Products;
  124. }
  125. /**
  126. * 获得购物车中的商品数
  127. *
  128. * @return int
  129. */
  130. function cartCount()
  131. {
  132. $Products = $this->getItems();
  133. $itemsCount = count($Products);
  134. $i = 0;
  135. if ($itemsCount > 0) {
  136. foreach ($Products as $val) {
  137. $i = $i + $val['buynum'];
  138. }
  139. }
  140. unset($Products, $val, $itemsCount);
  141. return $i;
  142. }
  143. /**
  144. * 获得购物车中的总金额
  145. *
  146. * @return string
  147. */
  148. function priceCount()
  149. {
  150. $price = 0.00;
  151. foreach ($_COOKIE as $key => $vals) {
  152. if (preg_match("/".DE_ItemEcode."/", $key)) {
  153. $Products = $this->getOneItem(str_replace(DE_ItemEcode, "", $key));
  154. if ($Products['buynum'] > 0 && $Products['price'] > 0) {
  155. $price = $price + ($Products['price'] * $Products['buynum']);
  156. }
  157. }
  158. }
  159. unset($key, $vals, $Products);
  160. return sprintf("%01.2f", $price);
  161. }
  162. //加密接口字符
  163. function enCrypt($txt)
  164. {
  165. return $this->mchStrCode($txt);
  166. }
  167. //解密接口字符串
  168. function deCrypt($txt)
  169. {
  170. return $this->mchStrCode($txt, 'DECODE');
  171. }
  172. function mchStrCode($string, $operation = 'ENCODE')
  173. {
  174. $key_length = 4;
  175. $expiry = 0;
  176. $key = md5($GLOBALS['cfg_cookie_encode']);
  177. $fixedkey = md5($key);
  178. $egiskeys = md5(substr($fixedkey, 16, 16));
  179. $runtokey = $key_length ? ($operation == 'ENCODE' ? substr(md5(microtime(true)), -$key_length) : substr($string, 0, $key_length)) : '';
  180. $keys = md5(substr($runtokey, 0, 16).substr($fixedkey, 0, 16).substr($runtokey, 16).substr($fixedkey, 16));
  181. $string = $operation == 'ENCODE' ? sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$egiskeys), 0, 16).$string : base64_decode(substr($string, $key_length));
  182. $i = 0;
  183. $result = '';
  184. $string_length = strlen($string);
  185. for ($i = 0; $i < $string_length; $i++) {
  186. $result .= chr(ord($string[$i]) ^ ord($keys[$i % 32]));
  187. }
  188. if ($operation == 'ENCODE') {
  189. return $runtokey.str_replace('=', '', base64_encode($result));
  190. } else {
  191. if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$egiskeys), 0, 16)) {
  192. return substr($result, 26);
  193. } else {
  194. return '';
  195. }
  196. }
  197. }
  198. //串行化数组
  199. function enCode($array)
  200. {
  201. $arrayenc = array();
  202. foreach ($array as $key => $val) {
  203. $arrayenc[] = $key.'='.urlencode($val);
  204. }
  205. return implode('&', $arrayenc);
  206. }
  207. //创建加密的_cookie
  208. function saveCookie($key, $value)
  209. {
  210. if (is_array($value)) {
  211. $value = $this->enCrypt($this->enCode($value));
  212. } else {
  213. $value = $this->enCrypt($value);
  214. }
  215. setcookie($key, $value, time() + 36000, '/');
  216. }
  217. //获得解密的_cookie
  218. function getCookie($key)
  219. {
  220. if (isset($_COOKIE[$key]) && !empty($_COOKIE[$key])) {
  221. return $this->deCrypt($_COOKIE[$key]);
  222. }
  223. }
  224. }