1*0603f506SAndreas Gohr<?php 2*0603f506SAndreas Gohr 3*0603f506SAndreas Gohrnamespace dokuwiki\File; 4*0603f506SAndreas Gohr 5*0603f506SAndreas Gohruse dokuwiki\Utf8\PhpString; 6*0603f506SAndreas Gohr 7*0603f506SAndreas Gohrclass MediaFile 8*0603f506SAndreas Gohr{ 9*0603f506SAndreas Gohr protected $id; 10*0603f506SAndreas Gohr protected $path; 11*0603f506SAndreas Gohr 12*0603f506SAndreas Gohr protected $mime; 13*0603f506SAndreas Gohr protected $ext; 14*0603f506SAndreas Gohr protected $downloadable; 15*0603f506SAndreas Gohr 16*0603f506SAndreas Gohr protected $width; 17*0603f506SAndreas Gohr protected $height; 18*0603f506SAndreas Gohr protected $meta; 19*0603f506SAndreas Gohr 20*0603f506SAndreas Gohr /** 21*0603f506SAndreas Gohr * MediaFile constructor. 22*0603f506SAndreas Gohr * @param string $id 23*0603f506SAndreas Gohr * @param string|int $rev optional revision 24*0603f506SAndreas Gohr */ 25*0603f506SAndreas Gohr public function __construct($id, $rev = '') 26*0603f506SAndreas Gohr { 27*0603f506SAndreas Gohr $this->id = $id; //FIXME should it be cleaned? 28*0603f506SAndreas Gohr $this->path = mediaFN($id, $rev); 29*0603f506SAndreas Gohr 30*0603f506SAndreas Gohr list($this->ext, $this->mime, $this->downloadable) = mimetype($this->path, false); 31*0603f506SAndreas Gohr } 32*0603f506SAndreas Gohr 33*0603f506SAndreas Gohr /** @return string */ 34*0603f506SAndreas Gohr public function getId() 35*0603f506SAndreas Gohr { 36*0603f506SAndreas Gohr return $this->id; 37*0603f506SAndreas Gohr } 38*0603f506SAndreas Gohr 39*0603f506SAndreas Gohr /** @return string */ 40*0603f506SAndreas Gohr public function getPath() 41*0603f506SAndreas Gohr { 42*0603f506SAndreas Gohr return $this->path; 43*0603f506SAndreas Gohr } 44*0603f506SAndreas Gohr 45*0603f506SAndreas Gohr /** 46*0603f506SAndreas Gohr * The ID without namespace, used for display purposes 47*0603f506SAndreas Gohr * 48*0603f506SAndreas Gohr * @return string 49*0603f506SAndreas Gohr */ 50*0603f506SAndreas Gohr public function getDisplayName() 51*0603f506SAndreas Gohr { 52*0603f506SAndreas Gohr return noNS($this->id); 53*0603f506SAndreas Gohr } 54*0603f506SAndreas Gohr 55*0603f506SAndreas Gohr /** @return string */ 56*0603f506SAndreas Gohr public function getMime() 57*0603f506SAndreas Gohr { 58*0603f506SAndreas Gohr if (!$this->mime) return 'application/octet-stream'; 59*0603f506SAndreas Gohr return $this->mime; 60*0603f506SAndreas Gohr } 61*0603f506SAndreas Gohr 62*0603f506SAndreas Gohr /** @return string */ 63*0603f506SAndreas Gohr public function getExtension() 64*0603f506SAndreas Gohr { 65*0603f506SAndreas Gohr return (string)$this->ext; 66*0603f506SAndreas Gohr } 67*0603f506SAndreas Gohr 68*0603f506SAndreas Gohr /** 69*0603f506SAndreas Gohr * Similar to the extesion but does some clean up 70*0603f506SAndreas Gohr * 71*0603f506SAndreas Gohr * @return string 72*0603f506SAndreas Gohr */ 73*0603f506SAndreas Gohr public function getIcoClass() 74*0603f506SAndreas Gohr { 75*0603f506SAndreas Gohr $ext = $this->getExtension(); 76*0603f506SAndreas Gohr if ($ext === '') $ext = 'file'; 77*0603f506SAndreas Gohr return preg_replace('/[^_\-a-z0-9]+/i', '_', $ext); 78*0603f506SAndreas Gohr } 79*0603f506SAndreas Gohr 80*0603f506SAndreas Gohr /** 81*0603f506SAndreas Gohr * Should this file be downloaded instead being displayed inline? 82*0603f506SAndreas Gohr * 83*0603f506SAndreas Gohr * @return bool 84*0603f506SAndreas Gohr */ 85*0603f506SAndreas Gohr public function isDownloadable() 86*0603f506SAndreas Gohr { 87*0603f506SAndreas Gohr return $this->downloadable; 88*0603f506SAndreas Gohr } 89*0603f506SAndreas Gohr 90*0603f506SAndreas Gohr /** @return int */ 91*0603f506SAndreas Gohr public function getFileSize() 92*0603f506SAndreas Gohr { 93*0603f506SAndreas Gohr return filesize($this->path); 94*0603f506SAndreas Gohr } 95*0603f506SAndreas Gohr 96*0603f506SAndreas Gohr /** @return int */ 97*0603f506SAndreas Gohr public function getLastModified() 98*0603f506SAndreas Gohr { 99*0603f506SAndreas Gohr return filemtime($this->path); 100*0603f506SAndreas Gohr } 101*0603f506SAndreas Gohr 102*0603f506SAndreas Gohr /** @return bool */ 103*0603f506SAndreas Gohr public function isWritable() 104*0603f506SAndreas Gohr { 105*0603f506SAndreas Gohr return is_writable($this->path); 106*0603f506SAndreas Gohr } 107*0603f506SAndreas Gohr 108*0603f506SAndreas Gohr /** @return bool */ 109*0603f506SAndreas Gohr public function isImage() 110*0603f506SAndreas Gohr { 111*0603f506SAndreas Gohr return (substr($this->mime, 0, 6) === 'image/'); 112*0603f506SAndreas Gohr } 113*0603f506SAndreas Gohr 114*0603f506SAndreas Gohr /** 115*0603f506SAndreas Gohr * initializes width and height for images when requested 116*0603f506SAndreas Gohr */ 117*0603f506SAndreas Gohr protected function initSizes() 118*0603f506SAndreas Gohr { 119*0603f506SAndreas Gohr $this->width = 0; 120*0603f506SAndreas Gohr $this->height = 0; 121*0603f506SAndreas Gohr if (!$this->isImage()) return; 122*0603f506SAndreas Gohr $info = getimagesize($this->path); 123*0603f506SAndreas Gohr if ($info === false) return; 124*0603f506SAndreas Gohr list($this->width, $this->height) = $info; 125*0603f506SAndreas Gohr } 126*0603f506SAndreas Gohr 127*0603f506SAndreas Gohr /** 128*0603f506SAndreas Gohr * Returns the width if this is a supported image, 0 otherwise 129*0603f506SAndreas Gohr * 130*0603f506SAndreas Gohr * @return int 131*0603f506SAndreas Gohr */ 132*0603f506SAndreas Gohr public function getWidth() 133*0603f506SAndreas Gohr { 134*0603f506SAndreas Gohr if ($this->width === null) $this->initSizes(); 135*0603f506SAndreas Gohr return $this->width; 136*0603f506SAndreas Gohr } 137*0603f506SAndreas Gohr 138*0603f506SAndreas Gohr /** 139*0603f506SAndreas Gohr * Returns the height if this is a supported image, 0 otherwise 140*0603f506SAndreas Gohr * 141*0603f506SAndreas Gohr * @return int 142*0603f506SAndreas Gohr */ 143*0603f506SAndreas Gohr public function getHeight() 144*0603f506SAndreas Gohr { 145*0603f506SAndreas Gohr if ($this->height === null) $this->initSizes(); 146*0603f506SAndreas Gohr return $this->height; 147*0603f506SAndreas Gohr } 148*0603f506SAndreas Gohr 149*0603f506SAndreas Gohr /** 150*0603f506SAndreas Gohr * Returns the permissions the current user has on the file 151*0603f506SAndreas Gohr * 152*0603f506SAndreas Gohr * @todo doing this for each file within a namespace is a waste, we need to cache this somehow 153*0603f506SAndreas Gohr * @return int 154*0603f506SAndreas Gohr */ 155*0603f506SAndreas Gohr public function userPermission() 156*0603f506SAndreas Gohr { 157*0603f506SAndreas Gohr return auth_quickaclcheck(getNS($this->id).':*'); 158*0603f506SAndreas Gohr } 159*0603f506SAndreas Gohr 160*0603f506SAndreas Gohr /** @return \JpegMeta */ 161*0603f506SAndreas Gohr public function getMeta() 162*0603f506SAndreas Gohr { 163*0603f506SAndreas Gohr if($this->meta === null) $this->meta = new \JpegMeta($this->path); 164*0603f506SAndreas Gohr return $this->meta; 165*0603f506SAndreas Gohr } 166*0603f506SAndreas Gohr} 167