Ejemplo n.º 1
0
 /** Load the site state */
 static function load_site_state()
 {
     $path = self::$site_dir . '/data/site.json';
     $data = @file_get_contents($path);
     if ($data === false) {
         # version <= 0.1.3 ?
         if (is_readable(gb::$site_dir . '/site.json')) {
             gb::$site_state = @json_decode(file_get_contents(gb::$site_dir . '/site.json'), true);
         }
         return gb::$site_state !== null;
     }
     gb::$site_state = json_decode($data, true);
     if (gb::$site_state === null || is_string(gb::$site_state)) {
         self::log(LOG_WARNING, 'syntax error in site.json -- moved to site.json.broken and creating new');
         if (!rename($path, $path . '.broken')) {
             self::log(LOG_WARNING, 'failed to move "%s" to "%s"', $path, $path . '.broken');
         }
         gb::$site_state = null;
         return false;
     }
     return true;
 }
Ejemplo n.º 2
0
 static function sync_site_state()
 {
     ignore_user_abort(true);
     # verify repo setup, which also makes sure the repo setup (hooks, config,
     # etc) is up to date:
     self::repair_repo_setup();
     # assure gitblog submodule is set up
     $dotgitmodules = gb::$site_dir . '/.gitmodules';
     if (!is_file($dotgitmodules) || !preg_match('/\\[submodule[\\s\\t ]+"gitblog"\\]/', file_get_contents($dotgitmodules))) {
         self::add_gitblog_submodule();
     }
     # read id of HEAD and current branch
     $gb_branch = 'master';
     $gb_head = '0000000000000000000000000000000000000000';
     try {
         $branches = trim(git::exec('branch --no-abbrev --verbose --no-color', null, gb::$dir . '/.git', gb::$dir));
         foreach (explode("\n", $branches) as $line) {
             if (!$line) {
                 continue;
             }
             if ($line[0] === '*') {
                 if (strpos($line, '(no branch)') !== false) {
                     $line = preg_split('/[ \\t]+/', $line, 5);
                     $gb_branch = null;
                     $gb_head_id = $line[3];
                 } else {
                     $line = preg_split('/[ \\t]+/', $line, 4);
                     $gb_branch = $line[1];
                     $gb_head_id = $line[2];
                 }
                 break;
             }
         }
     } catch (GitError $e) {
         gb::log(LOG_WARNING, 'failed to read branch info for gitblog -- git: %s', $e->getMessage());
     }
     # no previous state?
     if (!gb::$site_state) {
         gb::$site_state = array();
     }
     # Set current values
     gb::$site_state['url'] = gb::$site_url;
     gb::$site_state['version'] = gb::$version;
     gb::$site_state['posts_pagesize'] = gb::$posts_pagesize;
     # appeard in 0.1.3:
     gb::$site_state['gitblog'] = array('branch' => $gb_branch, 'head' => $gb_head_id);
     # Write site url for hooks
     $bytes_written = file_put_contents(gb::$site_dir . '/.git/info/gitblog-site-url', gb::$site_url, LOCK_EX);
     # Encode site.json
     $json = json::pretty(gb::$site_state) . "\n";
     $path = gb::$site_dir . '/data/site.json';
     # create data/ ?
     if (!is_dir(gb::$site_dir . '/data')) {
         mkdir(gb::$site_dir . '/data', 0775);
         chmod(gb::$site_dir . '/data', 0775);
     }
     # Write site.json
     $bytes_written += file_put_contents($path, $json, LOCK_EX);
     chmod($path, 0664);
     gb::log(LOG_NOTICE, 'wrote site state to %s (%d bytes)', $path, $bytes_written);
     return $bytes_written;
 }
Ejemplo n.º 3
0
    static function _000104($from, $to)
    {
        $datadir = gb::$site_dir . '/data/';
        $added = array();
        # create data/
        if (!file_exists($datadir)) {
            gb::log('creating directory %s', $datadir);
            mkdir($datadir, 0775);
            chmod($datadir, 0775);
        }
        # load old site.json
        gb::log('loading %s', gb::$site_dir . '/site.json');
        gb::$site_state = is_readable(gb::$site_dir . '/site.json') ? json_decode(file_get_contents(gb::$site_dir . '/site.json'), true) : array();
        # move site.json:plugins to data/plugins.json
        $plugins = isset(gb::$site_state['plugins']) ? gb::$site_state['plugins'] : array();
        gb::log('creating data:plugins');
        gb::data('plugins')->storage()->set($plugins);
        unset(gb::$site_state['plugins']);
        # write data/site.json
        gb::log('moving %s -> data:site', gb::$site_dir . '/site.json');
        # gb_maint::sync_site_state() will be called after this method returns
        @unlink(gb::$site_dir . '/site.json');
        # remove /site.json from .gitignore
        if (gb_maint::gitignore_sub('/(?:\\r?\\n)\\/site\\.json([\\t\\s \\r\\n]+|^)/m', '$1')) {
            gb::log('removed "/site.json" from .gitignore');
            $added[] = git::add('.gitignore');
        }
        # load settings.json
        gb::log('loading %s', gb::$site_dir . '/settings.json');
        $settings = is_readable(gb::$site_dir . '/settings.json') ? json_decode(file_get_contents(gb::$site_dir . '/settings.json'), true) : array();
        # move settings.json:* to data/plugins/*
        foreach ($settings as $pluginn => $d) {
            if (!is_array($d)) {
                $d = $d !== null ? array($d) : array();
            }
            if ($d) {
                gb::log('copying %s:%s -> data:plugins/%s', gb::$site_dir . '/settings.json', $pluginn, $pluginn);
                gb::data('plugins/' . $pluginn)->storage()->set($d);
            }
        }
        gb::log('removing old %s', gb::$site_dir . '/settings.json');
        @unlink(gb::$site_dir . '/settings.json');
        # load gb-users.php
        $users = array();
        if (is_readable(gb::$site_dir . '/gb-users.php')) {
            gb::log('loading %s', gb::$site_dir . '/gb-users.php');
            eval('class GBUserAccount {
				static function __set_state($state) {
					return GBUser::__set_state($state);
				}
			}');
            require gb::$site_dir . '/gb-users.php';
            if (isset($db)) {
                $admin = isset($db['_admin']) ? $db['_admin'] : '';
                foreach ($db as $email => $user) {
                    if (is_object($user)) {
                        $user->admin = $email === $admin;
                        $users[$email] = $user;
                        gb::log('transponded user %s', $email);
                    }
                }
            }
        }
        # move gb-users.php to data/users.json
        gb::log('moving %s -> data:users', gb::$site_dir . '/gb-users.php');
        GBUser::storage()->set($users);
        @unlink(gb::$site_dir . '/gb-users.php');
        # commit any modifications
        if ($added) {
            try {
                git::commit('upgrade 0.1.4 modified ' . implode(', ', $added), GBUser::findAdmin()->gitAuthor(), $added);
            } catch (GitError $e) {
                if (strpos($e->getMessage(), 'no changes added to commit') === false) {
                    throw $e;
                }
            }
        }
    }