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