-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
332 lines (310 loc) · 8.01 KB
/
functions.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
<?php
/**
* @file
* Functions to implement pages for class.
*/
$messages = array();
if (!file_exists('conf.php')) {
// @TODO create this page
header('Location: conf-required.html');
exit();
}
require_once('conf.php');
if (empty($conf['smarty_libs'])) {
print "Please update conf.php to add the key 'smarty_libs' and point to the location of the Smarty class libraries (no trailing slash).\n";
exit();
}
require($conf['smarty_libs'] . '/Smarty.class.php');
/**
* Connect to the database.
*
* Expects global variable $conf to be set and the keys 'db', 'db_user', and
* 'db_pwd' to be populated. They will be used to connect to the database.
*
* @return object
* A connected PDO object for queries.
* http://php.net/manual/en/class.pdo.php
*/
function connect() {
global $conf;
if (empty($conf['db']) || empty($conf['db_user']) || empty($conf['db_pwd'])) {
// @TODO This is not a good thing to show to the users.
die("database connection string not configured properly\n");
}
// We hard-coded localhost here, but extending to support a host and port
// is trivial.
try {
$pdo = new PDO('mysql:host=localhost;dbname=' . $conf['db'] . ';charset=UTF8', $conf['db_user'], $conf['db_pwd']);
} catch (Exception $e) {
// Well, that's not good.
// There's several things we can do at this point, none of them good.
// For now, just throw the error up on the page.
print_r($e->errorInfo);
die($e->getMessage() . "\n");
}
return $pdo;
}
/**
* Find all rooms.
*
* @return object
* A PDOStatement object, result of an executed query.
* http://php.net/manual/en/class.pdostatement.php
*/
function roomlist() {
$pdo = connect();
$stmt = $pdo->query("SELECT * FROM room");
return $stmt;
}
/**
* Find the row for a specific row.
*
* @param int $rid
* Room id
* @return array
* Array of columns in room row. If the room was not found, rid == -1. If
* it's a new room (rid=0), an array of empty elements is returned.
*/
function room($rid=0) {
if (empty($rid)) {
return empty_room(0);
}
$pdo = connect();
$stmt = $pdo->prepare("SELECT rid, rate, roomsize, sleeps, image FROM room WHERE rid = :rid");
$room = empty_room(-1);
if ($stmt->execute(array(':rid' => $rid))) {
$room = $stmt->fetch();
if (empty($room)) {
// Note the difference for "room not found"
return empty_room(-1);;
}
}
return $room;
}
/**
* Helper function to create an empty room (no data).
*
* @return array
* Room array.
*/
function empty_room($rid) {
// Return an empty array if they're adding a new room. Keep in mind that if
// we add columns to the room table, we need to add those fields to this
// array, too.
return array(
'rid' => $rid,
'rate' => 0,
'roomsize' => 'd',
'sleeps' => 2,
);
}
/**
* Helper function to execute a sql statement, with arguments
*
* @param string $sql
* SQL statement to run, with placeholders
* @param array $args
* Arguments for $sql
*
* @return array
* Two element array, pdo object, rowCount or FALSE if error
*/
function sql_execute($sql, $args, $desc) {
$pdo = connect();
$stmt = $pdo->prepare($sql);
$stmt->execute($args);
$err = $pdo->errorInfo();
if ($err[0] != '00000') {
msg_add($desc . ' failed: ' . $err[2], 'bg-danger');
return array($pdo, FALSE, FALSE);
}
return array($pdo, $stmt->rowCount(), $stmt);
}
function room_delete($rid) {
$pdo = connect();
$sql = "DELETE FROM room WHERE rid = :rid";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':rid' => $rid));
return $stmt->rowCount();
}
/**
* Make sure the user has been logged in.
*/
function check_logged_in($from = '') {
session_start();
if (empty($_SESSION['username'])) {
if (empty($from)) {
$from = $_SERVER['REQUEST_URI'];
}
$_SESSION['login_redirect'] = $from;
header('Location: login.php');
exit();
}
}
/**
* Add a user to the database.
*/
function user_add($username, $password) {
// @TODO duplicate usernames??!
$pdo = connect();
$sql = "INSERT INTO user (username, password)
VALUES (:username, :password)";
$stmt = $pdo->prepare($sql);
$enc_pwd = password_hash($password, PASSWORD_DEFAULT);
$args = array(
':username' => $username,
':password' => $enc_pwd,
);
$stmt->execute($args);
return $stmt->rowCount();
}
/**
* Load the user row for a specific username.
*/
function user_load($username) {
$pdo = connect();
$sql = "SELECT * FROM user WHERE username = :username";
$stmt = $pdo->prepare($sql);
$args = array(':username' => $username);
$stmt->execute($args);
return $stmt->fetch();
}
/**
* Add a message to be rendered on the next page load.
*/
function msg_add($msg, $sev = 'bg-success') {
global $messages;
if (empty($_SESSION['msg'])) {
$_SESSION['msg'] = array();
}
$_SESSION['msg'][] = array('msg' => $msg, 'sev' => $sev);
$messages[] = array('msg' => $msg, 'sev' => $sev);
dbg($msg);
}
/**
* Helper function to render any messages on the page.
*
* $msg (should come from $_SESSION
*/
function msg_render() {
global $messages;
if (!empty($_SESSION['msg'])) {
$messages = $_SESSION['msg'];
}
foreach ($messages as $msg) {
print '<h5 class="' . $msg['sev'] . '">' . $msg['msg'] . "</h5>\n";
}
$_SESSION['msg'] = array();
}
/**
* Return any tags we need in the <head> of most/all pages.
*/
function head_elements() {
return <<<EOT
<link href="css/bates.css" rel="stylesheet"></link>
<!-- jQuery (necessary for Bootstrap JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="js/bates.js"></script>
EOT;
}
/**
* Returns an options list of states in the union.
*/
function us_states_opts($sel = NULL) {
$states = array(
'AL' => 'Alabama',
'AK' => 'Alaska',
'AZ' => 'Arizona',
'AR' => 'Arkansas',
'CA' => 'California',
'CO' => 'Colorado',
'CT' => 'Connecticut',
'DE' => 'Delaware',
'DC' => 'District of Columbia',
'FL' => 'Florida',
'GA' => 'Georgia',
'HI' => 'Hawaii',
'ID' => 'Idaho',
'IL' => 'Illinois',
'IN' => 'Indiana',
'IA' => 'Iowa',
'KS' => 'Kansas',
'KY' => 'Kentucky',
'LA' => 'Louisiana',
'ME' => 'Maine',
'MD' => 'Maryland',
'MA' => 'Massachusetts',
'MI' => 'Michigan',
'MN' => 'Minnesota',
'MS' => 'Mississippi',
'MO' => 'Missouri',
'MT' => 'Montana',
'NE' => 'Nebraska',
'NV' => 'Nevada',
'NH' => 'New Hampshire',
'NJ' => 'New Jersey',
'NM' => 'New Mexico',
'NY' => 'New York',
'NC' => 'North Carolina',
'ND' => 'North Dakota',
'OH' => 'Ohio',
'OK' => 'Oklahoma',
'OR' => 'Oregon',
'PA' => 'Pennsylvania',
'RI' => 'Rhode Island',
'SC' => 'South Carolina',
'SD' => 'South Dakota',
'TN' => 'Tennessee',
'TX' => 'Texas',
'UT' => 'Utah',
'VT' => 'Vermont',
'VA' => 'Virginia',
'WA' => 'Washington',
'WV' => 'West Virginia',
'WI' => 'Wisconsin',
'WY' => 'Wyoming',
);
return options($states, $sel);
}
/**
* Converts an array to an html list of options.
*/
function options($options, $selected = NULL) {
$output = '';
foreach ($options as $key => $value) {
$selected = $key == $selected ? ' selected' : '';
$output .= "<option value=\"{$key}\"{$selected}>{$value}</option>\n";
}
return $output;
}
function dbg($val) {
echo "<pre>\n";
print to_string($val);
echo "</pre>\n";
}
// utility functions
function to_string($val) {
if (is_null($val)) {
return 'null';
}
elseif (is_bool($val)) {
return $val ? 'true' : 'false';
}
elseif (is_array($val) || is_object($val)) {
return print_r($val, true);
}
else {
return (string) $val;
}
}
class PSmarty extends Smarty {
public function __construct() {
//$this->assign('head', '');
}
/** might use later
// override display function to add functionality for all pages.
public function display($template = null, $cache_id = null, $compile_id = null, $parent = null) {
parent::display($template, $cache_id, $compile_id, $parent);
}
**/
}