-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.php
254 lines (211 loc) · 5.76 KB
/
funcs.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
<?php
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
defined('SW_BOOTSTRAP') or die();
$SW_DB = false;
$SW_BOARD = false;
$SW_BOARD_NAME = false;
/**
* Gets the ID of the underlying board.
*
* @return int The board ID.
*/
function sw_board() {
global $SW_BOARD;
if (false === $SW_BOARD) {
$SW_BOARD = trim(@$_REQUEST['b']);
if (!is_numeric($SW_BOARD)) {
$SW_BOARD = 1;
}
$SW_BOARD = (int)$SW_BOARD;
}
return $SW_BOARD;
}
/**
* Returns the title of the board.
*
* @return string The name of the board.
*/
function sw_board_name() {
global $SW_BOARD_NAME;
if (false === $SW_BOARD_NAME) {
$SW_BOARD_NAME = null;
$db = sw_db();
$stmt = $db->prepare("SELECT `name` FROM `boards` WHERE `id`=? LIMIT 0,1;");
try {
$id = sw_board();
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();
try {
if ($row = $result->fetch_array()) {
$SW_BOARD_NAME = $row[0];
}
}
finally {
$result->close();
}
}
finally {
$stmt->close();
}
$SW_BOARD_NAME = trim($SW_BOARD_NAME);
if ('' === $SW_BOARD_NAME) {
$SW_BOARD_NAME = 'Whiteboard';
}
}
return $SW_BOARD_NAME;
}
/**
* Returns the current version of a board.
*
* @param int $board The ID of the board.
*
* @return array|false The data of the current version or (false) if not found.
*/
function sw_current_version($board) {
$board = (int)trim($board);
$db = sw_db();
$stmt = $db->prepare("SELECT `id`,`content`,`content_type`,`time` FROM `content` WHERE `board_id`=? ORDER BY `id` DESC,`time` DESC LIMIT 0,1;");
try {
$stmt->bind_param('i', $board);
$stmt->execute();
$result = $stmt->get_result();
try {
return $result->fetch_assoc();
}
finally {
$result->close();
}
}
finally {
$stmt->close();
}
}
/**
* Returns the current database connection.
*
* @return mysqli The current database connection.
*/
function sw_db() {
global $SW_DB, $SW_CONFIG;
if (false === $SW_DB) {
$dbConf = @$SW_CONFIG['database'];
if (empty($dbConf)) {
$dbConf = array();
}
$SW_DB = new mysqli(
@$dbConf['host'],
@$dbConf['user'], @$dbConf['password'],
@$dbConf['db'],
@$dbConf['port'],
@$dbConf['socket']
);
if ($SW_DB->connect_errno) {
die(sprintf("Could not connect to MySQL database: [%s] '%s'",
$SW_DB->connect_errno, $SW_DB->connect_error));
}
$SW_DB->set_charset('utf8');
}
return $SW_DB;
}
/**
* Executes an action buffered.
*
* @param callable $action The action to invoke.
* @param mixed &$result|false (optional) The variable where the result of the action is written to.
* If (false) a 404 error will be send to the client.
*
* @return string The buffered output of the action.
*/
function sw_executed_buffered(callable $action, &$result = null) {
ob_start();
try {
$result = call_user_func($action);
}
finally {
$content = ob_get_contents();
ob_end_clean();
}
return $content;
}
/**
* Deactivates HTML frontend (header and footer).
*/
function sw_no_frontend() {
global $app;
$app['showHeader'] = false;
$app['showFooter'] = false;
}
$swRandSeed = false;
/**
* Generates a new random integer.
*
* @param int $min (optional) The minimum value.
* @param int $max (optional) The maximum value.
*
* @return int The random value.
*/
function sw_rand_int($min = null, $max = null): int {
global $swRandSeed;
if (false === $swRandSeed) {
list($usec, $sec) = explode(' ', microtime());
$swRandSeed = $sec + $usec * 1000000;
mt_srand($swRandSeed);
}
return call_user_func_array(
"\\mt_rand", func_get_args()
);
}
/**
* Sends a value as JSON string to the client.
*
* @param mixed $val The value to send.
* @param boolean $noFrontend (optional) Deactivate frontend or not.
*/
function sw_send_json($val, $noFrontend = true) {
global $app;
if ($noFrontend) {
sw_no_frontend();
}
$app['headers']['Content-type'] = 'application/json; charset=utf8';
echo json_encode($val);
}
/**
* Sends a result as JSON array.
*
* @param int $code (optional) The code.
* @param mixed $data (optional) The data.
*/
function sw_send_json_result($code = 0, $data = null) {
sw_send_json(array(
'code' => $code,
'data' => $data,
), true);
}
/**
* Returns the name of the current user.
*
* @return string The username.
*/
function sw_username() {
$username = trim(@$_SESSION[ SW_SESSION_USERNAME ]);
if ('' === $username) {
// try basic auth
$username = trim( @$_SERVER['PHP_AUTH_USER'] );
}
if (strlen($username) > 255) {
$username = trim(substr($username, 0, 255));
}
return $username;
}