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 1496 2011-08-11 16:15:31Z DarkViper $
|
14
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/class.database.php $
|
15
|
* @lastmodified $Date: 2011-08-11 18:15:31 +0200 (Thu, 11 Aug 2011) $
|
16
|
*
|
17
|
*/
|
18
|
|
19
|
/*
|
20
|
|
21
|
Database class
|
22
|
|
23
|
This class will be used to interface between the database
|
24
|
and the Website Baker code
|
25
|
|
26
|
*/
|
27
|
|
28
|
/* -------------------------------------------------------- */
|
29
|
// Must include code to stop this file being accessed directly
|
30
|
require_once('globalExceptionHandler.php');
|
31
|
if(!defined('WB_PATH')) { throw new IllegalFileException(); }
|
32
|
/* -------------------------------------------------------- */
|
33
|
if(!defined('DB_URL')) {
|
34
|
//define('DB_URL', DB_TYPE.'://'.DB_USERNAME.':'.DB_PASSWORD.'@'.DB_HOST.'/'.DB_NAME);
|
35
|
}
|
36
|
|
37
|
define('DATABASE_CLASS_LOADED', true);
|
38
|
|
39
|
class database {
|
40
|
|
41
|
private $db_handle = null; // readonly from outside
|
42
|
|
43
|
private $connected = false;
|
44
|
|
45
|
private $error = '';
|
46
|
private $error_type = '';
|
47
|
private $message = array();
|
48
|
|
49
|
|
50
|
// Set DB_URL
|
51
|
function database($url = '') {
|
52
|
// Connect to database
|
53
|
$this->connect();
|
54
|
// Check for database connection error
|
55
|
if($this->is_error()) {
|
56
|
die($this->get_error());
|
57
|
}
|
58
|
}
|
59
|
|
60
|
// Connect to the database
|
61
|
function connect() {
|
62
|
$status = $this->db_handle = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD);
|
63
|
if(mysql_error()) {
|
64
|
$this->connected = false;
|
65
|
$this->error = mysql_error();
|
66
|
} else {
|
67
|
if(!mysql_select_db(DB_NAME)) {
|
68
|
$this->connected = false;
|
69
|
$this->error = mysql_error();
|
70
|
} else {
|
71
|
$this->connected = true;
|
72
|
}
|
73
|
}
|
74
|
return $this->connected;
|
75
|
}
|
76
|
|
77
|
// Disconnect from the database
|
78
|
function disconnect() {
|
79
|
if($this->connected==true) {
|
80
|
mysql_close();
|
81
|
return true;
|
82
|
} else {
|
83
|
return false;
|
84
|
}
|
85
|
}
|
86
|
|
87
|
// Run a query
|
88
|
function query($statement) {
|
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
|
$fetch_row = mysql_fetch_array(mysql_query($statement) );
|
103
|
$result = $fetch_row[0];
|
104
|
$this->set_error(mysql_error());
|
105
|
if(mysql_error()) {
|
106
|
return null;
|
107
|
} else {
|
108
|
return $result;
|
109
|
}
|
110
|
}
|
111
|
|
112
|
// Set the DB error
|
113
|
function set_error($message = null) {
|
114
|
global $TABLE_DOES_NOT_EXIST, $TABLE_UNKNOWN;
|
115
|
$this->error = $message;
|
116
|
if(strpos($message, 'no such table')) {
|
117
|
$this->error_type = $TABLE_DOES_NOT_EXIST;
|
118
|
} else {
|
119
|
$this->error_type = $TABLE_UNKNOWN;
|
120
|
}
|
121
|
}
|
122
|
|
123
|
// Return true if there was an error
|
124
|
function is_error() {
|
125
|
return (!empty($this->error)) ? true : false;
|
126
|
}
|
127
|
|
128
|
// Return the error
|
129
|
function get_error() {
|
130
|
return $this->error;
|
131
|
}
|
132
|
|
133
|
/*
|
134
|
* default Getter
|
135
|
*/
|
136
|
public function __get($var_name)
|
137
|
{
|
138
|
if($var_name == 'db_handle')
|
139
|
{
|
140
|
return $this->db_handle;
|
141
|
}
|
142
|
return null;
|
143
|
}
|
144
|
|
145
|
/*
|
146
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
147
|
* @param string $field_name: name of the field to seek for
|
148
|
* @return bool: true if field exists
|
149
|
*/
|
150
|
public function field_exists($table_name, $field_name)
|
151
|
{
|
152
|
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
|
153
|
$query = $this->query($sql);
|
154
|
return ($query->numRows() != 0);
|
155
|
}
|
156
|
|
157
|
/*
|
158
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
159
|
* @param string $index_name: name of the index to seek for
|
160
|
* @return bool: true if field exists
|
161
|
*/
|
162
|
public function index_exists($table_name, $index_name, $number_fields = 0)
|
163
|
{
|
164
|
$number_fields = intval($number_fields);
|
165
|
$keys = 0;
|
166
|
$sql = 'SHOW INDEX FROM `'.$table_name.'`';
|
167
|
if( ($res_keys = $this->query($sql)) )
|
168
|
{
|
169
|
while(($rec_key = $res_keys->fetchRow()))
|
170
|
{
|
171
|
if( $rec_key['Key_name'] == $index_name )
|
172
|
{
|
173
|
$keys++;
|
174
|
}
|
175
|
}
|
176
|
|
177
|
}
|
178
|
if( $number_fields == 0 )
|
179
|
{
|
180
|
return ($keys != $number_fields);
|
181
|
}else
|
182
|
{
|
183
|
return ($keys == $number_fields);
|
184
|
}
|
185
|
}
|
186
|
/*
|
187
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
188
|
* @param string $field_name: name of the field to add
|
189
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
190
|
* @return bool: true if successful, otherwise false and error will be set
|
191
|
*/
|
192
|
public function field_add($table_name, $field_name, $description)
|
193
|
{
|
194
|
if( !$this->field_exists($table_name, $field_name) )
|
195
|
{ // add new field into a table
|
196
|
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
|
197
|
$query = $this->query($sql);
|
198
|
$this->set_error(mysql_error());
|
199
|
if( !$this->is_error() )
|
200
|
{
|
201
|
return ( $this->field_exists($table_name, $field_name) ) ? true : false;
|
202
|
}
|
203
|
}else
|
204
|
{
|
205
|
$this->set_error('field \''.$field_name.'\' already exists');
|
206
|
}
|
207
|
return false;
|
208
|
}
|
209
|
|
210
|
/*
|
211
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
212
|
* @param string $field_name: name of the field to add
|
213
|
* @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
|
214
|
* @return bool: true if successful, otherwise false and error will be set
|
215
|
*/
|
216
|
public function field_modify($table_name, $field_name, $description)
|
217
|
{
|
218
|
$retval = false;
|
219
|
if( $this->field_exists($table_name, $field_name) )
|
220
|
{ // modify a existing field in a table
|
221
|
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
|
222
|
$retval = ( $this->query($sql) ? true : false);
|
223
|
$this->set_error(mysql_error());
|
224
|
}
|
225
|
return $retval;
|
226
|
}
|
227
|
|
228
|
/*
|
229
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
230
|
* @param string $field_name: name of the field to remove
|
231
|
* @return bool: true if successful, otherwise false and error will be set
|
232
|
*/
|
233
|
public function field_remove($table_name, $field_name)
|
234
|
{
|
235
|
$retval = false;
|
236
|
if( $this->field_exists($field_name, $table_name) )
|
237
|
{ // modify a existing field in a table
|
238
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
|
239
|
$retval = ( $this->query($sql) ? true : false );
|
240
|
}
|
241
|
return $retval;
|
242
|
}
|
243
|
|
244
|
/*
|
245
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
246
|
* @param string $index_name: name of the new index
|
247
|
* @param string $field_list: comma seperated list of fields for this index
|
248
|
* @param string $index_type: kind of index (UNIQUE, PRIMARY, '')
|
249
|
* @return bool: true if successful, otherwise false and error will be set
|
250
|
*/
|
251
|
public function index_add($table_name, $index_name, $field_list, $index_type = '')
|
252
|
{
|
253
|
$retval = false;
|
254
|
$field_list = str_replace(' ', '', $field_list);
|
255
|
$field_list = explode(',', $field_list);
|
256
|
$number_fields = sizeof($field_list);
|
257
|
$field_list = '`'.implode('`,`', $field_list).'`';
|
258
|
if( $this->index_exists($table_name, $index_name, $number_fields) ||
|
259
|
$this->index_exists($table_name, $index_name))
|
260
|
{
|
261
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
262
|
$sql .= 'DROP INDEX `'.$index_name.'`';
|
263
|
if( $this->query($sql))
|
264
|
{
|
265
|
$sql = 'ALTER TABLE `'.$table_name.'` ';
|
266
|
$sql .= 'ADD '.$index_type.' `'.$index_name.'` ( '.$field_list.' ); ';
|
267
|
if( $this->query($sql)) { $retval = true; }
|
268
|
}
|
269
|
}
|
270
|
return $retval;
|
271
|
}
|
272
|
|
273
|
/*
|
274
|
* @param string $table_name: full name of the table (incl. TABLE_PREFIX)
|
275
|
* @param string $field_name: name of the field to remove
|
276
|
* @return bool: true if successful, otherwise false and error will be set
|
277
|
*/
|
278
|
public function index_remove($table_name, $index_name)
|
279
|
{
|
280
|
$retval = false;
|
281
|
if( $this->index_exists($table_name, $index_name) )
|
282
|
{ // modify a existing field in a table
|
283
|
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
|
284
|
$retval = ( $this->query($sql) ? true : false );
|
285
|
}
|
286
|
return $retval;
|
287
|
}
|
288
|
|
289
|
} /// end of class database
|
290
|
|
291
|
class mysql {
|
292
|
|
293
|
// Run a query
|
294
|
function query($statement) {
|
295
|
$this->result = mysql_query($statement);
|
296
|
$this->error = mysql_error();
|
297
|
return $this->result;
|
298
|
}
|
299
|
|
300
|
// Fetch num rows
|
301
|
function numRows() {
|
302
|
return mysql_num_rows($this->result);
|
303
|
}
|
304
|
|
305
|
// Fetch row $typ = MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH
|
306
|
function fetchRow($typ = MYSQL_BOTH) {
|
307
|
return mysql_fetch_array($this->result, $typ);
|
308
|
}
|
309
|
|
310
|
// Get error
|
311
|
function error() {
|
312
|
if(isset($this->error)) {
|
313
|
return $this->error;
|
314
|
} else {
|
315
|
return null;
|
316
|
}
|
317
|
}
|
318
|
|
319
|
}
|
320
|
/* this function is placed inside this file temporarely until a better place is found */
|
321
|
/* function to update a var/value-pair(s) in table ****************************
|
322
|
* nonexisting keys are inserted
|
323
|
* @param string $table: name of table to use (without prefix)
|
324
|
* @param mixed $key: a array of key->value pairs to update
|
325
|
* or a string with name of the key to update
|
326
|
* @param string $value: a sting with needed value, if $key is a string too
|
327
|
* @return bool: true if any keys are updated, otherwise false
|
328
|
*/
|
329
|
function db_update_key_value($table, $key, $value = '')
|
330
|
{
|
331
|
global $database;
|
332
|
if( !is_array($key))
|
333
|
{
|
334
|
if( trim($key) != '' )
|
335
|
{
|
336
|
$key = array( trim($key) => trim($value) );
|
337
|
} else {
|
338
|
$key = array();
|
339
|
}
|
340
|
}
|
341
|
$retval = true;
|
342
|
foreach( $key as $index=>$val)
|
343
|
{
|
344
|
$index = strtolower($index);
|
345
|
$sql = 'SELECT COUNT(`setting_id`) FROM `'.TABLE_PREFIX.$table.'` WHERE `name` = \''.$index.'\' ';
|
346
|
if($database->get_one($sql))
|
347
|
{
|
348
|
$sql = 'UPDATE ';
|
349
|
$sql_where = 'WHERE `name` = \''.$index.'\'';
|
350
|
}else {
|
351
|
$sql = 'INSERT INTO ';
|
352
|
$sql_where = '';
|
353
|
}
|
354
|
$sql .= '`'.TABLE_PREFIX.$table.'` ';
|
355
|
$sql .= 'SET `name` = \''.$index.'\', ';
|
356
|
$sql .= '`value` = \''.$val.'\' '.$sql_where;
|
357
|
if( !$database->query($sql) )
|
358
|
{
|
359
|
$retval = false;
|
360
|
}
|
361
|
}
|
362
|
return $retval;
|
363
|
}
|
364
|
?>
|