xref: /dokuwiki/inc/File/MediaFile.php (revision 6c16a3a9aa602bb7e269fb6d5d18e1353e17f97f)
10603f506SAndreas Gohr<?php
20603f506SAndreas Gohr
30603f506SAndreas Gohrnamespace dokuwiki\File;
40603f506SAndreas Gohr
579a2d784SGerrit Uitslaguse JpegMeta;
60603f506SAndreas Gohr
70603f506SAndreas Gohrclass MediaFile
80603f506SAndreas Gohr{
90603f506SAndreas Gohr    protected $id;
100603f506SAndreas Gohr    protected $path;
110603f506SAndreas Gohr
120603f506SAndreas Gohr    protected $mime;
130603f506SAndreas Gohr    protected $ext;
140603f506SAndreas Gohr    protected $downloadable;
150603f506SAndreas Gohr
160603f506SAndreas Gohr    protected $width;
170603f506SAndreas Gohr    protected $height;
180603f506SAndreas Gohr    protected $meta;
190603f506SAndreas Gohr
200603f506SAndreas Gohr    /**
210603f506SAndreas Gohr     * MediaFile constructor.
220603f506SAndreas Gohr     * @param string $id
230603f506SAndreas Gohr     * @param string|int $rev optional revision
240603f506SAndreas Gohr     */
250603f506SAndreas Gohr    public function __construct($id, $rev = '')
260603f506SAndreas Gohr    {
270603f506SAndreas Gohr        $this->id = $id; //FIXME should it be cleaned?
280603f506SAndreas Gohr        $this->path = mediaFN($id, $rev);
290603f506SAndreas Gohr
30445164b2SAndreas Gohr        [$this->ext, $this->mime, $this->downloadable] = mimetype($this->path, false);
310603f506SAndreas Gohr    }
320603f506SAndreas Gohr
330603f506SAndreas Gohr    /** @return string */
340603f506SAndreas Gohr    public function getId()
350603f506SAndreas Gohr    {
360603f506SAndreas Gohr        return $this->id;
370603f506SAndreas Gohr    }
380603f506SAndreas Gohr
390603f506SAndreas Gohr    /** @return string */
400603f506SAndreas Gohr    public function getPath()
410603f506SAndreas Gohr    {
420603f506SAndreas Gohr        return $this->path;
430603f506SAndreas Gohr    }
440603f506SAndreas Gohr
450603f506SAndreas Gohr    /**
460603f506SAndreas Gohr     * The ID without namespace, used for display purposes
470603f506SAndreas Gohr     *
480603f506SAndreas Gohr     * @return string
490603f506SAndreas Gohr     */
500603f506SAndreas Gohr    public function getDisplayName()
510603f506SAndreas Gohr    {
520603f506SAndreas Gohr        return noNS($this->id);
530603f506SAndreas Gohr    }
540603f506SAndreas Gohr
550603f506SAndreas Gohr    /** @return string */
560603f506SAndreas Gohr    public function getMime()
570603f506SAndreas Gohr    {
580603f506SAndreas Gohr        if (!$this->mime) return 'application/octet-stream';
590603f506SAndreas Gohr        return $this->mime;
600603f506SAndreas Gohr    }
610603f506SAndreas Gohr
620603f506SAndreas Gohr    /** @return string */
630603f506SAndreas Gohr    public function getExtension()
640603f506SAndreas Gohr    {
650603f506SAndreas Gohr        return (string)$this->ext;
660603f506SAndreas Gohr    }
670603f506SAndreas Gohr
680603f506SAndreas Gohr    /**
690603f506SAndreas Gohr     * Similar to the extesion but does some clean up
700603f506SAndreas Gohr     *
710603f506SAndreas Gohr     * @return string
720603f506SAndreas Gohr     */
730603f506SAndreas Gohr    public function getIcoClass()
740603f506SAndreas Gohr    {
750603f506SAndreas Gohr        $ext = $this->getExtension();
760603f506SAndreas Gohr        if ($ext === '') $ext = 'file';
770603f506SAndreas Gohr        return preg_replace('/[^_\-a-z0-9]+/i', '_', $ext);
780603f506SAndreas Gohr    }
790603f506SAndreas Gohr
800603f506SAndreas Gohr    /**
810603f506SAndreas Gohr     * Should this file be downloaded instead being displayed inline?
820603f506SAndreas Gohr     *
830603f506SAndreas Gohr     * @return bool
840603f506SAndreas Gohr     */
850603f506SAndreas Gohr    public function isDownloadable()
860603f506SAndreas Gohr    {
870603f506SAndreas Gohr        return $this->downloadable;
880603f506SAndreas Gohr    }
890603f506SAndreas Gohr
900603f506SAndreas Gohr    /** @return int */
910603f506SAndreas Gohr    public function getFileSize()
920603f506SAndreas Gohr    {
930603f506SAndreas Gohr        return filesize($this->path);
940603f506SAndreas Gohr    }
950603f506SAndreas Gohr
960603f506SAndreas Gohr    /** @return int */
970603f506SAndreas Gohr    public function getLastModified()
980603f506SAndreas Gohr    {
990603f506SAndreas Gohr        return filemtime($this->path);
1000603f506SAndreas Gohr    }
1010603f506SAndreas Gohr
1020603f506SAndreas Gohr    /** @return bool */
1030603f506SAndreas Gohr    public function isWritable()
1040603f506SAndreas Gohr    {
1050603f506SAndreas Gohr        return is_writable($this->path);
1060603f506SAndreas Gohr    }
1070603f506SAndreas Gohr
1080603f506SAndreas Gohr    /** @return bool */
1090603f506SAndreas Gohr    public function isImage()
1100603f506SAndreas Gohr    {
111*6c16a3a9Sfiwswe        return (str_starts_with($this->mime, 'image/'));
1120603f506SAndreas Gohr    }
1130603f506SAndreas Gohr
1140603f506SAndreas Gohr    /**
1150603f506SAndreas Gohr     * initializes width and height for images when requested
1160603f506SAndreas Gohr     */
1170603f506SAndreas Gohr    protected function initSizes()
1180603f506SAndreas Gohr    {
1190603f506SAndreas Gohr        $this->width = 0;
1200603f506SAndreas Gohr        $this->height = 0;
1210603f506SAndreas Gohr        if (!$this->isImage()) return;
1220603f506SAndreas Gohr        $info = getimagesize($this->path);
1230603f506SAndreas Gohr        if ($info === false) return;
124445164b2SAndreas Gohr        [$this->width, $this->height] = $info;
1250603f506SAndreas Gohr    }
1260603f506SAndreas Gohr
1270603f506SAndreas Gohr    /**
1280603f506SAndreas Gohr     * Returns the width if this is a supported image, 0 otherwise
1290603f506SAndreas Gohr     *
1300603f506SAndreas Gohr     * @return int
1310603f506SAndreas Gohr     */
1320603f506SAndreas Gohr    public function getWidth()
1330603f506SAndreas Gohr    {
1340603f506SAndreas Gohr        if ($this->width === null) $this->initSizes();
1350603f506SAndreas Gohr        return $this->width;
1360603f506SAndreas Gohr    }
1370603f506SAndreas Gohr
1380603f506SAndreas Gohr    /**
1390603f506SAndreas Gohr     * Returns the height if this is a supported image, 0 otherwise
1400603f506SAndreas Gohr     *
1410603f506SAndreas Gohr     * @return int
1420603f506SAndreas Gohr     */
1430603f506SAndreas Gohr    public function getHeight()
1440603f506SAndreas Gohr    {
1450603f506SAndreas Gohr        if ($this->height === null) $this->initSizes();
1460603f506SAndreas Gohr        return $this->height;
1470603f506SAndreas Gohr    }
1480603f506SAndreas Gohr
1490603f506SAndreas Gohr    /**
1500603f506SAndreas Gohr     * Returns the permissions the current user has on the file
1510603f506SAndreas Gohr     *
1520603f506SAndreas Gohr     * @todo doing this for each file within a namespace is a waste, we need to cache this somehow
1530603f506SAndreas Gohr     * @return int
1540603f506SAndreas Gohr     */
1550603f506SAndreas Gohr    public function userPermission()
1560603f506SAndreas Gohr    {
1570603f506SAndreas Gohr        return auth_quickaclcheck(getNS($this->id) . ':*');
1580603f506SAndreas Gohr    }
1590603f506SAndreas Gohr
16079a2d784SGerrit Uitslag    /** @return JpegMeta */
1610603f506SAndreas Gohr    public function getMeta()
1620603f506SAndreas Gohr    {
16379a2d784SGerrit Uitslag        if ($this->meta === null) $this->meta = new JpegMeta($this->path);
1640603f506SAndreas Gohr        return $this->meta;
1650603f506SAndreas Gohr    }
1660603f506SAndreas Gohr}
167