Inheritance: extends GBAuthor
Example #1
0
 /** Tries to resolve committer as a GBUser */
 function committerUser()
 {
     $user = $this->committerEmail ? GBUser::find($this->committerEmail) : null;
     if ($user === null) {
         return $default;
     }
 }
Example #2
0
 static function storage()
 {
     if (self::$_storage === null) {
         self::$_storage = new GBObjectStore(gb::$site_dir . '/data/users.json', __CLASS__);
         self::$_storage->autocommitToRepo = true;
     }
     return self::$_storage;
 }
Example #3
0
 static function add_gitblog_submodule()
 {
     # Add gitblog submodule
     $roundtrip_temp = false;
     $dotgitmodules = gb::$dir . '/.gitmodules';
     $did_have_dotgitmodules = is_file($dotgitmodules);
     $added = array();
     $origin_url = 'git://github.com/rsms/gitblog.git';
     # first, find the origin url if any
     $broken_gitblog_repo = false;
     if (is_dir(gb::$dir . '/.git')) {
         try {
             gb::log('deducing remote.origin.url for existing gitblog');
             $s = trim(git::exec('config remote.origin.url', null, gb::$dir . '/.git', gb::$dir));
             if ($s) {
                 $origin_url = $s;
             }
         } catch (GitError $e) {
             gb::log(LOG_WARNING, 'failed to read config remote.origin.url: %s', $e->getMessage());
             $broken_gitblog_repo = true;
         }
     }
     # if gitblog is not a repo or broken, rename existing and clone a fresh copy from upstream
     if ($broken_gitblog_repo) {
         $stash_dir = gb::$dir . '.old';
         $i = 1;
         while (file_exists($stash_dir)) {
             $stash_dir = gb::$dir . '.old' . $i++;
         }
         gb::log('moving broken gitblog %s to %s', gb::$dir, $stash_dir);
         if (!rename(gb::$dir, $stash_dir)) {
             # Note: This is tricky. If we get here, it probably means we are unable to
             # write in dirname(gb::$dir) and gb::$site_dir which we will try to do
             # further down, where we clone a new copy, which will most likely fail
             # because we can not create a new directory called "gitblog" due to lack of
             # priveleges.
             #
             # Now, one solution would be to:
             #
             #   git clone origin /tmp/xy
             #   mkdir ./gitblog/old
             #   mv ./gitblog/(?!old)+ ./gitblog/old/
             #   mv /tmp/xy/* ./gitblog/
             #   rm -rf ./gitblog/old
             #
             # But as this is a very thin use case, almost vanishingly small since
             # the gitblog itself can not function w/o write rights, we die hard:
             gb::log(LOG_CRIT, 'unable to replace gitblog with gitblog submodule (mv %s %s) -- directory not writable? Aborting.', gb::$dir, $stash_dir);
             exit;
         }
     }
     try {
         # remove "/gitblog" ignore from .gitignore
         if (self::gitignore_sub('/(?:\\r?\\n)\\/gitblog([\\t\\s \\r\\n]+|^)/m', '$1')) {
             $added[] = git::add('.gitignore');
         }
         # register (and clone if needed) the gitblog submodule. This might take some time.
         try {
             git::exec('submodule --quiet add -b ' . escapeshellarg(self::$branch) . ' -- ' . escapeshellarg($origin_url) . ' gitblog');
             # add gitblog
             $added[] = git::add('gitblog');
         } catch (GitError $e) {
             if (strpos($e->getMessage(), 'already exists in the index') === false) {
                 throw $e;
             }
         }
         # move back old shallow gitblog dir
         if ($roundtrip_temp) {
             $old_dst = gb::$dir . '/gitblog.old';
             gb::log('moving %s to %s', $roundtrip_temp, $old_dst);
             if (!@rename($roundtrip_temp, $old_dst)) {
                 gb::log(LOG_ERR, 'failed to move back %s to %s', $roundtrip_temp, $old_dst);
             }
             # we want to explicitly checkout the branch we requested
             git::exec('checkout ' . escapeshellarg(self::$branch), null, gb::$dir . '/.git', gb::$dir);
         }
     } catch (Exception $e) {
         # move back gitblog dir
         if ($roundtrip_temp) {
             gb::log('moving %s to %s', $roundtrip_temp, gb::$dir);
             if (!@rename($roundtrip_temp, gb::$dir)) {
                 gb::log(LOG_ERR, 'failed to move back %s to %s', $roundtrip_temp, gb::$dir);
             }
         }
         # forward exception
         throw $e;
     }
     # if .submodules did not exist when we started, track it
     if (!$did_have_dotgitmodules && is_file(gb::$site_dir . '/.gitmodules')) {
         $added[] = git::add('.gitmodules');
     }
     # commit any modifications
     if ($added) {
         try {
             git::commit('added ' . implode(', ', $added), GBUser::findAdmin()->gitAuthor(), $added);
         } catch (GitError $e) {
             if (strpos($e->getMessage(), 'no changes added to commit') === false) {
                 throw $e;
             }
         }
     }
 }
Example #4
0
 function importChannel(DOMNode $channel, $commit)
 {
     $channel_name = $channel->getElementsByTagName('title')->item(0)->nodeValue;
     $this->report('Importing channel "' . h($channel_name) . '"');
     $fallbackTZOffset = $this->deduceChannelTimezoneOffset($channel);
     $count_posts = 0;
     $count_pages = 0;
     $count_attachments = 0;
     $timer = microtime(1);
     git::reset();
     # rollback any previously prepared commit
     try {
         foreach ($channel->getElementsByTagName('item') as $item) {
             if ($item->nodeType !== XML_ELEMENT_NODE) {
                 continue;
             }
             $obj = $this->importItem($item, $fallbackTZOffset);
             if (!$obj) {
                 continue;
             }
             if ($obj instanceof GBExposedContent) {
                 $this->postProcessExposedContent($obj);
                 $this->report('Importing ' . ($obj instanceof WPPost ? 'post' : 'page') . ' ' . h($obj->name) . ' "' . h($obj->title) . '" by ' . h($obj->author->name) . ' published ' . $obj->published);
                 if ($this->writeExposedContent($obj)) {
                     if ($obj instanceof WPPost) {
                         $count_posts++;
                     } else {
                         $count_pages++;
                     }
                 }
             } elseif ($obj instanceof WPAttachment) {
                 $this->postProcessAttachment($obj);
                 $this->report('Importing attachment ' . h($obj->name) . ' (' . h($obj->wpurl) . ')');
                 if ($this->writeAttachment($obj)) {
                     $count_attachments++;
                 }
             }
             if ($this->debug) {
                 $this->dump($obj);
             }
         }
         $timer = microtime(1) - $timer;
         $count = $count_posts + $count_pages + $count_attachments;
         $this->importedObjectsCount += $count;
         $message = 'Imported ' . counted($count, 'object', 'objects', 'zero', 'one') . ' (' . counted($count_posts, 'post', 'posts', 'zero', 'one') . ', ' . counted($count_pages, 'page', 'pages', 'zero', 'one') . ' and ' . counted($count_attachments, 'attachment', 'attachments', 'zero', 'one') . ')';
         $this->report($message . ' from channel "' . h($channel_name) . '"' . ' in ' . gb_format_duration($timer));
         if ($commit) {
             $this->report('Creating commit...');
             try {
                 git::commit($message . ' from Wordpress blog ' . $channel_name, GBUser::findAdmin()->gitAuthor());
                 $this->report('Committed to git repository');
             } catch (GitError $e) {
                 if (strpos($e->getMessage(), 'nothing added to commit') !== false) {
                     $this->report('Nothing committed because no changes where done');
                 } else {
                     throw $e;
                 }
             }
         }
     } catch (Exception $e) {
         git::reset();
         # rollback prepared commit
         throw $e;
     }
 }
Example #5
0
    # commit changes (done by gb::init())
    if (!gb::$errors) {
        try {
            if (!git::commit('gitblog created', trim($_POST['name']) . ' <' . trim($_POST['email']) . '>')) {
                gb::$errors[] = 'failed to commit creation';
            }
        } catch (Exception $e) {
            gb::$errors[] = 'failed to commit creation: ' . nl2br(h(strval($e)));
        }
    }
    # -------------------------------------------------------------------------
    # create admin account
    if (!gb::$errors) {
        $_POST['email'] = trim($_POST['email']);
        $passhash = GBUser::passhash($_POST['email'], $_POST['passphrase']);
        $u = new GBUser(trim($_POST['name']), $_POST['email'], $passhash, true);
        $u->save();
        # issues git add, that's why we do this after init
    }
    # -------------------------------------------------------------------------
    # send the client along
    if (!gb::$errors) {
        header('Location: ' . gb::$site_url);
        exit(0);
    }
}
# ------------------------------------------------------------------------------------------------
# Perform a few sanity checks
#else {
#
#}
Example #6
0
 static function loadWork($path, $post = false, $class = 'GBExposedContent', $id = null, $slug = null, $applyBodyFilters = true)
 {
     if ($post === false) {
         $post = new $class(self::pathspecFromAbsPath($path), $id, $slug);
     }
     $post->id = $id;
     gb::event('will-load-work-object', $post);
     # load rebuild plugins before calling reload
     gb::load_plugins('rebuild');
     # reload post with work data
     $post->reload(file_get_contents($path), null, $applyBodyFilters);
     # set modified date from file
     $post->modified = new GBDateTime(filemtime($path));
     # set author if needed
     if (!$post->author) {
         # GBUser have the same properties as the regular class-less author
         # object, so it's safe to just pass it on here, as a clone.
         if (gb::$authorized) {
             $post->author = clone gb::$authorized;
             unset($post->passhash);
         } elseif ($padmin = GBUser::findAdmin()) {
             $post->author = clone $padmin;
             unset($post->passhash);
         } else {
             $post->author = new GBAuthor();
         }
     }
     gb::event('did-load-work-object', $post);
     return $post;
 }
Example #7
0
 function commit()
 {
     if ($this->txFp === false) {
         throw new LogicException('transaction is not active');
     }
     $ex = null;
     try {
         $did_write = $this->txWriteData();
     } catch (Exception $e) {
         $ex = $e;
     }
     $this->txEnd($ex);
     if ($ex) {
         throw $ex;
     }
     # commit to repo
     if ($did_write && $this->autocommitToRepo) {
         gb::log('committing changes to ' . $this->file);
         if (!($author = $this->autocommitToRepoAuthor)) {
             $author = GBUser::findAdmin()->gitAuthor();
         }
         $m = $this->autocommitToRepoMessage ? $this->autocommitToRepoMessage : 'autocommit:' . $this->file;
         git::add($this->file);
         git::commit($m, $author);
         # rollback() will handle gb::reset if needed
     }
     return $did_write;
 }
Example #8
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;
                }
            }
        }
    }
Example #9
0
 static function mkmailer()
 {
     # setup conf
     $siteurl = new GBURL(gb::$site_url);
     $admin = GBUser::findAdmin();
     $default_name = $admin ? $admin->name : $siteurl->host;
     $default_address = $admin ? $admin->email : 'root@' . $siteurl->host;
     self::$conf = gb::data('email', array('admin' => array($default_name, $default_address), 'from' => array(gb::$site_title, 'noreply@' . $siteurl->host), 'smtp.example-gmail' => array('secure' => 'ssl', 'host' => 'smtp.gmail.com:465', 'username' => $default_address, 'password' => 'secret')));
     # since PHPMailer is one ugly piece of software
     $orig_error_reporting = error_reporting(E_ALL ^ E_NOTICE);
     # setup phpmailer
     $e = new PHPMailer();
     $e->From = '';
     $e->FromName = '';
     $e->PluginDir = gb::$dir . '/lib/PHPMailer/';
     # SMTP
     if (($c = self::$conf['smtp']) !== null) {
         $e->IsSMTP();
         # enable SMTP
         # authenitcation?
         $e->SMTPAuth = isset($c['password']) || isset($c['username']);
         # secure?
         if (isset($c['secure']) && $c['secure']) {
             static $allowed = array('ssl' => 1, 'tls' => 1, '' => 1);
             $c['secure'] = is_string($c['secure']) ? strtolower($c['secure']) : ($c['secure'] ? 'ssl' : '');
             if (!isset($allowed[$c['secure']])) {
                 gb::log(LOG_WARNING, 'malformed configuration: bad value for "secure": %s -- only "ssl" or "tls" is allowed', $c['secure']);
             } else {
                 $e->SMTPSecure = $c['secure'];
             }
         }
         # support for multiple hosts
         if (isset($c['host'])) {
             $e->Host = $c['host'];
         } elseif (isset($c['hosts'])) {
             $e->Host = $c['hosts'];
         }
         if (is_array($e->Host)) {
             $hosts = $e->Host;
             $e->Host = array();
             foreach ($hosts as $host) {
                 if (is_array($host)) {
                     if (!isset($host['name'])) {
                         gb::log(LOG_WARNING, 'malformed configuration: missing "name" for host %s', var_export($host, 1));
                     } else {
                         $v[] = $host['name'] . (isset($host['port']) ? ':' . $host['port'] : '');
                     }
                 } else {
                     $v[] = $host;
                 }
             }
             $e->Host = implode(';', $e->Host);
         }
         # default port
         if (isset($c['port']) && ($port = intval($c['port'])) > 0) {
             $e->Port = $port;
         }
         # username
         if (isset($c['username'])) {
             $e->Username = $c['username'];
         }
         # password
         if (isset($c['password'])) {
             $e->Password = $c['password'];
         }
         # connection timeout
         if (isset($c['timeout']) && ($i = intval($c['timeout'])) > 0) {
             $e->Timeout = $i;
         }
     }
     # gitblog <heart> UTF-8
     $e->CharSet = 'utf-8';
     # Default from
     if ($from = self::$conf['from']) {
         list($e->From, $e->FromName) = self::normalizeRecipient($from);
     }
     # Default sender
     if (($sender = self::$conf['sender']) || ($sender = self::$conf['admin'])) {
         list($e->Sender, $discard) = self::normalizeRecipient($sender);
     }
     # default priority
     if ($priority = self::$conf['priority']) {
         $e->Priority = intval($priority);
     }
     # reset error reporting
     error_reporting($orig_error_reporting);
     return $e;
 }