Project

General

Profile

1 2 Manuela
<?php
2
/**
3
 *
4 11 Manuela
 * @package      Core
5
 * @copyright    WebsiteBaker Org. e.V.
6
 * @author       Ryan Djurovich
7
 * @author       Manuela v.d.Decken <manuela@isteam.de>
8
 * @author       Dietmar Wöllbrink <luisehahne@websitebaker.org>
9
 * @license      GNU General Public License 2.0
10
 * @version      3.0.1
11
 * @requirements PHP 5.6.x and higher
12
 * @revision     $Id$
13
 * @deprecated   no / since 0000/00/00
14
 * @description  This class will be used to interface between the database and the Website Baker code
15 2 Manuela
 */
16
17
18
    define('DATABASE_CLASS_LOADED', true);
19
    // define the old mysql consts for Backward compatibility
20
    if (!defined('MYSQL_ASSOC')) {
21
        define('MYSQL_ASSOC',                 1);
22
        define('MYSQL_NUM',                   2);
23
        define('MYSQL_BOTH',                  3);
24
        define('MYSQL_CLIENT_COMPRESS',      32);
25
        define('MYSQL_CLIENT_IGNORE_SPACE', 256);
26
        define('MYSQL_CLIENT_INTERACTIVE', 1024);
27
        define('MYSQL_CLIENT_SSL',         2048);
28
    }
29
30
class database {
31
32
    private $db_handle  = null; // readonly from outside
33
    private $db_name    = '';
34 6 Manuela
    private $sTablePrefix = '';
35 2 Manuela
    private $connected  = false;
36 6 Manuela
    private $sCharset   = 'utf8mb4';
37
    private $sCollation = 'utf8mb4_unicode_ci';
38 2 Manuela
    private $error      = '';
39
    private $error_no   = array();
40
    private $error_type = '';
41
    private $message    = array();
42
    private $sActionFile  = '';
43
44
45
    // Set DB_URL
46 6 Manuela
    function __construct($url = '')
47
    {
48 2 Manuela
        // Connect to database
49
        if (!$this->connect()) {
50
            throw new DatabaseException($this->get_error());
51
        }
52 6 Manuela
        $this->sTablePrefix = TABLE_PREFIX;
53 2 Manuela
    }
54
55
    // Connect to the database   DB_CHARSET
56 6 Manuela
    function connect()
57
    {
58
        $aTmp = preg_split(
59
            '/[^a-z0-9]/i',
60 11 Manuela
            strtolower(preg_replace('/[^a-z0-9_]/i', '', (defined('DB_CHARSET') ? DB_CHARSET : 'utf8mb4_unicode_ci'))),
61
            null,
62
            PREG_SPLIT_NO_EMPTY
63 6 Manuela
        );
64
        $this->sCharset = $aTmp[0];
65
        $this->sCollation = implode('_', $aTmp);
66
        $port = defined('DB_PORT') ? DB_PORT : ini_get('mysqli.default_port');
67 2 Manuela
        if (!($this->db_handle = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, $port))) {
68
            $this->connected = false;
69
            $this->error = mysqli_connect_error();
70
        } else {
71
            if ($this->sCharset) {
72 6 Manuela
                mysqli_query($this->db_handle, 'SET NAMES '.$this->sCharset);
73 2 Manuela
                mysqli_set_charset($this->db_handle, $this->sCharset);
74
            }
75
            $this->db_name = DB_NAME;
76
            $this->connected = true;
77
        }
78
        return $this->connected;
79
    }
80
81
    // Disconnect from the database
82 6 Manuela
    function disconnect()
83
    {
84 2 Manuela
        if($this->connected==true) {
85
            mysqli_close();
86
            return true;
87
        } else {
88
            return false;
89
        }
90
    }
91
92
    // Run a query
93 6 Manuela
    function query($statement)
94
    {
95 2 Manuela
        $mysql = new mysql($this->db_handle);
96
        $mysql->query($statement);
97
        $this->set_error($mysql->error());
98
        if($mysql->error()) {
99
            return null;
100
        } else {
101
            return $mysql;
102
        }
103
    }
104
105
    // Gets the first column of the first row
106
    function get_one( $statement )
107
    {
108
        $fetch_row = mysqli_fetch_array(mysqli_query($this->db_handle, $statement) );
109
        $result = $fetch_row[0];
110
        $this->set_error(null);
111
        if(mysqli_error($this->db_handle)) {
112
            $this->set_error(mysqli_error($this->db_handle));
113
            return null;
114
        } else {
115
            return $result;
116
        }
117
    }
118
119
    // Set the DB error
120 6 Manuela
    function set_error($message = null)
121
    {
122 2 Manuela
        $this->error = $message;
123
        $this->error_type = 'unknown';
124
        if ($message!=''){
125
        }
126
    }
127
128
    // Return true if there was an error
129 6 Manuela
    function is_error()
130
    {
131 2 Manuela
        return (!empty($this->error)) ? true : false;
132
    }
133
134
    // Return the error
135 6 Manuela
    function get_error()
136
    {
137 2 Manuela
        return $this->error;
138
    }
139
    // Return the errno
140 6 Manuela
    function get_errno()
141
    {
142 2 Manuela
        return $this->is_error() ? mysqli_errno($this->db_handle) : 0;
143
    }
144
/**
145
 * default Getter for some properties
146
 * @param string $sPropertyName
147
 * @return mixed NULL on error or missing property
148
 */
149
    public function __get($sPropertyName)
150
    {
151
        switch ($sPropertyName):
152
            case 'db_handle':
153
            case 'DbHandle':
154
                $retval = $this->db_handle;
155
                break;
156
            case 'db_name':
157
            case 'DbName':
158
                $retval = $this->db_name;
159
                break;
160 6 Manuela
            case 'sTablePrefix':
161
            case 'TablePrefix':
162
                $retval = $this->sTablePrefix;
163
                break;
164 2 Manuela
            default:
165
                $retval = null;
166
                break;
167
        endswitch;
168
        return $retval;
169
    } // __get()
170
/**
171
 * Escapes special characters in a string for use in an SQL statement
172
 * @param string $unescaped_string
173
 * @return string
174
 */
175
    public function escapeString($unescaped_string)
176
    {
177
        return mysqli_real_escape_string($this->db_handle, $unescaped_string);
178
    }
179
/**
180
 * Last inserted Id
181
 * @return bool|int false on error, 0 if no record inserted
182
 */
183
    public function getLastInsertId()
184
    {
185
        return mysqli_insert_id($this->db_handle);
186
    }
187
188
/*
189
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
190
 * @param string $field_name: name of the field to seek for
191
 * @return bool: true if field exists
192
 */
193
    public function field_exists($table_name, $field_name)
194
    {
195
        $bRetval = false;
196
        $aMatches = array();
197
        $sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
198
        if (($oQuery = $this->query($sql))) {
199
            while (($aRecord = $oQuery->fetchRow(MYSQLI_ASSOC))) {
200
                $aMatches[] = $aRecord['Field'];
201
            }
202
            $bRetval = in_array($field_name, $aMatches);
203
        }
204
        return $bRetval;
205
    }
206
207
/*
208
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
209
 * @param string $index_name: name of the index to seek for
210
 * @return bool: true if field exists
211
 */
212
    public function index_exists($table_name, $index_name, $number_fields = 0)
213
    {
214
        $number_fields = intval($number_fields);
215
        $keys = 0;
216
        $sql = 'SHOW INDEX FROM `'.$table_name.'`';
217
        if (($res_keys = $this->query($sql))) {
218
            while (($rec_key = $res_keys->fetchRow(MYSQLI_ASSOC))) {
219
                if ($rec_key['Key_name'] == $index_name ) {
220
                    $keys++;
221
                }
222
            }
223
        }
224
        if ( $number_fields == 0 ) {
225
            return ($keys != $number_fields);
226
        } else {
227
            return ($keys == $number_fields);
228
        }
229
    }
230
/*
231
    public function index_exists1($sTableName, $sIndexName, $number_fields = 0){
232
      $sql  = 'SHOW INDEX FROM `'.$sTableName.'` WHERE `Column_name`= \''.$sIndexName.'\'';
233
    }
234
*/
235
/*
236
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
237
 * @param string $field_name: name of the field to add
238
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
239
 * @return bool: true if successful, otherwise false and error will be set
240
 */
241
    public function field_add($table_name, $field_name, $description)
242
    {
243
        if( !$this->field_exists($table_name, $field_name) )
244
        { // add new field into a table
245
            $sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
246
            $query = $this->query($sql);
247
            $this->set_error(mysqli_error($this->db_handle));
248
            if( !$this->is_error() )
249
            {
250
                return ( $this->field_exists($table_name, $field_name) ) ? true : false;
251
            }
252
        }else
253
        {
254
            $this->set_error('field \''.$field_name.'\' already exists');
255
        }
256
        return false;
257
    }
258
259
/*
260
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
261
 * @param string $field_name: name of the field to add
262
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
263
 * @return bool: true if successful, otherwise false and error will be set
264
 */
265
    public function field_modify($table_name, $field_name, $description)
266
    {
267
        $retval = false;
268
        if( $this->field_exists($table_name, $field_name) )
269
        { // modify a existing field in a table
270
            $sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
271
            $retval = ( $this->query($sql) ? true : false);
272
            $this->set_error(mysqli_error($this->db_handle));
273
        }
274
        return $retval;
275
    }
276
277
/*
278
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
279
 * @param string $field_name: name of the field to remove
280
 * @return bool: true if successful, otherwise false and error will be set
281
 */
282
    public function field_remove($table_name, $field_name)
283
    {
284
        $retval = false;
285
        if( $this->field_exists($table_name, $field_name) )
286
        { // modify a existing field in a table
287
            $sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
288
            $retval = ( $this->query($sql) ? true : false );
289
        }
290
        return $retval;
291
    }
292
293
/*
294
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
295
 * @param string $index_name: name of the new index (empty string for PRIMARY)
296
 * @param string $field_list: comma seperated list of fields for this index
297
 * @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
298
 * @return bool: true if successful, otherwise false and error will be set
299
 */
300
    public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
301
    {
302
       $retval = false;
303
       $field_list = explode(',', (str_replace(' ', '', $field_list)));
304
       $number_fields = sizeof($field_list);
305
       $field_list = '`'.implode('`,`', $field_list).'`';
306
       $index_name = (($index_type == 'PRIMARY') ? $index_type : $index_name);
307
       if ( $this->index_exists($table_name, $index_name, $number_fields) ||
308
            $this->index_exists($table_name, $index_name))
309
       {
310
           $sql  = 'ALTER TABLE `'.$table_name.'` ';
311
           $sql .= 'DROP INDEX `'.$index_name.'`';
312
           if (!$this->query($sql)) { return false; }
313
       }
314
       $sql  = 'ALTER TABLE `'.$table_name.'` ';
315
       $sql .= 'ADD '.$index_type.' ';
316
       $sql .= (($index_type == 'PRIMARY') ? 'KEY ' : '`'.$index_name.'` ');
317
       $sql .= '( '.$field_list.' ); ';
318
       if ($this->query($sql)) { $retval = true; }
319
       return $retval;
320
    }
321
322
/*
323
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
324
 * @param string $field_name: name of the field to remove
325
 * @return bool: true if successful, otherwise false and error will be set
326
 */
327
    public function index_remove($table_name, $index_name)
328
    {
329
        $retval = false;
330
        if ($this->index_exists($table_name, $index_name)) {
331
        // modify a existing field in a table
332
            $sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
333
            $retval = ( $this->query($sql) ? true : false );
334
        }
335
        return $retval;
336
    }
337
338
    public function setSqlImportActionFile ( $sCallingScript ){
339
       $this->sActionFile = $sCallingScript;
340
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
341
    }
342
343
/**
344
 * Import a standard *.sql dump file
345
 * @param string $sSqlDump link to the sql-dumpfile
346
 * @param string $sTablePrefix
347
 * @param mixed $mAction
348
 *        (bool)true => upgrade (default)
349
 *        (bool)false => install
350
 *        or command (install|uninstall|upgrade) as string
351
 *        or calling script as string
352
 * @param string $sTblEngine
353
 * @param string $sTblCollation
354
 * @return boolean true if import successful
355
 */
356
    public function SqlImport(
357
        $sSqlDump,
358
        $sTablePrefix  = '',
359
        $mAction       = true,
360
        $sTblEngine    = 'MyISAM',
361 6 Manuela
        $sTblCollation = 'utf8mb4_unicode_ci'
362 2 Manuela
    ) {
363
        $iCount = 0;
364
        $sSqlBuffer  = '';
365
        $bRetval     = true;
366
        $this->error = '';
367
        // detect requested action
368
        if (is_string($mAction)) {
369
            // search for valid command string in $mAction
370
            $sAction = strtolower(preg_replace(
371
                '/^.*?(uninstall|install|upgrade)(\.[^\.]+)?$/is',
372
                '$1',
373
                $mAction,
374
                -1,
375
                $iCount
376
            ));
377
            $sAction = $iCount ? $sAction : 'upgrade';
378
        } else if (is_bool($mAction)) {
379
            // on boolean request select true='upgrade' or false='install'
380
            $sAction = $mAction ? 'upgrade' : 'install';
381
        } else {
382
            // select 'upgrade' if no valid command found
383
            $sAction = 'upgrade';
384
        }
385
        // extract charset from given collation
386
        $aTmp = preg_split('/_/', $sTblCollation, null, PREG_SPLIT_NO_EMPTY);
387
        $sCharset = $aTmp[0];
388
        // define placeholders
389
        $aSearch[] = '/\{TABLE_PREFIX\}/';                                        /* step 0 */
390
        $aSearch[] = '/\{FIELD_CHARSET\}/';                                       /* step 1 */
391
        $aSearch[] = '/\{FIELD_COLLATION\}/';                                     /* step 2 */
392
        $aSearch[] = '/\{TABLE_ENGINE\}/';                                        /* step 3 */
393
        $aSearch[] = '/\{TABLE_ENGINE=([a-zA-Z_0-9]*)\}/';                        /* step 4 */
394
        $aSearch[] = '/\{CHARSET\}/';                                             /* step 5 */
395
        $aSearch[] = '/\{COLLATION\}/';                                           /* step 6 */
396
        // define replacements
397
        $aReplace[] = $sTablePrefix;                                              /* step 0 */
398
        $aReplace[] = ' CHARACTER SET {CHARSET}';                                 /* step 1 */
399
        $aReplace[] = ' COLLATE {COLLATION}';                                     /* step 2 */
400
        $aReplace[] = ' {TABLE_ENGINE='.$sTblEngine.'}';                          /* step 3 */
401
        $aReplace[] = ' ENGINE=$1 DEFAULT CHARSET={CHARSET} COLLATE={COLLATION}'; /* step 4 */
402
        $aReplace[] = $sCharset;                                                  /* step 5 */
403
        $aReplace[] = $sTblCollation;                                             /* step 6 */
404
        // read file into an array
405
        if (($aSql = file( $sSqlDump, FILE_SKIP_EMPTY_LINES ))) {
406
            if (sizeof($aSql) > 0) {
407
                // remove possible BOM from file
408
                $aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
409
                // replace placeholders by replacements over the whole file
410
                $aSql = preg_replace($aSearch, $aReplace, $aSql);
411
            } else { $aSql = false; }
412
        }
413
414
        while ((bool)$aSql) {
415
            $sSqlLine = trim(array_shift($aSql));
416
            if (!preg_match('/^[\-\/]+.*/', $sSqlLine)) {
417
                $sSqlBuffer .= ' '.$sSqlLine;
418
                if ((substr($sSqlBuffer,-1,1) == ';')) {
419
                    if (
420
                        // drop tables on install or uninstall
421
                        preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sSqlBuffer) &&
422
                        ($sAction == 'install' || $sAction == 'uninstall')
423
                    ) {
424
                        if (!$this->query($sSqlBuffer)) {
425
                            $aSql = $bRetval = false;
426
                            break;
427
                        }
428
                   } else if (
429
                        // create and alter tables on install or upgrade
430
                        (preg_match('/^\s*CREATE TABLE/siU', $sSqlBuffer) ||
431
                         preg_match('/^\s*ALTER TABLE/siU', $sSqlBuffer)) &&
432
                        ($sAction == 'install' || $sAction == 'upgrade')
433
                    ) {
434
                        if (!$this->query($sSqlBuffer))
435
                        {
436
                            switch ($this->get_errno()):
437
                                case 0: // no error
438
                                case 1060:
439
                                case 1061:
440
                                    break;
441
                                default: // all other errors
442
                                    $aSql = $bRetval = false;
443
                                    break;
444
                            endswitch;
445
                        }
446
                    } else if (
447
                        // insert default data on install
448
                        (preg_match('/^\s*INSERT INTO /siU', $sSqlBuffer)) &&
449
                        ( $sAction == 'install' )
450
                    ) {
451
                        if (!$this->query($sSqlBuffer)) {
452
                            $aSql = $bRetval = false;
453
                            break;
454
                        }
455
                    }
456
                    // clear buffer for next statement
457
                    $sSqlBuffer = '';
458
                }
459
            }
460
        }
461
        return $bRetval;
462
    }
463
464
/**
465
 * retuns the type of the engine used for requested table
466
 * @param string $table name of the table, including prefix
467
 * @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
468
 */
469
    public function getTableEngine($table)
470
    {
471
        $retVal = false;
472
        $mysqlVersion = mysqli_get_server_info($this->db_handle);
473
        $engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
474
        $sql = "SHOW TABLE STATUS FROM " . $this->db_name . " LIKE '" . $table . "'";
475
        if(($result = $this->query($sql))) {
476
            if(($row = $result->fetchRow(MYSQLI_ASSOC))) {
477
                $retVal = $row[$engineValue];
478
            }
479
        }
480
        return $retVal;
481
    }
482
483
484
} /// end of class database
485
486
define('MYSQL_SEEK_FIRST', 0);
487
define('MYSQL_SEEK_LAST', -1);
488
define('MYSQLI_SEEK_FIRST', 0);
489
define('MYSQLI_SEEK_LAST', -1);
490
491 6 Manuela
class mysql
492
{
493 2 Manuela
    private $db_handle = null;
494
    private $result = null;
495
    private $error = '';
496
497 6 Manuela
    public function __construct($handle)
498
    {
499 2 Manuela
        $this->db_handle = $handle;
500
    }
501
/**
502
 * query sql statement
503
 * @param  string $statement
504
 * @return object
505
 * @throws WbDatabaseException
506
 */
507
    public function query($sStatement)
508
    {
509
        $this->result = @mysqli_query($this->db_handle, $sStatement);
510
        if (defined('DEBUG')&& DEBUG && ($this->result === false)) {
511
            if (DEBUG) {
512
                throw new DatabaseException(mysqli_error($this->db_handle));
513
            } else {
514
                throw new DatabaseException('Error in SQL-Statement');
515
            }
516
        }
517
        $this->error = mysqli_error($this->db_handle);
518
        return $this->result;
519
    }
520
521
    // Fetch num rows
522 6 Manuela
    public function numRows()
523
    {
524 2 Manuela
        return mysqli_num_rows($this->result);
525
    }
526
527
    // Fetch row  $typ = MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
528 6 Manuela
    public function fetchRow($typ = MYSQLI_BOTH)
529
    {
530 2 Manuela
        return mysqli_fetch_array($this->result, $typ);
531
    }
532
/**
533
 * fetchAssoc
534
 * @return array with assotiative indexes
535
 * @description get current record and increment pointer
536
 */
537
    public function fetchAssoc()
538
    {
539
        return mysqli_fetch_assoc($this->result);
540
    }
541
/**
542
 * fetchArray
543
 * @param  int $iType MYSQL_ASSOC(default) | MYSQL_BOTH | MYSQL_NUM
544
 * @return array of current record
545
 * @description get current record and increment pointer
546
 */
547
    public function fetchArray($iType = MYSQLI_ASSOC)
548
    {
549
        if ($iType < MYSQLI_ASSOC || $iType > MYSQLI_BOTH) {
550
            $iType = MYSQLI_ASSOC;
551
        }
552
        return mysqli_fetch_array($this->result, $iType);
553
    }
554
/**
555
 * fetchObject
556
 * @param  string $sClassname Name of the class to use. Is no given use stdClass
557
 * @param  string $aParams    optional array of arguments for the constructor
558
 * @return object
559
 * @description get current record as an object and increment pointer
560
 */
561
    public function fetchObject($sClassName = 'stdClass', array $aParams = [])
562
    {
563
        if ($sClassName === 'stdClass' || !$sClassName) {
564
            $oRetval = mysqli_fetch_object($this->result, 'stdClass');
565
        } elseif (class_exists($sClassName)) {
566
            $oRetval = mysqli_fetch_object($this->result, $sClassName, $aParams);
567
        } else {
568
            throw new DatabaseException('Class <'.$sClassName.'> not available on request of mysqli_fetch_object()');
569
        }
570
        return $oRetval;
571
    }
572
/**
573
 * fetchAll
574
 * @param  int $iType MYSQL_ASSOC(default) | MYSQL_NUM
575
 * @return array of rows
576
 * @description get all records of the result set
577
 */
578
    public function fetchAll($iType = MYSQLI_ASSOC)
579
    {
580
        $iType = $iType != MYSQLI_NUM ? MYSQLI_ASSOC : MYSQLI_NUM;
581
582
        if (function_exists('mysqli_fetch_all')) { # Compatibility layer with PHP < 5.3
583
            $aRetval = mysqli_fetch_all($this->result, $iType);
584
        } else {
585
            for ($aRetval = array(); ($aTmp = mysqli_fetch_array($this->result, $iType));) { $aRetval[] = $aTmp; }
586
        }
587
        return $aRetval;
588
    }
589
590
    public function rewind()
591
    {
592
        return $this->seekRow();
593
    }
594
595
    public function seekRow( $position = MYSQLI_SEEK_FIRST )
596
    {
597
        $pmax = $this->numRows() - 1;
598
        $offset = (($position < 0 || $position > $pmax) ? $pmax : $position);
599
        return mysqli_data_seek($this->result, $offset);
600
    }
601
602
    // Get error
603 6 Manuela
    public function error()
604
    {
605
        if (isset($this->error)) {
606 2 Manuela
            return $this->error;
607
        } else {
608
            return null;
609
        }
610
    }
611
612
} // end of class mysql
613
614
class DatabaseException extends AppException {}
615
616
/* this function is placed inside this file temporarely until a better place is found */
617
/*  function to update a var/value-pair(s) in table ****************************
618
 *  nonexisting keys are inserted
619
 *  @param string $table: name of table to use (without prefix)
620
 *  @param mixed $key:    a array of key->value pairs to update
621
 *                        or a string with name of the key to update
622
 *  @param string $value: a sting with needed value, if $key is a string too
623
 *  @return bool:  true if any keys are updated, otherwise false
624
 */
625
    function db_update_key_value($table, $key, $value = '')
626
    {
627
        global $database;
628
        if( !is_array($key))
629
        {
630
            if( trim($key) != '' )
631
            {
632
                $key = array( trim($key) => trim($value) );
633
            } else {
634
                $key = array();
635
            }
636
        }
637
        $retval = true;
638
        foreach( $key as $index=>$val)
639
        {
640
            $index = strtolower($index);
641
            $sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' ';
642
            if (intval($database->get_one($sql))>0)
643
            {
644
                $sql = 'UPDATE ';
645
                $sql_where = 'WHERE `name` = \''.$index.'\'';
646
            } else {
647
                $sql = 'INSERT INTO ';
648
                $sql_where = '';
649
            }
650
            $sql .= '`'.TABLE_PREFIX.$table.'` ';
651
            $sql .= 'SET `name` = \''.$index.'\', ';
652
            $sql .= '`value` = \''.$val.'\' '.$sql_where;
653
            if (!$database->query($sql) )
654
            {
655
                $retval = false;
656
            }
657
        }
658
        return $retval;
659
    }