1<?php
2
3use dokuwiki\File\PageResolver;
4
5/**
6 * Simple template replacement action for the bureaucracy plugin
7 *
8 * @author Michael Klier <chi@chimeric.de>
9 */
10
11class helper_plugin_bureaucracy_actiontemplate extends helper_plugin_bureaucracy_action {
12
13    var $targetpages;
14    var $pagename;
15
16    /**
17     * Performs template action
18     *
19     * @param helper_plugin_bureaucracy_field[] $fields  array with form fields
20     * @param string $thanks  thanks message
21     * @param array  $argv    array with entries: template, pagename, separator
22     * @return array|mixed
23     *
24     * @throws Exception
25     */
26    public function run($fields, $thanks, $argv) {
27        global $conf;
28
29        [$tpl, $this->pagename] = $argv;
30        $sep = $argv[2] ?? $conf['sepchar'];
31
32        $this->patterns = array();
33        $this->values   = array();
34        $this->targetpages = array();
35
36        $this->prepareNamespacetemplateReplacements();
37        $this->prepareDateTimereplacements();
38        $this->prepareLanguagePlaceholder();
39        $this->prepareNoincludeReplacement();
40        $this->prepareFieldReplacements($fields);
41
42        $evdata = array(
43            'patterns' => &$this->patterns,
44            'values' => &$this->values,
45            'fields' => $fields,
46            'action' => $this
47        );
48
49        $event = new Doku_Event('PLUGIN_BUREAUCRACY_PAGENAME', $evdata);
50        if ($event->advise_before()) {
51            $this->buildTargetPagename($fields, $sep);
52        }
53        $event->advise_after();
54
55        //target&template(s) from addpage fields
56        $this->getAdditionalTargetpages($fields);
57        //target&template(s) from action field
58        $tpl = $this->getActionTargetpages($tpl);
59
60        if(empty($this->targetpages)) {
61            throw new Exception(sprintf($this->getLang('e_template'), $tpl));
62        }
63
64        $this->checkTargetPageNames();
65
66        $this->processUploads($fields);
67        $this->replaceAndSavePages($fields);
68
69        $ret = $this->buildThankYouPage($thanks);
70
71        return $ret;
72    }
73
74    /**
75     * Prepare and resolve target page
76     *
77     * @param helper_plugin_bureaucracy_field[]  $fields  List of field objects
78     * @param string                             $sep     Separator between fields for page id
79     * @throws Exception missing pagename
80     */
81    protected function buildTargetPagename($fields, $sep) {
82        global $ID;
83
84        foreach ($fields as $field) {
85            $pname = $field->getParam('pagename');
86            if (!is_null($pname)) {
87                if (is_array($pname)) $pname = implode($sep, $pname);
88                $this->pagename .= $sep . $pname;
89            }
90        }
91
92        $resolver = new PageResolver($ID);
93        $this->pagename = $resolver->resolveId($this->replace($this->pagename));
94
95        if ($this->pagename === '') {
96            throw new Exception($this->getLang('e_pagename'));
97        }
98    }
99
100    /**
101     * Handle templates from addpage field
102     *
103     * @param helper_plugin_bureaucracy_field[]  $fields  List of field objects
104     * @return array
105     */
106    function getAdditionalTargetpages($fields) {
107        global $ID;
108
109        foreach ($fields as $field) {
110            if (!is_null($field->getParam('page_tpl')) && !is_null($field->getParam('page_tgt')) ) {
111                $resolver = new PageResolver($ID);
112
113                //template
114                $templatepage = $this->replace($field->getParam('page_tpl'));
115                $templatepage = $resolver->resolveId($templatepage);
116
117                //target
118                $relativetargetpage = $resolver->resolveId($field->getParam('page_tgt'));
119                $targetpage = "$this->pagename:$relativetargetpage";
120
121                $auth = $this->aclcheck($templatepage); // runas
122                if ($auth >= AUTH_READ ) {
123                    $this->addParsedTargetpage($targetpage, $templatepage);
124                }
125            }
126        }
127    }
128
129    /**
130     * Returns raw pagetemplate contents for the ID's namespace
131     *
132     * @param string $id the id of the page to be created
133     * @return string raw pagetemplate content
134     */
135    protected function rawPageTemplate($id) {
136        global $conf;
137
138        $path = dirname(wikiFN($id));
139        if(file_exists($path.'/_template.txt')) {
140            $tplfile = $path.'/_template.txt';
141        } else {
142            // search upper namespaces for templates
143            $len = strlen(rtrim($conf['datadir'], '/'));
144            while(strlen($path) >= $len) {
145                if(file_exists($path.'/__template.txt')) {
146                    $tplfile = $path.'/__template.txt';
147                    break;
148                }
149                $path = substr($path, 0, strrpos($path, '/'));
150            }
151        }
152
153        $tpl = io_readFile($tplfile);
154        return $tpl;
155    }
156
157    /**
158     * Load template(s) for targetpage as given via action field
159     *
160     * @param string $tpl    template name as given in form
161     * @return string parsed templatename
162     */
163    protected function getActionTargetpages($tpl) {
164        global $USERINFO;
165        global $conf;
166        global $ID;
167        $runas = $this->getConf('runas');
168
169        if ($tpl == '_') {
170            // use namespace template
171            if (!isset($this->targetpages[$this->pagename])) {
172                $raw = $this->rawPageTemplate($this->pagename);
173                $this->noreplace_save($raw);
174                $this->targetpages[$this->pagename] = pageTemplate(array($this->pagename));
175            }
176        } elseif ($tpl !== '!') {
177            $tpl = $this->replace($tpl);
178
179            // resolve templates, but keep references to whole namespaces intact (ending in a colon)
180            $resolver = new PageResolver($ID);
181            if(substr($tpl, -1) == ':') {
182                $tpl = $tpl.'xxx'; // append a fake page name
183                $tpl = $resolver->resolveId($tpl);
184                $tpl = substr($tpl, 0, -3); // cut off fake page name again
185            } else {
186                $tpl = $resolver->resolveId($tpl);
187            }
188
189            $backup = array();
190            if ($runas) {
191                // Hack user credentials.
192                $backup = array($_SERVER['REMOTE_USER'], $USERINFO['grps']);
193                $_SERVER['REMOTE_USER'] = $runas;
194                $USERINFO['grps'] = array();
195            }
196
197            $template_pages = array();
198            //search checks acl (as runas)
199            $opts = array(
200                'depth' => 0,
201                'listfiles' => true,
202                'showhidden' => true
203            );
204            search($template_pages, $conf['datadir'], 'search_universal', $opts, str_replace(':', '/', getNS($tpl)));
205
206            foreach ($template_pages as $template_page) {
207                $templatepageid = cleanID($template_page['id']);
208                // try to replace $tpl path with $this->pagename path in the founded $templatepageid
209                // - a single-page template will only match on itself and will be replaced,
210                //   other newtargets are pages in same namespace, so aren't changed
211                // - a namespace as template will match at the namespaces-part of the path of pages in this namespace
212                //   so these newtargets are changed
213                // if there exist a single-page and a namespace with name $tpl, both are selected
214                $newTargetpageid = preg_replace('/^' . preg_quote_cb(cleanID($tpl)) . '($|:)/', $this->pagename . '$1', $templatepageid);
215
216                if ($newTargetpageid === $templatepageid) {
217                    // only a single-page template or page in the namespace template
218                    // which matches the $tpl path are changed
219                    continue;
220                }
221
222                if (!isset($this->targetpages[$newTargetpageid])) {
223                    $this->addParsedTargetpage($newTargetpageid, $templatepageid);
224                }
225            }
226
227            if ($runas) {
228                /* Restore user credentials. */
229                list($_SERVER['REMOTE_USER'], $USERINFO['grps']) = $backup;
230            }
231        }
232        return $tpl;
233    }
234
235    /**
236     * Checks for existance and access of target pages
237     *
238     * @return mixed
239     * @throws Exception
240     */
241    protected function checkTargetPageNames() {
242        foreach (array_keys($this->targetpages) as $pname) {
243            // prevent overriding already existing pages
244            if (page_exists($pname)) {
245                throw new Exception(sprintf($this->getLang('e_pageexists'), html_wikilink($pname)));
246            }
247
248            $auth = $this->aclcheck($pname);
249            if ($auth < AUTH_CREATE) {
250                throw new Exception($this->getLang('e_denied'));
251            }
252        }
253    }
254
255    /**
256     * Perform replacements on the collected templates, and save the pages.
257     *
258     * Note: wrt runas, for changelog are used:
259     *  - $INFO['userinfo']['name']
260     *  - $INPUT->server->str('REMOTE_USER')
261     */
262    protected function replaceAndSavePages($fields) {
263        global $ID;
264        foreach ($this->targetpages as $pageName => $template) {
265            // set NSBASE var to make certain dataplugin constructs easier
266            $this->patterns['__nsbase__'] = '/@NSBASE@/';
267            $this->values['__nsbase__'] = noNS(getNS($pageName));
268
269            $evdata = array(
270                'patterns' => &$this->patterns,
271                'values' => &$this->values,
272                'id' => $pageName,
273                'template' => $template,
274                'form' => $ID,
275                'fields' => $fields
276            );
277
278            $event = new Doku_Event('PLUGIN_BUREAUCRACY_TEMPLATE_SAVE', $evdata);
279            if($event->advise_before()) {
280                // save page
281                saveWikiText(
282                    $evdata['id'],
283                    cleanText($this->replace($evdata['template'], false)),
284                    sprintf($this->getLang('summary'), $ID)
285                );
286            }
287            $event->advise_after();
288        }
289    }
290
291    /**
292     * (Callback) Sorts first by namespace depth, next by page ids
293     *
294     * @param string $a
295     * @param string $b
296     * @return int positive if $b is in deeper namespace than $a, negative higher.
297     *             further sorted by pageids
298     *
299     *  return an integer less than, equal to, or
300     * greater than zero if the first argument is considered to be
301     * respectively less than, equal to, or greater than the second.
302     */
303    public function _sorttargetpages($a, $b) {
304        $ns_diff = substr_count($a, ':') - substr_count($b, ':');
305        return ($ns_diff === 0) ? strcmp($a, $b) : ($ns_diff > 0 ? -1 : 1);
306    }
307
308    /**
309     * (Callback) Build content of item
310     *
311     * @param array $item
312     * @return string
313     */
314    public function html_list_index($item){
315        $ret = '';
316        if($item['type']=='f'){
317            $ret .= html_wikilink(':'.$item['id']);
318        } else {
319            $ret .= '<strong>' . trim(substr($item['id'], strrpos($item['id'], ':', -2)), ':') . '</strong>';
320        }
321        return $ret;
322    }
323
324    /**
325     * Build thanks message, trigger indexing and rendering of new pages.
326     *
327     * @param string $thanks
328     * @return string html of thanks message or when redirect the first page id of created pages
329     */
330    protected function buildThankYouPage($thanks) {
331        global $ID;
332        $backupID = $ID;
333
334        $html = "<p>$thanks</p>";
335
336        // Build result tree
337        $pages = array_keys($this->targetpages);
338        usort($pages, array($this, '_sorttargetpages'));
339
340        $data = array();
341        $last_folder = array();
342        foreach ($pages as $ID) {
343            $lvl = substr_count($ID, ':');
344            for ($n = 0; $n < $lvl; ++$n) {
345                if (!isset($last_folder[$n]) || strpos($ID, $last_folder[$n]['id']) !== 0) {
346                    $last_folder[$n] = array(
347                        'id' => substr($ID, 0, strpos($ID, ':', ($n > 0 ? strlen($last_folder[$n - 1]['id']) : 0) + 1) + 1),
348                        'level' => $n + 1,
349                        'open' => 1,
350                        'type' => null,
351                    );
352                    $data[] = $last_folder[$n];
353                }
354            }
355            $data[] = array('id' => $ID, 'level' => 1 + substr_count($ID, ':'), 'type' => 'f');
356        }
357        $index = new dokuwiki\Ui\Index();
358        $html .= html_buildlist($data, 'idx', array($this, 'html_list_index'), array($index, 'tagListItem'));
359
360        // Add indexer bugs for every just-created page
361        $html .= '<div class="no">';
362        ob_start();
363        foreach ($pages as $ID) {
364            // indexerWebBug uses ID and INFO[exists], but the bureaucracy form
365            // page always exists, as does the just-saved page, so INFO[exists]
366            // is correct in any case
367            tpl_indexerWebBug();
368
369            // the iframe will trigger real rendering of the pages to make sure
370            // any used plugins are initialized (eg. the do plugin)
371            echo '<iframe src="' . wl($ID, array('do' => 'export_html')) . '" width="1" height="1" style="visibility:hidden"></iframe>';
372        }
373        $html .= ob_get_contents();
374        ob_end_clean();
375        $html .= '</div>';
376
377        $ID = $backupID;
378        return $html;
379    }
380
381    /**
382     * move the uploaded files to <pagename>:FILENAME
383     *
384     *
385     * @param helper_plugin_bureaucracy_field[] $fields
386     * @throws Exception
387     */
388    protected function processUploads($fields) {
389        foreach($fields as $field) {
390
391            if($field->getFieldType() !== 'file') continue;
392
393            $label = $field->getParam('label');
394            $file  = $field->getParam('file');
395            $ns    = $field->getParam('namespace');
396
397            //skip empty files
398            if(!$file['size']) {
399                $this->values[$label] = '';
400                continue;
401            }
402
403            $id = $ns.':'.$file['name'];
404            resolve_mediaid($this->pagename, $id, $ignored); // resolve relatives
405
406            $auth = $this->aclcheck($id); // runas
407            $move = 'copy_uploaded_file';
408            //prevent from is_uploaded_file() check
409            if(defined('DOKU_UNITTEST')) {
410                $move = 'copy';
411            }
412            $res = media_save(
413                array('name' => $file['tmp_name']),
414                $id,
415                false,
416                $auth,
417                $move);
418
419            if(is_array($res)) throw new Exception($res[0]);
420
421            $this->values[$label] = $res;
422
423        }
424    }
425
426    /**
427     * Load page data and do default pattern replacements like namespace templates do
428     * and add it to list of targetpages
429     *
430     * Note: for runas the values of the real user are used for the placeholders
431     *       @NAME@ => $USERINFO['name']
432     *       @MAIL@ => $USERINFO['mail']
433     *       and the replaced value:
434     *       @USER@ => $INPUT->server->str('REMOTE_USER')
435     *
436     * @param string $targetpageid   pageid of destination
437     * @param string $templatepageid pageid of template for this targetpage
438     */
439    protected function addParsedTargetpage($targetpageid, $templatepageid) {
440        $tpl = rawWiki($templatepageid);
441        $this->noreplace_save($tpl);
442
443        $data = array(
444            'id' => $targetpageid,
445            'tpl' => $tpl,
446            'doreplace' => true,
447        );
448        parsePageTemplate($data);
449
450        //collect and apply some other replacements
451        $patterns = array();
452        $values = array();
453        $keys = array('__lang__', '__trans__', '__year__', '__month__', '__day__', '__time__');
454        foreach($keys as $key) {
455            $patterns[$key] = $this->patterns[$key];
456            $values[$key] = $this->values[$key];
457        }
458
459        $this->targetpages[$targetpageid] = preg_replace($patterns, $values, $data['tpl']);
460    }
461
462}
463// vim:ts=4:sw=4:et:enc=utf-8:
464