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