Edit File by line
/home/zeestwma/redstone.../wp-inclu.../SimplePi.../src/Cache
File: MySQL.php
<?php
[0] Fix | Delete
[1] Fix | Delete
// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
[2] Fix | Delete
// SPDX-License-Identifier: BSD-3-Clause
[3] Fix | Delete
[4] Fix | Delete
declare(strict_types=1);
[5] Fix | Delete
[6] Fix | Delete
namespace SimplePie\Cache;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Caches data to a MySQL database
[10] Fix | Delete
*
[11] Fix | Delete
* Registered for URLs with the "mysql" protocol
[12] Fix | Delete
*
[13] Fix | Delete
* For example, `mysql://root:password@localhost:3306/mydb?prefix=sp_` will
[14] Fix | Delete
* connect to the `mydb` database on `localhost` on port 3306, with the user
[15] Fix | Delete
* `root` and the password `password`. All tables will be prefixed with `sp_`
[16] Fix | Delete
*
[17] Fix | Delete
* @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
[18] Fix | Delete
*/
[19] Fix | Delete
class MySQL extends DB
[20] Fix | Delete
{
[21] Fix | Delete
/**
[22] Fix | Delete
* PDO instance
[23] Fix | Delete
*
[24] Fix | Delete
* @var \PDO|null
[25] Fix | Delete
*/
[26] Fix | Delete
protected $mysql;
[27] Fix | Delete
[28] Fix | Delete
/**
[29] Fix | Delete
* Options
[30] Fix | Delete
*
[31] Fix | Delete
* @var array<string, mixed>
[32] Fix | Delete
*/
[33] Fix | Delete
protected $options;
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Cache ID
[37] Fix | Delete
*
[38] Fix | Delete
* @var string
[39] Fix | Delete
*/
[40] Fix | Delete
protected $id;
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Create a new cache object
[44] Fix | Delete
*
[45] Fix | Delete
* @param string $location Location string (from SimplePie::$cache_location)
[46] Fix | Delete
* @param string $name Unique ID for the cache
[47] Fix | Delete
* @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
[48] Fix | Delete
*/
[49] Fix | Delete
public function __construct(string $location, string $name, $type)
[50] Fix | Delete
{
[51] Fix | Delete
$this->options = [
[52] Fix | Delete
'user' => null,
[53] Fix | Delete
'pass' => null,
[54] Fix | Delete
'host' => '127.0.0.1',
[55] Fix | Delete
'port' => '3306',
[56] Fix | Delete
'path' => '',
[57] Fix | Delete
'extras' => [
[58] Fix | Delete
'prefix' => '',
[59] Fix | Delete
'cache_purge_time' => 2592000
[60] Fix | Delete
],
[61] Fix | Delete
];
[62] Fix | Delete
[63] Fix | Delete
$this->options = array_replace_recursive($this->options, \SimplePie\Cache::parse_URL($location));
[64] Fix | Delete
[65] Fix | Delete
// Path is prefixed with a "/"
[66] Fix | Delete
$this->options['dbname'] = substr($this->options['path'], 1);
[67] Fix | Delete
[68] Fix | Delete
try {
[69] Fix | Delete
$this->mysql = new \PDO("mysql:dbname={$this->options['dbname']};host={$this->options['host']};port={$this->options['port']}", $this->options['user'], $this->options['pass'], [\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
[70] Fix | Delete
} catch (\PDOException $e) {
[71] Fix | Delete
$this->mysql = null;
[72] Fix | Delete
return;
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
$this->id = $name . $type;
[76] Fix | Delete
[77] Fix | Delete
if (!$query = $this->mysql->query('SHOW TABLES')) {
[78] Fix | Delete
$this->mysql = null;
[79] Fix | Delete
return;
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
$db = [];
[83] Fix | Delete
while ($row = $query->fetchColumn()) {
[84] Fix | Delete
$db[] = $row;
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
if (!in_array($this->options['extras']['prefix'] . 'cache_data', $db)) {
[88] Fix | Delete
$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'cache_data` (`id` TEXT CHARACTER SET utf8 NOT NULL, `items` SMALLINT NOT NULL DEFAULT 0, `data` BLOB NOT NULL, `mtime` INT UNSIGNED NOT NULL, UNIQUE (`id`(125)))');
[89] Fix | Delete
if ($query === false) {
[90] Fix | Delete
trigger_error("Can't create " . $this->options['extras']['prefix'] . "cache_data table, check permissions", \E_USER_WARNING);
[91] Fix | Delete
$this->mysql = null;
[92] Fix | Delete
return;
[93] Fix | Delete
}
[94] Fix | Delete
}
[95] Fix | Delete
[96] Fix | Delete
if (!in_array($this->options['extras']['prefix'] . 'items', $db)) {
[97] Fix | Delete
$query = $this->mysql->exec('CREATE TABLE `' . $this->options['extras']['prefix'] . 'items` (`feed_id` TEXT CHARACTER SET utf8 NOT NULL, `id` TEXT CHARACTER SET utf8 NOT NULL, `data` MEDIUMBLOB NOT NULL, `posted` INT UNSIGNED NOT NULL, INDEX `feed_id` (`feed_id`(125)))');
[98] Fix | Delete
if ($query === false) {
[99] Fix | Delete
trigger_error("Can't create " . $this->options['extras']['prefix'] . "items table, check permissions", \E_USER_WARNING);
[100] Fix | Delete
$this->mysql = null;
[101] Fix | Delete
return;
[102] Fix | Delete
}
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Save data to the cache
[108] Fix | Delete
*
[109] Fix | Delete
* @param array<string>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
[110] Fix | Delete
* @return bool Successfulness
[111] Fix | Delete
*/
[112] Fix | Delete
public function save($data)
[113] Fix | Delete
{
[114] Fix | Delete
if ($this->mysql === null) {
[115] Fix | Delete
return false;
[116] Fix | Delete
}
[117] Fix | Delete
[118] Fix | Delete
$query = $this->mysql->prepare('DELETE i, cd FROM `' . $this->options['extras']['prefix'] . 'cache_data` cd, ' .
[119] Fix | Delete
'`' . $this->options['extras']['prefix'] . 'items` i ' .
[120] Fix | Delete
'WHERE cd.id = i.feed_id ' .
[121] Fix | Delete
'AND cd.mtime < (unix_timestamp() - :purge_time)');
[122] Fix | Delete
$query->bindValue(':purge_time', $this->options['extras']['cache_purge_time']);
[123] Fix | Delete
[124] Fix | Delete
if (!$query->execute()) {
[125] Fix | Delete
return false;
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
if ($data instanceof \SimplePie\SimplePie) {
[129] Fix | Delete
$data = clone $data;
[130] Fix | Delete
[131] Fix | Delete
$prepared = self::prepare_simplepie_object_for_cache($data);
[132] Fix | Delete
[133] Fix | Delete
$query = $this->mysql->prepare('SELECT COUNT(*) FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
[134] Fix | Delete
$query->bindValue(':feed', $this->id);
[135] Fix | Delete
if ($query->execute()) {
[136] Fix | Delete
if ($query->fetchColumn() > 0) {
[137] Fix | Delete
$items = count($prepared[1]);
[138] Fix | Delete
if ($items) {
[139] Fix | Delete
$sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed';
[140] Fix | Delete
$query = $this->mysql->prepare($sql);
[141] Fix | Delete
$query->bindValue(':items', $items);
[142] Fix | Delete
} else {
[143] Fix | Delete
$sql = 'UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
[144] Fix | Delete
$query = $this->mysql->prepare($sql);
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
$query->bindValue(':data', $prepared[0]);
[148] Fix | Delete
$query->bindValue(':time', time());
[149] Fix | Delete
$query->bindValue(':feed', $this->id);
[150] Fix | Delete
if (!$query->execute()) {
[151] Fix | Delete
return false;
[152] Fix | Delete
}
[153] Fix | Delete
} else {
[154] Fix | Delete
$query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)');
[155] Fix | Delete
$query->bindValue(':feed', $this->id);
[156] Fix | Delete
$query->bindValue(':count', count($prepared[1]));
[157] Fix | Delete
$query->bindValue(':data', $prepared[0]);
[158] Fix | Delete
$query->bindValue(':time', time());
[159] Fix | Delete
if (!$query->execute()) {
[160] Fix | Delete
return false;
[161] Fix | Delete
}
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
$ids = array_keys($prepared[1]);
[165] Fix | Delete
if (!empty($ids)) {
[166] Fix | Delete
foreach ($ids as $id) {
[167] Fix | Delete
$database_ids[] = $this->mysql->quote($id);
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
$query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `id` = ' . implode(' OR `id` = ', $database_ids) . ' AND `feed_id` = :feed');
[171] Fix | Delete
$query->bindValue(':feed', $this->id);
[172] Fix | Delete
[173] Fix | Delete
if ($query->execute()) {
[174] Fix | Delete
$existing_ids = [];
[175] Fix | Delete
while ($row = $query->fetchColumn()) {
[176] Fix | Delete
$existing_ids[] = $row;
[177] Fix | Delete
}
[178] Fix | Delete
[179] Fix | Delete
$new_ids = array_diff($ids, $existing_ids);
[180] Fix | Delete
[181] Fix | Delete
foreach ($new_ids as $new_id) {
[182] Fix | Delete
if (!($date = $prepared[1][$new_id]->get_date('U'))) {
[183] Fix | Delete
$date = time();
[184] Fix | Delete
}
[185] Fix | Delete
[186] Fix | Delete
$query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)');
[187] Fix | Delete
$query->bindValue(':feed', $this->id);
[188] Fix | Delete
$query->bindValue(':id', $new_id);
[189] Fix | Delete
$query->bindValue(':data', serialize($prepared[1][$new_id]->data));
[190] Fix | Delete
$query->bindValue(':date', $date);
[191] Fix | Delete
if (!$query->execute()) {
[192] Fix | Delete
return false;
[193] Fix | Delete
}
[194] Fix | Delete
}
[195] Fix | Delete
return true;
[196] Fix | Delete
}
[197] Fix | Delete
} else {
[198] Fix | Delete
return true;
[199] Fix | Delete
}
[200] Fix | Delete
}
[201] Fix | Delete
} else {
[202] Fix | Delete
$query = $this->mysql->prepare('SELECT `id` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
[203] Fix | Delete
$query->bindValue(':feed', $this->id);
[204] Fix | Delete
if ($query->execute()) {
[205] Fix | Delete
if ($query->rowCount() > 0) {
[206] Fix | Delete
$query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed');
[207] Fix | Delete
$query->bindValue(':data', serialize($data));
[208] Fix | Delete
$query->bindValue(':time', time());
[209] Fix | Delete
$query->bindValue(':feed', $this->id);
[210] Fix | Delete
if ($query->execute()) {
[211] Fix | Delete
return true;
[212] Fix | Delete
}
[213] Fix | Delete
} else {
[214] Fix | Delete
$query = $this->mysql->prepare('INSERT INTO `' . $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)');
[215] Fix | Delete
$query->bindValue(':id', $this->id);
[216] Fix | Delete
$query->bindValue(':data', serialize($data));
[217] Fix | Delete
$query->bindValue(':time', time());
[218] Fix | Delete
if ($query->execute()) {
[219] Fix | Delete
return true;
[220] Fix | Delete
}
[221] Fix | Delete
}
[222] Fix | Delete
}
[223] Fix | Delete
}
[224] Fix | Delete
return false;
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
/**
[228] Fix | Delete
* Retrieve the data saved to the cache
[229] Fix | Delete
*
[230] Fix | Delete
* @return array<string>|false Data for SimplePie::$data
[231] Fix | Delete
*/
[232] Fix | Delete
public function load()
[233] Fix | Delete
{
[234] Fix | Delete
if ($this->mysql === null) {
[235] Fix | Delete
return false;
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
$query = $this->mysql->prepare('SELECT `items`, `data` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
[239] Fix | Delete
$query->bindValue(':id', $this->id);
[240] Fix | Delete
if ($query->execute() && ($row = $query->fetch())) {
[241] Fix | Delete
$data = unserialize($row[1]);
[242] Fix | Delete
[243] Fix | Delete
if (isset($this->options['items'][0])) {
[244] Fix | Delete
$items = (int) $this->options['items'][0];
[245] Fix | Delete
} else {
[246] Fix | Delete
$items = (int) $row[0];
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
if ($items !== 0) {
[250] Fix | Delete
if (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0])) {
[251] Fix | Delete
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0];
[252] Fix | Delete
} elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0])) {
[253] Fix | Delete
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0];
[254] Fix | Delete
} elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0])) {
[255] Fix | Delete
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0];
[256] Fix | Delete
} elseif (isset($data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0])) {
[257] Fix | Delete
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0];
[258] Fix | Delete
} else {
[259] Fix | Delete
$feed = null;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
if ($feed !== null) {
[263] Fix | Delete
$sql = 'SELECT `data` FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
[264] Fix | Delete
if ($items > 0) {
[265] Fix | Delete
$sql .= ' LIMIT ' . $items;
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
$query = $this->mysql->prepare($sql);
[269] Fix | Delete
$query->bindValue(':feed', $this->id);
[270] Fix | Delete
if ($query->execute()) {
[271] Fix | Delete
while ($row = $query->fetchColumn()) {
[272] Fix | Delete
$feed['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry'][] = unserialize((string) $row);
[273] Fix | Delete
}
[274] Fix | Delete
} else {
[275] Fix | Delete
return false;
[276] Fix | Delete
}
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
return $data;
[280] Fix | Delete
}
[281] Fix | Delete
return false;
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
/**
[285] Fix | Delete
* Retrieve the last modified time for the cache
[286] Fix | Delete
*
[287] Fix | Delete
* @return int|false Timestamp
[288] Fix | Delete
*/
[289] Fix | Delete
public function mtime()
[290] Fix | Delete
{
[291] Fix | Delete
if ($this->mysql === null) {
[292] Fix | Delete
return false;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
$query = $this->mysql->prepare('SELECT `mtime` FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
[296] Fix | Delete
$query->bindValue(':id', $this->id);
[297] Fix | Delete
if ($query->execute() && ($time = $query->fetchColumn())) {
[298] Fix | Delete
return (int) $time;
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
return false;
[302] Fix | Delete
}
[303] Fix | Delete
[304] Fix | Delete
/**
[305] Fix | Delete
* Set the last modified time to the current time
[306] Fix | Delete
*
[307] Fix | Delete
* @return bool Success status
[308] Fix | Delete
*/
[309] Fix | Delete
public function touch()
[310] Fix | Delete
{
[311] Fix | Delete
if ($this->mysql === null) {
[312] Fix | Delete
return false;
[313] Fix | Delete
}
[314] Fix | Delete
[315] Fix | Delete
$query = $this->mysql->prepare('UPDATE `' . $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
[316] Fix | Delete
$query->bindValue(':time', time());
[317] Fix | Delete
$query->bindValue(':id', $this->id);
[318] Fix | Delete
[319] Fix | Delete
return $query->execute() && $query->rowCount() > 0;
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
/**
[323] Fix | Delete
* Remove the cache
[324] Fix | Delete
*
[325] Fix | Delete
* @return bool Success status
[326] Fix | Delete
*/
[327] Fix | Delete
public function unlink()
[328] Fix | Delete
{
[329] Fix | Delete
if ($this->mysql === null) {
[330] Fix | Delete
return false;
[331] Fix | Delete
}
[332] Fix | Delete
[333] Fix | Delete
$query = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
[334] Fix | Delete
$query->bindValue(':id', $this->id);
[335] Fix | Delete
$query2 = $this->mysql->prepare('DELETE FROM `' . $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
[336] Fix | Delete
$query2->bindValue(':id', $this->id);
[337] Fix | Delete
[338] Fix | Delete
return $query->execute() && $query2->execute();
[339] Fix | Delete
}
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
class_alias('SimplePie\Cache\MySQL', 'SimplePie_Cache_MySQL');
[343] Fix | Delete
[344] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function