1<?php
2
3namespace dokuwiki\Ui;
4
5use dokuwiki\Extension\Event;
6use dokuwiki\Form\Form;
7
8/**
9 * DokuWiki Page Editor
10 *
11 * @package dokuwiki\Ui
12 */
13class Editor extends Ui
14{
15    /**
16     * Display the Edit Window
17     * preprocess edit form data
18     *
19     * @author   Andreas Gohr <andi@splitbrain.org>
20     *
21     * @triggers EDIT_FORM_ADDTEXTAREA
22     * @return void
23     */
24    public function show()
25    {
26        global $INPUT;
27        global $ID;
28        global $REV;
29        global $DATE;
30        global $PRE;
31        global $SUF;
32        global $INFO;
33        global $SUM;
34        global $lang;
35        global $conf;
36        global $TEXT;
37
38        global $license;
39
40        if ($INPUT->has('changecheck')) {
41            $check = $INPUT->str('changecheck');
42        } elseif (!$INFO['exists']) {
43            // $TEXT has been loaded from page template
44            $check = md5('');
45        } else {
46            $check = md5($TEXT);
47        }
48        $mod = (md5($TEXT) !== $check);
49
50        $wr = $INFO['writable'] && !$INFO['locked'];
51
52        // intro locale text (edit, rditrev, or read)
53        if ($wr) {
54            $intro = ($REV) ? 'editrev' : 'edit';
55        } else {
56            // check pseudo action 'source'
57            if (!actionOK('source')) {
58                msg('Command disabled: source', -1);
59                return;
60            }
61            $intro = 'read';
62        }
63
64        // create the Editor form
65        $form = new Form(['id' => 'dw__editform']);
66        $form->setHiddenField('id', $ID);
67        $form->setHiddenField('rev', $REV);
68        $form->setHiddenField('date', $DATE);
69        $form->setHiddenField('prefix', $PRE .'.');
70        $form->setHiddenField('suffix', $SUF);
71        $form->setHiddenField('changecheck', $check);
72
73        // prepare data for EDIT_FORM_ALTERNATE event
74        $data = array(
75            'form' => $form,
76            'wr'   => $wr,
77            'media_manager' => true,
78            'target' => ($INPUT->has('target') && $wr) ? $INPUT->str('target') : 'section',
79            'intro_locale' => $intro,
80        );
81
82        if ($data['target'] !== 'section') {
83            // Only emit event if page is writable, section edit data is valid and
84            // edit target is not section.
85            Event::createAndTrigger('EDIT_FORM_ADDTEXTAREA', $data, [$this,'addTextarea'], true);
86        } else {
87            $this->addTextarea($data);
88        }
89
90        $form->setHiddenField('target', $data['target']);
91
92        if ($INPUT->has('hid')) {
93            $form->setHiddenField('hid', $INPUT->str('hid'));
94        }
95        if ($INPUT->has('codeblockOffset')) {
96            $form->setHiddenField('codeblockOffset', $INPUT->str('codeblockOffset'));
97        }
98
99        $form->addTagOpen('div')->id('wiki__editbar')->addClass('editBar');
100
101        $form->addTagOpen('div')->id('size__ctl');
102        $form->addTagClose('div');
103
104        if ($wr) {
105            // add edit buttons: save, preview, cancel
106            $form->addTagOpen('div')->addClass('editButtons');
107            $form->addButton('do[save]', $lang['btn_save'])->attr('type', 'submit')
108                ->attrs(['accesskey' => 's', 'tabindex' => '4'])
109                ->id('edbtn__save');
110            $form->addButton('do[preview]', $lang['btn_preview'])->attr('type', 'submit')
111                ->attrs(['accesskey' => 'p', 'tabindex' => '5'])
112                ->id('edbtn__preview');
113            $form->addButton('do[cancel]', $lang['btn_cancel'])->attr('type', 'submit')
114                ->attrs(['tabindex' => '6']);
115            $form->addTagClose('div'); // close div editButtons class
116
117            // add a textbox for edit summary
118            $form->addTagOpen('div')->addClass('summary');
119            $input = $form->addTextInput('summary', $lang['summary'])
120                ->attrs(['size' => '50', 'tabindex' => '2'])
121                ->id('edit__summary')->addClass('edit')
122                ->val($SUM);
123            $input->getLabel()->attr('class', 'nowrap');
124
125            // adds a checkbox for minor edits for logged in users
126            if ($conf['useacl'] && $INPUT->server->str('REMOTE_USER')) {
127                $form->addHTML(' ');
128                $form->addCheckbox('minor', $lang['minoredit'])->id('edit__minoredit')->addClass('nowrap')->val('1');
129            }
130            $form->addTagClose('div'); // close div summary class
131        }
132
133        $form->addTagClose('div'); // close div editBar class
134
135        // license note
136        if ($wr && $conf['license']) {
137            $attr = array(
138                'href'   => $license[$conf['license']]['url'],
139                'rel'    => 'license',
140                'class'  => 'urlextern',
141                'target' => $conf['target']['extern'] ? $conf['target']['extern'] : '',
142            );
143            $form->addTagOpen('div')->addClass('license');
144            $form->addHTML($lang['licenseok']
145                .' <a '.buildAttributes($attr, true).'>'.$license[$conf['license']]['name'].'</a>'
146            );
147            $form->addTagClose('div');
148        }
149
150        // start editor html output
151        if ($wr) {
152            // sets changed to true when previewed
153            echo '<script>/*<![CDATA[*/'.'textChanged = '. ($mod ? 'true' : 'false') .'/*!]]>*/</script>';
154        }
155
156        // print intro locale text (edit, rditrev, or read.txt)
157        if (isset($data['intro_locale'])) {
158            echo p_locale_xhtml($data['intro_locale']);
159        }
160
161        echo '<div class="editBox" role="application">';
162
163        echo '<div class="toolbar group">';
164        echo '<div id="tool__bar" class="tool__bar">';
165        if ($wr && $data['media_manager']) {
166            echo '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.$INFO['namespace'].'" target="_blank">';
167            echo $lang['mediaselect'];
168            echo '</a>';
169        }
170        echo '</div>';
171        echo '</div>';
172
173        echo '<div id="draft__status" class="draft__status">';
174        $draft = new \dokuwiki\Draft($ID, $INFO['client']);
175        if ($draft->isDraftAvailable()) {
176            echo $draft->getDraftMessage();
177        }
178        echo '</div>';
179
180        echo $form->toHTML('Edit');
181
182        echo '</div>'; // close div editBox class
183    }
184
185    /**
186     * Display the default edit form (textarea)
187     *
188     * the default action for EDIT_FORM_ADDTEXTAREA
189     *
190     * @param mixed[] $data
191     */
192    public function addTextarea(&$data)
193    {
194        global $TEXT;
195
196        if ($data['target'] !== 'section') {
197            msg('No editor for edit target '. hsc($data['target']) .' found.', -1);
198        }
199
200        // set textarea attributes
201        $attr = array('tabindex' => '1');
202        if (!$data['wr']) $attr['readonly'] = 'readonly';
203        $attr['dir']  = 'auto';
204        $attr['cols'] = '80';
205        $attr['rows'] = '10';
206
207        /** @var dokuwiki\Form\Form $data['form'] */
208        $data['form']->addTextarea('wikitext','')->attrs($attr)->val($TEXT)
209                ->id('wiki__text')->addClass('edit');
210    }
211
212}
213