/
Db.php
executable file
·188 lines (173 loc) · 4.22 KB
/
Db.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* The database abstraction factory
*
* PHP Version 5.4
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* @category phpMyFAQ
* @package PMF_Db
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2003-2014 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link http://www.phpmyfaq.de
* @since 2003-02-24
*/
if (!defined('IS_VALID_PHPMYFAQ')) {
exit();
}
/**
* PMF_Db
*
* @category phpMyFAQ
* @package PMF_Db
* @author Thorsten Rinne <thorsten@phpmyfaq.de>
* @copyright 2003-2014 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link http://www.phpmyfaq.de
* @since 2003-02-24
*/
class PMF_Db
{
/**
* Instance
*
* @var PMF_Db_Driver
*/
private static $instance = null;
/**
* Database type
*
* @var string
*/
private static $dbType = null;
/**
* Table prefix
*
* @var string
*/
private static $tablePrefix = null;
/**
* Constructor
*
*/
private function __construct()
{
}
/**
* Database factory
*
* @param string $type Database management system type
*
* @throws PMF_Exception
*
* @return PMF_DB_Driver
*/
public static function factory($type)
{
self::$dbType = $type;
if (0 === strpos($type, 'pdo_')) {
$class = 'PMF_DB_Pdo_' . ucfirst(substr($type, 4));
} else {
$class = 'PMF_DB_' . ucfirst($type);
}
if (class_exists($class)) {
self::$instance = new $class;
return self::$instance;
} else {
throw new PMF_Exception('Invalid Database Type: ' . $type);
}
}
/**
* Returns the single instance
*
* @return PMF_Db_Driver
*/
public static function getInstance()
{
if (null == self::$instance) {
$className = __CLASS__;
self::$instance = new $className();
}
return self::$instance;
}
/**
* __clone() Magic method to prevent cloning
*
* @return void
*/
private function __clone()
{
}
/**
* Returns the database type
*
* @return string
*/
public static function getType()
{
return self::$dbType;
}
/**
* Check if a table is filled with data
*
* @param string $tableName Table name
*
* @return boolean true, if table is empty, otherwise false
*/
public static function checkOnEmptyTable($tableName)
{
if (self::$instance->numRows(self::$instance->query('SELECT * FROM ' . PMF_Db::getTablePrefix() . $tableName)) < 1) {
return true;
} else {
return false;
}
}
/**
* Error page, if the database connection is not possible
*
* @param string $method
*
* @return string
*/
public static function errorPage($method)
{
echo '<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<meta charset="utf-8">
<title>Fatal phpMyFAQ Error</title>
<style type="text/css">
@import url(assets/template/default/css/style.min.css);
</style>
</head>
<body>
<div class="container">
<p class="alert alert-danger">The connection to the database server could not be established.</p>
<p class="alert alert-danger">The error message of the database server:<br />' . $method . '</p>
</div>
</body>
</html>';
}
/**
* Sets the table prefix
*
* @param string $tablePrefix
*/
public static function setTablePrefix($tablePrefix)
{
self::$tablePrefix = $tablePrefix;
}
/**
* Returns the table prefix
*
* @return string
*/
public static function getTablePrefix()
{
return self::$tablePrefix;
}
}