PluginProbe ʕ •ᴥ•ʔ
Wordfence Security – Firewall, Malware Scan, and Login Security / 7.3.2
Wordfence Security – Firewall, Malware Scan, and Login Security v7.3.2
8.2.2 8.2.1 8.2.0 3.7.1 3.7.2 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.9.1 4.0.1 4.0.2 4.0.3 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.1 5.1.2 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.1 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.1 5.3.10 5.3.11 5.3.12 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.3.7 5.3.8 5.3.9 6.0.1 6.0.10 6.0.11 6.0.12 6.0.14 6.0.15 6.0.16 6.0.17 6.0.18 6.0.19 6.0.2 6.0.20 6.0.21 6.0.22 6.0.23 6.0.24 6.0.25 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.1 6.1.10 6.1.11 6.1.12 6.1.14 6.1.15 6.1.16 6.1.17 6.1.2 6.1.3 6.1.4 6.1.5 6.1.6 6.1.7 6.1.8 6.1.9 6.2.0 6.2.1 6.2.10 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.2.7 6.2.8 6.2.9 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.14 6.3.15 6.3.16 6.3.17 6.3.18 6.3.19 6.3.2 6.3.20 6.3.21 6.3.22 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.1 7.0.2 7.0.3 7.0.4 7.0.5 7.1.0 7.1.1 7.1.10 7.1.11 7.1.12 7.1.14 7.1.15 7.1.16 7.1.17 7.1.18 7.1.19 7.1.2 7.1.20 7.1.3 7.1.4 7.1.5 7.1.6 7.1.7 7.1.8 7.1.9 7.10.0 7.10.1 7.10.2 7.10.3 7.10.4 7.10.5 7.10.6 7.10.7 7.11.0 7.11.1 7.11.2 7.11.3 7.11.4 7.11.5 7.11.6 7.11.7 7.2.1 7.2.2 7.2.3 7.2.4 7.2.5 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.4.0 7.4.1 7.4.10 7.4.11 7.4.12 7.4.14 7.4.2 7.4.3 trunk 7.4.4 1.1 7.4.5 1.2 7.4.6 1.3 7.4.7 1.3.1 7.4.8 1.3.2 7.4.9 1.3.3 7.5.0 1.4.2 7.5.1 1.4.3 7.5.10 1.4.4 7.5.11 1.4.5 7.5.2 1.4.6 7.5.3 1.4.7 7.5.4 1.4.8 7.5.5 1.5.1 7.5.6 1.5.2 7.5.7 1.5.3 7.5.8 1.5.4 7.5.9 1.5.5 7.6.0 1.5.6 7.6.1 2.0.1 7.6.2 2.0.2 7.7.0 2.0.3 7.7.1 2.0.5 7.8.0 2.0.6 7.8.1 2.0.7 7.8.2 2.1.0 7.9.0 2.1.1 7.9.1 2.1.2 7.9.2 2.1.3 7.9.3 2.1.4 8.0.0 2.1.5 8.0.1 3.0.2 8.0.2 3.0.3 8.0.3 3.0.4 8.0.4 3.0.5 8.0.5 3.0.6 8.1.0 3.0.7 8.1.1 3.0.8 8.1.2 3.0.9 8.1.3 3.1.0 8.1.4 3.1.1 v1.4.1 3.1.2 3.1.4 3.1.6 3.2.1 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.1 3.4.4 3.4.5 3.5.1 3.5.2 3.6.1 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
wordfence / lib / wfDB.php
wordfence / lib Last commit date
Diff 8 years ago dashboard 7 years ago rest-api 7 years ago .htaccess 7 years ago Diff.php 14 years ago GeoLite2-Country.mmdb 7 years ago IPTraf.php 8 years ago IPTrafList.php 7 years ago WFLSPHP52Compatability.php 7 years ago compat.php 8 years ago conntest.php 7 years ago cronview.php 8 years ago dbview.php 8 years ago diffResult.php 8 years ago email_genericAlert.php 7 years ago email_newIssues.php 7 years ago email_unlockRequest.php 8 years ago email_unsubscribeRequest.php 7 years ago flags.php 7 years ago live_activity.php 8 years ago menu_dashboard.php 7 years ago menu_dashboard_options.php 7 years ago menu_firewall.php 7 years ago menu_firewall_blocking.php 7 years ago menu_firewall_blocking_options.php 8 years ago menu_firewall_waf.php 7 years ago menu_firewall_waf_options.php 7 years ago menu_options.php 7 years ago menu_scanner.php 7 years ago menu_scanner_credentials.php 8 years ago menu_scanner_options.php 8 years ago menu_support.php 7 years ago menu_tools.php 7 years ago menu_tools_diagnostic.php 7 years ago menu_tools_importExport.php 7 years ago menu_tools_livetraffic.php 7 years ago menu_tools_twoFactor.php 7 years ago menu_tools_whois.php 8 years ago menu_wordfence_central.php 7 years ago noc1.key 7 years ago sysinfo.php 8 years ago unknownFiles.php 8 years ago viewFullActivityLog.php 8 years ago wf503.php 7 years ago wfAPI.php 7 years ago wfActivityReport.php 7 years ago wfAdminNoticeQueue.php 8 years ago wfArray.php 7 years ago wfBrowscap.php 8 years ago wfBrowscapCache.php 7 years ago wfBulkCountries.php 7 years ago wfCache.php 9 years ago wfCentralAPI.php 7 years ago wfConfig.php 7 years ago wfCrawl.php 8 years ago wfCredentialsController.php 7 years ago wfCrypt.php 7 years ago wfDB.php 7 years ago wfDashboard.php 7 years ago wfDateLocalization.php 8 years ago wfDiagnostic.php 7 years ago wfDict.php 8 years ago wfDirectoryIterator.php 7 years ago wfHelperBin.php 11 years ago wfHelperString.php 11 years ago wfIPWhitelist.php 7 years ago wfImportExportController.php 7 years ago wfIssues.php 7 years ago wfJWT.php 7 years ago wfLockedOut.php 7 years ago wfLog.php 7 years ago wfMD5BloomFilter.php 8 years ago wfModuleController.php 7 years ago wfNotification.php 8 years ago wfOnboardingController.php 7 years ago wfPersistenceController.php 8 years ago wfRESTAPI.php 7 years ago wfScan.php 7 years ago wfScanEngine.php 7 years ago wfSchema.php 7 years ago wfStyle.php 7 years ago wfSupportController.php 7 years ago wfUnlockMsg.php 7 years ago wfUpdateCheck.php 8 years ago wfUtils.php 7 years ago wfVersionCheckController.php 8 years ago wfView.php 10 years ago wfViewResult.php 8 years ago wordfenceClass.php 7 years ago wordfenceConstants.php 7 years ago wordfenceHash.php 7 years ago wordfenceScanner.php 7 years ago wordfenceURLHoover.php 7 years ago
wfDB.php
325 lines
1 <?php
2 class wfDB {
3 public $errorMsg = false;
4
5 public static function shared() {
6 static $_shared = null;
7 if ($_shared === null) {
8 $_shared = new wfDB();
9 }
10 return $_shared;
11 }
12
13 /**
14 * Returns the table prefix for the main site on multisites and the site itself on single site installations.
15 *
16 * @return string
17 */
18 public static function networkPrefix() {
19 global $wpdb;
20 return $wpdb->base_prefix;
21 }
22
23 /**
24 * Returns the table with the site (single site installations) or network (multisite) prefix added.
25 *
26 * @param string $table
27 * @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
28 * @return string
29 */
30 public static function networkTable($table, $applyCaseConversion = true) {
31 if (wfSchema::usingLowercase() && $applyCaseConversion) {
32 $table = strtolower($table);
33 }
34 return self::networkPrefix() . $table;
35 }
36
37 /**
38 * Returns the table prefix for the given blog ID. On single site installations, this will be equivalent to wfDB::networkPrefix().
39 *
40 * @param int $blogID
41 * @return string
42 */
43 public static function blogPrefix($blogID) {
44 global $wpdb;
45 return $wpdb->get_blog_prefix($blogID);
46 }
47
48 /**
49 * Returns the table with the site (single site installations) or blog-specific (multisite) prefix added.
50 *
51 * @param string $table
52 * @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
53 * @return string
54 */
55 public static function blogTable($table, $blogID, $applyCaseConversion = true) {
56 if (wfSchema::usingLowercase() && $applyCaseConversion) {
57 $table = strtolower($table);
58 }
59 return self::blogPrefix($blogID) . $table;
60 }
61
62 public function querySingle(){
63 global $wpdb;
64 if(func_num_args() > 1){
65 $args = func_get_args();
66 return $wpdb->get_var(call_user_func_array(array($wpdb, 'prepare'), $args));
67 } else {
68 return $wpdb->get_var(func_get_arg(0));
69 }
70 }
71 public function querySingleRec(){ //queryInSprintfFormat, arg1, arg2, ... :: Returns a single assoc-array or null if nothing found.
72 global $wpdb;
73 if(func_num_args() > 1){
74 $args = func_get_args();
75 return $wpdb->get_row(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
76 } else {
77 return $wpdb->get_row(func_get_arg(0), ARRAY_A);
78 }
79 }
80 public function queryWrite(){
81 global $wpdb;
82 if(func_num_args() > 1){
83 $args = func_get_args();
84 return $wpdb->query(call_user_func_array(array($wpdb, 'prepare'), $args));
85 } else {
86 return $wpdb->query(func_get_arg(0));
87 }
88 }
89 public function flush(){ //Clear cache
90 global $wpdb;
91 $wpdb->flush();
92 }
93 public function querySelect(){ //sprintfString, arguments :: always returns array() and will be empty if no results.
94 global $wpdb;
95 if(func_num_args() > 1){
96 $args = func_get_args();
97 return $wpdb->get_results(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
98 } else {
99 return $wpdb->get_results(func_get_arg(0), ARRAY_A);
100 }
101 }
102 public function queryWriteIgnoreError(){ //sprintfString, arguments
103 global $wpdb;
104 $oldSuppress = $wpdb->suppress_errors(true);
105 $args = func_get_args();
106 call_user_func_array(array($this, 'queryWrite'), $args);
107 $wpdb->suppress_errors($oldSuppress);
108 }
109 public function columnExists($table, $col){
110 $table = wfDB::networkTable($table);
111 $q = $this->querySelect("desc $table");
112 foreach($q as $row){
113 if($row['Field'] == $col){
114 return true;
115 }
116 }
117 return false;
118 }
119 public function dropColumn($table, $col){
120 $table = wfDB::networkTable($table);
121 $this->queryWrite("alter table $table drop column $col");
122 }
123 public function createKeyIfNotExists($table, $col, $keyName){
124 $table = wfDB::networkTable($table);
125
126 $exists = $this->querySingle(<<<SQL
127 SELECT TABLE_NAME FROM information_schema.TABLES
128 WHERE TABLE_SCHEMA=DATABASE()
129 AND TABLE_NAME='%s'
130 SQL
131 , $table);
132 $keyFound = false;
133 if($exists){
134 $q = $this->querySelect("show keys from $table");
135 foreach($q as $row){
136 if($row['Key_name'] == $keyName){
137 $keyFound = true;
138 }
139 }
140 }
141 if(! $keyFound){
142 $this->queryWrite("alter table $table add KEY $keyName($col)");
143 }
144 }
145 public function getMaxAllowedPacketBytes(){
146 $rec = $this->querySingleRec("show variables like 'max_allowed_packet'");
147 return intval($rec['Value']);
148 }
149 public function getMaxLongDataSizeBytes() {
150 $rec = $this->querySingleRec("show variables like 'max_long_data_size'");
151 return $rec['Value'];
152 }
153 public function truncate($table){ //Ensures everything is deleted if user is using MySQL >= 5.1.16 and does not have "drop" privileges
154 $this->queryWrite("truncate table $table");
155 $this->queryWrite("delete from $table");
156 }
157 public function getLastError(){
158 global $wpdb;
159 return $wpdb->last_error;
160 }
161 public function realEscape($str){
162 global $wpdb;
163 return $wpdb->_real_escape($str);
164 }
165 }
166
167 abstract class wfModel {
168
169 private $data;
170 private $db;
171 private $dirty = false;
172
173 /**
174 * Column name of the primary key field.
175 *
176 * @return string
177 */
178 abstract public function getIDColumn();
179
180 /**
181 * Table name.
182 *
183 * @return mixed
184 */
185 abstract public function getTable();
186
187 /**
188 * Checks if this is a valid column in the table before setting data on the model.
189 *
190 * @param string $column
191 * @return boolean
192 */
193 abstract public function hasColumn($column);
194
195 /**
196 * wfModel constructor.
197 * @param array|int|string $data
198 */
199 public function __construct($data = array()) {
200 if (is_array($data) || is_object($data)) {
201 $this->setData($data);
202 } else if (is_numeric($data)) {
203 $this->fetchByID($data);
204 }
205 }
206
207 public function fetchByID($id) {
208 $id = absint($id);
209 $data = $this->getDB()->get_row($this->getDB()->prepare('SELECT * FROM ' . $this->getTable() .
210 ' WHERE ' . $this->getIDColumn() . ' = %d', $id));
211 if ($data) {
212 $this->setData($data);
213 return true;
214 }
215 return false;
216 }
217
218 /**
219 * @return bool
220 */
221 public function save() {
222 if (!$this->dirty) {
223 return false;
224 }
225 $this->dirty = ($this->getPrimaryKey() ? $this->update() : $this->insert()) === false;
226 return !$this->dirty;
227 }
228
229 /**
230 * @return false|int
231 */
232 public function insert() {
233 $data = $this->getData();
234 unset($data[$this->getPrimaryKey()]);
235 $rowsAffected = $this->getDB()->insert($this->getTable(), $data);
236 $this->setPrimaryKey($this->getDB()->insert_id);
237 return $rowsAffected;
238 }
239
240 /**
241 * @return false|int
242 */
243 public function update() {
244 return $this->getDB()->update($this->getTable(), $this->getData(), array(
245 $this->getIDColumn() => $this->getPrimaryKey(),
246 ));
247 }
248
249 /**
250 * @param $name string
251 * @return mixed
252 */
253 public function __get($name) {
254 if (!$this->hasColumn($name)) {
255 return null;
256 }
257 return array_key_exists($name, $this->data) ? $this->data[$name] : null;
258 }
259
260 /**
261 * @param $name string
262 * @param $value mixed
263 */
264 public function __set($name, $value) {
265 if (!$this->hasColumn($name)) {
266 return;
267 }
268 $this->data[$name] = $value;
269 $this->dirty = true;
270 }
271
272 /**
273 * @return array
274 */
275 public function getData() {
276 return $this->data;
277 }
278
279 /**
280 * @param array $data
281 * @param bool $flagDirty
282 */
283 public function setData($data, $flagDirty = true) {
284 $this->data = array();
285 foreach ($data as $column => $value) {
286 if ($this->hasColumn($column)) {
287 $this->data[$column] = $value;
288 $this->dirty = (bool) $flagDirty;
289 }
290 }
291 }
292
293 /**
294 * @return wpdb
295 */
296 public function getDB() {
297 if ($this->db === null) {
298 global $wpdb;
299 $this->db = $wpdb;
300 }
301 return $this->db;
302 }
303
304 /**
305 * @param wpdb $db
306 */
307 public function setDB($db) {
308 $this->db = $db;
309 }
310
311 /**
312 * @return int
313 */
314 public function getPrimaryKey() {
315 return $this->{$this->getIDColumn()};
316 }
317
318 /**
319 * @param int $value
320 */
321 public function setPrimaryKey($value) {
322 $this->{$this->getIDColumn()} = $value;
323 }
324 }
325