Source of trunk/l1_mysql.php at revision 432 (01/19/2010 1:01:04, 28694 bytes, 902 lines, language: php) [download]:

1
<?php
2
3
/*
4
** L1 MySQL Driver version 2.0.1
5
** L1 SQL version 2.0
6
**
7
** http://projects.codewise.org/l1_sql/
8
**
9
** by William R. Fraser <wrf@codewise.org>
10
** Copyright (c) 2004-2009 Codewise.org
11
*/
12
13
/*
14
** This file is part of L1 SQL
15
**
16
** L1 SQL is free software; you can redistribute it and/or modify
17
** it under the terms of the GNU General Public License as published by
18
** the Free Software Foundation; either version 2 of the License, or
19
** (at your option) any later version.
20
**
21
** L1 SQL is distributed in the hope that it will be useful,
22
** but WITHOUT ANY WARRANTY; without even the implied warranty of
23
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
** GNU General Public License for more details.
25
**
26
** You should have received a copy of the GNU General Public License
27
** along with L1 SQL; if not, write to the Free Software
28
** Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
29
*/
30
31
if (!function_exists("mysql_connect")) {
32
    die("MySQL PHP extension not installed!");
33
}
34
35
if(!defined("L1SQL_ASSOC")) define("L1SQL_ASSOC",1);
36
if(!defined("L1SQL_NUM"))   define("L1SQL_NUM",  2);
37
if(!defined("L1SQL_BOTH"))  define("L1SQL_BOTH"3);
38
39
if(!class_exists("L1_MySQL")) {
40
class L1_MySQL
41
{
42
    var $set false//true if connected and database set
43
44
    var $hostname "";
45
    var $username "";
46
    var $password "";
47
    var $database "";
48
    var $session null;
49
50
    var $error_no 0;
51
    var $error_str "";
52
    var $halt_on_error true//if true, stop on an error
53
54
    var $results = array();  //array of query results by index
55
    var $num_rows = array(); //array of row numbers by index
56
    var $queries = array();  //array of query strings by index
57
58
    var $error_callback;   // function for displaying fatal errors
59
    var $warning_callback// function for displaying warnings
60
61
    var $pconnect false// use persistent connections?
62
63
    /*
64
    ** Return version string.
65
    */
66
    function version()
67
    {
68
        return("2.0.0");
69
    }
70
71
    /*
72
    ** Return L1 SQL API version
73
    */
74
    function apiversion()
75
    {
76
        return("2.0");
77
    }
78
79
    /*
80
    ** For getting member variable values
81
    */
82
    function get($var)
83
    {
84
        return($this->$var);
85
    }
86
87
    /*
88
    ** For setting member variable values
89
    */
90
    function set($var,$value)
91
    {
92
        $this->$var $value;
93
        return($value);
94
    }
95
96
    /*
97
    ** Escape and prepare values for inclusion in a MySQL query.
98
    **
99
    ** Backslashes single slashes and adds single slashes around values.
100
    ** eg: "O'Reilly's" => "'O\'Reilly\'s'"
101
    ** Also converts php null value to "null"
102
    **
103
    ** If data is an array (must be associative), the keys are treated as
104
    ** column names and the values are treated as SQL values and both are
105
    ** escaped as such.
106
    **
107
    ** The $use_quotes argument dictates whether to enclose values in single
108
    ** quotes.
109
    */
110
    function prepare_value($data$use_quotes TRUE)
111
    {
112
        if($use_quotes)
113
            $quote "'";
114
        else
115
            $quote "";
116
117
        if(is_array($data))
118
        {
119
            $a_sql = array();
120
            foreach($data as $field => $value)
121
            {
122
                $a_sql$this->prepare_value($field,FALSE) ] = ($value === null "null" $quote.mysql_real_escape_string($value,$this->session).$quote);
123
            }
124
            return($a_sql);
125
        } else {
126
            $sql = ($data === null "null" $quote.mysql_real_escape_string($data,$this->session).$quote);
127
            return($sql);
128
        }
129
    }
130
131
    /*
132
    ** Format an array of data into an INSERT query
133
    ** Data is in the form of array(columname=>value,...)
134
    **
135
    ** It will pass the query to issue_query() unless the third parameter is TRUE,
136
    ** in which case it returns the SQL statement generated.
137
    **
138
    ** Otherwise it returns the index of the query or FALSE if the query failed.
139
    */
140
    function insert($table,$a_values,$return false)
141
    {
142
        if(!$this->set && !stristr($table,"."))
143
        {
144
            if(!$this->trycode("database"))
145
                $this->error("Could not change database, database not specified in table parameter");
146
        }
147
148
        $a_sql $this->prepare_value($a_values);
149
        $fields implode(",",array_keys($a_sql));
150
        $values implode(",",array_values($a_sql));
151
152
        $sql "INSERT INTO $table ($fields) VALUES($values)";
153
        if($return)
154
            return($sql);
155
        $index $this->trycode("issue_query",$sql);
156
        if(!$index)
157
            $this->error("Error issuing INSERT query.");
158
        return($index);
159
    }
160
161
    /*
162
    ** Format an array of data into an UPDATE query
163
    ** Data is in the form of array(columname=>value,...)
164
    **
165
    ** The $condition argument is an array in the form of
166
    ** array(columnname=>value), and it causes the statement to be applied where
167
    ** columnname has a value equal to value.
168
    **
169
    ** It will pass the query to issue_query() unless the fourth parameter is
170
    ** TRUE, in which case it returns the SQL statement generated.
171
    **
172
    ** Otherwise it returns the index of the query or FALSE if the query failed.
173
    */
174
    function update($table,$a_values,$condition,$return false)
175
    {
176
        if(!$this->set && !stristr($table,"."))
177
        {
178
            if(!$this->trycode("database"))
179
                $this->error("Could not change database, database not specified in table parameter");
180
        }
181
182
        $a_sql = array();
183
        $a_sql $this->prepare_value($a_values);
184
        $condition $this->prepare_value($condition);
185
        foreach($a_sql as $col => $val)
186
            $set .= "$col = $val, ";
187
        $set substr($set,0,-2);
188
189
        foreach($condition as $col => $val)
190
            $cond .= "$col = $val AND ";
191
        $cond substr($cond,0,-5);
192
193
        $sql "UPDATE $table SET $set WHERE $cond";
194
        if($return)
195
            return($sql);
196
        $index $this->trycode('issue_query',$sql);
197
        if(!$index)
198
            $this->error("Error issuing UPDATE query.");
199
        return($index);
200
    }
201
202
    /*
203
    ** Issue a mysql query.
204
    **
205
    ** Returns the array index that will be used to store results and rows,
206
    ** returns FALSE on query failure
207
    */
208
    function issue_query($query)
209
    {
210
        list($usec,$sec) = explode(" ",microtime()); // this may be changed in the future
211
        $index = (string) $sec substr($usec,1);    //
212
        if(!@mysql_get_server_info($this->session))
213
        {
214
            if(!$this->trycode("connect")) // try to connect with current params
215
            {
216
                $this->error("Driver not connected, connection tried and failed.");
217
                return(false);
218
            }
219
        }
220
        $this->queries[$index] = $query;
221
        $this->num_rows[$index] = 0;
222
        $this->results[$index] = @mysql_query($query,$this->session);
223
        // hmm, should we generate error or leave it up to scripts? For now, yes.
224
        if($this->check_error("Query error",$php_errormsg)) return(false);
225
        if(!$this->results[$index])
226
        {
227
            $this->error("Query error");
228
            return(false);
229
        }
230
        $this->num_rows[$index] = @mysql_affected_rows($this->session);
231
        return $index;
232
    }
233
234
    /*
235
    ** Returns an array of all the tables in the current database as a numeric
236
    ** array
237
    **
238
    ** Returns FALSE on any kind of error.
239
    */
240
    function fetch_tables()
241
    {
242
        if(!$this->set)
243
            $this->error("Database connection is not set.");
244
245
        list($usec,$sec) = explode(" ",microtime()); // same method as issue_query()
246
        $index = (string) $sec substr($usec,1);    //
247
248
        $this->results[$index] = @mysql_list_tables($this->database,$this->session);
249
        $this->queries[$index] = "L1_MySQL::fetch_tables()"// special query
250
        $this->num_rows[$index] = 0;
251
252
        if($this->check_error("Error listing tables")) return(false);
253
        if(!$this->results[$index])
254
        {
255
            $this->error("Error listing tables",$php_errormsg);
256
            return(false);
257
        }
258
259
        $this->num_rows[$index] = @mysql_affected_rows($this->session);
260
261
        $return = array();
262
        while($row = @mysql_fetch_array($this->results[$index],L1SQL_NUM))
263
        {
264
            if($this->check_error("Error fetching table listing")) return(false);
265
            $return array_merge($return,$row);
266
        }
267
        return($return);
268
    }
269
270
    /*
271
    ** Fetch all the rows of a query
272
    **
273
    ** This will return a multidimensional array of the form
274
    ** $return[row][column]
275
    ** You must give the index returned by issue_query.
276
    ** Specify the format of the column as the first argument (it is a constant
277
    ** passed to mysql_fetch_array ;) and if you want the rows indexed by a
278
    ** particular field, list that as the second argument, otherwise they'll be
279
    ** numeric in the order returned by MySQL.
280
    ** The results are also stored in $this->row[$index]
281
    */
282
    function fetch_all($index$type L1SQL_BOTH$index_by "")
283
    {
284
        if(($type != L1SQL_ASSOC) && ($type != L1SQL_NUM) && ($type != L1SQL_BOTH))
285
        {
286
            $this->error("Invalid type specified for L1_MySQL::fetch_all");
287
            return(false);
288
        }
289
        $return = array();
290
        for($i=0;$row = @mysql_fetch_array($this->results[$index],$type);$i++)
291
        {
292
            if($index_by !== "")
293
            {
294
                $field $row[$index_by];
295
                $return[$field] = $row;
296
            } else {
297
                $return[$i] = $row;
298
            }
299
        }
300
        //$this->free_result($index);
301
        return($return);
302
    }
303
304
    /*
305
    ** Fetch the result as an array of rows in a column
306
    **
307
    ** For use when the result is many rows containing one column each
308
    ** It will return a numeric array based on the order the rows are returned.
309
    */
310
    function fetch_column($index$col_offset 0$index_by "")
311
    {
312
        $return=array();
313
        for($i=0;$row = @mysql_fetch_array($this->results[$index],L1SQL_BOTH);$i++)
314
        {
315
            $this->check_error("Error fetching column",$php_errormsg);
316
            $php_errormsg false;
317
            if(!isset($row[$col_offset]))
318
            {
319
                $this->error("Column offset too high: column $col_offset requested, ".
320
                    count($row)." columns in result");
321
                return(false);
322
            }
323
            $idx $index_by === "" $i $row[$index_by];
324
            $return[$idx]=$row[$col_offset];
325
            //$this->free_result($index);
326
        }
327
        return($return);
328
    }
329
330
    /*
331
    ** Fetch one row of the result of a query.
332
    **
333
    ** You must give the index returned by issue_query().
334
    ** The return value can be an associative array (column => value),
335
    ** numeric ([0] => value), or both.
336
    ** Specify the argument to mysql_fetch_array.
337
    */
338
    function fetch_row($index$row_offset 0$type L1SQL_BOTH)
339
    {
340
        if(($type != L1SQL_ASSOC) && ($type != L1SQL_NUM) && ($type != L1SQL_BOTH))
341
        {
342
            $this->error("Invalid type specified for L1_MySQL::fetch_all");
343
            return(false);
344
        }
345
346
        if($this->num_rows[$index] < $row_offset)
347
        {
348
            $this->error("Row offset too high: row $row_offset requested, ".
349
                $this->num_rows[$index]." rows in result.");
350
            return(false);
351
        }
352
353
        // loop through specified # of rows
354
        for($i=0;$i<$row_offset;$i++)
355
            @mysql_fetch_array($this->results[$index]);
356
357
        $row = @mysql_fetch_array($this->results[$index],$type);
358
        if($this->check_error("Error fetching row",$php_errormsg)) return(false);
359
        if(is_array($row))
360
        {
361
            return($row);
362
        } else {
363
            //$this->free_result($index);
364
            return(false);
365
        }
366
    }
367
368
    /*
369
    ** Fetch a single variable from a query
370
    **
371
    ** You must give the index returned by issue_query().
372
    **
373
    ** Use this to get a single value from a query at the specified row and column
374
    ** offsets. The column offset can be a string row name or numeral row number.
375
    **
376
    ** On error, a message is generated and the function returns FALSE.
377
    */
378
    function fetch_var($index$row_offset 0$col_offset 0)
379
    {
380
        if($this->num_rows[$index] < $row_offset)
381
        {
382
            $this->error("Row offset too high: row $row_offset requested, ".
383
                "{$this->num_rows[$index]} rows in result");
384
            return(false);
385
        }
386
387
        // loop through the specified # of rows
388
        for($i=0;$i<$row_offset;$i++)
389
            @mysql_fetch_array($this->results[$index]);
390
391
        $row = @mysql_fetch_array($this->results[$index],L1SQL_NUM);
392
        if($this->check_error("Error fetching row",$php_errormsg)) return(false);
393
394
        //if(isset($row[$col_offset]))
395
        if(in_array($col_offsetarray_keys($row)))
396
        {
397
            $return $row[$col_offset];
398
        } else {
399
            $this->error("Column offset too high: column $col_offset requested, ".
400
                count($row)." columns in result");
401
            $return false;
402
        }
403
404
        return($return);
405
    }
406
407
    /*
408
    ** Free result of a query
409
    **
410
    ** You must give the index of the results to free
411
    ** This function isn't actually ever used here; all results are saved.
412
    ** You can use this if you want, but it isn't really necessary.
413
    */
414
    function free_result($index)
415
    {
416
        if(@is_resource($this->results[$index]))
417
        {
418
            $ok = @mysql_free_result($this->results[$index]);
419
            if(!$ok)
420
                $this->error("Error freeing result",$php_errormsg);
421
        } else {
422
            $this->error("Result is not a resource",$php_errormsg);
423
        }
424
        unset($this->results[$index]);
425
        // might as well unset these too
426
        unset($this->query[$index]);
427
        unset($this->num_rows[$index]);
428
429
        return(true);
430
    }
431
432
    /*
433
    ** Print an error
434
    **
435
    ** This is the origional error message function that only prints one line of
436
    ** backtrace info (it tries to determine the most relevant line)
437
    */
438
    function errorMsg($message,$backtrace,$severity)
439
    {
440
        if(empty($backtrace))
441
        {
442
            $out "$message<br /><i>no backtrace info available</i><br />\n";
443
        } else {
444
            for($i=count($backtrace);$i>-1;$i--)
445
            {
446
                if(!stristr($backtrace[$i]['file'],__FILE__))
447
                {
448
                    $traceline $backtrace[$i+1];
449
                }
450
            }
451
452
            $line = isset($traceline['line'])?$traceline['line']:"unknown";
453
            $file = isset($traceline['file'])?$traceline['file']:"unknown";
454
            $function = isset($traceline['function'])?$traceline['function']."()":"unknown";
455
            $class = isset($traceline['class'])? " in class <b>".$traceline['class']."</b>" "";
456
            $out "$message<br />in fule <b>$file</b> on line <b>$line</b> in function <b>$function</b>$class<br />";
457
        }
458
        switch($severity)
459
        {
460
case E_USER_ERROR:
461
            if($this->error_callback === FALSE)
462
                ; // do nothing
463
            elseif($this->error_callback !== null)
464
                call_user_func($this->error_callback,$out);
465
            else
466
                print($out);
467
            exit;
468
469
case E_USER_WARNING:
470
            if($this->warning_callback === FALSE)
471
                ; // do nothing
472
            elseif($this->warning_callback !== null)
473
                call_user_func($this->warning_callback,$out);
474
            else
475
                print($out);
476
477
case E_USER_NOTICE:
478
default:
479
            break;
480
        }
481
    }
482
483
    function multiErrorMsg($message,$backtrace,$severity)
484
    {
485
        switch($severity)
486
        {
487
case E_USER_ERROR:
488
case E_USER_WARNING:
489
                $out $message."<br />\n";
490
                if(empty($backtrace))
491
                {
492
                    $out .= "<i>no backtrace info available</i><br />\n";
493
                } else {
494
                    foreach($backtrace as $entry)
495
                    {
496
                        $line = isset($entry['line']) ? $entry['line'] : "unknown";
497
                        $file = isset($entry['file']) ? $entry['file'] : "unknown";
498
                        $function = isset($entry['function']) ? $entry['function']."()" "unknown";
499
                        $class = isset($entry['class']) ? " in class <b>".$entry['class']."</b>" "";
500
                        $out .= "in file <b>".$file."</b> on line <b>".$line."</b> in function <b>".$function."</b>".$class."<br />\n";
501
                    }
502
                }
503
                if($severity == E_USER_ERROR)
504
                {
505
                    if($this->warning_callback === FALSE) {
506
                        // do nothing
507
                    } elseif($this->error_callback !== null) {
508
                        call_user_func($this->error_callback,$out."<b>FATAL</b>\n");
509
                        exit;
510
                    } else {
511
                        print($out."<b>FATAL</b>\n");
512
                        exit;
513
                    }
514
                } else {
515
                    if($this->warning_callback === FALSE) {
516
                        // do nothing
517
                    } elseif($this->warning_callback !== null) {
518
                        call_user_func($this->warning_callback,$out);
519
                    } else {
520
                        print($out);
521
                    }
522
                }
523
524
case E_USER_NOTICE:
525
default:
526
                break;
527
        }
528
    }
529
530
    /*
531
    ** Handle an error
532
    **
533
    ** Use this when you're sure there is an error, and you want
534
    ** it cleanly handled.
535
    ** It will display the error code and explanation as well as
536
    ** the message you set.
537
    **
538
    ** You MUST pass $php_errormsg as the second parameter for it to be used!
539
    **
540
    ** It will obey $halt_on_error unless behavior
541
    ** is otherwise specified in the third parameter.
542
    **
543
    ** The fourth parameter should only be set when being called by check_error().
544
    **
545
    ** If the fifth parameter is true, a shorter error message is printed
546
    ** (with only one line of backtrace instead of the default all).
547
    */
548
    function error($message ""$php ""$force_abort ""$check_error ""$short_error false)
549
    {
550
        if($check_error === ""// we're being called from a script
551
        {
552
            if(mysql_errno()) //MySQL error
553
            {
554
                $type "MySQL";
555
                $this->error_no mysql_errno();
556
                $this->error_str mysql_error();
557
            } elseif($php) { //PHP error
558
                $type "Script";
559
                $this->error_no = -1;
560
                $this->error_str $php;
561
            } else { //some other error
562
                $type "Special";
563
                $this->error_no = -1;
564
                $this->error_str "L1 MySQL Driver special error";
565
            }
566
        } else { // check_error() has already diagnosed the problem
567
            $type $check_error;
568
        }
569
570
        $msg_func = ($short_error) ? "errorMsg" "multiErrorMsg";
571
572
        if(function_exists("debug_backtrace"))
573
            $backtrace debug_backtrace();
574
        else
575
            $backtrace null;
576
577
        if($force_abort !== "" $force_abort $this->halt_on_error)
578
        {
579
            $this->disconnect();
580
            $this->$msg_func(nl2br(htmlspecialchars(
581
                "$message\n$type error #".$this->error_no." (".$this->error_str.")"
582
            )),$backtrace,E_USER_ERROR);
583
        } else {
584
            $this->$msg_func(nl2br(htmlspecialchars(
585
                "$message\n$type warning #".$this->error_no." (".$this->error_str.")"
586
            )),$backtrace,E_USER_WARNING);
587
        }
588
    }
589
590
    /*
591
    ** Check for an error
592
    **
593
    ** This is for use when you aren't sure there's an error
594
    ** or it's not very easy to test for it. This will catch it.
595
    **
596
    ** You MUST pass $php_errormsg as the second parameter for it to be used!
597
    **
598
    ** Set the message to give if an error is found,
599
    ** and if it's necessary to stop on error, set the third parameter.
600
    **
601
    ** This function returns TRUE if an error was detected, otherwise FALSE;
602
    ** this makes it easy to do things like:
603
    **   if(check_error("message")) return(false);
604
    */
605
    function check_error($message ""$php ""$force_abort ""$short_error false)
606
    {
607
        if(mysql_errno()) //mysql error
608
        {
609
            $type "MySQL";
610
            $this->error_no mysql_errno();
611
            $this->error_str mysql_error();
612
        } elseif($php) { //php error
613
            $type "Script";
614
            $this->error_no = -1;
615
            $this->error_str $php;
616
        } else { //no error
617
            $type "";
618
            $this->error_no 0;
619
            $this->error_str "";
620
        }
621
622
        if($type != "")
623
        {
624
            if($message !== "")
625
            {
626
                $this->error($message,$php,$force_abort,$type,$short_error);
627
                return(true);
628
            } else {
629
                return(true);
630
            }
631
        } else {
632
            $php_errormsg null//clear any errors generated by this function.
633
            return(false); //all quiet on the western front ;)
634
        }
635
    }
636
637
    /*
638
    ** Switch to the selected database
639
    */
640
    function database($database)
641
    {
642
        if(!$this->session)
643
        {
644
            $conn_success $this->trycode("connect");
645
            if(!$conn_success)
646
            {
647
                $this->error("Could not connect");
648
                return(false);
649
            }
650
        }
651
652
        $ok = @mysql_select_db($database$this->session);
653
        if(!$ok)
654
        {
655
            $this->error("Error selecting database",$php_errormsg);
656
            return(false);
657
        }
658
659
        $this->database $database;
660
        $this->set true;
661
        return(true);
662
    }
663
664
    /*
665
    ** Connect to database server
666
    **
667
    ** It will use given connection settings or use ones already in the class.
668
    ** It returns true on success.
669
    ** Host should be either "localhost[:/path/to/sock]", "1.2.3.4[:port]" or
670
    **   "hostname[:port]". Only use "localhost..." for local socket connections.
671
    */
672
    function connect($hostname ""$username ""$password "")
673
    {
674
        if($hostname === "")
675
            $hostname $this->hostname;
676
        else
677
            $this->hostname $hostname;
678
679
        if($username === "")
680
            $username $this->username;
681
        else
682
            $this->username $username;
683
684
        if($password === "")
685
            $password $this->password;
686
        else
687
            $this->password $password;
688
689
        if($this->pconnect)
690
        {
691
            $this->session = @mysql_pconnect($hostname,$username,$password);
692
        } else {
693
            $this->session = @mysql_connect($hostname,$username,$password);
694
        }
695
696
        if(!$this->session)
697
        {
698
            $this->error("Could not connect to database server",$php_errormsg);
699
            return(false);
700
        } else {
701
            return(true);
702
        }
703
    }
704
705
    /*
706
    ** Disconnect from database
707
    ** and optionally reset the class
708
    */
709
    function disconnect($reset false)
710
    {
711
        if(!@mysql_get_server_info($this->session))
712
            return(false);
713
        $ok = @mysql_close($this->session);
714
        if(!$ok)
715
        {
716
            $this->error("Couldn't disconnect from database server",$php_errormsg);
717
            return(false);
718
        }
719
        $this->session "";
720
        $this->set false;
721
        if($reset)
722
        {
723
            $this->hostname "";
724
            $this->username "";
725
            $this->password "";
726
            $this->database "";
727
728
            $this->error_no 0;
729
            $this->error_str "";
730
731
            $this->results = array();
732
            $this->num_rows = array();
733
            $this->queries = array();
734
735
            $this->error_callback null;
736
            $this->warning_callback null;
737
            $this->pconnect false;
738
        }
739
        return(true);
740
    }
741
742
    /*
743
    ** Instantiater function
744
    **
745
    ** If given the proper values, will set up class and connect to database server.
746
    ** If also given a database, will switch to the database.
747
    */
748
    function L1_MySQL($hostname ""$username ""$password ""$database ""$halt_on_error true$pconnect false)
749
    {
750
        $this->halt_on_error $halt_on_error//override the default
751
        $this->pconnect $pconnect// override the default
752
        if(($hostname !== "") && ($username !== "") && ($password !== ""))
753
        {
754
            $this->session = @mysql_connect($hostname,$username,$password);
755
            if(!@mysql_get_server_info($this->session))
756
                $this->error("Could not connect to database server",$php_errormsg);
757
            $this->hostname $hostname;
758
            $this->username $username;
759
            $this->password $password;
760
761
            if($database !== ""//just to be safe; database could be "false" or "0"
762
            {
763
                $this->database $database;
764
                $ok = @mysql_select_db($database,$this->session);
765
                if(!$ok)
766
                    $this->error("Could not select database",$php_errormsg);
767
                $this->set true;
768
            } else {
769
                $this->set false;
770
            }
771
        }
772
    }
773
774
    /*
775
    ** Class dumper
776
    **
777
    ** This is a good function to use for debugging
778
    ** WARNING!! This will reveal the password field of the class unless
779
    ** you specify otherwise!
780
    */
781
    function dump($no_password false)
782
    {
783
        if(!$no_password)
784
        {
785
            print("<pre>\n");
786
            print_r($this);
787
            print("</pre>\n");
788
        } else {
789
            ob_start();
790
            print("<pre>\n");
791
            print_r($this);
792
            print("</pre>\n");
793
            $out ob_get_contents();
794
            ob_clean();
795
            $out ereg_replace("    \[password\] => [^\n]*\n","    [password] => *****\n",$out);
796
            print($out);
797
        }
798
    }
799
800
    /*
801
    ** Executes code in an environment where errors will be completely ignored.
802
    ** Error messages will be suppressed, and execution will continue thru errors.
803
    **
804
    ** To execute code, pass the name of the function in this class to execute,
805
    ** and pass the remaining arguments in order.
806
    */
807
    function trycode($func)
808
    {
809
        ob_start();
810
        $params func_get_args();
811
        array_shift($params);
812
813
        $old_halt $this->halt_on_error;
814
        $this->halt_on_error false;
815
        $old_warning_callback $this->warning_callback;
816
        $this->warning_callback null;
817
        $old_error_callback $this->error_callback;
818
        $this->error_callback null;
819
820
        $retval call_user_func_array(array(&$this,$func),$params);
821
        //eval('$retval = '.$eval_code);
822
823
        $this->error_callback $old_error_callback;
824
        $this->warning_callback $old_warning_callback;
825
        $this->halt_on_error $old_halt;
826
827
        ob_end_clean();
828
        return($retval);
829
    }
830
831
    function register_error_func($callback,$type "")
832
    {
833
        switch($type)
834
        {
835
case E_USER_ERROR:
836
            if(function_exists($callback))
837
                $this->error_callback $callback;
838
            break;
839
840
case E_USER_WARNING:
841
            if(function_exists($callback))
842
                $this->warning_callback $callback;
843
            break;
844
845
default:        if(function_exists($callback))
846
                $this->error_callback $this->warning_callback $callback;
847
            break;
848
        }
849
    }
850
851
    function unregister_error_func($type "")
852
    {
853
        switch($type)
854
        {
855
case E_USER_ERROR:
856
            $this->error_callback null;
857
            break;
858
859
case E_USER_WARNING:
860
            $this->warning_callback null;
861
            break;
862
863
default:        $this->error_callback $this->warning_callback null;
864
            break;
865
        }
866
    }
867
868
}}
869
870
/*
871
** Simple replacement function for pre PHP 4.3.0
872
*/
873
if(!function_exists("mysql_real_escape_string"))
874
{
875
    function mysql_real_escape_string($string$link "")
876
    {
877
        $search = array
878
        (
879
            "\\",
880
            "\x00",
881
            "\n",
882
            "\r",
883
            "'",
884
            "\"",
885
            "\x1a",
886
        );
887
        $replace = array
888
        (
889
            "\\\\",
890
            "\\\x00",
891
            "\\\n",
892
            "\\\r",
893
            "\\'",
894
            "\\\"",
895
            "\\\x1a",
896
        );
897
        return(str_replace($search,$replace,$string));
898
    }
899
}
900
901
?>
902

powered by Codewise Manager v0.1-DEV :: 182.52ms, 6 ops, 3 queries