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    switch ($_FILES['upload']['error']) {
72        case 1:
73        case 2:
74            msg(sprintf(
75                $lang['uploadsize'],
76                filesize_h(php_to_byte(ini_get('upload_max_filesize')))
77            ), -1);
78            break;
79        default:
80            msg($lang['uploadfail'] . ' (' . $_FILES['upload']['error'] . ')', -1);
81    }
82    unset($_FILES['upload']);
83}
84
85// handle upload
86if (!empty($_FILES['upload']['tmp_name'])) {
87    $JUMPTO = media_upload($NS, $AUTH);
88    if ($JUMPTO) $NS = getNS($JUMPTO);
89}
90
91// handle meta saving
92if ($IMG && @array_key_exists('save', $INPUT->arr('do'))) {
93    $JUMPTO = media_metasave($IMG, $AUTH, $INPUT->arr('meta'));
94}
95
96if ($IMG && ($INPUT->str('mediado') == 'save' || @array_key_exists('save', $INPUT->arr('mediado')))) {
97    $JUMPTO = media_metasave($IMG, $AUTH, $INPUT->arr('meta'));
98}
99
100if ($INPUT->int('rev') && $conf['mediarevisions']) $REV = $INPUT->int('rev');
101
102if ($INPUT->str('mediado') == 'restore' && $conf['mediarevisions'] && checkSecurityToken()) {
103    $JUMPTO = media_restore($INPUT->str('image'), $REV, $AUTH);
104}
105
106// handle deletion
107if ($DEL) {
108    $res = 0;
109    if (checkSecurityToken()) {
110        $res = media_delete($DEL, $AUTH);
111    }
112    if ($res & DOKU_MEDIA_DELETED) {
113        $msg = sprintf($lang['deletesucc'], noNS($DEL));
114        if ($res & DOKU_MEDIA_EMPTY_NS && !$fullscreen) {
115            // current namespace was removed. redirecting to root ns passing msg along
116            send_redirect(DOKU_URL . 'lib/exe/mediamanager.php?msg1=' .
117                rawurlencode($msg) . '&edid=' . $INPUT->str('edid'));
118        }
119        msg($msg, 1);
120    } elseif ($res & DOKU_MEDIA_INUSE) {
121        msg(sprintf($lang['mediainuse'], noNS($DEL)), 0);
122    } else {
123        msg(sprintf($lang['deletefail'], noNS($DEL)), -1);
124    }
125}
126// finished - start output
127
128if (!$fullscreen) {
129    header('Content-Type: text/html; charset=utf-8');
130    include(template('mediamanager.php'));
131}
132