xref: /plugin/upgrade/admin.php (revision d391262f7ee0887e5df33471b573e441c9d79769)
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        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])){
84            $step = array_shift(array_keys($_REQUEST['step']));
85        }else{
86            $step = '';
87        }
88
89        if($step == 'cancel'){
90            # cleanup
91            @unlink($this->tgzfile);
92            $this->_rdel($this->tgzdir);
93            $step = '';
94        }
95
96        if($step){
97            $abrt = true;
98            $next = false;
99            if(!file_exists($this->tgzfile)){
100                if($this->_step_download()) $next = 'unpack';
101            }elseif(!is_dir($this->tgzdir)){
102                if($this->_step_unpack()) $next = 'check';
103            }elseif($step != 'upgrade'){
104                if($this->_step_copy(true)) $next = 'upgrade';
105            }elseif($step == 'upgrade'){
106                if($this->_step_copy(false)) $next = 'cancel';
107            }else{
108                echo 'uhm. what happened? where am I? This should not happen';
109            }
110        }else{
111            # first time run, show intro
112            echo $this->locale_xhtml('step0');
113            $abrt = false;
114            $next = 'download';
115        }
116    }
117
118    private function _say(){
119        $args = func_get_args();
120        echo vsprintf(array_shift($args)."<br />\n",$args);
121        flush();
122        ob_flush();
123    }
124
125    /**
126     * Recursive delete
127     *
128     * @author Jon Hassall
129     * @link http://de.php.net/manual/en/function.unlink.php#87045
130     */
131    private function _rdel($dir) {
132        if(!$dh = @opendir($dir)) {
133            return;
134        }
135        while (false !== ($obj = readdir($dh))) {
136            if($obj == '.' || $obj == '..') continue;
137
138            if (!@unlink($dir . '/' . $obj)) {
139                $this->_rdel($dir.'/'.$obj);
140            }
141        }
142        closedir($dh);
143        @rmdir($dir);
144    }
145
146    private function _step_download(){
147        $this->_say('Downloading from %s',$this->tgzurl);
148
149        @set_time_limit(120);
150        @ignore_user_abort();
151
152        $http = new DokuHTTPClient();
153        $http->timeout = 120;
154        $data = $http->get($this->tgzurl);
155
156        if(!$data){
157            $this->_say($http->error);
158            $this->_say("Download failed.");
159            return false;
160        }
161
162        $this->_say('Received %d bytes',strlen($data));
163
164        if(!io_saveFile($this->tgzfile,$data)){
165            $this->_say("Failed to save download.");
166            return false;
167        }
168
169        return true;
170    }
171
172    private function _step_unpack(){
173        global $conf;
174        $this->_say('<b>Extracting the archive...</b>');
175
176        @set_time_limit(120);
177        @ignore_user_abort();
178
179        $tar = new VerboseTarLib($this->tgzfile);
180        if($tar->_initerror < 0){
181            $this->_say($tar->TarErrorStr($tar->_initerror));
182            $this->_say('Extraction failed on init');
183            return false;
184        }
185
186        $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE,$this->tgzdir,1,$conf['fmode'],'/^(_cs|_test|\.gitignore)/');
187        if($ok < 1){
188            $this->_say($tar->TarErrorStr($ok));
189            $this->_say('Extraction failed');
190            return false;
191        }
192
193        $this->_say('Extraction done.');
194
195        $this->_say('Version <b>%s</b> ready to install. Your current version is <b>%s</b>.',
196                    hsc(file_get_contents($this->tgzdir.'/VERSION')),
197                    getVersion());
198        return true;
199    }
200
201    private function _step_copy($dryrun=true){
202        $ok = $this->_traverse('',$dryrun);
203        if($dryrun){
204            if($ok){
205                $this->_say('<b>All files are writable, ready to upgrade</b>');
206            }else{
207                $this->_say('<b>Some files aren\'t writable. Uprade not possible.</b>');
208            }
209        }else{
210            if($ok){
211                $this->_say('<b>All files upgraded successfully</b>');
212
213                #FIXME delete unused files
214            }else{
215                $this->_say('<b>Some files couldn\'t be upgraded. Uh-oh. Better check manually.</b>');
216            }
217        }
218        return $ok;
219    }
220
221    private function _traverse($dir,$dryrun){
222        $base = $this->tgzdir;
223        $ok = true;
224
225        $dh = @opendir($base.'/'.$dir);
226        if(!$dh) return;
227        while(($file = readdir($dh)) !== false){
228            if($file == '.' || $file == '..') continue;
229            $from = "$base/$dir/$file";
230            $to   = DOKU_INC."$dir/$file";
231
232            if(is_dir($from)){
233                if($dryrun){
234                    // just check for writability
235                    if(!is_dir($to)){
236                        if(is_dir(dirname($to)) && !is_writable(dirname($to))){
237                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
238                            $ok = false;
239                        }
240                    }
241                }
242
243                // recursion
244                if(!$this->_traverse("$dir/$file",$dryrun)){
245                    $ok = false;
246                }
247            }else{
248                $fmd5 = md5(@file_get_contents($from));
249                $tmd5 = md5(@file_get_contents($to));
250                if($fmd5 != $tmd5){
251                    if($dryrun){
252                        // just check for writability
253                        if( (file_exists($to) && !is_writable($to)) ||
254                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){
255
256                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
257                            $ok = false;
258                        }else{
259                            $this->_say("%s needs update",hsc("$dir/$file"));
260                        }
261                    }else{
262                        // check dir
263                        if(io_mkdir_p(dirname($to))){
264                            // copy
265                            if(!copy($from,$to)){
266                                $this->_say("<b>%s couldn't be copied</b>",hsc("$dir/$file"));
267                                $ok = false;
268                            }else{
269                                $this->_say("%s updated",hsc("$dir/$file"));
270                            }
271                        }else{
272                            $this->_say("<b>failed to create %s</b>",hsc("$dir"));
273                            $ok = false;
274                        }
275                    }
276                }
277            }
278        }
279        closedir($dh);
280        return $ok;
281    }
282}
283
284// vim:ts=4:sw=4:et:enc=utf-8:
285