-
Notifications
You must be signed in to change notification settings - Fork 71
/
actions-web.php
568 lines (505 loc) · 24.2 KB
/
actions-web.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
require("common.php");
function response($message, $error = 0, $additional = '', $log = 1)
{
global $db, $user;
$json = array('error' => $error, 'content' => $message);
if (is_array($additional)) {
foreach ($additional as $key => $value) {
$json[$key] = $value;
}
}
$json = json_encode($json);
if ($log == 1 and $message) {
if (isset($_COOKIE['loguserid'])) {
$userid = $db->escape(trim($_COOKIE['loguserid']));
} else {
$userid = 0;
}
$number = $user->findPhoneNumber($userid);
logresult($number, $message);
}
$db->commit();
echo $json;
exit;
}
function where($userId, $bike)
{
global $db;
$bikeNum = $bike;
$result = $db->query("SELECT number,userName,stands.standName FROM bikes LEFT JOIN users on bikes.currentUser=users.userID LEFT JOIN stands on bikes.currentStand=stands.standId where bikeNum=$bikeNum");
$row = $result->fetch_assoc();
$phone = $row['number'];
$userName = $row['userName'];
$standName = $row['standName'];
$result = $db->query("SELECT note FROM notes WHERE bikeNum='$bikeNum' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$note = _('Bike note:') . ' ' . $note;
}
if ($standName) {
response('<h3>' . _('Bike') . ' ' . $bikeNum . ' ' . _('at') . ' <span class="label label-primary">' . $standName . '</span>.</h3>' . $note);
} else {
response('<h3>' . _('Bike') . ' ' . $bikeNum . ' ' . _('rented by') . ' <span class="label label-primary">' . $userName . '</span>.</h3>' . _('Phone') . ': <a href="tel:+' . $phone . '">+' . $phone . '</a>. ' . $note);
}
}
function listbikes($stand)
{
global $db, $configuration;
$stacktopbike = false;
$stand = $db->escape($stand);
if ($configuration->get('forcestack')) {
$result = $db->query("SELECT standId FROM stands WHERE standName='$stand'");
$row = $result->fetch_assoc();
$stacktopbike = checktopofstack($row['standId']);
}
$result = $db->query("SELECT bikeNum FROM bikes LEFT JOIN stands ON bikes.currentStand=stands.standId WHERE standName='$stand'");
while ($row = $result->fetch_assoc()) {
$bikenum = $row['bikeNum'];
$result2 = $db->query("SELECT note FROM notes WHERE bikeNum='$bikenum' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result2->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$bicycles[] = '*' . $bikenum; // bike with note / issue
$notes[] = $note;
} else {
$bicycles[] = $bikenum;
$notes[] = '';
}
}
if (!$result->num_rows) {
$bicycles = '';
$notes = '';
}
response($bicycles, 0, array('notes' => $notes, 'stacktopbike' => $stacktopbike), 0);
}
function liststands()
{
global $db;
response(_('not implemented'), 0, '', 0);
exit;
$result = $db->query('SELECT standId,standName,standDescription,standPhoto,serviceTag,placeName,longitude,latitude FROM stands ORDER BY standName');
while ($row = $result->fetch_assoc()) {
$bikenum = $row['bikeNum'];
$result2 = $db->query("SELECT note FROM notes WHERE bikeNum='$bikenum' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result2->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$bicycles[] = '*' . $bikenum; // bike with note / issue
$notes[] = $note;
} else {
$bicycles[] = $bikenum;
$notes[] = '';
}
}
response($stands, 0, '', 0);
}
function removenote($userId, $bikeNum)
{
global $db;
$result = $db->query("DELETE FROM notes WHERE bikeNum=$bikeNum LIMIT XXXX");
response(_('Note for bike') . ' ' . $bikeNum . ' ' . _('deleted') . '.');
}
function last($userId, $bike = 0)
{
global $db;
$bikeNum = intval($bike);
if ($bikeNum) {
$result = $db->query("SELECT note FROM notes WHERE bikeNum='$bikeNum' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$note = _('Bike note:') . ' ' . $note;
}
$historyInfo = '<h3>' . _('Bike') . ' ' . $bikeNum . ' ' . _('history') . ':</h3>' . $note . '<ul style="margin-top: 0.5em">';
$result = $db->query("SELECT userName,parameter,standName,action,time FROM `history` JOIN users ON history.userid=users.userid LEFT JOIN stands ON stands.standid=history.parameter WHERE bikenum=$bikeNum AND (action NOT LIKE '%CREDIT%') ORDER BY time DESC LIMIT 10");
while ($row = $result->fetch_assoc()) {
$time = strtotime($row['time']);
$historyInfo .= '<li>' . date('d/m H:i', $time) . ' - ';
if ($row['standName'] != null) {
$historyInfo .= $row['standName'];
if (strpos($row['parameter'], '|')) {
$revertcode = explode('|', $row['parameter']);
$revertcode = $revertcode[1];
}
if ($row['action'] == 'REVERT') {
$historyInfo .= ' <span class="label label-warning">' . _('Revert') . ' (' . str_pad($revertcode, 4, '0', STR_PAD_LEFT) . ')</span>';
}
} else {
$historyInfo .= $row['userName'] . ' (<span class="label label-default">' . str_pad($row['parameter'], 4, '0', STR_PAD_LEFT) . '</span>)';
}
$historyInfo .= '</li>';
}
$historyInfo .= '</ul>';
} else {
$result = $db->query("SELECT bikeNum FROM bikes WHERE currentUser<>''");
$inuse = $result->num_rows;
$result = $db->query('SELECT bikeNum,userName,standName,users.userId FROM bikes LEFT JOIN users ON bikes.currentUser=users.userId LEFT JOIN stands ON bikes.currentStand=stands.standId ORDER BY bikeNum');
$total = $result->num_rows;
$historyInfo = '<h3>' . _('Current network usage:') . '</h3>';
$historyInfo .= '<h4>' . sprintf(ngettext('%d bicycle', '%d bicycles', $total), $total) . ', ' . $inuse . ' ' . _('in use') . '</h4><ul>';
while ($row = $result->fetch_assoc()) {
$historyInfo .= '<li>' . $row['bikeNum'] . ' - ';
if ($row['standName'] != null) {
$historyInfo .= $row['standName'];
} else {
$historyInfo .= '<span class="bg-warning">' . $row['userName'];
$result2 = $db->query('SELECT time FROM history WHERE bikeNum=' . $row['bikeNum'] . ' AND userId=' . $row['userId'] . " AND action='RENT' ORDER BY time DESC");
$row2 = $result2->fetch_assoc();
$historyInfo .= ': ' . date('d/m H:i', strtotime($row2['time'])) . '</span>';
}
$result2 = $db->query("SELECT note FROM notes WHERE bikeNum='" . $row['bikeNum'] . "' AND deleted IS NULL ORDER BY time DESC");
$note = '';
while ($row = $result2->fetch_assoc()) {
$note .= $row['note'] . '; ';
}
$note = substr($note, 0, strlen($note) - 2); // remove last two chars - comma and space
if ($note) {
$historyInfo .= ' (' . $note . ')';
}
$historyInfo .= '</li>';
}
$historyInfo .= '</ul>';
}
response($historyInfo, 0, '', 0);
}
function userbikes($userId)
{
global $db, $auth;
if (!$auth->isLoggedIn()) {
response('');
}
$result = $db->query("SELECT bikeNum,currentCode FROM bikes WHERE currentUser=$userId ORDER BY bikeNum");
while ($row = $result->fetch_assoc()) {
$bikenum = $row['bikeNum'];
$bicycles[] = $bikenum;
$codes[] = str_pad($row['currentCode'], 4, '0', STR_PAD_LEFT);
// get rented seconds and the old code
$result2 = $db->query("SELECT TIMESTAMPDIFF(SECOND, time, NOW()) as rentedSeconds, parameter FROM history WHERE bikeNum=$bikenum AND action IN ('RENT','FORCERENT') ORDER BY time DESC LIMIT 2");
$row2 = $result2->fetchAssoc();
$rentedseconds[] = $row2['rentedSeconds'];
$row2 = $result2->fetchAssoc();
$oldcodes[] = str_pad($row2['parameter'], 4, '0', STR_PAD_LEFT);
}
if (!$result->num_rows) {
$bicycles = '';
}
if (!isset($codes)) {
$codes = '';
} else {
$codes = array('codes' => $codes, 'oldcodes' => $oldcodes, 'rentedseconds' => $rentedseconds);
}
response($bicycles, 0, $codes, 0);
}
function revert($userId, $bikeNum)
{
global $db, $smsSender, $user;
$standId = 0;
$result = $db->query("SELECT currentUser FROM bikes WHERE bikeNum=$bikeNum AND currentUser IS NOT NULL");
if (!$result->num_rows) {
response(_('Bicycle') . ' ' . $bikeNum . ' ' . _('is not rented right now. Revert not successful!'), ERROR);
return;
} else {
$row = $result->fetch_assoc();
$revertusernumber = $user->findPhoneNumber($row['currentUser']);
}
$result = $db->query("SELECT parameter,standName FROM stands LEFT JOIN history ON stands.standId=parameter WHERE bikeNum=$bikeNum AND action IN ('RETURN','FORCERETURN') ORDER BY time DESC LIMIT 1");
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$standId = $row['parameter'];
$stand = $row['standName'];
}
$result = $db->query("SELECT parameter FROM history WHERE bikeNum=$bikeNum AND action IN ('RENT','FORCERENT') ORDER BY time DESC LIMIT 1,1");
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$code = str_pad($row['parameter'], 4, '0', STR_PAD_LEFT);
}
if ($standId and $code) {
$result = $db->query("UPDATE bikes SET currentUser=NULL,currentStand=$standId,currentCode=$code WHERE bikeNum=$bikeNum");
$result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='REVERT',parameter='$standId|$code'");
$result = $db->query("INSERT INTO history SET userId=0,bikeNum=$bikeNum,action='RENT',parameter=$code");
$result = $db->query("INSERT INTO history SET userId=0,bikeNum=$bikeNum,action='RETURN',parameter=$standId");
response('<h3>' . _('Bicycle') . ' ' . $bikeNum . ' ' . _('reverted to') . ' <span class="label label-primary">' . $stand . '</span> ' . _('with code') . ' <span class="label label-primary">' . $code . '</span>.</h3>');
$smsSender->send($revertusernumber, _('Bike') . ' ' . $bikeNum . ' ' . _('has been returned. You can now rent a new bicycle.'));
} else {
response(_('No last stand or code for bicycle') . ' ' . $bikeNum . ' ' . _('found. Revert not successful!'), ERROR);
}
}
function register($number, $code, $checkcode, $fullname, $email, $password, $password2, $existing)
{
global $db, $configuration, $user;
$number = $db->escape(trim($number));
$code = $db->escape(trim($code));
$checkcode = $db->escape(trim($checkcode));
$fullname = $db->escape(trim($fullname));
$email = $db->escape(trim($email));
$password = $db->escape(trim($password));
$password2 = $db->escape(trim($password2));
$existing = $db->escape(trim($existing));
$parametercheck = $number . ';' . str_replace(' ', '', $code) . ';' . $checkcode;
if ($password != $password2) {
response(_('Password do not match. Please correct and try again.'), ERROR);
}
if (issmssystemenabled() == true) {
$result = $db->query("SELECT parameter FROM history WHERE userId=0 AND bikeNum=0 AND action='REGISTER' AND parameter='$parametercheck' ORDER BY time DESC LIMIT 1");
if ($result->num_rows == 1) {
if (!$existing) { // new user registration
$result = $db->query("INSERT INTO users SET userName='$fullname',password=SHA2('$password',512),mail='$email',number='$number',privileges=0");
$userId = $db->getLastInsertId();
sendConfirmationEmail($email);
response(_('You have been successfully registered. Please, check your email and read the instructions to finish your registration.'));
} else { // existing user, password change
$userId = $user->findUserIdByNumber($number);
$result = $db->query("UPDATE users SET password=SHA2('$password',512) WHERE userId='$userId'");
response(_('Password successfully changed. Your username is your phone number. Continue to') . ' <a href="' . $configuration->get('systemURL') . '">' . _('login') . '</a>.');
}
} else {
response(_('Problem with the SMS code entered. Please check and try again.'), ERROR);
}
} else { // SMS system disabled
$result = $db->query("INSERT INTO users SET userName='$fullname',password=SHA2('$password',512),mail='$email',number='',privileges=0");
$userId = $db->getLastInsertId();
$result = $db->query("UPDATE users SET number='$userId' WHERE userId='$userId'");
sendConfirmationEmail($email);
response(_('You have been successfully registered. Please, check your email and read the instructions to finish your registration. Your number for login is:') . ' ' . $userId);
}
}
function checkprivileges($userid)
{
global $db, $user;
$privileges = $user->findPrivileges($userid);
if ($privileges < 1) {
response(_('Sorry, this command is only available for the privileged users.'), ERROR);
exit;
}
}
function smscode($number)
{
global $db, $smsSender, $user, $phonePurifier;
srand();
$number = $phonePurifier->purify($number);
$number = $db->escape($number);
$userexists = 0;
if ($user->findUserIdByNumber($number)) {
$userexists = 1;
}
$smscode = chr(rand(65, 90)) . chr(rand(65, 90)) . ' ' . rand(100000, 999999);
$smscodenormalized = str_replace(' ', '', $smscode);
$checkcode = md5('WB' . $number . $smscodenormalized);
if (!$userexists) {
$text = _('Enter this code to register:') . ' ' . $smscode;
} else {
$text = _('Enter this code to change password:') . ' ' . $smscode;
}
$text = $db->escape($text);
if (!issmssystemenabled()) {
$result = $db->query("INSERT INTO sent SET number='$number',text='$text'");
}
$result = $db->query("INSERT INTO history SET userId=0,bikeNum=0,action='REGISTER',parameter='$number;$smscodenormalized;$checkcode'");
if (DEBUG === true) {
response($number, 0, array('checkcode' => $checkcode, 'smscode' => $smscode, 'existing' => $userexists));
} else {
$smsSender->send($number, $text);
if (issmssystemenabled() == true) {
response($number, 0, array('checkcode' => $checkcode, 'existing' => $userexists));
} else {
response($number, 0, array('checkcode' => $checkcode, 'existing' => $userexists));
}
}
}
function trips($userId, $bike = 0)
{
global $db;
$bikeNum = intval($bike);
if ($bikeNum) {
$result = $db->query("SELECT longitude,latitude FROM `history` LEFT JOIN stands ON stands.standid=history.parameter WHERE bikenum=$bikeNum AND action='RETURN' ORDER BY time DESC");
while ($row = $result->fetch_assoc()) {
$jsoncontent[] = array('longitude' => $row['longitude'], 'latitude' => $row['latitude']);
}
} else {
$result = $db->query("SELECT bikeNum,longitude,latitude FROM `history` LEFT JOIN stands ON stands.standid=history.parameter WHERE action='RETURN' ORDER BY bikeNum,time DESC");
$i = 0;
while ($row = $result->fetch_assoc()) {
$bikenum = $row['bikeNum'];
$jsoncontent[$bikenum][] = array('longitude' => $row['longitude'], 'latitude' => $row['latitude']);
}
}
echo json_encode($jsoncontent); // TODO change to response function
}
function getuserlist()
{
global $db;
$result = $db->query('SELECT users.userId,username,mail,number,privileges,credit,userLimit FROM users LEFT JOIN credit ON users.userId=credit.userId LEFT JOIN limits ON users.userId=limits.userId ORDER BY username');
while ($row = $result->fetch_assoc()) {
$jsoncontent[] = array('userid' => $row['userId'], 'username' => $row['username'], 'mail' => $row['mail'], 'number' => $row['number'], 'privileges' => $row['privileges'], 'credit' => $row['credit'], 'limit' => $row['userLimit']);
}
echo json_encode($jsoncontent); // TODO change to response function
}
function getuserstats()
{
global $db;
$result = $db->query('SELECT users.userId,username,count(action) AS count FROM users LEFT JOIN history ON users.userId=history.userId WHERE history.userId IS NOT NULL GROUP BY username ORDER BY count DESC');
while ($row = $result->fetch_assoc()) {
$result2 = $db->query("SELECT count(action) AS rentals FROM history WHERE action='RENT' AND userId=" . $row['userId']);
$row2 = $result2->fetch_assoc();
$result2 = $db->query("SELECT count(action) AS returns FROM history WHERE action='RETURN' AND userId=" . $row['userId']);
$row3 = $result2->fetch_assoc();
$jsoncontent[] = array('userid' => $row['userId'], 'username' => $row['username'], 'count' => $row['count'], 'rentals' => $row2['rentals'], 'returns' => $row3['returns']);
}
echo json_encode($jsoncontent); // TODO change to response function
}
function getusagestats()
{
global $db;
$result = $db->query("SELECT count(action) AS count,DATE(time) AS day,action FROM history WHERE userId IS NOT NULL AND action IN ('RENT','RETURN') GROUP BY day,action ORDER BY day DESC LIMIT 60");
while ($row = $result->fetch_assoc()) {
$jsoncontent[] = array('day' => $row['day'], 'count' => $row['count'], 'action' => $row['action']);
}
echo json_encode($jsoncontent); // TODO change to response function
}
function edituser($userid)
{
global $db;
$result = $db->query('SELECT users.userId,userName,mail,number,privileges,userLimit,credit FROM users LEFT JOIN limits ON users.userId=limits.userId LEFT JOIN credit ON users.userId=credit.userId WHERE users.userId=' . $userid);
$row = $result->fetch_assoc();
$jsoncontent = array('userid' => $row['userId'], 'username' => $row['userName'], 'email' => $row['mail'], 'phone' => $row['number'], 'privileges' => $row['privileges'], 'limit' => $row['userLimit'], 'credit' => $row['credit']);
echo json_encode($jsoncontent); // TODO change to response function
}
function saveuser($userid, $username, $email, $phone, $privileges, $limit)
{
global $db;
$result = $db->query("UPDATE users SET username='$username',mail='$email',privileges='$privileges' WHERE userId=" . $userid);
if ($phone) {
$result = $db->query("UPDATE users SET number='$phone' WHERE userId=" . $userid);
}
$result = $db->query("UPDATE limits SET userLimit='$limit' WHERE userId=" . $userid);
response(_('Details of user') . ' ' . $username . ' ' . _('updated') . '.');
}
function addcredit($userid, $creditmultiplier)
{
global $db, $user, $creditSystem;
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
$addcreditamount = $minRequiredCredit * $creditmultiplier;
$result = $db->query('UPDATE credit SET credit=credit+' . $addcreditamount . ' WHERE userId=' . $userid);
$result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=0,action='CREDITCHANGE',parameter='" . $addcreditamount . '|add+' . $addcreditamount . "'");
$userName = $user->findUserName($userid);
response(_('Added') . ' ' . $addcreditamount . $creditSystem->getCreditCurrency() . ' ' . _('credit for') . ' ' . $userName . '.');
}
function getcouponlist()
{
global $db, $creditSystem;
if ($creditSystem->isEnabled() == false) {
return;
}
// if credit system disabled, exit
$result = $db->query("SELECT coupon,value FROM coupons WHERE status='0' ORDER BY status,value,coupon");
while ($row = $result->fetch_assoc()) {
$jsoncontent[] = array('coupon' => $row['coupon'], 'value' => $row['value']);
}
echo json_encode($jsoncontent); // TODO change to response function
}
function generatecoupons($multiplier)
{
global $db, $codeGenerator, $creditSystem;
if ($creditSystem->isEnabled() == false) {
return;
}
// if credit system disabled, exit
$minRequiredCredit = $creditSystem->getMinRequiredCredit();
$value = $minRequiredCredit * $multiplier;
$codes = $codeGenerator->generate(10, 6);
foreach ($codes as $code) {
$result = $db->query("INSERT IGNORE INTO coupons SET coupon='" . $code . "',value='" . $value . "',status='0'");
}
response(_('Generated 10 new') . ' ' . $value . ' ' . $creditSystem->getCreditCurrency() . ' ' . _('coupons') . '.', 0, array('coupons' => $codes));
}
function sellcoupon($coupon)
{
global $db, $creditSystem;
if ($creditSystem->isEnabled() == false) {
return;
}
// if credit system disabled, exit
$result = $db->query("UPDATE coupons SET status='1' WHERE coupon='" . $coupon . "'");
response(_('Coupon') . ' ' . $coupon . ' ' . _('sold') . '.');
}
function validatecoupon($userid, $coupon)
{
global $db, $creditSystem;
if ($creditSystem->isEnabled() == false) {
return;
}
// if credit system disabled, exit
$result = $db->query("SELECT coupon,value FROM coupons WHERE coupon='" . $coupon . "' AND status<'2'");
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$value = $row['value'];
$result = $db->query("UPDATE credit SET credit=credit+'" . $value . "' WHERE userId='" . $userid . "'");
$result = $db->query("INSERT INTO history SET userId=$userid,bikeNum=0,action='CREDITCHANGE',parameter='" . $value . '|add+' . $value . '|' . $coupon . "'");
$result = $db->query("UPDATE coupons SET status='2' WHERE coupon='" . $coupon . "'");
response('+' . $value . ' ' . $creditSystem->getCreditCurrency() . '. ' . _('Coupon') . ' ' . $coupon . ' ' . _('has been redeemed') . '.');
}
response(_('Invalid coupon, try again.'), 1);
}
function changecity($userid, $city)
{
global $db, $configuration;
if (in_array($city, $configuration->get('cities'))) {
$result = $db->query("UPDATE users SET city='$city' WHERE userId=" . $userid);
response('City changed');
}
response(_('Invalid City.'), 1);
}
function mapgetmarkers($userId)
{
global $db, $configuration, $user;
$filtercity = '';
if ($configuration->get('cities')) {
if ($userId != 0) {
$filtercity = ' AND city = "' . $user->findCity($userId) . '" ';
} else {
$filtercity = "";
}
}
$jsoncontent = array();
$result = $db->query('SELECT standId,count(bikeNum) AS bikecount,standDescription,standName,standPhoto,longitude AS lon, latitude AS lat FROM stands LEFT JOIN bikes on bikes.currentStand=stands.standId WHERE stands.serviceTag=0 '.$filtercity.' GROUP BY standName ORDER BY standName');
while ($row = $result->fetch_assoc()) {
$jsoncontent[] = $row;
}
echo json_encode($jsoncontent); // TODO proper response function
}
function mapgetlimit($userId)
{
global $db, $auth, $creditSystem;
if (!$auth->isLoggedIn()) {
response('');
}
$result = $db->query("SELECT count(*) as countRented FROM bikes where currentUser=$userId");
$row = $result->fetch_assoc();
$rented = $row['countRented'];
$result = $db->query("SELECT userLimit FROM limits where userId=$userId");
$row = $result->fetch_assoc();
$limit = $row['userLimit'];
$currentlimit = $limit - $rented;
$userCredit = $creditSystem->getUserCredit($userId);
echo json_encode(array('limit' => $currentlimit, 'rented' => $rented, 'usercredit' => $userCredit));
}
function mapgeolocation($userid, $lat, $long)
{
global $db;
$result = $db->query("INSERT INTO geolocation SET userId='$userid',latitude='$lat',longitude='$long'");
response('');
}; // TODO for admins: show bikes position on map depending on the user (allowed) geolocation, do not display user bikes without geoloc