forked from cyklokoalicia/OpenSourceBikeShare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.php
539 lines (481 loc) · 15.7 KB
/
base.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
<?php
define('OK',1);
function notification($result,$values=FALSE)
{
if ($result==10)
{
notifyAdmins(_('Bike')." ".$values->bikenum." "._('rented out of stack by')." ".$values->username.". ".$values->stacktopbike." "._('was on the top of the stack at')." ".$values->currentstand.".",ERROR);
}
elseif ($result==20)
{
notifyAdmins(_('Note')." b.".$values->bikenum." (".$values->bikestatus.") "._('by')." ".$values->username."/".$values->phone.":".$values->note);
}
response('Unhandled notification '.$result.'.',ERROR);
}
function rentbike($userid,$bikenum,$force=FALSE)
{
global $forcestack,$watches;
$values=new stdClass;
$stacktopbike=FALSE;
$requiredcredit=getrequiredcredit();
if ($force==FALSE)
{
$creditcheck=checkrequiredcredit($userid);
if ($creditcheck===FALSE)
{
$credit=R::findOne('credit','userid=?',[$userid]);
$values->credit=$credit->credit;
$values->requiredcredit=$requiredcredit;
status('RENT',130,$values);
}
checktoomany(0,$userid);
$countrented=R::count('bikes','currentuser=?',[$userid]);
$limit=R::findOne('limits','userid=?',[$userid]);
if ($countrented>=$limit->userlimit)
{
if ($limit->userlimit==0)
{
status('RENT',100);
}
elseif ($limit->userlimit==1)
{
$values->userlimit=$limit->userlimit;
status('RENT',101,$values);
}
else
{
$values->userlimit=$limit->userlimit;
status('RENT',101,$values);
}
}
if ($forcestack OR $watches["stack"])
{
$bike=R::findOne('bikes','bikenum=?',[$bikenum]);
$stacktopbike=checktopofstack($bike->currentstand);
if ($watches["stack"] AND $stacktopbike<>$bikenum)
{
$stand=R::load('stands',$bike->currentstand);
$username=getusername($userid);
$values->bikenum=$bikenum;
$values->username=$username;
$values->stacktopbike=$stacktopbike;
$values->currentstand=$bike->currentstand;
notification(10,$values);
}
if ($forcestack AND $stacktopbike<>$bikenum)
{
$values->bikenum=$bikenum;
$values->stacktopbike=$stacktopbike;
status('RENT',110,$values);
}
}
}
$bike=R::findOne('bikes','bikenum=?',[$bikenum]);
$bike->currentcode=sprintf("%04d",$bike->currentcode);
$notes=R::find('notes','bikenum=? ORDER BY time DESC',[$bikenum]);
$note="";
if (!empty($notes))
{
foreach($notes as $noteitem)
{
$note.=$noteitem->note."; ";
}
}
$note=substr($note,0,strlen($note)-2); // remove last two chars - comma and space
$newcode=sprintf("%04d",rand(100,9900)); //do not create a code with more than one leading zero or more than two leading 9s (kind of unusual/unsafe).
$values->bikenum=$bikenum;
$values->currentcode=$bike->currentcode;
$values->currentuser=$bike->currentuser;
$values->newcode=$newcode;
$values->note=$note;
if ($bike->currentuser) $values->currentusernumber=getphonenumber($bike->currentuser);
if ($force==FALSE)
{
if ($bike->currentuser==$userid)
{
$values->bikenum=$bikenum;
$values->currentcode=$bike->currentcode;
status('RENT',120,$values);
}
elseif ($bike->currentuser!=0)
{
$values->bikenum=$bikenum;
status('RENT',121,$values);
}
}
$bike->currentuser=$userid;
$bike->currentcode=$newcode;
$bike->currentstand=NULL;
R::store($bike);
$history=R::dispense('history');
$history->userid=$userid;
$history->bikenum=$bikenum;
if ($force==FALSE)
{
$history->action='RENT';
}
else
{
$history->action='FORCERENT';
}
$history->parameter=$newcode;
R::store($history);
status('RENT',OK,$values);
}
function returnbike($userid,$bikenum,$stand,$note="",$force=FALSE)
{
global $connectors;
$values=new stdClass;
$stand=strtoupper($stand);
$values->bikenum=$bikenum;
if ($force==FALSE)
{
$countrented=R::count('bikes','currentuser=? ORDER BY bikenum',[$userid]);
$values->countrented=$countrented;
if ($countrented==0)
{
status('RETURN',100);
}
elseif ($countrented>1 AND !$bikenum) // QR code only, when multiple bikes rented
{
$values->countrented=$countrented;
status('RETURN',101,$values);
}
$bike=R::findOne('bikes','currentuser=:userid AND bikenum=:bikenum',[':userid'=>$userid,':bikenum'=>$bikenum]);
if (empty($bike)) // no such bike is rented
{
$userbikes=R::find('bikes','currentuser=?',[$userid]);
$values->bikelist="";
foreach($userbikes as $userbike)
{
$values->bikelist.=$userbike->bikenum.',';
}
if ($countrented>=1) $values->bikelist=substr($values->bikelist,0,strlen($values->bikelist)-1); // remove last comma
status('RETURN',102,$values);
}
}
else
{
$bike=R::findOne('bikes','currentuser=:userid AND bikenum=:bikenum',[':userid'=>$userid,':bikenum'=>$bikenum]);
if (!empty($bike)) // bike does not exist
{
$values->bikenum=$bikenum;
status('RETURN',103,$values);
}
$values->currentuser=getphonenumber($bike->currentuser);
// ??????? $existingnote=R::findOne('notes','bikeNum=? AND deleted IS NULL ORDER BY time DESC LIMIT 1',[$bikenum]);
// ?????? $values->existingnote=$existingnote->note;
}
$bike->currentcode=sprintf("%04d",$bike->currentcode);
$values->currentcode=$bike->currentcode;
$stand=R::findOne('stands','standname=?',[$stand]);
$bike->currentuser=NULL;
$bike->currentstand=$stand->id;
R::store($bike);
$creditchange=changecreditendrental($bike->bikenum,$userid);
if (iscreditenabled() AND $creditchange) $values->creditchange=$creditchange;
if ($note)
{
$notes=R::dispense('notes');
$notes->bikenum=$bikenum;
$notes->userid=$userid;
$notes->note=$note;
R::store($notes);
$values->note=$note;
$values->standname=$stand;
$values->bikestatus=_('at')." ".$stand;
$values->username=getusername($userid);
$values->phone=getphonenumber($userid);
notification(20,$values);
}
$history=R::dispense('history');
$history->userid=$userid;
$history->bikenum=$bike->bikenum;
if ($force==FALSE)
{
$history->action='RETURN';
}
else
{
$history->action='FORCERETURN';
}
$history->parameter=$stand->id;
R::store($history);
$values->userid=$userid;
status('RETURN',OK,$values);
}
function where($userid,$bikenum)
{
$values=new stdClass;
$bike=R::find('bikes','bikenum=?',[$bikenum]);
$notes=R::find('notes','bikenum=? AND deleted IS NULL ORDER BY time DESC',[$bikenum]);
$note="";
foreach ($notes as $noteitem)
{
$note.=$noteitem->note."; ";
}
$values->bikenum=$bikenum;
$values->note=substr($note,0,strlen($note)-2); // remove last two chars - comma and space
if ($note)
{
$values->note=$note;
}
if ($bike->standname)
{
$values->standname=$bike->currentstand;
status('WHERE',100,$values);
}
else
{
$values->username=getusername($bike->currentuser);
$values->phone=getphonenumber($bike->currentuser);
status('WHERE',101,$values);
}
}
function listbikes($stand)
{
global $forcestack;
$values=new stdClass;
$values->stacktopbike=FALSE;
if ($forcestack)
{
$stand=R::findOne('stand','standname=?',[$stand]);
$values->stacktopbike=checktopofstack($stand->id);
}
$rows=R::getAll('SELECT bikes.id,bikenum FROM bikes LEFT JOIN stands ON bikes.currentstand=stands.id WHERE standname=?',[$stand]);
$bikes=R::convertToBeans('bikes',$rows);
if (!empty($bikes))
{
$values->standcount=count($bikes);
foreach ($bikes as $bike)
{
$notes=R::find('notes','bikenum=:bikenum AND deleted=:deleted ORDER BY time DESC',[':bikenum'=>$bike->bikenum,':deleted'=>'0000-00-00 00:00:00']);
$note="";
foreach ($notes as $noteitem)
{
$note.=$noteitem->note."; ";
}
$note=substr($note,0,strlen($note)-2); // remove last two chars - comma and space
if ($note)
{
$values->id[]=$bike->bikenum; // bike with note / issue
$values->notes[]=$note;
}
else
{
$values->id[]=$bike->bikenum;
$values->notes[]="";
}
}
}
else
{
status('LISTBIKES',100,$values);
}
status('LISTBIKES',OK,$values);
}
function addnote($userid,$bikenum,$message)
{
$values=new stdClass;
$user=R::load('users',$userid);
$stand=R::getRow('SELECT stands.standName FROM bikes LEFT JOIN users on bikes.currentUser=users.userId LEFT JOIN stands on bikes.currentStand=stands.standId WHERE bikeNum=?',[$bikenum]);
if (!empty($stand))
{
$bikestatus=_('at')." ".$stand->standname;
}
else
{
$bikestatus=_('used by')." ".$user->username." +".$user->phone;
}
$note=R::dispense('notes');
$note->bikenum=$bikenum;
$note->userid=$userid;
$note->note=$message;
$noteid=R::store($note);
notifyAdmins(_('Note #').$noteid.": b.".$bikenum." (".$bikestatus.") "._('by')." ".$user->username."/".$user->phone.":".$message);
}
function removenote($userid,$bikenum)
{
$values=new stdClass;
$values->bikenum=$bikenum;
$note=R::findOne('notes','bikenum=?',[$bikenum]);
R::trash($note);
status('DELNOTE',OK,$values);
}
function saveuser($userid,$username,$email,$phone,$privileges,$userlimit)
{
$user=R::load('users',$userid);
if ($user->id)
{
$user->username=$username;
$user->mail=$email;
if ($phone) $user->phone=$phone;
$user->privileges=$privileges;
R::store($user);
$limit=findOne('limits','userid=?',[$userid]);
$limit->limit=$userlimit;
R::store($limit);
response(_('Details of user')." ".$user->username." "._('updated').".");
}
}
function addcredit($userid,$creditmultiplier)
{
$addcreditamount=getrequiredcredit()*$creditmultiplier;
$credit->findOne('credit','userid=?',[$userid]);
$credit->credit=$credit->credit+$addcreditamount;
R::store($credit);
$history=R::dispense('history');
$history->userid=$userid;
$history->action='CREDITCHANGE';
$history->parameter=$addcreditamount.'|add+'.$addcreditamount;
R::store($history);
response(_('Added')." ".$addcreditamount.getcreditcurrency()." "._('credit for')." ".getusername($userid).".");
}
function getcouponlist()
{
if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
$coupons=R::find('coupons','status=0 ORDER BY status,value,coupon');
foreach($coupons as $coupon)
{
$jsoncontent[]=array("coupon"=>$coupon->coupon,"value"=>$coupon->value);
}
echo json_encode($jsoncontent);// TODO change to response function
}
function generatecoupons($multiplier)
{
if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
$value=getrequiredcredit()*$multiplier;
$codes=generatecodes(10,6);
foreach ($codes as $code)
{
$coupon=findOne('coupons','coupon=?',$code);
if (empty($coupon))
{
$coupon=R::dispense('coupons');
$coupon->coupon=$code;
$coupon->value=$value;
$coupon->status=0;
R::store($coupon);
}
}
response(_('Generated 10 new').' '.$value.' '.getcreditcurrency().' '._('coupons').'.',0,array("coupons"=>$codes));
}
function sellcoupon($couponcode)
{
if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
$coupon=R::findOne('coupons','coupon=?',[$couponcode]);
$coupon->status=1;
R::store($coupon);
response(_('Coupon').' '.$coupon->coupon.' '._('sold').'.');
}
function validatecoupon($userid,$couponcode)
{
if (iscreditenabled()==FALSE) return; // if credit system disabled, exit
$coupon=R::findOne('coupons','coupon=? AND status<2',[$couponcode]);
if (!empty($coupon))
{
$credit=R::findOne('credit','userid=?',$userid);
$credit->credit=$credit->credit+$coupon->value;
R::store($credit);
$history=R::dispense('history');
$history->userid=$userid;
$history->action='CREDITCHANGE';
$history->parameter=$coupon->value.'|add+'.$coupon->value.'|'.$coupon->coupon;
R::store($history);
$coupon->status=2;
R::store($coupon);
response('+'.$coupon->value.' '.getcreditcurrency().'. '._('Coupon').' '.$coupon->coupon.' '._('has been redeemed').'.');
}
response(_('Invalid coupon, try again.'),1);
}
function resetpassword($number)
{
$user=findOne('users','number=?',[$number]);
if (empty($user)) response(_('No such user found.'),1);
$subject=_('Password reset');
mt_srand(crc32(microtime()));
$user->password=substr(md5(mt_rand().microtime().$user->mail),0,8);
R::store($user);
$names=preg_split("/[\s,]+/",$user->username);
$firstname=$names[0];
$message=_('Hello').' '.$firstname.",\n\n".
_('Your password has been reset successfully.')."\n\n".
_('Your new password is:')."\n".$user->password;
sendEmail($user->mail, $subject, $message);
response(_('Your password has been reset successfully.').' '._('Check your email.'));
}
function credit($number)
{
$values=new stdClass;
$userid=getuserid($number);
$values->usercredit=getusercredit($userid);
status('CREDIT',100,$values);
}
function checkbikenum($bikenum)
{
$values=new stdClass;
$bike=R::load('bikes',$bikenum);
if (!$bike->id)
{
$values->bikenum=$bikenum;
status('CHECKBIKE',100,$values);
}
}
function userbikes($userid)
{
if (!isloggedin()) response("");
$bikes=R::find('bikes','currentuser=? ORDER BY bikenum',[$userid]);
if (!empty($bikes))
{
foreach ($bikes as $bike)
{
$bicycles[]=$bike->bikenum;
$codes[]=str_pad($bike->code,4,"0",STR_PAD_LEFT);
$history=R::findOne('history','bikenum=:bikenum AND action=:action ORDER BY time DESC',[':bikenum'=>$bike->bikenum,':action'=>'RENT']);
if (!empty($history))
{
$oldcodes[]=str_pad($history->parameter,4,"0",STR_PAD_LEFT);
}
}
}
else
{
$bicycles="";
}
if (!isset($codes)) $codes="";
if (!isset($oldcodes)) $oldcodes="";
response('',0,array("userbikes"=>array("bicycles"=>$bicycles,"codes"=>$codes,"oldcodes"=>$oldcodes)),0);
}
function mapgeolocation ($userid,$lat,$long)
{
$geolocation=R::dispense('geolocation');
$geolocation->userid=$userid;
$geolocation->latitude=$lat;
$geolocation->longitude=$long;
R::store($geolocation);
response("");
}
function mapgetmarkers()
{
$rows=R::getAll('SELECT stands.id,count(bikeNum) AS bikecount,standDescription,standName,standPhoto,longitude AS lon, latitude AS lat FROM stands LEFT JOIN bikes on bikes.currentStand=stands.id WHERE stands.serviceTag=0 GROUP BY standName ORDER BY standName');
if (!empty($rows))
{
foreach ($rows as $row)
{
$jsoncontent["markers"][]=$row;
}
}
response('',0,$jsoncontent,0);
}
function mapgetlimit($userid)
{
if (!isloggedin()) response("");
$rented=R::count('bikes','currentuser=?',[$userid]);
$limit=R::findOne('limits','userid=?',[$userid]);
$currentlimit=$limit->userlimit-$rented;
$usercredit=0;
$usercredit=getusercredit($userid);
$jsoncontent=array("limit"=>$currentlimit,"rented"=>$rented,"usercredit"=>$usercredit);
response('',0,$jsoncontent,0);
}
?>