annamaria-daneswood-25102012 /administrator/components/com_virtuemart/classes/ps_database.php

Language PHP Lines 448
MD5 Hash 5c0a2269786d063828da85a07b9eb79f Estimated Cost $6,514 (why?)
Repository https://bitbucket.org/dgough/annamaria-daneswood-25102012.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
<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id: ps_database.php 1397 2008-05-10 03:34:46Z gregdev $
* @package VirtueMart
* @subpackage classes
* @copyright Copyright (C) 2004-2007 soeren - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/

/***********************************************************************
Wrapper Class for the $database - Object
************************************************************************/

class ps_DB {

	/** @var int   Current row in query result set */
	var $row = 0;
	/** @var stdclass	Current row record data */
	var $record = null;
	/** @var string  Error Message */
	var $error = "";
	/** @var int  Error Number */
	var $errno = "";
	/** @var string   The current sql Query    */
	var $_sql = "";
	/** @var boolean Flag to see if a query has been renewed between two query calls */
	var $_query_set= false;
	/** @var boolean   true if next_record has already been called   */
	var $called = false;
	/** @var database The core database object */
	var $_database = null;
	
	function ps_DB() {
		if( is_callable(array('jfactory', 'getdbo'))) {
			$this->_database =& jfactory::getDBO();
		} else {
			$this->_database =& $GLOBALS['database'];
		}
	}
	/**
     * Clone an object
     *
     * @param mixed $obj
     * @return mixed copy of $obj
     */
	function _clone( $obj ) {
		return $obj;
	}

	/**
    * Sets the SQL query string for later execution.
    *
    * This function replaces a string identifier <var>$prefix</var> with the
    * string held is the <var>_table_prefix</var> class variable.
    *
    * @param string The SQL query
    */
	function setQuery( $sql ) {
		
		$vm_prefix = "{vm}";
		$sql = trim( $sql );
		$this->_sql = trim(str_replace( $vm_prefix, VM_TABLEPREFIX, $sql ));
		$this->_database->setQuery( $this->_sql );
		
		$this->_query_set = true;
		
		if( defined('DEBUG') && DEBUG == '1' ) {
			// Register Double-run (multiple-run) queries 
			if( !isset($GLOBALS['queries']))$GLOBALS['queries'] = array();
			if( !isset($GLOBALS['double_queries']))$GLOBALS['double_queries'] = array();
			if( !in_array($this->_database->_sql, $GLOBALS['queries'] ) ) {
				$GLOBALS['queries'][] = $this->_database->_sql;
			} else {
				$GLOBALS['double_queries'][] = $this->_database->_sql;
			}
		}
	}

	/**
	* Runs query and sets up the query id for the class.
	*
	* @param string The SQL query
	*/
	function query( $q='' ) {
		global $mosConfig_dbprefix, $mosConfig_debug, $vmLogger;
		$prefix = "#__";
		$vm_prefix = "{vm}";

		if (empty($q) ) {
			if( empty($this->_sql)) {
				$vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'" called without a pending query.');
			}
			elseif( !$this->_query_set ) {
				$vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'": A query was run twice without having changed the SQL text.');
			}
		}
		else {
			$this->setQuery( $q );
		}

		$this->row = 0;
		$this->called = false;
		$this->record = null;
		$this->record = Array(0);

		if (strtoupper(substr( $this->_sql , 0, 6 )) == "SELECT" 
			|| strtoupper(substr( $this->_sql , 0, 4 ))=='SHOW' 
			|| strtoupper(substr( $this->_sql , 0, 7 ))=='EXPLAIN' 
			|| strtoupper(substr( $this->_sql , 0, 8 ))=='DESCRIBE' 
			) {
			$this->record = $this->_database->loadObjectList();

			if( $this->record === false ) {
				$result = false;
			}
		}
		else {
			$result = $this->_database->query();
		}

		$this->_query_set = false;
		
		if( isset( $result )) {
			return $result;
		}
	}

	/**
	 * Returns the next row in the RecordSet for the last query run.  
	 *
	 * @return boolean False if RecordSet is empty or the pointer is at the end.
	 */
	function next_record() {
		global $vmLogger;
		if ( empty( $this->_sql ) ) {
			$vmLogger->debug( '"'.__CLASS__.'::'.__FUNCTION__.'()" called with no query pending.' );
			return false;
		}
		if ( $this->called ) {
			$this->row++;
		}
		else {
			$this->called = true;
		}

		if ($this->row < sizeof( $this->record ) ) {
			return true;
		}
		else {
			$this->row--;
			return false;
		}
	}

	function nextRow() {
		return isset( $this->record[$this->row + 1] ) ? $this->record[$this->row + 1] : false;
	}
	function previousRow() {
		return isset( $this->record[$this->row - 1] ) ? $this->record[$this->row - 1] : false;
	}
	/**
	 * Returns the value of the given field name for the current
	 * record in the RecordSet. 
	 * f == fetch
	 * @param string  The field name
	 * @param boolean Strip slashes from the data?
	 * @return string the value of the field $field_name in the recent row of the record set
	 */
	function f($field_name, $stripslashes=true) {
		if (isset($this->record[$this->row]->$field_name)) {

			if($stripslashes) {
				return( stripslashes( $this->record[$this->row]->$field_name ) );
			}
			else {
				return( $this->record[$this->row]->$field_name );
			}
		}
	}

	/**
	 * Returns the value of the field name from the $vars variable
	 * if it is set, otherwise returns the value of the current
	 * record in the RecordSet.  Useful for handling forms that have
	 * been submitted with errors.  This way, fields retain the values 
	 * sent in the $vars variable (user input) instead of the database values.
	 * sf == selective fetch
	 * @param string  The field name
	 * @param boolean Strip slashes from the data?
	 * @return string the value of the field $field_name in the recent row of the record set
	 */
	function sf($field_name, $stripslashes=true) {
		global $vars, $default;

		if ((defined( '_VM_LOG_ERRORS' ) || isset($vars["error"])) && !empty($vars["$field_name"])) {
			if($stripslashes) {
				return  stripslashes($vars[$field_name] );
			}
			else {
				return( $vars[$field_name] );
			}
		}
		elseif (isset($this->record[$this->row]->$field_name)) {
			if($stripslashes) {
				return  stripslashes($this->record[$this->row]->$field_name );
			}
			else {
				return( $this->record[$this->row]->$field_name );
			}
		}
		elseif (isset($default[$field_name])) {
			if($stripslashes) {
				return  stripslashes($default[$field_name]);
			}
			else {
				return( $default[$field_name] );
			}
		}
	}

	/**
	* Prints the value of the given field name for the current
	* record in the RecordSet.
	* p == print
	* @param string  The field name
	* @param boolean Strip slashes from the data?
	*/
	function p($field_name, $stripslashes=true) {
		echo $this->f( $field_name, $stripslashes );
	}

	/**
  	* Prints the value of the field name from the $vars variable
  	* if it is set, otherwise prints the value of the current
  	* record in the RecordSet.  Useful for handling forms that have
  	* been submitted with errors.  This way, fields retain the values 
  	* sent in the $vars variable (user input) instead of the database
  	* values.
  	* sp == selective print
  	* @param string  The field name
  	* @param boolean Strip slashes from the data?
  	*/
	function sp($field_name, $stripslashes=true) {
		echo $this->sf( $field_name, $stripslashes);
	}
	/**
	 * Returns the object of the current row in the rowset
	 *
	 * @return mixed
	 */
	function get_row() {
		return $this->record[$this->row];
	}
	
	/**
	 * Returns the number of rows in the RecordSet from a query.
	 * @return int
	 */
	function num_rows() {
		return sizeof( $this->record );
	}

	/**
	 * Returns the ID of the last AUTO_INCREMENT INSERT.
	 *
	 * @return int
	 */
	function last_insert_id() {
		return $this->_database->insertid();
	}

	/**
	 * returns true when the actual row is the last record in the record set
	 * otherwise returns false
	 *
	 * @return boolean
	 */
	function is_last_record() {
		return ($this->row+1 >= $this->num_rows());
	}

	/**
	 * Set the "next_record" pointer back to the first row.
	 *
	 */
	function reset() {

		$this->row = 0;
		$this->called = false;

	}
	/**
	 * Returns the current row of the recordset
	 * @since VirtueMart 1.1.0
	 * @return stdClass Object
	 */
	function getCurrentRow() {
		return $this->record[$this->row];
	}
	
	/**
	 * Query Builder Functions
	 * @author soeren
	 * @since VirtueMart 1.1.0
	 * 
	 * @param string $type Either INSERT or UPDATE
	 * @param string $table Example: #__{vm}_user_info
	 * @param array $values Array of the format array( FieldName => Value ), Example: array( 'user_info_id' => md5( $hash ) )
	 * @param string $whereClause
	 *
	 */
	function buildQuery( $type='INSERT', $table, $values, $whereClause='', $doNotEnclose=array() ) {
		global $vmLogger;
		
		if( empty($table) || empty($values)) {
			return;
		}
		$table = trim( $table );
		$type = trim( $type );
		$type = strtoupper($type);
		
		switch( $type ) {
			
			case 'INSERT':
			case 'REPLACE':
				
				$q = "$type INTO `$table` (`";
				$q .= implode( "`,\n`", array_keys($values) );
				
				$q .= "`) VALUES (\n";
				$count = count( $values );
				$i = 1;
				foreach ( $values as $key => $value ) {
					if( in_array( $key, $doNotEnclose )) {
						// Important when using MySQL functions like "AES_ENCRYPT", "ENCODE", "REPLACE" or such
						$q .= $value;
					} 
					else {
						$q .= '\'' . $this->getEscaped($value)."'\n";
					}
					if( $i++ < $count ) {
						$q.= ',';
					}
				}
				$q .= ')';
				break;
				
			case 'UPDATE':
				
				$q = "UPDATE `$table` SET ";
				$count = count( $values );
				$i = 1;
				foreach ( $values as $key => $value ) {
					if( in_array( $key, $doNotEnclose )) {
						// Important when using MySQL functions like "AES_ENCRYPT", "ENCODE", "REPLACE" or such
						$q .= "`$key` = ".$value;
					} 
					else {
						//$q .= '\'' . $this->getEscaped($value)."'\n";
						$q .= "`$key` = '" . $this->getEscaped($value)."'";
					}
					
					if( $i++ < $count ) {
						$q.= ",\n";
					}
				}
				$q .= "\n$whereClause";
				
				break;
				
				
			default:
				$vmLogger->debug( 'Function '.__FUNCTION__.' can\'t build a query of the type "'.$type.'"' );
				return;
		}
		
		$this->setQuery( $q );

	}

	/**
	 * @param array A list of valid (and safe!) table names
	 * @return array An array of fields by table
	 */
	function getTableFields( $tables ) {
		$result = array();

		foreach ($tables as $tblval) {
			$this->setQuery( 'SHOW FIELDS FROM ' . $tblval );
			$fields = $this->loadObjectList();
			foreach ($fields as $field) {
				$result[$tblval][$field->Field] = preg_replace("/[(0-9)]/",'', $field->Type );
			}
		}

		return $result;
	}
	
	///////////////////////////////
	// Parental Database functions
	// We must overwrite them because
	// we still use a global database
	// object, not a ps_DB object
	///////////////////////////////
	function loadResult() {
		return $this->_database->loadResult();
	}
	function loadResultArray($numinarray = 0) {
		return $this->_database->loadResultArray( $numinarray );
	}
	function loadAssocList( $key='' ) {
		return $this->_database->loadAssocList( $key );
	}
	function loadObject( &$object ) {
		return $this->_database->loadObject($object);
	}
	function loadObjectList( $key='' ) {
		return $this->_database->loadObjectList( $key );
	}
	function loadRow() {
		return $this->_database->loadRow();
	}
	function loadRowList( $key='' ) {
		return $this->_database->loadRowList($key);
	}
	function getErrorMsg() {
		return $this->_database->getErrorMsg();
	}
	function getErrorNum() {
		return $this->_database->getErrorNum();
	}
	function stderr() {
		return $this->_database->stderr();
	}
	function getEscaped( $text ) {
		return $this->_database->getEscaped( $text );
	}
}
?>
Back to Top