xref: /plugin/upgrade/admin.php (revision 9f8d43557edaa21f6e86d600737c5d1988743afd)
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
82*9f8d4355SAndreas Gohr        $action = script();
83*9f8d4355SAndreas Gohr        echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">';
8434aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
85d391262fSAndreas Gohr        echo '<input type="hidden" name="page" value="upgrade" />';
8641163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
87*9f8d4355SAndreas Gohr        if($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue">' . $this->getLang('btn_continue') . ' ➡</button>';
88*9f8d4355SAndreas Gohr        if($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>';
8934aae6dbSAndreas Gohr        echo '</form>';
90eb52c46aSAndreas Gohr
91eb52c46aSAndreas Gohr        $this->_progress($next);
92eb52c46aSAndreas Gohr    }
93eb52c46aSAndreas Gohr
94eb52c46aSAndreas Gohr    /**
95eb52c46aSAndreas Gohr     * Display a progress bar of all steps
96eb52c46aSAndreas Gohr     *
97eb52c46aSAndreas Gohr     * @param string $next the next step
98eb52c46aSAndreas Gohr     */
99eb52c46aSAndreas Gohr    private function _progress($next) {
100eb52c46aSAndreas Gohr        $steps  = array('version', 'download', 'unpack', 'check', 'upgrade');
101eb52c46aSAndreas Gohr        $active = true;
102eb52c46aSAndreas Gohr        $count = 0;
103eb52c46aSAndreas Gohr
104eb52c46aSAndreas Gohr        echo '<div id="plugin__upgrade_meter"><ol>';
105eb52c46aSAndreas Gohr        foreach($steps as $step) {
106eb52c46aSAndreas Gohr            $count++;
107eb52c46aSAndreas Gohr            if($step == $next) $active = false;
108eb52c46aSAndreas Gohr            if($active) {
109eb52c46aSAndreas Gohr                echo '<li class="active">';
110eb52c46aSAndreas Gohr                echo '<span class="step">✔</span>';
111eb52c46aSAndreas Gohr            } else {
112eb52c46aSAndreas Gohr                echo '<li>';
113eb52c46aSAndreas Gohr                echo '<span class="step">'.$count.'</span>';
114eb52c46aSAndreas Gohr            }
115eb52c46aSAndreas Gohr
116eb52c46aSAndreas Gohr            echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>';
117eb52c46aSAndreas Gohr            echo '</li>';
118eb52c46aSAndreas Gohr        }
119eb52c46aSAndreas Gohr        echo '</ol></div>';
12034aae6dbSAndreas Gohr    }
12134aae6dbSAndreas Gohr
122bd08ebd1SAndreas Gohr    /**
123bd08ebd1SAndreas Gohr     * Decides the current step and executes it
124bd08ebd1SAndreas Gohr     *
125bd08ebd1SAndreas Gohr     * @param bool $abrt
126bd08ebd1SAndreas Gohr     * @param bool $next
127bd08ebd1SAndreas Gohr     */
12834aae6dbSAndreas Gohr    private function _stepit(&$abrt, &$next) {
1294bed5591SAndreas Gohr
13034aae6dbSAndreas Gohr        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
13134aae6dbSAndreas Gohr            $step = array_shift(array_keys($_REQUEST['step']));
13234aae6dbSAndreas Gohr        } else {
13334aae6dbSAndreas Gohr            $step = '';
13434aae6dbSAndreas Gohr        }
13534aae6dbSAndreas Gohr
136fb8e77caSAndreas Gohr        if($step == 'cancel' || $step == 'done') {
13734aae6dbSAndreas Gohr            # cleanup
13834aae6dbSAndreas Gohr            @unlink($this->tgzfile);
1396b2d1b30SAndreas Gohr            $this->_rdel($this->tgzdir);
140fb8e77caSAndreas Gohr            if($step == 'cancel') $step = '';
14134aae6dbSAndreas Gohr        }
14234aae6dbSAndreas Gohr
14334aae6dbSAndreas Gohr        if($step) {
14434aae6dbSAndreas Gohr            $abrt = true;
14534aae6dbSAndreas Gohr            $next = false;
146f378cb7eSAndreas Gohr            if($step == 'version') {
147f378cb7eSAndreas Gohr                $this->_step_version();
148f378cb7eSAndreas Gohr                $next = 'download';
149fb8e77caSAndreas Gohr            } elseif ($step == 'done') {
150fb8e77caSAndreas Gohr                $this->_step_done();
151fb8e77caSAndreas Gohr                $next = '';
152fb8e77caSAndreas Gohr                $abrt = '';
153f378cb7eSAndreas Gohr            } elseif(!file_exists($this->tgzfile)) {
15434aae6dbSAndreas Gohr                if($this->_step_download()) $next = 'unpack';
15534aae6dbSAndreas Gohr            } elseif(!is_dir($this->tgzdir)) {
15634aae6dbSAndreas Gohr                if($this->_step_unpack()) $next = 'check';
15734aae6dbSAndreas Gohr            } elseif($step != 'upgrade') {
1583c38de15SAndreas Gohr                if($this->_step_check()) $next = 'upgrade';
15934aae6dbSAndreas Gohr            } elseif($step == 'upgrade') {
160fb8e77caSAndreas Gohr                if($this->_step_copy()) {
161fb8e77caSAndreas Gohr                    $next = 'done';
162fb8e77caSAndreas Gohr                    $abrt = '';
163fb8e77caSAndreas Gohr                }
16434aae6dbSAndreas Gohr            } else {
16575e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
16634aae6dbSAndreas Gohr            }
16734aae6dbSAndreas Gohr        } else {
16834aae6dbSAndreas Gohr            # first time run, show intro
16934aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
17034aae6dbSAndreas Gohr            $abrt = false;
171f378cb7eSAndreas Gohr            $next = 'version';
17234aae6dbSAndreas Gohr        }
17334aae6dbSAndreas Gohr    }
17434aae6dbSAndreas Gohr
175bd08ebd1SAndreas Gohr    /**
176bd08ebd1SAndreas Gohr     * Output the given arguments using vsprintf and flush buffers
177bd08ebd1SAndreas Gohr     */
178d04a99cdSAndreas Gohr    public static function _say() {
17934aae6dbSAndreas Gohr        $args = func_get_args();
1806e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="16" height="16" alt="" /> ';
1816e7afd39SAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
1826e7afd39SAndreas Gohr        flush();
1836e7afd39SAndreas Gohr        ob_flush();
1846e7afd39SAndreas Gohr    }
1856e7afd39SAndreas Gohr
1866e7afd39SAndreas Gohr    /**
1876e7afd39SAndreas Gohr     * Print a warning using the given arguments with vsprintf and flush buffers
1886e7afd39SAndreas Gohr     */
189d04a99cdSAndreas Gohr    public static function _warn() {
1906e7afd39SAndreas Gohr        $args = func_get_args();
1916e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/error.png" width="16" height="16" alt="!" /> ';
19234aae6dbSAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
19334aae6dbSAndreas Gohr        flush();
19434aae6dbSAndreas Gohr        ob_flush();
19534aae6dbSAndreas Gohr    }
19634aae6dbSAndreas Gohr
1976b2d1b30SAndreas Gohr    /**
1986b2d1b30SAndreas Gohr     * Recursive delete
1996b2d1b30SAndreas Gohr     *
2009285faa5SAndreas Gohr     * @author Jon Hassall
2019285faa5SAndreas Gohr     * @link   http://de.php.net/manual/en/function.unlink.php#87045
2026b2d1b30SAndreas Gohr     */
2039285faa5SAndreas Gohr    private function _rdel($dir) {
2049285faa5SAndreas Gohr        if(!$dh = @opendir($dir)) {
205bd08ebd1SAndreas Gohr            return false;
2069285faa5SAndreas Gohr        }
2079285faa5SAndreas Gohr        while(false !== ($obj = readdir($dh))) {
2089285faa5SAndreas Gohr            if($obj == '.' || $obj == '..') continue;
2099285faa5SAndreas Gohr
2109285faa5SAndreas Gohr            if(!@unlink($dir.'/'.$obj)) {
2119285faa5SAndreas Gohr                $this->_rdel($dir.'/'.$obj);
2129285faa5SAndreas Gohr            }
2139285faa5SAndreas Gohr        }
2149285faa5SAndreas Gohr        closedir($dh);
215e32047e3SAndreas Gohr        return @rmdir($dir);
2166b2d1b30SAndreas Gohr    }
2176b2d1b30SAndreas Gohr
218bd08ebd1SAndreas Gohr    /**
219f378cb7eSAndreas Gohr     * Check various versions
220f378cb7eSAndreas Gohr     *
221f378cb7eSAndreas Gohr     * @return bool
222f378cb7eSAndreas Gohr     */
223f378cb7eSAndreas Gohr    private function _step_version() {
224f378cb7eSAndreas Gohr        $ok = true;
225f378cb7eSAndreas Gohr
226c37aa5dcSAndreas Gohr        // we need SSL - only newer HTTPClients check that themselves
227c37aa5dcSAndreas Gohr        if(!in_array('ssl', stream_get_transports())) {
228c37aa5dcSAndreas Gohr            $this->_warn($this->getLang('vs_ssl'));
229c37aa5dcSAndreas Gohr            $ok = false;
230c37aa5dcSAndreas Gohr        }
231c37aa5dcSAndreas Gohr
232f378cb7eSAndreas Gohr        // get the available version
233f378cb7eSAndreas Gohr        $http       = new DokuHTTPClient();
234f378cb7eSAndreas Gohr        $tgzversion = $http->get($this->tgzversion);
235f378cb7eSAndreas Gohr        if(!$tgzversion) {
2366e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error));
237f378cb7eSAndreas Gohr            $ok = false;
238f378cb7eSAndreas Gohr        }
239f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) {
2406e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno'));
241f378cb7eSAndreas Gohr            $ok            = false;
242f378cb7eSAndreas Gohr            $tgzversionnum = 0;
243f378cb7eSAndreas Gohr        } else {
244f378cb7eSAndreas Gohr            $tgzversionnum = $m[2];
245f378cb7eSAndreas Gohr            $this->_say($this->getLang('vs_tgz'), $tgzversion);
246f378cb7eSAndreas Gohr        }
247f378cb7eSAndreas Gohr
248f378cb7eSAndreas Gohr        // get the current version
249f378cb7eSAndreas Gohr        $version = getVersion();
250f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) {
251f378cb7eSAndreas Gohr            $versionnum = 0;
252f378cb7eSAndreas Gohr        } else {
253f378cb7eSAndreas Gohr            $versionnum = $m[2];
254f378cb7eSAndreas Gohr        }
255f378cb7eSAndreas Gohr        $this->_say($this->getLang('vs_local'), $version);
256f378cb7eSAndreas Gohr
257f378cb7eSAndreas Gohr        // compare versions
258f378cb7eSAndreas Gohr        if(!$versionnum) {
2596e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_localno'));
260f378cb7eSAndreas Gohr            $ok = false;
261f378cb7eSAndreas Gohr        } else if($tgzversionnum) {
262f378cb7eSAndreas Gohr            if($tgzversionnum < $versionnum) {
2636e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_newer'));
264f378cb7eSAndreas Gohr                $ok = false;
265f378cb7eSAndreas Gohr            } elseif($tgzversionnum == $versionnum) {
2666e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_same'));
267f378cb7eSAndreas Gohr                $ok = false;
268f378cb7eSAndreas Gohr            }
269f378cb7eSAndreas Gohr        }
270f378cb7eSAndreas Gohr
271f378cb7eSAndreas Gohr        // check plugin version
272f378cb7eSAndreas Gohr        $pluginversion = $http->get($this->pluginversion);
273f378cb7eSAndreas Gohr        if($pluginversion) {
274f378cb7eSAndreas Gohr            $plugininfo = linesToHash(explode("\n", $pluginversion));
275f378cb7eSAndreas Gohr            $myinfo     = $this->getInfo();
276f378cb7eSAndreas Gohr            if($plugininfo['date'] > $myinfo['date']) {
2777cb119ecSAndreas Gohr                $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']);
278f378cb7eSAndreas Gohr                $ok = false;
279f378cb7eSAndreas Gohr            }
280f378cb7eSAndreas Gohr        }
281f378cb7eSAndreas Gohr
2829bbe795aSAndreas Gohr        // check if PHP is up to date
2831e627e00SAndreas Gohr        $minphp = '5.3.3';
2849bbe795aSAndreas Gohr        if(version_compare(phpversion(), $minphp, '<')) {
285a92a10d4SAndreas Gohr            $this->_warn($this->getLang('vs_php'), $minphp, phpversion());
2869bbe795aSAndreas Gohr            $ok = false;
2879bbe795aSAndreas Gohr        }
2889bbe795aSAndreas Gohr
289f378cb7eSAndreas Gohr        return $ok;
290f378cb7eSAndreas Gohr    }
291f378cb7eSAndreas Gohr
292f378cb7eSAndreas Gohr    /**
293fb8e77caSAndreas Gohr     * Redirect to the start page
294fb8e77caSAndreas Gohr     */
295fb8e77caSAndreas Gohr    private function _step_done() {
296fb8e77caSAndreas Gohr        echo $this->getLang('finish');
29761f0f3bfSAndreas Gohr        echo "<script type='text/javascript'>location.href='".DOKU_URL."';</script>";
298fb8e77caSAndreas Gohr    }
299fb8e77caSAndreas Gohr
300fb8e77caSAndreas Gohr    /**
301bd08ebd1SAndreas Gohr     * Download the tarball
302bd08ebd1SAndreas Gohr     *
303bd08ebd1SAndreas Gohr     * @return bool
304bd08ebd1SAndreas Gohr     */
30534aae6dbSAndreas Gohr    private function _step_download() {
3063c38de15SAndreas Gohr        $this->_say($this->getLang('dl_from'), $this->tgzurl);
30734aae6dbSAndreas Gohr
30858774abaSFrancis GUDIN        @set_time_limit(300);
30934aae6dbSAndreas Gohr        @ignore_user_abort();
31034aae6dbSAndreas Gohr
31134aae6dbSAndreas Gohr        $http          = new DokuHTTPClient();
31258774abaSFrancis GUDIN        $http->timeout = 300;
31334aae6dbSAndreas Gohr        $data          = $http->get($this->tgzurl);
31434aae6dbSAndreas Gohr
31534aae6dbSAndreas Gohr        if(!$data) {
3166e7afd39SAndreas Gohr            $this->_warn($http->error);
3176e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
31834aae6dbSAndreas Gohr            return false;
31934aae6dbSAndreas Gohr        }
32034aae6dbSAndreas Gohr
32134aae6dbSAndreas Gohr        if(!io_saveFile($this->tgzfile, $data)) {
3226e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
32334aae6dbSAndreas Gohr            return false;
32434aae6dbSAndreas Gohr        }
32534aae6dbSAndreas Gohr
326a7b56078SAndreas Gohr        $this->_say($this->getLang('dl_done'), filesize_h(strlen($data)));
3273c38de15SAndreas Gohr
32834aae6dbSAndreas Gohr        return true;
32934aae6dbSAndreas Gohr    }
33034aae6dbSAndreas Gohr
331bd08ebd1SAndreas Gohr    /**
332bd08ebd1SAndreas Gohr     * Unpack the tarball
333bd08ebd1SAndreas Gohr     *
334bd08ebd1SAndreas Gohr     * @return bool
335bd08ebd1SAndreas Gohr     */
33634aae6dbSAndreas Gohr    private function _step_unpack() {
3373c38de15SAndreas Gohr        $this->_say('<b>'.$this->getLang('pk_extract').'</b>');
33834aae6dbSAndreas Gohr
33958774abaSFrancis GUDIN        @set_time_limit(300);
34034aae6dbSAndreas Gohr        @ignore_user_abort();
34134aae6dbSAndreas Gohr
342d04a99cdSAndreas Gohr        try {
343d04a99cdSAndreas Gohr            $tar = new VerboseTar();
344d04a99cdSAndreas Gohr            $tar->open($this->tgzfile);
345d04a99cdSAndreas Gohr            $tar->extract($this->tgzdir, 1);
346d04a99cdSAndreas Gohr            $tar->close();
347d04a99cdSAndreas Gohr        } catch (Exception $e) {
348d04a99cdSAndreas Gohr            $this->_warn($e->getMessage());
3496e7afd39SAndreas Gohr            $this->_warn($this->getLang('pk_fail'));
35034aae6dbSAndreas Gohr            return false;
35134aae6dbSAndreas Gohr        }
35234aae6dbSAndreas Gohr
3533c38de15SAndreas Gohr        $this->_say($this->getLang('pk_done'));
354738c0102SAndreas Gohr
355bd08ebd1SAndreas Gohr        $this->_say(
356bd08ebd1SAndreas Gohr            $this->getLang('pk_version'),
357738c0102SAndreas Gohr            hsc(file_get_contents($this->tgzdir.'/VERSION')),
358bd08ebd1SAndreas Gohr            getVersion()
359bd08ebd1SAndreas Gohr        );
36034aae6dbSAndreas Gohr        return true;
36134aae6dbSAndreas Gohr    }
36234aae6dbSAndreas Gohr
363bd08ebd1SAndreas Gohr    /**
364bd08ebd1SAndreas Gohr     * Check permissions of files to change
365bd08ebd1SAndreas Gohr     *
366bd08ebd1SAndreas Gohr     * @return bool
367bd08ebd1SAndreas Gohr     */
3683c38de15SAndreas Gohr    private function _step_check() {
3693c38de15SAndreas Gohr        $this->_say($this->getLang('ck_start'));
3703c38de15SAndreas Gohr        $ok = $this->_traverse('', true);
37134aae6dbSAndreas Gohr        if($ok) {
3723c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
37334aae6dbSAndreas Gohr        } else {
3746e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('ck_fail').'</b>');
37534aae6dbSAndreas Gohr        }
3763c38de15SAndreas Gohr        return $ok;
3773c38de15SAndreas Gohr    }
37834aae6dbSAndreas Gohr
379bd08ebd1SAndreas Gohr    /**
380bd08ebd1SAndreas Gohr     * Copy over new files
381bd08ebd1SAndreas Gohr     *
382bd08ebd1SAndreas Gohr     * @return bool
383bd08ebd1SAndreas Gohr     */
3843c38de15SAndreas Gohr    private function _step_copy() {
3853c38de15SAndreas Gohr        $this->_say($this->getLang('cp_start'));
3863c38de15SAndreas Gohr        $ok = $this->_traverse('', false);
3873c38de15SAndreas Gohr        if($ok) {
3883c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
38963712694SAndreas Gohr            $this->_rmold();
390e32047e3SAndreas Gohr            $this->_say('<b>'.$this->getLang('finish').'</b>');
39134aae6dbSAndreas Gohr        } else {
3926e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('cp_fail').'</b>');
39334aae6dbSAndreas Gohr        }
39434aae6dbSAndreas Gohr        return $ok;
39534aae6dbSAndreas Gohr    }
39634aae6dbSAndreas Gohr
397bd08ebd1SAndreas Gohr    /**
398bd08ebd1SAndreas Gohr     * Delete outdated files
399bd08ebd1SAndreas Gohr     */
40063712694SAndreas Gohr    private function _rmold() {
401a85c9abbSAndreas Gohr        global $conf;
402a85c9abbSAndreas Gohr
40363712694SAndreas Gohr        $list = file($this->tgzdir.'data/deleted.files');
40463712694SAndreas Gohr        foreach($list as $line) {
40563712694SAndreas Gohr            $line = trim(preg_replace('/#.*$/', '', $line));
40663712694SAndreas Gohr            if(!$line) continue;
40763712694SAndreas Gohr            $file = DOKU_INC.$line;
40863712694SAndreas Gohr            if(!file_exists($file)) continue;
40963712694SAndreas Gohr            if((is_dir($file) && $this->_rdel($file)) ||
410bd08ebd1SAndreas Gohr                @unlink($file)
411bd08ebd1SAndreas Gohr            ) {
412e32047e3SAndreas Gohr                $this->_say($this->getLang('rm_done'), hsc($line));
41363712694SAndreas Gohr            } else {
4146e7afd39SAndreas Gohr                $this->_warn($this->getLang('rm_fail'), hsc($line));
41563712694SAndreas Gohr            }
41663712694SAndreas Gohr        }
417331439fbSAndreas Gohr        // delete install
418331439fbSAndreas Gohr        @unlink(DOKU_INC.'install.php');
419de440c89SAndreas Gohr
420de440c89SAndreas Gohr        // make sure update message will be gone
421de440c89SAndreas Gohr        @touch(DOKU_INC.'doku.php');
422a85c9abbSAndreas Gohr        @unlink($conf['cachedir'].'/messages.txt');
42363712694SAndreas Gohr    }
42463712694SAndreas Gohr
425bd08ebd1SAndreas Gohr    /**
426bd08ebd1SAndreas Gohr     * Traverse over the given dir and compare it to the DokuWiki dir
427bd08ebd1SAndreas Gohr     *
428bd08ebd1SAndreas Gohr     * Checks what files need an update, tests for writability and copies
429bd08ebd1SAndreas Gohr     *
430bd08ebd1SAndreas Gohr     * @param string $dir
431bd08ebd1SAndreas Gohr     * @param bool   $dryrun do not copy but only check permissions
432bd08ebd1SAndreas Gohr     * @return bool
433bd08ebd1SAndreas Gohr     */
43434aae6dbSAndreas Gohr    private function _traverse($dir, $dryrun) {
43534aae6dbSAndreas Gohr        $base = $this->tgzdir;
43634aae6dbSAndreas Gohr        $ok   = true;
43734aae6dbSAndreas Gohr
43834aae6dbSAndreas Gohr        $dh = @opendir($base.'/'.$dir);
439bd08ebd1SAndreas Gohr        if(!$dh) return false;
44034aae6dbSAndreas Gohr        while(($file = readdir($dh)) !== false) {
44134aae6dbSAndreas Gohr            if($file == '.' || $file == '..') continue;
44234aae6dbSAndreas Gohr            $from = "$base/$dir/$file";
44334aae6dbSAndreas Gohr            $to   = DOKU_INC."$dir/$file";
44434aae6dbSAndreas Gohr
44534aae6dbSAndreas Gohr            if(is_dir($from)) {
44634aae6dbSAndreas Gohr                if($dryrun) {
44734aae6dbSAndreas Gohr                    // just check for writability
44834aae6dbSAndreas Gohr                    if(!is_dir($to)) {
44934aae6dbSAndreas Gohr                        if(is_dir(dirname($to)) && !is_writable(dirname($to))) {
4506e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
45134aae6dbSAndreas Gohr                            $ok = false;
45234aae6dbSAndreas Gohr                        }
45334aae6dbSAndreas Gohr                    }
45434aae6dbSAndreas Gohr                }
45534aae6dbSAndreas Gohr
45634aae6dbSAndreas Gohr                // recursion
45734aae6dbSAndreas Gohr                if(!$this->_traverse("$dir/$file", $dryrun)) {
45834aae6dbSAndreas Gohr                    $ok = false;
45934aae6dbSAndreas Gohr                }
46034aae6dbSAndreas Gohr            } else {
461f2fa6d10SAndreas Gohr                $fmd5 = md5(@file_get_contents($from));
462f2fa6d10SAndreas Gohr                $tmd5 = md5(@file_get_contents($to));
4636deeb3b1SAndreas Gohr                if($fmd5 != $tmd5 || !file_exists($to)) {
46434aae6dbSAndreas Gohr                    if($dryrun) {
46534aae6dbSAndreas Gohr                        // just check for writability
46634aae6dbSAndreas Gohr                        if((file_exists($to) && !is_writable($to)) ||
467bd08ebd1SAndreas Gohr                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to)))
468bd08ebd1SAndreas Gohr                        ) {
46934aae6dbSAndreas Gohr
4706e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
47134aae6dbSAndreas Gohr                            $ok = false;
47234aae6dbSAndreas Gohr                        } else {
4733c38de15SAndreas Gohr                            $this->_say($this->getLang('tv_upd'), hsc("$dir/$file"));
47434aae6dbSAndreas Gohr                        }
47534aae6dbSAndreas Gohr                    } else {
47634aae6dbSAndreas Gohr                        // check dir
47734aae6dbSAndreas Gohr                        if(io_mkdir_p(dirname($to))) {
47834aae6dbSAndreas Gohr                            // copy
47934aae6dbSAndreas Gohr                            if(!copy($from, $to)) {
4806e7afd39SAndreas Gohr                                $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file"));
48134aae6dbSAndreas Gohr                                $ok = false;
48234aae6dbSAndreas Gohr                            } else {
4833c38de15SAndreas Gohr                                $this->_say($this->getLang('tv_done'), hsc("$dir/$file"));
48434aae6dbSAndreas Gohr                            }
48534aae6dbSAndreas Gohr                        } else {
4866e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir"));
48734aae6dbSAndreas Gohr                            $ok = false;
48834aae6dbSAndreas Gohr                        }
48934aae6dbSAndreas Gohr                    }
49034aae6dbSAndreas Gohr                }
49134aae6dbSAndreas Gohr            }
49234aae6dbSAndreas Gohr        }
49334aae6dbSAndreas Gohr        closedir($dh);
49434aae6dbSAndreas Gohr        return $ok;
49534aae6dbSAndreas Gohr    }
49634aae6dbSAndreas Gohr}
49734aae6dbSAndreas Gohr
49834aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
499