Diff
8 years ago
dashboard
7 years ago
.htaccess
8 years ago
Diff.php
14 years ago
GeoLite2-Country.mmdb
7 years ago
IPTraf.php
8 years ago
IPTrafList.php
8 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
live_activity.php
8 years ago
menu_dashboard.php
8 years ago
menu_dashboard_options.php
8 years ago
menu_firewall.php
8 years ago
menu_firewall_blocking.php
7 years ago
menu_firewall_blocking_options.php
8 years ago
menu_firewall_waf.php
8 years ago
menu_firewall_waf_options.php
8 years ago
menu_options.php
7 years ago
menu_scanner.php
8 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
8 years ago
menu_tools_commentSpam.php
8 years ago
menu_tools_diagnostic.php
7 years ago
menu_tools_livetraffic.php
7 years ago
menu_tools_twoFactor.php
8 years ago
menu_tools_whois.php
8 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
8 years ago
wfBulkCountries.php
8 years ago
wfCache.php
9 years ago
wfConfig.php
7 years ago
wfCountryMap.php
8 years ago
wfCrawl.php
8 years ago
wfCredentialsController.php
8 years ago
wfCrypt.php
8 years ago
wfDB.php
8 years ago
wfDashboard.php
8 years ago
wfDateLocalization.php
8 years ago
wfDiagnostic.php
8 years ago
wfDict.php
8 years ago
wfDirectoryIterator.php
7 years ago
wfHelperBin.php
11 years ago
wfHelperString.php
11 years ago
wfIPWhitelist.php
9 years ago
wfImportExportController.php
8 years ago
wfIssues.php
7 years ago
wfLockedOut.php
7 years ago
wfLog.php
7 years ago
wfMD5BloomFilter.php
8 years ago
wfNotification.php
8 years ago
wfOnboardingController.php
7 years ago
wfPersistenceController.php
8 years ago
wfRESTAPI.php
9 years ago
wfScan.php
7 years ago
wfScanEngine.php
7 years ago
wfSchema.php
7 years ago
wfStyle.php
8 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
8 years ago
wordfenceScanner.php
7 years ago
wordfenceURLHoover.php
7 years ago
wfDB.php
311 lines
| 1 | <?php |
| 2 | class wfDB { |
| 3 | public $errorMsg = false; |
| 4 | |
| 5 | /** |
| 6 | * Returns the table prefix for the main site on multisites and the site itself on single site installations. |
| 7 | * |
| 8 | * @return string |
| 9 | */ |
| 10 | public static function networkPrefix() { |
| 11 | global $wpdb; |
| 12 | return $wpdb->base_prefix; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Returns the table with the site (single site installations) or network (multisite) prefix added. |
| 17 | * |
| 18 | * @param string $table |
| 19 | * @return string |
| 20 | */ |
| 21 | public static function networkTable($table) { |
| 22 | return self::networkPrefix() . $table; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Returns the table prefix for the given blog ID. On single site installations, this will be equivalent to wfDB::networkPrefix(). |
| 27 | * |
| 28 | * @param int $blogID |
| 29 | * @return string |
| 30 | */ |
| 31 | public static function blogPrefix($blogID) { |
| 32 | global $wpdb; |
| 33 | return $wpdb->get_blog_prefix($blogID); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Returns the table with the site (single site installations) or blog-specific (multisite) prefix added. |
| 38 | * |
| 39 | * @param string $table |
| 40 | * @return string |
| 41 | */ |
| 42 | public static function blogTable($table, $blogID) { |
| 43 | return self::blogPrefix($blogID) . $table; |
| 44 | } |
| 45 | |
| 46 | public function __construct(){ |
| 47 | } |
| 48 | public function querySingle(){ |
| 49 | global $wpdb; |
| 50 | if(func_num_args() > 1){ |
| 51 | $args = func_get_args(); |
| 52 | return $wpdb->get_var(call_user_func_array(array($wpdb, 'prepare'), $args)); |
| 53 | } else { |
| 54 | return $wpdb->get_var(func_get_arg(0)); |
| 55 | } |
| 56 | } |
| 57 | public function querySingleRec(){ //queryInSprintfFormat, arg1, arg2, ... :: Returns a single assoc-array or null if nothing found. |
| 58 | global $wpdb; |
| 59 | if(func_num_args() > 1){ |
| 60 | $args = func_get_args(); |
| 61 | return $wpdb->get_row(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A); |
| 62 | } else { |
| 63 | return $wpdb->get_row(func_get_arg(0), ARRAY_A); |
| 64 | } |
| 65 | } |
| 66 | public function queryWrite(){ |
| 67 | global $wpdb; |
| 68 | if(func_num_args() > 1){ |
| 69 | $args = func_get_args(); |
| 70 | return $wpdb->query(call_user_func_array(array($wpdb, 'prepare'), $args)); |
| 71 | } else { |
| 72 | return $wpdb->query(func_get_arg(0)); |
| 73 | } |
| 74 | } |
| 75 | public function flush(){ //Clear cache |
| 76 | global $wpdb; |
| 77 | $wpdb->flush(); |
| 78 | } |
| 79 | public function querySelect(){ //sprintfString, arguments :: always returns array() and will be empty if no results. |
| 80 | global $wpdb; |
| 81 | if(func_num_args() > 1){ |
| 82 | $args = func_get_args(); |
| 83 | return $wpdb->get_results(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A); |
| 84 | } else { |
| 85 | return $wpdb->get_results(func_get_arg(0), ARRAY_A); |
| 86 | } |
| 87 | } |
| 88 | public function queryWriteIgnoreError(){ //sprintfString, arguments |
| 89 | global $wpdb; |
| 90 | $oldSuppress = $wpdb->suppress_errors(true); |
| 91 | $args = func_get_args(); |
| 92 | call_user_func_array(array($this, 'queryWrite'), $args); |
| 93 | $wpdb->suppress_errors($oldSuppress); |
| 94 | } |
| 95 | public function columnExists($table, $col){ |
| 96 | $table = wfDB::networkTable($table); |
| 97 | $q = $this->querySelect("desc $table"); |
| 98 | foreach($q as $row){ |
| 99 | if($row['Field'] == $col){ |
| 100 | return true; |
| 101 | } |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | public function dropColumn($table, $col){ |
| 106 | $table = wfDB::networkTable($table); |
| 107 | $this->queryWrite("alter table $table drop column $col"); |
| 108 | } |
| 109 | public function createKeyIfNotExists($table, $col, $keyName){ |
| 110 | $table = wfDB::networkTable($table); |
| 111 | |
| 112 | $exists = $this->querySingle(<<<SQL |
| 113 | SELECT TABLE_NAME FROM information_schema.TABLES |
| 114 | WHERE TABLE_SCHEMA=DATABASE() |
| 115 | AND TABLE_NAME='%s' |
| 116 | SQL |
| 117 | , $table); |
| 118 | $keyFound = false; |
| 119 | if($exists){ |
| 120 | $q = $this->querySelect("show keys from $table"); |
| 121 | foreach($q as $row){ |
| 122 | if($row['Key_name'] == $keyName){ |
| 123 | $keyFound = true; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | if(! $keyFound){ |
| 128 | $this->queryWrite("alter table $table add KEY $keyName($col)"); |
| 129 | } |
| 130 | } |
| 131 | public function getMaxAllowedPacketBytes(){ |
| 132 | $rec = $this->querySingleRec("show variables like 'max_allowed_packet'"); |
| 133 | return intval($rec['Value']); |
| 134 | } |
| 135 | public function getMaxLongDataSizeBytes() { |
| 136 | $rec = $this->querySingleRec("show variables like 'max_long_data_size'"); |
| 137 | return $rec['Value']; |
| 138 | } |
| 139 | public function truncate($table){ //Ensures everything is deleted if user is using MySQL >= 5.1.16 and does not have "drop" privileges |
| 140 | $this->queryWrite("truncate table $table"); |
| 141 | $this->queryWrite("delete from $table"); |
| 142 | } |
| 143 | public function getLastError(){ |
| 144 | global $wpdb; |
| 145 | return $wpdb->last_error; |
| 146 | } |
| 147 | public function realEscape($str){ |
| 148 | global $wpdb; |
| 149 | return $wpdb->_real_escape($str); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | abstract class wfModel { |
| 154 | |
| 155 | private $data; |
| 156 | private $db; |
| 157 | private $dirty = false; |
| 158 | |
| 159 | /** |
| 160 | * Column name of the primary key field. |
| 161 | * |
| 162 | * @return string |
| 163 | */ |
| 164 | abstract public function getIDColumn(); |
| 165 | |
| 166 | /** |
| 167 | * Table name. |
| 168 | * |
| 169 | * @return mixed |
| 170 | */ |
| 171 | abstract public function getTable(); |
| 172 | |
| 173 | /** |
| 174 | * Checks if this is a valid column in the table before setting data on the model. |
| 175 | * |
| 176 | * @param string $column |
| 177 | * @return boolean |
| 178 | */ |
| 179 | abstract public function hasColumn($column); |
| 180 | |
| 181 | /** |
| 182 | * wfModel constructor. |
| 183 | * @param array|int|string $data |
| 184 | */ |
| 185 | public function __construct($data = array()) { |
| 186 | if (is_array($data) || is_object($data)) { |
| 187 | $this->setData($data); |
| 188 | } else if (is_numeric($data)) { |
| 189 | $this->fetchByID($data); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | public function fetchByID($id) { |
| 194 | $id = absint($id); |
| 195 | $data = $this->getDB()->get_row($this->getDB()->prepare('SELECT * FROM ' . $this->getTable() . |
| 196 | ' WHERE ' . $this->getIDColumn() . ' = %d', $id)); |
| 197 | if ($data) { |
| 198 | $this->setData($data); |
| 199 | return true; |
| 200 | } |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @return bool |
| 206 | */ |
| 207 | public function save() { |
| 208 | if (!$this->dirty) { |
| 209 | return false; |
| 210 | } |
| 211 | $this->dirty = ($this->getPrimaryKey() ? $this->update() : $this->insert()) === false; |
| 212 | return !$this->dirty; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * @return false|int |
| 217 | */ |
| 218 | public function insert() { |
| 219 | $data = $this->getData(); |
| 220 | unset($data[$this->getPrimaryKey()]); |
| 221 | $rowsAffected = $this->getDB()->insert($this->getTable(), $data); |
| 222 | $this->setPrimaryKey($this->getDB()->insert_id); |
| 223 | return $rowsAffected; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * @return false|int |
| 228 | */ |
| 229 | public function update() { |
| 230 | return $this->getDB()->update($this->getTable(), $this->getData(), array( |
| 231 | $this->getIDColumn() => $this->getPrimaryKey(), |
| 232 | )); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * @param $name string |
| 237 | * @return mixed |
| 238 | */ |
| 239 | public function __get($name) { |
| 240 | if (!$this->hasColumn($name)) { |
| 241 | return null; |
| 242 | } |
| 243 | return array_key_exists($name, $this->data) ? $this->data[$name] : null; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param $name string |
| 248 | * @param $value mixed |
| 249 | */ |
| 250 | public function __set($name, $value) { |
| 251 | if (!$this->hasColumn($name)) { |
| 252 | return; |
| 253 | } |
| 254 | $this->data[$name] = $value; |
| 255 | $this->dirty = true; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @return array |
| 260 | */ |
| 261 | public function getData() { |
| 262 | return $this->data; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @param array $data |
| 267 | * @param bool $flagDirty |
| 268 | */ |
| 269 | public function setData($data, $flagDirty = true) { |
| 270 | $this->data = array(); |
| 271 | foreach ($data as $column => $value) { |
| 272 | if ($this->hasColumn($column)) { |
| 273 | $this->data[$column] = $value; |
| 274 | $this->dirty = (bool) $flagDirty; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return wpdb |
| 281 | */ |
| 282 | public function getDB() { |
| 283 | if ($this->db === null) { |
| 284 | global $wpdb; |
| 285 | $this->db = $wpdb; |
| 286 | } |
| 287 | return $this->db; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param wpdb $db |
| 292 | */ |
| 293 | public function setDB($db) { |
| 294 | $this->db = $db; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * @return int |
| 299 | */ |
| 300 | public function getPrimaryKey() { |
| 301 | return $this->{$this->getIDColumn()}; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * @param int $value |
| 306 | */ |
| 307 | public function setPrimaryKey($value) { |
| 308 | $this->{$this->getIDColumn()} = $value; |
| 309 | } |
| 310 | } |
| 311 |