xref: /dokuwiki/lib/exe/mediamanager.php (revision 093fe67e98c0cdb4b73fd46938e49b64971483c2)
1<?php
2
3use dokuwiki\Extension\Event;
4
5if (!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../');
6define('DOKU_MEDIAMANAGER', 1);
7
8// for multi uploader:
9@ini_set('session.use_only_cookies', 0);
10
11require_once(DOKU_INC . 'inc/init.php');
12
13global $INPUT;
14global $lang;
15global $conf;
16// handle passed message
17if ($INPUT->str('msg1')) msg(hsc($INPUT->str('msg1')), 1);
18if ($INPUT->str('err')) msg(hsc($INPUT->str('err')), -1);
19
20global $DEL;
21// get namespace to display (either direct or from deletion order)
22if ($INPUT->str('delete')) {
23    $DEL = cleanID($INPUT->str('delete'));
24    $IMG = $DEL;
25    $NS = getNS($DEL);
26} elseif ($INPUT->str('edit')) {
27    $IMG = cleanID($INPUT->str('edit'));
28    $NS = getNS($IMG);
29} elseif ($INPUT->str('img')) {
30    $IMG = cleanID($INPUT->str('img'));
31    $NS = getNS($IMG);
32} else {
33    $NS = cleanID($INPUT->str('ns'));
34    $IMG = null;
35}
36
37global $INFO, $JSINFO;
38$INFO = empty($INFO) ? mediainfo() : array_merge($INFO, mediainfo());
39$JSINFO['id'] = '';
40$JSINFO['namespace'] = '';
41$AUTH = $INFO['perm'];    // shortcut for historical reasons
42
43// If this page is directly opened it means we are in popup mode not fullscreen
44// $fullscreen isn't defined by default it might lead to some PHP warnings
45$fullscreen ??= false;
46
47$tmp = [];
48Event::createAndTrigger('MEDIAMANAGER_STARTED', $tmp);
49session_write_close();  //close session
50
51// do not display the manager if user does not have read access
52if ($AUTH < AUTH_READ && !$fullscreen) {
53    http_status(403);
54    die($lang['accessdenied']);
55}
56
57// handle flash upload
58if (isset($_FILES['Filedata'])) {
59    $_FILES['upload'] =& $_FILES['Filedata'];
60    $JUMPTO = media_upload($NS, $AUTH);
61    if ($JUMPTO == false) {
62        http_status(400);
63        echo 'Upload failed';
64    }
65    echo 'ok';
66    exit;
67}
68
69// give info on PHP caught upload errors
70if (!empty($_FILES['upload']['error'])) {
71    match ($_FILES['upload']['error']) {
72        1, 2 => msg(sprintf(
73            $lang['uploadsize'],
74            filesize_h(php_to_byte(ini_get('upload_max_filesize')))
75        ), -1),
76        default => msg($lang['uploadfail'] . ' (' . $_FILES['upload']['error'] . ')', -1),
77    };
78    unset($_FILES['upload']);
79}
80
81// handle upload
82if (!empty($_FILES['upload']['tmp_name'])) {
83    $JUMPTO = media_upload($NS, $AUTH);
84    if ($JUMPTO) $NS = getNS($JUMPTO);
85}
86
87// handle meta saving
88if ($IMG && @array_key_exists('save', $INPUT->arr('do'))) {
89    $JUMPTO = media_metasave($IMG, $AUTH, $INPUT->arr('meta'));
90}
91
92if ($IMG && ($INPUT->str('mediado') == 'save' || @array_key_exists('save', $INPUT->arr('mediado')))) {
93    $JUMPTO = media_metasave($IMG, $AUTH, $INPUT->arr('meta'));
94}
95
96if ($INPUT->int('rev') && $conf['mediarevisions']) $REV = $INPUT->int('rev');
97
98if ($INPUT->str('mediado') == 'restore' && $conf['mediarevisions'] && checkSecurityToken()) {
99    $JUMPTO = media_restore($INPUT->str('image'), $REV, $AUTH);
100}
101
102// handle deletion
103if ($DEL) {
104    $res = 0;
105    if (checkSecurityToken()) {
106        $res = media_delete($DEL, $AUTH);
107    }
108    if ($res & DOKU_MEDIA_DELETED) {
109        $msg = sprintf($lang['deletesucc'], noNS($DEL));
110        if ($res & DOKU_MEDIA_EMPTY_NS && !$fullscreen) {
111            // current namespace was removed. redirecting to root ns passing msg along
112            send_redirect(DOKU_URL . 'lib/exe/mediamanager.php?msg1=' .
113                rawurlencode($msg) . '&edid=' . $INPUT->str('edid'));
114        }
115        msg($msg, 1);
116    } elseif ($res & DOKU_MEDIA_INUSE) {
117        msg(sprintf($lang['mediainuse'], noNS($DEL)), 0);
118    } else {
119        msg(sprintf($lang['deletefail'], noNS($DEL)), -1);
120    }
121}
122// finished - start output
123
124if (!$fullscreen) {
125    header('Content-Type: text/html; charset=utf-8');
126    include(template('mediamanager.php'));
127}
128