]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DomainStatusNetwork/domainstatusnetworkinstaller.php
First version of installer for domain-based status networks
[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     protected $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         $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'];
101
102         $pwgen = $config['PWDGEN'];
103
104         $password = `$pwgen`;
105
106         $this->password = trim($password);
107
108         // For setting up the database
109
110         $this->rootname = $config['ADMIN'];
111         $this->rootpass = $config['ADMINPASS'];
112         $this->sitehost = $config['DBHOST'];
113         $this->sitedb   = $config['SITEDB'];
114
115         // Explicitly empty
116
117         $this->adminNick    = null;
118         $this->adminPass    = null;
119         $this->adminEmail   = null;
120         $this->adminUpdates = null;
121
122         /** Should we skip writing the configuration file? */
123         $this->skipConfig = true;
124
125         if (!$this->validateDb()) {
126             return false;
127         }
128
129         return true;
130     }
131
132     function handle()
133     {
134         return $this->doInstall();
135     }
136
137     function setupDatabase()
138     {
139         $this->createDatabase();
140         parent::setupDatabase();
141         $this->createDirectories();
142         $this->saveStatusNetwork();
143         $this->checkSchema();
144     }
145
146     function saveStatusNetwork()
147     {
148         Status_network::setupDB($this->sitehost,
149                                 $this->rootname,
150                                 $this->rootpass,
151                                 $this->sitedb, array());
152
153         $sn = new Status_network();
154
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;
160
161         $result = $sn->insert();
162
163         if (!$result) {
164             throw new ServerException("Couldn't create status_network: " . print_r($sn, true));
165         }
166
167         $sn->setTags(array('domain='.$this->domain));
168
169         $this->sn = $sn;
170
171     }
172     
173     function checkSchema()
174     {
175         $config = $this->getConfig();
176
177         Status_network::$wildcard = $config['WILDCARD'];
178
179         StatusNet::switchSite($this->nickname);
180
181         Event::handle('CheckSchema');
182     }
183
184     function getStatusNetwork()
185     {
186         return $this->sn;
187     }
188
189     function createDirectories()
190     {
191         $config = $this->getConfig();
192
193         foreach (array('AVATARBASE', 'BACKGROUNDBASE', 'FILEBASE') as $key) {
194             $base = $config[$key];
195             mkdir($base.'/'.$this->nickname, 0777, true);
196         }
197     }
198
199     function createDatabase()
200     {
201         // Create the New DB
202         $res = mysql_connect($this->host, $this->rootname, $this->rootpass);
203         if (!$res) {
204             throw new ServerException("Can't connect to {$this->host} as {$this->rootname}");
205         }
206
207         mysql_query("CREATE DATABASE ". mysql_real_escape_string($this->database), $res);
208
209         $return = mysql_select_db($this->database, $res);
210
211         if (!$return) {
212             throw new ServerException("Unable to connect to {$this->database} on {$this->host}");
213         }
214
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);
220         }
221
222         mysql_close($res);
223     }
224
225     function getConfig()
226     {
227         static $config;
228         
229         $cfg_file = "/etc/statusnet/setup.cfg";
230
231         if (empty($config)) {
232             $result = parse_ini_file($cfg_file);
233
234             $config = array();
235             foreach ($result as $key => $value) {
236                 $key = str_replace('export ', '', $key);
237                 $config[$key] = $value;
238             }
239         }
240
241         return $config;
242     }
243
244     function showHelp()
245     {
246     }
247
248     function warning($message, $submessage='')
249     {
250         print $this->html2text($message) . "\n";
251         if ($submessage != '') {
252             print "  " . $this->html2text($submessage) . "\n";
253         }
254         print "\n";
255     }
256
257     function updateStatus($status, $error=false)
258     {
259         if ($this->verbose || $error) {
260             if ($error) {
261                 print "ERROR: ";
262             }
263             print $this->html2text($status);
264             print "\n";
265         }
266     }
267
268     private function html2text($html)
269     {
270         // break out any links for text legibility
271         $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
272                                  '\2 &lt;\1&gt;',
273                                  $html);
274         return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8');
275     }
276 }