xref: /plugin/upgrade/admin.php (revision e90f51473c7dddb959d2852bbe60c511f25ae129)
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';
14*e90f5147SAndreas Gohrrequire_once DOKU_PLUGIN.'upgrade/HTTPClient.php';
1534aae6dbSAndreas Gohr
16ff284f1fSAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin {
1734aae6dbSAndreas Gohr    private $tgzurl;
1834aae6dbSAndreas Gohr    private $tgzfile;
1934aae6dbSAndreas Gohr    private $tgzdir;
20f378cb7eSAndreas Gohr    private $tgzversion;
21f378cb7eSAndreas Gohr    private $pluginversion;
2234aae6dbSAndreas Gohr
234ed3677eSAndreas Gohr    protected $haderrors = false;
244ed3677eSAndreas Gohr
2534aae6dbSAndreas Gohr    public function __construct() {
2634aae6dbSAndreas Gohr        global $conf;
2734aae6dbSAndreas Gohr
2834aae6dbSAndreas Gohr        $branch = 'stable';
2934aae6dbSAndreas Gohr
30bd08ebd1SAndreas Gohr        $this->tgzurl        = "https://github.com/splitbrain/dokuwiki/archive/$branch.tar.gz";
31d391262fSAndreas Gohr        $this->tgzfile       = $conf['tmpdir'].'/dokuwiki-upgrade.tgz';
32d391262fSAndreas Gohr        $this->tgzdir        = $conf['tmpdir'].'/dokuwiki-upgrade/';
33f378cb7eSAndreas Gohr        $this->tgzversion    = "https://raw.githubusercontent.com/splitbrain/dokuwiki/$branch/VERSION";
34f378cb7eSAndreas Gohr        $this->pluginversion = "https://raw.githubusercontent.com/splitbrain/dokuwiki-plugin-upgrade/master/plugin.info.txt";
3534aae6dbSAndreas Gohr    }
3634aae6dbSAndreas Gohr
37bd08ebd1SAndreas Gohr    public function getMenuSort() {
38bd08ebd1SAndreas Gohr        return 555;
39bd08ebd1SAndreas Gohr    }
4034aae6dbSAndreas Gohr
4141163f44SAndreas Gohr    public function handle() {
4241163f44SAndreas Gohr        if($_REQUEST['step'] && !checkSecurityToken()) {
4341163f44SAndreas Gohr            unset($_REQUEST['step']);
4441163f44SAndreas Gohr        }
4534aae6dbSAndreas Gohr    }
4634aae6dbSAndreas Gohr
4734aae6dbSAndreas Gohr    public function html() {
4834aae6dbSAndreas Gohr        $abrt = false;
4934aae6dbSAndreas Gohr        $next = false;
5034aae6dbSAndreas Gohr
5134aae6dbSAndreas Gohr        echo '<h1>'.$this->getLang('menu').'</h1>';
5234aae6dbSAndreas Gohr
5306dc93a8SAndreas Gohr        global $conf;
5406dc93a8SAndreas Gohr        if($conf['safemodehack']) {
5506dc93a8SAndreas Gohr            $abrt = false;
5606dc93a8SAndreas Gohr            $next = false;
5706dc93a8SAndreas Gohr            echo $this->locale_xhtml('safemode');
5806dc93a8SAndreas Gohr            return;
5906dc93a8SAndreas Gohr        }
6006dc93a8SAndreas Gohr
61d391262fSAndreas Gohr        $this->_say('<div id="plugin__upgrade">');
6275e9d164SAndreas Gohr        // enable auto scroll
6375e9d164SAndreas Gohr        ?>
6475e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
65d391262fSAndreas Gohr            var plugin_upgrade = window.setInterval(function () {
66d4376367SAndreas Gohr                var obj = document.getElementById('plugin__upgrade');
6775e9d164SAndreas Gohr                if (obj) obj.scrollTop = obj.scrollHeight;
6875e9d164SAndreas Gohr            }, 25);
6975e9d164SAndreas Gohr        </script>
7075e9d164SAndreas Gohr        <?php
7175e9d164SAndreas Gohr
7275e9d164SAndreas Gohr        // handle current step
7322c39b33SAndreas Gohr        $this->_stepit($abrt, $next);
7434aae6dbSAndreas Gohr
7575e9d164SAndreas Gohr        // disable auto scroll
7675e9d164SAndreas Gohr        ?>
7775e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
7875e9d164SAndreas Gohr            window.setTimeout(function () {
79d391262fSAndreas Gohr                window.clearInterval(plugin_upgrade);
8075e9d164SAndreas Gohr            }, 50);
8175e9d164SAndreas Gohr        </script>
8275e9d164SAndreas Gohr        <?php
8375e9d164SAndreas Gohr        $this->_say('</div>');
8475e9d164SAndreas Gohr
854ed3677eSAndreas Gohr        $careful = '';
864ed3677eSAndreas Gohr        if($this->haderrors) {
874ed3677eSAndreas Gohr            echo '<div id="plugin__upgrade_careful">'.$this->getLang('careful').'</div>';
884ed3677eSAndreas Gohr            $careful = 'careful';
894ed3677eSAndreas Gohr        }
904ed3677eSAndreas Gohr
919f8d4355SAndreas Gohr        $action = script();
929f8d4355SAndreas Gohr        echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">';
9334aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
94d391262fSAndreas Gohr        echo '<input type="hidden" name="page" value="upgrade" />';
9541163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
9638665ab9SAndreas Gohr        if($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue '.$careful.'">' . $this->getLang('btn_continue') . ' ➡</button>';
979f8d4355SAndreas Gohr        if($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>';
9834aae6dbSAndreas Gohr        echo '</form>';
99eb52c46aSAndreas Gohr
100eb52c46aSAndreas Gohr        $this->_progress($next);
101eb52c46aSAndreas Gohr    }
102eb52c46aSAndreas Gohr
103eb52c46aSAndreas Gohr    /**
104eb52c46aSAndreas Gohr     * Display a progress bar of all steps
105eb52c46aSAndreas Gohr     *
106eb52c46aSAndreas Gohr     * @param string $next the next step
107eb52c46aSAndreas Gohr     */
108eb52c46aSAndreas Gohr    private function _progress($next) {
109eb52c46aSAndreas Gohr        $steps  = array('version', 'download', 'unpack', 'check', 'upgrade');
110eb52c46aSAndreas Gohr        $active = true;
111eb52c46aSAndreas Gohr        $count = 0;
112eb52c46aSAndreas Gohr
113eb52c46aSAndreas Gohr        echo '<div id="plugin__upgrade_meter"><ol>';
114eb52c46aSAndreas Gohr        foreach($steps as $step) {
115eb52c46aSAndreas Gohr            $count++;
116eb52c46aSAndreas Gohr            if($step == $next) $active = false;
117eb52c46aSAndreas Gohr            if($active) {
118eb52c46aSAndreas Gohr                echo '<li class="active">';
119eb52c46aSAndreas Gohr                echo '<span class="step">✔</span>';
120eb52c46aSAndreas Gohr            } else {
121eb52c46aSAndreas Gohr                echo '<li>';
122eb52c46aSAndreas Gohr                echo '<span class="step">'.$count.'</span>';
123eb52c46aSAndreas Gohr            }
124eb52c46aSAndreas Gohr
125eb52c46aSAndreas Gohr            echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>';
126eb52c46aSAndreas Gohr            echo '</li>';
127eb52c46aSAndreas Gohr        }
128eb52c46aSAndreas Gohr        echo '</ol></div>';
12934aae6dbSAndreas Gohr    }
13034aae6dbSAndreas Gohr
131bd08ebd1SAndreas Gohr    /**
132bd08ebd1SAndreas Gohr     * Decides the current step and executes it
133bd08ebd1SAndreas Gohr     *
134bd08ebd1SAndreas Gohr     * @param bool $abrt
135bd08ebd1SAndreas Gohr     * @param bool $next
136bd08ebd1SAndreas Gohr     */
13734aae6dbSAndreas Gohr    private function _stepit(&$abrt, &$next) {
1384bed5591SAndreas Gohr
13934aae6dbSAndreas Gohr        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
14034aae6dbSAndreas Gohr            $step = array_shift(array_keys($_REQUEST['step']));
14134aae6dbSAndreas Gohr        } else {
14234aae6dbSAndreas Gohr            $step = '';
14334aae6dbSAndreas Gohr        }
14434aae6dbSAndreas Gohr
145fb8e77caSAndreas Gohr        if($step == 'cancel' || $step == 'done') {
14634aae6dbSAndreas Gohr            # cleanup
14734aae6dbSAndreas Gohr            @unlink($this->tgzfile);
1486b2d1b30SAndreas Gohr            $this->_rdel($this->tgzdir);
149fb8e77caSAndreas Gohr            if($step == 'cancel') $step = '';
15034aae6dbSAndreas Gohr        }
15134aae6dbSAndreas Gohr
15234aae6dbSAndreas Gohr        if($step) {
15334aae6dbSAndreas Gohr            $abrt = true;
15434aae6dbSAndreas Gohr            $next = false;
155f378cb7eSAndreas Gohr            if($step == 'version') {
156f378cb7eSAndreas Gohr                $this->_step_version();
157f378cb7eSAndreas Gohr                $next = 'download';
158fb8e77caSAndreas Gohr            } elseif ($step == 'done') {
159fb8e77caSAndreas Gohr                $this->_step_done();
160fb8e77caSAndreas Gohr                $next = '';
161fb8e77caSAndreas Gohr                $abrt = '';
162f378cb7eSAndreas Gohr            } elseif(!file_exists($this->tgzfile)) {
16334aae6dbSAndreas Gohr                if($this->_step_download()) $next = 'unpack';
16434aae6dbSAndreas Gohr            } elseif(!is_dir($this->tgzdir)) {
16534aae6dbSAndreas Gohr                if($this->_step_unpack()) $next = 'check';
16634aae6dbSAndreas Gohr            } elseif($step != 'upgrade') {
1673c38de15SAndreas Gohr                if($this->_step_check()) $next = 'upgrade';
16834aae6dbSAndreas Gohr            } elseif($step == 'upgrade') {
169fb8e77caSAndreas Gohr                if($this->_step_copy()) {
170fb8e77caSAndreas Gohr                    $next = 'done';
171fb8e77caSAndreas Gohr                    $abrt = '';
172fb8e77caSAndreas Gohr                }
17334aae6dbSAndreas Gohr            } else {
17475e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
17534aae6dbSAndreas Gohr            }
17634aae6dbSAndreas Gohr        } else {
17734aae6dbSAndreas Gohr            # first time run, show intro
17834aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
17934aae6dbSAndreas Gohr            $abrt = false;
180f378cb7eSAndreas Gohr            $next = 'version';
18134aae6dbSAndreas Gohr        }
18234aae6dbSAndreas Gohr    }
18334aae6dbSAndreas Gohr
184bd08ebd1SAndreas Gohr    /**
185bd08ebd1SAndreas Gohr     * Output the given arguments using vsprintf and flush buffers
186bd08ebd1SAndreas Gohr     */
1874ed3677eSAndreas Gohr    public function _say() {
18834aae6dbSAndreas Gohr        $args = func_get_args();
1896e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/blank.gif" width="16" height="16" alt="" /> ';
1906e7afd39SAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
1916e7afd39SAndreas Gohr        flush();
1926e7afd39SAndreas Gohr        ob_flush();
1936e7afd39SAndreas Gohr    }
1946e7afd39SAndreas Gohr
1956e7afd39SAndreas Gohr    /**
1966e7afd39SAndreas Gohr     * Print a warning using the given arguments with vsprintf and flush buffers
1976e7afd39SAndreas Gohr     */
1984ed3677eSAndreas Gohr    public function _warn() {
1994ed3677eSAndreas Gohr        $this->haderrors = true;
2004ed3677eSAndreas Gohr
2016e7afd39SAndreas Gohr        $args = func_get_args();
2026e7afd39SAndreas Gohr        echo '<img src="'.DOKU_BASE.'lib/images/error.png" width="16" height="16" alt="!" /> ';
20334aae6dbSAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
20434aae6dbSAndreas Gohr        flush();
20534aae6dbSAndreas Gohr        ob_flush();
20634aae6dbSAndreas Gohr    }
20734aae6dbSAndreas Gohr
2086b2d1b30SAndreas Gohr    /**
2096b2d1b30SAndreas Gohr     * Recursive delete
2106b2d1b30SAndreas Gohr     *
2119285faa5SAndreas Gohr     * @author Jon Hassall
2129285faa5SAndreas Gohr     * @link   http://de.php.net/manual/en/function.unlink.php#87045
2136b2d1b30SAndreas Gohr     */
2149285faa5SAndreas Gohr    private function _rdel($dir) {
2159285faa5SAndreas Gohr        if(!$dh = @opendir($dir)) {
216bd08ebd1SAndreas Gohr            return false;
2179285faa5SAndreas Gohr        }
2189285faa5SAndreas Gohr        while(false !== ($obj = readdir($dh))) {
2199285faa5SAndreas Gohr            if($obj == '.' || $obj == '..') continue;
2209285faa5SAndreas Gohr
2219285faa5SAndreas Gohr            if(!@unlink($dir.'/'.$obj)) {
2229285faa5SAndreas Gohr                $this->_rdel($dir.'/'.$obj);
2239285faa5SAndreas Gohr            }
2249285faa5SAndreas Gohr        }
2259285faa5SAndreas Gohr        closedir($dh);
226e32047e3SAndreas Gohr        return @rmdir($dir);
2276b2d1b30SAndreas Gohr    }
2286b2d1b30SAndreas Gohr
229bd08ebd1SAndreas Gohr    /**
230f378cb7eSAndreas Gohr     * Check various versions
231f378cb7eSAndreas Gohr     *
232f378cb7eSAndreas Gohr     * @return bool
233f378cb7eSAndreas Gohr     */
234f378cb7eSAndreas Gohr    private function _step_version() {
235f378cb7eSAndreas Gohr        $ok = true;
236f378cb7eSAndreas Gohr
237c37aa5dcSAndreas Gohr        // we need SSL - only newer HTTPClients check that themselves
238c37aa5dcSAndreas Gohr        if(!in_array('ssl', stream_get_transports())) {
239c37aa5dcSAndreas Gohr            $this->_warn($this->getLang('vs_ssl'));
240c37aa5dcSAndreas Gohr            $ok = false;
241c37aa5dcSAndreas Gohr        }
242c37aa5dcSAndreas Gohr
243f378cb7eSAndreas Gohr        // get the available version
244f378cb7eSAndreas Gohr        $http       = new DokuHTTPClient();
245f378cb7eSAndreas Gohr        $tgzversion = $http->get($this->tgzversion);
246f378cb7eSAndreas Gohr        if(!$tgzversion) {
2476e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno').' '.hsc($http->error));
248f378cb7eSAndreas Gohr            $ok = false;
249f378cb7eSAndreas Gohr        }
250f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) {
2516e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_tgzno'));
252f378cb7eSAndreas Gohr            $ok            = false;
253f378cb7eSAndreas Gohr            $tgzversionnum = 0;
254f378cb7eSAndreas Gohr        } else {
255f378cb7eSAndreas Gohr            $tgzversionnum = $m[2];
256f378cb7eSAndreas Gohr            $this->_say($this->getLang('vs_tgz'), $tgzversion);
257f378cb7eSAndreas Gohr        }
258f378cb7eSAndreas Gohr
259f378cb7eSAndreas Gohr        // get the current version
260f378cb7eSAndreas Gohr        $version = getVersion();
261f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) {
262f378cb7eSAndreas Gohr            $versionnum = 0;
263f378cb7eSAndreas Gohr        } else {
264f378cb7eSAndreas Gohr            $versionnum = $m[2];
265f378cb7eSAndreas Gohr        }
266f378cb7eSAndreas Gohr        $this->_say($this->getLang('vs_local'), $version);
267f378cb7eSAndreas Gohr
268f378cb7eSAndreas Gohr        // compare versions
269f378cb7eSAndreas Gohr        if(!$versionnum) {
2706e7afd39SAndreas Gohr            $this->_warn($this->getLang('vs_localno'));
271f378cb7eSAndreas Gohr            $ok = false;
272f378cb7eSAndreas Gohr        } else if($tgzversionnum) {
273f378cb7eSAndreas Gohr            if($tgzversionnum < $versionnum) {
2746e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_newer'));
275f378cb7eSAndreas Gohr                $ok = false;
276f378cb7eSAndreas Gohr            } elseif($tgzversionnum == $versionnum) {
2776e7afd39SAndreas Gohr                $this->_warn($this->getLang('vs_same'));
278f378cb7eSAndreas Gohr                $ok = false;
279f378cb7eSAndreas Gohr            }
280f378cb7eSAndreas Gohr        }
281f378cb7eSAndreas Gohr
282f378cb7eSAndreas Gohr        // check plugin version
283f378cb7eSAndreas Gohr        $pluginversion = $http->get($this->pluginversion);
284f378cb7eSAndreas Gohr        if($pluginversion) {
285f378cb7eSAndreas Gohr            $plugininfo = linesToHash(explode("\n", $pluginversion));
286f378cb7eSAndreas Gohr            $myinfo     = $this->getInfo();
287f378cb7eSAndreas Gohr            if($plugininfo['date'] > $myinfo['date']) {
2887cb119ecSAndreas Gohr                $this->_warn($this->getLang('vs_plugin'), $plugininfo['date']);
289f378cb7eSAndreas Gohr                $ok = false;
290f378cb7eSAndreas Gohr            }
291f378cb7eSAndreas Gohr        }
292f378cb7eSAndreas Gohr
2939bbe795aSAndreas Gohr        // check if PHP is up to date
294cd074bc5SAndreas Gohr        $minphp = '5.6';
2959bbe795aSAndreas Gohr        if(version_compare(phpversion(), $minphp, '<')) {
296a92a10d4SAndreas Gohr            $this->_warn($this->getLang('vs_php'), $minphp, phpversion());
2979bbe795aSAndreas Gohr            $ok = false;
2989bbe795aSAndreas Gohr        }
2999bbe795aSAndreas Gohr
300f378cb7eSAndreas Gohr        return $ok;
301f378cb7eSAndreas Gohr    }
302f378cb7eSAndreas Gohr
303f378cb7eSAndreas Gohr    /**
304fb8e77caSAndreas Gohr     * Redirect to the start page
305fb8e77caSAndreas Gohr     */
306fb8e77caSAndreas Gohr    private function _step_done() {
307fb8e77caSAndreas Gohr        echo $this->getLang('finish');
30861f0f3bfSAndreas Gohr        echo "<script type='text/javascript'>location.href='".DOKU_URL."';</script>";
309fb8e77caSAndreas Gohr    }
310fb8e77caSAndreas Gohr
311fb8e77caSAndreas Gohr    /**
312bd08ebd1SAndreas Gohr     * Download the tarball
313bd08ebd1SAndreas Gohr     *
314bd08ebd1SAndreas Gohr     * @return bool
315bd08ebd1SAndreas Gohr     */
31634aae6dbSAndreas Gohr    private function _step_download() {
3173c38de15SAndreas Gohr        $this->_say($this->getLang('dl_from'), $this->tgzurl);
31834aae6dbSAndreas Gohr
31958774abaSFrancis GUDIN        @set_time_limit(300);
32034aae6dbSAndreas Gohr        @ignore_user_abort();
32134aae6dbSAndreas Gohr
32234aae6dbSAndreas Gohr        $http          = new DokuHTTPClient();
32358774abaSFrancis GUDIN        $http->timeout = 300;
32434aae6dbSAndreas Gohr        $data          = $http->get($this->tgzurl);
32534aae6dbSAndreas Gohr
32634aae6dbSAndreas Gohr        if(!$data) {
3276e7afd39SAndreas Gohr            $this->_warn($http->error);
3286e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
32934aae6dbSAndreas Gohr            return false;
33034aae6dbSAndreas Gohr        }
33134aae6dbSAndreas Gohr
33234aae6dbSAndreas Gohr        if(!io_saveFile($this->tgzfile, $data)) {
3336e7afd39SAndreas Gohr            $this->_warn($this->getLang('dl_fail'));
33434aae6dbSAndreas Gohr            return false;
33534aae6dbSAndreas Gohr        }
33634aae6dbSAndreas Gohr
337a7b56078SAndreas Gohr        $this->_say($this->getLang('dl_done'), filesize_h(strlen($data)));
3383c38de15SAndreas Gohr
33934aae6dbSAndreas Gohr        return true;
34034aae6dbSAndreas Gohr    }
34134aae6dbSAndreas Gohr
342bd08ebd1SAndreas Gohr    /**
343bd08ebd1SAndreas Gohr     * Unpack the tarball
344bd08ebd1SAndreas Gohr     *
345bd08ebd1SAndreas Gohr     * @return bool
346bd08ebd1SAndreas Gohr     */
34734aae6dbSAndreas Gohr    private function _step_unpack() {
3483c38de15SAndreas Gohr        $this->_say('<b>'.$this->getLang('pk_extract').'</b>');
34934aae6dbSAndreas Gohr
35058774abaSFrancis GUDIN        @set_time_limit(300);
35134aae6dbSAndreas Gohr        @ignore_user_abort();
35234aae6dbSAndreas Gohr
353d04a99cdSAndreas Gohr        try {
354d04a99cdSAndreas Gohr            $tar = new VerboseTar();
355d04a99cdSAndreas Gohr            $tar->open($this->tgzfile);
356d04a99cdSAndreas Gohr            $tar->extract($this->tgzdir, 1);
357d04a99cdSAndreas Gohr            $tar->close();
358d04a99cdSAndreas Gohr        } catch (Exception $e) {
359d04a99cdSAndreas Gohr            $this->_warn($e->getMessage());
3606e7afd39SAndreas Gohr            $this->_warn($this->getLang('pk_fail'));
36134aae6dbSAndreas Gohr            return false;
36234aae6dbSAndreas Gohr        }
36334aae6dbSAndreas Gohr
3643c38de15SAndreas Gohr        $this->_say($this->getLang('pk_done'));
365738c0102SAndreas Gohr
366bd08ebd1SAndreas Gohr        $this->_say(
367bd08ebd1SAndreas Gohr            $this->getLang('pk_version'),
368738c0102SAndreas Gohr            hsc(file_get_contents($this->tgzdir.'/VERSION')),
369bd08ebd1SAndreas Gohr            getVersion()
370bd08ebd1SAndreas Gohr        );
37134aae6dbSAndreas Gohr        return true;
37234aae6dbSAndreas Gohr    }
37334aae6dbSAndreas Gohr
374bd08ebd1SAndreas Gohr    /**
375bd08ebd1SAndreas Gohr     * Check permissions of files to change
376bd08ebd1SAndreas Gohr     *
377bd08ebd1SAndreas Gohr     * @return bool
378bd08ebd1SAndreas Gohr     */
3793c38de15SAndreas Gohr    private function _step_check() {
3803c38de15SAndreas Gohr        $this->_say($this->getLang('ck_start'));
3813c38de15SAndreas Gohr        $ok = $this->_traverse('', true);
38234aae6dbSAndreas Gohr        if($ok) {
3833c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
38434aae6dbSAndreas Gohr        } else {
3856e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('ck_fail').'</b>');
38634aae6dbSAndreas Gohr        }
3873c38de15SAndreas Gohr        return $ok;
3883c38de15SAndreas Gohr    }
38934aae6dbSAndreas Gohr
390bd08ebd1SAndreas Gohr    /**
391bd08ebd1SAndreas Gohr     * Copy over new files
392bd08ebd1SAndreas Gohr     *
393bd08ebd1SAndreas Gohr     * @return bool
394bd08ebd1SAndreas Gohr     */
3953c38de15SAndreas Gohr    private function _step_copy() {
3963c38de15SAndreas Gohr        $this->_say($this->getLang('cp_start'));
3973c38de15SAndreas Gohr        $ok = $this->_traverse('', false);
3983c38de15SAndreas Gohr        if($ok) {
3993c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
40063712694SAndreas Gohr            $this->_rmold();
401e32047e3SAndreas Gohr            $this->_say('<b>'.$this->getLang('finish').'</b>');
40234aae6dbSAndreas Gohr        } else {
4036e7afd39SAndreas Gohr            $this->_warn('<b>'.$this->getLang('cp_fail').'</b>');
40434aae6dbSAndreas Gohr        }
40534aae6dbSAndreas Gohr        return $ok;
40634aae6dbSAndreas Gohr    }
40734aae6dbSAndreas Gohr
408bd08ebd1SAndreas Gohr    /**
409bd08ebd1SAndreas Gohr     * Delete outdated files
410bd08ebd1SAndreas Gohr     */
41163712694SAndreas Gohr    private function _rmold() {
412a85c9abbSAndreas Gohr        global $conf;
413a85c9abbSAndreas Gohr
41463712694SAndreas Gohr        $list = file($this->tgzdir.'data/deleted.files');
41563712694SAndreas Gohr        foreach($list as $line) {
41663712694SAndreas Gohr            $line = trim(preg_replace('/#.*$/', '', $line));
41763712694SAndreas Gohr            if(!$line) continue;
41863712694SAndreas Gohr            $file = DOKU_INC.$line;
41963712694SAndreas Gohr            if(!file_exists($file)) continue;
4207b7da9aaSAndreas Gohr
4217b7da9aaSAndreas Gohr            // check that the given file is an case sensitive match
4227b7da9aaSAndreas Gohr            if(basename(realpath($file)) != basename($file)) {
4237b7da9aaSAndreas Gohr                $this->_warn($this->getLang('rm_mismatch'), hsc($line));
4247b7da9aaSAndreas Gohr            }
4257b7da9aaSAndreas Gohr
42663712694SAndreas Gohr            if((is_dir($file) && $this->_rdel($file)) ||
427bd08ebd1SAndreas Gohr                @unlink($file)
428bd08ebd1SAndreas Gohr            ) {
429e32047e3SAndreas Gohr                $this->_say($this->getLang('rm_done'), hsc($line));
43063712694SAndreas Gohr            } else {
4316e7afd39SAndreas Gohr                $this->_warn($this->getLang('rm_fail'), hsc($line));
43263712694SAndreas Gohr            }
43363712694SAndreas Gohr        }
434331439fbSAndreas Gohr        // delete install
435331439fbSAndreas Gohr        @unlink(DOKU_INC.'install.php');
436de440c89SAndreas Gohr
437de440c89SAndreas Gohr        // make sure update message will be gone
438de440c89SAndreas Gohr        @touch(DOKU_INC.'doku.php');
439a85c9abbSAndreas Gohr        @unlink($conf['cachedir'].'/messages.txt');
44063712694SAndreas Gohr    }
44163712694SAndreas Gohr
442bd08ebd1SAndreas Gohr    /**
443bd08ebd1SAndreas Gohr     * Traverse over the given dir and compare it to the DokuWiki dir
444bd08ebd1SAndreas Gohr     *
445bd08ebd1SAndreas Gohr     * Checks what files need an update, tests for writability and copies
446bd08ebd1SAndreas Gohr     *
447bd08ebd1SAndreas Gohr     * @param string $dir
448bd08ebd1SAndreas Gohr     * @param bool   $dryrun do not copy but only check permissions
449bd08ebd1SAndreas Gohr     * @return bool
450bd08ebd1SAndreas Gohr     */
45134aae6dbSAndreas Gohr    private function _traverse($dir, $dryrun) {
45234aae6dbSAndreas Gohr        $base = $this->tgzdir;
45334aae6dbSAndreas Gohr        $ok   = true;
45434aae6dbSAndreas Gohr
45534aae6dbSAndreas Gohr        $dh = @opendir($base.'/'.$dir);
456bd08ebd1SAndreas Gohr        if(!$dh) return false;
45734aae6dbSAndreas Gohr        while(($file = readdir($dh)) !== false) {
45834aae6dbSAndreas Gohr            if($file == '.' || $file == '..') continue;
45934aae6dbSAndreas Gohr            $from = "$base/$dir/$file";
46034aae6dbSAndreas Gohr            $to   = DOKU_INC."$dir/$file";
46134aae6dbSAndreas Gohr
46234aae6dbSAndreas Gohr            if(is_dir($from)) {
46334aae6dbSAndreas Gohr                if($dryrun) {
46434aae6dbSAndreas Gohr                    // just check for writability
46534aae6dbSAndreas Gohr                    if(!is_dir($to)) {
46634aae6dbSAndreas Gohr                        if(is_dir(dirname($to)) && !is_writable(dirname($to))) {
4676e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
46834aae6dbSAndreas Gohr                            $ok = false;
46934aae6dbSAndreas Gohr                        }
47034aae6dbSAndreas Gohr                    }
47134aae6dbSAndreas Gohr                }
47234aae6dbSAndreas Gohr
47334aae6dbSAndreas Gohr                // recursion
47434aae6dbSAndreas Gohr                if(!$this->_traverse("$dir/$file", $dryrun)) {
47534aae6dbSAndreas Gohr                    $ok = false;
47634aae6dbSAndreas Gohr                }
47734aae6dbSAndreas Gohr            } else {
478f2fa6d10SAndreas Gohr                $fmd5 = md5(@file_get_contents($from));
479f2fa6d10SAndreas Gohr                $tmd5 = md5(@file_get_contents($to));
4806deeb3b1SAndreas Gohr                if($fmd5 != $tmd5 || !file_exists($to)) {
48134aae6dbSAndreas Gohr                    if($dryrun) {
48234aae6dbSAndreas Gohr                        // just check for writability
48334aae6dbSAndreas Gohr                        if((file_exists($to) && !is_writable($to)) ||
484bd08ebd1SAndreas Gohr                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to)))
485bd08ebd1SAndreas Gohr                        ) {
48634aae6dbSAndreas Gohr
4876e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
48834aae6dbSAndreas Gohr                            $ok = false;
48934aae6dbSAndreas Gohr                        } else {
4903c38de15SAndreas Gohr                            $this->_say($this->getLang('tv_upd'), hsc("$dir/$file"));
49134aae6dbSAndreas Gohr                        }
49234aae6dbSAndreas Gohr                    } else {
49334aae6dbSAndreas Gohr                        // check dir
49434aae6dbSAndreas Gohr                        if(io_mkdir_p(dirname($to))) {
4957b7da9aaSAndreas Gohr                            // remove existing (avoid case sensitivity problems)
4967b7da9aaSAndreas Gohr                            if(file_exists($to) && !@unlink($to)) {
4977b7da9aaSAndreas Gohr                                $this->_warn('<b>'.$this->getLang('tv_nodel').'</b>', hsc("$dir/$file"));
4987b7da9aaSAndreas Gohr                                $ok = false;
4997b7da9aaSAndreas Gohr                            }
50034aae6dbSAndreas Gohr                            // copy
50134aae6dbSAndreas Gohr                            if(!copy($from, $to)) {
5026e7afd39SAndreas Gohr                                $this->_warn('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file"));
50334aae6dbSAndreas Gohr                                $ok = false;
50434aae6dbSAndreas Gohr                            } else {
5053c38de15SAndreas Gohr                                $this->_say($this->getLang('tv_done'), hsc("$dir/$file"));
50634aae6dbSAndreas Gohr                            }
50734aae6dbSAndreas Gohr                        } else {
5086e7afd39SAndreas Gohr                            $this->_warn('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir"));
50934aae6dbSAndreas Gohr                            $ok = false;
51034aae6dbSAndreas Gohr                        }
51134aae6dbSAndreas Gohr                    }
51234aae6dbSAndreas Gohr                }
51334aae6dbSAndreas Gohr            }
51434aae6dbSAndreas Gohr        }
51534aae6dbSAndreas Gohr        closedir($dh);
51634aae6dbSAndreas Gohr        return $ok;
51734aae6dbSAndreas Gohr    }
51834aae6dbSAndreas Gohr}
51934aae6dbSAndreas Gohr
52034aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
521