xref: /plugin/upgrade/admin.php (revision eb52c46a343434f52d0105c529efb816add8ca38)
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
50d391262fSAndreas Gohr        $this->_say('<div id="plugin__upgrade">');
5175e9d164SAndreas Gohr        // enable auto scroll
5275e9d164SAndreas Gohr        ?>
5375e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
54d391262fSAndreas Gohr            var plugin_upgrade = window.setInterval(function () {
55d4376367SAndreas Gohr                var obj = document.getElementById('plugin__upgrade');
5675e9d164SAndreas Gohr                if (obj) obj.scrollTop = obj.scrollHeight;
5775e9d164SAndreas Gohr            }, 25);
5875e9d164SAndreas Gohr        </script>
5975e9d164SAndreas Gohr        <?php
6075e9d164SAndreas Gohr
6175e9d164SAndreas Gohr        // handle current step
6222c39b33SAndreas Gohr        $this->_stepit($abrt, $next);
6334aae6dbSAndreas Gohr
6475e9d164SAndreas Gohr        // disable auto scroll
6575e9d164SAndreas Gohr        ?>
6675e9d164SAndreas Gohr        <script language="javascript" type="text/javascript">
6775e9d164SAndreas Gohr            window.setTimeout(function () {
68d391262fSAndreas Gohr                window.clearInterval(plugin_upgrade);
6975e9d164SAndreas Gohr            }, 50);
7075e9d164SAndreas Gohr        </script>
7175e9d164SAndreas Gohr        <?php
7275e9d164SAndreas Gohr        $this->_say('</div>');
7375e9d164SAndreas Gohr
74d391262fSAndreas Gohr        echo '<form action="" method="get" id="plugin__upgrade_form">';
7534aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
76d391262fSAndreas Gohr        echo '<input type="hidden" name="page" value="upgrade" />';
7741163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />';
7875e9d164SAndreas Gohr        if($next) echo '<input type="submit" name="step['.$next.']" value="Continue" class="button continue" />';
7975e9d164SAndreas Gohr        if($abrt) echo '<input type="submit" name="step[cancel]" value="Abort" class="button abort" />';
8034aae6dbSAndreas Gohr        echo '</form>';
81*eb52c46aSAndreas Gohr
82*eb52c46aSAndreas Gohr        $this->_progress($next);
83*eb52c46aSAndreas Gohr    }
84*eb52c46aSAndreas Gohr
85*eb52c46aSAndreas Gohr    /**
86*eb52c46aSAndreas Gohr     * Display a progress bar of all steps
87*eb52c46aSAndreas Gohr     *
88*eb52c46aSAndreas Gohr     * @param string $next the next step
89*eb52c46aSAndreas Gohr     */
90*eb52c46aSAndreas Gohr    private function _progress($next) {
91*eb52c46aSAndreas Gohr        $steps  = array('version', 'download', 'unpack', 'check', 'upgrade');
92*eb52c46aSAndreas Gohr        $active = true;
93*eb52c46aSAndreas Gohr        $count = 0;
94*eb52c46aSAndreas Gohr
95*eb52c46aSAndreas Gohr        echo '<div id="plugin__upgrade_meter"><ol>';
96*eb52c46aSAndreas Gohr        foreach($steps as $step) {
97*eb52c46aSAndreas Gohr            $count++;
98*eb52c46aSAndreas Gohr            if($step == $next) $active = false;
99*eb52c46aSAndreas Gohr            if($active) {
100*eb52c46aSAndreas Gohr                echo '<li class="active">';
101*eb52c46aSAndreas Gohr                echo '<span class="step">✔</span>';
102*eb52c46aSAndreas Gohr            } else {
103*eb52c46aSAndreas Gohr                echo '<li>';
104*eb52c46aSAndreas Gohr                echo '<span class="step">'.$count.'</span>';
105*eb52c46aSAndreas Gohr            }
106*eb52c46aSAndreas Gohr
107*eb52c46aSAndreas Gohr            echo '<span class="stage">'.$this->getLang('step_'.$step).'</span>';
108*eb52c46aSAndreas Gohr            echo '</li>';
109*eb52c46aSAndreas Gohr        }
110*eb52c46aSAndreas Gohr        echo '</ol></div>';
11134aae6dbSAndreas Gohr    }
11234aae6dbSAndreas Gohr
113bd08ebd1SAndreas Gohr    /**
114bd08ebd1SAndreas Gohr     * Decides the current step and executes it
115bd08ebd1SAndreas Gohr     *
116bd08ebd1SAndreas Gohr     * @param bool $abrt
117bd08ebd1SAndreas Gohr     * @param bool $next
118bd08ebd1SAndreas Gohr     */
11934aae6dbSAndreas Gohr    private function _stepit(&$abrt, &$next) {
1204bed5591SAndreas Gohr        global $conf;
1214bed5591SAndreas Gohr        if($conf['safemodehack']) {
1224bed5591SAndreas Gohr            $abrt = false;
1234bed5591SAndreas Gohr            $next = false;
1244bed5591SAndreas Gohr            echo $this->locale_xhtml('safemode');
1254bed5591SAndreas Gohr        }
1264bed5591SAndreas Gohr
12734aae6dbSAndreas Gohr        if(isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
12834aae6dbSAndreas Gohr            $step = array_shift(array_keys($_REQUEST['step']));
12934aae6dbSAndreas Gohr        } else {
13034aae6dbSAndreas Gohr            $step = '';
13134aae6dbSAndreas Gohr        }
13234aae6dbSAndreas Gohr
13334aae6dbSAndreas Gohr        if($step == 'cancel') {
13434aae6dbSAndreas Gohr            # cleanup
13534aae6dbSAndreas Gohr            @unlink($this->tgzfile);
1366b2d1b30SAndreas Gohr            $this->_rdel($this->tgzdir);
13734aae6dbSAndreas Gohr            $step = '';
13834aae6dbSAndreas Gohr        }
13934aae6dbSAndreas Gohr
14034aae6dbSAndreas Gohr        if($step) {
14134aae6dbSAndreas Gohr            $abrt = true;
14234aae6dbSAndreas Gohr            $next = false;
143f378cb7eSAndreas Gohr            if($step == 'version') {
144f378cb7eSAndreas Gohr                $this->_step_version();
145f378cb7eSAndreas Gohr                $next = 'download';
146f378cb7eSAndreas Gohr            } elseif(!file_exists($this->tgzfile)) {
14734aae6dbSAndreas Gohr                if($this->_step_download()) $next = 'unpack';
14834aae6dbSAndreas Gohr            } elseif(!is_dir($this->tgzdir)) {
14934aae6dbSAndreas Gohr                if($this->_step_unpack()) $next = 'check';
15034aae6dbSAndreas Gohr            } elseif($step != 'upgrade') {
1513c38de15SAndreas Gohr                if($this->_step_check()) $next = 'upgrade';
15234aae6dbSAndreas Gohr            } elseif($step == 'upgrade') {
1533c38de15SAndreas Gohr                if($this->_step_copy()) $next = 'cancel';
15434aae6dbSAndreas Gohr            } else {
15575e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
15634aae6dbSAndreas Gohr            }
15734aae6dbSAndreas Gohr        } else {
15834aae6dbSAndreas Gohr            # first time run, show intro
15934aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
16034aae6dbSAndreas Gohr            $abrt = false;
161f378cb7eSAndreas Gohr            $next = 'version';
16234aae6dbSAndreas Gohr        }
16334aae6dbSAndreas Gohr    }
16434aae6dbSAndreas Gohr
165bd08ebd1SAndreas Gohr    /**
166bd08ebd1SAndreas Gohr     * Output the given arguments using vsprintf and flush buffers
167bd08ebd1SAndreas Gohr     */
16834aae6dbSAndreas Gohr    private function _say() {
16934aae6dbSAndreas Gohr        $args = func_get_args();
17034aae6dbSAndreas Gohr        echo vsprintf(array_shift($args)."<br />\n", $args);
17134aae6dbSAndreas Gohr        flush();
17234aae6dbSAndreas Gohr        ob_flush();
17334aae6dbSAndreas Gohr    }
17434aae6dbSAndreas Gohr
1756b2d1b30SAndreas Gohr    /**
1766b2d1b30SAndreas Gohr     * Recursive delete
1776b2d1b30SAndreas Gohr     *
1789285faa5SAndreas Gohr     * @author Jon Hassall
1799285faa5SAndreas Gohr     * @link   http://de.php.net/manual/en/function.unlink.php#87045
1806b2d1b30SAndreas Gohr     */
1819285faa5SAndreas Gohr    private function _rdel($dir) {
1829285faa5SAndreas Gohr        if(!$dh = @opendir($dir)) {
183bd08ebd1SAndreas Gohr            return false;
1849285faa5SAndreas Gohr        }
1859285faa5SAndreas Gohr        while(false !== ($obj = readdir($dh))) {
1869285faa5SAndreas Gohr            if($obj == '.' || $obj == '..') continue;
1879285faa5SAndreas Gohr
1889285faa5SAndreas Gohr            if(!@unlink($dir.'/'.$obj)) {
1899285faa5SAndreas Gohr                $this->_rdel($dir.'/'.$obj);
1909285faa5SAndreas Gohr            }
1919285faa5SAndreas Gohr        }
1929285faa5SAndreas Gohr        closedir($dh);
193e32047e3SAndreas Gohr        return @rmdir($dir);
1946b2d1b30SAndreas Gohr    }
1956b2d1b30SAndreas Gohr
196bd08ebd1SAndreas Gohr    /**
197f378cb7eSAndreas Gohr     * Check various versions
198f378cb7eSAndreas Gohr     *
199f378cb7eSAndreas Gohr     * @return bool
200f378cb7eSAndreas Gohr     */
201f378cb7eSAndreas Gohr    private function _step_version() {
202f378cb7eSAndreas Gohr        $ok = true;
203f378cb7eSAndreas Gohr
204f378cb7eSAndreas Gohr        // check if PHP is up to date
205f378cb7eSAndreas Gohr        if(version_compare(phpversion(), '5.2.0', '<')) {
206f378cb7eSAndreas Gohr            $this->_say('<div class="error">'.$this->getLang('vs_php').'</div>');
207f378cb7eSAndreas Gohr            $ok = false;
208f378cb7eSAndreas Gohr        }
209f378cb7eSAndreas Gohr
210f378cb7eSAndreas Gohr        // get the available version
211f378cb7eSAndreas Gohr        $http       = new DokuHTTPClient();
212f378cb7eSAndreas Gohr        $tgzversion = $http->get($this->tgzversion);
213f378cb7eSAndreas Gohr        if(!$tgzversion) {
214f378cb7eSAndreas Gohr            $this->_say('<div class="error">'.$this->getLang('vs_tgzno').' '.hsc($http->error).'</div>');
215f378cb7eSAndreas Gohr            $ok = false;
216f378cb7eSAndreas Gohr        }
217f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $tgzversion, $m)) {
218f378cb7eSAndreas Gohr            $this->_say('<div class="error">'.$this->getLang('vs_tgzno').'</div>');
219f378cb7eSAndreas Gohr            $ok            = false;
220f378cb7eSAndreas Gohr            $tgzversionnum = 0;
221f378cb7eSAndreas Gohr        } else {
222f378cb7eSAndreas Gohr            $tgzversionnum = $m[2];
223f378cb7eSAndreas Gohr            $this->_say($this->getLang('vs_tgz'), $tgzversion);
224f378cb7eSAndreas Gohr        }
225f378cb7eSAndreas Gohr
226f378cb7eSAndreas Gohr        // get the current version
227f378cb7eSAndreas Gohr        $version = getVersion();
228f378cb7eSAndreas Gohr        if(!preg_match('/(^| )(\d\d\d\d-\d\d-\d\d[a-z]*)( |$)/i', $version, $m)) {
229f378cb7eSAndreas Gohr            $versionnum = 0;
230f378cb7eSAndreas Gohr        } else {
231f378cb7eSAndreas Gohr            $versionnum = $m[2];
232f378cb7eSAndreas Gohr        }
233f378cb7eSAndreas Gohr        $this->_say($this->getLang('vs_local'), $version);
234f378cb7eSAndreas Gohr
235f378cb7eSAndreas Gohr        // compare versions
236f378cb7eSAndreas Gohr        if(!$versionnum) {
237f378cb7eSAndreas Gohr            $this->_say('<div class="error">'.$this->getLang('vs_localno').'</div>');
238f378cb7eSAndreas Gohr            $ok = false;
239f378cb7eSAndreas Gohr        } else if($tgzversionnum) {
240f378cb7eSAndreas Gohr            if($tgzversionnum < $versionnum) {
241f378cb7eSAndreas Gohr                $this->_say('<div class="error">'.$this->getLang('vs_newer').'</div>');
242f378cb7eSAndreas Gohr                $ok = false;
243f378cb7eSAndreas Gohr            } elseif($tgzversionnum == $versionnum) {
244f378cb7eSAndreas Gohr                $this->_say('<div class="error">'.$this->getLang('vs_same').'</div>');
245f378cb7eSAndreas Gohr                $ok = false;
246f378cb7eSAndreas Gohr            }
247f378cb7eSAndreas Gohr        }
248f378cb7eSAndreas Gohr
249f378cb7eSAndreas Gohr        // check plugin version
250f378cb7eSAndreas Gohr        $pluginversion = $http->get($this->pluginversion);
251f378cb7eSAndreas Gohr        if($pluginversion) {
252f378cb7eSAndreas Gohr            $plugininfo = linesToHash(explode("\n", $pluginversion));
253f378cb7eSAndreas Gohr            $myinfo     = $this->getInfo();
254f378cb7eSAndreas Gohr            if($plugininfo['date'] > $myinfo['date']) {
255f378cb7eSAndreas Gohr                $this->_say('<div class="error">'.$this->getLang('vs_plugin').'</div>');
256f378cb7eSAndreas Gohr                $ok = false;
257f378cb7eSAndreas Gohr            }
258f378cb7eSAndreas Gohr        }
259f378cb7eSAndreas Gohr
260f378cb7eSAndreas Gohr        return $ok;
261f378cb7eSAndreas Gohr    }
262f378cb7eSAndreas Gohr
263f378cb7eSAndreas Gohr    /**
264bd08ebd1SAndreas Gohr     * Download the tarball
265bd08ebd1SAndreas Gohr     *
266bd08ebd1SAndreas Gohr     * @return bool
267bd08ebd1SAndreas Gohr     */
26834aae6dbSAndreas Gohr    private function _step_download() {
2693c38de15SAndreas Gohr        $this->_say($this->getLang('dl_from'), $this->tgzurl);
27034aae6dbSAndreas Gohr
27134aae6dbSAndreas Gohr        @set_time_limit(120);
27234aae6dbSAndreas Gohr        @ignore_user_abort();
27334aae6dbSAndreas Gohr
27434aae6dbSAndreas Gohr        $http          = new DokuHTTPClient();
27534aae6dbSAndreas Gohr        $http->timeout = 120;
27634aae6dbSAndreas Gohr        $data          = $http->get($this->tgzurl);
27734aae6dbSAndreas Gohr
27834aae6dbSAndreas Gohr        if(!$data) {
27934aae6dbSAndreas Gohr            $this->_say($http->error);
2803c38de15SAndreas Gohr            $this->_say($this->getLang('dl_fail'));
28134aae6dbSAndreas Gohr            return false;
28234aae6dbSAndreas Gohr        }
28334aae6dbSAndreas Gohr
28434aae6dbSAndreas Gohr        if(!io_saveFile($this->tgzfile, $data)) {
2853c38de15SAndreas Gohr            $this->_say($this->getLang('dl_fail'));
28634aae6dbSAndreas Gohr            return false;
28734aae6dbSAndreas Gohr        }
28834aae6dbSAndreas Gohr
289a7b56078SAndreas Gohr        $this->_say($this->getLang('dl_done'), filesize_h(strlen($data)));
2903c38de15SAndreas Gohr
29134aae6dbSAndreas Gohr        return true;
29234aae6dbSAndreas Gohr    }
29334aae6dbSAndreas Gohr
294bd08ebd1SAndreas Gohr    /**
295bd08ebd1SAndreas Gohr     * Unpack the tarball
296bd08ebd1SAndreas Gohr     *
297bd08ebd1SAndreas Gohr     * @return bool
298bd08ebd1SAndreas Gohr     */
29934aae6dbSAndreas Gohr    private function _step_unpack() {
30034aae6dbSAndreas Gohr        global $conf;
3013c38de15SAndreas Gohr        $this->_say('<b>'.$this->getLang('pk_extract').'</b>');
30234aae6dbSAndreas Gohr
30334aae6dbSAndreas Gohr        @set_time_limit(120);
30434aae6dbSAndreas Gohr        @ignore_user_abort();
30534aae6dbSAndreas Gohr
30634aae6dbSAndreas Gohr        $tar = new VerboseTarLib($this->tgzfile);
30734aae6dbSAndreas Gohr        if($tar->_initerror < 0) {
30834aae6dbSAndreas Gohr            $this->_say($tar->TarErrorStr($tar->_initerror));
3093c38de15SAndreas Gohr            $this->_say($this->getLang('pk_fail'));
31034aae6dbSAndreas Gohr            return false;
31134aae6dbSAndreas Gohr        }
31234aae6dbSAndreas Gohr
31334aae6dbSAndreas Gohr        $ok = $tar->Extract(VerboseTarLib::FULL_ARCHIVE, $this->tgzdir, 1, $conf['fmode'], '/^(_cs|_test|\.gitignore)/');
31434aae6dbSAndreas Gohr        if($ok < 1) {
31534aae6dbSAndreas Gohr            $this->_say($tar->TarErrorStr($ok));
3163c38de15SAndreas Gohr            $this->_say($this->getLang('pk_fail'));
31734aae6dbSAndreas Gohr            return false;
31834aae6dbSAndreas Gohr        }
31934aae6dbSAndreas Gohr
3203c38de15SAndreas Gohr        $this->_say($this->getLang('pk_done'));
321738c0102SAndreas Gohr
322bd08ebd1SAndreas Gohr        $this->_say(
323bd08ebd1SAndreas Gohr            $this->getLang('pk_version'),
324738c0102SAndreas Gohr            hsc(file_get_contents($this->tgzdir.'/VERSION')),
325bd08ebd1SAndreas Gohr            getVersion()
326bd08ebd1SAndreas Gohr        );
32734aae6dbSAndreas Gohr        return true;
32834aae6dbSAndreas Gohr    }
32934aae6dbSAndreas Gohr
330bd08ebd1SAndreas Gohr    /**
331bd08ebd1SAndreas Gohr     * Check permissions of files to change
332bd08ebd1SAndreas Gohr     *
333bd08ebd1SAndreas Gohr     * @return bool
334bd08ebd1SAndreas Gohr     */
3353c38de15SAndreas Gohr    private function _step_check() {
3363c38de15SAndreas Gohr        $this->_say($this->getLang('ck_start'));
3373c38de15SAndreas Gohr        $ok = $this->_traverse('', true);
33834aae6dbSAndreas Gohr        if($ok) {
3393c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('ck_done').'</b>');
34034aae6dbSAndreas Gohr        } else {
3413c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('ck_fail').'</b>');
34234aae6dbSAndreas Gohr        }
3433c38de15SAndreas Gohr        return $ok;
3443c38de15SAndreas Gohr    }
34534aae6dbSAndreas Gohr
346bd08ebd1SAndreas Gohr    /**
347bd08ebd1SAndreas Gohr     * Copy over new files
348bd08ebd1SAndreas Gohr     *
349bd08ebd1SAndreas Gohr     * @return bool
350bd08ebd1SAndreas Gohr     */
3513c38de15SAndreas Gohr    private function _step_copy() {
3523c38de15SAndreas Gohr        $this->_say($this->getLang('cp_start'));
3533c38de15SAndreas Gohr        $ok = $this->_traverse('', false);
3543c38de15SAndreas Gohr        if($ok) {
3553c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('cp_done').'</b>');
35663712694SAndreas Gohr            $this->_rmold();
357e32047e3SAndreas Gohr            $this->_say('<b>'.$this->getLang('finish').'</b>');
35834aae6dbSAndreas Gohr        } else {
3593c38de15SAndreas Gohr            $this->_say('<b>'.$this->getLang('cp_fail').'</b>');
36034aae6dbSAndreas Gohr        }
36134aae6dbSAndreas Gohr        return $ok;
36234aae6dbSAndreas Gohr    }
36334aae6dbSAndreas Gohr
364bd08ebd1SAndreas Gohr    /**
365bd08ebd1SAndreas Gohr     * Delete outdated files
366bd08ebd1SAndreas Gohr     */
36763712694SAndreas Gohr    private function _rmold() {
36863712694SAndreas Gohr        $list = file($this->tgzdir.'data/deleted.files');
36963712694SAndreas Gohr        foreach($list as $line) {
37063712694SAndreas Gohr            $line = trim(preg_replace('/#.*$/', '', $line));
37163712694SAndreas Gohr            if(!$line) continue;
37263712694SAndreas Gohr            $file = DOKU_INC.$line;
37363712694SAndreas Gohr            if(!file_exists($file)) continue;
37463712694SAndreas Gohr            if((is_dir($file) && $this->_rdel($file)) ||
375bd08ebd1SAndreas Gohr                @unlink($file)
376bd08ebd1SAndreas Gohr            ) {
377e32047e3SAndreas Gohr                $this->_say($this->getLang('rm_done'), hsc($line));
37863712694SAndreas Gohr            } else {
379e32047e3SAndreas Gohr                $this->_say($this->getLang('rm_fail'), hsc($line));
38063712694SAndreas Gohr            }
38163712694SAndreas Gohr        }
38263712694SAndreas Gohr    }
38363712694SAndreas Gohr
384bd08ebd1SAndreas Gohr    /**
385bd08ebd1SAndreas Gohr     * Traverse over the given dir and compare it to the DokuWiki dir
386bd08ebd1SAndreas Gohr     *
387bd08ebd1SAndreas Gohr     * Checks what files need an update, tests for writability and copies
388bd08ebd1SAndreas Gohr     *
389bd08ebd1SAndreas Gohr     * @param string $dir
390bd08ebd1SAndreas Gohr     * @param bool   $dryrun do not copy but only check permissions
391bd08ebd1SAndreas Gohr     * @return bool
392bd08ebd1SAndreas Gohr     */
39334aae6dbSAndreas Gohr    private function _traverse($dir, $dryrun) {
39434aae6dbSAndreas Gohr        $base = $this->tgzdir;
39534aae6dbSAndreas Gohr        $ok   = true;
39634aae6dbSAndreas Gohr
39734aae6dbSAndreas Gohr        $dh = @opendir($base.'/'.$dir);
398bd08ebd1SAndreas Gohr        if(!$dh) return false;
39934aae6dbSAndreas Gohr        while(($file = readdir($dh)) !== false) {
40034aae6dbSAndreas Gohr            if($file == '.' || $file == '..') continue;
40134aae6dbSAndreas Gohr            $from = "$base/$dir/$file";
40234aae6dbSAndreas Gohr            $to   = DOKU_INC."$dir/$file";
40334aae6dbSAndreas Gohr
40434aae6dbSAndreas Gohr            if(is_dir($from)) {
40534aae6dbSAndreas Gohr                if($dryrun) {
40634aae6dbSAndreas Gohr                    // just check for writability
40734aae6dbSAndreas Gohr                    if(!is_dir($to)) {
40834aae6dbSAndreas Gohr                        if(is_dir(dirname($to)) && !is_writable(dirname($to))) {
409a7b56078SAndreas Gohr                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
41034aae6dbSAndreas Gohr                            $ok = false;
41134aae6dbSAndreas Gohr                        }
41234aae6dbSAndreas Gohr                    }
41334aae6dbSAndreas Gohr                }
41434aae6dbSAndreas Gohr
41534aae6dbSAndreas Gohr                // recursion
41634aae6dbSAndreas Gohr                if(!$this->_traverse("$dir/$file", $dryrun)) {
41734aae6dbSAndreas Gohr                    $ok = false;
41834aae6dbSAndreas Gohr                }
41934aae6dbSAndreas Gohr            } else {
420f2fa6d10SAndreas Gohr                $fmd5 = md5(@file_get_contents($from));
421f2fa6d10SAndreas Gohr                $tmd5 = md5(@file_get_contents($to));
4226deeb3b1SAndreas Gohr                if($fmd5 != $tmd5 || !file_exists($to)) {
42334aae6dbSAndreas Gohr                    if($dryrun) {
42434aae6dbSAndreas Gohr                        // just check for writability
42534aae6dbSAndreas Gohr                        if((file_exists($to) && !is_writable($to)) ||
426bd08ebd1SAndreas Gohr                            (!file_exists($to) && is_dir(dirname($to)) && !is_writable(dirname($to)))
427bd08ebd1SAndreas Gohr                        ) {
42834aae6dbSAndreas Gohr
429a7b56078SAndreas Gohr                            $this->_say('<b>'.$this->getLang('tv_noperm').'</b>', hsc("$dir/$file"));
43034aae6dbSAndreas Gohr                            $ok = false;
43134aae6dbSAndreas Gohr                        } else {
4323c38de15SAndreas Gohr                            $this->_say($this->getLang('tv_upd'), hsc("$dir/$file"));
43334aae6dbSAndreas Gohr                        }
43434aae6dbSAndreas Gohr                    } else {
43534aae6dbSAndreas Gohr                        // check dir
43634aae6dbSAndreas Gohr                        if(io_mkdir_p(dirname($to))) {
43734aae6dbSAndreas Gohr                            // copy
43834aae6dbSAndreas Gohr                            if(!copy($from, $to)) {
4393c38de15SAndreas Gohr                                $this->_say('<b>'.$this->getLang('tv_nocopy').'</b>', hsc("$dir/$file"));
44034aae6dbSAndreas Gohr                                $ok = false;
44134aae6dbSAndreas Gohr                            } else {
4423c38de15SAndreas Gohr                                $this->_say($this->getLang('tv_done'), hsc("$dir/$file"));
44334aae6dbSAndreas Gohr                            }
44434aae6dbSAndreas Gohr                        } else {
4453c38de15SAndreas Gohr                            $this->_say('<b>'.$this->getLang('tv_nodir').'</b>', hsc("$dir"));
44634aae6dbSAndreas Gohr                            $ok = false;
44734aae6dbSAndreas Gohr                        }
44834aae6dbSAndreas Gohr                    }
44934aae6dbSAndreas Gohr                }
45034aae6dbSAndreas Gohr            }
45134aae6dbSAndreas Gohr        }
45234aae6dbSAndreas Gohr        closedir($dh);
45334aae6dbSAndreas Gohr        return $ok;
45434aae6dbSAndreas Gohr    }
45534aae6dbSAndreas Gohr}
45634aae6dbSAndreas Gohr
45734aae6dbSAndreas Gohr// vim:ts=4:sw=4:et:enc=utf-8:
458