aboutsummaryrefslogtreecommitdiff
path: root/lib/filesystem/StandardFolder.php
blob: a34ca4ae820d1037c260a901bb8d448367875463 (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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
<?php
/**
 * StandardFolder.php
 *
 * the standard folder is the default implementation for folders from courses institutes
 * and users. Special folders should extend this class
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author    André Noack <noack@data-quest.de>
 * @copyright 2016 Stud.IP Core-Group
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category  Stud.IP
 */
class StandardFolder implements FolderType
{

    public static $sorter = 0;

    /**
     * @var Folder
     */
    protected $folderdata;

    /**
     * StandardFolder constructor.
     * @param Folder|StandardFolder|null $folderdata
     */
    public function __construct($folderdata = null)
    {
        if ($folderdata instanceof StandardFolder) {
            $this->folderdata = $folderdata->folderdata;
        } elseif ($folderdata instanceof Folder) {
            $this->folderdata = $folderdata;
        } else {
            $this->folderdata = Folder::build($folderdata);
        }
        $this->folderdata['folder_type'] = get_class($this);
    }

    /**
     * @return string
     */
    public static function getTypeName()
    {
        return _('Ordner');
    }

    public static function availableInRange($range_id_or_object, $user_id)
    {
        return true;
    }

    /**
     * @param string $role
     * @return Icon
     */
    public function getIcon($role = Icon::DEFAULT_ROLE)
    {
        if ($this->parent_id || !$this->id) {
            $shape = $this->is_empty
                   ? 'folder-empty'
                   : 'folder-full';
        } else {
            $shape = $this->is_empty
                   ? 'folder-home-empty'
                   : 'folder-home-full';
        }

        return Icon::create($shape, $role);
    }

    /**
     * @return string
     */
    public function getId()
    {
        return $this->folderdata->getId();
    }

    /**
     * @param string $attribute
     * @return mixed
     */
    public function __get($attribute)
    {
        return $this->folderdata[$attribute];
    }

    /**
     * @param string $name
     * @param mixed $value
     * @return mixed
     */
    public function __set($name, $value)
    {
        return $this->folderdata[$name] = $value;
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isVisible($user_id)
    {
        $visible = $this->isVisibleNonRecursive($user_id);

        if ($visible && $parent_folder = $this->getParent()) {
            return $parent_folder->isVisible($user_id);
        }

        return $visible;
    }

    /**
     * @param string $user_id
     * @return bool
     */
    protected function isVisibleNonRecursive($user_id)
    {
        $visible = false;

        if ($this->range_type === 'user') {
            $visible = $this->range_id === $user_id;
        }

        if ($this->range_type === 'institute' ) {
            $visible = (Config::get()->ENABLE_FREE_ACCESS
                     && (Config::get()->ENABLE_FREE_ACCESS != 'courses_only')) ||
                       Seminar_Perm::get()->have_perm('user', $user_id);
        }

        if ($this->range_type === 'course') {
            $range = $this->getRangeObject();
            $visible = Config::get()->ENABLE_FREE_ACCESS && isset($range) && $range->lesezugriff == 0
                    || Seminar_Perm::get()->have_studip_perm('user', $this->range_id, $user_id);
        }
        return $visible;
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isReadable($user_id)
    {
        $readable = $this->isVisibleNonRecursive($user_id);
        if ($readable && $parent_folder = $this->getParent()) {
            return $parent_folder->isReadable($user_id);
        }

        return $readable;
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isWritable($user_id)
    {
        return ($this->range_type === 'user' && $this->range_id === $user_id)
            || Seminar_Perm::get()->have_studip_perm('autor', $this->range_id, $user_id);
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isEditable($user_id)
    {
        return ($this->range_type === 'user' && $this->range_id === $user_id)
            || Seminar_Perm::get()->have_studip_perm('tutor', $this->range_id, $user_id);
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isSubfolderAllowed($user_id)
    {
        return ($this->range_type === 'user' && $this->range_id === $user_id)
            || Seminar_Perm::get()->have_studip_perm('tutor', $this->range_id, $user_id);
    }

    /**
     * @return string|Flexi_Template
     */
    public function getDescriptionTemplate()
    {
        return formatReady($this->folderdata['description']);
    }

    /**
     * @return string|Flexi_Template
     */
    public function getEditTemplate()
    {
        return '';
    }

    /**
     * @param array $request
     * @return FolderType|MessageBox
     */
    public function setDataFromEditTemplate($request)
    {
        if (!$request['name']) {
            return MessageBox::error(_('Die Bezeichnung des Ordners fehlt.'));
        }
        $this->folderdata['name']        = $request['name'];
        $this->folderdata['description'] = $request['description'] ?: '';
        return $this;
    }

    /**
     * @return bool|number
     */
    public function store()
    {
        return $this->folderdata->store();
    }

    /**
     * @param FileType $newfile
     * @param string $user_id
     * @return string
     */
    public function validateUpload(FileType $newfile, $user_id)
    {
        $upload_type = FileManager::getUploadTypeConfig($this->range_id, $user_id);
        return $this->getValidationMessages($upload_type, $newfile);
    }

    protected function getValidationMessages($upload_type, $newfile)
    {
        if ($upload_type['file_size'] < $newfile->getSize()) {
            return sprintf(
                _('Die maximale Größe für einen Upload (%s) wurde überschritten.'),
                relsize($upload_type['file_size'])
            );
        }

        $ext   = strtolower(pathinfo($newfile->getFilename(), PATHINFO_EXTENSION));
        $types = array_map('strtolower', $upload_type['file_types']);

        if (!in_array($ext, $types) && $upload_type['type'] === 'deny') {
            return sprintf(
                _('Sie dürfen nur die Dateitypen %s hochladen!'),
                join(',', $upload_type['file_types'])
            );
        }

        if (in_array($ext, $types) && $upload_type['type'] === 'allow') {
            return sprintf(_('Sie dürfen den Dateityp %s nicht hochladen!'), $ext);
        }
    }

    /**
     * @return FolderType[]
     */
    public function getSubfolders()
    {
        // We must load the subfolders from the database instead
        // of using $this->folderdata->subfolders, because subfolders
        // that have been added to this folder aren't included in
        // $this->folderdata->subfolders although they are in the database.
        $subfolders = [];
        $database_subfolders = Folder::findByParent_id($this->getId(), "ORDER BY name");
        foreach ($database_subfolders as $subfolder) {
            //check FolderType of subfolder
            $subfolders[] = $subfolder->getTypedFolder();
        }
        return $subfolders;
    }

    /**
     * @return FileType[]
     */
    public function getFiles()
    {
        $filerefs = FileRef::findByFolder_id($this->getId(), "ORDER BY name");
        $files = [];
        foreach ($filerefs as $fileref) {
            $class = $fileref->file['filetype'];
            if (class_exists($class) && is_a($class, "StandardFile", true)) {
                $files[] = new $class($fileref);
            } else {
                $files[] = new UnknownFileType($fileref);
            }
        }
        NotificationCenter::postNotification("StandardFolderCollectsFiles", $this, $user_data = null);
        return $files;
    }

    /**
     * Returns the parent-folder as a StandardFolder
     * @return FolderType
     */
    public function getParent()
    {
        return $this->folderdata->parentfolder
             ? $this->folderdata->parentfolder->getTypedFolder()
             : null;
    }

    public function addFile(FileType $file, $user_id = null)
    {
        $file = $file->convertToStandardFile();

        $filename = $original_filename = $file->getFilename();
        $ext = pathinfo($original_filename, PATHINFO_EXTENSION);
        if ($ext) {
            $name = substr($original_filename, 0, -mb_strlen('.' . $ext));
        } else {
            $name = $original_filename;
        }
        $files = $this->getFiles();
        $c = 1;

        //rename file if name already exists in folder:
        do {
            $name_available = true;
            foreach ($files as $folderfile) {
                if ($folderfile->getFilename() === $filename) {
                    $name_available = false;
                    $filename;
                    $filename = $name . '[' . ++$c . ']';
                    if ($ext) {
                        $filename .= '.' . $ext;
                    }
                    break;
                }
            }
        } while (!$name_available);
        return $file->addToFolder($this, $filename, $user_id);
    }

    /**
     * @param string $file_ref_id
     * @return int
     */
    public function deleteFile($file_ref_id)
    {
        $file_ref = $this->folderdata->file_refs->find($file_ref_id);

        if (!$this->isFileWritable($file_ref->id, $GLOBALS['user']->id)) {
            return [sprintf(
                _('Ungenügende Berechtigungen zum Löschen der Datei %s in Ordner %s!'),
                $file_ref->name,
                $this->name
            )];
        }

        if ($file_ref) {
            return $file_ref->delete();
        }

        return 0;
    }


    /**
     * @param FolderType $foldertype
     * @return FolderType|null
     */
    public function createSubfolder(FolderType $foldertype)
    {
        if (!$foldertype->user_id) {
            $foldertype->user_id = $GLOBALS['user']->id;
        }
        $type = get_class($foldertype);
        if (!$type::availableInRange($this->folderdata['range_id'], $foldertype->user_id)) {
            $foldertype = new StandardFolder($foldertype);
        }
        $foldertype->range_id   = $this->folderdata['range_id'];
        $foldertype->range_type = $this->folderdata['range_type'];
        $foldertype->parent_id  = $this->folderdata['id'];
        if ($foldertype->store()) {
            return $foldertype;
        }

        #In case something went wrong while storing the subfolder:
        return null;
    }

    /**
     * @param string $subfolder_id
     * @return bool
     */
    public function deleteSubfolder($subfolder_id)
    {
        $subfolders = $this->folderdata->subfolders;

        if ($subfolders) {
            foreach ($subfolders as $subfolder) {
                if ($subfolder->id === $subfolder_id) {
                    //we found the subfolder that shall be deleted
                    return $subfolder->delete();
                }
            }
        }

        //if no subfolders are present or the subfolder can't be found
        //we return false:
        return false;
    }

    /**
     * @return int
     */
    public function delete()
    {
        return $this->folderdata->delete();
    }

    /**
     * @param FileRef|string $fileref_or_id
     * @param string $user_id
     * @return bool
     */
    public function isFileDownloadable($fileref_or_id, $user_id)
    {
        $fileref = FileRef::toObject($fileref_or_id);
        if ($this->range_type === 'user') {
            return $user_id === $this->range_id;
        }

        if (in_array($this->range_type, ['course', 'institute'])) {
            if (is_object($fileref->terms_of_use)) {
                //terms of use are defined for this file!
                return $this->isReadable($user_id)
                    && $fileref->terms_of_use->isDownloadable($this->range_id, $this->range_type,true, $user_id);
            } else {
                return $this->isReadable($user_id)
                    && $GLOBALS['perm']->have_studip_perm('user', $this->range_id, $user_id);
            }
        }

        return false;
    }

    /**
     * @param FileRef|string $fileref_or_id
     * @param string $user_id
     * @return bool
     */
    public function isFileEditable($fileref_or_id, $user_id)
    {
        if ($this->range_type === 'user') {
            return $user_id === $this->range_id;
        }
        $fileref = FileRef::toObject($fileref_or_id);
        return $fileref->user_id === $user_id
            || $GLOBALS['perm']->have_studip_perm('tutor', $this->range_id, $user_id);
    }

    /**
     * Checks if a user has write permissions to a file.
     *
     * For standard folders write permissions are granted
     * if the user is the owner of the file or if the user has at least
     * tutor permissions on the Stud.IP object specified by range_id
     * (such objects may be courses or institutes for example).
     *
     * @param FileRef|string $fileref_or_id
     * @param string $user_id
     * @return bool
     */
    public function isFileWritable($fileref_or_id, $user_id)
    {
        if ($this->range_type === 'user') {
            return $user_id === $this->range_id;
        }
        $fileref = FileRef::toObject($fileref_or_id);
        return $fileref->user_id == $user_id
            || $GLOBALS['perm']->have_studip_perm('tutor', $this->range_id, $user_id);
    }

    /**
     * returns the object for the range_id of the folder
     *
     * @return Course|Institute|User
     */
    public function getRangeObject()
    {
        $range = null;
        if ($this->range_type && $this->range_id) {
            if (Context::getId() === $this->range_id) {
                $range = Context::get();
            } else {
                $range = call_user_func([ucfirst($this->range_type), 'find'], $this->range_id);
            }
        }
        return $range;
    }

    /**
     * Returns an associative array of additional colums with the index the id of the column
     * and their values as the localized names of the columns
     * @return array('col1' => _("Anfragestatus"))
     */
    public function getAdditionalColumns()
    {
        return [];
    }

    /**
     * Returns the content for that additional column, if it exists. You can return null a string
     * or a Flexi_Template as the content.
     * @param string $column_index
     * @return null|string|Flexi_Template
     */
    public function getContentForAdditionalColumn($column_index)
    {
        return null;
    }

    /**
     * Returns an integer or text that marks the value the content of the given column should be
     * ordered by.
     * @param string $column_index
     * @return mixed : order value
     */
    public function getAdditionalColumnOrderWeigh($column_index)
    {
        return 1;
    }

    /**
     * Returns an array of Studip\Button or Studip\LinkButton objects that get displayed
     * underneath the files-table.
     * @return array of Studip\Button or Studip\LinkButton
     */
    public function getAdditionalActionButtons()
    {
        return [];
    }

    /**
     * @see FolderType::copySettings()
     */
    public function copySettings()
    {
        return ['description' => $this->description];
    }

}