Project

General

Profile

1
<?php
2

    
3
// $Id: class.database.php 1149 2009-09-22 14:50:47Z aldus $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2008, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
/*
27

    
28
Database class
29

    
30
This class will be used to interface between the database
31
and the Website Baker code
32

    
33
*/
34

    
35
/**
36
 *	Stop this file from being accessed directly
37
 */
38
if ( !defined('WB_URL') ) die (header('location: ../index.php') );
39

    
40

    
41
/**
42
 *	Modifications on this file made by
43
 *	- Dietrich Roland Pehlke (aldus)
44
 *		
45
 *	@version	2.2.2
46
 *	@date		2009-01-09
47
 *	@state		beta
48
 *	@require	php 5.2.x
49
 *	@package	Website Baker - framework: class.database
50
 *
51
 *	0.2.2	add "type" to mysql-fetchRow method
52
 *
53
 *	0.2.1	add new class functions "copy_content" and "fetch_query".
54
 */
55
define('DATABASE_CLASS_LOADED', true);
56

    
57
class database {
58
	
59
	public $mySQL_handle	= 0;
60
	public $db_handle		= 0;
61
	public $connected		= 0;
62
	public $status			= 0;
63
	public $log_querys		= false;
64
	public $log_filename	= "wb_querys.log";
65
	public $log_path		= "";
66
	public $last_query		= "";
67
	public $last_jobnumber	= 0;
68
	
69
	public $error_tmpl  = "
70
	<span style='font-family:Verdana, sans-serif;font-size:11px;display:block; width:400px;'>
71
	An error has occured:<br />
72
	Job: <b><font color='#990000'>{job}</font></b><br />
73
	Message: <i><font color='#990000'>{message}</font></i><br />
74
	</span>";
75
	
76
	public $error = 0;
77
	
78
	public function __construct ($url="") {
79
		$this->database($url);
80
		$this->mySQL_handle = new c_mysql();
81
	}
82
	
83
	/**
84
	 *	Set DB_URL
85
	 */
86
	public function database($url = '') {
87
		// Connect to database
88
		$this->connect();
89
		// Check for database connection error
90
		if($this->is_error()) {
91
			die($this->get_error());
92
		}
93
	}
94
	
95
	/**
96
	 *	Connect to the database
97
	 */
98
	public function connect() {
99
		$this->status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
100
		if(mysql_error()) {
101
			$this->connected = false;
102
			$this->error = mysql_error();
103
		} else {
104
			if(!mysql_select_db(DB_NAME)) {
105
				$this->connected = false;
106
				$this->error = mysql_error();
107
			} else {
108
				$this->connected = true;
109
			}
110
		}
111
		return $this->connected;
112
	}
113
	
114
	/**
115
	 *	Disconnect from the database
116
	 */
117
	public function disconnect() {
118
		if($this->connected==true) {
119
			mysql_close( $this->db_handle );
120
			return true;
121
		} else {
122
			return false;
123
		}
124
	}
125
	
126
	/**
127
	 *	Run a query
128
	 */
129
	public function query( $statement="", $aJobNumber = 0 ) {
130
		$this->last_query = $statement;
131
		$this->last_jobnumber = $aJobNumber;
132
		if (true == $this->log_querys) $this->__write_log();
133
		$this->mySQL_handle->query($statement);
134
		$this->set_error($this->mySQL_handle->error());
135
		if($this->mySQL_handle->error()) {
136
			if (true == $this->log_querys) {
137
				$this->last_query = "Error: ".$this->mySQL_handle->error();
138
				$this->__write_log();
139
			}
140
			$this->__display_error();
141
			return null;
142
		} else {
143
			return clone $this->mySQL_handle;
144
		}
145
	}
146
	
147
	/**
148
	 *
149
	 *
150
	 */
151
	public function fetch_query ($aQuery="", $aNumber=0) {
152
		$result = $this->query($aQuery, $aNumber);
153
		if (!$result) return false;
154
		
155
		if ($result->numRows() > 0) {
156
			return $result->fetchRow();
157
		} else {
158
			return false;
159
		}
160
	}
161
	
162
	/**
163
	 *	Gets the first column of the first row
164
	 */
165
	public function get_one($statement) {
166
		$fetch_row = mysql_fetch_row(mysql_query($statement));
167
		$result = $fetch_row[0];
168
		$this->set_error(mysql_error());
169
		if(mysql_error()) {
170
			return null;
171
		} else {
172
			return $result;
173
		}
174
	}
175
	
176
	/**
177
	 *	Set the DB error
178
	 */
179
	public function set_error($message = null) {
180
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
181
		$this->error = $message;
182
		if(strpos($message, 'no such table')) {
183
			$this->error_type = $TABLE_DOES_NOT_EXIST;
184
		} else {
185
			$this->error_type = $TABLE_UNKNOWN;
186
		}
187
	}
188
	
189
	/**
190
	 *	Return true if there was an error
191
	 */
192
	public function is_error() {
193
		return ( 0 <> $this->error ) ? true : false;
194
	}
195
	
196
	/**
197
	 *	Return the error
198
	 */
199
	public function get_error() {
200
		return $this->error;
201
	}
202
	
203
	/**
204
	 *	Copy a content of a given table to another entry of the table
205
	 *
206
	 *	@param	string	The tablename
207
	 *	@param	string	The source condition, e.g. "page_id=23"
208
	 *	@param	string	The target condition, e.g. "page_id=33"
209
	 *	@param	mixed	The exeptions, the field, that should not copied; e.g. "page_id". Could be also an array.
210
	 *	@param	integer	An optional Jobnumber for debugging.
211
	 *	@param	bool	Debugmode to display the final querys on screen instead of execute them
212
	 *
213
	 *	@return	bool	true if all's ok.
214
	 *
215
	 */
216
	public function copy_content ($aTableName="", $aSourceCondition="", $aTargetCondition="", $aException="", $aJobNum=3100, $debug=false) {
217
		if (!is_array($aException)) $aException = array ($aException);
218
		$all_fields = mysql_list_fields( DB_NAME, $aTableName);
219
		$n = mysql_num_fields($all_fields);
220
		if ($n == 0) die ("Error: no fields found in ".$aTableName);
221
		$i=0;
222
		$all_names = array();
223
		while($i < $n) {
224
			$field_name = mysql_field_name($all_fields, $i++);
225
			if (!in_array ($field_name, $aException) ) $all_names[] = $field_name; 
226
		}
227
		
228
		$result = $this->query ("SELECT ".implode(", ",$all_names)." from ". $aTableName ." where ".$aSourceCondition, 3000 );
229
		$data = $result->fetchRow();
230
		if (false === $debug) {
231
			foreach ($all_names as $c => $item) $this->query ("UPDATE ". $aTableName ." set ". $item ."='" .$data[ $item ]."' where ".$aTargetCondition, ($aJobNum + (integer)$c));
232
		} else {
233
			foreach ($all_names as $c => $item) echo "UPDATE ". $aTableName ." set ". $item ."='" .$data[ $item ]."' where ".$aTargetCondition."<br />";
234
			die();
235
		}
236
		return true;
237
	}
238
	
239
	/**
240
	 *	Simple way to log the querys for debugging
241
	 */
242
	public function __write_log () {
243
	
244
		$path = ($this->log_path == "") ? WB_PATH."/framework/" : $this->log_path;
245
		$fp = fopen($path.$this->log_filename, 'a');
246
			
247
		if ($fp) {
248
			$str = "\n## ".DATE("Y-m-d H:i:s", TIME())." --------------\n".str_replace( array("\n", "\t", "\r"), array("", "", ""), $this->last_query);
249
			$str .= "\n";
250
			
251
			fwrite($fp, $str, strlen($str) );
252
			fclose($fp);
253
		}
254
	}
255
	
256
	public function __display_error() {
257
		ob_end_flush();
258
		if ($this->last_jobnumber == 0) $this->last_jobnumber = "0 (no job-number specified)";
259
		$msg = str_replace (
260
			array ('{job}', '{message}'), 
261
			array ($this->last_jobnumber, $this->error), 
262
			$this->error_tmpl 
263
		);
264
		
265
		die ($msg);
266
	}
267
	
268
}
269

    
270
class c_mysql {
271

    
272
	public $result 	= 0;
273
	public $error	= 0;
274
	
275
	/**
276
	 *	Run a query
277
	 */
278
	public function query($statement) {
279
		$this->result = mysql_query($statement);
280
		$this->error = mysql_error();
281
		return $this->result;
282
	}
283
	
284
	/**
285
	 *	Fetch num rows
286
	 */
287
	public function numRows() {
288
		return mysql_num_rows($this->result);
289
	}
290
	
291
	/**
292
	 *	Fetch row
293
	 *	
294
	 *	@param string	typ	One of the following
295
	 *						MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
296
	 */
297
	public function fetchRow($typ = MYSQL_BOTH) {
298
		return mysql_fetch_array($this->result);
299
	}
300
	
301
	/**
302
	 *	Get error
303
	 */
304
	public function error() {
305
		if ( ( 0 <> $this->error ) AND (is_string($this->error) ) ) {
306
			return $this->error;
307
		} else {
308
			return null;
309
		}
310
	}
311
}
312

    
313
?>
(4-4/15)