Project

General

Profile

« Previous | Next » 

Revision 1149

Added by aldus over 14 years ago

Modifications inside class.database.
Remove the empty config.php
Update index.php and save.php

View differences:

class.database.php
5 5
/*
6 6

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

  
10 10
 Website Baker is free software; you can redistribute it and/or modify
11 11
 it under the terms of the GNU General Public License as published by
......
32 32

  
33 33
*/
34 34

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

  
41
if(!defined('DB_URL')) {
42
	//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
43
}
44 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
 */
45 55
define('DATABASE_CLASS_LOADED', true);
46 56

  
47 57
class database {
48 58
	
49
	// Set DB_URL
50
	function database($url = '') {
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 = '') {
51 87
		// Connect to database
52 88
		$this->connect();
53 89
		// Check for database connection error
......
56 92
		}
57 93
	}
58 94
	
59
	// Connect to the database
60
	function connect() {
61
		$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
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);
62 100
		if(mysql_error()) {
63 101
			$this->connected = false;
64 102
			$this->error = mysql_error();
......
73 111
		return $this->connected;
74 112
	}
75 113
	
76
	// Disconnect from the database
77
	function disconnect() {
114
	/**
115
	 *	Disconnect from the database
116
	 */
117
	public function disconnect() {
78 118
		if($this->connected==true) {
79
			mysql_close();
119
			mysql_close( $this->db_handle );
80 120
			return true;
81 121
		} else {
82 122
			return false;
83 123
		}
84 124
	}
85 125
	
86
	// Run a query
87
	function query($statement) {
88
		$mysql = new mysql();
89
		$mysql->query($statement);
90
		$this->set_error($mysql->error());
91
		if($mysql->error()) {
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();
92 141
			return null;
93 142
		} else {
94
			return $mysql;
143
			return clone $this->mySQL_handle;
95 144
		}
96 145
	}
97 146
	
98
	// Gets the first column of the first row
99
	function get_one($statement) {
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) {
100 166
		$fetch_row = mysql_fetch_row(mysql_query($statement));
101 167
		$result = $fetch_row[0];
102 168
		$this->set_error(mysql_error());
......
107 173
		}
108 174
	}
109 175
	
110
	// Set the DB error
111
	function set_error($message = null) {
176
	/**
177
	 *	Set the DB error
178
	 */
179
	public function set_error($message = null) {
112 180
		global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
113 181
		$this->error = $message;
114 182
		if(strpos($message, 'no such table')) {
......
118 186
		}
119 187
	}
120 188
	
121
	// Return true if there was an error
122
	function is_error() {
123
		return (!empty($this->error)) ? true : false;
189
	/**
190
	 *	Return true if there was an error
191
	 */
192
	public function is_error() {
193
		return ( 0 <> $this->error ) ? true : false;
124 194
	}
125 195
	
126
	// Return the error
127
	function get_error() {
196
	/**
197
	 *	Return the error
198
	 */
199
	public function get_error() {
128 200
		return $this->error;
129 201
	}
130 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
	
131 268
}
132 269

  
133
class mysql {
270
class c_mysql {
134 271

  
135
	// Run a query
136
	function query($statement) {
272
	public $result 	= 0;
273
	public $error	= 0;
274
	
275
	/**
276
	 *	Run a query
277
	 */
278
	public function query($statement) {
137 279
		$this->result = mysql_query($statement);
138 280
		$this->error = mysql_error();
139 281
		return $this->result;
140 282
	}
141 283
	
142
	// Fetch num rows
143
	function numRows() {
284
	/**
285
	 *	Fetch num rows
286
	 */
287
	public function numRows() {
144 288
		return mysql_num_rows($this->result);
145 289
	}
146

  
147
	// Fetch row  $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
148
	function fetchRow($typ = MYSQL_BOTH) {
149
		return mysql_fetch_array($this->result, $typ);
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);
150 299
	}
151

  
152
	// Get error
153
	function error() {
154
		if(isset($this->error)) {
300
	
301
	/**
302
	 *	Get error
303
	 */
304
	public function error() {
305
		if ( ( 0 <> $this->error ) AND (is_string($this->error) ) ) {
155 306
			return $this->error;
156 307
		} else {
157 308
			return null;
158 309
		}
159 310
	}
160

  
161 311
}
162 312

  
163 313
?>

Also available in: Unified diff