1<?php
2/**
3 * Upload Action Plugin:   Handle Upload and temporarily disabling cache of page.
4 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
5 *
6 * @author    Franz Häfner <fhaefner@informatik.tu-cottbus.de>
7 * @author    Randolf Rotta <rrotta@informatik.tu-cottbus.de>
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
12require_once DOKU_PLUGIN . 'action.php';
13require_once(DOKU_INC . 'inc/media.php');
14require_once(DOKU_INC . 'inc/infoutils.php');
15
16class action_plugin_upload extends DokuWiki_Action_Plugin {
17
18    function getInfo() {
19        return array(
20            'author' => 'Franz Häfner',
21            'email' => 'fhaefner@informatik.tu-cottbus.de',
22            'date' => '2010-09-07',
23            'name' => 'upload plugin',
24            'desc' => 'upload plugin can add a link to the media manager in your wikipage.
25            			Basic syntax: {{upload>namespace|option1|option2}}
26                Use @page@ as namespage to use ID of the actual page as namespace or @current@ to use the namespace the current page is in.',
27            'url' => 'https://www.dokuwiki.org/plugin:upload',
28        );
29    }
30
31    /**
32     * Register its handlers with the DokuWiki's event controller
33     */
34    function register(Doku_Event_Handler $controller) {
35        $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, '_hook_function_cache');
36        $controller->register_hook('ACTION_HEADERS_SEND', 'BEFORE', $this, '_hook_function_upload');
37    }
38
39    function _hook_function_cache(&$event, $param) {
40        if($_FILES['upload']['tmp_name']) {
41            $event->preventDefault();
42            $event->stopPropagation();
43            $event->result = false;
44        }
45
46        $namespace = p_get_metadata($event->data->page, 'has_upload_form');
47        if(!empty($namespace)) {
48            $event->data->key .= '|ACL' . auth_quickaclcheck($namespace);
49            $event->data->cache = getCacheName($event->data->key, $event->data->ext);
50        }
51    }
52
53    function _hook_function_upload(&$event, $param) {
54        global $lang;
55        global $INPUT;
56        // get namespace to display (either direct or from deletion order)
57        $NS = $_POST['ns'];
58        $NS = cleanID($NS);
59
60        // check auth
61        $AUTH = auth_quickaclcheck("$NS:*");
62        if($AUTH < AUTH_UPLOAD) {
63            msg($lang['uploadfail'], -1);
64            return;
65        }
66
67        // handle upload
68        if($_FILES['upload']['tmp_name']) {
69            $_POST['mediaid'] = $INPUT->post->str('new_name');
70            $JUMPTO = media_upload($NS, $AUTH);
71            if($JUMPTO) {
72                $NS = getNS($JUMPTO);
73                $ID = $INPUT->post->str('page');
74                $NS = getNS($ID);
75            }
76        }
77    }
78}
79