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