1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file has all the main functions in it that relate to the database. |
5
|
|
|
* |
6
|
|
|
* Simple Machines Forum (SMF) |
7
|
|
|
* |
8
|
|
|
* @package SMF |
9
|
|
|
* @author Simple Machines https://www.simplemachines.org |
10
|
|
|
* @copyright 2020 Simple Machines and individual contributors |
11
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
12
|
|
|
* |
13
|
|
|
* @version 2.1 RC2 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
if (!defined('SMF')) |
17
|
|
|
die('No direct access...'); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Maps the implementations in this file (smf_db_function_name) |
21
|
|
|
* to the $smcFunc['db_function_name'] variable. |
22
|
|
|
* |
23
|
|
|
* @see Subs-Db-mysql.php#smf_db_initiate |
24
|
|
|
* |
25
|
|
|
* @param string $db_server The database server |
26
|
|
|
* @param string $db_name The name of the database |
27
|
|
|
* @param string $db_user The database username |
28
|
|
|
* @param string $db_passwd The database password |
29
|
|
|
* @param string $db_prefix The table prefix |
30
|
|
|
* @param array $db_options An array of database options |
31
|
|
|
* @return null|resource Returns null on failure if $db_options['non_fatal'] is true or a PostgreSQL connection resource handle if the connection was successful. |
32
|
|
|
*/ |
33
|
|
|
function smf_db_initiate($db_server, $db_name, $db_user, $db_passwd, &$db_prefix, $db_options = array()) |
34
|
|
|
{ |
35
|
|
|
global $smcFunc; |
36
|
|
|
|
37
|
|
|
// Map some database specific functions, only do this once. |
38
|
|
|
if (!isset($smcFunc['db_fetch_assoc'])) |
39
|
|
|
$smcFunc += array( |
40
|
|
|
'db_query' => 'smf_db_query', |
41
|
|
|
'db_quote' => 'smf_db_quote', |
42
|
|
|
'db_insert' => 'smf_db_insert', |
43
|
|
|
'db_insert_id' => 'smf_db_insert_id', |
44
|
|
|
'db_fetch_assoc' => 'pg_fetch_assoc', |
45
|
|
|
'db_fetch_row' => 'pg_fetch_row', |
46
|
|
|
'db_free_result' => 'pg_free_result', |
47
|
|
|
'db_num_rows' => 'pg_num_rows', |
48
|
|
|
'db_data_seek' => 'pg_result_seek', |
49
|
|
|
'db_num_fields' => 'pg_num_fields', |
50
|
|
|
'db_escape_string' => 'smf_db_escape_string', |
51
|
|
|
'db_unescape_string' => 'stripslashes', |
52
|
|
|
'db_server_info' => 'smf_db_version', |
53
|
|
|
'db_affected_rows' => 'smf_db_affected_rows', |
54
|
|
|
'db_transaction' => 'smf_db_transaction', |
55
|
|
|
'db_error' => 'pg_last_error', |
56
|
|
|
'db_select_db' => 'smf_db_select_db', |
57
|
|
|
'db_title' => POSTGRE_TITLE, |
58
|
|
|
'db_sybase' => true, |
59
|
|
|
'db_case_sensitive' => true, |
60
|
|
|
'db_escape_wildcard_string' => 'smf_db_escape_wildcard_string', |
61
|
|
|
'db_is_resource' => 'is_resource', |
62
|
|
|
'db_mb4' => true, |
63
|
|
|
'db_ping' => 'pg_ping', |
64
|
|
|
'db_fetch_all' => 'smf_db_fetch_all', |
65
|
|
|
'db_error_insert' => 'smf_db_error_insert', |
66
|
|
|
'db_custom_order' => 'smf_db_custom_order', |
67
|
|
|
'db_native_replace' => 'smf_db_native_replace', |
68
|
|
|
'db_cte_support' => 'smf_db_cte_support', |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
// We are not going to make it very far without these. |
72
|
|
|
if (!function_exists('pg_pconnect')) |
73
|
|
|
display_db_error(); |
74
|
|
|
|
75
|
|
|
// We need to escape ' and \ |
76
|
|
|
$db_passwd = str_replace(array('\\','\''), array('\\\\','\\\''), $db_passwd); |
77
|
|
|
|
78
|
|
|
if (!empty($db_options['persist'])) |
79
|
|
|
$connection = @pg_pconnect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
80
|
|
|
else |
81
|
|
|
$connection = @pg_connect((empty($db_server) ? '' : 'host=' . $db_server . ' ') . 'dbname=' . $db_name . ' user=\'' . $db_user . '\' password=\'' . $db_passwd . '\'' . (empty($db_options['port']) ? '' : ' port=\'' . $db_options['port'] . '\'')); |
82
|
|
|
|
83
|
|
|
// Something's wrong, show an error if its fatal (which we assume it is) |
84
|
|
|
if (!$connection) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
if (!empty($db_options['non_fatal'])) |
87
|
|
|
{ |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
else |
91
|
|
|
{ |
92
|
|
|
display_db_error(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!empty($db_options['db_mb4'])) |
97
|
|
|
$smcFunc['db_mb4'] = (bool) $db_options['db_mb4']; |
98
|
|
|
|
99
|
|
|
return $connection; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Extend the database functionality. It calls the respective file's init |
104
|
|
|
* to add the implementations in that file to $smcFunc array. |
105
|
|
|
* |
106
|
|
|
* @param string $type Indicates which additional file to load. ('extra', 'packages') |
107
|
|
|
*/ |
108
|
|
|
function db_extend($type = 'extra') |
109
|
|
|
{ |
110
|
|
|
global $sourcedir, $db_type; |
111
|
|
|
|
112
|
|
|
require_once($sourcedir . '/Db' . strtoupper($type[0]) . substr($type, 1) . '-' . $db_type . '.php'); |
113
|
|
|
$initFunc = 'db_' . $type . '_init'; |
114
|
|
|
$initFunc(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Fix the database prefix if necessary. |
119
|
|
|
* Does nothing on PostgreSQL |
120
|
|
|
* |
121
|
|
|
* @param string $db_prefix The database prefix |
122
|
|
|
* @param string $db_name The database name |
123
|
|
|
*/ |
124
|
|
|
function db_fix_prefix(&$db_prefix, $db_name) |
125
|
|
|
{ |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Callback for preg_replace_callback on the query. |
131
|
|
|
* It allows to replace on the fly a few pre-defined strings, for convenience ('query_see_board', 'query_wanna_see_board', etc), with |
132
|
|
|
* their current values from $user_info. |
133
|
|
|
* In addition, it performs checks and sanitization on the values sent to the database. |
134
|
|
|
* |
135
|
|
|
* @param array $matches The matches from preg_replace_callback |
136
|
|
|
* @return string The appropriate string depending on $matches[1] |
137
|
|
|
*/ |
138
|
|
|
function smf_db_replacement__callback($matches) |
139
|
|
|
{ |
140
|
|
|
global $db_callback, $user_info, $db_prefix, $smcFunc; |
141
|
|
|
|
142
|
|
|
list ($values, $connection) = $db_callback; |
143
|
|
|
|
144
|
|
|
if ($matches[1] === 'db_prefix') |
145
|
|
|
return $db_prefix; |
146
|
|
|
|
147
|
|
|
if (isset($user_info[$matches[1]]) && strpos($matches[1], 'query_') !== false) |
148
|
|
|
return $user_info[$matches[1]]; |
149
|
|
|
|
150
|
|
|
if ($matches[1] === 'empty') |
151
|
|
|
return '\'\''; |
152
|
|
|
|
153
|
|
|
if (!isset($matches[2])) |
154
|
|
|
smf_db_error_backtrace('Invalid value inserted or no type specified.', '', E_USER_ERROR, __FILE__, __LINE__); |
155
|
|
|
|
156
|
|
|
if ($matches[1] === 'literal') |
157
|
|
|
return '\'' . pg_escape_string($matches[2]) . '\''; |
|
|
|
|
158
|
|
|
|
159
|
|
|
if (!isset($values[$matches[2]])) |
160
|
|
|
smf_db_error_backtrace('The database value you\'re trying to insert does not exist: ' . (isset($smcFunc['htmlspecialchars']) ? $smcFunc['htmlspecialchars']($matches[2]) : htmlspecialchars($matches[2])), '', E_USER_ERROR, __FILE__, __LINE__); |
161
|
|
|
|
162
|
|
|
$replacement = $values[$matches[2]]; |
163
|
|
|
|
164
|
|
|
switch ($matches[1]) |
165
|
|
|
{ |
166
|
|
|
case 'int': |
167
|
|
|
if (!is_numeric($replacement) || (string) $replacement !== (string) (int) $replacement) |
168
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Integer expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
169
|
|
|
return (string) (int) $replacement; |
170
|
|
|
break; |
|
|
|
|
171
|
|
|
|
172
|
|
|
case 'string': |
173
|
|
|
case 'text': |
174
|
|
|
return sprintf('\'%1$s\'', pg_escape_string($replacement)); |
175
|
|
|
break; |
176
|
|
|
|
177
|
|
|
case 'array_int': |
178
|
|
|
if (is_array($replacement)) |
179
|
|
|
{ |
180
|
|
|
if (empty($replacement)) |
181
|
|
|
smf_db_error_backtrace('Database error, given array of integer values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
182
|
|
|
|
183
|
|
|
foreach ($replacement as $key => $value) |
184
|
|
|
{ |
185
|
|
|
if (!is_numeric($value) || (string) $value !== (string) (int) $value) |
186
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
187
|
|
|
|
188
|
|
|
$replacement[$key] = (string) (int) $value; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return implode(', ', $replacement); |
192
|
|
|
} |
193
|
|
|
else |
194
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Array of integers expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
195
|
|
|
|
196
|
|
|
break; |
197
|
|
|
|
198
|
|
|
case 'array_string': |
199
|
|
|
if (is_array($replacement)) |
200
|
|
|
{ |
201
|
|
|
if (empty($replacement)) |
202
|
|
|
smf_db_error_backtrace('Database error, given array of string values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
203
|
|
|
|
204
|
|
|
foreach ($replacement as $key => $value) |
205
|
|
|
$replacement[$key] = sprintf('\'%1$s\'', pg_escape_string($value)); |
206
|
|
|
|
207
|
|
|
return implode(', ', $replacement); |
208
|
|
|
} |
209
|
|
|
else |
210
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Array of strings expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
211
|
|
|
break; |
212
|
|
|
|
213
|
|
|
case 'date': |
214
|
|
|
if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d)$~', $replacement, $date_matches) === 1) |
215
|
|
|
return sprintf('\'%04d-%02d-%02d\'', $date_matches[1], $date_matches[2], $date_matches[3]) . '::date'; |
216
|
|
|
else |
217
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Date expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
218
|
|
|
break; |
219
|
|
|
|
220
|
|
|
case 'time': |
221
|
|
|
if (preg_match('~^([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $time_matches) === 1) |
222
|
|
|
return sprintf('\'%02d:%02d:%02d\'', $time_matches[1], $time_matches[2], $time_matches[3]) . '::time'; |
223
|
|
|
else |
224
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Time expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
225
|
|
|
break; |
226
|
|
|
|
227
|
|
|
case 'datetime': |
228
|
|
|
if (preg_match('~^(\d{4})-([0-1]?\d)-([0-3]?\d) ([0-1]?\d|2[0-3]):([0-5]\d):([0-5]\d)$~', $replacement, $datetime_matches) === 1) |
229
|
|
|
return 'to_timestamp(' . |
230
|
|
|
sprintf('\'%04d-%02d-%02d %02d:%02d:%02d\'', $datetime_matches[1], $datetime_matches[2], $datetime_matches[3], $datetime_matches[4], $datetime_matches[5], $datetime_matches[6]) . |
231
|
|
|
',\'YYYY-MM-DD HH24:MI:SS\')'; |
232
|
|
|
else |
233
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Datetime expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
234
|
|
|
break; |
235
|
|
|
|
236
|
|
|
case 'float': |
237
|
|
|
if (!is_numeric($replacement)) |
238
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Floating point number expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
239
|
|
|
return (string) (float) $replacement; |
240
|
|
|
break; |
241
|
|
|
|
242
|
|
|
case 'identifier': |
243
|
|
|
return '"' . strtr($replacement, array('`' => '', '.' => '"."')) . '"'; |
244
|
|
|
break; |
245
|
|
|
|
246
|
|
|
case 'raw': |
247
|
|
|
return $replacement; |
248
|
|
|
break; |
249
|
|
|
|
250
|
|
|
case 'inet': |
251
|
|
|
if ($replacement == 'null' || $replacement == '') |
252
|
|
|
return 'null'; |
253
|
|
|
if (inet_pton($replacement) === false) |
254
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
255
|
|
|
return sprintf('\'%1$s\'::inet', pg_escape_string($replacement)); |
256
|
|
|
|
257
|
|
|
case 'array_inet': |
258
|
|
|
if (is_array($replacement)) |
259
|
|
|
{ |
260
|
|
|
if (empty($replacement)) |
261
|
|
|
smf_db_error_backtrace('Database error, given array of IPv4 or IPv6 values is empty. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
262
|
|
|
|
263
|
|
|
foreach ($replacement as $key => $value) |
264
|
|
|
{ |
265
|
|
|
if ($replacement == 'null' || $replacement == '') |
266
|
|
|
$replacement[$key] = 'null'; |
267
|
|
|
if (!isValidIP($value)) |
268
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. IPv4 or IPv6 expected.(' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
269
|
|
|
$replacement[$key] = sprintf('\'%1$s\'::inet', pg_escape_string($value)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return implode(', ', $replacement); |
273
|
|
|
} |
274
|
|
|
else |
275
|
|
|
smf_db_error_backtrace('Wrong value type sent to the database. Array of IPv4 or IPv6 expected. (' . $matches[2] . ')', '', E_USER_ERROR, __FILE__, __LINE__); |
276
|
|
|
break; |
277
|
|
|
|
278
|
|
|
default: |
279
|
|
|
smf_db_error_backtrace('Undefined type used in the database query. (' . $matches[1] . ':' . $matches[2] . ')', '', false, __FILE__, __LINE__); |
280
|
|
|
break; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Just like the db_query, escape and quote a string, but not executing the query. |
286
|
|
|
* |
287
|
|
|
* @param string $db_string The database string |
288
|
|
|
* @param array $db_values An array of values to be injected into the string |
289
|
|
|
* @param resource $connection = null The connection to use (null to use $db_connection) |
290
|
|
|
* @return string The string with the values inserted |
291
|
|
|
*/ |
292
|
|
|
function smf_db_quote($db_string, $db_values, $connection = null) |
293
|
|
|
{ |
294
|
|
|
global $db_callback, $db_connection; |
295
|
|
|
|
296
|
|
|
// Only bother if there's something to replace. |
297
|
|
|
if (strpos($db_string, '{') !== false) |
298
|
|
|
{ |
299
|
|
|
// This is needed by the callback function. |
300
|
|
|
$db_callback = array($db_values, $connection === null ? $db_connection : $connection); |
301
|
|
|
|
302
|
|
|
// Do the quoting and escaping |
303
|
|
|
$db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'smf_db_replacement__callback', $db_string); |
304
|
|
|
|
305
|
|
|
// Clear this global variable. |
306
|
|
|
$db_callback = array(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $db_string; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Do a query. Takes care of errors too. |
314
|
|
|
* Special queries may need additional replacements to be appropriate |
315
|
|
|
* for PostgreSQL. |
316
|
|
|
* |
317
|
|
|
* @param string $identifier An identifier. Only used in Postgres when we need to do things differently... |
318
|
|
|
* @param string $db_string The database string |
319
|
|
|
* @param array $db_values = array() The values to be inserted into the string |
320
|
|
|
* @param resource $connection = null The connection to use (null to use $db_connection) |
321
|
|
|
* @return resource|bool Returns a MySQL result resource (for SELECT queries), true (for UPDATE queries) or false if the query failed |
322
|
|
|
*/ |
323
|
|
|
function smf_db_query($identifier, $db_string, $db_values = array(), $connection = null) |
324
|
|
|
{ |
325
|
|
|
global $db_cache, $db_count, $db_connection, $db_show_debug; |
326
|
|
|
global $db_callback, $db_last_result, $db_replace_result, $modSettings; |
327
|
|
|
|
328
|
|
|
// Decide which connection to use. |
329
|
|
|
$connection = $connection === null ? $db_connection : $connection; |
330
|
|
|
|
331
|
|
|
// Special queries that need processing. |
332
|
|
|
$replacements = array( |
333
|
|
|
'get_random_number' => array( |
334
|
|
|
'~RAND~' => 'RANDOM', |
335
|
|
|
), |
336
|
|
|
'insert_log_search_topics' => array( |
337
|
|
|
'~NOT RLIKE~' => '!~', |
338
|
|
|
), |
339
|
|
|
'insert_log_search_results_no_index' => array( |
340
|
|
|
'~NOT RLIKE~' => '!~', |
341
|
|
|
), |
342
|
|
|
'insert_log_search_results_subject' => array( |
343
|
|
|
'~NOT RLIKE~' => '!~', |
344
|
|
|
), |
345
|
|
|
'profile_board_stats' => array( |
346
|
|
|
'~COUNT\(\*\) \/ MAX\(b.num_posts\)~' => 'CAST(COUNT(*) AS DECIMAL) / CAST(b.num_posts AS DECIMAL)', |
347
|
|
|
), |
348
|
|
|
); |
349
|
|
|
|
350
|
|
|
// Special optimizer Hints |
351
|
|
|
$query_opt = array( |
352
|
|
|
'load_board_info' => array( |
353
|
|
|
'join_collapse_limit' => 1, |
354
|
|
|
), |
355
|
|
|
'calendar_get_events' => array( |
356
|
|
|
'enable_seqscan' => 'off', |
357
|
|
|
), |
358
|
|
|
); |
359
|
|
|
|
360
|
|
|
if (isset($replacements[$identifier])) |
361
|
|
|
$db_string = preg_replace(array_keys($replacements[$identifier]), array_values($replacements[$identifier]), $db_string); |
362
|
|
|
|
363
|
|
|
// Limits need to be a little different. |
364
|
|
|
$db_string = preg_replace('~\sLIMIT\s(\d+|{int:.+}),\s*(\d+|{int:.+})\s*$~i', 'LIMIT $2 OFFSET $1', $db_string); |
365
|
|
|
|
366
|
|
|
if (trim($db_string) == '') |
367
|
|
|
return false; |
368
|
|
|
|
369
|
|
|
// Comments that are allowed in a query are preg_removed. |
370
|
|
|
static $allowed_comments_from = array( |
371
|
|
|
'~\s+~s', |
372
|
|
|
'~/\*!40001 SQL_NO_CACHE \*/~', |
373
|
|
|
'~/\*!40000 USE INDEX \([A-Za-z\_]+?\) \*/~', |
374
|
|
|
'~/\*!40100 ON DUPLICATE KEY UPDATE id_msg = \d+ \*/~', |
375
|
|
|
); |
376
|
|
|
static $allowed_comments_to = array( |
377
|
|
|
' ', |
378
|
|
|
'', |
379
|
|
|
'', |
380
|
|
|
'', |
381
|
|
|
); |
382
|
|
|
|
383
|
|
|
// One more query.... |
384
|
|
|
$db_count = !isset($db_count) ? 1 : $db_count + 1; |
385
|
|
|
$db_replace_result = 0; |
386
|
|
|
|
387
|
|
|
if (empty($modSettings['disableQueryCheck']) && strpos($db_string, '\'') !== false && empty($db_values['security_override'])) |
388
|
|
|
smf_db_error_backtrace('Hacking attempt...', 'Illegal character (\') used in query...', true, __FILE__, __LINE__); |
389
|
|
|
|
390
|
|
|
if (empty($db_values['security_override']) && (!empty($db_values) || strpos($db_string, '{db_prefix}') !== false)) |
391
|
|
|
{ |
392
|
|
|
// Pass some values to the global space for use in the callback function. |
393
|
|
|
$db_callback = array($db_values, $connection); |
394
|
|
|
|
395
|
|
|
// Inject the values passed to this function. |
396
|
|
|
$db_string = preg_replace_callback('~{([a-z_]+)(?::([a-zA-Z0-9_-]+))?}~', 'smf_db_replacement__callback', $db_string); |
397
|
|
|
|
398
|
|
|
// This shouldn't be residing in global space any longer. |
399
|
|
|
$db_callback = array(); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
// First, we clean strings out of the query, reduce whitespace, lowercase, and trim - so we can check it over. |
403
|
|
|
if (empty($modSettings['disableQueryCheck'])) |
404
|
|
|
{ |
405
|
|
|
$clean = ''; |
406
|
|
|
$old_pos = 0; |
407
|
|
|
$pos = -1; |
408
|
|
|
// Remove the string escape for better runtime |
409
|
|
|
$db_string_1 = str_replace('\'\'', '', $db_string); |
410
|
|
|
while (true) |
411
|
|
|
{ |
412
|
|
|
$pos = strpos($db_string_1, '\'', $pos + 1); |
413
|
|
|
if ($pos === false) |
414
|
|
|
break; |
415
|
|
|
$clean .= substr($db_string_1, $old_pos, $pos - $old_pos); |
416
|
|
|
|
417
|
|
|
while (true) |
418
|
|
|
{ |
419
|
|
|
$pos1 = strpos($db_string_1, '\'', $pos + 1); |
420
|
|
|
$pos2 = strpos($db_string_1, '\\', $pos + 1); |
421
|
|
|
if ($pos1 === false) |
422
|
|
|
break; |
423
|
|
|
elseif ($pos2 === false || $pos2 > $pos1) |
424
|
|
|
{ |
425
|
|
|
$pos = $pos1; |
426
|
|
|
break; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
$pos = $pos2 + 1; |
430
|
|
|
} |
431
|
|
|
$clean .= ' %s '; |
432
|
|
|
|
433
|
|
|
$old_pos = $pos + 1; |
434
|
|
|
} |
435
|
|
|
$clean .= substr($db_string_1, $old_pos); |
436
|
|
|
$clean = trim(strtolower(preg_replace($allowed_comments_from, $allowed_comments_to, $clean))); |
437
|
|
|
|
438
|
|
|
// Comments? We don't use comments in our queries, we leave 'em outside! |
439
|
|
|
if (strpos($clean, '/*') > 2 || strpos($clean, '--') !== false || strpos($clean, ';') !== false) |
440
|
|
|
$fail = true; |
441
|
|
|
// Trying to change passwords, slow us down, or something? |
442
|
|
|
elseif (strpos($clean, 'sleep') !== false && preg_match('~(^|[^a-z])sleep($|[^[_a-z])~s', $clean) != 0) |
443
|
|
|
$fail = true; |
444
|
|
|
elseif (strpos($clean, 'benchmark') !== false && preg_match('~(^|[^a-z])benchmark($|[^[a-z])~s', $clean) != 0) |
445
|
|
|
$fail = true; |
446
|
|
|
|
447
|
|
|
if (!empty($fail) && function_exists('log_error')) |
448
|
|
|
smf_db_error_backtrace('Hacking attempt...', 'Hacking attempt...' . "\n" . $db_string, E_USER_ERROR, __FILE__, __LINE__); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
// Set optimize stuff |
452
|
|
|
if (isset($query_opt[$identifier])) |
453
|
|
|
{ |
454
|
|
|
$query_hints = $query_opt[$identifier]; |
455
|
|
|
$query_hints_set = ''; |
456
|
|
|
if (isset($query_hints['join_collapse_limit'])) |
457
|
|
|
{ |
458
|
|
|
$query_hints_set .= 'SET LOCAL join_collapse_limit = ' . $query_hints['join_collapse_limit'] . ';'; |
459
|
|
|
} |
460
|
|
|
if (isset($query_hints['enable_seqscan'])) |
461
|
|
|
{ |
462
|
|
|
$query_hints_set .= 'SET LOCAL enable_seqscan = ' . $query_hints['enable_seqscan'] . ';'; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$db_string = $query_hints_set . $db_string; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
// Debugging. |
469
|
|
|
if (isset($db_show_debug) && $db_show_debug === true) |
470
|
|
|
{ |
471
|
|
|
// Get the file and line number this function was called. |
472
|
|
|
list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
473
|
|
|
|
474
|
|
|
// Initialize $db_cache if not already initialized. |
475
|
|
|
if (!isset($db_cache)) |
476
|
|
|
$db_cache = array(); |
477
|
|
|
|
478
|
|
|
if (!empty($_SESSION['debug_redirect'])) |
479
|
|
|
{ |
480
|
|
|
$db_cache = array_merge($_SESSION['debug_redirect'], $db_cache); |
481
|
|
|
$db_count = count($db_cache) + 1; |
482
|
|
|
$_SESSION['debug_redirect'] = array(); |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
// Don't overload it. |
486
|
|
|
$db_cache[$db_count]['q'] = $db_count < 50 ? $db_string : '...'; |
487
|
|
|
$db_cache[$db_count]['f'] = $file; |
488
|
|
|
$db_cache[$db_count]['l'] = $line; |
489
|
|
|
$db_cache[$db_count]['s'] = ($st = microtime(true)) - TIME_START; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$db_last_result = @pg_query($connection, $db_string); |
493
|
|
|
|
494
|
|
|
if ($db_last_result === false && empty($db_values['db_error_skip'])) |
495
|
|
|
$db_last_result = smf_db_error($db_string, $connection); |
|
|
|
|
496
|
|
|
|
497
|
|
|
// Debugging. |
498
|
|
|
if (isset($db_show_debug) && $db_show_debug === true) |
499
|
|
|
$db_cache[$db_count]['t'] = microtime(true) - $st; |
|
|
|
|
500
|
|
|
|
501
|
|
|
return $db_last_result; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Returns the amount of affected rows for a query. |
506
|
|
|
* |
507
|
|
|
* @param mixed $result |
508
|
|
|
* |
509
|
|
|
* @return int |
510
|
|
|
* |
511
|
|
|
*/ |
512
|
|
|
function smf_db_affected_rows($result = null) |
513
|
|
|
{ |
514
|
|
|
global $db_last_result, $db_replace_result; |
515
|
|
|
|
516
|
|
|
if ($db_replace_result) |
517
|
|
|
return $db_replace_result; |
518
|
|
|
elseif ($result === null && !$db_last_result) |
519
|
|
|
return 0; |
520
|
|
|
|
521
|
|
|
return pg_affected_rows($result === null ? $db_last_result : $result); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Gets the ID of the most recently inserted row. |
526
|
|
|
* |
527
|
|
|
* @param string $table The table (only used for Postgres) |
528
|
|
|
* @param string $field = null The specific field (not used here) |
529
|
|
|
* @param resource $connection = null The connection (if null, $db_connection is used) (not used here) |
530
|
|
|
* @return int The ID of the most recently inserted row |
531
|
|
|
*/ |
532
|
|
|
function smf_db_insert_id($table, $field = null, $connection = null) |
533
|
|
|
{ |
534
|
|
|
global $smcFunc, $db_prefix; |
535
|
|
|
|
536
|
|
|
$table = str_replace('{db_prefix}', $db_prefix, $table); |
537
|
|
|
|
538
|
|
|
// Try get the last ID for the auto increment field. |
539
|
|
|
$request = $smcFunc['db_query']('', 'SELECT CURRVAL(\'' . $table . '_seq\') AS insertID', |
540
|
|
|
array( |
541
|
|
|
) |
542
|
|
|
); |
543
|
|
|
if (!$request) |
544
|
|
|
return false; |
545
|
|
|
list ($lastID) = $smcFunc['db_fetch_row']($request); |
546
|
|
|
$smcFunc['db_free_result']($request); |
547
|
|
|
|
548
|
|
|
return $lastID; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* Do a transaction. |
553
|
|
|
* |
554
|
|
|
* @param string $type The step to perform (i.e. 'begin', 'commit', 'rollback') |
555
|
|
|
* @param resource $connection The connection to use (if null, $db_connection is used) |
556
|
|
|
* @return bool True if successful, false otherwise |
557
|
|
|
*/ |
558
|
|
|
function smf_db_transaction($type = 'commit', $connection = null) |
559
|
|
|
{ |
560
|
|
|
global $db_connection; |
561
|
|
|
|
562
|
|
|
// Decide which connection to use |
563
|
|
|
$connection = $connection === null ? $db_connection : $connection; |
564
|
|
|
|
565
|
|
|
if ($type == 'begin') |
566
|
|
|
return @pg_query($connection, 'BEGIN'); |
567
|
|
|
elseif ($type == 'rollback') |
568
|
|
|
return @pg_query($connection, 'ROLLBACK'); |
569
|
|
|
elseif ($type == 'commit') |
570
|
|
|
return @pg_query($connection, 'COMMIT'); |
571
|
|
|
|
572
|
|
|
return false; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Database error! |
577
|
|
|
* Backtrace, log, try to fix. |
578
|
|
|
* |
579
|
|
|
* @param string $db_string The DB string |
580
|
|
|
* @param resource $connection The connection to use (if null, $db_connection is used) |
581
|
|
|
*/ |
582
|
|
|
function smf_db_error($db_string, $connection = null) |
583
|
|
|
{ |
584
|
|
|
global $txt, $context, $modSettings; |
585
|
|
|
global $db_connection; |
586
|
|
|
global $db_show_debug; |
587
|
|
|
|
588
|
|
|
// We'll try recovering the file and line number the original db query was called from. |
589
|
|
|
list ($file, $line) = smf_db_error_backtrace('', '', 'return', __FILE__, __LINE__); |
590
|
|
|
|
591
|
|
|
// Decide which connection to use |
592
|
|
|
$connection = $connection === null ? $db_connection : $connection; |
593
|
|
|
|
594
|
|
|
// This is the error message... |
595
|
|
|
$query_error = @pg_last_error($connection); |
596
|
|
|
|
597
|
|
|
// Log the error. |
598
|
|
|
if (function_exists('log_error')) |
599
|
|
|
log_error($txt['database_error'] . ': ' . $query_error . (!empty($modSettings['enableErrorQueryLogging']) ? "\n\n" . $db_string : ''), 'database', $file, $line); |
600
|
|
|
|
601
|
|
|
// Nothing's defined yet... just die with it. |
602
|
|
|
if (empty($context) || empty($txt)) |
603
|
|
|
die($query_error); |
|
|
|
|
604
|
|
|
|
605
|
|
|
// Show an error message, if possible. |
606
|
|
|
$context['error_title'] = $txt['database_error']; |
607
|
|
|
if (allowedTo('admin_forum')) |
608
|
|
|
$context['error_message'] = nl2br($query_error) . '<br>' . $txt['file'] . ': ' . $file . '<br>' . $txt['line'] . ': ' . $line; |
609
|
|
|
else |
610
|
|
|
$context['error_message'] = $txt['try_again']; |
611
|
|
|
|
612
|
|
|
if (allowedTo('admin_forum') && isset($db_show_debug) && $db_show_debug === true) |
613
|
|
|
{ |
614
|
|
|
$context['error_message'] .= '<br><br>' . nl2br($db_string); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
// It's already been logged... don't log it again. |
618
|
|
|
fatal_error($context['error_message'], false); |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Inserts data into a table |
623
|
|
|
* |
624
|
|
|
* @param string $method The insert method - can be 'replace', 'ignore' or 'insert' |
625
|
|
|
* @param string $table The table we're inserting the data into |
626
|
|
|
* @param array $columns An array of the columns we're inserting the data into. Should contain 'column' => 'datatype' pairs |
627
|
|
|
* @param array $data The data to insert |
628
|
|
|
* @param array $keys The keys for the table, needs to be not empty on replace mode |
629
|
|
|
* @param int returnmode 0 = nothing(default), 1 = last row id, 2 = all rows id as array; every mode runs only with method != 'ignore' |
|
|
|
|
630
|
|
|
* @param resource $connection The connection to use (if null, $db_connection is used) |
631
|
|
|
* @return mixed value of the first key, behavior based on returnmode. null if no data. |
632
|
|
|
*/ |
633
|
|
|
function smf_db_insert($method = 'replace', $table, $columns, $data, $keys, $returnmode = 0, $connection = null) |
634
|
|
|
{ |
635
|
|
|
global $smcFunc, $db_connection, $db_prefix; |
636
|
|
|
|
637
|
|
|
$connection = $connection === null ? $db_connection : $connection; |
638
|
|
|
|
639
|
|
|
$replace = ''; |
640
|
|
|
|
641
|
|
|
if (empty($data)) |
642
|
|
|
return; |
643
|
|
|
|
644
|
|
|
if (!is_array($data[array_rand($data)])) |
645
|
|
|
$data = array($data); |
646
|
|
|
|
647
|
|
|
// Replace the prefix holder with the actual prefix. |
648
|
|
|
$table = str_replace('{db_prefix}', $db_prefix, $table); |
649
|
|
|
|
650
|
|
|
// Sanity check for replace is key part of the columns array |
651
|
|
|
if ($method == 'replace') |
652
|
|
|
{ |
653
|
|
|
if (empty($keys)) |
654
|
|
|
smf_db_error_backtrace('When using the replace mode, the key column is a required entry.', |
655
|
|
|
'Change the method of db insert to insert or add the pk field to the key array', E_USER_ERROR, __FILE__, __LINE__); |
656
|
|
|
if (count(array_intersect_key($columns, array_flip($keys))) !== count($keys)) |
657
|
|
|
smf_db_error_backtrace('Primary Key field missing in insert call', |
658
|
|
|
'Change the method of db insert to insert or add the pk field to the columns array', E_USER_ERROR, __FILE__, __LINE__); |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
// PostgreSQL doesn't support replace: we implement a MySQL-compatible behavior instead |
662
|
|
|
if ($method == 'replace' || $method == 'ignore') |
663
|
|
|
{ |
664
|
|
|
$key_str = ''; |
665
|
|
|
$col_str = ''; |
666
|
|
|
$replace_support = $smcFunc['db_native_replace'](); |
667
|
|
|
|
668
|
|
|
$count = 0; |
669
|
|
|
$where = ''; |
670
|
|
|
$count_pk = 0; |
671
|
|
|
|
672
|
|
|
If ($replace_support) |
673
|
|
|
{ |
674
|
|
|
foreach ($columns as $columnName => $type) |
675
|
|
|
{ |
676
|
|
|
//check pk fiel |
677
|
|
|
IF (in_array($columnName, $keys)) |
678
|
|
|
{ |
679
|
|
|
$key_str .= ($count_pk > 0 ? ',' : ''); |
680
|
|
|
$key_str .= $columnName; |
681
|
|
|
$count_pk++; |
682
|
|
|
} |
683
|
|
|
elseif ($method == 'replace') //normal field |
684
|
|
|
{ |
685
|
|
|
$col_str .= ($count > 0 ? ',' : ''); |
686
|
|
|
$col_str .= $columnName . ' = EXCLUDED.' . $columnName; |
687
|
|
|
$count++; |
688
|
|
|
} |
689
|
|
|
} |
690
|
|
|
if ($method == 'replace') |
691
|
|
|
$replace = ' ON CONFLICT (' . $key_str . ') DO UPDATE SET ' . $col_str; |
692
|
|
|
else |
693
|
|
|
$replace = ' ON CONFLICT (' . $key_str . ') DO NOTHING'; |
694
|
|
|
} |
695
|
|
|
elseif ($method == 'replace') |
696
|
|
|
{ |
697
|
|
|
foreach ($columns as $columnName => $type) |
698
|
|
|
{ |
699
|
|
|
// Are we restricting the length? |
700
|
|
|
if (strpos($type, 'string-') !== false) |
701
|
|
|
$actualType = sprintf($columnName . ' = SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $count); |
702
|
|
|
else |
703
|
|
|
$actualType = sprintf($columnName . ' = {%1$s:%2$s}, ', $type, $count); |
704
|
|
|
|
705
|
|
|
// A key? That's what we were looking for. |
706
|
|
|
if (in_array($columnName, $keys)) |
707
|
|
|
$where .= (empty($where) ? '' : ' AND ') . substr($actualType, 0, -2); |
708
|
|
|
$count++; |
709
|
|
|
} |
710
|
|
|
|
711
|
|
|
// Make it so. |
712
|
|
|
if (!empty($where) && !empty($data)) |
713
|
|
|
{ |
714
|
|
|
foreach ($data as $k => $entry) |
715
|
|
|
{ |
716
|
|
|
$smcFunc['db_query']('', ' |
717
|
|
|
DELETE FROM ' . $table . |
718
|
|
|
' WHERE ' . $where, |
719
|
|
|
$entry, $connection |
720
|
|
|
); |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
} |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
$returning = ''; |
727
|
|
|
$with_returning = false; |
728
|
|
|
// lets build the returning string, mysql allow only in normal mode |
729
|
|
|
if (!empty($keys) && (count($keys) > 0) && $returnmode > 0) |
730
|
|
|
{ |
731
|
|
|
// we only take the first key |
732
|
|
|
$returning = ' RETURNING ' . $keys[0]; |
733
|
|
|
$with_returning = true; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
if (!empty($data)) |
737
|
|
|
{ |
738
|
|
|
// Create the mold for a single row insert. |
739
|
|
|
$insertData = '('; |
740
|
|
|
foreach ($columns as $columnName => $type) |
741
|
|
|
{ |
742
|
|
|
// Are we restricting the length? |
743
|
|
|
if (strpos($type, 'string-') !== false) |
744
|
|
|
$insertData .= sprintf('SUBSTRING({string:%1$s}, 1, ' . substr($type, 7) . '), ', $columnName); |
745
|
|
|
else |
746
|
|
|
$insertData .= sprintf('{%1$s:%2$s}, ', $type, $columnName); |
747
|
|
|
} |
748
|
|
|
$insertData = substr($insertData, 0, -2) . ')'; |
749
|
|
|
|
750
|
|
|
// Create an array consisting of only the columns. |
751
|
|
|
$indexed_columns = array_keys($columns); |
752
|
|
|
|
753
|
|
|
// Here's where the variables are injected to the query. |
754
|
|
|
$insertRows = array(); |
755
|
|
|
foreach ($data as $dataRow) |
756
|
|
|
$insertRows[] = smf_db_quote($insertData, array_combine($indexed_columns, $dataRow), $connection); |
|
|
|
|
757
|
|
|
|
758
|
|
|
// Do the insert. |
759
|
|
|
$request = $smcFunc['db_query']('', ' |
760
|
|
|
INSERT INTO ' . $table . '("' . implode('", "', $indexed_columns) . '") |
761
|
|
|
VALUES |
762
|
|
|
' . implode(', |
763
|
|
|
', $insertRows) . $replace . $returning, |
764
|
|
|
array( |
765
|
|
|
'security_override' => true, |
766
|
|
|
'db_error_skip' => $method == 'ignore' || $table === $db_prefix . 'log_errors', |
767
|
|
|
), |
768
|
|
|
$connection |
769
|
|
|
); |
770
|
|
|
|
771
|
|
|
if ($with_returning && $request !== false) |
772
|
|
|
{ |
773
|
|
|
if ($returnmode === 2) |
774
|
|
|
$return_var = array(); |
775
|
|
|
|
776
|
|
|
while (($row = $smcFunc['db_fetch_row']($request)) && $with_returning) |
777
|
|
|
{ |
778
|
|
|
if (is_numeric($row[0])) // try to emulate mysql limitation |
779
|
|
|
{ |
780
|
|
|
if ($returnmode === 1) |
781
|
|
|
$return_var = $row[0]; |
782
|
|
|
elseif ($returnmode === 2) |
783
|
|
|
$return_var[] = $row[0]; |
784
|
|
|
} |
785
|
|
|
else |
786
|
|
|
{ |
787
|
|
|
$with_returning = false; |
788
|
|
|
trigger_error('trying to returning ID Field which is not a Int field', E_USER_ERROR); |
789
|
|
|
} |
790
|
|
|
} |
791
|
|
|
} |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
if ($with_returning && !empty($return_var)) |
795
|
|
|
return $return_var; |
796
|
|
|
} |
797
|
|
|
|
798
|
|
|
/** |
799
|
|
|
* Dummy function really. Doesn't do anything on PostgreSQL. |
800
|
|
|
* |
801
|
|
|
* @param string $db_name The database name |
802
|
|
|
* @param resource $db_connection The database connection |
803
|
|
|
* @return true Always returns true |
804
|
|
|
*/ |
805
|
|
|
function smf_db_select_db($db_name, $db_connection) |
806
|
|
|
{ |
807
|
|
|
return true; |
808
|
|
|
} |
809
|
|
|
|
810
|
|
|
/** |
811
|
|
|
* Get the current version. |
812
|
|
|
* |
813
|
|
|
* @return string The client version |
814
|
|
|
*/ |
815
|
|
|
function smf_db_version() |
816
|
|
|
{ |
817
|
|
|
$version = pg_version(); |
818
|
|
|
|
819
|
|
|
return $version['client']; |
820
|
|
|
} |
821
|
|
|
|
822
|
|
|
/** |
823
|
|
|
* This function tries to work out additional error information from a back trace. |
824
|
|
|
* |
825
|
|
|
* @param string $error_message The error message |
826
|
|
|
* @param string $log_message The message to log |
827
|
|
|
* @param string|bool $error_type What type of error this is |
828
|
|
|
* @param string $file The file the error occurred in |
829
|
|
|
* @param int $line What line of $file the code which generated the error is on |
830
|
|
|
* @return void|array Returns an array with the file and line if $error_type is 'return' |
831
|
|
|
*/ |
832
|
|
|
function smf_db_error_backtrace($error_message, $log_message = '', $error_type = false, $file = null, $line = null) |
833
|
|
|
{ |
834
|
|
|
if (empty($log_message)) |
835
|
|
|
$log_message = $error_message; |
836
|
|
|
|
837
|
|
|
foreach (debug_backtrace() as $step) |
838
|
|
|
{ |
839
|
|
|
// Found it? |
840
|
|
|
if (strpos($step['function'], 'query') === false && !in_array(substr($step['function'], 0, 7), array('smf_db_', 'preg_re', 'db_erro', 'call_us')) && strpos($step['function'], '__') !== 0) |
841
|
|
|
{ |
842
|
|
|
$log_message .= '<br>Function: ' . $step['function']; |
843
|
|
|
break; |
844
|
|
|
} |
845
|
|
|
|
846
|
|
|
if (isset($step['line'])) |
847
|
|
|
{ |
848
|
|
|
$file = $step['file']; |
849
|
|
|
$line = $step['line']; |
850
|
|
|
} |
851
|
|
|
} |
852
|
|
|
|
853
|
|
|
// A special case - we want the file and line numbers for debugging. |
854
|
|
|
if ($error_type == 'return') |
855
|
|
|
return array($file, $line); |
856
|
|
|
|
857
|
|
|
// Is always a critical error. |
858
|
|
|
if (function_exists('log_error')) |
859
|
|
|
log_error($log_message, 'critical', $file, $line); |
860
|
|
|
|
861
|
|
|
if (function_exists('fatal_error')) |
862
|
|
|
{ |
863
|
|
|
fatal_error($error_message, $error_type); |
864
|
|
|
|
865
|
|
|
// Cannot continue... |
866
|
|
|
exit; |
|
|
|
|
867
|
|
|
} |
868
|
|
|
elseif ($error_type) |
869
|
|
|
trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : ''), $error_type); |
|
|
|
|
870
|
|
|
else |
871
|
|
|
trigger_error($error_message . ($line !== null ? '<em>(' . basename($file) . '-' . $line . ')</em>' : '')); |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* Escape the LIKE wildcards so that they match the character and not the wildcard. |
876
|
|
|
* |
877
|
|
|
* @param string $string The string to escape |
878
|
|
|
* @param bool $translate_human_wildcards If true, turns human readable wildcards into SQL wildcards. |
879
|
|
|
* @return string The escaped string |
880
|
|
|
*/ |
881
|
|
|
function smf_db_escape_wildcard_string($string, $translate_human_wildcards = false) |
882
|
|
|
{ |
883
|
|
|
$replacements = array( |
884
|
|
|
'%' => '\%', |
885
|
|
|
'_' => '\_', |
886
|
|
|
'\\' => '\\\\', |
887
|
|
|
); |
888
|
|
|
|
889
|
|
|
if ($translate_human_wildcards) |
890
|
|
|
$replacements += array( |
891
|
|
|
'*' => '%', |
892
|
|
|
); |
893
|
|
|
|
894
|
|
|
return strtr($string, $replacements); |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
/** |
898
|
|
|
* Fetches all rows from a result as an array |
899
|
|
|
* |
900
|
|
|
* @param resource $request A PostgreSQL result resource |
901
|
|
|
* @return array An array that contains all rows (records) in the result resource |
902
|
|
|
*/ |
903
|
|
|
function smf_db_fetch_all($request) |
904
|
|
|
{ |
905
|
|
|
// Return the right row. |
906
|
|
|
$return = @pg_fetch_all($request); |
907
|
|
|
return !empty($return) ? $return : array(); |
908
|
|
|
} |
909
|
|
|
|
910
|
|
|
/** |
911
|
|
|
* Function to save errors in database in a safe way |
912
|
|
|
* |
913
|
|
|
* @param array with keys in this order id_member, log_time, ip, url, message, session, error_type, file, line |
|
|
|
|
914
|
|
|
* @return void |
915
|
|
|
*/ |
916
|
|
|
function smf_db_error_insert($error_array) |
917
|
|
|
{ |
918
|
|
|
global $db_prefix, $db_connection, $db_persist; |
919
|
|
|
static $pg_error_data_prep; |
920
|
|
|
|
921
|
|
|
// without database we can't do anything |
922
|
|
|
if (empty($db_connection)) |
923
|
|
|
return; |
924
|
|
|
|
925
|
|
|
if (filter_var($error_array[2], FILTER_VALIDATE_IP) === false) |
926
|
|
|
$error_array[2] = null; |
927
|
|
|
|
928
|
|
|
if(empty($db_persist)) |
929
|
|
|
{ // without pooling |
930
|
|
|
if (empty($pg_error_data_prep)) |
931
|
|
|
$pg_error_data_prep = pg_prepare($db_connection, 'smf_log_errors', |
932
|
|
|
'INSERT INTO ' . $db_prefix . 'log_errors |
933
|
|
|
(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace) |
934
|
|
|
VALUES( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' |
935
|
|
|
); |
936
|
|
|
|
937
|
|
|
pg_execute($db_connection, 'smf_log_errors', $error_array); |
938
|
|
|
} |
939
|
|
|
else |
940
|
|
|
{ //with pooling |
941
|
|
|
$pg_error_data_prep = pg_prepare($db_connection, '', |
942
|
|
|
'INSERT INTO ' . $db_prefix . 'log_errors |
943
|
|
|
(id_member, log_time, ip, url, message, session, error_type, file, line, backtrace) |
944
|
|
|
VALUES( $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' |
945
|
|
|
); |
946
|
|
|
|
947
|
|
|
pg_execute($db_connection, '', $error_array); |
948
|
|
|
} |
949
|
|
|
|
950
|
|
|
} |
951
|
|
|
|
952
|
|
|
/** |
953
|
|
|
* Function which constructs an optimize custom order string |
954
|
|
|
* as an improved alternative to find_in_set() |
955
|
|
|
* |
956
|
|
|
* @param string $field name |
957
|
|
|
* @param array $array_values Field values sequenced in array via order priority. Must cast to int. |
958
|
|
|
* @param boolean $desc default false |
959
|
|
|
* @return string case field when ... then ... end |
960
|
|
|
*/ |
961
|
|
|
function smf_db_custom_order($field, $array_values, $desc = false) |
962
|
|
|
{ |
963
|
|
|
$return = 'CASE ' . $field . ' '; |
964
|
|
|
$count = count($array_values); |
965
|
|
|
$then = ($desc ? ' THEN -' : ' THEN '); |
966
|
|
|
|
967
|
|
|
for ($i = 0; $i < $count; $i++) |
968
|
|
|
$return .= 'WHEN ' . (int) $array_values[$i] . $then . $i . ' '; |
969
|
|
|
|
970
|
|
|
$return .= 'END'; |
971
|
|
|
return $return; |
972
|
|
|
} |
973
|
|
|
|
974
|
|
|
/** |
975
|
|
|
* Function which return the information if the database supports native replace inserts |
976
|
|
|
* |
977
|
|
|
* @return boolean true or false |
978
|
|
|
*/ |
979
|
|
|
function smf_db_native_replace() |
980
|
|
|
{ |
981
|
|
|
global $smcFunc; |
982
|
|
|
static $pg_version; |
983
|
|
|
static $replace_support; |
984
|
|
|
|
985
|
|
|
if (empty($pg_version)) |
986
|
|
|
{ |
987
|
|
|
db_extend(); |
988
|
|
|
//pg 9.5 got replace support |
989
|
|
|
$pg_version = $smcFunc['db_get_version'](); |
990
|
|
|
// if we got a Beta Version |
991
|
|
|
if (stripos($pg_version, 'beta') !== false) |
992
|
|
|
$pg_version = substr($pg_version, 0, stripos($pg_version, 'beta')) . '.0'; |
993
|
|
|
// or RC |
994
|
|
|
if (stripos($pg_version, 'rc') !== false) |
995
|
|
|
$pg_version = substr($pg_version, 0, stripos($pg_version, 'rc')) . '.0'; |
996
|
|
|
|
997
|
|
|
$replace_support = (version_compare($pg_version, '9.5.0', '>=') ? true : false); |
998
|
|
|
} |
999
|
|
|
|
1000
|
|
|
return $replace_support; |
1001
|
|
|
} |
1002
|
|
|
|
1003
|
|
|
/** |
1004
|
|
|
* Function which return the information if the database supports cte with recursive |
1005
|
|
|
* |
1006
|
|
|
* @return boolean true or false |
1007
|
|
|
*/ |
1008
|
|
|
function smf_db_cte_support() |
1009
|
|
|
{ |
1010
|
|
|
return true; |
1011
|
|
|
} |
1012
|
|
|
|
1013
|
|
|
/** |
1014
|
|
|
* Function which return the escaped string |
1015
|
|
|
* |
1016
|
|
|
* @param string the unescaped text |
|
|
|
|
1017
|
|
|
* @param resource $connection = null The connection to use (null to use $db_connection) |
1018
|
|
|
* @return string escaped string |
1019
|
|
|
*/ |
1020
|
|
|
function smf_db_escape_string($string, $connection = null) |
1021
|
|
|
{ |
1022
|
|
|
global $db_connection; |
1023
|
|
|
|
1024
|
|
|
return pg_escape_string($connection === null ? $db_connection : $connection, $string); |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
?> |