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