3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Installer class for domain-based multi-homing systems
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @category DomainStatusNetwork
25 * @author Evan Prodromou <evan@status.net>
26 * @copyright 2011 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Installer class for domain-based multi-homing systems
40 * @category DomainStatusNetwork
42 * @author Evan Prodromou <evan@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class DomainStatusNetworkInstaller extends Installer
49 protected $domain = null;
50 protected $rootname = null;
51 protected $sitedb = null;
52 protected $rootpass = null;
53 protected $nickname = null;
56 public $verbose = false;
58 function __construct($domain)
60 $this->domain = $domain;
65 * @return boolean success
69 // We don't check prereqs. Check 'em before setting up a
70 // multi-home system, kthxbi
71 if ($this->prepare()) {
72 return $this->handle();
80 * Get our input parameters...
81 * @return boolean success
85 $config = $this->getConfig();
87 $this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
89 // XXX make this configurable
91 $this->sitename = sprintf('The %s Status Network', $this->domain);
93 $this->server = $this->nickname.'.'.$config['WILDCARD'];
97 $datanick = $this->databaseize($this->nickname);
99 $this->host = $config['DBHOSTNAME'];
100 $this->database = $datanick.$config['DBBASE'];
101 $this->dbtype = 'mysql'; // XXX: support others... someday
102 $this->username = $datanick.$config['USERBASE'];
104 // Max size for MySQL
106 if (strlen($this->username) > 16) {
107 $this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
110 $pwgen = $config['PWDGEN'];
112 $password = `$pwgen`;
114 $this->password = trim($password);
116 // For setting up the database
118 $this->rootname = $config['ADMIN'];
119 $this->rootpass = $config['ADMINPASS'];
120 $this->sitehost = $config['DBHOST'];
121 $this->sitedb = $config['SITEDB'];
125 $this->adminNick = null;
126 $this->adminPass = null;
127 $this->adminEmail = null;
128 $this->adminUpdates = null;
130 /** Should we skip writing the configuration file? */
131 $this->skipConfig = true;
133 if (!$this->validateDb()) {
142 return $this->doInstall();
145 function setupDatabase()
147 $this->updateStatus('Creating database...');
148 $this->createDatabase();
149 parent::setupDatabase();
150 $this->updateStatus('Creating file directories...');
151 $this->createDirectories();
152 $this->updateStatus('Saving status network...');
153 $this->saveStatusNetwork();
154 $this->updateStatus('Checking schema for plugins...');
155 $this->checkSchema();
158 function saveStatusNetwork()
160 Status_network::setupDB($this->sitehost,
163 $this->sitedb, array());
165 $sn = new Status_network();
167 $sn->nickname = $this->nickname;
168 $sn->dbhost = $this->host;
169 $sn->dbuser = $this->username;
170 $sn->dbpass = $this->password;
171 $sn->dbname = $this->database;
172 $sn->sitename = $this->sitename;
174 $result = $sn->insert();
177 throw new ServerException("Could not create status_network: " . print_r($sn, true));
180 $sn->setTags(array('domain='.$this->domain));
186 function checkSchema()
188 $config = $this->getConfig();
190 Status_network::$wildcard = $config['WILDCARD'];
192 StatusNet::switchSite($this->nickname);
194 Event::handle('CheckSchema');
197 function getStatusNetwork()
202 function createDirectories()
204 $config = $this->getConfig();
206 foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
207 $base = $config[$key];
208 mkdir($base.'/'.$this->nickname, 0777, true);
212 function createDatabase()
215 $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
217 throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
220 mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
222 $return = mysql_select_db($this->database, $res);
225 throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
228 foreach (array('localhost', '%') as $src) {
229 mysql_query("GRANT ALL ON " .
230 mysql_real_escape_string($this->database).".* TO '" .
231 $this->username . "'@'".$src."' ".
232 "IDENTIFIED BY '".$this->password."'", $res);
242 $cfg_file = "/etc/statusnet/setup.cfg";
244 if (empty($config)) {
245 $result = parse_ini_file($cfg_file);
248 foreach ($result as $key => $value) {
249 $key = str_replace('export ', '', $key);
250 $config[$key] = $value;
261 function warning($message, $submessage='')
263 print $this->html2text($message) . "\n";
264 if ($submessage != '') {
265 print " " . $this->html2text($submessage) . "\n";
270 function updateStatus($status, $error=false)
272 if ($this->verbose || $error) {
276 print $this->html2text($status);
281 private function html2text($html)
283 // break out any links for text legibility
284 $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
287 return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
290 function databaseize($nickname)
292 $nickname = str_replace('-', '_', $nickname);