xref: /plugin/upgrade/admin.php (revision 331439fb50e788351aa87942480ee4e96ea7cb42)
134aae6dbSAndreas Gohr<?php
234aae6dbSAndreas Gohr/**
3d391262fSAndreas Gohr * DokuWiki Plugin upgrade (Admin Component)
434aae6dbSAndreas Gohr *
534aae6dbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
634aae6dbSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
734aae6dbSAndreas Gohr */
834aae6dbSAndreas Gohr
934aae6dbSAndreas Gohr// must be run within Dokuwiki
1034aae6dbSAndreas Gohrif(!defined('DOKU_INC')) die();
1134aae6dbSAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
1234aae6dbSAndreas Gohrrequire_once DOKU_PLUGIN.'admin.php';
13d391262fSAndreas Gohrrequire_once DOKU_PLUGIN.'upgrade/VerboseTarLib.class.php';
1434aae6dbSAndreas Gohr
15ff284f1fSAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin {
1634aae6dbSAndreas Gohr    private $tgzurl;
1734aae6dbSAndreas Gohr    private $tgzfile;
1834aae6dbSAndreas Gohr    private $tgzdir;
19f378cb7eSAndreas Gohr    private $tgzversion;
20f378cb7eSAndreas Gohr    private $pluginversion;
2134aae6dbSAndreas Gohr
2234aae6dbSAndreas Gohr    public function __construct() {
2334aae6dbSAndreas Gohr        global $conf;
2434aae6dbSAndreas Gohr
2534aae6dbSAndreas Gohr        $branch = 'stable';
2634aae6dbSAndreas Gohr
27bd08ebd1SAndreas Gohr        $this->tgzurl        = "https://github.com/splitbrain/dokuwiki/archive/$branch.tar.gz";
28d391262fSAndreas Gohr        $this->tgzfile       = $conf['tmpdir'].'/dokuwiki-upgrade.tgz';
29d391262fSAndreas Gohr        $this->tgzdir        = $conf['tmpdir'].'/dokuwiki-upgrade/';
30f378cb7eSAndreas Gohr        $this->tgzversion    = "https://raw.githubusercontent.com/splitbrain/dokuwiki/$branch/VERSION";
31f378cb7eSAndreas Gohr        $this->pluginversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki-plugin-upgrade/master/plugin.info.txt";
3234aae6dbSAndreas Gohr    }
3334aae6dbSAndreas Gohr
34bd08ebd1SAndreas Gohr    public function getMenuSort() {
35bd08ebd1SAndreas Gohr        return 555;
36bd08ebd1SAndreas Gohr    }
3734aae6dbSAndreas Gohr
3841163f44SAndreas Gohr    public function handle() {
3941163f44SAndreas Gohr        if($_REQUEST['step'] && !checkSecurityToken()) {
4041163f44SAndreas Gohr            unset($_REQUEST['step']);
4141163f44SAndreas Gohr        }
4234aae6dbSAndreas Gohr    }
4334aae6dbSAndreas Gohr
4434aae6dbSAndreas Gohr    public function html() {
4534aae6dbSAndreas Gohr        $abrt = false;
4634aae6dbSAndreas Gohr        $next = false;
4734aae6dbSAndreas Gohr
4834aae6dbSAndreas Gohr        echo '<h1>'.$this->getLang('menu').'</h1>';
4934aae6dbSAndreas Gohr
5006dc93a8SAndreas Gohr        global $conf;
5106dc93a8SAndreas Gohr        if($conf['safemodehack']) {
5206dc93a8SAndreas Gohr            $abrt = false;
5306dc93a8SAndreas Gohr            $next = false;
5406dc93a8SAndreas Gohr            echo $this->locale_xhtml('safemode');
5506dc93a8SAndreas Gohr            return;
5606dc93a8SAndreas Gohr        }
5706dc93a8SAndreas Gohr
58d391262fSAndreas Gohr        $this->_say('<div id="plugin__upgrade">');
5975e9d164SAndreas Gohr        // enable auto scroll
6075e9d164SAndreas Gohr        ?>
6175e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
62d391262fSAndreas Gohr            var plugin_upgrade = window.setInterval(function () {
63d4376367SAndreas Gohr                var obj = document.getElementById('plugin__upgrade');
6475e9d164SAndreas Gohr                if (obj) obj.scrollTop = obj.scrollHeight;
6575e9d164SAndreas Gohr            }, 25);
6675e9d164SAndreas Gohr        </script>
6775e9d164SAndreas Gohr        <?php
6875e9d164SAndreas Gohr
6975e9d164SAndreas Gohr        // handle current step
7022c39b33SAndreas Gohr        $this->_stepit($abrt, $next);
7134aae6dbSAndreas Gohr
7275e9d164SAndreas Gohr        // disable auto scroll
7375e9d164SAndreas Gohr        ?>
7475e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
7575e9d164SAndreas Gohr            window.setTimeout(function () {
76d391262fSAndreas Gohr                window.clearInterval(plugin_upgrade);
7775e9d164SAndreas Gohr            }, 50);
7875e9d164SAndreas Gohr        </script>
7975e9d164SAndreas Gohr        <?php
8075e9d164SAndreas Gohr        $this->_say('</div>');
8175e9d164SAndreas Gohr
82d391262fSAndreas Gohr        echo '<form action="" method="get" id="plugin__upgrade_form">';
8334aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
84d391262fSAndreas Gohr        echo '<input type="hidden" name="page" value="upgrade" />';
8541163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
866e7afd39SAndreas Gohr        if($next) echo '<input type="submit" name="step['.$next.']" value="'.$this->getLang('btn_continue').' ➡" class="button continue" />';
876e7afd39SAndreas Gohr        if($abrt) echo '<input type="submit" name="step[cancel]" value="✖ '.$this->getLang('btn_abort').'" class="button abort" />';
8834aae6dbSAndreas Gohr        echo '</form>';
89eb52c46aSAndreas Gohr
90eb52c46aSAndreas Gohr        $this->_progress($next);
91eb52c46aSAndreas Gohr    }
92eb52c46aSAndreas Gohr
93eb52c46aSAndreas Gohr    /**
94eb52c46aSAndreas Gohr     * Display a progress bar of all steps
95eb52c46aSAndreas Gohr     *
96eb52c46aSAndreas Gohr     * @param string $next the next step
97eb52c46aSAndreas Gohr     */
98eb52c46aSAndreas Gohr    private function _progress($next) {
99eb52c46aSAndreas Gohr        $steps  = array('version', 'download', 'unpack', 'check', 'upgrade');
100eb52c46aSAndreas Gohr        $active = true;
101eb52c46aSAndreas Gohr        $count = 0;
102eb52c46aSAndreas Gohr
103eb52c46aSAndreas Gohr        echo '<div id="plugin__upgrade_meter"><ol>';
104eb52c46aSAndreas Gohr        foreach($steps as $step) {
105eb52c46aSAndreas Gohr            $count++;
106eb52c46aSAndreas Gohr            if($step == $next) $active = false;
107eb52c46aSAndreas Gohr            if($active) {
108eb52c46aSAndreas Gohr                echo '<li class="active">';
109eb52c46aSAndreas Gohr                echo '<span class="step">✔</span>';
110eb52c46aSAndreas Gohr            } else {
111eb52c46aSAndreas Gohr                echo '<li>';
112eb52c46aSAndreas Gohr                echo '<span class="step">'.$count.'</span>';
113eb52c46aSAndreas Gohr            }
114eb52c46aSAndreas Gohr
115eb52c46aSAndreas Gohr            echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>';
116eb52c46aSAndreas Gohr            echo '</li>';
117eb52c46aSAndreas Gohr        }
118eb52c46aSAndreas Gohr        echo '</ol></div>';
11934aae6dbSAndreas Gohr    }
12034aae6dbSAndreas Gohr
121bd08ebd1SAndreas Gohr    /**
122bd08ebd1SAndreas Gohr     * Decides the current step and executes it
123bd08ebd1SAndreas Gohr     *
124bd08ebd1SAndreas Gohr     * @param bool $abrt
125bd08ebd1SAndreas Gohr     * @param bool $next
126bd08ebd1SAndreas Gohr     */
12734aae6dbSAndreas Gohr    private function _stepit(&$abrt, &$next) {
1284bed5591SAndreas Gohr
12934aae6dbSAndreas Gohr        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
13034aae6dbSAndreas Gohr            $step = array_shift(array_keys($_REQUEST['step']));
13134aae6dbSAndreas Gohr        } else {
13234aae6dbSAndreas Gohr            $step = '';
13334aae6dbSAndreas Gohr        }
13434aae6dbSAndreas Gohr
13534aae6dbSAndreas Gohr        if($step == 'cancel') {
13634aae6dbSAndreas Gohr            # cleanup
13734aae6dbSAndreas Gohr            @unlink($this->tgzfile);
1386b2d1b30SAndreas Gohr            $this->_rdel($this->tgzdir);
13934aae6dbSAndreas Gohr            $step = '';
14034aae6dbSAndreas Gohr        }
14134aae6dbSAndreas Gohr
14234aae6dbSAndreas Gohr        if($step) {
14334aae6dbSAndreas Gohr            $abrt = true;
14434aae6dbSAndreas Gohr            $next = false;
145f378cb7eSAndreas Gohr            if($step == 'version') {
146f378cb7eSAndreas Gohr                $this->_step_version();
147f378cb7eSAndreas Gohr                $next = 'download';
148f378cb7eSAndreas Gohr            } elseif(!file_exists($this->tgzfile)) {
14934aae6dbSAndreas Gohr                if($this->_step_download()) $next = 'unpack';
15034aae6dbSAndreas Gohr            } elseif(!is_dir($this->tgzdir)) {
15134aae6dbSAndreas Gohr                if($this->_step_unpack()) $next = 'check';
15234aae6dbSAndreas Gohr            } elseif($step != 'upgrade') {
1533c38de15SAndreas Gohr                if($this->_step_check()) $next = 'upgrade';
15434aae6dbSAndreas Gohr            } elseif($step == 'upgrade') {
1553c38de15SAndreas Gohr                if($this->_step_copy()) $next = 'cancel';
15634aae6dbSAndreas Gohr            } else {
15775e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
15834aae6dbSAndreas Gohr            }
15934aae6dbSAndreas Gohr        } else {
16034aae6dbSAndreas Gohr            # first time run, show intro
16134aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
16234aae6dbSAndreas Gohr            $abrt = false;
163f378cb7eSAndreas Gohr            $next = 'version';
16434aae6dbSAndreas Gohr        }
16534aae6dbSAndreas Gohr    }
16634aae6dbSAndreas Gohr
167bd08ebd1SAndreas Gohr    /**
168bd08ebd1SAndreas Gohr     * Output the given arguments using vsprintf and flush buffers
169bd08ebd1SAndreas Gohr     */
170d04a99cdSAndreas Gohr    public static function _say() {
17134aae6dbSAndreas Gohr        $args = func_get_args();
1726e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="16" height="16" alt="" /> ';
1736e7afd39SAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
1746e7afd39SAndreas Gohr        flush();
1756e7afd39SAndreas Gohr        ob_flush();
1766e7afd39SAndreas Gohr    }
1776e7afd39SAndreas Gohr
1786e7afd39SAndreas Gohr    /**
1796e7afd39SAndreas Gohr     * Print a warning using the given arguments with vsprintf and flush buffers
1806e7afd39SAndreas Gohr     */
181d04a99cdSAndreas Gohr    public static function _warn() {
1826e7afd39SAndreas Gohr        $args = func_get_args();
1836e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/error.png" width="16" height="16" alt="!" /> ';
18434aae6dbSAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
18534aae6dbSAndreas Gohr        flush();
18634aae6dbSAndreas Gohr        ob_flush();
18734aae6dbSAndreas Gohr    }
18834aae6dbSAndreas Gohr
1896b2d1b30SAndreas Gohr    /**
1906b2d1b30SAndreas Gohr     * Recursive delete
1916b2d1b30SAndreas Gohr     *
1929285faa5SAndreas Gohr     * @author Jon Hassall
1939285faa5SAndreas Gohr     * @link   http://de.php.net/manual/en/function.unlink.php#87045
1946b2d1b30SAndreas Gohr     */
1959285faa5SAndreas Gohr    private function _rdel($dir) {
1969285faa5SAndreas Gohr        if(!$dh = @opendir($dir)) {
197bd08ebd1SAndreas Gohr            return false;
1989285faa5SAndreas Gohr        }
1999285faa5SAndreas Gohr        while(false !== ($obj = readdir($dh))) {
2009285faa5SAndreas Gohr            if($obj == '.' || $obj == '..') continue;
2019285faa5SAndreas Gohr
2029285faa5SAndreas Gohr            if(!@unlink($dir.'/'.$obj)) {
2039285faa5SAndreas Gohr                $this->_rdel($dir.'/'.$obj);
2049285faa5SAndreas Gohr            }
2059285faa5SAndreas Gohr        }
2069285faa5SAndreas Gohr        closedir($dh);
207e32047e3SAndreas Gohr        return @rmdir($dir);
2086b2d1b30SAndreas Gohr    }
2096b2d1b30SAndreas Gohr
210bd08ebd1SAndreas Gohr    /**
211f378cb7eSAndreas Gohr     * Check various versions
212f378cb7eSAndreas Gohr     *
213f378cb7eSAndreas Gohr     * @return bool
214f378cb7eSAndreas Gohr     */
215f378cb7eSAndreas Gohr    private function _step_version() {
216f378cb7eSAndreas Gohr        $ok = true;
217f378cb7eSAndreas Gohr
218f378cb7eSAndreas Gohr        // check if PHP is up to date
219f378cb7eSAndreas Gohr        if(version_compare(phpversion(), '5.2.0', '<')) {
2206e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_php'));
221f378cb7eSAndreas Gohr            $ok = false;
222f378cb7eSAndreas Gohr        }
223f378cb7eSAndreas Gohr
224c37aa5dcSAndreas Gohr        // we need SSL - only newer HTTPClients check that themselves
225c37aa5dcSAndreas Gohr        if(!in_array('ssl', stream_get_transports())) {
226c37aa5dcSAndreas Gohr            $this->_warn($this->getLang('vs_ssl'));
227c37aa5dcSAndreas Gohr            $ok = false;
228c37aa5dcSAndreas Gohr        }
229c37aa5dcSAndreas Gohr
230f378cb7eSAndreas Gohr        // get the available version
231f378cb7eSAndreas Gohr        $http       = new DokuHTTPClient();
232f378cb7eSAndreas Gohr        $tgzversion = $http->get($this->tgzversion);
233f378cb7eSAndreas Gohr        if(!$tgzversion) {
2346e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error));
235f378cb7eSAndreas Gohr            $ok = false;
236f378cb7eSAndreas Gohr        }
237f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) {
2386e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno'));
239f378cb7eSAndreas Gohr            $ok            = false;
240f378cb7eSAndreas Gohr            $tgzversionnum = 0;
241f378cb7eSAndreas Gohr        } else {
242f378cb7eSAndreas Gohr            $tgzversionnum = $m[2];
243f378cb7eSAndreas Gohr            $this->_say($this->getLang('vs_tgz'), $tgzversion);
244f378cb7eSAndreas Gohr        }
245f378cb7eSAndreas Gohr
246f378cb7eSAndreas Gohr        // get the current version
247f378cb7eSAndreas Gohr        $version = getVersion();
248f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) {
249f378cb7eSAndreas Gohr            $versionnum = 0;
250f378cb7eSAndreas Gohr        } else {
251f378cb7eSAndreas Gohr            $versionnum = $m[2];
252f378cb7eSAndreas Gohr        }
253f378cb7eSAndreas Gohr        $this->_say($this->getLang('vs_local'), $version);
254f378cb7eSAndreas Gohr
255f378cb7eSAndreas Gohr        // compare versions
256f378cb7eSAndreas Gohr        if(!$versionnum) {
2576e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_localno'));
258f378cb7eSAndreas Gohr            $ok = false;
259f378cb7eSAndreas Gohr        } else if($tgzversionnum) {
260f378cb7eSAndreas Gohr            if($tgzversionnum < $versionnum) {
2616e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_newer'));
262f378cb7eSAndreas Gohr                $ok = false;
263f378cb7eSAndreas Gohr            } elseif($tgzversionnum == $versionnum) {
2646e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_same'));
265f378cb7eSAndreas Gohr                $ok = false;
266f378cb7eSAndreas Gohr            }
267f378cb7eSAndreas Gohr        }
268f378cb7eSAndreas Gohr
269f378cb7eSAndreas Gohr        // check plugin version
270f378cb7eSAndreas Gohr        $pluginversion = $http->get($this->pluginversion);
271f378cb7eSAndreas Gohr        if($pluginversion) {
272f378cb7eSAndreas Gohr            $plugininfo = linesToHash(explode("\n", $pluginversion));
273f378cb7eSAndreas Gohr            $myinfo     = $this->getInfo();
274f378cb7eSAndreas Gohr            if($plugininfo['date'] > $myinfo['date']) {
2757cb119ecSAndreas Gohr                $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']);
276f378cb7eSAndreas Gohr                $ok = false;
277f378cb7eSAndreas Gohr            }
278f378cb7eSAndreas Gohr        }
279f378cb7eSAndreas Gohr
280f378cb7eSAndreas Gohr        return $ok;
281f378cb7eSAndreas Gohr    }
282f378cb7eSAndreas Gohr
283f378cb7eSAndreas Gohr    /**
284bd08ebd1SAndreas Gohr     * Download the tarball
285bd08ebd1SAndreas Gohr     *
286bd08ebd1SAndreas Gohr     * @return bool
287bd08ebd1SAndreas Gohr     */
28834aae6dbSAndreas Gohr    private function _step_download() {
2893c38de15SAndreas Gohr        $this->_say($this->getLang('dl_from'), $this->tgzurl);
29034aae6dbSAndreas Gohr
29134aae6dbSAndreas Gohr        @set_time_limit(120);
29234aae6dbSAndreas Gohr        @ignore_user_abort();
29334aae6dbSAndreas Gohr
29434aae6dbSAndreas Gohr        $http          = new DokuHTTPClient();
29534aae6dbSAndreas Gohr        $http->timeout = 120;
29634aae6dbSAndreas Gohr        $data          = $http->get($this->tgzurl);
29734aae6dbSAndreas Gohr
29834aae6dbSAndreas Gohr        if(!$data) {
2996e7afd39SAndreas Gohr            $this->_warn($http->error);
3006e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
30134aae6dbSAndreas Gohr            return false;
30234aae6dbSAndreas Gohr        }
30334aae6dbSAndreas Gohr
30434aae6dbSAndreas Gohr        if(!io_saveFile($this->tgzfile, $data)) {
3056e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
30634aae6dbSAndreas Gohr            return false;
30734aae6dbSAndreas Gohr        }
30834aae6dbSAndreas Gohr
309a7b56078SAndreas Gohr        $this->_say($this->getLang('dl_done'), filesize_h(strlen($data)));
3103c38de15SAndreas Gohr
31134aae6dbSAndreas Gohr        return true;
31234aae6dbSAndreas Gohr    }
31334aae6dbSAndreas Gohr
314bd08ebd1SAndreas Gohr    /**
315bd08ebd1SAndreas Gohr     * Unpack the tarball
316bd08ebd1SAndreas Gohr     *
317bd08ebd1SAndreas Gohr     * @return bool
318bd08ebd1SAndreas Gohr     */
31934aae6dbSAndreas Gohr    private function _step_unpack() {
3203c38de15SAndreas Gohr        $this->_say('<b>'.$this->getLang('pk_extract').'</b>');
32134aae6dbSAndreas Gohr
32234aae6dbSAndreas Gohr        @set_time_limit(120);
32334aae6dbSAndreas Gohr        @ignore_user_abort();
32434aae6dbSAndreas Gohr
325d04a99cdSAndreas Gohr        try {
326d04a99cdSAndreas Gohr            $tar = new VerboseTar();
327d04a99cdSAndreas Gohr            $tar->open($this->tgzfile);
328d04a99cdSAndreas Gohr            $tar->extract($this->tgzdir, 1);
329d04a99cdSAndreas Gohr            $tar->close();
330d04a99cdSAndreas Gohr        } catch (Exception $e) {
331d04a99cdSAndreas Gohr            $this->_warn($e->getMessage());
3326e7afd39SAndreas Gohr            $this->_warn($this->getLang('pk_fail'));
33334aae6dbSAndreas Gohr            return false;
33434aae6dbSAndreas Gohr        }
33534aae6dbSAndreas Gohr
3363c38de15SAndreas Gohr        $this->_say($this->getLang('pk_done'));
337738c0102SAndreas Gohr
338bd08ebd1SAndreas Gohr        $this->_say(
339bd08ebd1SAndreas Gohr            $this->getLang('pk_version'),
340738c0102SAndreas Gohr            hsc(file_get_contents($this->tgzdir.'/VERSION')),
341bd08ebd1SAndreas Gohr            getVersion()
342bd08ebd1SAndreas Gohr        );
34334aae6dbSAndreas Gohr        return true;
34434aae6dbSAndreas Gohr    }
34534aae6dbSAndreas Gohr
346bd08ebd1SAndreas Gohr    /**
347bd08ebd1SAndreas Gohr     * Check permissions of files to change
348bd08ebd1SAndreas Gohr     *
349bd08ebd1SAndreas Gohr     * @return bool
350bd08ebd1SAndreas Gohr     */
3513c38de15SAndreas Gohr    private function _step_check() {
3523c38de15SAndreas Gohr        $this->_say($this->getLang('ck_start'));
3533c38de15SAndreas Gohr        $ok = $this->_traverse('', true);
35434aae6dbSAndreas Gohr        if($ok) {
3553c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
35634aae6dbSAndreas Gohr        } else {
3576e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('ck_fail').'</b>');
35834aae6dbSAndreas Gohr        }
3593c38de15SAndreas Gohr        return $ok;
3603c38de15SAndreas Gohr    }
36134aae6dbSAndreas Gohr
362bd08ebd1SAndreas Gohr    /**
363bd08ebd1SAndreas Gohr     * Copy over new files
364bd08ebd1SAndreas Gohr     *
365bd08ebd1SAndreas Gohr     * @return bool
366bd08ebd1SAndreas Gohr     */
3673c38de15SAndreas Gohr    private function _step_copy() {
3683c38de15SAndreas Gohr        $this->_say($this->getLang('cp_start'));
3693c38de15SAndreas Gohr        $ok = $this->_traverse('', false);
3703c38de15SAndreas Gohr        if($ok) {
3713c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
37263712694SAndreas Gohr            $this->_rmold();
373e32047e3SAndreas Gohr            $this->_say('<b>'.$this->getLang('finish').'</b>');
37434aae6dbSAndreas Gohr        } else {
3756e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('cp_fail').'</b>');
37634aae6dbSAndreas Gohr        }
37734aae6dbSAndreas Gohr        return $ok;
37834aae6dbSAndreas Gohr    }
37934aae6dbSAndreas Gohr
380bd08ebd1SAndreas Gohr    /**
381bd08ebd1SAndreas Gohr     * Delete outdated files
382bd08ebd1SAndreas Gohr     */
38363712694SAndreas Gohr    private function _rmold() {
38463712694SAndreas Gohr        $list = file($this->tgzdir.'data/deleted.files');
38563712694SAndreas Gohr        foreach($list as $line) {
38663712694SAndreas Gohr            $line = trim(preg_replace('/#.*$/', '', $line));
38763712694SAndreas Gohr            if(!$line) continue;
38863712694SAndreas Gohr            $file = DOKU_INC.$line;
38963712694SAndreas Gohr            if(!file_exists($file)) continue;
39063712694SAndreas Gohr            if((is_dir($file) && $this->_rdel($file)) ||
391bd08ebd1SAndreas Gohr                @unlink($file)
392bd08ebd1SAndreas Gohr            ) {
393e32047e3SAndreas Gohr                $this->_say($this->getLang('rm_done'), hsc($line));
39463712694SAndreas Gohr            } else {
3956e7afd39SAndreas Gohr                $this->_warn($this->getLang('rm_fail'), hsc($line));
39663712694SAndreas Gohr            }
39763712694SAndreas Gohr        }
398*331439fbSAndreas Gohr        // delete install
399*331439fbSAndreas Gohr        @unlink(DOKU_INC.'install.php');
40063712694SAndreas Gohr    }
40163712694SAndreas Gohr
402bd08ebd1SAndreas Gohr    /**
403bd08ebd1SAndreas Gohr     * Traverse over the given dir and compare it to the DokuWiki dir
404bd08ebd1SAndreas Gohr     *
405bd08ebd1SAndreas Gohr     * Checks what files need an update, tests for writability and copies
406bd08ebd1SAndreas Gohr     *
407bd08ebd1SAndreas Gohr     * @param string $dir
408bd08ebd1SAndreas Gohr     * @param bool   $dryrun do not copy but only check permissions
409bd08ebd1SAndreas Gohr     * @return bool
410bd08ebd1SAndreas Gohr     */
41134aae6dbSAndreas Gohr    private function _traverse($dir, $dryrun) {
41234aae6dbSAndreas Gohr        $base = $this->tgzdir;
41334aae6dbSAndreas Gohr        $ok   = true;
41434aae6dbSAndreas Gohr
41534aae6dbSAndreas Gohr        $dh = @opendir($base.'/'.$dir);
416bd08ebd1SAndreas Gohr        if(!$dh) return false;
41734aae6dbSAndreas Gohr        while(($file = readdir($dh)) !== false) {
41834aae6dbSAndreas Gohr            if($file == '.' || $file == '..') continue;
41934aae6dbSAndreas Gohr            $from = "$base/$dir/$file";
42034aae6dbSAndreas Gohr            $to   = DOKU_INC."$dir/$file";
42134aae6dbSAndreas Gohr
42234aae6dbSAndreas Gohr            if(is_dir($from)) {
42334aae6dbSAndreas Gohr                if($dryrun) {
42434aae6dbSAndreas Gohr                    // just check for writability
42534aae6dbSAndreas Gohr                    if(!is_dir($to)) {
42634aae6dbSAndreas Gohr                        if(is_dir(dirname($to)) && !is_writable(dirname($to))) {
4276e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
42834aae6dbSAndreas Gohr                            $ok = false;
42934aae6dbSAndreas Gohr                        }
43034aae6dbSAndreas Gohr                    }
43134aae6dbSAndreas Gohr                }
43234aae6dbSAndreas Gohr
43334aae6dbSAndreas Gohr                // recursion
43434aae6dbSAndreas Gohr                if(!$this->_traverse("$dir/$file", $dryrun)) {
43534aae6dbSAndreas Gohr                    $ok = false;
43634aae6dbSAndreas Gohr                }
43734aae6dbSAndreas Gohr            } else {
438f2fa6d10SAndreas Gohr                $fmd5 = md5(@file_get_contents($from));
439f2fa6d10SAndreas Gohr                $tmd5 = md5(@file_get_contents($to));
4406deeb3b1SAndreas Gohr                if($fmd5 != $tmd5 || !file_exists($to)) {
44134aae6dbSAndreas Gohr                    if($dryrun) {
44234aae6dbSAndreas Gohr                        // just check for writability
44334aae6dbSAndreas Gohr                        if((file_exists($to) && !is_writable($to)) ||
444bd08ebd1SAndreas Gohr                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to)))
445bd08ebd1SAndreas Gohr                        ) {
44634aae6dbSAndreas Gohr
4476e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
44834aae6dbSAndreas Gohr                            $ok = false;
44934aae6dbSAndreas Gohr                        } else {
4503c38de15SAndreas Gohr                            $this->_say($this->getLang('tv_upd'), hsc("$dir/$file"));
45134aae6dbSAndreas Gohr                        }
45234aae6dbSAndreas Gohr                    } else {
45334aae6dbSAndreas Gohr                        // check dir
45434aae6dbSAndreas Gohr                        if(io_mkdir_p(dirname($to))) {
45534aae6dbSAndreas Gohr                            // copy
45634aae6dbSAndreas Gohr                            if(!copy($from, $to)) {
4576e7afd39SAndreas Gohr                                $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file"));
45834aae6dbSAndreas Gohr                                $ok = false;
45934aae6dbSAndreas Gohr                            } else {
4603c38de15SAndreas Gohr                                $this->_say($this->getLang('tv_done'), hsc("$dir/$file"));
46134aae6dbSAndreas Gohr                            }
46234aae6dbSAndreas Gohr                        } else {
4636e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir"));
46434aae6dbSAndreas Gohr                            $ok = false;
46534aae6dbSAndreas Gohr                        }
46634aae6dbSAndreas Gohr                    }
46734aae6dbSAndreas Gohr                }
46834aae6dbSAndreas Gohr            }
46934aae6dbSAndreas Gohr        }
47034aae6dbSAndreas Gohr        closedir($dh);
47134aae6dbSAndreas Gohr        return $ok;
47234aae6dbSAndreas Gohr    }
47334aae6dbSAndreas Gohr}
47434aae6dbSAndreas Gohr
47534aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
476