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