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/
48 class DomainStatusNetworkInstaller extends Installer
50 protected $domain = null;
51 protected $rootname = null;
52 protected $sitedb = null;
53 protected $rootpass = null;
54 protected $nickname = null;
57 public $verbose = false;
59 function __construct($domain)
61 $this->domain = $domain;
66 * @return boolean success
70 // We don't check prereqs. Check 'em before setting up a
71 // multi-home system, kthxbi
72 if ($this->prepare()) {
73 return $this->handle();
81 * Get our input parameters...
82 * @return boolean success
86 $config = $this->getConfig();
88 $this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
90 // XXX make this configurable
92 $this->sitename = sprintf('The %s Status Network', $this->domain);
94 $this->server = $this->nickname.'.'.$config['WILDCARD'];
98 $datanick = $this->databaseize($this->nickname);
100 $this->host = $config['DBHOSTNAME'];
101 $this->database = $datanick.$config['DBBASE'];
102 $this->dbtype = 'mysql'; // XXX: support others... someday
103 $this->username = $datanick.$config['USERBASE'];
105 // Max size for MySQL
107 if (strlen($this->username) > 16) {
108 $this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
111 $pwgen = $config['PWDGEN'];
113 $password = `$pwgen`;
115 $this->password = trim($password);
117 // For setting up the database
119 $this->rootname = $config['ADMIN'];
120 $this->rootpass = $config['ADMINPASS'];
121 $this->sitehost = $config['DBHOST'];
122 $this->sitedb = $config['SITEDB'];
126 $this->adminNick = null;
127 $this->adminPass = null;
128 $this->adminEmail = null;
129 $this->adminUpdates = null;
131 /** Should we skip writing the configuration file? */
132 $this->skipConfig = true;
134 if (!$this->validateDb()) {
143 return $this->doInstall();
146 function setupDatabase()
148 $this->updateStatus('Creating database...');
149 $this->createDatabase();
150 parent::setupDatabase();
151 $this->updateStatus('Creating file directories...');
152 $this->createDirectories();
153 $this->updateStatus('Saving status network...');
154 $this->saveStatusNetwork();
155 $this->updateStatus('Checking schema for plugins...');
156 $this->checkSchema();
159 function saveStatusNetwork()
161 Status_network::setupDB($this->sitehost,
164 $this->sitedb, array());
166 $sn = new Status_network();
168 $sn->nickname = $this->nickname;
169 $sn->dbhost = $this->host;
170 $sn->dbuser = $this->username;
171 $sn->dbpass = $this->password;
172 $sn->dbname = $this->database;
173 $sn->sitename = $this->sitename;
175 $result = $sn->insert();
178 throw new ServerException("Couldn't create status_network: " . print_r($sn, true));
181 $sn->setTags(array('domain='.$this->domain));
187 function checkSchema()
189 $config = $this->getConfig();
191 Status_network::$wildcard = $config['WILDCARD'];
193 StatusNet::switchSite($this->nickname);
195 Event::handle('CheckSchema');
198 function getStatusNetwork()
203 function createDirectories()
205 $config = $this->getConfig();
207 foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
208 $base = $config[$key];
209 mkdir($base.'/'.$this->nickname, 0777, true);
213 function createDatabase()
216 $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
218 throw new ServerException("Can't connect to {$this->host} as {$this->rootname}");
221 mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
223 $return = mysql_select_db($this->database, $res);
226 throw new ServerException("Unable to connect to {$this->database} on {$this->host}");
229 foreach (array('localhost', '%') as $src) {
230 mysql_query("GRANT ALL ON " .
231 mysql_real_escape_string($this->database).".* TO '" .
232 $this->username . "'@'".$src."' ".
233 "IDENTIFIED BY '".$this->password."'", $res);
243 $cfg_file = "/etc/statusnet/setup.cfg";
245 if (empty($config)) {
246 $result = parse_ini_file($cfg_file);
249 foreach ($result as $key => $value) {
250 $key = str_replace('export ', '', $key);
251 $config[$key] = $value;
262 function warning($message, $submessage='')
264 print $this->html2text($message) . "\n";
265 if ($submessage != '') {
266 print " " . $this->html2text($submessage) . "\n";
271 function updateStatus($status, $error=false)
273 if ($this->verbose || $error) {
277 print $this->html2text($status);
282 private function html2text($html)
284 // break out any links for text legibility
285 $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
288 return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
291 function databaseize($nickname)
293 $nickname = str_replace('-', '_', $nickname);