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