Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wordfenc.../lib
File: wfDB.php
<?php
[0] Fix | Delete
class wfDB {
[1] Fix | Delete
public $errorMsg = false;
[2] Fix | Delete
[3] Fix | Delete
public static function shared() {
[4] Fix | Delete
static $_shared = null;
[5] Fix | Delete
if ($_shared === null) {
[6] Fix | Delete
$_shared = new wfDB();
[7] Fix | Delete
}
[8] Fix | Delete
return $_shared;
[9] Fix | Delete
}
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Returns the table prefix for the main site on multisites and the site itself on single site installations.
[13] Fix | Delete
*
[14] Fix | Delete
* @return string
[15] Fix | Delete
*/
[16] Fix | Delete
public static function networkPrefix() {
[17] Fix | Delete
global $wpdb;
[18] Fix | Delete
return $wpdb->base_prefix;
[19] Fix | Delete
}
[20] Fix | Delete
[21] Fix | Delete
/**
[22] Fix | Delete
* Returns the table with the site (single site installations) or network (multisite) prefix added.
[23] Fix | Delete
*
[24] Fix | Delete
* @param string $table
[25] Fix | Delete
* @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
[26] Fix | Delete
* @return string
[27] Fix | Delete
*/
[28] Fix | Delete
public static function networkTable($table, $applyCaseConversion = true) {
[29] Fix | Delete
if (wfSchema::usingLowercase() && $applyCaseConversion) {
[30] Fix | Delete
$table = strtolower($table);
[31] Fix | Delete
}
[32] Fix | Delete
return self::networkPrefix() . $table;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Returns the table prefix for the given blog ID. On single site installations, this will be equivalent to wfDB::networkPrefix().
[37] Fix | Delete
*
[38] Fix | Delete
* @param int $blogID
[39] Fix | Delete
* @return string
[40] Fix | Delete
*/
[41] Fix | Delete
public static function blogPrefix($blogID) {
[42] Fix | Delete
global $wpdb;
[43] Fix | Delete
return $wpdb->get_blog_prefix($blogID);
[44] Fix | Delete
}
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* Returns the table with the site (single site installations) or blog-specific (multisite) prefix added.
[48] Fix | Delete
*
[49] Fix | Delete
* @param string $table
[50] Fix | Delete
* @param bool $applyCaseConversion Whether or not to convert the table case to what is actually in use.
[51] Fix | Delete
* @return string
[52] Fix | Delete
*/
[53] Fix | Delete
public static function blogTable($table, $blogID, $applyCaseConversion = true) {
[54] Fix | Delete
if (wfSchema::usingLowercase() && $applyCaseConversion) {
[55] Fix | Delete
$table = strtolower($table);
[56] Fix | Delete
}
[57] Fix | Delete
return self::blogPrefix($blogID) . $table;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Converts the given value into a MySQL hex string. This is needed because WordPress will run an unnecessary `SHOW
[62] Fix | Delete
* FULL COLUMNS` on every hit where we use non-ASCII data (e.g., packed binary-encoded IP addresses) in queries.
[63] Fix | Delete
*
[64] Fix | Delete
* @param string $binary
[65] Fix | Delete
* @return string
[66] Fix | Delete
*/
[67] Fix | Delete
public static function binaryValueToSQLHex($binary) {
[68] Fix | Delete
return sprintf("X'%s'", bin2hex($binary));
[69] Fix | Delete
}
[70] Fix | Delete
[71] Fix | Delete
public function querySingle(){
[72] Fix | Delete
global $wpdb;
[73] Fix | Delete
if(func_num_args() > 1){
[74] Fix | Delete
$args = func_get_args();
[75] Fix | Delete
return $wpdb->get_var(call_user_func_array(array($wpdb, 'prepare'), $args));
[76] Fix | Delete
} else {
[77] Fix | Delete
return $wpdb->get_var(func_get_arg(0));
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
public function querySingleRec(){ //queryInSprintfFormat, arg1, arg2, ... :: Returns a single assoc-array or null if nothing found.
[81] Fix | Delete
global $wpdb;
[82] Fix | Delete
if(func_num_args() > 1){
[83] Fix | Delete
$args = func_get_args();
[84] Fix | Delete
return $wpdb->get_row(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
[85] Fix | Delete
} else {
[86] Fix | Delete
return $wpdb->get_row(func_get_arg(0), ARRAY_A);
[87] Fix | Delete
}
[88] Fix | Delete
}
[89] Fix | Delete
public function queryWrite(){
[90] Fix | Delete
global $wpdb;
[91] Fix | Delete
if(func_num_args() > 1){
[92] Fix | Delete
$args = func_get_args();
[93] Fix | Delete
return $wpdb->query(call_user_func_array(array($wpdb, 'prepare'), $args));
[94] Fix | Delete
} else {
[95] Fix | Delete
return $wpdb->query(func_get_arg(0));
[96] Fix | Delete
}
[97] Fix | Delete
}
[98] Fix | Delete
public function queryWriteArray($query, $array) {
[99] Fix | Delete
global $wpdb;
[100] Fix | Delete
return $wpdb->query($wpdb->prepare($query, $array));
[101] Fix | Delete
}
[102] Fix | Delete
public function flush(){ //Clear cache
[103] Fix | Delete
global $wpdb;
[104] Fix | Delete
$wpdb->flush();
[105] Fix | Delete
}
[106] Fix | Delete
public function querySelect(){ //sprintfString, arguments :: always returns array() and will be empty if no results.
[107] Fix | Delete
global $wpdb;
[108] Fix | Delete
if(func_num_args() > 1){
[109] Fix | Delete
$args = func_get_args();
[110] Fix | Delete
return $wpdb->get_results(call_user_func_array(array($wpdb, 'prepare'), $args), ARRAY_A);
[111] Fix | Delete
} else {
[112] Fix | Delete
return $wpdb->get_results(func_get_arg(0), ARRAY_A);
[113] Fix | Delete
}
[114] Fix | Delete
}
[115] Fix | Delete
public function queryWriteIgnoreError(){ //sprintfString, arguments
[116] Fix | Delete
global $wpdb;
[117] Fix | Delete
$oldSuppress = $wpdb->suppress_errors(true);
[118] Fix | Delete
$args = func_get_args();
[119] Fix | Delete
call_user_func_array(array($this, 'queryWrite'), $args);
[120] Fix | Delete
$wpdb->suppress_errors($oldSuppress);
[121] Fix | Delete
}
[122] Fix | Delete
public function columnExists($table, $col){
[123] Fix | Delete
$table = wfDB::networkTable($table);
[124] Fix | Delete
$q = $this->querySelect("desc $table");
[125] Fix | Delete
foreach($q as $row){
[126] Fix | Delete
if($row['Field'] == $col){
[127] Fix | Delete
return true;
[128] Fix | Delete
}
[129] Fix | Delete
}
[130] Fix | Delete
return false;
[131] Fix | Delete
}
[132] Fix | Delete
public function dropColumn($table, $col){
[133] Fix | Delete
$table = wfDB::networkTable($table);
[134] Fix | Delete
$this->queryWrite("alter table $table drop column $col");
[135] Fix | Delete
}
[136] Fix | Delete
public function createKeyIfNotExists($table, $col, $keyName){
[137] Fix | Delete
$table = wfDB::networkTable($table);
[138] Fix | Delete
[139] Fix | Delete
$exists = $this->querySingle(<<<SQL
[140] Fix | Delete
SELECT TABLE_NAME FROM information_schema.TABLES
[141] Fix | Delete
WHERE TABLE_SCHEMA=DATABASE()
[142] Fix | Delete
AND TABLE_NAME='%s'
[143] Fix | Delete
SQL
[144] Fix | Delete
, $table);
[145] Fix | Delete
$keyFound = false;
[146] Fix | Delete
if($exists){
[147] Fix | Delete
$q = $this->querySelect("show keys from $table");
[148] Fix | Delete
foreach($q as $row){
[149] Fix | Delete
if($row['Key_name'] == $keyName){
[150] Fix | Delete
$keyFound = true;
[151] Fix | Delete
}
[152] Fix | Delete
}
[153] Fix | Delete
}
[154] Fix | Delete
if(! $keyFound){
[155] Fix | Delete
$this->queryWrite("alter table $table add KEY $keyName($col)");
[156] Fix | Delete
}
[157] Fix | Delete
}
[158] Fix | Delete
public function getMaxAllowedPacketBytes(){
[159] Fix | Delete
$rec = $this->querySingleRec("show variables like 'max_allowed_packet'");
[160] Fix | Delete
return intval($rec['Value']);
[161] Fix | Delete
}
[162] Fix | Delete
public function getMaxLongDataSizeBytes() {
[163] Fix | Delete
$rec = $this->querySingleRec("show variables like 'max_long_data_size'");
[164] Fix | Delete
return $rec['Value'];
[165] Fix | Delete
}
[166] Fix | Delete
public function truncate($table){ //Ensures everything is deleted if user is using MySQL >= 5.1.16 and does not have "drop" privileges
[167] Fix | Delete
$this->queryWrite("truncate table $table");
[168] Fix | Delete
$this->queryWrite("delete from $table");
[169] Fix | Delete
}
[170] Fix | Delete
public function getLastError(){
[171] Fix | Delete
global $wpdb;
[172] Fix | Delete
return $wpdb->last_error;
[173] Fix | Delete
}
[174] Fix | Delete
public function realEscape($str){
[175] Fix | Delete
global $wpdb;
[176] Fix | Delete
return $wpdb->_real_escape($str);
[177] Fix | Delete
}
[178] Fix | Delete
public function insert($table, $columns, $rows, $updateOnDuplicate) {
[179] Fix | Delete
global $wpdb;
[180] Fix | Delete
$rowCount = count($rows);
[181] Fix | Delete
if ($rowCount === 0)
[182] Fix | Delete
return;
[183] Fix | Delete
$columnClause = implode(',', array_keys($columns));
[184] Fix | Delete
$valuesClause = ltrim(str_repeat(',(' . implode(',', $columns) . ')', $rowCount), ',');
[185] Fix | Delete
if ($updateOnDuplicate) {
[186] Fix | Delete
$duplicateClause = ' ON DUPLICATE KEY UPDATE ' . implode(',', array_map(function($column) {
[187] Fix | Delete
return "{$column} = VALUES({$column})";
[188] Fix | Delete
}, $updateOnDuplicate));
[189] Fix | Delete
}
[190] Fix | Delete
else {
[191] Fix | Delete
$duplicateClause = null;
[192] Fix | Delete
}
[193] Fix | Delete
$parameters = [];
[194] Fix | Delete
foreach ($rows as $row) {
[195] Fix | Delete
foreach ($row as $value) {
[196] Fix | Delete
$parameters[] = $value;
[197] Fix | Delete
}
[198] Fix | Delete
}
[199] Fix | Delete
$query = $wpdb->prepare("INSERT INTO {$table} ({$columnClause}) VALUES {$valuesClause}{$duplicateClause}", $parameters);
[200] Fix | Delete
$result = $wpdb->query($query);
[201] Fix | Delete
if ($result === false)
[202] Fix | Delete
throw new RuntimeException("Insert query failed: {$query}");
[203] Fix | Delete
}
[204] Fix | Delete
private static function getBindingType($value, $override = null) {
[205] Fix | Delete
if ($override !== null)
[206] Fix | Delete
return $override;
[207] Fix | Delete
if (is_int($value)) {
[208] Fix | Delete
return '%d';
[209] Fix | Delete
}
[210] Fix | Delete
else {
[211] Fix | Delete
return '%s';
[212] Fix | Delete
}
[213] Fix | Delete
}
[214] Fix | Delete
private static function buildWhereClause($conditions, $bindingOverrides, &$parameters) {
[215] Fix | Delete
$whereExpressions = [];
[216] Fix | Delete
foreach ($conditions as $column => $value) {
[217] Fix | Delete
$override = array_key_exists($column, $bindingOverrides) ? $bindingOverrides[$column] : null;
[218] Fix | Delete
if ($override === null) {
[219] Fix | Delete
$getBinding = [self::class, 'getBindingType'];
[220] Fix | Delete
}
[221] Fix | Delete
else {
[222] Fix | Delete
$getBinding = function($value) use ($override) { return $override; };
[223] Fix | Delete
}
[224] Fix | Delete
if (is_array($value)) {
[225] Fix | Delete
$whereExpressions[] = "{$column} IN (" . implode(',', array_map($getBinding, $value)) . ')';
[226] Fix | Delete
$parameters = array_merge($parameters, $value);
[227] Fix | Delete
}
[228] Fix | Delete
else {
[229] Fix | Delete
$whereExpressions[] = "{$column} = " . $getBinding($value);
[230] Fix | Delete
$parameters[] = $value;
[231] Fix | Delete
}
[232] Fix | Delete
}
[233] Fix | Delete
return implode(' AND ', $whereExpressions);
[234] Fix | Delete
}
[235] Fix | Delete
public function update($table, $set, $conditions, $bindingOverrides = []) {
[236] Fix | Delete
global $wpdb;
[237] Fix | Delete
$setExpressions = [];
[238] Fix | Delete
$parameters = [];
[239] Fix | Delete
foreach ($set as $column => $value) {
[240] Fix | Delete
if (is_array($value)) {
[241] Fix | Delete
$parameters[] = $value[1];
[242] Fix | Delete
$value = $value[0];
[243] Fix | Delete
}
[244] Fix | Delete
$setExpressions[] = "{$column} = {$value}";
[245] Fix | Delete
}
[246] Fix | Delete
$whereClause = self::buildWhereClause($conditions, $bindingOverrides, $parameters);
[247] Fix | Delete
$setClause = implode(',', $setExpressions);
[248] Fix | Delete
$query = $wpdb->prepare("UPDATE {$table} SET {$setClause} WHERE {$whereClause}", $parameters);
[249] Fix | Delete
$result = $wpdb->query($query);
[250] Fix | Delete
if ($result === false)
[251] Fix | Delete
throw new RuntimeException("UPDATE query failed: {$query}");
[252] Fix | Delete
}
[253] Fix | Delete
public function select($table, $columns, $conditions, $bindingOverrides = [], $limit = 500) {
[254] Fix | Delete
global $wpdb;
[255] Fix | Delete
$parameters = [];
[256] Fix | Delete
$selectClause = implode(',', $columns);
[257] Fix | Delete
$whereClause = Self::buildWhereClause($conditions, $bindingOverrides, $parameters);
[258] Fix | Delete
$limitClause = $limit === null ? '' : " LIMIT {$limit}";
[259] Fix | Delete
$query = $wpdb->prepare("SELECT {$selectClause} FROM {$table} WHERE {$whereClause}{$limitClause}", $parameters);
[260] Fix | Delete
if (count($columns) == 1) {
[261] Fix | Delete
$result = $wpdb->get_col($query);
[262] Fix | Delete
}
[263] Fix | Delete
else {
[264] Fix | Delete
$result = $wpdb->get_results($query, ARRAY_N);
[265] Fix | Delete
}
[266] Fix | Delete
if (!is_array($result))
[267] Fix | Delete
throw new RuntimeException("SELECT query failed: {$query}");
[268] Fix | Delete
return $result;
[269] Fix | Delete
}
[270] Fix | Delete
public function selectAll($table, $columns, $conditions, $bindingOverrides = []) {
[271] Fix | Delete
return $this->select($table, $columns, $conditions, $bindingOverrides, null);
[272] Fix | Delete
}
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
abstract class wfModel {
[276] Fix | Delete
[277] Fix | Delete
private $data;
[278] Fix | Delete
private $db;
[279] Fix | Delete
private $dirty = false;
[280] Fix | Delete
[281] Fix | Delete
/**
[282] Fix | Delete
* Column name of the primary key field.
[283] Fix | Delete
*
[284] Fix | Delete
* @return string
[285] Fix | Delete
*/
[286] Fix | Delete
abstract public function getIDColumn();
[287] Fix | Delete
[288] Fix | Delete
/**
[289] Fix | Delete
* Table name.
[290] Fix | Delete
*
[291] Fix | Delete
* @return mixed
[292] Fix | Delete
*/
[293] Fix | Delete
abstract public function getTable();
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Checks if this is a valid column in the table before setting data on the model.
[297] Fix | Delete
*
[298] Fix | Delete
* @param string $column
[299] Fix | Delete
* @return boolean
[300] Fix | Delete
*/
[301] Fix | Delete
abstract public function hasColumn($column);
[302] Fix | Delete
[303] Fix | Delete
/**
[304] Fix | Delete
* wfModel constructor.
[305] Fix | Delete
* @param array|int|string $data
[306] Fix | Delete
*/
[307] Fix | Delete
public function __construct($data = array()) {
[308] Fix | Delete
if (is_array($data) || is_object($data)) {
[309] Fix | Delete
$this->setData($data);
[310] Fix | Delete
} else if (is_numeric($data)) {
[311] Fix | Delete
$this->fetchByID($data);
[312] Fix | Delete
}
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
public function fetchByID($id) {
[316] Fix | Delete
$id = absint($id);
[317] Fix | Delete
$data = $this->getDB()->get_row($this->getDB()->prepare('SELECT * FROM ' . $this->getTable() .
[318] Fix | Delete
' WHERE ' . $this->getIDColumn() . ' = %d', $id));
[319] Fix | Delete
if ($data) {
[320] Fix | Delete
$this->setData($data);
[321] Fix | Delete
return true;
[322] Fix | Delete
}
[323] Fix | Delete
return false;
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* @return bool
[328] Fix | Delete
*/
[329] Fix | Delete
public function save() {
[330] Fix | Delete
if (!$this->dirty) {
[331] Fix | Delete
return false;
[332] Fix | Delete
}
[333] Fix | Delete
$this->dirty = ($this->getPrimaryKey() ? $this->update() : $this->insert()) === false;
[334] Fix | Delete
return !$this->dirty;
[335] Fix | Delete
}
[336] Fix | Delete
[337] Fix | Delete
/**
[338] Fix | Delete
* @return false|int
[339] Fix | Delete
*/
[340] Fix | Delete
public function insert() {
[341] Fix | Delete
$data = $this->getData();
[342] Fix | Delete
unset($data[$this->getPrimaryKey()]);
[343] Fix | Delete
$rowsAffected = $this->getDB()->insert($this->getTable(), $data);
[344] Fix | Delete
$this->setPrimaryKey($this->getDB()->insert_id);
[345] Fix | Delete
return $rowsAffected;
[346] Fix | Delete
}
[347] Fix | Delete
[348] Fix | Delete
/**
[349] Fix | Delete
* @return false|int
[350] Fix | Delete
*/
[351] Fix | Delete
public function update() {
[352] Fix | Delete
return $this->getDB()->update($this->getTable(), $this->getData(), array(
[353] Fix | Delete
$this->getIDColumn() => $this->getPrimaryKey(),
[354] Fix | Delete
));
[355] Fix | Delete
}
[356] Fix | Delete
[357] Fix | Delete
/**
[358] Fix | Delete
* @param $name string
[359] Fix | Delete
* @return mixed
[360] Fix | Delete
*/
[361] Fix | Delete
public function __get($name) {
[362] Fix | Delete
if (!$this->hasColumn($name)) {
[363] Fix | Delete
return null;
[364] Fix | Delete
}
[365] Fix | Delete
return array_key_exists($name, $this->data) ? $this->data[$name] : null;
[366] Fix | Delete
}
[367] Fix | Delete
[368] Fix | Delete
/**
[369] Fix | Delete
* @param $name string
[370] Fix | Delete
* @param $value mixed
[371] Fix | Delete
*/
[372] Fix | Delete
public function __set($name, $value) {
[373] Fix | Delete
if (!$this->hasColumn($name)) {
[374] Fix | Delete
return;
[375] Fix | Delete
}
[376] Fix | Delete
$this->data[$name] = $value;
[377] Fix | Delete
$this->dirty = true;
[378] Fix | Delete
}
[379] Fix | Delete
[380] Fix | Delete
/**
[381] Fix | Delete
* @return array
[382] Fix | Delete
*/
[383] Fix | Delete
public function getData() {
[384] Fix | Delete
return $this->data;
[385] Fix | Delete
}
[386] Fix | Delete
[387] Fix | Delete
/**
[388] Fix | Delete
* @param array $data
[389] Fix | Delete
* @param bool $flagDirty
[390] Fix | Delete
*/
[391] Fix | Delete
public function setData($data, $flagDirty = true) {
[392] Fix | Delete
$this->data = array();
[393] Fix | Delete
foreach ($data as $column => $value) {
[394] Fix | Delete
if ($this->hasColumn($column)) {
[395] Fix | Delete
$this->data[$column] = $value;
[396] Fix | Delete
$this->dirty = (bool) $flagDirty;
[397] Fix | Delete
}
[398] Fix | Delete
}
[399] Fix | Delete
}
[400] Fix | Delete
[401] Fix | Delete
/**
[402] Fix | Delete
* @return wpdb
[403] Fix | Delete
*/
[404] Fix | Delete
public function getDB() {
[405] Fix | Delete
if ($this->db === null) {
[406] Fix | Delete
global $wpdb;
[407] Fix | Delete
$this->db = $wpdb;
[408] Fix | Delete
}
[409] Fix | Delete
return $this->db;
[410] Fix | Delete
}
[411] Fix | Delete
[412] Fix | Delete
/**
[413] Fix | Delete
* @param wpdb $db
[414] Fix | Delete
*/
[415] Fix | Delete
public function setDB($db) {
[416] Fix | Delete
$this->db = $db;
[417] Fix | Delete
}
[418] Fix | Delete
[419] Fix | Delete
/**
[420] Fix | Delete
* @return int
[421] Fix | Delete
*/
[422] Fix | Delete
public function getPrimaryKey() {
[423] Fix | Delete
return $this->{$this->getIDColumn()};
[424] Fix | Delete
}
[425] Fix | Delete
[426] Fix | Delete
/**
[427] Fix | Delete
* @param int $value
[428] Fix | Delete
*/
[429] Fix | Delete
public function setPrimaryKey($value) {
[430] Fix | Delete
$this->{$this->getIDColumn()} = $value;
[431] Fix | Delete
}
[432] Fix | Delete
}
[433] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function