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;
56 protected $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 $this->host = $config['DBHOSTNAME'];
98 $this->database = $this->nickname.$config['DBBASE'];
99 $this->dbtype = 'mysql'; // XXX: support others... someday
100 $this->username = $this->nickname.$config['USERBASE'];
102 $pwgen = $config['PWDGEN'];
104 $password = `$pwgen`;
106 $this->password = trim($password);
108 // For setting up the database
110 $this->rootname = $config['ADMIN'];
111 $this->rootpass = $config['ADMINPASS'];
112 $this->sitehost = $config['DBHOST'];
113 $this->sitedb = $config['SITEDB'];
117 $this->adminNick = null;
118 $this->adminPass = null;
119 $this->adminEmail = null;
120 $this->adminUpdates = null;
122 /** Should we skip writing the configuration file? */
123 $this->skipConfig = true;
125 if (!$this->validateDb()) {
134 return $this->doInstall();
137 function setupDatabase()
139 $this->createDatabase();
140 parent::setupDatabase();
141 $this->createDirectories();
142 $this->saveStatusNetwork();
143 $this->checkSchema();
146 function saveStatusNetwork()
148 Status_network::setupDB($this->sitehost,
151 $this->sitedb, array());
153 $sn = new Status_network();
155 $sn->nickname = $this->nickname;
156 $sn->dbhost = $this->host;
157 $sn->dbuser = $this->username;
158 $sn->dbpass = $this->password;
159 $sn->dbname = $this->database;
161 $result = $sn->insert();
164 throw new ServerException("Couldn't create status_network: " . print_r($sn, true));
167 $sn->setTags(array('domain='.$this->domain));
173 function checkSchema()
175 $config = $this->getConfig();
177 Status_network::$wildcard = $config['WILDCARD'];
179 StatusNet::switchSite($this->nickname);
181 Event::handle('CheckSchema');
184 function getStatusNetwork()
189 function createDirectories()
191 $config = $this->getConfig();
193 foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
194 $base = $config[$key];
195 mkdir($base.'/'.$this->nickname, 0777, true);
199 function createDatabase()
202 $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
204 throw new ServerException("Can't connect to {$this->host} as {$this->rootname}");
207 mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
209 $return = mysql_select_db($this->database, $res);
212 throw new ServerException("Unable to connect to {$this->database} on {$this->host}");
215 foreach (array('localhost', '%') as $src) {
216 mysql_query("GRANT ALL ON " .
217 mysql_real_escape_string($this->database).".* TO '" .
218 $this->username . "'@'".$src."' ".
219 "IDENTIFIED BY '".$this->password."'", $res);
229 $cfg_file = "/etc/statusnet/setup.cfg";
231 if (empty($config)) {
232 $result = parse_ini_file($cfg_file);
235 foreach ($result as $key => $value) {
236 $key = str_replace('export ', '', $key);
237 $config[$key] = $value;
248 function warning($message, $submessage='')
250 print $this->html2text($message) . "\n";
251 if ($submessage != '') {
252 print " " . $this->html2text($submessage) . "\n";
257 function updateStatus($status, $error=false)
259 if ($this->verbose || $error) {
263 print $this->html2text($status);
268 private function html2text($html)
270 // break out any links for text legibility
271 $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
274 return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');