Project

General

Profile

1
<?php
2
/**
3
 *
4
 * @category        framework
5
 * @package         database
6
 * @author          WebsiteBaker Project
7
 * @copyright       2004-2009, Ryan Djurovich
8
 * @copyright       2009-2011, Website Baker Org. e.V.
9
 * @link            http://www.websitebaker2.org/
10
 * @license         http://www.gnu.org/licenses/gpl.html
11
 * @platform        WebsiteBaker 2.8.x
12
 * @requirements    PHP 5.2.2 and higher
13
 * @version         $Id: class.database.php 1662 2012-04-17 01:00:02Z darkviper $
14
 * @filesource      $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.database.php $
15
 * @lastmodified    $Date: 2012-04-17 03:00:02 +0200 (Tue, 17 Apr 2012) $
16
 *
17
 */
18
/*
19
Database class
20
This class will be used to interface between the database
21
and the Website Baker code
22
*/
23
/* -------------------------------------------------------- */
24
// Must include code to stop this file being accessed directly
25
if(!defined('WB_PATH')) {
26
	require_once(dirname(__FILE__).'/globalExceptionHandler.php');
27
	throw new IllegalFileException();
28
}
29
/* -------------------------------------------------------- */
30
if(!defined('DB_URL')) {
31
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
32
}
33

    
34
define('DATABASE_CLASS_LOADED', true);
35

    
36
class database {
37

    
38
	private $db_handle  = null; // readonly from outside
39
	private $db_name    = '';
40
	private $connected  = false;
41

    
42
	private $error      = '';
43
	private $error_type = '';
44
	private $message    = array();
45
	private $iQueryCount= 0;
46

    
47

    
48
	// Set DB_URL
49
	function database($url = '') {
50
		// Connect to database
51
		$this->connect();
52
		// Check for database connection error
53
		if($this->is_error()) {
54
			die($this->get_error());
55
		}
56
	}
57
	
58
	// Connect to the database
59
	function connect() {
60
		$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
61
		if(mysql_error()) {
62
			$this->connected = false;
63
			$this->error = mysql_error();
64
		} else {
65
			if(!mysql_select_db(DB_NAME)) {
66
				$this->connected = false;
67
				$this->error = mysql_error();
68
			} else {
69
				$this->db_name = DB_NAME;
70
				$this->connected = true;
71
			}
72
		}
73
		return $this->connected;
74
	}
75
	
76
	// Disconnect from the database
77
	function disconnect() {
78
		if($this->connected==true) {
79
			mysql_close();
80
			return true;
81
		} else {
82
			return false;
83
		}
84
	}
85
	
86
	// Run a query
87
	function query($statement) {
88
		$this->iQueryCount++;
89
		$mysql = new mysql();
90
		$mysql->query($statement);
91
		$this->set_error($mysql->error());
92
		if($mysql->error()) {
93
			return null;
94
		} else {
95
			return $mysql;
96
		}
97
	}
98

    
99
	// Gets the first column of the first row
100
	function get_one( $statement )
101
	{
102
		$this->iQueryCount++;
103
		$fetch_row = mysql_fetch_array(mysql_query($statement) );
104
		$result = $fetch_row[0];
105
		$this->set_error(mysql_error());
106
		if(mysql_error()) {
107
			return null;
108
		} else {
109
			return $result;
110
		}
111
	}
112
	
113
	// Set the DB error
114
	function set_error($message = null) {
115
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
116
		$this->error = $message;
117
		if(strpos($message, 'no such table')) {
118
			$this->error_type = $TABLE_DOES_NOT_EXIST;
119
		} else {
120
			$this->error_type = $TABLE_UNKNOWN;
121
		}
122
	}
123
	
124
	// Return true if there was an error
125
	function is_error() {
126
		return (!empty($this->error)) ? true : false;
127
	}
128
	
129
	// Return the error
130
	function get_error() {
131
		return $this->error;
132
	}
133

    
134
/**
135
 * default Getter for some properties
136
 * @param string $sPropertyName
137
 * @return mixed NULL on error or missing property
138
 */
139
	public function __get($sPropertyName)
140
	{
141
		switch ($sPropertyName):
142
			case 'db_handle':
143
			case 'DbHandle':
144
			case 'getDbHandle':
145
				$retval = $this->db_handle;
146
				break;
147
			case 'db_name':
148
			case 'DbName':
149
			case 'getDbName':
150
				$retval = $this->db_name;
151
				break;
152
			case 'getQueryCount':
153
				$retval = $this->iQueryCount;
154
				break;
155
			default:
156
				$retval = null;
157
				break;
158
		endswitch;
159
		return $retval;
160
	} // __get()
161

    
162
/*
163
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
164
 * @param string $field_name: name of the field to seek for
165
 * @return bool: true if field exists
166
 */
167
	public function field_exists($table_name, $field_name)
168
	{
169
		$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
170
		$query = $this->query($sql);
171
		return ($query->numRows() != 0);
172
	}
173

    
174
/*
175
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
176
 * @param string $index_name: name of the index to seek for
177
 * @return bool: true if field exists
178
 */
179
	public function index_exists($table_name, $index_name, $number_fields = 0)
180
	{
181
		$number_fields = intval($number_fields);
182
		$keys = 0;
183
		$sql = 'SHOW INDEX FROM `'.$table_name.'`';
184
		if( ($res_keys = $this->query($sql)) )
185
		{
186
			while(($rec_key = $res_keys->fetchRow()))
187
			{
188
				if( $rec_key['Key_name'] == $index_name )
189
				{
190
					$keys++;
191
				}
192
			}
193

    
194
		}
195
		if( $number_fields == 0 )
196
		{
197
			return ($keys != $number_fields);
198
		}else
199
		{
200
			return ($keys == $number_fields);
201
		}
202
	}
203
/*
204
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
205
 * @param string $field_name: name of the field to add
206
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
207
 * @return bool: true if successful, otherwise false and error will be set
208
 */
209
	public function field_add($table_name, $field_name, $description)
210
	{
211
		if( !$this->field_exists($table_name, $field_name) )
212
		{ // add new field into a table
213
			$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
214
			$query = $this->query($sql);
215
			$this->set_error(mysql_error());
216
			if( !$this->is_error() )
217
			{
218
				return ( $this->field_exists($table_name, $field_name) ) ? true : false;
219
			}
220
		}else
221
		{
222
			$this->set_error('field \''.$field_name.'\' already exists');
223
		}
224
		return false;
225
	}
226

    
227
/*
228
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
229
 * @param string $field_name: name of the field to add
230
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
231
 * @return bool: true if successful, otherwise false and error will be set
232
 */
233
	public function field_modify($table_name, $field_name, $description)
234
	{
235
		$retval = false;
236
		if( $this->field_exists($table_name, $field_name) )
237
		{ // modify a existing field in a table
238
			$sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
239
			$retval = ( $this->query($sql) ? true : false);
240
			$this->set_error(mysql_error());
241
		}
242
		return $retval;
243
	}
244

    
245
/*
246
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
247
 * @param string $field_name: name of the field to remove
248
 * @return bool: true if successful, otherwise false and error will be set
249
 */
250
	public function field_remove($table_name, $field_name)
251
	{
252
		$retval = false;
253
		if( $this->field_exists($table_name, $field_name) )
254
		{ // modify a existing field in a table
255
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
256
			$retval = ( $this->query($sql) ? true : false );
257
		}
258
		return $retval;
259
	}
260

    
261
/*
262
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
263
 * @param string $index_name: name of the new index
264
 * @param string $field_list: comma seperated list of fields for this index
265
 * @param string $index_type: kind of index (UNIQUE, PRIMARY, '')
266
 * @return bool: true if successful, otherwise false and error will be set
267
 */
268
	public function index_add($table_name, $index_name, $field_list, $index_type = '')
269
	{
270
		$retval = false;
271
		$field_list = str_replace(' ', '', $field_list);
272
		$field_list = explode(',', $field_list);
273
		$number_fields = sizeof($field_list);
274
		$field_list = '`'.implode('`,`', $field_list).'`';
275
		if( $this->index_exists($table_name, $index_name, $number_fields) ||
276
		    $this->index_exists($table_name, $index_name))
277
		{
278
			$sql  = 'ALTER TABLE `'.$table_name.'` ';
279
			$sql .= 'DROP INDEX `'.$index_name.'`';
280
			if( $this->query($sql))
281
			{
282
				$sql  = 'ALTER TABLE `'.$table_name.'` ';
283
				$sql .= 'ADD '.$index_type.' `'.$index_name.'` ( '.$field_list.' ); ';
284
				if( $this->query($sql)) { $retval = true; }
285
			}
286
		}
287
		return $retval;
288
	}
289

    
290
/*
291
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
292
 * @param string $field_name: name of the field to remove
293
 * @return bool: true if successful, otherwise false and error will be set
294
 */
295
	public function index_remove($table_name, $index_name)
296
	{
297
		$retval = false;
298
		if( $this->index_exists($table_name, $index_name) )
299
		{ // modify a existing field in a table
300
			$sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
301
			$retval = ( $this->query($sql) ? true : false );
302
		}
303
		return $retval;
304
	}
305
/**
306
 * Import a standard *.sql dump file
307
 * @param string $sSqlDump link to the sql-dumpfile
308
 * @param string $sTablePrefix
309
 * @param bool $bPreserve set to true will ignore all DROP TABLE statements
310
 * @param string $sTblEngine
311
 * @param string $sTblCollation
312
 * @return boolean true if import successful
313
 */
314
	public function SqlImport($sSqlDump,
315
	                          $sTablePrefix = '',
316
	                          $bPreserve = true,
317
	                          $sTblEngine = 'ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci',
318
	                          $sTblCollation = ' collate utf8_unicode_ci')
319
	{
320
		$retval = true;
321
		$this->error = '';
322
		$aSearch  = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
323
		$aReplace = array($sTablePrefix, $sTblEngine, $sTblCollation);
324
		$sql = '';
325
		$aSql = file($sSqlDump);
326
		while ( sizeof($aSql) > 0 ) {
327
			$sSqlLine = trim(array_shift($aSql));
328
			if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
329
				$sql = $sql.' '.$sSqlLine;
330
				if ((substr($sql,-1,1) == ';')) {
331
					$sql = trim(str_replace( $aSearch, $aReplace, $sql));
332
					if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
333
						if(!mysql_query($sql, $this->db_handle)) {
334
							$retval = false;
335
							$this->error = mysql_error($this->db_handle);
336
							unset($aSql);
337
							break;
338
						}
339
					}
340
					$sql = '';
341
				}
342
			}
343
		}
344
		return $retval;
345
	}
346

    
347
/**
348
 * retuns the type of the engine used for requested table
349
 * @param string $table name of the table, including prefix
350
 * @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
351
 */
352
	public function getTableEngine($table)
353
	{
354
		$retVal = false;
355
		$mysqlVersion = mysql_get_server_info($this->db_handle);
356
		$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
357
		$sql = "SHOW TABLE STATUS FROM " . $this->db_name . " LIKE '" . $table . "'";
358
		if(($result = $this->query($sql))) {
359
			if(($row = $result->fetchRow(MYSQL_ASSOC))) {
360
				$retVal = $row[$engineValue];
361
			}
362
		}
363
		return $retVal;
364
	}
365

    
366

    
367
} /// end of class database
368

    
369
define('MYSQL_SEEK_FIRST', 0);
370
define('MYSQL_SEEK_LAST', -1);
371

    
372
class mysql {
373

    
374
	// Run a query
375
	function query($statement) {
376
		$this->result = mysql_query($statement);
377
		$this->error = mysql_error();
378
		return $this->result;
379
	}
380
	
381
	// Fetch num rows
382
	function numRows() {
383
		return mysql_num_rows($this->result);
384
	}
385

    
386
	// Fetch row  $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
387
	function fetchRow($typ = MYSQL_BOTH) {
388
		return mysql_fetch_array($this->result, $typ);
389
	}
390

    
391
	function rewind()
392
	{
393
		return $this->seekRow();
394
	}
395

    
396
	function seekRow( $position = MYSQL_SEEK_FIRST )
397
	{
398
		$pmax = $this->numRows() - 1;
399
		$p = (($position < 0 || $position > $pmax) ? $pmax : $position);
400
		return mysql_data_seek($this->result, $p);
401
	}
402

    
403
	// Get error
404
	function error() {
405
		if(isset($this->error)) {
406
			return $this->error;
407
		} else {
408
			return null;
409
		}
410
	}
411

    
412
}
413
/* this function is placed inside this file temporarely until a better place is found */
414
/*  function to update a var/value-pair(s) in table ****************************
415
 *  nonexisting keys are inserted
416
 *  @param string $table: name of table to use (without prefix)
417
 *  @param mixed $key:    a array of key->value pairs to update
418
 *                        or a string with name of the key to update
419
 *  @param string $value: a sting with needed value, if $key is a string too
420
 *  @return bool:  true if any keys are updated, otherwise false
421
 */
422
	function db_update_key_value($table, $key, $value = '')
423
	{
424
		global $database;
425
		if( !is_array($key))
426
		{
427
			if( trim($key) != '' )
428
			{
429
				$key = array( trim($key) => trim($value) );
430
			} else {
431
				$key = array();
432
			}
433
		}
434
		$retval = true;
435
		foreach( $key as $index=>$val)
436
		{
437
			$index = strtolower($index);
438
			$sql = 'SELECT COUNT(`setting_id`) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' ';
439
			if($database->get_one($sql))
440
			{
441
				$sql = 'UPDATE ';
442
				$sql_where = 'WHERE `name` = \''.$index.'\'';
443
			}else {
444
				$sql = 'INSERT INTO ';
445
				$sql_where = '';
446
			}
447
			$sql .= '`'.TABLE_PREFIX.$table.'` ';
448
			$sql .= 'SET `name` = \''.$index.'\', ';
449
			$sql .= '`value` = \''.$val.'\' '.$sql_where;
450
			if( !$database->query($sql) )
451
			{
452
				$retval = false;
453
			}
454
		}
455
		return $retval;
456
	}
(8-8/22)