xref: /plugin/upgrade/admin.php (revision a7b5607839b5481202b974a4d98ee8f4f9cbbd6a)
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        #FIXME check unused files
212        if($ok){
213            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
214        }else{
215            $this->_say('<b>'.$this->getLang('ck_fail').'</b>');
216        }
217        return $ok;
218    }
219
220    private function _step_copy(){
221        $this->_say($this->getLang('cp_start'));
222        $ok = $this->_traverse('',false);
223        if($ok){
224            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
225            #FIXME delete unused files
226        }else{
227            $this->_say('<b>'.$this->getLang('cp_fail').'</b>');
228        }
229        return $ok;
230    }
231
232    private function _traverse($dir,$dryrun){
233        $base = $this->tgzdir;
234        $ok = true;
235
236        $dh = @opendir($base.'/'.$dir);
237        if(!$dh) return;
238        while(($file = readdir($dh)) !== false){
239            if($file == '.' || $file == '..') continue;
240            $from = "$base/$dir/$file";
241            $to   = DOKU_INC."$dir/$file";
242
243            if(is_dir($from)){
244                if($dryrun){
245                    // just check for writability
246                    if(!is_dir($to)){
247                        if(is_dir(dirname($to)) && !is_writable(dirname($to))){
248                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file"));
249                            $ok = false;
250                        }
251                    }
252                }
253
254                // recursion
255                if(!$this->_traverse("$dir/$file",$dryrun)){
256                    $ok = false;
257                }
258            }else{
259                $fmd5 = md5(@file_get_contents($from));
260                $tmd5 = md5(@file_get_contents($to));
261                if($fmd5 != $tmd5){
262                    if($dryrun){
263                        // just check for writability
264                        if( (file_exists($to) && !is_writable($to)) ||
265                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){
266
267                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>',hsc("$dir/$file"));
268                            $ok = false;
269                        }else{
270                            $this->_say($this->getLang('tv_upd'),hsc("$dir/$file"));
271                        }
272                    }else{
273                        // check dir
274                        if(io_mkdir_p(dirname($to))){
275                            // copy
276                            if(!copy($from,$to)){
277                                $this->_say('<b>'.$this->getLang('tv_nocopy').'</b>',hsc("$dir/$file"));
278                                $ok = false;
279                            }else{
280                                $this->_say($this->getLang('tv_done'),hsc("$dir/$file"));
281                            }
282                        }else{
283                            $this->_say('<b>'.$this->getLang('tv_nodir').'</b>',hsc("$dir"));
284                            $ok = false;
285                        }
286                    }
287                }
288            }
289        }
290        closedir($dh);
291        return $ok;
292    }
293}
294
295// vim:ts=4:sw=4:et:enc=utf-8:
296