1 <?php
2
3 4 5 6 7
8 class rex_media
9 {
10 use rex_instance_pool_trait;
11 use rex_instance_list_pool_trait;
12
13
14 protected $id = '';
15
16 protected $category_id = '';
17
18
19 protected $name = '';
20
21 protected $originalname = '';
22
23 protected $type = '';
24
25 protected $size = '';
26
27
28 protected $width = '';
29
30 protected $height = '';
31
32
33 protected $title = '';
34
35
36 protected $updatedate = '';
37
38 protected $createdate = '';
39
40
41 protected $updateuser = '';
42
43 protected $createuser = '';
44
45 46 47 48 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 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 111
112 public function getId()
113 {
114 return $this->id;
115 }
116
117 118 119
120 public function getCategory()
121 {
122 return rex_media_category::get($this->getCategoryId());
123 }
124
125 126 127
128 public function getCategoryId()
129 {
130 return $this->category_id;
131 }
132
133 134 135
136 public function getTitle()
137 {
138 return $this->title;
139 }
140
141 142 143
144 public function getFileName()
145 {
146 return $this->name;
147 }
148
149 150 151
152 public function getOriginalFileName()
153 {
154 return $this->originalname;
155 }
156
157 158 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 168
169 public function getWidth()
170 {
171 return $this->width;
172 }
173
174 175 176
177 public function getHeight()
178 {
179 return $this->height;
180 }
181
182 183 184
185 public function getType()
186 {
187 return $this->type;
188 }
189
190 191 192
193 public function getSize()
194 {
195 return $this->size;
196 }
197
198 199 200
201 public function getFormattedSize()
202 {
203 return rex_formatter::bytes($this->getSize());
204 }
205
206 207 208
209 public function getUpdateUser()
210 {
211 return $this->updateuser;
212 }
213
214 215 216
217 public function getUpdateDate()
218 {
219 return $this->updatedate;
220 }
221
222 223 224
225 public function getCreateUser()
226 {
227 return $this->createuser;
228 }
229
230 231 232
233 public function getCreateDate()
234 {
235 return $this->createdate;
236 }
237
238 239 240 241 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 276 277 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 286
287 public function isImage()
288 {
289 return self::isImageType($this->getExtension());
290 }
291
292
293
294 295 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
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
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
337
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 348 349 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