1 <?php
  2 
  3 /**
  4  * Object Oriented Framework: Bildet ein Medium des Medienpools ab.
  5  *
  6  * @package redaxo\mediapool
  7  */
  8 class rex_media
  9 {
 10     use rex_instance_pool_trait;
 11     use rex_instance_list_pool_trait;
 12 
 13     // id
 14     protected $id = '';
 15     // categoryid
 16     protected $category_id = '';
 17 
 18     // filename
 19     protected $name = '';
 20     // originalname
 21     protected $originalname = '';
 22     // filetype
 23     protected $type = '';
 24     // filesize
 25     protected $size = '';
 26 
 27     // filewidth
 28     protected $width = '';
 29     // fileheight
 30     protected $height = '';
 31 
 32     // filetitle
 33     protected $title = '';
 34 
 35     // updatedate
 36     protected $updatedate = '';
 37     // createdate
 38     protected $createdate = '';
 39 
 40     // updateuser
 41     protected $updateuser = '';
 42     // createuser
 43     protected $createuser = '';
 44 
 45     /**
 46      * @param string $name
 47      *
 48      * @return null|static
 49      */
 50     public static function get($name)
 51     {
 52         if (!$name) {
 53             return null;
 54         }
 55 
 56         return static::getInstance($name, function ($name) {
 57             $media_path = rex_path::addonCache('mediapool', $name . '.media');
 58 
 59             $cache = rex_file::getCache($media_path);
 60             if (!$cache) {
 61                 rex_media_cache::generate($name);
 62                 $cache = rex_file::getCache($media_path);
 63             }
 64 
 65             if ($cache) {
 66                 $aliasMap = [
 67                     'filename' => 'name',
 68                     'filetype' => 'type',
 69                     'filesize' => 'size',
 70                 ];
 71 
 72                 $media = new static();
 73                 foreach ($cache as $key => $value) {
 74                     if (isset($aliasMap[$key])) {
 75                         $var_name = $aliasMap[$key];
 76                     } else {
 77                         $var_name = $key;
 78                     }
 79 
 80                     $media->$var_name = $value;
 81                 }
 82                 $media->category = null;
 83 
 84                 return $media;
 85             }
 86 
 87             return null;
 88         });
 89     }
 90 
 91     /**
 92      * @return static[]
 93      */
 94     public static function getRootMedia()
 95     {
 96         return static::getInstanceList('root_media', 'static::get', function () {
 97             $list_path = rex_path::addonCache('mediapool', '0.mlist');
 98 
 99             $list = rex_file::getCache($list_path);
100             if (!$list) {
101                 rex_media_cache::generateList(0);
102                 $list = rex_file::getCache($list_path);
103             }
104 
105             return $list;
106         });
107     }
108 
109     /**
110      * @return int
111      */
112     public function getId()
113     {
114         return $this->id;
115     }
116 
117     /**
118      * @return rex_media_category
119      */
120     public function getCategory()
121     {
122         return rex_media_category::get($this->getCategoryId());
123     }
124 
125     /**
126      * @return int
127      */
128     public function getCategoryId()
129     {
130         return $this->category_id;
131     }
132 
133     /**
134      * @return string
135      */
136     public function getTitle()
137     {
138         return $this->title;
139     }
140 
141     /**
142      * @return string
143      */
144     public function getFileName()
145     {
146         return $this->name;
147     }
148 
149     /**
150      * @return string
151      */
152     public function getOriginalFileName()
153     {
154         return $this->originalname;
155     }
156 
157     /**
158      * @return string
159      */
160     public function getUrl()
161     {
162         $url = rex_extension::registerPoint(new rex_extension_point('MEDIA_URL_REWRITE', '', ['media' => $this]));
163         return $url ?: rex_url::media($this->getFileName());
164     }
165 
166     /**
167      * @return int
168      */
169     public function getWidth()
170     {
171         return $this->width;
172     }
173 
174     /**
175      * @return int
176      */
177     public function getHeight()
178     {
179         return $this->height;
180     }
181 
182     /**
183      * @return string
184      */
185     public function getType()
186     {
187         return $this->type;
188     }
189 
190     /**
191      * @return int
192      */
193     public function getSize()
194     {
195         return $this->size;
196     }
197 
198     /**
199      * @return string
200      */
201     public function getFormattedSize()
202     {
203         return rex_formatter::bytes($this->getSize());
204     }
205 
206     /**
207      * @return string
208      */
209     public function getUpdateUser()
210     {
211         return $this->updateuser;
212     }
213 
214     /**
215      * @return int
216      */
217     public function getUpdateDate()
218     {
219         return $this->updatedate;
220     }
221 
222     /**
223      * @return string
224      */
225     public function getCreateUser()
226     {
227         return $this->createuser;
228     }
229 
230     /**
231      * @return int
232      */
233     public function getCreateDate()
234     {
235         return $this->createdate;
236     }
237 
238     /**
239      * @param array $params
240      *
241      * @return string
242      */
243     public function toImage(array $params = [])
244     {
245         if (!$this->isImage()) {
246             return '';
247         }
248 
249         $filename = rex_url::media($this->getFileName());
250         $title = $this->getTitle();
251 
252         if (!isset($params['alt'])) {
253             if ($title != '') {
254                 $params['alt'] = rex_escape($title);
255             }
256         }
257 
258         if (!isset($params['title'])) {
259             if ($title != '') {
260                 $params['title'] = rex_escape($title);
261             }
262         }
263 
264         rex_extension::registerPoint(new rex_extension_point('MEDIA_TOIMAGE', '', ['filename' => &$filename, 'params' => &$params]));
265 
266         $additional = '';
267         foreach ($params as $name => $value) {
268             $additional .= ' ' . $name . '="' . $value . '"';
269         }
270 
271         return sprintf('<img src="%s"%s />', $filename, $additional);
272     }
273 
274     /**
275      * @param string $attributes
276      *
277      * @return string
278      */
279     public function toLink($attributes = '')
280     {
281         return sprintf('<a href="%s" title="%s"%s>%s</a>', $this->getUrl(), $this->getValue('med_description'), $attributes, $this->getFileName());
282     }
283 
284     /**
285      * @return bool
286      */
287     public function isImage()
288     {
289         return self::isImageType($this->getExtension());
290     }
291 
292     // new functions by vscope
293 
294     /**
295      * @return string
296      */
297     public function getExtension()
298     {
299         return rex_file::extension($this->name);
300     }
301 
302     public function fileExists()
303     {
304         return file_exists(rex_path::media($this->getFileName()));
305     }
306 
307     // allowed filetypes
308     public static function getDocTypes()
309     {
310         return rex_addon::get('mediapool')->getProperty('allowed_doctypes');
311     }
312 
313     public static function isDocType($type)
314     {
315         return in_array($type, self :: getDocTypes());
316     }
317 
318     // allowed image upload types
319     public static function getImageTypes()
320     {
321         return rex_addon::get('mediapool')->getProperty('image_extensions');
322     }
323 
324     public static function isImageType($extension)
325     {
326         return in_array($extension, self::getImageTypes());
327     }
328 
329     public function hasValue($value)
330     {
331         return isset($this->$value);
332     }
333 
334     public function getValue($value)
335     {
336         // damit alte rex_article felder wie copyright, description
337         // noch funktionieren
338         if ($this->hasValue($value)) {
339             return $this->$value;
340         }
341         if ($this->hasValue('med_' . $value)) {
342             return $this->getValue('med_' . $value);
343         }
344     }
345 
346     /**
347      * Returns whether the element is permitted.
348      *
349      * @return bool
350      */
351     public function isPermitted()
352     {
353         return (bool) rex_extension::registerPoint(new rex_extension_point('MEDIA_IS_PERMITTED', true, ['element' => $this]));
354     }
355 }
356