Croissierd /system/db/db.mysql.php

Language PHP Lines 797
MD5 Hash 35ab95f66eb1fdf80a2a1d2df873f1b9
Repository https://github.com/danboy/Croissierd.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
<?php
/*
=====================================================
 ExpressionEngine - by EllisLab
-----------------------------------------------------
 http://expressionengine.com/
-----------------------------------------------------
 Copyright (c) 2003 - 2010 EllisLab, Inc.
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
 http://expressionengine.com/docs/license.html
=====================================================
 File: db.mysql.php
-----------------------------------------------------
 Purpose: SQL database abstraction: MySQL
=====================================================
*/


if ( ! defined('EXT'))
{
    exit('Invalid file request');
}


//---------------------------------------    
//    DB Cache Class
//---------------------------------------

// This object gets serialized and cached.
// It provides a simple mechanism to store queries
// that are portable as objects

class DB_Cache {

    var $result   = array();
    var $row      = array();
    var $num_rows = 0;
    var $q_count  = 0;
}
// END CLASS



//---------------------------------------    
//	DB Class
//---------------------------------------


class DB {

    // Public variables

    var $hostname       	= 'localhost';
    var $username      		= 'root';
    var $password      		= '';
    var $database       	= '';
    var $prefix         	= 'exp_';       // Table prefix
    var $conntype       	= 1;            // 1 = persistent.  0 = non
    var $cache_dir      	= 'db_cache/';  // Cache directory/path with trailing slash.
    var $debug          	= 0;            // Manually turns on debugging
    var $enable_cache   	= TRUE;         // true/false Enables query caching
	var $error_footer		= '';			// This is used by the update script
	var $error_header		= '';

    // Private variables. 

    var $exp_prefix     	= 'exp_';
    var $cache_path     	= '';
    var $cache_file     	= '';
    var $sql_table      	= '';
    var $insert_id      	= '';
    var $q_count        	= 0;
    var $affected_rows  	= 0;
    var $conn_id        	= FALSE;
    var $query_id       	= FALSE;
    var $fetch_fields   	= FALSE;
    var $cache_enabled		= FALSE;
    var $field_names    	= array();
    var $tables_list		= array();
    var $show_queries		= FALSE;		// Enables queries to be shown for debugging
    var $queries			= array();		// Stores the queries
    var $server_info		= '';			// MySQL Server Info, like version


    /** ---------------------------------------    
    /**  Constructor
    /** ---------------------------------------*/

    function DB($settings)
    {
        global $PREFS;
        
		$db_settings = array(
								'hostname', 
								'username',
								'password',
								'database',
								'conntype',
								'prefix',
								'debug',
								'show_queries',
								'enable_cache'
							);
       
		foreach ($db_settings as $item)
		{
			if (isset($settings[$item]))
			{
				$this->$item = $settings[$item];
			}
		}
                
		if ($this->prefix != '' && substr($this->prefix, -1) != '_')
		{
            $this->prefix .= '_';
		}
    }
    /* END */


    /** ---------------------------------------    
    /**  Forces a Reconnect On Next Query
    /** ---------------------------------------*/
    
    function reconnect()
    {    
		if (function_exists('mysql_ping'))
		{
			if (mysql_ping($this->conn_id) === FALSE)
			{
				$this->conn_id = FALSE;
			}
		}
    }
    /* END */
    
    
	/** ---------------------------------------    
    /**  Connect to database
    /** ---------------------------------------*/
    
    function db_connect($select_db = TRUE)
    {    
        $this->conn_id = ($this->conntype == 0) ?
          @mysql_connect ($this->hostname, $this->username, $this->password):
          @mysql_pconnect($this->hostname, $this->username, $this->password);
        
        if ( ! $this->conn_id)
        {            
            return FALSE;        
        }
        
        if ($select_db == TRUE)
        {
			if ( ! $this->select_db())
			{
				return FALSE;	
			}
        }
        
        $this->server_info = @mysql_get_server_info();
        
        return TRUE;
    }
    /* END */


    /** ---------------------------------------    
    /**  Select database
    /** ---------------------------------------*/

    function select_db()
    {
        if ( ! @mysql_select_db($this->database, $this->conn_id))
        {            
            return FALSE;
        }
        
        return TRUE;
	}
	/* END */


    /** ---------------------------------------    
    /**  Close database connection
    /** ---------------------------------------*/

    function db_close()
    {
        if ($this->conn_id)
            mysql_close($this->conn_id);
    }         
    /* END */
    
	/** ---------------------------------------    
    /**  Enable SQL Query Caching
    /** ---------------------------------------*/
    
    function enable_cache()
    {
    	global $PREFS;
    	
    	if ($this->enable_cache == TRUE)
        {
        	$this->cache_enabled = TRUE;
			$this->cache_dir	 = rtrim(PATH_CACHE.$this->cache_dir, '/').'/';
			
			// We limit the total number of cache files in order to
			// keep some sanity with large sites or ones that get
			// hit by overambitious crawlers.
			if ($dh = @opendir($this->cache_dir))
			{
				$i = 0;
				while (false !== (readdir($dh)))
				{
					$i++;
				}
				
			 	//$max = ( ! $PREFS->ini('max_caches') OR ! is_numeric($PREFS->ini('max_caches')) OR $PREFS->ini('max_caches') > 1000) ? 100 : $PREFS->ini('max_caches');
	
				if ($i > 150)
				{
					$this->delete_directory($this->cache_dir);
				}
			}
        }
    }
    /* END */
    

    /** ---------------------------------------    
    /**  DB Query
    /** ---------------------------------------*/
    
    function query($sql)
    { 
		if ($sql == '')
			return;

		$sql = trim($sql);
		$this->affected_rows	= 0;
		$this->insert_id 		= 0;

		// Store the query for debugging
        
        if ($this->show_queries == TRUE)
        {
        	$this->queries[] = $sql;
        }
           
        // Verify table prefix and replace if necessary.
            
        if ($this->prefix != $this->exp_prefix)
        { 
           $sql = preg_replace("/(\W)".$this->exp_prefix."(\S+?)/", "\\1".$this->prefix."\\2", $sql);

			// If the custom prefix includes 'exp_' the above can sometimes cause partial doubling.
			// This is a quick fix to prevent this from causing errors in 1.x.
			if (strncmp($this->prefix, 'exp_', 4) == 0)
			{
				$sql = str_replace($this->prefix.str_replace('exp_', '', $this->prefix), $this->prefix, $sql);
			}
        }
        
        /**
         *	The Cache Cannot be enabled until AFTER the Input class is insantiated.
         */
        if ($this->enable_cache == TRUE && $this->cache_enabled == FALSE && isset($GLOBALS['IN']) && is_object($GLOBALS['IN']))
        {
        	$this->enable_cache();
        }
                        
        if ($this->cache_enabled == TRUE)
        {
        	global $IN;
        
			// The URI being requested will become the name of the cache directory
					
			$this->cache_path = ($IN->URI == '') ? $this->cache_dir.md5('index').'/' : $this->cache_path = $this->cache_dir.md5($IN->URI).'/';
					
			// Convert the SQL query into a hash.  This will become the cache file name.
		
			$this->cache_file = md5($sql);
	
			// Is this query a read type?  
			// If so, return the previously cached data if it exists and bail out.
			
			if (stristr($sql, 'SELECT'))
			{
				if (FALSE !== ($cache = $this->get_cache()))
				{
					return $cache;
				}
			}
		}
        
        // Connect to the DB if we haven't done so on a previous query
        
        if ( ! $this->conn_id)    
        {        
			if ( ! $this->db_connect(0))
			{
				exit("Database Error:  Unable to connect to your database. Your database appears to be turned off or the database connection settings in your config file are not correct. Please contact your hosting provider if the problem persists.");
			}
			
			if ( ! $this->select_db())
			{
				exit("Database Error:  Unable to select your database");
			}
        }

        // Execute the query
                
        if ( ! $this->query_id = mysql_query($sql, $this->conn_id))
        {
            if ($this->debug)
            {
                return $this->db_error("MySQL ERROR:", $this->conn_id, $sql);
            }
          
			return FALSE;
        }

        // Increment the query counter
        
        $this->q_count++;

        // Determine if the query is one of the 'write' types. If so, gather the
        // affected rows and insert ID, and delete the existing cache file.

        $qtypes = array('INSERT', 'UPDATE', 'DELETE', 'CREATE', 'ALTER', 'DROP', 'REPLACE', 'GRANT', 'REVOKE', 'LOCK', 'UNLOCK', 'TRUNCATE');
		
		if (preg_match("#^(".implode('|', $qtypes).")#i", $sql, $qmatches))
		{
			$type = $qmatches[1];
			
			$this->affected_rows = mysql_affected_rows($this->conn_id);
			
			if ($type == 'INSERT' || $type == 'REPLACE')
			{
				$this->insert_id = mysql_insert_id($this->conn_id);
			}
			
			// Delete the cache file since the data in it is no longer current.
			
			if ($this->cache_enabled == TRUE)
			{
				$this->delete_cache();
			}

			// Bail out.  We are done
			if ($type == 'INSERT' OR $type == 'UPDATE' OR $type == 'DELETE')
			{
				return ($this->affected_rows == 0 AND $this->insert_id == 0) ? FALSE : TRUE;     
			}
			else
			{
				return TRUE;
			}
        }
        
        // Fetch the field names, but only if explicitly requested
        // We use this in our SQL utilities functions
        
        if ($this->fetch_fields == TRUE)
        { 
            $this->field_names = array();
            
            while ($field = mysql_fetch_field($this->query_id))
            {
                $this->field_names[] = $field->name;       
            }
         }

        // Fetch the result of the query and assign it to an array.
        // I know, the result *is* an array.  But we want our own
        // numerically indexed array so we can cache it.

        $i = 0;
        $result = array();
        while ($row = mysql_fetch_array($this->query_id, MYSQL_ASSOC)) 
        {                                    
            $result[$i] = $row;
            $i++;
        }
        
        // Free the result.  Optional with MySQL, but might as well be thorough
        
        mysql_free_result($this->query_id);

        // Instantiate the cache super-class and assign the data 
        // to it if a subsequent query hasn't already done so
        
		$DBC = new DB_Cache;
		$DBC->result   = $result;
		$DBC->row      = (isset($result['0'])) ? $result['0'] : array();
		$DBC->num_rows = $i;
	
        // Serialize the class and store it in a cache file
        
        if ($this->cache_enabled == TRUE)
        {
            $this->store_cache(serialize($DBC));
        }
            
        // Assign the query count to the super-class.  
        // The query count only applies to non-cached queries,
        // so we add it after the class has already been cached.
        
        $DBC->q_count = $this->q_count;
        $DBC->fields  = $this->field_names;
        
        // Return it    
        return $DBC;        
    }
    /* END */


    /** ---------------------------------------    
    /**  Fetch SQL tables
    /** ---------------------------------------*/

    function fetch_tables()
    {      
    	if (sizeof($this->tables_list) > 0)
    	{	
    		return $this->tables_list;	
    	}
    
        if ( ! $this->conn_id)    
        {
			if ( ! $this->db_connect(0))
			{
				exit("Database Error:  Unable to connect to your database. Your database appears to be turned off or the database connection settings in your config file are not correct. Please contact your hosting provider if the problem persists.");
			}
			
			if ( ! $this->select_db())
			{
				exit("Database Error:  Unable to select your database");
			}
        }
        
        // mysql_list_tables() was depreciated, so we switched to using
        // this query, which should work. -Paul
        
		// We use $this->prefix as query() will not match the like escaped exp_prefix.
        $query = $this->query("SHOW TABLES FROM `{$this->database}` LIKE '".$this->escape_like_str($this->prefix)."%'"); 
        
        if ($query->num_rows > 0)
        {
        	foreach($query->result as $row)
        	{
        		$this->tables_list[]  = array_shift($row);
        	}
        }
        
        return $this->tables_list;
    }
    /* END */
    
    
    /** ---------------------------------------    
    /**  Determine if a table exists
    /** ---------------------------------------*/

    function table_exists($table_name)
    {
		if ($this->prefix != $this->exp_prefix)
        { 
			$table_name = preg_replace("/".$this->exp_prefix."(\S+?)/", $this->prefix."\\1", $table_name);
        }
    
		if ( ! in_array($table_name, $this->fetch_tables()))
		{
			return FALSE;
		}
		
		return TRUE;
	}
    /* END */


    /** ---------------------------------------    
    /**  Cache a query
    /** ---------------------------------------*/

    function store_cache($object)
    {
        $dirs = array(PATH_CACHE.'db_cache', substr($this->cache_path, 0, -1));
        
        foreach ($dirs as $dir)
        {       
			if ( ! @is_dir($dir))
			{
				if ( ! @mkdir($dir, 0777))
				{
					return;
				}
				
				if ($dir == PATH_CACHE.'db_cache' && $fp = @fopen($dir.'/index.html', 'wb'))
				{
					fclose($fp);					
				}
				
				@chmod($dir, 0777);            
			}
        }
	      
        if ( ! $fp = @fopen($this->cache_path.$this->cache_file, 'wb'))
            return;

        flock($fp, LOCK_EX);
        fwrite($fp, $object);
        flock($fp, LOCK_UN);
        fclose($fp);
        
		@chmod($this->cache_path.$this->cache_file, 0777);            
    }
    /* END */


    /** ---------------------------------------    
    /**  Retreive a cached query
    /** ---------------------------------------*/

    function get_cache()
    {            
        if ( ! @is_dir($this->cache_path))
            return false;    
        
        if ( ! file_exists($this->cache_path.$this->cache_file))
            return false;
        
        if ( ! $fp = @fopen($this->cache_path.$this->cache_file, 'rb'))
            return false;

        flock($fp, LOCK_SH);
        
        $cachedata = @fread($fp, filesize($this->cache_path.$this->cache_file));
        
        flock($fp, LOCK_UN);
        fclose($fp);
        
        if ( ! is_string($cachedata)) return FALSE;
        
		return unserialize($cachedata);            
    }
    /* END */
    

    /** ---------------------------------------    
    /**  Delete cache files
    /** ---------------------------------------*/

    function delete_cache()
    {    
        if ( ! @is_dir($this->cache_path))
            return FALSE;
    
        if ( ! $fp = @opendir($this->cache_path)) 
        { 
			return FALSE;
        } 
        
        while (false !== ($file = @readdir($fp))) 
        {
             if ($file != "."  AND  $file != "..")
             {
                if ( ! @unlink($this->cache_path.$file))
                {
					return FALSE;
                }
            }
        }
        
		if ( ! @rmdir($this->cache_path))
		{
			return FALSE;
		}
                
        closedir($fp); 
    }
    /* END */


    /** -----------------------------------------
    /**  Delete Direcories
    /** -----------------------------------------*/

    function delete_directory($path, $del_root = FALSE)
    {
        if ( ! $current_dir = @opendir($path))
        {
        	return;
        }
        
        while($filename = @readdir($current_dir))
        {        
            if (@is_dir($path.'/'.$filename) and ($filename != "." and $filename != ".."))
            {
                $this->delete_directory($path.'/'.$filename, TRUE);
            }
            elseif($filename != "." and $filename != "..")
            {
                @unlink($path.'/'.$filename);
            }
        }
        
        closedir($current_dir);
        
        if ($del_root == TRUE)
        {
            @rmdir($path);
        }
    }
    /* END */

    /** ---------------------------------------    
    /**  MySQL escape string
    /** ---------------------------------------*/

    function escape_str($str, $like = FALSE)    
    {    
    	if (is_array($str))
    	{
    		foreach($str as $key => $val)
    		{
    			$str[$key] = $this->escape_str($val, $like);
    		}
    		
    		return $str;
    	}

		if (function_exists('mysql_real_escape_string') AND is_resource($this->conn_id))
		{
			$str =  mysql_real_escape_string(stripslashes($str), $this->conn_id);
		}
		elseif (function_exists('mysql_escape_string'))
    	{
			$str = mysql_escape_string(stripslashes($str));
		}
		else
		{
        	$str = addslashes(stripslashes($str));
    	}
    	
    	if ($like === TRUE)
    	{
    		$replace_characters = array('%', '_');
			$escaped_characters = array('\\%', '\\_');
			
			$str = str_replace($replace_characters, $escaped_characters, $str);
    	}
    	
    	return $str;
    }
    /* END */
    
    /** ---------------------------------------    
    /**  MySQL escape plus LIKE wildcards
    /** ---------------------------------------*/

    function escape_like_str($str)    
    {    
    	return $this->escape_str($str, TRUE);
	}    

    /** ---------------------------------------    
    /**  Error Message
    /** ---------------------------------------*/
    
    function db_error($msg, $id="", $sql="") 
    {    
        if ($this->error_header != '')
        {
        	$msg = $this->error_header.$msg;
        }    
    
        if ($id) 
        { 
            $msg .= "<br /><br />";
            $msg .= "Error Number: " . mysql_errno($id);
            $msg .= "<br /><br />";
            $msg .= "Description: "  . mysql_error($id);
        }
        
        if ($sql)
            $msg .= "<br /><br />Query: ".$sql;
         
        if ($this->error_footer != '')
        {
        	$msg .= $this->error_footer;
        }
        
        exit($msg);
    }    
  
  
    /** ---------------------------------------    
    /**  Write an INSERT string
    /** ---------------------------------------*/

    // This function simplifies the process of writing database inserts.  
    // It returns a correctly formatted SQL insert string.
    //
    // Example:
    //
    //  $data = array('name' => $name, 'email' => $email, 'url' => $url);
    //
    //  $str = $DB->insert_string('exp_weblog', $data);
    //
    //  Produces:  INSERT INTO exp_weblog (name, email, url) VALUES ('Joe', 'joe@joe.com', 'www.joe.com')

    function insert_string($table, $data, $addslashes = FALSE)
    {
        $fields = '';      
        $values = '';
        
        if (stristr($table, '.'))
        {
        	$x = explode('.', $table, 3);
        	$table = $x['0'].'`.`'.$x['1'];
        }
        
        foreach($data as $key => $val) 
        {
            $fields .= '`' . $key . '`, ';
            $val = ($addslashes === TRUE) ? addslashes($val) : $val;
            $values .= "'".$this->escape_str($val)."'".', ';
        }
        
        $fields = preg_replace( "/, $/" , "" , $fields);
        $values = preg_replace( "/, $/" , "" , $values);

        return 'INSERT INTO `'.$table.'` ('.$fields.') VALUES ('.$values.')';
    }    
    /* END */


    /** ---------------------------------------    
    /**  Write an UPDATE string
    /** ---------------------------------------*/

    // This function simplifies the process of writing database updates.  
    // It returns a correctly formatted SQL update string.
    //
    // Example:
    //
    //  $data = array('name' => $name, 'email' => $email, 'url' => $url);
    //
    //  $str = $DB->update_string('exp_weblog', $data, "author_id = '1'");
	//
    //  Produces:  UPDATE exp_weblog SET name = 'Joe', email = 'joe@joe.com', url = 'www.joe.com' WHERE author_id = '1'

    function update_string($table, $data, $where)
    {
        if ($where == '')
            return false;
    
        $str  = '';
        $dest = '';
        
        if (stristr($table, '.'))
        {
        	$x = explode('.', $table, 3);
        	$table = $x['0'].'`.`'.$x['1'];
        }
        
        foreach($data as $key => $val) 
        {
            $str .= '`'.$key."` = '".$this->escape_str($val)."', ";
        }

        $str = preg_replace( "/, $/" , "" , $str);
        
        if (is_array($where))
        {
            foreach ($where as $key => $val)
            {
                $dest .= $key." = '".$this->escape_str($val)."' AND ";
            }
            
            $dest = preg_replace( "/AND $/" , "" , $dest);
        }
        else
            $dest = $where;

        return 'UPDATE `'.$table.'` SET '.$str.' WHERE '.$dest;        
    }    
    /* END */


}
// END CLASS
?>
Back to Top