/var/www/html_nl/wp-includes/SimplePie/src/Cache/MySQL.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php

// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
// SPDX-License-Identifier: BSD-3-Clause

declare(strict_types=1);

namespace 
SimplePie\Cache;

/**
 * Caches data to a MySQL database
 *
 * Registered for URLs with the "mysql" protocol
 *
 * For example, `mysql://root:password@localhost:3306/mydb?prefix=sp_` will
 * connect to the `mydb` database on `localhost` on port 3306, with the user
 * `root` and the password `password`. All tables will be prefixed with `sp_`
 *
 * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
 */
class MySQL extends DB
{
    
/**
     * PDO instance
     *
     * @var \PDO|null
     */
    
protected $mysql;

    
/**
     * Options
     *
     * @var array<string, mixed>
     */
    
protected $options;

    
/**
     * Cache ID
     *
     * @var string
     */
    
protected $id;

    
/**
     * Create a new cache object
     *
     * @param string $location Location string (from SimplePie::$cache_location)
     * @param string $name Unique ID for the cache
     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
     */
    
public function __construct(string $locationstring $name$type)
    {
        
$this->options = [
            
'user' => null,
            
'pass' => null,
            
'host' => '127.0.0.1',
            
'port' => '3306',
            
'path' => '',
            
'extras' => [
                
'prefix' => '',
                
'cache_purge_time' => 2592000
            
],
        ];

        
$this->options array_replace_recursive($this->options\SimplePie\Cache::parse_URL($location));

        
// Path is prefixed with a "/"
        
$this->options['dbname'] = substr($this->options['path'], 1);

        try {
            
$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']);
        } catch (
\PDOException $e) {
            
$this->mysql null;
            return;
        }

        
$this->id $name $type;

        if (!
$query $this->mysql->query('SHOW TABLES')) {
            
$this->mysql null;
            return;
        }

        
$db = [];
        while (
$row $query->fetchColumn()) {
            
$db[] = $row;
        }

        if (!
in_array($this->options['extras']['prefix'] . 'cache_data'$db)) {
            
$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)))');
            if (
$query === false) {
                
trigger_error("Can't create " $this->options['extras']['prefix'] . "cache_data table, check permissions"\E_USER_WARNING);
                
$this->mysql null;
                return;
            }
        }

        if (!
in_array($this->options['extras']['prefix'] . 'items'$db)) {
            
$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)))');
            if (
$query === false) {
                
trigger_error("Can't create " $this->options['extras']['prefix'] . "items table, check permissions"\E_USER_WARNING);
                
$this->mysql null;
                return;
            }
        }
    }

    
/**
     * Save data to the cache
     *
     * @param array<string>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
     * @return bool Successfulness
     */
    
public function save($data)
    {
        if (
$this->mysql === null) {
            return 
false;
        }

        
$query $this->mysql->prepare('DELETE i, cd FROM `' $this->options['extras']['prefix'] . 'cache_data` cd, ' .
            
'`' $this->options['extras']['prefix'] . 'items` i ' .
            
'WHERE cd.id = i.feed_id ' .
            
'AND cd.mtime < (unix_timestamp() - :purge_time)');
        
$query->bindValue(':purge_time'$this->options['extras']['cache_purge_time']);

        if (!
$query->execute()) {
            return 
false;
        }

        if (
$data instanceof \SimplePie\SimplePie) {
            
$data = clone $data;

            
$prepared self::prepare_simplepie_object_for_cache($data);

            
$query $this->mysql->prepare('SELECT COUNT(*) FROM `' $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
            
$query->bindValue(':feed'$this->id);
            if (
$query->execute()) {
                if (
$query->fetchColumn() > 0) {
                    
$items count($prepared[1]);
                    if (
$items) {
                        
$sql 'UPDATE `' $this->options['extras']['prefix'] . 'cache_data` SET `items` = :items, `data` = :data, `mtime` = :time WHERE `id` = :feed';
                        
$query $this->mysql->prepare($sql);
                        
$query->bindValue(':items'$items);
                    } else {
                        
$sql 'UPDATE `' $this->options['extras']['prefix'] . 'cache_data` SET `data` = :data, `mtime` = :time WHERE `id` = :feed';
                        
$query $this->mysql->prepare($sql);
                    }

                    
$query->bindValue(':data'$prepared[0]);
                    
$query->bindValue(':time'time());
                    
$query->bindValue(':feed'$this->id);
                    if (!
$query->execute()) {
                        return 
false;
                    }
                } else {
                    
$query $this->mysql->prepare('INSERT INTO `' $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:feed, :count, :data, :time)');
                    
$query->bindValue(':feed'$this->id);
                    
$query->bindValue(':count'count($prepared[1]));
                    
$query->bindValue(':data'$prepared[0]);
                    
$query->bindValue(':time'time());
                    if (!
$query->execute()) {
                        return 
false;
                    }
                }

                
$ids array_keys($prepared[1]);
                if (!empty(
$ids)) {
                    foreach (
$ids as $id) {
                        
$database_ids[] = $this->mysql->quote($id);
                    }

                    
$query $this->mysql->prepare('SELECT `id` FROM `' $this->options['extras']['prefix'] . 'items` WHERE `id` = ' implode(' OR `id` = '$database_ids) . ' AND `feed_id` = :feed');
                    
$query->bindValue(':feed'$this->id);

                    if (
$query->execute()) {
                        
$existing_ids = [];
                        while (
$row $query->fetchColumn()) {
                            
$existing_ids[] = $row;
                        }

                        
$new_ids array_diff($ids$existing_ids);

                        foreach (
$new_ids as $new_id) {
                            if (!(
$date $prepared[1][$new_id]->get_date('U'))) {
                                
$date time();
                            }

                            
$query $this->mysql->prepare('INSERT INTO `' $this->options['extras']['prefix'] . 'items` (`feed_id`, `id`, `data`, `posted`) VALUES(:feed, :id, :data, :date)');
                            
$query->bindValue(':feed'$this->id);
                            
$query->bindValue(':id'$new_id);
                            
$query->bindValue(':data'serialize($prepared[1][$new_id]->data));
                            
$query->bindValue(':date'$date);
                            if (!
$query->execute()) {
                                return 
false;
                            }
                        }
                        return 
true;
                    }
                } else {
                    return 
true;
                }
            }
        } else {
            
$query $this->mysql->prepare('SELECT `id` FROM `' $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :feed');
            
$query->bindValue(':feed'$this->id);
            if (
$query->execute()) {
                if (
$query->rowCount() > 0) {
                    
$query $this->mysql->prepare('UPDATE `' $this->options['extras']['prefix'] . 'cache_data` SET `items` = 0, `data` = :data, `mtime` = :time WHERE `id` = :feed');
                    
$query->bindValue(':data'serialize($data));
                    
$query->bindValue(':time'time());
                    
$query->bindValue(':feed'$this->id);
                    if (
$query->execute()) {
                        return 
true;
                    }
                } else {
                    
$query $this->mysql->prepare('INSERT INTO `' $this->options['extras']['prefix'] . 'cache_data` (`id`, `items`, `data`, `mtime`) VALUES(:id, 0, :data, :time)');
                    
$query->bindValue(':id'$this->id);
                    
$query->bindValue(':data'serialize($data));
                    
$query->bindValue(':time'time());
                    if (
$query->execute()) {
                        return 
true;
                    }
                }
            }
        }
        return 
false;
    }

    
/**
     * Retrieve the data saved to the cache
     *
     * @return array<string>|false Data for SimplePie::$data
     */
    
public function load()
    {
        if (
$this->mysql === null) {
            return 
false;
        }

        
$query $this->mysql->prepare('SELECT `items`, `data` FROM `' $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
        
$query->bindValue(':id'$this->id);
        if (
$query->execute() && ($row $query->fetch())) {
            
$data unserialize($row[1]);

            if (isset(
$this->options['items'][0])) {
                
$items = (int) $this->options['items'][0];
            } else {
                
$items = (int) $row[0];
            }

            if (
$items !== 0) {
                if (isset(
$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0])) {
                    
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['feed'][0];
                } elseif (isset(
$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0])) {
                    
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_03]['feed'][0];
                } elseif (isset(
$data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0])) {
                    
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RDF]['RDF'][0];
                } elseif (isset(
$data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0])) {
                    
$feed = &$data['child'][\SimplePie\SimplePie::NAMESPACE_RSS_20]['rss'][0];
                } else {
                    
$feed null;
                }

                if (
$feed !== null) {
                    
$sql 'SELECT `data` FROM `' $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :feed ORDER BY `posted` DESC';
                    if (
$items 0) {
                        
$sql .= ' LIMIT ' $items;
                    }

                    
$query $this->mysql->prepare($sql);
                    
$query->bindValue(':feed'$this->id);
                    if (
$query->execute()) {
                        while (
$row $query->fetchColumn()) {
                            
$feed['child'][\SimplePie\SimplePie::NAMESPACE_ATOM_10]['entry'][] = unserialize((string) $row);
                        }
                    } else {
                        return 
false;
                    }
                }
            }
            return 
$data;
        }
        return 
false;
    }

    
/**
     * Retrieve the last modified time for the cache
     *
     * @return int|false Timestamp
     */
    
public function mtime()
    {
        if (
$this->mysql === null) {
            return 
false;
        }

        
$query $this->mysql->prepare('SELECT `mtime` FROM `' $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
        
$query->bindValue(':id'$this->id);
        if (
$query->execute() && ($time $query->fetchColumn())) {
            return (int) 
$time;
        }

        return 
false;
    }

    
/**
     * Set the last modified time to the current time
     *
     * @return bool Success status
     */
    
public function touch()
    {
        if (
$this->mysql === null) {
            return 
false;
        }

        
$query $this->mysql->prepare('UPDATE `' $this->options['extras']['prefix'] . 'cache_data` SET `mtime` = :time WHERE `id` = :id');
        
$query->bindValue(':time'time());
        
$query->bindValue(':id'$this->id);

        return 
$query->execute() && $query->rowCount() > 0;
    }

    
/**
     * Remove the cache
     *
     * @return bool Success status
     */
    
public function unlink()
    {
        if (
$this->mysql === null) {
            return 
false;
        }

        
$query $this->mysql->prepare('DELETE FROM `' $this->options['extras']['prefix'] . 'cache_data` WHERE `id` = :id');
        
$query->bindValue(':id'$this->id);
        
$query2 $this->mysql->prepare('DELETE FROM `' $this->options['extras']['prefix'] . 'items` WHERE `feed_id` = :id');
        
$query2->bindValue(':id'$this->id);

        return 
$query->execute() && $query2->execute();
    }
}

class_alias('SimplePie\Cache\MySQL''SimplePie_Cache_MySQL');