82474746 |
1 | <?php |
15e6162e |
2 | |
d6c32258 |
3 | /** |
35586184 |
4 | * db_prefs.php |
5 | * |
35586184 |
6 | * This contains functions for manipulating user preferences |
21ddfbc3 |
7 | * stored in a database, accessed through the Pear DB layer |
8 | * or PDO, the latter taking precedence if available. |
35586184 |
9 | * |
35586184 |
10 | * Database: |
35586184 |
11 | * |
99a6c222 |
12 | * The preferences table should have three columns: |
13 | * user char \ primary |
35586184 |
14 | * prefkey char / key |
15 | * prefval blob |
16 | * |
4b7dd3d9 |
17 | * CREATE TABLE userprefs (user CHAR(128) NOT NULL DEFAULT '', |
35586184 |
18 | * prefkey CHAR(64) NOT NULL DEFAULT '', |
19 | * prefval BLOB NOT NULL DEFAULT '', |
20 | * primary key (user,prefkey)); |
21 | * |
22 | * Configuration of databasename, username and password is done |
3499f99f |
23 | * by using conf.pl or the administrator plugin |
35586184 |
24 | * |
21ddfbc3 |
25 | * Three settings that control PDO behavior can be specified in |
26 | * config/config_local.php if needed: |
27 | * boolean $disable_pdo SquirrelMail uses PDO by default to access the |
28 | * user preferences and address book databases, but |
29 | * setting this to TRUE will cause SquirrelMail to |
30 | * fall back to using Pear DB instead. |
31 | * boolean $pdo_show_sql_errors When database errors are encountered, |
32 | * setting this to TRUE causes the actual |
33 | * database error to be displayed, otherwise |
34 | * generic errors are displayed, preventing |
35 | * internal database information from being |
36 | * exposed. This should be enabled only for |
37 | * debugging purposes. |
38 | * string $pdo_identifier_quote_char By default, SquirrelMail will quote |
39 | * table and field names in database |
40 | * queries with what it thinks is the |
41 | * appropriate quote character for the |
42 | * database type being used (backtick |
43 | * for MySQL (and thus MariaDB), double |
44 | * quotes for all others), but you can |
45 | * override the character used by |
46 | * putting it here, or tell SquirrelMail |
47 | * NOT to quote identifiers by setting |
48 | * this to "none" |
49 | * |
22387c8d |
50 | * @copyright 1999-2017 The SquirrelMail Project Team |
4b4abf93 |
51 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
52 | * @version $Id$ |
d6c32258 |
53 | * @package squirrelmail |
ace4c62c |
54 | * @subpackage prefs |
55 | * @since 1.1.3 |
35586184 |
56 | */ |
57 | |
ace4c62c |
58 | /** @ignore */ |
59 | if (!defined('SM_PATH')) define('SM_PATH','../'); |
60 | |
d6c32258 |
61 | /** Unknown database */ |
98749983 |
62 | define('SMDB_UNKNOWN', 0); |
d6c32258 |
63 | /** MySQL */ |
98749983 |
64 | define('SMDB_MYSQL', 1); |
d6c32258 |
65 | /** PostgreSQL */ |
98749983 |
66 | define('SMDB_PGSQL', 2); |
67 | |
099fea11 |
68 | /** |
21ddfbc3 |
69 | * Needs either PDO or the DB functions |
70 | * Don't display errors here. (no code execution in functions/*.php). |
099fea11 |
71 | * will handle error in dbPrefs class. |
72 | */ |
21ddfbc3 |
73 | global $use_pdo, $disable_pdo; |
74 | if (empty($disable_pdo) && class_exists('PDO')) |
75 | $use_pdo = TRUE; |
76 | else |
77 | $use_pdo = FALSE; |
78 | |
79 | if (!$use_pdo) |
80 | @include_once('DB.php'); |
35586184 |
81 | |
370059dd |
82 | global $prefs_are_cached, $prefs_cache; |
2d367c68 |
83 | |
4d30c1b7 |
84 | /** |
85 | * @ignore |
86 | */ |
370059dd |
87 | function cachePrefValues($username) { |
88 | global $prefs_are_cached, $prefs_cache; |
89 | |
37d5278d |
90 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
370059dd |
91 | if ($prefs_are_cached) { |
37d5278d |
92 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
370059dd |
93 | return; |
94 | } |
2d367c68 |
95 | |
9eb0fbd4 |
96 | sqsession_unregister('prefs_cache'); |
97 | sqsession_unregister('prefs_are_cached'); |
370059dd |
98 | |
99 | $db = new dbPrefs; |
100 | if(isset($db->error)) { |
101 | printf( _("Preference database error (%s). Exiting abnormally"), |
102 | $db->error); |
103 | exit; |
104 | } |
2d367c68 |
105 | |
370059dd |
106 | $db->fillPrefsCache($username); |
107 | if (isset($db->error)) { |
108 | printf( _("Preference database error (%s). Exiting abnormally"), |
109 | $db->error); |
110 | exit; |
111 | } |
112 | |
113 | $prefs_are_cached = true; |
114 | |
9eb0fbd4 |
115 | sqsession_register($prefs_cache, 'prefs_cache'); |
116 | sqsession_register($prefs_are_cached, 'prefs_are_cached'); |
370059dd |
117 | } |
118 | |
d6c32258 |
119 | /** |
ace4c62c |
120 | * Class used to handle connections to prefs database and operations with preferences |
37b11ab0 |
121 | * |
d6c32258 |
122 | * @package squirrelmail |
ace4c62c |
123 | * @subpackage prefs |
124 | * @since 1.1.3 |
37b11ab0 |
125 | * |
d6c32258 |
126 | */ |
370059dd |
127 | class dbPrefs { |
ace4c62c |
128 | /** |
129 | * Table used to store preferences |
130 | * @var string |
131 | */ |
370059dd |
132 | var $table = 'userprefs'; |
37b11ab0 |
133 | |
ace4c62c |
134 | /** |
135 | * Field used to store owner of preference |
136 | * @var string |
137 | */ |
99a6c222 |
138 | var $user_field = 'user'; |
37b11ab0 |
139 | |
ace4c62c |
140 | /** |
141 | * Field used to store preference name |
142 | * @var string |
143 | */ |
99a6c222 |
144 | var $key_field = 'prefkey'; |
37b11ab0 |
145 | |
ace4c62c |
146 | /** |
147 | * Field used to store preference value |
148 | * @var string |
149 | */ |
99a6c222 |
150 | var $val_field = 'prefval'; |
370059dd |
151 | |
ace4c62c |
152 | /** |
153 | * Database connection object |
154 | * @var object |
155 | */ |
370059dd |
156 | var $dbh = NULL; |
37b11ab0 |
157 | |
ace4c62c |
158 | /** |
159 | * Error messages |
160 | * @var string |
161 | */ |
370059dd |
162 | var $error = NULL; |
37b11ab0 |
163 | |
ace4c62c |
164 | /** |
165 | * Database type (SMDB_* constants) |
166 | * Is used in setKey(). |
167 | * @var integer |
168 | */ |
98749983 |
169 | var $db_type = SMDB_UNKNOWN; |
370059dd |
170 | |
21ddfbc3 |
171 | /** |
172 | * Character used to quote database table |
173 | * and field names |
174 | * @var string |
175 | */ |
176 | var $identifier_quote_char = ''; |
177 | |
ace4c62c |
178 | /** |
179 | * Default preferences |
180 | * @var array |
181 | */ |
2ea6df85 |
182 | var $default = Array('theme_default' => 0, |
102b278b |
183 | 'include_self_reply_all' => '0', |
184 | 'do_not_reply_to_self' => '1', |
370059dd |
185 | 'show_html_default' => '0'); |
186 | |
06316c07 |
187 | /** |
188 | * Preference owner field size |
189 | * @var integer |
190 | * @since 1.5.1 |
191 | */ |
192 | var $user_size = 128; |
37b11ab0 |
193 | |
06316c07 |
194 | /** |
195 | * Preference key field size |
196 | * @var integer |
197 | * @since 1.5.1 |
198 | */ |
199 | var $key_size = 64; |
37b11ab0 |
200 | |
06316c07 |
201 | /** |
202 | * Preference value field size |
203 | * @var integer |
204 | * @since 1.5.1 |
205 | */ |
206 | var $val_size = 65536; |
207 | |
37b11ab0 |
208 | |
209 | |
3ec364a4 |
210 | /** |
634f552a |
211 | * Constructor (PHP5 style, required in some future version of PHP) |
3ec364a4 |
212 | * initialize the default preferences array. |
213 | * |
214 | */ |
634f552a |
215 | function __construct() { |
3ec364a4 |
216 | // Try and read the default preferences file. |
217 | $default_pref = SM_PATH . 'config/default_pref'; |
218 | if (@file_exists($default_pref)) { |
219 | if ($file = @fopen($default_pref, 'r')) { |
220 | while (!feof($file)) { |
221 | $pref = fgets($file, 1024); |
222 | $i = strpos($pref, '='); |
223 | if ($i > 0) { |
224 | $this->default[trim(substr($pref, 0, $i))] = trim(substr($pref, $i + 1)); |
225 | } |
226 | } |
227 | fclose($file); |
228 | } |
229 | } |
230 | } |
231 | |
634f552a |
232 | /** |
233 | * Constructor (PHP4 style, kept for compatibility reasons) |
234 | * initialize the default preferences array. |
235 | * |
236 | */ |
237 | function dbPrefs() { |
238 | self::__construct(); |
239 | } |
240 | |
ace4c62c |
241 | /** |
242 | * initialize DB connection object |
37b11ab0 |
243 | * |
ace4c62c |
244 | * @return boolean true, if object is initialized |
37b11ab0 |
245 | * |
ace4c62c |
246 | */ |
370059dd |
247 | function open() { |
21ddfbc3 |
248 | global $prefs_dsn, $prefs_table, $use_pdo, $pdo_identifier_quote_char; |
98749983 |
249 | global $prefs_user_field, $prefs_key_field, $prefs_val_field; |
06316c07 |
250 | global $prefs_user_size, $prefs_key_size, $prefs_val_size; |
3499f99f |
251 | |
21ddfbc3 |
252 | /* test if PDO or Pear DB classes are available and freak out if necessary */ |
253 | if (!$use_pdo && !class_exists('DB')) { |
099fea11 |
254 | // same error also in abook_database.php |
21ddfbc3 |
255 | $error = _("Could not find or include PHP PDO or PEAR database functions required for the database backend.") . "\n"; |
256 | $error .= sprintf(_("PDO should come preinstalled with PHP version 5.1 or higher. Otherwise, is PEAR installed, and is the include path set correctly to find %s?"), 'DB.php') . "\n"; |
257 | $error .= _("Please contact your system administrator and report this error."); |
099fea11 |
258 | return false; |
259 | } |
260 | |
370059dd |
261 | if(isset($this->dbh)) { |
262 | return true; |
263 | } |
3499f99f |
264 | |
21ddfbc3 |
265 | if (strpos($prefs_dsn, 'mysql') === 0) { |
98749983 |
266 | $this->db_type = SMDB_MYSQL; |
21ddfbc3 |
267 | } else if (strpos($prefs_dsn, 'pgsql') === 0) { |
98749983 |
268 | $this->db_type = SMDB_PGSQL; |
269 | } |
270 | |
21ddfbc3 |
271 | // figure out identifier quoting (only used for PDO, though we could change that) |
272 | if (empty($pdo_identifier_quote_char)) { |
273 | if ($this->db_type == SMDB_MYSQL) |
274 | $this->identifier_quote_char = '`'; |
275 | else |
276 | $this->identifier_quote_char = '"'; |
277 | } else if ($pdo_identifier_quote_char === 'none') |
278 | $this->identifier_quote_char = ''; |
279 | else |
280 | $this->identifier_quote_char = $pdo_identifier_quote_char; |
281 | |
3499f99f |
282 | if (!empty($prefs_table)) { |
283 | $this->table = $prefs_table; |
284 | } |
99a6c222 |
285 | if (!empty($prefs_user_field)) { |
286 | $this->user_field = $prefs_user_field; |
287 | } |
865050ce |
288 | |
289 | // the default user field is "user", which in PostgreSQL |
290 | // is an identifier and causes errors if not escaped |
291 | // |
292 | if ($this->db_type == SMDB_PGSQL) { |
293 | $this->user_field = '"' . $this->user_field . '"'; |
294 | } |
295 | |
99a6c222 |
296 | if (!empty($prefs_key_field)) { |
297 | $this->key_field = $prefs_key_field; |
298 | } |
299 | if (!empty($prefs_val_field)) { |
300 | $this->val_field = $prefs_val_field; |
301 | } |
06316c07 |
302 | if (!empty($prefs_user_size)) { |
303 | $this->user_size = (int) $prefs_user_size; |
304 | } |
305 | if (!empty($prefs_key_size)) { |
306 | $this->key_size = (int) $prefs_key_size; |
307 | } |
308 | if (!empty($prefs_val_size)) { |
309 | $this->val_size = (int) $prefs_val_size; |
310 | } |
2d367c68 |
311 | |
21ddfbc3 |
312 | // connect, create database connection object |
313 | // |
314 | if ($use_pdo) { |
315 | // parse and convert DSN to PDO style |
a95fae0b |
316 | // Pear's full DSN syntax is one of the following: |
317 | // phptype(dbsyntax)://username:password@protocol+hostspec/database?option=value |
318 | // phptype(syntax)://user:pass@protocol(proto_opts)/database |
319 | // |
21ddfbc3 |
320 | // $matches will contain: |
321 | // 1: database type |
322 | // 2: username |
323 | // 3: password |
a95fae0b |
324 | // 4: hostname (and possible port number) OR protocol (and possible protocol options) |
325 | // 5: database name (and possible options) |
326 | // 6: port number (moved from match number 4) |
327 | // 7: options (moved from match number 5) |
328 | // 8: protocol (instead of hostname) |
329 | // 9: protocol options (moved from match number 4/8) |
330 | //TODO: do we care about supporting cases where no password is given? (this is a legal DSN, but causes an error below) |
21ddfbc3 |
331 | if (!preg_match('|^(.+)://(.+):(.+)@(.+)/(.+)$|i', $prefs_dsn, $matches)) { |
332 | $this->error = _("Could not parse prefs DSN"); |
333 | return false; |
334 | } |
a95fae0b |
335 | $matches[6] = NULL; |
336 | $matches[7] = NULL; |
337 | $matches[8] = NULL; |
338 | $matches[9] = NULL; |
21ddfbc3 |
339 | if (preg_match('|^(.+):(\d+)$|', $matches[4], $host_port_matches)) { |
340 | $matches[4] = $host_port_matches[1]; |
341 | $matches[6] = $host_port_matches[2]; |
a95fae0b |
342 | } |
343 | if (preg_match('|^(.+?)\((.+)\)$|', $matches[4], $protocol_matches)) { |
344 | $matches[8] = $protocol_matches[1]; |
345 | $matches[9] = $protocol_matches[2]; |
346 | $matches[4] = NULL; |
21ddfbc3 |
347 | $matches[6] = NULL; |
a95fae0b |
348 | } |
349 | //TODO: currently we just ignore options specified on the end of the DSN |
350 | if (preg_match('|^(.+?)\?(.+)$|', $matches[5], $database_name_options_matches)) { |
351 | $matches[5] = $database_name_options_matches[1]; |
352 | $matches[7] = $database_name_options_matches[2]; |
353 | } |
354 | if ($matches[8] === 'unix' && !empty($matches[9])) |
355 | $pdo_prefs_dsn = $matches[1] . ':unix_socket=' . $matches[9] . ';dbname=' . $matches[5]; |
356 | else |
357 | $pdo_prefs_dsn = $matches[1] . ':host=' . $matches[4] . (!empty($matches[6]) ? ';port=' . $matches[6] : '') . ';dbname=' . $matches[5]; |
21ddfbc3 |
358 | try { |
359 | $dbh = new PDO($pdo_prefs_dsn, $matches[2], $matches[3]); |
360 | } catch (Exception $e) { |
361 | $this->error = $e->getMessage(); |
362 | return false; |
363 | } |
364 | } else { |
365 | $dbh = DB::connect($prefs_dsn, true); |
366 | |
367 | if(DB::isError($dbh)) { |
368 | $this->error = DB::errorMessage($dbh); |
369 | return false; |
370 | } |
2d367c68 |
371 | } |
372 | |
373 | $this->dbh = $dbh; |
374 | return true; |
370059dd |
375 | } |
82474746 |
376 | |
ace4c62c |
377 | /** |
378 | * Function used to handle database connection errors |
37b11ab0 |
379 | * |
202bcbcc |
380 | * @param object PEAR Error object |
37b11ab0 |
381 | * |
ace4c62c |
382 | */ |
370059dd |
383 | function failQuery($res = NULL) { |
21ddfbc3 |
384 | global $use_pdo; |
2d367c68 |
385 | if($res == NULL) { |
386 | printf(_("Preference database error (%s). Exiting abnormally"), |
370059dd |
387 | $this->error); |
2d367c68 |
388 | } else { |
389 | printf(_("Preference database error (%s). Exiting abnormally"), |
21ddfbc3 |
390 | ($use_pdo ? implode(' - ', $res->errorInfo()) : DB::errorMessage($res))); |
2d367c68 |
391 | } |
392 | exit; |
370059dd |
393 | } |
82474746 |
394 | |
ace4c62c |
395 | /** |
396 | * Get user's prefs setting |
37b11ab0 |
397 | * |
ace4c62c |
398 | * @param string $user user name |
399 | * @param string $key preference name |
400 | * @param mixed $default (since 1.2.5) default value |
37b11ab0 |
401 | * |
ace4c62c |
402 | * @return mixed preference value |
37b11ab0 |
403 | * |
ace4c62c |
404 | */ |
370059dd |
405 | function getKey($user, $key, $default = '') { |
406 | global $prefs_cache; |
2d367c68 |
407 | |
971c7b1a |
408 | $temp = array(&$user, &$key); |
409 | $result = do_hook('get_pref_override', $temp); |
410 | if (is_null($result)) { |
411 | cachePrefValues($user); |
2d367c68 |
412 | |
971c7b1a |
413 | if (isset($prefs_cache[$key])) { |
414 | $result = $prefs_cache[$key]; |
62337234 |
415 | } else { |
971c7b1a |
416 | //FIXME: is there a justification for having two prefs hooks so close? who uses them? |
417 | $temp = array(&$user, &$key); |
418 | $result = do_hook('get_pref', $temp); |
419 | if (is_null($result)) { |
420 | if (isset($this->default[$key])) { |
421 | $result = $this->default[$key]; |
422 | } else { |
423 | $result = $default; |
424 | } |
425 | } |
62337234 |
426 | } |
2d367c68 |
427 | } |
971c7b1a |
428 | return $result; |
370059dd |
429 | } |
2d367c68 |
430 | |
ace4c62c |
431 | /** |
432 | * Delete user's prefs setting |
37b11ab0 |
433 | * |
202bcbcc |
434 | * @param string $user user name |
37b11ab0 |
435 | * @param string $key preference name |
436 | * |
ace4c62c |
437 | * @return boolean |
37b11ab0 |
438 | * |
ace4c62c |
439 | */ |
370059dd |
440 | function deleteKey($user, $key) { |
21ddfbc3 |
441 | global $prefs_cache, $use_pdo, $pdo_show_sql_errors; |
82474746 |
442 | |
b279d7f4 |
443 | if (!$this->open()) { |
444 | return false; |
445 | } |
21ddfbc3 |
446 | if ($use_pdo) { |
447 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
448 | if ($pdo_show_sql_errors) |
449 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
450 | else |
451 | $this->error = _("Could not prepare query"); |
452 | $this->failQuery(); |
453 | } |
454 | if (!($res = $sth->execute(array($user, $key)))) { |
455 | if ($pdo_show_sql_errors) |
456 | $this->error = implode(' - ', $sth->errorInfo()); |
457 | else |
458 | $this->error = _("Could not execute query"); |
459 | $this->failQuery(); |
460 | } |
461 | } else { |
462 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
463 | $this->table, |
464 | $this->user_field, |
465 | $this->dbh->quoteString($user), |
466 | $this->key_field, |
467 | $this->dbh->quoteString($key)); |
468 | |
469 | $res = $this->dbh->simpleQuery($query); |
470 | if(DB::isError($res)) { |
471 | $this->failQuery($res); |
472 | } |
370059dd |
473 | } |
474 | |
475 | unset($prefs_cache[$key]); |
82474746 |
476 | |
2d367c68 |
477 | return true; |
370059dd |
478 | } |
82474746 |
479 | |
ace4c62c |
480 | /** |
481 | * Set user's preference |
37b11ab0 |
482 | * |
483 | * @param string $user user name |
484 | * @param string $key preference name |
485 | * @param mixed $value preference value |
486 | * |
ace4c62c |
487 | * @return boolean |
37b11ab0 |
488 | * |
ace4c62c |
489 | */ |
370059dd |
490 | function setKey($user, $key, $value) { |
21ddfbc3 |
491 | global $use_pdo, $pdo_show_sql_errors; |
b279d7f4 |
492 | if (!$this->open()) { |
493 | return false; |
494 | } |
06316c07 |
495 | |
496 | /** |
497 | * Check if username fits into db field |
498 | */ |
499 | if (strlen($user) > $this->user_size) { |
500 | $this->error = "Oversized username value." |
5e07597f |
501 | ." Your preferences can't be saved." |
6f4c512c |
502 | ." See the administrator's manual or contact your system administrator."; |
06316c07 |
503 | |
504 | /** |
202bcbcc |
505 | * Debugging function. Can be used to log all issues that trigger |
506 | * oversized field errors. Function should be enabled in all three |
06316c07 |
507 | * strlen checks. See http://www.php.net/error-log |
508 | */ |
509 | // error_log($user.'|'.$key.'|'.$value."\n",3,'/tmp/oversized_log'); |
510 | |
511 | // error is fatal |
512 | $this->failQuery(null); |
513 | } |
514 | /** |
515 | * Check if preference key fits into db field |
516 | */ |
517 | if (strlen($key) > $this->key_size) { |
518 | $err_msg = "Oversized user's preference key." |
5e07597f |
519 | ." Some preferences were not saved." |
6f4c512c |
520 | ." See the administrator's manual or contact your system administrator."; |
06316c07 |
521 | // error is not fatal. Only some preference is not saved. |
522 | trigger_error($err_msg,E_USER_WARNING); |
523 | return false; |
524 | } |
525 | /** |
526 | * Check if preference value fits into db field |
527 | */ |
528 | if (strlen($value) > $this->val_size) { |
529 | $err_msg = "Oversized user's preference value." |
5e07597f |
530 | ." Some preferences were not saved." |
6f4c512c |
531 | ." See the administrator's manual or contact your system administrator."; |
06316c07 |
532 | // error is not fatal. Only some preference is not saved. |
533 | trigger_error($err_msg,E_USER_WARNING); |
534 | return false; |
535 | } |
536 | |
537 | |
98749983 |
538 | if ($this->db_type == SMDB_MYSQL) { |
21ddfbc3 |
539 | if ($use_pdo) { |
540 | if (!($sth = $this->dbh->prepare('REPLACE INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
541 | if ($pdo_show_sql_errors) |
542 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
543 | else |
544 | $this->error = _("Could not prepare query"); |
545 | $this->failQuery(); |
546 | } |
547 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
548 | if ($pdo_show_sql_errors) |
549 | $this->error = implode(' - ', $sth->errorInfo()); |
550 | else |
551 | $this->error = _("Could not execute query"); |
552 | $this->failQuery(); |
553 | } |
554 | } else { |
555 | $query = sprintf("REPLACE INTO %s (%s, %s, %s) ". |
556 | "VALUES('%s','%s','%s')", |
557 | $this->table, |
558 | $this->user_field, |
559 | $this->key_field, |
560 | $this->val_field, |
561 | $this->dbh->quoteString($user), |
562 | $this->dbh->quoteString($key), |
563 | $this->dbh->quoteString($value)); |
564 | |
565 | $res = $this->dbh->simpleQuery($query); |
566 | if(DB::isError($res)) { |
567 | $this->failQuery($res); |
568 | } |
98749983 |
569 | } |
570 | } elseif ($this->db_type == SMDB_PGSQL) { |
21ddfbc3 |
571 | if ($use_pdo) { |
572 | if ($this->dbh->exec('BEGIN TRANSACTION') === FALSE) { |
573 | if ($pdo_show_sql_errors) |
574 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
575 | else |
576 | $this->error = _("Could not execute query"); |
577 | $this->failQuery(); |
578 | } |
579 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
580 | if ($pdo_show_sql_errors) |
581 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
582 | else |
583 | $this->error = _("Could not prepare query"); |
584 | $this->failQuery(); |
585 | } |
586 | if (!($res = $sth->execute(array($user, $key)))) { |
587 | if ($pdo_show_sql_errors) |
588 | $this->error = implode(' - ', $sth->errorInfo()); |
589 | else |
590 | $this->error = _("Could not execute query"); |
591 | $this->dbh->exec('ROLLBACK TRANSACTION'); |
592 | $this->failQuery(); |
593 | } |
594 | if (!($sth = $this->dbh->prepare('INSERT INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
595 | if ($pdo_show_sql_errors) |
596 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
597 | else |
598 | $this->error = _("Could not prepare query"); |
599 | $this->failQuery(); |
600 | } |
601 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
602 | if ($pdo_show_sql_errors) |
603 | $this->error = implode(' - ', $sth->errorInfo()); |
604 | else |
605 | $this->error = _("Could not execute query"); |
606 | $this->dbh->exec('ROLLBACK TRANSACTION'); |
607 | $this->failQuery(); |
608 | } |
609 | if ($this->dbh->exec('COMMIT TRANSACTION') === FALSE) { |
610 | if ($pdo_show_sql_errors) |
611 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
612 | else |
613 | $this->error = _("Could not execute query"); |
614 | $this->failQuery(); |
615 | } |
616 | } else { |
617 | $this->dbh->simpleQuery("BEGIN TRANSACTION"); |
618 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
619 | $this->table, |
620 | $this->user_field, |
621 | $this->dbh->quoteString($user), |
622 | $this->key_field, |
623 | $this->dbh->quoteString($key)); |
624 | $res = $this->dbh->simpleQuery($query); |
625 | if (DB::isError($res)) { |
626 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
627 | $this->failQuery($res); |
628 | } |
629 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
630 | $this->table, |
631 | $this->user_field, |
632 | $this->key_field, |
633 | $this->val_field, |
634 | $this->dbh->quoteString($user), |
635 | $this->dbh->quoteString($key), |
636 | $this->dbh->quoteString($value)); |
637 | $res = $this->dbh->simpleQuery($query); |
638 | if (DB::isError($res)) { |
639 | $this->dbh->simpleQuery("ROLLBACK TRANSACTION"); |
640 | $this->failQuery($res); |
641 | } |
642 | $this->dbh->simpleQuery("COMMIT TRANSACTION"); |
98749983 |
643 | } |
98749983 |
644 | } else { |
21ddfbc3 |
645 | if ($use_pdo) { |
646 | if (!($sth = $this->dbh->prepare('DELETE FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ? AND ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' = ?'))) { |
647 | if ($pdo_show_sql_errors) |
648 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
649 | else |
650 | $this->error = _("Could not prepare query"); |
651 | $this->failQuery(); |
652 | } |
653 | if (!($res = $sth->execute(array($user, $key)))) { |
654 | if ($pdo_show_sql_errors) |
655 | $this->error = implode(' - ', $sth->errorInfo()); |
656 | else |
657 | $this->error = _("Could not execute query"); |
658 | $this->failQuery(); |
659 | } |
660 | if (!($sth = $this->dbh->prepare('INSERT INTO ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' (' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ', ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ') VALUES (?, ?, ?)'))) { |
661 | if ($pdo_show_sql_errors) |
662 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
663 | else |
664 | $this->error = _("Could not prepare query"); |
665 | $this->failQuery(); |
666 | } |
667 | if (!($res = $sth->execute(array($user, $key, $value)))) { |
668 | if ($pdo_show_sql_errors) |
669 | $this->error = implode(' - ', $sth->errorInfo()); |
670 | else |
671 | $this->error = _("Could not execute query"); |
672 | $this->failQuery(); |
673 | } |
674 | } else { |
675 | $query = sprintf("DELETE FROM %s WHERE %s='%s' AND %s='%s'", |
676 | $this->table, |
677 | $this->user_field, |
678 | $this->dbh->quoteString($user), |
679 | $this->key_field, |
680 | $this->dbh->quoteString($key)); |
681 | $res = $this->dbh->simpleQuery($query); |
682 | if (DB::isError($res)) { |
683 | $this->failQuery($res); |
684 | } |
685 | $query = sprintf("INSERT INTO %s (%s, %s, %s) VALUES ('%s', '%s', '%s')", |
686 | $this->table, |
687 | $this->user_field, |
688 | $this->key_field, |
689 | $this->val_field, |
690 | $this->dbh->quoteString($user), |
691 | $this->dbh->quoteString($key), |
692 | $this->dbh->quoteString($value)); |
693 | $res = $this->dbh->simpleQuery($query); |
694 | if (DB::isError($res)) { |
695 | $this->failQuery($res); |
696 | } |
98749983 |
697 | } |
370059dd |
698 | } |
2d367c68 |
699 | |
700 | return true; |
370059dd |
701 | } |
82474746 |
702 | |
ace4c62c |
703 | /** |
704 | * Fill preference cache array |
37b11ab0 |
705 | * |
ace4c62c |
706 | * @param string $user user name |
37b11ab0 |
707 | * |
ace4c62c |
708 | * @since 1.2.3 |
37b11ab0 |
709 | * |
ace4c62c |
710 | */ |
370059dd |
711 | function fillPrefsCache($user) { |
21ddfbc3 |
712 | global $prefs_cache, $use_pdo, $pdo_show_sql_errors; |
2d367c68 |
713 | |
b279d7f4 |
714 | if (!$this->open()) { |
715 | return; |
716 | } |
370059dd |
717 | |
718 | $prefs_cache = array(); |
21ddfbc3 |
719 | if ($use_pdo) { |
720 | if (!($sth = $this->dbh->prepare('SELECT ' . $this->identifier_quote_char . $this->key_field . $this->identifier_quote_char . ' AS prefkey, ' . $this->identifier_quote_char . $this->val_field . $this->identifier_quote_char . ' AS prefval FROM ' . $this->identifier_quote_char . $this->table . $this->identifier_quote_char . ' WHERE ' . $this->identifier_quote_char . $this->user_field . $this->identifier_quote_char . ' = ?'))) { |
721 | if ($pdo_show_sql_errors) |
722 | $this->error = implode(' - ', $this->dbh->errorInfo()); |
723 | else |
724 | $this->error = _("Could not prepare query"); |
725 | $this->failQuery(); |
726 | } |
727 | if (!($res = $sth->execute(array($user)))) { |
728 | if ($pdo_show_sql_errors) |
729 | $this->error = implode(' - ', $sth->errorInfo()); |
730 | else |
731 | $this->error = _("Could not execute query"); |
732 | $this->failQuery(); |
733 | } |
370059dd |
734 | |
21ddfbc3 |
735 | while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { |
736 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
737 | } |
738 | } else { |
739 | $query = sprintf("SELECT %s as prefkey, %s as prefval FROM %s ". |
740 | "WHERE %s = '%s'", |
741 | $this->key_field, |
742 | $this->val_field, |
743 | $this->table, |
744 | $this->user_field, |
745 | $this->dbh->quoteString($user)); |
746 | $res = $this->dbh->query($query); |
747 | if (DB::isError($res)) { |
748 | $this->failQuery($res); |
749 | } |
750 | |
751 | while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) { |
752 | $prefs_cache[$row['prefkey']] = $row['prefval']; |
753 | } |
370059dd |
754 | } |
755 | } |
756 | |
370059dd |
757 | } /* end class dbPrefs */ |
82474746 |
758 | |
759 | |
4d30c1b7 |
760 | /** |
37b11ab0 |
761 | * Returns the value for the requested preference |
4d30c1b7 |
762 | * @ignore |
763 | */ |
37b11ab0 |
764 | function getPref($data_dir, $username, $pref_name, $default = '') { |
370059dd |
765 | $db = new dbPrefs; |
766 | if(isset($db->error)) { |
2d367c68 |
767 | printf( _("Preference database error (%s). Exiting abnormally"), |
370059dd |
768 | $db->error); |
2d367c68 |
769 | exit; |
370059dd |
770 | } |
771 | |
37b11ab0 |
772 | return $db->getKey($username, $pref_name, $default); |
370059dd |
773 | } |
774 | |
4d30c1b7 |
775 | /** |
37b11ab0 |
776 | * Remove the desired preference setting ($pref_name) |
4d30c1b7 |
777 | * @ignore |
778 | */ |
37b11ab0 |
779 | function removePref($data_dir, $username, $pref_name) { |
1fa62ab9 |
780 | global $prefs_cache; |
370059dd |
781 | $db = new dbPrefs; |
782 | if(isset($db->error)) { |
783 | $db->failQuery(); |
784 | } |
785 | |
37b11ab0 |
786 | $db->deleteKey($username, $pref_name); |
88a99543 |
787 | |
37b11ab0 |
788 | if (isset($prefs_cache[$pref_name])) { |
789 | unset($prefs_cache[$pref_name]); |
88a99543 |
790 | } |
791 | |
792 | sqsession_register($prefs_cache , 'prefs_cache'); |
370059dd |
793 | return; |
794 | } |
795 | |
4d30c1b7 |
796 | /** |
37b11ab0 |
797 | * Sets the desired preference setting ($pref_name) to whatever is in $value |
4d30c1b7 |
798 | * @ignore |
799 | */ |
37b11ab0 |
800 | function setPref($data_dir, $username, $pref_name, $value) { |
370059dd |
801 | global $prefs_cache; |
802 | |
37b11ab0 |
803 | if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) { |
1fa62ab9 |
804 | return; |
370059dd |
805 | } |
806 | |
37b11ab0 |
807 | if ($value === '') { |
808 | removePref($data_dir, $username, $pref_name); |
370059dd |
809 | return; |
810 | } |
811 | |
812 | $db = new dbPrefs; |
813 | if(isset($db->error)) { |
814 | $db->failQuery(); |
815 | } |
816 | |
37b11ab0 |
817 | $db->setKey($username, $pref_name, $value); |
818 | $prefs_cache[$pref_name] = $value; |
370059dd |
819 | assert_options(ASSERT_ACTIVE, 1); |
820 | assert_options(ASSERT_BAIL, 1); |
37b11ab0 |
821 | assert ('$value == $prefs_cache[$pref_name]'); |
88a99543 |
822 | sqsession_register($prefs_cache , 'prefs_cache'); |
370059dd |
823 | return; |
824 | } |
825 | |
4d30c1b7 |
826 | /** |
827 | * This checks if the prefs are available |
828 | * @ignore |
829 | */ |
370059dd |
830 | function checkForPrefs($data_dir, $username) { |
831 | $db = new dbPrefs; |
832 | if(isset($db->error)) { |
833 | $db->failQuery(); |
834 | } |
835 | } |
836 | |
4d30c1b7 |
837 | /** |
838 | * Writes the Signature |
839 | * @ignore |
840 | */ |
37b11ab0 |
841 | function setSig($data_dir, $username, $number, $value) { |
16e5635d |
842 | if ($number == "g") { |
843 | $key = '___signature___'; |
844 | } else { |
845 | $key = sprintf('___sig%s___', $number); |
846 | } |
37b11ab0 |
847 | setPref($data_dir, $username, $key, $value); |
370059dd |
848 | return; |
849 | } |
850 | |
4d30c1b7 |
851 | /** |
852 | * Gets the signature |
853 | * @ignore |
854 | */ |
16e5635d |
855 | function getSig($data_dir, $username, $number) { |
16e5635d |
856 | if ($number == "g") { |
857 | $key = '___signature___'; |
858 | } else { |
859 | $key = sprintf('___sig%d___', $number); |
860 | } |
57f1d1c1 |
861 | return getPref($data_dir, $username, $key); |
370059dd |
862 | } |