xref: /plugin/upgrade/admin.php (revision 63712694be80a76fc17f6e72bf47795e2574528d)
1<?php
2/**
3 * DokuWiki Plugin upgrade (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16require_once DOKU_PLUGIN.'admin.php';
17require_once DOKU_PLUGIN.'upgrade/VerboseTarLib.class.php';
18
19class admin_plugin_upgrade extends DokuWiki_Admin_Plugin {
20    private $tgzurl;
21    private $tgzfile;
22    private $tgzdir;
23
24    public function __construct(){
25        global $conf;
26
27        $branch = 'stable';
28
29        $this->tgzurl  = 'http://github.com/splitbrain/dokuwiki/tarball/'.$branch;
30        $this->tgzfile = $conf['tmpdir'].'/dokuwiki-upgrade.tgz';
31        $this->tgzdir  = $conf['tmpdir'].'/dokuwiki-upgrade/';
32    }
33
34    public function getMenuSort() { return 555; }
35
36    public function handle() {
37        if($_REQUEST['step'] && !checkSecurityToken()){
38            unset($_REQUEST['step']);
39        }
40    }
41
42    public function html() {
43        global $ID;
44        $abrt = false;
45        $next = false;
46
47        echo '<h1>' . $this->getLang('menu') . '</h1>';
48
49        $this->_say('<div id="plugin__upgrade">');
50        // enable auto scroll
51        ?>
52        <script language="javascript" type="text/javascript">
53            var plugin_upgrade = window.setInterval(function(){
54                var obj = $('plugin__upgrade');
55                if(obj) obj.scrollTop = obj.scrollHeight;
56            },25);
57        </script>
58        <?php
59
60        // handle current step
61        $this->_stepit(&$abrt, &$next);
62
63        // disable auto scroll
64        ?>
65        <script language="javascript" type="text/javascript">
66            window.setTimeout(function(){
67                window.clearInterval(plugin_upgrade);
68            },50);
69        </script>
70        <?php
71        $this->_say('</div>');
72
73        echo '<form action="" method="get" id="plugin__upgrade_form">';
74        echo '<input type="hidden" name="do" value="admin" />';
75        echo '<input type="hidden" name="page" value="upgrade" />';
76        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
77        if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" class="button continue" />';
78        if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" class="button abort" />';
79        echo '</form>';
80    }
81
82    private function _stepit(&$abrt, &$next){
83        global $conf;
84        if($conf['safemodehack']){
85            $abrt = false;
86            $next = false;
87            echo $this->locale_xhtml('safemode');
88        }
89
90        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){
91            $step = array_shift(array_keys($_REQUEST['step']));
92        }else{
93            $step = '';
94        }
95
96        if($step == 'cancel'){
97            # cleanup
98            @unlink($this->tgzfile);
99            $this->_rdel($this->tgzdir);
100            $step = '';
101        }
102
103        if($step){
104            $abrt = true;
105            $next = false;
106            if(!file_exists($this->tgzfile)){
107                if($this->_step_download()) $next = 'unpack';
108            }elseif(!is_dir($this->tgzdir)){
109                if($this->_step_unpack()) $next = 'check';
110            }elseif($step != 'upgrade'){
111                if($this->_step_check()) $next = 'upgrade';
112            }elseif($step == 'upgrade'){
113                if($this->_step_copy()) $next = 'cancel';
114            }else{
115                echo 'uhm. what happened? where am I? This should not happen';
116            }
117        }else{
118            # first time run, show intro
119            echo $this->locale_xhtml('step0');
120            $abrt = false;
121            $next = 'download';
122        }
123    }
124
125    private function _say(){
126        $args = func_get_args();
127        echo vsprintf(array_shift($args)."<br />\n",$args);
128        flush();
129        ob_flush();
130    }
131
132    /**
133     * Recursive delete
134     *
135     * @author Jon Hassall
136     * @link http://de.php.net/manual/en/function.unlink.php#87045
137     */
138    private function _rdel($dir) {
139        if(!$dh = @opendir($dir)) {
140            return;
141        }
142        while (false !== ($obj = readdir($dh))) {
143            if($obj == '.' || $obj == '..') continue;
144
145            if (!@unlink($dir . '/' . $obj)) {
146                $this->_rdel($dir.'/'.$obj);
147            }
148        }
149        closedir($dh);
150        @rmdir($dir);
151    }
152
153    private function _step_download(){
154        $this->_say($this->getLang('dl_from'),$this->tgzurl);
155
156        @set_time_limit(120);
157        @ignore_user_abort();
158
159        $http = new DokuHTTPClient();
160        $http->timeout = 120;
161        $data = $http->get($this->tgzurl);
162
163        if(!$data){
164            $this->_say($http->error);
165            $this->_say($this->getLang('dl_fail'));
166            return false;
167        }
168
169        if(!io_saveFile($this->tgzfile,$data)){
170            $this->_say($this->getLang('dl_fail'));
171            return false;
172        }
173
174        $this->_say($this->getLang('dl_done'),filesize_h(strlen($data)));
175
176        return true;
177    }
178
179    private function _step_unpack(){
180        global $conf;
181        $this->_say('<b>'.$this->getLang('pk_extract').'</b>');
182
183        @set_time_limit(120);
184        @ignore_user_abort();
185
186        $tar = new VerboseTarLib($this->tgzfile);
187        if($tar->_initerror < 0){
188            $this->_say($tar->TarErrorStr($tar->_initerror));
189            $this->_say($this->getLang('pk_fail'));
190            return false;
191        }
192
193        $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/');
194        if($ok < 1){
195            $this->_say($tar->TarErrorStr($ok));
196            $this->_say($this->getLang('pk_fail'));
197            return false;
198        }
199
200        $this->_say($this->getLang('pk_done'));
201
202        $this->_say($this->getLang('pk_version'),
203                    hsc(file_get_contents($this->tgzdir.'/VERSION')),
204                    getVersion());
205        return true;
206    }
207
208    private function _step_check(){
209        $this->_say($this->getLang('ck_start'));
210        $ok = $this->_traverse('',true);
211        if($ok){
212            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
213        }else{
214            $this->_say('<b>'.$this->getLang('ck_fail').'</b>');
215        }
216        return $ok;
217    }
218
219    private function _step_copy(){
220        $this->_say($this->getLang('cp_start'));
221        $ok = $this->_traverse('',false);
222        if($ok){
223            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
224            $this->_rmold();
225        }else{
226            $this->_say('<b>'.$this->getLang('cp_fail').'</b>');
227        }
228        return $ok;
229    }
230
231    private function _rmold(){
232        $list = file($this->tgzdir.'data/deleted.files');
233        foreach($list as $line){
234            $line = trim(preg_replace('/#.*$/','',$line));
235            if(!$line) continue;
236            $file = DOKU_INC.$line;
237            if(!file_exists($file)) continue;
238            if( (is_dir($file) && $this->_rdel($file)) ||
239                @unlink($file)){
240                $this->_say($this->getLang('rm_done'),$file);
241            }else{
242                $this->_say($this->getLang('rm_fail'),$file);
243            }
244        }
245    }
246
247    private function _traverse($dir,$dryrun){
248        $base = $this->tgzdir;
249        $ok = true;
250
251        $dh = @opendir($base.'/'.$dir);
252        if(!$dh) return;
253        while(($file = readdir($dh)) !== false){
254            if($file == '.' || $file == '..') continue;
255            $from = "$base/$dir/$file";
256            $to   = DOKU_INC."$dir/$file";
257
258            if(is_dir($from)){
259                if($dryrun){
260                    // just check for writability
261                    if(!is_dir($to)){
262                        if(is_dir(dirname($to)) && !is_writable(dirname($to))){
263                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file"));
264                            $ok = false;
265                        }
266                    }
267                }
268
269                // recursion
270                if(!$this->_traverse("$dir/$file",$dryrun)){
271                    $ok = false;
272                }
273            }else{
274                $fmd5 = md5(@file_get_contents($from));
275                $tmd5 = md5(@file_get_contents($to));
276                if($fmd5 != $tmd5){
277                    if($dryrun){
278                        // just check for writability
279                        if( (file_exists($to) && !is_writable($to)) ||
280                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){
281
282                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file"));
283                            $ok = false;
284                        }else{
285                            $this->_say($this->getLang('tv_upd'),hsc("$dir/$file"));
286                        }
287                    }else{
288                        // check dir
289                        if(io_mkdir_p(dirname($to))){
290                            // copy
291                            if(!copy($from,$to)){
292                                $this->_say('<b>'.$this->getLang('tv_nocopy').'</b>',hsc("$dir/$file"));
293                                $ok = false;
294                            }else{
295                                $this->_say($this->getLang('tv_done'),hsc("$dir/$file"));
296                            }
297                        }else{
298                            $this->_say('<b>'.$this->getLang('tv_nodir').'</b>',hsc("$dir"));
299                            $ok = false;
300                        }
301                    }
302                }
303            }
304        }
305        closedir($dh);
306        return $ok;
307    }
308}
309
310// vim:ts=4:sw=4:et:enc=utf-8:
311