1<?php
2/**
3 * PDF-Tools
4 *
5 * @license    GPL2
6 * @author     Gero Gothe <gero.gothe@medizindoku.de>
7 */
8
9require_once(DOKU_INC . 'lib/plugins/pdftools/functions.php');
10
11class admin_plugin_pdftools extends DokuWiki_Admin_Plugin {
12
13    var $output = 'COMMAND: none';
14
15    function getMenuText($language){
16        return $this->getLang('admin menu');
17    }
18
19    /**
20    * handle user request
21    */
22    function handle() {
23
24        # Check if dw2pdf is activated
25        $list = plugin_list();
26        if(in_array('dw2pdf',$list)===false) return;
27        if (!isset($_REQUEST['cmd'])) return;   // first time - nothing to do
28
29
30        if (!checkSecurityToken()) return;
31        if (!is_array($_REQUEST['cmd'])) return;
32
33        // verify valid values
34        $command = key($_REQUEST['cmd']);
35
36
37        if (strpos($command,"install:")===0) {
38            $command = str_replace("install:","",$command);
39
40            $this->output = "COMMAND: Install template '$command'";
41
42            recurse_copy(DOKU_PLUGIN."/pdftools/tpl/$command",DOKU_PLUGIN."/dw2pdf/tpl/$command");
43        }
44
45        if (strpos($command,"erase:")===0) {
46            $command = str_replace("erase:","",$command);
47
48            $this->output = "COMMAND: Erase '$command'";
49
50            $this->rrmdir(DOKU_PLUGIN."dw2pdf/tpl/$command");
51        }
52
53
54        # Upload templates
55
56        if(strpos($command,"upload")===0) {
57            $filename = $_FILES["zip_file"]["name"];
58            $source = $_FILES["zip_file"]["tmp_name"];
59            $type = $_FILES["zip_file"]["type"];
60
61            $name = explode(".", $filename);
62            $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
63            foreach($accepted_types as $mime_type) {
64                if($mime_type == $type) {
65                    $okay = true;
66                    break;
67                }
68            }
69
70            $continue = strtolower($name[1]) == 'zip' ? true : false;
71            if(!$continue) {
72                msg("The file you are trying to upload is not a .zip file. Please try again.",-1);
73                return;
74            }
75
76            $target_path = DOKU_PLUGIN."dw2pdf/tpl/".$filename;  // change this to the correct site path
77
78            if(move_uploaded_file($source, $target_path)) {
79                # Using Linix unzip on the command line
80                ob_start();
81                system("unzip -d '".DOKU_PLUGIN."dw2pdf/tpl/' $target_path");
82                $retval = ob_get_contents();
83                ob_end_clean();
84
85                unlink($target_path);
86
87                msg("Your .zip file was uploaded and unpacked into the <code>dw2pdf/tpl</code> directory.<br><br><pre>$retval</pre>",1);
88
89            } else {
90                msg("There was a problem with the upload. Could not move uploaded file to plugin directory.",-1);
91            }
92
93        }
94
95    }
96
97    /**
98    * output appropriate html
99    */
100    function html() {
101        global $ID;
102
103
104
105        ptln('<form action="'.wl($ID).'" method="post" enctype="multipart/form-data">');
106
107        // output hidden values to ensure dokuwiki will return back to this plugin
108        ptln('  <input type="hidden" name="do"   value="admin" />');
109        ptln('  <input type="hidden" name="page" value="'.$this->getPluginName().'" />');
110        formSecurityToken();
111
112        $dw = dirList(DOKU_PLUGIN."/dw2pdf/tpl/");
113        $pi = dirList(DOKU_PLUGIN."/pdftools/tpl/");
114
115        echo '<h1>'.$this->getLang('admin title main').'</h1>';
116
117        $p_time = filectime(__FILE__);
118
119        # Upload form
120        echo '<h2>'.$this->getLang('admin title upload').'</h2>';
121
122        echo $this->getLang('text upload');
123
124        echo '<label>'.$this->getLang('text zipfile').': <input type="file" name="zip_file" /></label>
125            <input type="submit" name="cmd[upload]" value="'.$this->getLang('btn upload').'" /><br><br>';
126
127        echo '<hr>';
128
129        echo '<h2>'.$this->getLang('admin title manage').'</h2>';
130
131        echo '<style>.pdftools_preview:hover{transform:scale(3);box-shadow:0px 0px 17px 12px rgba(0,0,0,0.53);}</style>';
132
133        echo '<table>';
134
135        # Installed templates
136        foreach ($dw as $v) {
137            ptln ('<tr>');
138            ptln("<td style='background-color:lightgreen;font-weight:bold'>$v</td>");
139
140
141            ptln ("<td>");
142            if (in_array($v,$pi) !== false) {
143                ptln('<input type="submit" name="cmd[install:'.$v.']"  value="'.($this->getLang('btn reinstall')).'" /><br><br>');
144            }
145
146            echo '<input type="submit" name="cmd[erase:'.$v.']"  value="'.($this->getLang('btn erase')).'" /><br><br>';
147
148            # Zeitpunkt der Installation der Vorlage
149            $m_time = filectime(DOKU_PLUGIN."/dw2pdf/tpl/$v/style.css");
150            ptln("Installation: ".date("Y-m-d / H:i:s",$m_time));
151
152            # Zeitpunkt, an der das Plugin bzw. das Template aktualisiert wurde, prüfen.
153            # Aktualisierungshinweis geben, wenn dieser über dem Zeitpunkt der Installation liegt
154            if (in_array($v,$pi) !== false) {
155                $o_time = filectime(DOKU_PLUGIN."/pdftools/tpl/$v/style.css");
156                if ($o_time>$m_time) ptln ("<br><br><span style='color:red'>".$this->getLang('text update')."<br>Update: ".date("Y-m-d / H:i:s",$o_time)."</span>");
157            }
158            ptln ("</td>");
159
160            if (file_exists(DOKU_PLUGIN."/dw2pdf/tpl/$v/preview.png")) {
161                ptln ('<td><img class="pdftools_preview" style="height:200px" src="'.DOKU_URL.'lib/plugins/dw2pdf/tpl/'.$v.'/preview.png"></td>');
162            } else ptln("<td><i>".$this->getLang('text preview')."</i></td>");
163
164            if (file_exists(DOKU_PLUGIN."/dw2pdf/tpl/$v/description.html")) {
165                ptln ('<td>'.file_get_contents(DOKU_URL.'lib/plugins/dw2pdf/tpl/'.$v.'/description.html').'</td>');
166            } else ptln("<td><i>".$this->getLang('text desc')."</i></td>");
167
168            ptln ('</tr>');
169        }
170
171        # Volagen aus dem Vorlagenpaket, welche nicht installiert sind
172        foreach ($pi as $v) {
173            if (in_array($v,$dw) === false) {
174                ptln ('<tr>');
175
176                ptln ("<td style='background-color:linen;font-weight:bold'>$v</td>");
177
178                ptln('<td>');
179
180                ptln('<input type="submit" name="cmd[install:'.$v.']"  value="'.$this->getLang('btn install').'" /><br><br>');
181
182                $m_time = filectime(DOKU_PLUGIN."/pdftools/tpl/$v/style.css");
183                ptln(date("Y-m-d / H:i:s",$m_time));
184                ptln('</td>');
185
186                if (file_exists(DOKU_PLUGIN."/pdftools/tpl/$v/preview.png")) {
187                    ptln ('<td><img class="pdftools_preview" style="height:200px" src="'.DOKU_URL.'lib/plugins/pdftools/tpl/'.$v.'/preview.png"></td>');
188                } else ptln("<td><i>".$this->getLang('text preview')."</i></td>");
189
190                if (file_exists(DOKU_PLUGIN."/pdftools/tpl/$v/description.html")) {
191                ptln ('<td>'.file_get_contents(DOKU_URL.'lib/plugins/pdftools/tpl/'.$v.'/description.html').'</td>');
192                } else ptln("<td><i>".$this->getLang('text desc')."</i></td>");
193
194                ptln ('</tr>');
195            }
196        }
197
198        ptln ('</table>');
199        ptln('</form>');
200
201        ptln('<br><code>'.htmlspecialchars($this->output).'</code>');
202
203    }
204
205    # From: https://www.php.net/manual/de/function.rmdir.php
206    # recursively erase a directory with its subdirectories
207    function rrmdir($src) {
208        $dir = opendir($src);
209        while(false !== ( $file = readdir($dir)) ) {
210            if (( $file != '.' ) && ( $file != '..' )) {
211                $full = $src . '/' . $file;
212                if ( is_dir($full) ) {
213                    rrmdir($full);
214                }
215                else {
216                    unlink($full);
217                }
218            }
219        }
220        closedir($dir);
221        rmdir($src);
222    }
223
224
225}
226
227