1<?php
2/**
3 * Upload plugin, allows upload for users with correct
4 * permission fromin a wikipage to a defined namespace.
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 *
7 * @author Christian Moll <christian@chrmoll.de>
8 * @author    Franz Häfner <fhaefner@informatik.tu-cottbus.de>
9 * @author    Randolf Rotta <rrotta@informatik.tu-cottbus.de>
10 */
11
12if(!defined('NL')) define('NL', "\n");
13if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__) . '/../../');
14if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once(DOKU_PLUGIN . 'syntax.php');
16require_once(DOKU_INC . 'inc/media.php');
17require_once(DOKU_INC . 'inc/auth.php');
18
19require_once(DOKU_INC . 'inc/infoutils.php');
20
21class syntax_plugin_upload extends DokuWiki_Syntax_Plugin {
22
23    function getInfo() {
24        return array(
25            'author' => 'Franz Häfner',
26            'email' => 'fhaefner@informatik.tu-cottbus.de',
27            'date' => '2010-09-07',
28            'name' => 'upload plugin',
29            'desc' => 'upload plugin can add a link to the media manager in your wikipage.
30            			Basic syntax: {{upload>namespace|option1|option2}}
31				Use @page@ as namespage to use ID of the actual page as namespace or @current@ to use the namespace the current page is in.',
32            'url' => 'http://wiki.splitbrain.org/plugin:upload',
33        );
34    }
35
36    function getType() {
37        return 'substition';
38    }
39
40    function getSort() {
41        return 32;
42    }
43
44    function connectTo($mode) {
45        $this->Lexer->addSpecialPattern('\{\{upload>.+?\}\}', $mode, 'plugin_upload');
46    }
47
48    function handle($match, $state, $pos, Doku_Handler $handler) {
49        global $ID;
50
51        $match = substr($match, 9, -2);
52        $matches = explode('|', $match, 2);
53        $o = explode('|', $matches[1]);
54
55        $options['overwrite'] = in_array('OVERWRITE', $o);
56        $options['renameable'] = in_array('RENAMEABLE', $o);
57
58        $ns = $matches[0];
59
60        if($ns == '@page@') {
61            $ns = $ID;
62        } else if($ns == '@current@') {
63            $ns = getNS($ID);
64        } else {
65            resolve_pageid(getNS($ID), $ns, $exists);
66        }
67
68        return array('uploadns' => hsc($ns), 'para' => $options);
69    }
70
71    function render($mode, Doku_Renderer $renderer, $data) {
72        if($mode == 'xhtml') {
73            //check auth
74            $auth = auth_quickaclcheck($data['uploadns'] . ':*');
75
76            if($auth >= AUTH_UPLOAD) {
77                $renderer->doc .= $this->upload_plugin_uploadform($data['uploadns'], $auth, $data['para']);
78//				$renderer->info['cache'] = false;
79            }
80            return true;
81        } else if($mode == 'metadata') {
82            $renderer->meta['has_upload_form'] = $data['uploadns'] . ':*';
83            return true;
84        }
85        return false;
86    }
87
88    /**
89     * Print the media upload form if permissions are correct
90     *
91     * @author Christian Moll <christian@chrmoll.de>
92     * @author Andreas Gohr <andi@splitbrain.org>
93     * @author    Franz Häfner <fhaefner@informatik.tu-cottbus.de>
94     * @author    Randolf Rotta <rrotta@informatik.tu-cottbus.de>
95     */
96    function upload_plugin_uploadform($ns, $auth, $options) {
97        global $ID;
98        global $lang;
99        $html = '';
100
101        if($auth < AUTH_UPLOAD) return;
102
103        $params = array();
104        $params['id'] = 'upload_plugin';
105        $params['action'] = wl($ID);
106        $params['method'] = 'post';
107        $params['enctype'] = 'multipart/form-data';
108        $params['class'] = 'upload__plugin';
109
110        // Modification of the default dw HTML upload form
111        $form = new Doku_Form($params);
112        $form->startFieldset($lang['fileupload']);
113        $form->addElement(formSecurityToken());
114        $form->addHidden('page', hsc($ID));
115        $form->addHidden('ns', hsc($ns));
116        $form->addElement(form_makeFileField('upload', $lang['txt_upload'] . ':', 'upload__file'));
117        if($options['renameable']) {
118            // don't name this field here "id" because it is misinterpreted by DokuWiki if the upload form is not in media manager
119            $form->addElement(form_makeTextField('new_name', '', $lang['txt_filename'] . ':', 'upload__name'));
120        }
121
122        if($auth >= AUTH_DELETE) {
123            if($options['overwrite']) {
124                //$form->addElement(form_makeCheckboxField('ow', 1, $lang['txt_overwrt'], 'dw__ow', 'check'));
125                // circumvent wrong formatting in doku_form
126                $form->addElement(
127                    '<label class="check" for="dw__ow">' .
128                    '<span>' . $lang['txt_overwrt'] . '</span>' .
129                    '<input type="checkbox" id="dw__ow" name="ow" value="1"/>' .
130                    '</label>'
131                );
132            }
133        }
134        $form->addElement(form_makeButton('submit', '', $lang['btn_upload']));
135        $form->endFieldset();
136
137        $html .= '<div class="upload_plugin"><p>' . NL;
138        $html .= $form->getForm();
139        $html .= '</p></div>' . NL;
140        return $html;
141    }
142}
143
144//Setup VIM: ex: et ts=4 enc=utf-8 :
145