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