[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/File/ -> MediaFile.php (source)

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