aboutsummaryrefslogtreecommitdiff
path: root/app/class/Media.php
blob: 3a34a04c8431743876b9efc16f3be9337e70c098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php

namespace Wcms;

use DateTime;
use DateTimeImmutable;
use DateTimeZone;
use Exception;

class Media extends Item
{
    protected $id;
    protected $path;
    protected $extension;
    protected $type;
    protected $size;
    protected $date;
    protected $width;
    protected $height;
    protected $length;
    protected $uid;
    protected $permissions;

    public const IMAGE = array('jpg', 'jpeg', 'gif', 'png');
    public const SOUND = array('mp3', 'flac', 'wav', 'ogg');
    public const VIDEO = array('mp4', 'mov', 'avi', 'mkv');
    public const ARCHIVE = array('zip', 'rar');



// _____________________________________________________ F U N ____________________________________________________

    public function __construct(array $donnees)
    {
        $this->hydrate($donnees);
    }

    public function analyse()
    {
        $this->settype();

        $this->setdate();

        $filepath = $this->path . $this->id . '.' . $this->extension;

        if ($this->type == 'image') {
            list($width, $height, $type, $attr) = getimagesize($filepath);
            $this->width = $width;
            $this->height = $height;
        }

        $stat = stat($filepath);

        $permissions = decoct(fileperms($filepath) & 0777);

        $this->setpermissions($permissions);

        $this->hydrate($stat);
    }


    public function getfullpath()
    {
        if (!empty(Config::basepath())) {
            $base = '/' . Config::basepath();
        } else {
            $base = '';
        }
        $fullpath = $base . '/' . $this->path() . $this->id() . '.' . $this->extension();
        $fullpath = str_replace('\\', '/', $fullpath);
        return $fullpath;
    }

    public function getincludepath()
    {
        $includepath = $this->path() . $this->id() . '.' . $this->extension();
        $includepath = str_replace('\\', '/', $includepath);
        $includepath = substr($includepath, 6);
        return $includepath;
    }

    public function getfulldir()
    {
        return $this->path . $this->id . '.' . $this->extension;
    }

    /**
     * Generate html code depending on media type
     *
     * @return string html code
     */
    public function getcode(): string
    {
        switch ($this->type) {
            case 'image':
                $code = '![' . $this->id . '](' . $this->getincludepath() . ')';
                break;
                
            case 'sound':
                    $code = '&lt;audio controls src=&quot;' . $this->getincludepath() . '&quot;&gt;&lt;/audio&gt;';
                break;
                
            case 'video':
                $src = $this->getincludepath();
                $ext = $this->extension;
                $code = '&lt;video controls=&quot;&quot;&gt;';
                $code .= '&lt;source src=&quot;' . $src . '&quot; type="video/' . $ext . '&quot;&gt;&lt;/video&gt;';
                break;

            default:
                    $code = '[' . $this->id . '](' . $this->getincludepath() . ')';
                break;
        }
            
        return $code;
    }

    public function getsymbol()
    {
        switch ($this->type) {
            case 'image':
                $symbol = "🖼";
                break;
            
            case 'sound':
                $symbol = "🎵";
                break;
            
            case 'video':
                $symbol = "🎞";
                break;
                
            case 'document':
                $symbol = "📓";
                break;
            
            case 'archive':
                $symbol = "🗜";
                break;
                    
            case 'code':
                $symbol = "📄";
                break;
                            
            default:
                $symbol = "🎲";
                break;
        }
        return $symbol;
    }



// _________________________________________________ G E T ____________________________________________________

    public function id()
    {
        return $this->id;
    }

    public function path()
    {
        return $this->path;
    }

    public function extension()
    {
        return $this->extension;
    }

    public function type()
    {
        return $this->type;
    }

    public function size($display = 'binary')
    {
        if ($display == 'hr') {
            return readablesize($this->size) . 'o';
        } else {
            return $this->size;
        }
    }

    public function date($option = 'date')
    {
        return $this->datetransform('date', $option);
    }

    public function width()
    {
        return $this->width;
    }

    public function height()
    {
        return $this->height;
    }

    public function length()
    {
        return $this->length;
    }

    public function surface()
    {
        $surface = $this->width * $this->height;
        return readablesize($surface, 1000) . 'px';
    }

    public function uid($option = 'id')
    {
        if ($option === 'name') {
            $userinfo = posix_getpwuid($this->uid);
            return $userinfo['name'];
        } else {
            return $this->uid;
        }
    }

    public function permissions()
    {
        return $this->permissions;
    }

// ___________________________________________________ S E T __________________________________________________

    public function setid($id)
    {
        if (is_string($id)) {
            $this->id = $id;
        }
    }

    public function setpath($path)
    {
        if (strlen($path) < 40 and is_string($path)) {
            $this->path = strip_tags(strtolower($path));
        }
    }

    public function setextension($extension)
    {
        if (strlen($extension) < 7 and is_string($extension)) {
            $this->extension = strip_tags(strtolower($extension));
        }
    }

    public function settype()
    {
        if (!empty($this->extension) && isset(Model::MEDIA_EXT[$this->extension])) {
            $this->type = Model::MEDIA_EXT[$this->extension];
        } else {
            $this->type = 'other';
        }
    }

    public function setsize($size)
    {
        if (is_int($size)) {
            $this->size = $size;
        }
    }

    public function setdate()
    {
        $timestamp = filemtime($this->getfulldir());
        try {
            $this->date = new DateTimeImmutable("@$timestamp");
        } catch (Exception $e) {
            Logger::warningex($e);
            $this->date = new DateTimeImmutable();
        }
    }

    public function setwidth($width)
    {
        if (is_int($width)) {
            $this->width = $width;
        }
    }

    public function setheight($height)
    {
        if (is_int($height)) {
            $this->height = $height;
        }
    }

    public function setlength($length)
    {
        if ($this->type == 'sound') {
            $this->length = $length;
        }
    }

    public function setuid($uid)
    {
        $this->uid = $uid;
    }

    public function setpermissions($permissions)
    {
        $this->permissions = $permissions;
    }
}