Project

General

Profile

1 4 ryan
<?php
2 1362 Luisehahne
/**
3 1866 Luisehahne
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 1362 Luisehahne
 *
5 1866 Luisehahne
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9 1362 Luisehahne
 *
10 1866 Luisehahne
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 1362 Luisehahne
 */
18 1866 Luisehahne
/**
19
 * WbDatabase.php
20
 *
21
 * @category     Core
22
 * @package      Core_database
23
 * @author       Werner v.d.Decken <wkl@isteam.de>
24
 * @author       Dietmar W. <dietmar.woellbrink@websitebaker.org>
25
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
26
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
27
 * @version      0.0.9
28
 * @revision     $Revision$
29
 * @lastmodified $Date$
30
 * @deprecated   from WB version number 2.9
31
 * @description  Mysql database wrapper for use with websitebaker up to version 2.8.4
32
 */
33
34 1496 DarkViper
/* -------------------------------------------------------- */
35 1885 Luisehahne
@define('DATABASE_CLASS_LOADED', true);
36 4 ryan
37 1686 darkviper
class WbDatabase {
38 1763 Luisehahne
39 1686 darkviper
	private static $_oInstances = array();
40 1680 darkviper
41 1885 Luisehahne
	private $_db_handle  = null; // readonly from outside
42
	private $_db_name    = '';
43
	protected $sTablePrefix = '';
44 1889 Luisehahne
	protected $sCharset     = '';
45 1885 Luisehahne
	protected $connected    = false;
46
	protected $error        = '';
47
	protected $error_type   = '';
48
	protected $iQueryCount  = 0;
49 1362 Luisehahne
50 1683 darkviper
/* prevent from public instancing */
51 1686 darkviper
	protected function  __construct() {}
52 1683 darkviper
/* prevent from cloning */
53
	private function __clone() {}
54
/**
55 1686 darkviper
 * get a valid instance of this class
56
 * @param string $sIdentifier selector for several different instances
57
 * @return object
58
 */
59
	public static function getInstance($sIdentifier = 'core') {
60
		if( !isset(self::$_oInstances[$sIdentifier])) {
61
            $c = __CLASS__;
62
            self::$_oInstances[$sIdentifier] = new $c;
63
		}
64
		return self::$_oInstances[$sIdentifier];
65
	}
66
/**
67
 * disconnect and kills an existing instance
68
 * @param string $sIdentifier
69
 */
70
	public static function killInstance($sIdentifier) {
71
		if($sIdentifier != 'core') {
72
			if( isset(self::$_oInstances[$sIdentifier])) {
73
				self::$_oInstances[$sIdentifier]->disconnect();
74
				unset(self::$_oInstances[$sIdentifier]);
75
			}
76
		}
77
	}
78
/**
79 1683 darkviper
 * Connect to the database
80
 * @param string $url
81
 * @return bool
82
 *
83
 * Example for SQL-Url:  'mysql://user:password@demo.de[:3306]/datenbank'
84
 */
85
	public function doConnect($url = '') {
86 1885 Luisehahne
		$this->connected = false;
87 1680 darkviper
		if($url != '') {
88
			$aIni = parse_url($url);
89 1866 Luisehahne
90
			$scheme   = isset($aIni['scheme']) ? $aIni['scheme'] : 'mysql';
91
			$hostname = isset($aIni['host']) ? $aIni['host'] : '';
92
			$username = isset($aIni['user']) ? $aIni['user'] : '';
93
			$password = isset($aIni['pass']) ? $aIni['pass'] : '';
94
			$hostport = isset($aIni['port']) ? $aIni['port'] : '3306';
95
			$hostport = $hostport == '3306' ? '' : ':'.$hostport;
96
			$db_name  = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\');
97 1885 Luisehahne
			$sTmp = isset($aIni['query']) ? $aIni['query'] : '';
98
			$aQuery = explode('&', $sTmp);
99
			foreach($aQuery as $sArgument) {
100
				$aArg = explode('=', $sArgument);
101
				switch(strtolower($aArg[0])) {
102
					case 'charset':
103
						$this->sCharset = strtolower(preg_replace('/[^a-z0-9]/i', '', $aArg[1]));
104
						break;
105
					case 'tableprefix':
106
						$this->sTablePrefix = $aArg[1];
107
						break;
108
					default:
109
						break;
110
				}
111
			}
112 1866 Luisehahne
			$this->_db_name = $db_name;
113 1680 darkviper
		}else {
114 1885 Luisehahne
			throw new WbDatabaseException('Missing parameter: unable to connect database');
115 1680 darkviper
		}
116 1885 Luisehahne
		$this->_db_handle = @mysql_connect($hostname.$hostport,
117 1887 Luisehahne
		                                   $username,
118
		                                   $password);
119 1680 darkviper
		if(!$this->_db_handle) {
120 1885 Luisehahne
			throw new WbDatabaseException('unable to connect \''.$scheme.'://'.
121 1866 Luisehahne
			                           $hostname.$hostport.'\'');
122 4 ryan
		} else {
123 1885 Luisehahne
			if(!@mysql_select_db($db_name)) {
124
				throw new WbDatabaseException('unable to select database \''.$db_name.
125 1866 Luisehahne
				                           '\' on \''.$scheme.'://'.
126
				                           $hostname.$hostport.'\'');
127 4 ryan
			} else {
128 1885 Luisehahne
				if($this->sCharset) {
129
					@mysql_query('SET NAMES \''.$this->sCharset.'\'');
130
				}
131 4 ryan
				$this->connected = true;
132
			}
133
		}
134
		return $this->connected;
135
	}
136 1763 Luisehahne
137 4 ryan
	// Disconnect from the database
138 1686 darkviper
	public function disconnect() {
139 95 stefan
		if($this->connected==true) {
140 1680 darkviper
			mysql_close($this->_db_handle);
141 4 ryan
			return true;
142
		} else {
143
			return false;
144
		}
145
	}
146 1763 Luisehahne
147 4 ryan
	// Run a query
148 1686 darkviper
	public function query($statement) {
149 1662 darkviper
		$this->iQueryCount++;
150 4 ryan
		$mysql = new mysql();
151 1680 darkviper
		$mysql->query($statement, $this->_db_handle);
152
		$this->set_error($mysql->error($this->_db_handle));
153
		if($mysql->error($this->_db_handle)) {
154 4 ryan
			return null;
155
		} else {
156
			return $mysql;
157
		}
158
	}
159 1362 Luisehahne
160 4 ryan
	// Gets the first column of the first row
161 1686 darkviper
	public function get_one( $statement )
162 1362 Luisehahne
	{
163 1662 darkviper
		$this->iQueryCount++;
164 1680 darkviper
		$fetch_row = mysql_fetch_array(mysql_query($statement, $this->_db_handle));
165 4 ryan
		$result = $fetch_row[0];
166 1680 darkviper
		$this->set_error(mysql_error($this->_db_handle));
167
		if(mysql_error($this->_db_handle)) {
168 4 ryan
			return null;
169
		} else {
170
			return $result;
171
		}
172
	}
173 1763 Luisehahne
174 4 ryan
	// Set the DB error
175 1686 darkviper
	public function set_error($message = null) {
176 4 ryan
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
177
		$this->error = $message;
178
		if(strpos($message, 'no such table')) {
179
			$this->error_type = $TABLE_DOES_NOT_EXIST;
180
		} else {
181
			$this->error_type = $TABLE_UNKNOWN;
182
		}
183
	}
184 1763 Luisehahne
185 4 ryan
	// Return true if there was an error
186 1686 darkviper
	public function is_error() {
187 4 ryan
		return (!empty($this->error)) ? true : false;
188
	}
189 1763 Luisehahne
190 4 ryan
	// Return the error
191 1686 darkviper
	public function get_error() {
192 4 ryan
		return $this->error;
193
	}
194 1613 darkviper
/**
195 1885 Luisehahne
 * Protect class from property injections
196
 * @param string name of property
197
 * @param mixed value
198
 * @throws WbDatabaseException
199 1866 Luisehahne
 */
200 1885 Luisehahne
	public function __set($name, $value) {
201
		throw new WbDatabaseException('tried to set a readonly or nonexisting property ['.$name.']!! ');
202 1866 Luisehahne
	}
203
/**
204 1613 darkviper
 * default Getter for some properties
205 1866 Luisehahne
 * @param string name of the Property
206 1613 darkviper
 * @return mixed NULL on error or missing property
207 1362 Luisehahne
 */
208 1613 darkviper
	public function __get($sPropertyName)
209 1362 Luisehahne
	{
210 1613 darkviper
		switch ($sPropertyName):
211
			case 'db_handle':
212
			case 'DbHandle':
213 1662 darkviper
			case 'getDbHandle':
214 1680 darkviper
				$retval = $this->_db_handle;
215 1613 darkviper
				break;
216 1866 Luisehahne
			case 'LastInsertId':
217 1885 Luisehahne
			case 'getLastInsertId':
218 1866 Luisehahne
				$retval = mysql_insert_id($this->_db_handle);
219
				break;
220 1613 darkviper
			case 'db_name':
221
			case 'DbName':
222 1662 darkviper
			case 'getDbName':
223 1680 darkviper
				$retval = $this->_db_name;
224 1613 darkviper
				break;
225 1885 Luisehahne
			case 'TablePrefix':
226
			case 'getTablePrefix':
227
				$retval = $this->sTablePrefix;
228
				break;
229 1662 darkviper
			case 'getQueryCount':
230
				$retval = $this->iQueryCount;
231
				break;
232 1613 darkviper
			default:
233
				$retval = null;
234
				break;
235
		endswitch;
236
		return $retval;
237
	} // __get()
238 1885 Luisehahne
/**
239
 * Escapes special characters in a string for use in an SQL statement
240
 * @param string $unescaped_string
241
 * @return string
242
 */
243
	public function escapeString($unescaped_string)
244
	{
245
		return mysql_real_escape_string($unescaped_string, $this->_db_handle);
246
	}
247
/**
248
 * Last inserted Id
249
 * @return bool|int false on error, 0 if no record inserted
250
 */
251
	public function getLastInsertId()
252
	{
253
		return mysql_insert_id($this->_db_handle);
254
	}
255 1362 Luisehahne
/*
256 1866 Luisehahne
 * @param string full name of the table (incl. TABLE_PREFIX)
257
 * @param string name of the field to seek for
258
 * @return bool true if field exists
259 1362 Luisehahne
 */
260
	public function field_exists($table_name, $field_name)
261
	{
262
		$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
263 1680 darkviper
		$query = $this->query($sql, $this->_db_handle);
264 1362 Luisehahne
		return ($query->numRows() != 0);
265
	}
266
/*
267 1866 Luisehahne
 * @param string full name of the table (incl. TABLE_PREFIX)
268
 * @param string name of the index to seek for
269
 * @return bool true if field exists
270 1362 Luisehahne
 */
271
	public function index_exists($table_name, $index_name, $number_fields = 0)
272
	{
273
		$number_fields = intval($number_fields);
274
		$keys = 0;
275
		$sql = 'SHOW INDEX FROM `'.$table_name.'`';
276 1680 darkviper
		if( ($res_keys = $this->query($sql, $this->_db_handle)) )
277 1362 Luisehahne
		{
278
			while(($rec_key = $res_keys->fetchRow()))
279
			{
280
				if( $rec_key['Key_name'] == $index_name )
281
				{
282
					$keys++;
283
				}
284
			}
285
286
		}
287
		if( $number_fields == 0 )
288
		{
289
			return ($keys != $number_fields);
290
		}else
291
		{
292
			return ($keys == $number_fields);
293
		}
294
	}
295 1763 Luisehahne
296 1362 Luisehahne
/*
297 1866 Luisehahne
 * @param string full name of the table (incl. TABLE_PREFIX)
298
 * @param string name of the field to add
299
 * @param string describes the new field like ( INT NOT NULL DEFAULT '0')
300
 * @return bool true if successful, otherwise false and error will be set
301 1362 Luisehahne
 */
302
	public function field_add($table_name, $field_name, $description)
303
	{
304 1442 Luisehahne
		if( !$this->field_exists($table_name, $field_name) )
305 1362 Luisehahne
		{ // add new field into a table
306
			$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
307 1680 darkviper
			$query = $this->query($sql, $this->_db_handle);
308
			$this->set_error(mysql_error($this->_db_handle));
309 1362 Luisehahne
			if( !$this->is_error() )
310
			{
311 1442 Luisehahne
				return ( $this->field_exists($table_name, $field_name) ) ? true : false;
312 1362 Luisehahne
			}
313
		}else
314
		{
315
			$this->set_error('field \''.$field_name.'\' already exists');
316
		}
317
		return false;
318
	}
319
320
/*
321
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
322
 * @param string $field_name: name of the field to add
323
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
324
 * @return bool: true if successful, otherwise false and error will be set
325
 */
326
	public function field_modify($table_name, $field_name, $description)
327
	{
328
		$retval = false;
329 1486 DarkViper
		if( $this->field_exists($table_name, $field_name) )
330 1362 Luisehahne
		{ // modify a existing field in a table
331 1486 DarkViper
			$sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
332 1680 darkviper
			$retval = ( $this->query($sql, $this->_db_handle) ? true : false);
333 1486 DarkViper
			$this->set_error(mysql_error());
334 1362 Luisehahne
		}
335 1486 DarkViper
		return $retval;
336 1362 Luisehahne
	}
337
338
/*
339
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
340
 * @param string $field_name: name of the field to remove
341
 * @return bool: true if successful, otherwise false and error will be set
342
 */
343
	public function field_remove($table_name, $field_name)
344
	{
345
		$retval = false;
346 1507 Luisehahne
		if( $this->field_exists($table_name, $field_name) )
347 1362 Luisehahne
		{ // modify a existing field in a table
348
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
349 1680 darkviper
			$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
350 1362 Luisehahne
		}
351
		return $retval;
352
	}
353
354
/*
355
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
356 1763 Luisehahne
 * @param string $index_name: name of the new index (empty string for PRIMARY)
357 1362 Luisehahne
 * @param string $field_list: comma seperated list of fields for this index
358 1763 Luisehahne
 * @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
359 1362 Luisehahne
 * @return bool: true if successful, otherwise false and error will be set
360
 */
361 1763 Luisehahne
     public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
362
     {
363
        $retval = false;
364 1885 Luisehahne
        $field_list = explode(',', (str_replace(' ', '', $field_list)));
365 1763 Luisehahne
        $number_fields = sizeof($field_list);
366
        $field_list = '`'.implode('`,`', $field_list).'`';
367
        $index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
368
        if( $this->index_exists($table_name, $index_name, $number_fields) ||
369
            $this->index_exists($table_name, $index_name))
370
        {
371
            $sql  = 'ALTER TABLE `'.$table_name.'` ';
372
            $sql .= 'DROP INDEX `'.$index_name.'`';
373
            if( !$this->query($sql, $this->_db_handle)) { return false; }
374
        }
375
        $sql  = 'ALTER TABLE `'.$table_name.'` ';
376
        $sql .= 'ADD '.$index_type.' ';
377
        $sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
378
        $sql .= '( '.$field_list.' ); ';
379
        if( $this->query($sql, $this->_db_handle)) { $retval = true; }
380
        return $retval;
381
    }
382 1362 Luisehahne
383
/*
384
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
385
 * @param string $field_name: name of the field to remove
386
 * @return bool: true if successful, otherwise false and error will be set
387
 */
388
	public function index_remove($table_name, $index_name)
389
	{
390
		$retval = false;
391
		if( $this->index_exists($table_name, $index_name) )
392
		{ // modify a existing field in a table
393
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
394 1680 darkviper
			$retval = ( $this->query($sql, $this->_db_handle) ? true : false );
395 1362 Luisehahne
		}
396
		return $retval;
397
	}
398 1763 Luisehahne
399 1586 darkviper
/**
400
 * Import a standard *.sql dump file
401
 * @param string $sSqlDump link to the sql-dumpfile
402
 * @param string $sTablePrefix
403 1887 Luisehahne
 * @param bool     $bPreserve   set to true will ignore all DROP TABLE statements
404
 * @param string   $sEngine     can be 'MyISAM' or 'InnoDB'
405
 * @param string   $sCollation  one of the list of available collations
406 1586 darkviper
 * @return boolean true if import successful
407
 */
408
	public function SqlImport($sSqlDump,
409
	                          $sTablePrefix = '',
410 1887 Luisehahne
	                          $bPreserve    = true,
411
	                          $sEngine      = 'MyISAM',
412
	                          $sCollation   = 'utf8_unicode_ci')
413 1586 darkviper
	{
414 1887 Luisehahne
		$sCollation = ($sCollation != '' ? $sCollation : 'utf8_unicode_ci');
415
		$aCharset = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY);
416
		$sEngine = 'ENGINE='.$sEngine.' DEFAULT CHARSET='.$aCharset[0].' COLLATE='.$sCollation;
417
		$sCollation = ' collate '.$sCollation;
418 1586 darkviper
		$retval = true;
419
		$this->error = '';
420
		$aSearch  = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
421 1887 Luisehahne
		$aReplace = array($this->sTablePrefix, $sEngine, $sCollation);
422 1586 darkviper
		$sql = '';
423
		$aSql = file($sSqlDump);
424
		while ( sizeof($aSql) > 0 ) {
425
			$sSqlLine = trim(array_shift($aSql));
426 1591 darkviper
			if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
427 1592 darkviper
				$sql = $sql.' '.$sSqlLine;
428 1586 darkviper
				if ((substr($sql,-1,1) == ';')) {
429
					$sql = trim(str_replace( $aSearch, $aReplace, $sql));
430
					if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
431 1680 darkviper
						if(!mysql_query($sql, $this->_db_handle)) {
432 1586 darkviper
							$retval = false;
433 1680 darkviper
							$this->error = mysql_error($this->_db_handle);
434 1586 darkviper
							unset($aSql);
435
							break;
436
						}
437
					}
438
					$sql = '';
439
				}
440
			}
441
		}
442
		return $retval;
443
	}
444 1362 Luisehahne
445 1586 darkviper
/**
446
 * retuns the type of the engine used for requested table
447
 * @param string $table name of the table, including prefix
448
 * @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
449
 */
450 1535 Luisehahne
	public function getTableEngine($table)
451
	{
452
		$retVal = false;
453 1680 darkviper
		$mysqlVersion = mysql_get_server_info($this->_db_handle);
454 1535 Luisehahne
		$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
455 1770 Luisehahne
		$sql = 'SHOW TABLE STATUS FROM `' . $this->_db_name . '` LIKE \'' . $table . '\'';
456 1680 darkviper
		if(($result = $this->query($sql, $this->_db_handle))) {
457 1535 Luisehahne
			if(($row = $result->fetchRow(MYSQL_ASSOC))) {
458
				$retVal = $row[$engineValue];
459
			}
460
		}
461
		return $retVal;
462
	}
463
464
465 1362 Luisehahne
} /// end of class database
466 1885 Luisehahne
// //////////////////////////////////////////////////////////////////////////////////// //
467
/**
468
 * WbDatabaseException
469
 *
470
 * @category     Core
471
 * @package      Core_database
472
 * @author       Werner v.d.Decken <wkl@isteam.de>
473
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
474
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
475
 * @version      2.9.0
476
 * @revision     $Revision$
477
 * @lastmodified $Date$
478
 * @description  Exceptionhandler for the WbDatabase and depending classes
479
 */
480
class WbDatabaseException extends AppException {}
481 1362 Luisehahne
482 1549 Luisehahne
define('MYSQL_SEEK_FIRST', 0);
483
define('MYSQL_SEEK_LAST', -1);
484
485 4 ryan
class mysql {
486
487 1680 darkviper
	private $result = null;
488
	private $_db_handle = null;
489 4 ryan
	// Run a query
490 1680 darkviper
	function query($statement, $dbHandle) {
491
		$this->_db_handle = $dbHandle;
492 1889 Luisehahne
		$this->result = @mysql_query($statement, $this->_db_handle);
493
		if($this->result === false) {
494
			if(DEBUG) {
495
				throw new WbDatabaseException(mysql_error($this->_db_handle));
496
			}else{
497
				throw new WbDatabaseException('Error in SQL-Statement');
498
			}
499
		}
500 1680 darkviper
		$this->error = mysql_error($this->_db_handle);
501 4 ryan
		return $this->result;
502
	}
503 1763 Luisehahne
504 4 ryan
	// Fetch num rows
505
	function numRows() {
506
		return mysql_num_rows($this->result);
507
	}
508 1011 Ruebenwurz
509
	// Fetch row  $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
510
	function fetchRow($typ = MYSQL_BOTH) {
511
		return mysql_fetch_array($this->result, $typ);
512 4 ryan
	}
513 1011 Ruebenwurz
514 1549 Luisehahne
	function rewind()
515
	{
516
		return $this->seekRow();
517
	}
518
519
	function seekRow( $position = MYSQL_SEEK_FIRST )
520
	{
521
		$pmax = $this->numRows() - 1;
522
		$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
523
		return mysql_data_seek($this->result, $p);
524
	}
525
526 4 ryan
	// Get error
527
	function error() {
528
		if(isset($this->error)) {
529
			return $this->error;
530
		} else {
531
			return null;
532
		}
533
	}
534
535
}
536 1885 Luisehahne
// //////////////////////////////////////////////////////////////////////////////////// //
537 1364 Luisehahne
/* this function is placed inside this file temporarely until a better place is found */
538
/*  function to update a var/value-pair(s) in table ****************************
539
 *  nonexisting keys are inserted
540
 *  @param string $table: name of table to use (without prefix)
541
 *  @param mixed $key:    a array of key->value pairs to update
542
 *                        or a string with name of the key to update
543
 *  @param string $value: a sting with needed value, if $key is a string too
544
 *  @return bool:  true if any keys are updated, otherwise false
545
 */
546
	function db_update_key_value($table, $key, $value = '')
547
	{
548
		global $database;
549
		if( !is_array($key))
550
		{
551
			if( trim($key) != '' )
552
			{
553
				$key = array( trim($key) => trim($value) );
554
			} else {
555
				$key = array();
556
			}
557
		}
558
		$retval = true;
559
		foreach( $key as $index=>$val)
560
		{
561
			$index = strtolower($index);
562 1680 darkviper
			$sql = 'SELECT COUNT(`setting_id`) '
563
			     . 'FROM `'.TABLE_PREFIX.$table.'` '
564
			     . 'WHERE `name` = \''.$index.'\' ';
565 1364 Luisehahne
			if($database->get_one($sql))
566
			{
567
				$sql = 'UPDATE ';
568
				$sql_where = 'WHERE `name` = \''.$index.'\'';
569
			}else {
570
				$sql = 'INSERT INTO ';
571
				$sql_where = '';
572
			}
573
			$sql .= '`'.TABLE_PREFIX.$table.'` ';
574
			$sql .= 'SET `name` = \''.$index.'\', ';
575
			$sql .= '`value` = \''.$val.'\' '.$sql_where;
576
			if( !$database->query($sql) )
577
			{
578
				$retval = false;
579
			}
580
		}
581
		return $retval;
582
	}