2011-04-28 20:39:59 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* Installer class for domain-based multi-homing systems
|
2011-04-29 11:30:30 +01:00
|
|
|
*
|
2011-04-28 20:39:59 +01:00
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category DomainStatusNetwork
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
// This check helps protect against security problems;
|
|
|
|
// your code file can't be executed directly from the web.
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Installer class for domain-based multi-homing systems
|
|
|
|
*
|
|
|
|
* @category DomainStatusNetwork
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2011 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
class DomainStatusNetworkInstaller extends Installer
|
|
|
|
{
|
|
|
|
protected $domain = null;
|
|
|
|
protected $rootname = null;
|
|
|
|
protected $sitedb = null;
|
|
|
|
protected $rootpass = null;
|
|
|
|
protected $nickname = null;
|
|
|
|
protected $sn = null;
|
2011-04-28 23:30:59 +01:00
|
|
|
|
|
|
|
public $verbose = false;
|
2011-04-28 20:39:59 +01:00
|
|
|
|
|
|
|
function __construct($domain)
|
|
|
|
{
|
|
|
|
$this->domain = $domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Go for it!
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
function main()
|
|
|
|
{
|
|
|
|
// We don't check prereqs. Check 'em before setting up a
|
|
|
|
// multi-home system, kthxbi
|
|
|
|
if ($this->prepare()) {
|
|
|
|
return $this->handle();
|
|
|
|
} else {
|
|
|
|
$this->showHelp();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get our input parameters...
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
function prepare()
|
|
|
|
{
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
|
|
$this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
|
|
|
|
|
|
|
|
// XXX make this configurable
|
|
|
|
|
|
|
|
$this->sitename = sprintf('The %s Status Network', $this->domain);
|
|
|
|
|
|
|
|
$this->server = $this->nickname.'.'.$config['WILDCARD'];
|
|
|
|
$this->path = null;
|
|
|
|
$this->fancy = true;
|
|
|
|
|
2011-04-28 23:30:59 +01:00
|
|
|
$datanick = $this->databaseize($this->nickname);
|
|
|
|
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->host = $config['DBHOSTNAME'];
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->database = $datanick.$config['DBBASE'];
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->dbtype = 'mysql'; // XXX: support others... someday
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->username = $datanick.$config['USERBASE'];
|
|
|
|
|
|
|
|
// Max size for MySQL
|
|
|
|
|
|
|
|
if (strlen($this->username) > 16) {
|
|
|
|
$this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
|
|
|
|
}
|
2011-04-28 20:39:59 +01:00
|
|
|
|
|
|
|
$pwgen = $config['PWDGEN'];
|
|
|
|
|
|
|
|
$password = `$pwgen`;
|
|
|
|
|
|
|
|
$this->password = trim($password);
|
|
|
|
|
|
|
|
// For setting up the database
|
|
|
|
|
|
|
|
$this->rootname = $config['ADMIN'];
|
|
|
|
$this->rootpass = $config['ADMINPASS'];
|
|
|
|
$this->sitehost = $config['DBHOST'];
|
|
|
|
$this->sitedb = $config['SITEDB'];
|
|
|
|
|
|
|
|
// Explicitly empty
|
|
|
|
|
|
|
|
$this->adminNick = null;
|
|
|
|
$this->adminPass = null;
|
|
|
|
$this->adminEmail = null;
|
|
|
|
$this->adminUpdates = null;
|
|
|
|
|
|
|
|
/** Should we skip writing the configuration file? */
|
|
|
|
$this->skipConfig = true;
|
|
|
|
|
|
|
|
if (!$this->validateDb()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle()
|
|
|
|
{
|
|
|
|
return $this->doInstall();
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupDatabase()
|
|
|
|
{
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->updateStatus('Creating database...');
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->createDatabase();
|
|
|
|
parent::setupDatabase();
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->updateStatus('Creating file directories...');
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->createDirectories();
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->updateStatus('Saving status network...');
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->saveStatusNetwork();
|
2011-04-28 23:30:59 +01:00
|
|
|
$this->updateStatus('Checking schema for plugins...');
|
2011-04-28 20:39:59 +01:00
|
|
|
$this->checkSchema();
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveStatusNetwork()
|
|
|
|
{
|
|
|
|
Status_network::setupDB($this->sitehost,
|
|
|
|
$this->rootname,
|
|
|
|
$this->rootpass,
|
|
|
|
$this->sitedb, array());
|
|
|
|
|
|
|
|
$sn = new Status_network();
|
|
|
|
|
|
|
|
$sn->nickname = $this->nickname;
|
|
|
|
$sn->dbhost = $this->host;
|
|
|
|
$sn->dbuser = $this->username;
|
|
|
|
$sn->dbpass = $this->password;
|
|
|
|
$sn->dbname = $this->database;
|
2011-04-28 23:30:59 +01:00
|
|
|
$sn->sitename = $this->sitename;
|
2011-04-28 20:39:59 +01:00
|
|
|
|
|
|
|
$result = $sn->insert();
|
|
|
|
|
|
|
|
if (!$result) {
|
2011-04-29 11:30:30 +01:00
|
|
|
throw new ServerException("Could not create status_network: " . print_r($sn, true));
|
2011-04-28 20:39:59 +01:00
|
|
|
}
|
|
|
|
|
2011-05-03 02:51:09 +01:00
|
|
|
// Re-fetch; stupid auto-increment integer isn't working
|
|
|
|
|
|
|
|
$sn = Status_network::staticGet('nickname', $sn->nickname);
|
|
|
|
|
|
|
|
if (empty($sn)) {
|
|
|
|
throw new ServerException("Created {$this->nickname} status_network and could not find it again.");
|
|
|
|
}
|
|
|
|
|
2011-04-28 20:39:59 +01:00
|
|
|
$sn->setTags(array('domain='.$this->domain));
|
|
|
|
|
|
|
|
$this->sn = $sn;
|
|
|
|
}
|
2011-04-29 11:30:30 +01:00
|
|
|
|
2011-04-28 20:39:59 +01:00
|
|
|
function checkSchema()
|
|
|
|
{
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
|
|
Status_network::$wildcard = $config['WILDCARD'];
|
|
|
|
|
|
|
|
StatusNet::switchSite($this->nickname);
|
|
|
|
|
|
|
|
Event::handle('CheckSchema');
|
|
|
|
}
|
|
|
|
|
|
|
|
function getStatusNetwork()
|
|
|
|
{
|
|
|
|
return $this->sn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDirectories()
|
|
|
|
{
|
|
|
|
$config = $this->getConfig();
|
|
|
|
|
|
|
|
foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
|
|
|
|
$base = $config[$key];
|
|
|
|
mkdir($base.'/'.$this->nickname, 0777, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createDatabase()
|
|
|
|
{
|
|
|
|
// Create the New DB
|
|
|
|
$res = mysql_connect($this->host, $this->rootname, $this->rootpass);
|
|
|
|
if (!$res) {
|
2011-04-29 11:30:30 +01:00
|
|
|
throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
|
2011-04-28 20:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
|
|
|
|
|
|
|
|
$return = mysql_select_db($this->database, $res);
|
|
|
|
|
|
|
|
if (!$return) {
|
2011-04-29 11:30:30 +01:00
|
|
|
throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
|
2011-04-28 20:39:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array('localhost', '%') as $src) {
|
2011-04-29 11:30:30 +01:00
|
|
|
mysql_query("GRANT ALL ON " .
|
2011-04-28 20:39:59 +01:00
|
|
|
mysql_real_escape_string($this->database).".* TO '" .
|
|
|
|
$this->username . "'@'".$src."' ".
|
|
|
|
"IDENTIFIED BY '".$this->password."'", $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
mysql_close($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getConfig()
|
|
|
|
{
|
|
|
|
static $config;
|
2011-04-29 11:30:30 +01:00
|
|
|
|
2011-04-28 20:39:59 +01:00
|
|
|
$cfg_file = "/etc/statusnet/setup.cfg";
|
|
|
|
|
|
|
|
if (empty($config)) {
|
|
|
|
$result = parse_ini_file($cfg_file);
|
|
|
|
|
|
|
|
$config = array();
|
|
|
|
foreach ($result as $key => $value) {
|
|
|
|
$key = str_replace('export ', '', $key);
|
|
|
|
$config[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
function warning($message, $submessage='')
|
|
|
|
{
|
|
|
|
print $this->html2text($message) . "\n";
|
|
|
|
if ($submessage != '') {
|
|
|
|
print " " . $this->html2text($submessage) . "\n";
|
|
|
|
}
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStatus($status, $error=false)
|
|
|
|
{
|
|
|
|
if ($this->verbose || $error) {
|
|
|
|
if ($error) {
|
|
|
|
print "ERROR: ";
|
|
|
|
}
|
|
|
|
print $this->html2text($status);
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function html2text($html)
|
|
|
|
{
|
|
|
|
// break out any links for text legibility
|
|
|
|
$breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
|
|
|
|
'\2 <\1>',
|
|
|
|
$html);
|
|
|
|
return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
|
|
|
|
}
|
2011-04-28 23:30:59 +01:00
|
|
|
|
|
|
|
function databaseize($nickname)
|
|
|
|
{
|
|
|
|
$nickname = str_replace('-', '_', $nickname);
|
|
|
|
return $nickname;
|
|
|
|
}
|
2011-04-28 20:39:59 +01:00
|
|
|
}
|