xref: /plugin/upgrade/admin.php (revision 4bed55911b3866a53ad5ad9d7df0f979a21fda83)
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_copy(true)) $next = 'upgrade';
112            }elseif($step == 'upgrade'){
113                if($this->_step_copy(false)) $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('Downloading from %s',$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("Download failed.");
166            return false;
167        }
168
169        $this->_say('Received %d bytes',strlen($data));
170
171        if(!io_saveFile($this->tgzfile,$data)){
172            $this->_say("Failed to save download.");
173            return false;
174        }
175
176        return true;
177    }
178
179    private function _step_unpack(){
180        global $conf;
181        $this->_say('<b>Extracting the archive...</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('Extraction failed on init');
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('Extraction failed');
197            return false;
198        }
199
200        $this->_say('Extraction done.');
201
202        $this->_say('Version <b>%s</b> ready to install. Your current version is <b>%s</b>.',
203                    hsc(file_get_contents($this->tgzdir.'/VERSION')),
204                    getVersion());
205        return true;
206    }
207
208    private function _step_copy($dryrun=true){
209        $ok = $this->_traverse('',$dryrun);
210        if($dryrun){
211            if($ok){
212                $this->_say('<b>All files are writable, ready to upgrade</b>');
213            }else{
214                $this->_say('<b>Some files aren\'t writable. Uprade not possible.</b>');
215            }
216        }else{
217            if($ok){
218                $this->_say('<b>All files upgraded successfully</b>');
219
220                #FIXME delete unused files
221            }else{
222                $this->_say('<b>Some files couldn\'t be upgraded. Uh-oh. Better check manually.</b>');
223            }
224        }
225        return $ok;
226    }
227
228    private function _traverse($dir,$dryrun){
229        $base = $this->tgzdir;
230        $ok = true;
231
232        $dh = @opendir($base.'/'.$dir);
233        if(!$dh) return;
234        while(($file = readdir($dh)) !== false){
235            if($file == '.' || $file == '..') continue;
236            $from = "$base/$dir/$file";
237            $to   = DOKU_INC."$dir/$file";
238
239            if(is_dir($from)){
240                if($dryrun){
241                    // just check for writability
242                    if(!is_dir($to)){
243                        if(is_dir(dirname($to)) && !is_writable(dirname($to))){
244                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
245                            $ok = false;
246                        }
247                    }
248                }
249
250                // recursion
251                if(!$this->_traverse("$dir/$file",$dryrun)){
252                    $ok = false;
253                }
254            }else{
255                $fmd5 = md5(@file_get_contents($from));
256                $tmd5 = md5(@file_get_contents($to));
257                if($fmd5 != $tmd5){
258                    if($dryrun){
259                        // just check for writability
260                        if( (file_exists($to) && !is_writable($to)) ||
261                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to))) ){
262
263                            $this->_say("<b>%s is not writable</b>",hsc("$dir/$file"));
264                            $ok = false;
265                        }else{
266                            $this->_say("%s needs update",hsc("$dir/$file"));
267                        }
268                    }else{
269                        // check dir
270                        if(io_mkdir_p(dirname($to))){
271                            // copy
272                            if(!copy($from,$to)){
273                                $this->_say("<b>%s couldn't be copied</b>",hsc("$dir/$file"));
274                                $ok = false;
275                            }else{
276                                $this->_say("%s updated",hsc("$dir/$file"));
277                            }
278                        }else{
279                            $this->_say("<b>failed to create %s</b>",hsc("$dir"));
280                            $ok = false;
281                        }
282                    }
283                }
284            }
285        }
286        closedir($dh);
287        return $ok;
288    }
289}
290
291// vim:ts=4:sw=4:et:enc=utf-8:
292