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 |