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;
136 $this->adminUpdates = null;
138 /** Should we skip writing the configuration file? */
139 $this->skipConfig = true;
141 if (!$this->validateDb()) {
150 return $this->doInstall();
153 function setupDatabase()
155 $this->updateStatus('Creating database...');
156 $this->createDatabase();
157 parent::setupDatabase();
158 $this->updateStatus('Creating file directories...');
159 $this->createDirectories();
160 $this->updateStatus('Saving status network...');
161 $this->saveStatusNetwork();
162 $this->updateStatus('Checking schema for plugins...');
163 $this->checkSchema();
166 function saveStatusNetwork()
168 Status_network::setupDB($this->sitehost,
171 $this->sitedb, array());
173 $sn = new Status_network();
175 $sn->nickname = $this->nickname;
176 $sn->dbhost = $this->host;
177 $sn->dbuser = $this->username;
178 $sn->dbpass = $this->password;
179 $sn->dbname = $this->database;
180 $sn->sitename = $this->sitename;
181 $sn->created = common_sql_now();
183 $result = $sn->insert();
186 throw new ServerException("Could not create status_network: " . print_r($sn, true));
189 // Re-fetch; stupid auto-increment integer isn't working
191 $sn = Status_network::getKV('nickname', $sn->nickname);
194 throw new ServerException("Created {$this->nickname} status_network and could not find it again.");
203 $tags[] = 'domain='.$this->domain;
210 function checkSchema()
212 $config = $this->getConfig();
214 Status_network::$wildcard = $config['WILDCARD'];
216 StatusNet::switchSite($this->nickname);
218 // We need to initialize the schema_version stuff to make later setup easier
221 require INSTALLDIR.'/db/core.php';
222 $tableDefs = $schema;
224 $schema = Schema::get();
225 $schemaUpdater = new SchemaUpdater($schema);
227 foreach ($tableDefs as $table => $def) {
228 $schemaUpdater->register($table, $def);
231 $schemaUpdater->checkSchema();
233 Event::handle('CheckSchema');
236 function getStatusNetwork()
241 function createDirectories()
243 $config = $this->getConfig();
245 foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
246 $base = $config[$key];
247 $dirname = $base.'/'.$this->nickname;
249 // Make sure our bits are set
251 mkdir($dirname, 0770, true);
254 // If you set the setuid bit on your base dirs this should be
255 // unnecessary, but just in case. You must be root for this
258 if (array_key_exists('WEBUSER', $config)) {
259 chown($dirname, $config['WEBUSER']);
261 if (array_key_exists('WEBGROUP', $config)) {
262 chgrp($dirname, $config['WEBGROUP']);
267 function createDatabase()
271 * Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli instead.
273 $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
275 throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
278 mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
280 $return = mysql_select_db($this->database, $res);
283 throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
286 foreach (array('localhost', '%') as $src) {
287 mysql_query("GRANT ALL ON " .
288 mysql_real_escape_string($this->database).".* TO '" .
289 $this->username . "'@'".$src."' ".
290 "IDENTIFIED BY '".$this->password."'", $res);
300 $cfg_file = "/etc/statusnet/setup.cfg";
302 if (empty($config)) {
303 $result = parse_ini_file($cfg_file);
306 foreach ($result as $key => $value) {
307 $key = str_replace('export ', '', $key);
308 $config[$key] = $value;
319 function warning($message, $submessage='')
321 print $this->html2text($message) . "\n";
322 if ($submessage != '') {
323 print " " . $this->html2text($submessage) . "\n";
328 function updateStatus($status, $error=false)
330 if ($this->verbose || $error) {
334 print $this->html2text($status);
339 private function html2text($html)
341 // break out any links for text legibility
342 $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
345 return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
348 function databaseize($nickname)
350 $nickname = str_replace('-', '_', $nickname);