]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/domainstatusnetworkinstaller.php
more verbose, better checks for installforemail
[quix0rs-gnu-social.git] / plugins / DomainStatusNetwork / domainstatusnetworkinstaller.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Installer class for domain-based multi-homing systems
7  * 
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  DomainStatusNetwork
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Installer class for domain-based multi-homing systems
39  *
40  * @category  DomainStatusNetwork
41  * @package   StatusNet
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/
46  */
47
48 class DomainStatusNetworkInstaller extends Installer
49 {
50     protected $domain   = null;
51     protected $rootname = null;
52     protected $sitedb   = null;
53     protected $rootpass = null;
54     protected $nickname = null;
55     protected $sn       = null;
56
57     public $verbose     = false;
58
59     function __construct($domain)
60     {
61         $this->domain = $domain;
62     }
63
64     /**
65      * Go for it!
66      * @return boolean success
67      */
68     function main()
69     {
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();
74         } else {
75             $this->showHelp();
76             return false;
77         }
78     }
79
80     /**
81      * Get our input parameters...
82      * @return boolean success
83      */
84     function prepare()
85     {
86         $config = $this->getConfig();
87
88         $this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
89
90         // XXX make this configurable
91
92         $this->sitename = sprintf('The %s Status Network', $this->domain);
93
94         $this->server   = $this->nickname.'.'.$config['WILDCARD'];
95         $this->path     = null;
96         $this->fancy    = true;
97
98         $datanick = $this->databaseize($this->nickname);
99
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'];
104
105         // Max size for MySQL
106
107         if (strlen($this->username) > 16) {
108             $this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
109         }
110
111         $pwgen = $config['PWDGEN'];
112
113         $password = `$pwgen`;
114
115         $this->password = trim($password);
116
117         // For setting up the database
118
119         $this->rootname = $config['ADMIN'];
120         $this->rootpass = $config['ADMINPASS'];
121         $this->sitehost = $config['DBHOST'];
122         $this->sitedb   = $config['SITEDB'];
123
124         // Explicitly empty
125
126         $this->adminNick    = null;
127         $this->adminPass    = null;
128         $this->adminEmail   = null;
129         $this->adminUpdates = null;
130
131         /** Should we skip writing the configuration file? */
132         $this->skipConfig = true;
133
134         if (!$this->validateDb()) {
135             return false;
136         }
137
138         return true;
139     }
140
141     function handle()
142     {
143         return $this->doInstall();
144     }
145
146     function setupDatabase()
147     {
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();
157     }
158
159     function saveStatusNetwork()
160     {
161         Status_network::setupDB($this->sitehost,
162                                 $this->rootname,
163                                 $this->rootpass,
164                                 $this->sitedb, array());
165
166         $sn = new Status_network();
167
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;
174
175         $result = $sn->insert();
176
177         if (!$result) {
178             throw new ServerException("Couldn't create status_network: " . print_r($sn, true));
179         }
180
181         $sn->setTags(array('domain='.$this->domain));
182
183         $this->sn = $sn;
184
185     }
186     
187     function checkSchema()
188     {
189         $config = $this->getConfig();
190
191         Status_network::$wildcard = $config['WILDCARD'];
192
193         StatusNet::switchSite($this->nickname);
194
195         Event::handle('CheckSchema');
196     }
197
198     function getStatusNetwork()
199     {
200         return $this->sn;
201     }
202
203     function createDirectories()
204     {
205         $config = $this->getConfig();
206
207         foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
208             $base = $config[$key];
209             mkdir($base.'/'.$this->nickname, 0777, true);
210         }
211     }
212
213     function createDatabase()
214     {
215         // Create the New DB
216         $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
217         if (!$res) {
218             throw new ServerException("Can't connect to {$this->host} as {$this->rootname}");
219         }
220
221         mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
222
223         $return = mysql_select_db($this->database, $res);
224
225         if (!$return) {
226             throw new ServerException("Unable to connect to {$this->database} on {$this->host}");
227         }
228
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);
234         }
235
236         mysql_close($res);
237     }
238
239     function getConfig()
240     {
241         static $config;
242         
243         $cfg_file = "/etc/statusnet/setup.cfg";
244
245         if (empty($config)) {
246             $result = parse_ini_file($cfg_file);
247
248             $config = array();
249             foreach ($result as $key => $value) {
250                 $key = str_replace('export ', '', $key);
251                 $config[$key] = $value;
252             }
253         }
254
255         return $config;
256     }
257
258     function showHelp()
259     {
260     }
261
262     function warning($message, $submessage='')
263     {
264         print $this->html2text($message) . "\n";
265         if ($submessage != '') {
266             print "  " . $this->html2text($submessage) . "\n";
267         }
268         print "\n";
269     }
270
271     function updateStatus($status, $error=false)
272     {
273         if ($this->verbose || $error) {
274             if ($error) {
275                 print "ERROR: ";
276             }
277             print $this->html2text($status);
278             print "\n";
279         }
280     }
281
282     private function html2text($html)
283     {
284         // break out any links for text legibility
285         $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
286                                  '\2 &lt;\1&gt;',
287                                  $html);
288         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
289     }
290
291     function databaseize($nickname)
292     {
293         $nickname = str_replace('-', '_', $nickname);
294         return $nickname;
295     }
296 }