-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcommon.php
167 lines (142 loc) · 5.36 KB
/
common.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
<?php
require_once 'vendor/autoload.php';
use BikeShare\App\Configuration;
use BikeShare\App\Kernel;
use BikeShare\Authentication\Auth;
use BikeShare\Credit\CodeGenerator\CodeGeneratorInterface;
use BikeShare\Credit\CreditSystemInterface;
use BikeShare\Db\DbInterface;
use BikeShare\Mail\MailSenderInterface;
use BikeShare\Purifier\PhonePurifierInterface;
use BikeShare\Rent\RentSystemFactory;
use BikeShare\Sms\SmsSenderInterface;
use BikeShare\SmsConnector\SmsConnectorInterface;
use BikeShare\User\User;
use Symfony\Component\Dotenv\Dotenv;
/**
* should be removed
*/
global $configuration,
$sms,
$db,
$db,
$mailer,
$smsSender,
$codeGenerator,
$phonePurifier,
$creditSystem,
$user,
$auth,
$rentSystemFactory,
$translator,
$logger;
if (empty($kernel)) {
$dotenv = new Dotenv();
$dotenv->setProdEnvs(['prod']);
$dotenv->bootEnv(__DIR__.'/.env', 'dev', ['test'], true);
$kernel = new Kernel($_ENV['APP_ENV'], (bool) $_ENV['APP_DEBUG']);
$kernel->boot();
$container = $kernel->getContainer();
$logger = $kernel->getContainer()->get('logger');
Monolog\ErrorHandler::register($logger);
#Should be removed in the future. Currently, we are using it to log errors in old code
set_error_handler(
function ($severity, $message, $file, $line) use ($logger) {
$logger->error($message, ['severity' => $severity, 'file' => $file, 'line' => $line]);
return true;
}
);
}
$logger = $kernel->getContainer()->get('logger');
$configuration = $kernel->getContainer()->get(Configuration::class);
$sms = $kernel->getContainer()->get(SmsConnectorInterface::class);
$db = $kernel->getContainer()->get(DbInterface::class);
$db = $kernel->getContainer()->get(DbInterface::class);
$mailer = $kernel->getContainer()->get(MailSenderInterface::class);
$smsSender = $kernel->getContainer()->get(SmsSenderInterface::class);
$codeGenerator = $kernel->getContainer()->get(CodeGeneratorInterface::class);
$phonePurifier = $kernel->getContainer()->get(PhonePurifierInterface::class);
$creditSystem = $kernel->getContainer()->get(CreditSystemInterface::class);
$user = $kernel->getContainer()->get(User::class);
$auth = $kernel->getContainer()->get(Auth::class);
$rentSystemFactory = $kernel->getContainer()->get(RentSystemFactory::class);
$translator = $kernel->getContainer()->get('translator');
$locale = $configuration->get('systemlang') . ".utf8";
setlocale(LC_ALL, $locale);
putenv("LANG=" . $locale);
bindtextdomain("messages", dirname(__FILE__) . '/languages');
textdomain("messages");
function logrequest($userid)
{
global $user, $db;
$number = $user->findPhoneNumber($userid);
$db->query("INSERT INTO received SET sms_uuid='', sender='$number',receive_time='" . date('Y-m-d H:i:s') . "',sms_text='" . $_SERVER['REQUEST_URI'] . "',ip='" . $_SERVER['REMOTE_ADDR'] . "'");
}
function logresult($userid, $text)
{
global $db;
$userid = $db->escape($userid);
$logtext = "";
if (is_array($text)) {
foreach ($text as $value) {
$logtext .= $value . "; ";
}
} else {
$logtext = $text;
}
$logtext = substr(strip_tags($db->escape($logtext)), 0, 200);
$db->query("INSERT INTO sent SET number='$userid',text='$logtext'");
}
function checkbikeno($bikeNum)
{
global $db;
$bikeNum = intval($bikeNum);
$result = $db->query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum");
if (!$result->num_rows) {
response('<h3>Bike ' . $bikeNum . ' does not exist!</h3>', ERROR);
}
}
function checkstandname($stand)
{
global $db;
$standname = trim(strtoupper($stand));
$result = $db->query("SELECT standName FROM stands WHERE standName='$standname'");
if (!$result->num_rows) {
response('<h3>' . _('Stand') . ' ' . $standname . ' ' . _('does not exist') . '!</h3>', ERROR);
}
}
function confirmUser($userKey)
{
global $db, $configuration;
$userKey = $db->escape($userKey);
$result = $db->query("SELECT userId FROM registration WHERE userKey='$userKey'");
if ($result->num_rows == 1) {
$row = $result->fetch_assoc();
$userId = $row['userId'];
} else {
echo '<div class="alert alert-danger" role="alert">', _('Registration key not found!'), '</div>';
return false;
}
$db->query("UPDATE limits SET userLimit='" . $configuration->get('limits')['registration'] . "' WHERE userId=$userId");
$db->query("DELETE FROM registration WHERE userId='$userId'");
echo '<div class="alert alert-success" role="alert">', _('Your account has been activated. Welcome!'), '</div>';
}
function checktopofstack($standid)
{
global $db;
$currentbikes = array();
// find current bikes at stand
$result = $db->query("SELECT bikeNum FROM bikes LEFT JOIN stands ON bikes.currentStand=stands.standId WHERE standId='$standid'");
while ($row = $result->fetch_assoc()) {
$currentbikes[] = $row['bikeNum'];
}
if (count($currentbikes)) {
// find last returned bike at stand
$result = $db->query("SELECT bikeNum FROM history WHERE action IN ('RETURN','FORCERETURN') AND parameter='$standid' AND bikeNum IN (" . implode(',', $currentbikes) . ') ORDER BY time DESC LIMIT 1');
if ($result->num_rows) {
$row = $result->fetch_assoc();
return $row['bikeNum'];
}
}
return false;
}