1<?php
2
3declare(strict_types=1);
4/**
5 * SimplePie
6 *
7 * A PHP-Based RSS and Atom Feed Framework.
8 * Takes the hard work out of managing a complete RSS/Atom solution.
9 *
10 * Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification, are
14 * permitted provided that the following conditions are met:
15 *
16 * 	* Redistributions of source code must retain the above copyright notice, this list of
17 * 	  conditions and the following disclaimer.
18 *
19 * 	* Redistributions in binary form must reproduce the above copyright notice, this list
20 * 	  of conditions and the following disclaimer in the documentation and/or other materials
21 * 	  provided with the distribution.
22 *
23 * 	* Neither the name of the SimplePie Team nor the names of its contributors may be used
24 * 	  to endorse or promote products derived from this software without specific prior
25 * 	  written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
28 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
29 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
30 * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 *
37 * @package SimplePie
38 * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
39 * @author Ryan Parman
40 * @author Sam Sneddon
41 * @author Ryan McCue
42 * @link http://simplepie.org/ SimplePie
43 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
44 */
45
46namespace SimplePie\Cache;
47
48/**
49 * Caches data to the filesystem
50 *
51 * @package SimplePie
52 * @subpackage Caching
53 * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
54 */
55class File implements Base
56{
57    /**
58     * Location string
59     *
60     * @see SimplePie::$cache_location
61     * @var string
62     */
63    protected $location;
64
65    /**
66     * Filename
67     *
68     * @var string
69     */
70    protected $filename;
71
72    /**
73     * File extension
74     *
75     * @var string
76     */
77    protected $extension;
78
79    /**
80     * File path
81     *
82     * @var string
83     */
84    protected $name;
85
86    /**
87     * Create a new cache object
88     *
89     * @param string $location Location string (from SimplePie::$cache_location)
90     * @param string $name Unique ID for the cache
91     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
92     */
93    public function __construct($location, $name, $type)
94    {
95        $this->location = $location;
96        $this->filename = $name;
97        $this->extension = $type;
98        $this->name = "$this->location/$this->filename.$this->extension";
99    }
100
101    /**
102     * Save data to the cache
103     *
104     * @param array|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
105     * @return bool Successfulness
106     */
107    public function save($data)
108    {
109        if (file_exists($this->name) && is_writable($this->name) || file_exists($this->location) && is_writable($this->location)) {
110            if ($data instanceof \SimplePie\SimplePie) {
111                $data = $data->data;
112            }
113
114            $data = serialize($data);
115            return (bool) file_put_contents($this->name, $data);
116        }
117        return false;
118    }
119
120    /**
121     * Retrieve the data saved to the cache
122     *
123     * @return array Data for SimplePie::$data
124     */
125    public function load()
126    {
127        if (file_exists($this->name) && is_readable($this->name)) {
128            return unserialize(file_get_contents($this->name));
129        }
130        return false;
131    }
132
133    /**
134     * Retrieve the last modified time for the cache
135     *
136     * @return int Timestamp
137     */
138    public function mtime()
139    {
140        return @filemtime($this->name);
141    }
142
143    /**
144     * Set the last modified time to the current time
145     *
146     * @return bool Success status
147     */
148    public function touch()
149    {
150        return @touch($this->name);
151    }
152
153    /**
154     * Remove the cache
155     *
156     * @return bool Success status
157     */
158    public function unlink()
159    {
160        if (file_exists($this->name)) {
161            return unlink($this->name);
162        }
163        return false;
164    }
165}
166
167class_alias('SimplePie\Cache\File', 'SimplePie_Cache_File');
168