Issues (493)

lib/SP/Core/MimeTypes.php (1 issue)

1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Core;
26
27
use SP\Storage\File\FileCacheInterface;
28
use SP\Storage\File\FileException;
29
use SP\Storage\File\XmlFileStorageInterface;
30
31
/**
32
 * Class Mime
33
 *
34
 * @package SP\Core
35
 */
36
final class MimeTypes
37
{
38
    /**
39
     * Cache file name
40
     */
41
    const MIME_CACHE_FILE = CACHE_PATH . DIRECTORY_SEPARATOR . 'mime.cache';
42
    /**
43
     * Cache expire time
44
     */
45
    const CACHE_EXPIRE = 86400;
46
    /**
47
     * @var int
48
     */
49
    protected $lastLoadTime;
50
    /**
51
     * @var  array
52
     */
53
    protected $mimeTypes;
54
    /**
55
     * @var XmlFileStorageInterface
56
     */
57
    protected $xmlFileStorage;
58
    /**
59
     * @var FileCacheInterface
60
     */
61
    private $fileCache;
62
63
    /**
64
     * Mime constructor.
65
     *
66
     * @param FileCacheInterface      $fileCache
67
     * @param XmlFileStorageInterface $xmlFileStorage
68
     *
69
     * @throws FileException
70
     */
71
    public function __construct(FileCacheInterface $fileCache, XmlFileStorageInterface $xmlFileStorage)
72
    {
73
        $this->xmlFileStorage = $xmlFileStorage;
74
        $this->fileCache = $fileCache;
75
76
        $this->loadCache();
77
    }
78
79
    /**
80
     * Loads MIME types from cache file
81
     *
82
     * @return void
83
     * @throws FileException
84
     */
85
    protected function loadCache()
86
    {
87
        try {
88
            if ($this->fileCache->isExpired(self::CACHE_EXPIRE)
89
                || $this->fileCache->isExpiredDate($this->xmlFileStorage->getFileHandler()->getFileTime())
90
            ) {
91
                $this->mapAndSave();
92
            } else {
93
                $this->mimeTypes = $this->fileCache->load();
94
95
                logger('Loaded MIME types cache', 'INFO');
96
            }
97
        } catch (FileException $e) {
98
            processException($e);
99
100
            $this->mapAndSave();
101
        }
102
    }
103
104
    /**
105
     * @throws FileException
106
     */
107
    protected function mapAndSave()
108
    {
109
        logger('MIME TYPES CACHE MISS', 'INFO');
110
111
        $this->map();
112
        $this->saveCache();
113
    }
114
115
    /**
116
     * Sets an array of mime types
117
     *
118
     * @throws FileException
119
     */
120
    protected function map()
121
    {
122
        $this->mimeTypes = [];
123
124
        foreach ($this->load() as $item) {
125
            $this->mimeTypes[] = $item;
126
        }
127
    }
128
129
    /**
130
     * Loads MIME types from XML
131
     *
132
     * @return array
133
     * @throws FileException
134
     */
135
    protected function load()
136
    {
137
        return $this->xmlFileStorage->load('mimetypes')->getItems();
138
    }
139
140
    /**
141
     * Saves MIME types into cache file
142
     */
143
    protected function saveCache()
144
    {
145
        try {
146
            $this->fileCache->save($this->mimeTypes);
147
148
            logger('Saved MIME types cache', 'INFO');
149
        } catch (FileException $e) {
150
            processException($e);
151
        }
152
    }
153
154
    /**
155
     * @throws FileException
156
     */
157
    public function reset()
158
    {
159
        @unlink(self::MIME_CACHE_FILE);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

159
        /** @scrutinizer ignore-unhandled */ @unlink(self::MIME_CACHE_FILE);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
160
161
        $this->loadCache();
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getMimeTypes(): array
168
    {
169
        return $this->mimeTypes;
170
    }
171
}