-
Notifications
You must be signed in to change notification settings - Fork 1
/
Database.php
106 lines (86 loc) · 2.73 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
class Database {
protected $_name;
protected $_user;
protected $_password;
protected $_host;
protected $_oldConnection;
protected $_connection;
public function __construct( $dbProperties = array(), $old = false, $new = true ) {
$defaults = array(
'name' => 'babynewt',
'user' => 'babynewt',
'password' => 'babynewt',
'host' =>'localhost',
);
$merged = array_merge( $defaults, $dbProperties );
$this->_name = $merged['name'];
$this->_user = $merged['user'];
$this->_password = $merged['password'];
$this->_host = $merged['host'];
$this->_oldConnection = null;
$this->_connection = null;
if( $old ) {
$this->getOldConnection();
}
if( $new ) {
$this->getConnection();
}
}
protected function _connectOld() {
if ( null === $this->_oldConnection ) {
$this->_oldConnection = mysqli_connect(
$this->_host,
$this->_user,
$this->_password
);
if ( !mysqli_select_db( $this->_oldConnection, $this->_name ) ) {
throw new Exception(
'Unable to select old database: [' . mysqli_errno() .
']' . mysqli_error()
);
}
}
return $this->_oldConnection;
}
protected function _connect() {
if ( null === $this->_connection ) {
$this->_connection = new mysqli(
$this->_host,
$this->_user,
$this->_password,
$this->_name
);
if ( $this->_connection->connect_errno ) {
throw new Exception(
'Unable to select database: [' . $this->_connection->connect_errno .
']' . $this->_connection->connect_error
);
}
}
return $this->_connection;
}
public function getOldConnection() {
return $this->_connectOld();
}
public function getConnection() {
return $this->_connect();
}
/**
* @see http://www.askbee.net/articles/php/SQL_Injection/sql_injection.html
*/
public static function sql_quote( $value ) {
if ( get_magic_quotes_gpc() ) {
$value = stripslashes( $value );
}
// check if this function exists
if ( function_exists( "mysqli_real_escape_string" ) ) {
$value = mysqli_real_escape_string( $value );
}
// for PHP version < 4.3.0 use addslashes
else {
$value = addslashes( $value );
}
return $value;
}
}