-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.php
330 lines (276 loc) · 9.01 KB
/
config.php
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
$g_login_session_key = "LOGIN_SESSION_KEY";
//using mysqli
function getConnection()
{
//Connecting to mysql database
$conn = mysqli_connect('localhost', 'user', 'password', 'dbname')
or die('Error connecting to MySQL server.');
return $conn;
}
///using pdo
function getConnectionPDO()
{
try {
$conn = new PDO("mysql:host=localhost;dbname=dbname", "user", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
return $conn;
}
function CheckLoginSESSION()
{
global $g_login_session_key;
session_start();
$sessionvar = $g_login_session_key;
if (empty($_SESSION[$g_login_session_key]))
{
return false;
}
else
{
return true;
}
}
function CheckLoginInDB($useremail, $userpass)
{
$conn = getConnectionPDO();
if ((strlen($useremail) > 0) && (strlen($userpass) > 0))
{
$sql = " select * from User " .
" where userEmail = :user_email and userPass = :user_pass";
$ps = $conn->prepare($sql);
$ps->bindParam(':user_email', $useremail);
$ps->bindParam(':user_pass', $userpass);
$ps->execute();
$result = $ps->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0) {
return true;
}
}
return false;
}
function constructTable($data, $showHeader, $keyName, $editLink, $deleteLink)
{
print "<table class='table table-hover'>\n";
$doHeader = $showHeader;
foreach ($data as $row)
{
if ($doHeader)
{
print " <tr>\n";
if(!empty($editLink))
print " <th> </th>\n";
foreach ($row as $name => $value)
print " <th>$name</th>\n";
if(!empty($deleteLink))
print " <th> </th>\n";
print " </tr>\n";
$doHeader = false;
}
print " <tr>\n";
$keyValue = "";
if(!empty($keyName))
$keyValue = $row[$keyName];
if(!empty($editLink))
print " <td><a href='". $editLink ."?id=". $keyValue ."'>Edit</a</th>\n";
foreach ($row as $name => $value)
print " <td>$value</td>\n";
if(!empty($deleteLink))
print " <td><a href='". $deleteLink ."?id=". $keyValue ."'>Delete</a</th>\n";
print " </tr>\n";
}
print "</table>\n";
}
function drawStars($avg_rating){
for ($i = 0; $i < $avg_rating; $i++) {
print "<span class='glyphicon glyphicon-star'></span>";
}
for ($i = $avg_rating; $i < 5; $i++) {
print "<span class='glyphicon glyphicon-star-empty'></span>";
}
}
function ratingStars(){
//for ($i = 0; $i < $avg_rating; $i++) {
//print "<span class='glyphicon glyphicon-star'></span>";
//}
for ($i = 0; $i < 5; $i++) {
print "<span class='glyphicon glyphicon-star-empty'></span><";
}
}
function getAvgRatingById($id){
$conn = getConnectionPDO();
$query = " SELECT avg(rating) as avg_rating, count(rating) as count_rating from Rating where productId = :id ";
$params = array(':id' => $id);
$ps = $conn->prepare($query);
$ps->execute($params);
$data = $ps->fetchAll(PDO::FETCH_ASSOC);
$avg_rating = 0;
foreach ($data as $row) {
$avg_rating = $row["avg_rating"];
}
return $avg_rating;
}
function getCountRatingById($id){
$conn = getConnectionPDO();
$query = " SELECT avg(rating) as avg_rating, count(rating) as count_rating from Rating where productId = :id ";
$params = array(':id' => $id);
$ps = $conn->prepare($query);
$ps->execute($params);
$data = $ps->fetchAll(PDO::FETCH_ASSOC);
$count_rating = 0;
foreach ($data as $row) {
$count_rating = $row["count_rating"];
}
return $count_rating;
}
function Redirect($url)
{
//header("Location: " . $url);
print "<script type='text/javascript'>
window.location = '". $url ."';
</script>";
exit();
}
function checkRating($userEmail, $productId, $rating, $comments){
try
{
$conn = getConnectionPDO();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = " select * from Rating " .
" where userEmail = :user_email and productId = :product_id ";
$params = array(':user_email' => $userEmail,
':product_id' => $productId);
$ps = $conn->prepare($sql);
$ps->execute($params);
$result = $ps->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0) {
return true;
}
return false;
}
catch (PDOException $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
catch (Exception $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
}
function insertRating($userEmail, $productId, $rating, $comments){
try
{
$conn = getConnectionPDO();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = " insert into Rating (userEmail, productId, rating, comments) " .
" values (:user_email, :product_id, :rating, :comments) ";
$params = array(':user_email' => $userEmail,
':product_id' => $productId,
':rating' => $rating,
':comments' => $comments);
$ps = $conn->prepare($sql);
return $ps->execute($params);
}
catch (PDOException $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
catch (Exception $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
}
function updateRating($userEmail, $productId, $rating, $comments){
try
{
$conn = getConnectionPDO();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = " update Rating set rating = :rating, comments = :comments where userEmail = :user_email and productId = :product_id ";
$params = array(':user_email' => $userEmail,
':product_id' => $productId,
':rating' => $rating,
':comments' => $comments);
$ps = $conn->prepare($sql);
return $ps->execute($params);
}
catch (PDOException $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
catch (Exception $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
}
function getMarketProduct($link) {
// Initialize cURL session
$ch = curl_init();
// Set the URL of the page file to download.
curl_setopt($ch, CURLOPT_URL, $link);
// Ask cURL to write the contents to a file
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Execute the c session
$content = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Close file
return $array = json_decode(trim($content), TRUE);
}
function createNewUser($useremail, $userfirst, $userlast, $usergender,
$cellphone, $homephone, $address, $city, $state, $zip,
$userpass, $userrole, $search){
try
{
$conn = getConnectionPDO();
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ((strlen($useremail) > 0) && (strlen($userpass) > 0))
{
$sql = " select userEmail from User" .
" where userEmail = :user_email";
$ps = $conn->prepare($sql);
$ps->bindParam(':user_email', $useremail);
$ps->execute();
$result = $ps->fetchAll(PDO::FETCH_ASSOC);
if (count($result) > 0)
{
alert("This email already exists.");
return false;
}
else
{
$sql = " insert into User (userEmail, userFirst, userLast, userGender, cellPhone, address, city " .
" , state, zip, homePhone, userRole, userPass, search)" .
" values (:user_email, :user_first, :user_last, :user_gender, :cell_phone " .
" , :address, :city, :state, :zip, :home_phone, :user_role, :user_pass, :search)";
$params = array(':user_email' => $useremail,
':user_first' => $userfirst,
':user_last' => $userlast,
':user_gender' => $usergender,
':cell_phone' => $cellphone,
':address' => $address,
':city' => $city,
':state' => $state,
':zip' => $zip,
':home_phone' => $homephone,
':user_role' => $userrole,
':user_pass' => $userpass,
':search' => $search);
$ps = $conn->prepare($sql);
return $ps->execute($params);
}
}
}
catch (PDOException $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
catch (Exception $ex)
{
echo 'ERROR: ' . $ex->getMessage();
}
}
?>