Failed Conditions
Push — mediaitems ( 8f7d0e )
by Andreas
05:44 queued 03:05
created

inc/Media/MediaFile.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace dokuwiki\Media;
4
5
use dokuwiki\Utf8\PhpString;
6
7
class MediaFile
8
{
9
    protected $id;
10
    protected $path;
11
12
    protected $mime;
13
    protected $ext;
14
    protected $downloadable;
15
16
    protected $width;
17
    protected $height;
18
    protected $meta;
19
20
    /**
21
     * MediaFile constructor.
22
     * @param string $id
23
     * @param string|int $rev optional revision
24
     */
25
    public function __construct($id, $rev = '')
0 ignored issues
show
The parameter $rev is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
26
    {
27
        $this->id = $id; //FIXME should it be cleaned?
28
        $this->path = mediaFN($id);
29
30
        list($this->mime, $this->ext, $this->downloadable) = mimetype($this->path, false);
31
    }
32
33
    /**
34
     * Factory to create a new MediaFile from a Media Path
35
     *
36
     * @param string $path The path of a file, relative to the media base dir
37
     * @return MediaFile
38
     */
39
    static public function fromRelativePath($path)
40
    {
41
        if ($path[0] === '/' || $path[1] === ':') {
42
            throw new \RuntimeException('Only paths relative to the media directory may be used');
43
        }
44
45
        return new MediaFile(pathID($path, true));
46
    }
47
48
    /** @return string */
49
    public function getId()
50
    {
51
        return $this->id;
52
    }
53
54
    /** @return string */
55
    public function getPath()
56
    {
57
        return $this->path;
58
    }
59
60
    /**
61
     * The ID without namespace, used for display purposes
62
     *
63
     * @return string
64
     */
65
    public function getDisplayName()
66
    {
67
        return noNS($this->id);
68
    }
69
70
    /** @return string */
71
    public function getMime()
72
    {
73
        if (!$this->mime) return 'application/octet-stream';
74
        return $this->mime;
75
    }
76
77
    /** @return string */
78
    public function getExtension()
79
    {
80
        return (string)$this->ext;
81
    }
82
83
    /**
84
     * Similar to the extesion but does some clean up
85
     *
86
     * @return string
87
     */
88
    public function getIcoClass()
89
    {
90
        $ext = $this->getExtension();
91
        if ($ext === '') $ext = 'file';
92
        return preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
93
    }
94
95
    /**
96
     * Should this file be downloaded instead being displayed inline?
97
     *
98
     * @return bool
99
     */
100
    public function isDownloadable()
101
    {
102
        return $this->downloadable;
103
    }
104
105
    /** @return int */
106
    public function getFileSize()
107
    {
108
        return filesize($this->path);
109
    }
110
111
    /** @return int */
112
    public function getLastModified()
113
    {
114
        return filemtime($this->path);
115
    }
116
117
    /** @return bool */
118
    public function isWritable()
119
    {
120
        return is_writable($this->path);
121
    }
122
123
    /** @return bool */
124
    public function isImage()
125
    {
126
        return (substr($this->mime, 0, 6) === 'image/');
127
    }
128
129
    /**
130
     * initializes width and height for images when requested
131
     */
132
    protected function initSizes()
133
    {
134
        $this->width = 0;
135
        $this->height = 0;
136
        if (!$this->isImage()) return;
137
        $info = getimagesize($this->path);
138
        if ($info === false) return;
139
        list($this->width, $this->height) = $info;
140
    }
141
142
    /**
143
     * Returns the width if this is a supported image, 0 otherwise
144
     *
145
     * @return int
146
     */
147
    public function getWidth()
148
    {
149
        if ($this->width === null) $this->initSizes();
150
        return $this->width;
151
    }
152
153
    /**
154
     * Returns the height if this is a supported image, 0 otherwise
155
     *
156
     * @return int
157
     */
158
    public function getHeight()
159
    {
160
        if ($this->height === null) $this->initSizes();
161
        return $this->height;
162
    }
163
164
    /** @return \JpegMeta */
165
    public function getMeta()
166
    {
167
        if($this->meta === null) $this->meta = new \JpegMeta($this->path);
168
        return $this->meta;
169
    }
170
}
171