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['SITEDBHOSTNAME'];
121 $this->sitedb = $config['SITEDB'];
123 $tagstr = $config['TAGS'];
125 if (!empty($tagstr)) {
126 $this->tags = preg_split('/[\s,]+/', $tagstr);
128 $this->tags = array();
133 $this->adminNick = null;
134 $this->adminPass = null;
135 $this->adminEmail = null;
137 /** Should we skip writing the configuration file? */
138 $this->skipConfig = true;
140 if (!$this->validateDb()) {
149 return $this->doInstall();
152 function setupDatabase()
154 $this->updateStatus('Creating database...');
155 $this->createDatabase();
156 parent::setupDatabase();
157 $this->updateStatus('Creating file directories...');
158 $this->createDirectories();
159 $this->updateStatus('Saving status network...');
160 $this->saveStatusNetwork();
161 $this->updateStatus('Checking schema for plugins...');
162 $this->checkSchema();
165 function saveStatusNetwork()
167 Status_network::setupDB($this->sitehost,
170 $this->sitedb, array());
172 $sn = new Status_network();
174 $sn->nickname = $this->nickname;
175 $sn->dbhost = $this->host;
176 $sn->dbuser = $this->username;
177 $sn->dbpass = $this->password;
178 $sn->dbname = $this->database;
179 $sn->sitename = $this->sitename;
180 $sn->created = common_sql_now();
182 $result = $sn->insert();
185 throw new ServerException("Could not create status_network: " . print_r($sn, true));
188 // Re-fetch; stupid auto-increment integer isn't working
190 $sn = Status_network::getKV('nickname', $sn->nickname);
193 throw new ServerException("Created {$this->nickname} status_network and could not find it again.");
202 $tags[] = 'domain='.$this->domain;
209 function checkSchema()
211 $config = $this->getConfig();
213 Status_network::$wildcard = $config['WILDCARD'];
215 GNUsocial::switchSite($this->nickname);
217 // We need to initialize the schema_version stuff to make later setup easier
220 require INSTALLDIR.'/db/core.php';
221 $tableDefs = $schema;
223 $schema = Schema::get();
224 $schemaUpdater = new SchemaUpdater($schema);
226 foreach ($tableDefs as $table => $def) {
227 $schemaUpdater->register($table, $def);
230 $schemaUpdater->checkSchema();
232 Event::handle('CheckSchema');
235 function getStatusNetwork()
240 function createDirectories()
242 $config = $this->getConfig();
244 foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
245 $base = $config[$key];
246 $dirname = $base.'/'.$this->nickname;
248 // Make sure our bits are set
250 mkdir($dirname, 0770, true);
253 // If you set the setuid bit on your base dirs this should be
254 // unnecessary, but just in case. You must be root for this
257 if (array_key_exists('WEBUSER', $config)) {
258 chown($dirname, $config['WEBUSER']);
260 if (array_key_exists('WEBGROUP', $config)) {
261 chgrp($dirname, $config['WEBGROUP']);
266 function createDatabase()
270 * Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli instead.
272 $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
274 throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
277 mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
279 $return = mysql_select_db($this->database, $res);
282 throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
285 foreach (array('localhost', '%') as $src) {
286 mysql_query("GRANT ALL ON " .
287 mysql_real_escape_string($this->database).".* TO '" .
288 $this->username . "'@'".$src."' ".
289 "IDENTIFIED BY '".$this->password."'", $res);
299 $cfg_file = "/etc/statusnet/setup.cfg";
301 if (empty($config)) {
302 $result = parse_ini_file($cfg_file);
305 foreach ($result as $key => $value) {
306 $key = str_replace('export ', '', $key);
307 $config[$key] = $value;
318 function warning($message, $submessage='')
320 print $this->html2text($message) . "\n";
321 if ($submessage != '') {
322 print " " . $this->html2text($submessage) . "\n";
327 function updateStatus($status, $error=false)
329 if ($this->verbose || $error) {
333 print $this->html2text($status);
338 private function html2text($html)
340 // break out any links for text legibility
341 $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
344 return common_strip_html($breakout);
347 function databaseize($nickname)
349 $nickname = str_replace('-', '_', $nickname);