1<?php
2define("NEWPAGETPL_CMDL", 'php ' . DOKU_INC . 'bin/plugin.php newpagetemplate ');
3
4class admin_plugin_newpagetemplate extends DokuWiki_Admin_Plugin
5{
6
7    private $output = '';
8    private $help = false;
9
10    /**
11     * handle user request
12     */
13    function handle()
14    {
15
16        if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do
17
18        $this->output = '';
19        if (!checkSecurityToken()) return;
20        if (!is_array($_REQUEST['cmd'])) return;
21       // echo '<pre>' . print_r($_REQUEST, 1) . '</pre>';
22
23        global $INPUT;
24        $ini = $INPUT->str('ini_file','none');
25        $tpl = $INPUT->str('tpl_file','none');
26        $nosave = $INPUT->str('nosave','none');
27        $userepl = $INPUT->str('userrelpl','none');
28        $tplns = $this->getConf('default_tplns');
29        $user = $INPUT->server->str('REMOTE_USER', 'none');
30
31        $this->help = false;
32        switch (key($_REQUEST['cmd'])) {
33            case 'submit' :
34                $this->output = 'submit';
35                if(!empty($_REQUEST['id'])) {
36                    if($tpl == 'none') {
37                        $this->output = "The -p (--page) option requires a template";
38                        return;
39                    }
40                    $cmdL = '-p ' .$_REQUEST['id'] .  " -t $tplns:$tpl";
41                    if($userepl != 'none') {
42                        $cmdL .= " -u \"$userepl\" ";
43                    }
44                    $cmdL .= " -s admin ";
45                    if($nosave == 'on') {
46                        $cmdL .= " -n true ";
47                    }
48                    if($user != 'none') {
49                      $cmdL .= " -c $user ";
50                    }
51                    $this->output = shell_exec(NEWPAGETPL_CMDL  . $cmdL) ;
52                    $this->help = true;
53                }
54                else if($ini != 'none') {
55                    $cmdL = " -i $ini";
56                    if($userepl != 'none') {
57                        $cmdL .= " -u \"$userepl\" ";
58                    }
59                    $cmdL .= " -s admin ";
60                    if($nosave != 'none') {
61                        $cmdL .= " -n $nosave ";
62                    }
63                    if($user != 'none') {
64                      $cmdL .= " -c $user ";
65                    }
66                    $this->output = shell_exec(NEWPAGETPL_CMDL  . $cmdL) ;
67                    $this->help = true;
68                }
69                $this->output = preg_replace("/\n+/","<br />", $this->output);
70                break;
71             case 'help':
72                $this->help = true;
73                $this->output = shell_exec(NEWPAGETPL_CMDL  .'-h') ;
74                $this->output = preg_replace("/\n\n/","<br />", htmlentities($this->output));
75                $this->output = preg_replace("/(-\w\,\s+--\w+)/","<span style='color:blue;'>$1</span>",$this->output);
76                $this->output = preg_replace("/browser]]/","browser]]",$this->output);
77                $this->output = preg_replace("/OPTIONS:/","OPTIONS:<br />",$this->output);
78                $this->output = preg_replace("/https:\/\/www.dokuwiki.org\/plugin:newpagetemplate/",
79                "<a href='https://www.dokuwiki.org/plugin:newpagetemplate'>" .
80                'https://www.dokuwiki.org/plugin:newpagetemplate</a>',$this->output);
81
82                break;
83        }
84
85    }
86    /**
87     * output appropriate html
88     */
89    function html()
90    {
91
92       ptln('<div id="nptpl_howto" style="display:none;border:1px black solid;padding:12px 12px 12px 8px;height:400px;overflow:auto;">' );
93       ptln('<button  style = "float:right;" onclick="nptpl_toggle(\'#nptpl_howto\')">' . $this->getLang('close'). '</button>&nbsp;' . $this->locale_xhtml('howto'));
94       ptln('<button  style = "float:right" onclick="nptpl_toggle(\'#nptpl_howto\')">' . $this->getLang('close'). '</button>&nbsp;<br /><br /></div>');
95
96       ptln('<form action="' . wl($ID) . '" method="post">');
97        // output hidden values to ensure dokuwiki will return back to this plugin
98       ptln('  <input type="hidden" name="do"   value="admin" />');
99       ptln('  <input type="hidden" name="page" value="' . $this->getPluginName() . '" />');
100       formSecurityToken();
101
102        /*Select ini files */
103        ptln($this->getLang('select-ini') .': <select name="ini_file">');
104        $ini_files = $this->ini_files();
105        ptln($ini_files);
106        ptln('</select>&nbsp;&nbsp;');
107
108        /* Select Template */
109        ptln($this->getLang('templ') .': <select name="tpl_file">');
110        $tpls = $this->templates();
111        ptln($tpls);
112        ptln('</select>&nbsp;&nbsp;');
113
114        ptln($this->getLang('page') .':&nbsp;<input type="textbox" name="id"/>&nbsp;&nbsp;');
115        ptln($this->getLang('userrelpl') . ':&nbsp;<input type="textbox" name="userrelpl"/>');
116
117        ptln('<div style="line-height:2"><input type="submit" name="cmd[submit]"  value="' . $this->getLang('btn_submit') . '" />');
118
119        ptln('<input type = "button" onclick=" nptpl_toggle(\'#nptpl_howto\')" value ="'. $this->getLang('howto') .'">&nbsp;');
120        ptln('<input type="submit" name="cmd[help]"  value="' . $this->getLang('btn_help') . '" /></div>');
121        ptln('<div>'.$this->getLang('nosave') . '&nbsp;<input type="radio" checked  name="nosave" value = "true"/>&nbsp;');
122        ptln($this->getLang('overwrite') . '&nbsp;<input type="radio" name="nosave" value = "false"/>&nbsp;');
123        ptln($this->getLang('existing') . '&nbsp;<input type="radio" name="nosave" value="existing"/></div>');
124
125        ptln('</form>');
126
127        if($this->help) {
128            $display = "block";
129        }
130        else $display = "none";
131
132        ptln('<br /><div id="nptpl_output" style="display:'. $display .'; border:1px black solid;padding:12px 12px 12px 8px;height:400px;overflow:auto;">' . $this->output);
133        ptln('<button style = "display:'. $display .';" onclick=" nptpl_toggle(\'#nptpl_output\')">'. $this->getLang('close') .'</button>&nbsp;');
134        ptln('</div>');
135    }
136
137    function ini_files()
138    {
139        $lib = DOKU_INC . 'lib/plugins/newpagetemplate/';
140        $files = scandir($lib);
141        $opt_str = "<option value = 'none'>".$this->getLang('no_selection')."</option>";
142        foreach ($files as $file) {
143            if (preg_match("/\.ini$/", $file)) {
144                $opt_str .= "<option value = '$file'>$file</option>";
145            }
146        }
147        return ($opt_str);
148    }
149    function templates() {
150        $tplns = $this->getConf('default_tplns');
151        $pages = DOKU_INC . 'data/pages/' . $tplns;
152        $files = scandir($pages);
153        $opt_str = "<option value='none'>".$this->getLang('no_selection')."</option>";
154        foreach ($files as $file) {
155            if (preg_match("/\.txt$/", $file)) {
156                list($id,$ext) = explode('.',$file);
157                $opt_str .= "<option value = '$id'>$id</option>";
158            }
159        }
160        return ($opt_str);
161    }
162
163}