]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/lib/domainstatusnetworkinstaller.php
Make errors work correctly
[quix0rs-gnu-social.git] / plugins / DomainStatusNetwork / lib / 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 class DomainStatusNetworkInstaller extends Installer
48 {
49     protected $domain   = null;
50     protected $rootname = null;
51     protected $sitedb   = null;
52     protected $rootpass = null;
53     protected $nickname = null;
54     protected $sn       = null;
55
56     public $verbose     = false;
57
58     function __construct($domain)
59     {
60         $this->domain = $domain;
61     }
62
63     /**
64      * Go for it!
65      * @return boolean success
66      */
67     function main()
68     {
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();
73         } else {
74             $this->showHelp();
75             return false;
76         }
77     }
78
79     /**
80      * Get our input parameters...
81      * @return boolean success
82      */
83     function prepare()
84     {
85         $config = $this->getConfig();
86
87         $this->nickname = DomainStatusNetworkPlugin::nicknameForDomain($this->domain);
88
89         // XXX make this configurable
90
91         $this->sitename = sprintf('The %s Status Network', $this->domain);
92
93         $this->server   = $this->nickname.'.'.$config['WILDCARD'];
94         $this->path     = null;
95         $this->fancy    = true;
96
97         $datanick = $this->databaseize($this->nickname);
98
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'];
103
104         // Max size for MySQL
105
106         if (strlen($this->username) > 16) {
107             $this->username = sprintf('%s%08x', substr($this->username, 0, 8), crc32($this->username));
108         }
109
110         $pwgen = $config['PWDGEN'];
111
112         $password = `$pwgen`;
113
114         $this->password = trim($password);
115
116         // For setting up the database
117
118         $this->rootname = $config['ADMIN'];
119         $this->rootpass = $config['ADMINPASS'];
120         $this->sitehost = $config['DBHOST'];
121         $this->sitedb   = $config['SITEDB'];
122
123         // Explicitly empty
124
125         $this->adminNick    = null;
126         $this->adminPass    = null;
127         $this->adminEmail   = null;
128         $this->adminUpdates = null;
129
130         /** Should we skip writing the configuration file? */
131         $this->skipConfig = true;
132
133         if (!$this->validateDb()) {
134             return false;
135         }
136
137         return true;
138     }
139
140     function handle()
141     {
142         return $this->doInstall();
143     }
144
145     function setupDatabase()
146     {
147         $this->updateStatus('Creating database...');
148         $this->createDatabase();
149         parent::setupDatabase();
150         $this->updateStatus('Creating file directories...');
151         $this->createDirectories();
152         $this->updateStatus('Saving status network...');
153         $this->saveStatusNetwork();
154         $this->updateStatus('Checking schema for plugins...');
155         $this->checkSchema();
156     }
157
158     function saveStatusNetwork()
159     {
160         Status_network::setupDB($this->sitehost,
161                                 $this->rootname,
162                                 $this->rootpass,
163                                 $this->sitedb, array());
164
165         $sn = new Status_network();
166
167         $sn->nickname = $this->nickname;
168         $sn->dbhost   = $this->host;
169         $sn->dbuser   = $this->username;
170         $sn->dbpass   = $this->password;
171         $sn->dbname   = $this->database;
172         $sn->sitename = $this->sitename;
173
174         $result = $sn->insert();
175
176         if (!$result) {
177             throw new ServerException("Could not create status_network: " . print_r($sn, true));
178         }
179
180         // Re-fetch; stupid auto-increment integer isn't working
181
182         $sn = Status_network::staticGet('nickname', $sn->nickname);
183
184         if (empty($sn)) {
185             throw new ServerException("Created {$this->nickname} status_network and could not find it again.");
186         }
187
188         $sn->setTags(array('domain='.$this->domain));
189
190         $this->sn = $sn;
191     }
192
193     function checkSchema()
194     {
195         $config = $this->getConfig();
196
197         Status_network::$wildcard = $config['WILDCARD'];
198
199         StatusNet::switchSite($this->nickname);
200
201         Event::handle('CheckSchema');
202     }
203
204     function getStatusNetwork()
205     {
206         return $this->sn;
207     }
208
209     function createDirectories()
210     {
211         $config = $this->getConfig();
212
213         foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
214             $base = $config[$key];
215             $dirname = $base.'/'.$this->nickname;
216
217             // Make sure our bits are set
218             $mask = umask(0);
219             mkdir($dirname, 0770, true);
220             umask($mask);
221
222             // If you set the setuid bit on your base dirs this should be
223             // unnecessary, but just in case. You must be root for this
224             // to work.
225
226             if (array_key_exists('WEBUSER', $config)) {
227                 chown($dirname, $config['WEBUSER']);
228             }
229             if (array_key_exists('WEBGROUP', $config)) {
230                 chgrp($dirname, $config['WEBGROUP']);
231             }
232         }
233     }
234
235     function createDatabase()
236     {
237         // Create the New DB
238         $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
239         if (!$res) {
240             throw new ServerException("Cannot connect to {$this->host} as {$this->rootname}.");
241         }
242
243         mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
244
245         $return = mysql_select_db($this->database, $res);
246
247         if (!$return) {
248             throw new ServerException("Unable to connect to {$this->database} on {$this->host}.");
249         }
250
251         foreach (array('localhost', '%') as $src) {
252             mysql_query("GRANT ALL ON " .
253                         mysql_real_escape_string($this->database).".* TO '" .
254                         $this->username . "'@'".$src."' ".
255                         "IDENTIFIED BY '".$this->password."'", $res);
256         }
257
258         mysql_close($res);
259     }
260
261     function getConfig()
262     {
263         static $config;
264
265         $cfg_file = "/etc/statusnet/setup.cfg";
266
267         if (empty($config)) {
268             $result = parse_ini_file($cfg_file);
269
270             $config = array();
271             foreach ($result as $key => $value) {
272                 $key = str_replace('export ', '', $key);
273                 $config[$key] = $value;
274             }
275         }
276
277         return $config;
278     }
279
280     function showHelp()
281     {
282     }
283
284     function warning($message, $submessage='')
285     {
286         print $this->html2text($message) . "\n";
287         if ($submessage != '') {
288             print "  " . $this->html2text($submessage) . "\n";
289         }
290         print "\n";
291     }
292
293     function updateStatus($status, $error=false)
294     {
295         if ($this->verbose || $error) {
296             if ($error) {
297                 print "ERROR: ";
298             }
299             print $this->html2text($status);
300             print "\n";
301         }
302     }
303
304     private function html2text($html)
305     {
306         // break out any links for text legibility
307         $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
308                                  '\2 &lt;\1&gt;',
309                                  $html);
310         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
311     }
312
313     function databaseize($nickname)
314     {
315         $nickname = str_replace('-', '_', $nickname);
316         return $nickname;
317     }
318 }