]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / install.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 define('INSTALLDIR', dirname(__FILE__));
21
22 function main()
23 {
24     if (!checkPrereqs())
25     {
26         return;
27     }
28
29     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
30         handlePost();
31     } else {
32         showForm();
33     }
34 }
35
36 function checkPrereqs()
37 {
38         $pass = true;
39
40     if (file_exists(INSTALLDIR.'/config.php')) {
41          ?><p class="error">Config file &quot;config.php&quot; already exists.</p>
42          <?php
43         $pass = false;
44     }
45
46     if (version_compare(PHP_VERSION, '5.2.3', '<')) {
47             ?><p class="error">Require PHP version 5.2.3 or greater.</p><?php
48                     $pass = false;
49     }
50
51     $reqs = array('gd', 'curl',
52                   'xmlwriter', 'mbstring');
53
54     foreach ($reqs as $req) {
55         if (!checkExtension($req)) {
56             ?><p class="error">Cannot load required extension: <code><?php echo $req; ?></code></p><?php
57                     $pass = false;
58         }
59     }
60     if (!checkExtension('pgsql') && !checkExtension('mysql')) {
61       ?><p class="error">Cannot find mysql or pgsql extension. You need one or the other: <code><?php echo $req; ?></code></p><?php
62                     $pass = false;
63     }
64
65         if (!is_writable(INSTALLDIR)) {
66          ?><p class="error">Cannot write config file to: <code><?php echo INSTALLDIR; ?></code></p>
67                <p>On your server, try this command: <code>chmod a+w <?php echo INSTALLDIR; ?></code>
68          <?php
69              $pass = false;
70         }
71
72         // Check the subdirs used for file uploads
73         $fileSubdirs = array('avatar', 'background', 'file');
74         foreach ($fileSubdirs as $fileSubdir) {
75                 $fileFullPath = INSTALLDIR."/$fileSubdir/";
76                 if (!is_writable($fileFullPath)) {
77              ?><p class="error">Cannot write <?php echo $fileSubdir; ?> directory: <code><?php echo $fileFullPath; ?></code></p>
78                        <p>On your server, try this command: <code>chmod a+w <?php echo $fileFullPath; ?></code></p>
79              <?php
80                      $pass = false;
81                 }
82         }
83
84         return $pass;
85 }
86
87 function checkExtension($name)
88 {
89     if (!extension_loaded($name)) {
90         if (!@dl($name.'.so')) {
91             return false;
92         }
93     }
94     return true;
95 }
96
97 function showForm()
98 {
99     echo<<<E_O_T
100         </ul>
101     </dd>
102 </dl>
103 <dl id="page_notice" class="system_notice">
104     <dt>Page notice</dt>
105     <dd>
106         <div class="instructions">
107             <p>Enter your database connection information below to initialize the database.</p>
108         </div>
109     </dd>
110 </dl>
111 <form method="post" action="install.php" class="form_settings" id="form_install">
112     <fieldset>
113         <legend>Connection settings</legend>
114         <ul class="form_data">
115             <li>
116                 <label for="sitename">Site name</label>
117                 <input type="text" id="sitename" name="sitename" />
118                 <p class="form_guide">The name of your site</p>
119             </li>
120             <li>
121                 <label for="fancy-enable">Fancy URLs</label>
122                 <input type="radio" name="fancy" id="fancy-enable" value="enable" checked='checked' /> enable<br />
123                 <input type="radio" name="fancy" id="fancy-disable" value="" /> disable<br />
124                 <p class="form_guide" id='fancy-form_guide'>Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.</p>
125             </li>
126             <li>
127                 <label for="host">Hostname</label>
128                 <input type="text" id="host" name="host" />
129                 <p class="form_guide">Database hostname</p>
130             </li>
131             <li>
132
133                 <label for="dbtype">Type</label>
134                 <input type="radio" name="dbtype" id="fancy-mysql" value="mysql" checked='checked' /> MySQL<br />
135                 <input type="radio" name="dbtype" id="dbtype-pgsql" value="pgsql" /> PostgreSQL<br />
136                 <p class="form_guide">Database type</p>
137             </li>
138
139             <li>
140                 <label for="database">Name</label>
141                 <input type="text" id="database" name="database" />
142                 <p class="form_guide">Database name</p>
143             </li>
144             <li>
145                 <label for="username">Username</label>
146                 <input type="text" id="username" name="username" />
147                 <p class="form_guide">Database username</p>
148             </li>
149             <li>
150                 <label for="password">Password</label>
151                 <input type="password" id="password" name="password" />
152                 <p class="form_guide">Database password (optional)</p>
153             </li>
154         </ul>
155         <input type="submit" name="submit" class="submit" value="Submit" />
156     </fieldset>
157 </form>
158
159 E_O_T;
160 }
161
162 function updateStatus($status, $error=false)
163 {
164 ?>
165                 <li <?php echo ($error) ? 'class="error"': ''; ?>><?php echo $status;?></li>
166
167 <?php
168 }
169
170 function handlePost()
171 {
172 ?>
173
174 <?php
175     $host     = $_POST['host'];
176     $dbtype   = $_POST['dbtype'];
177     $database = $_POST['database'];
178     $username = $_POST['username'];
179     $password = $_POST['password'];
180     $sitename = $_POST['sitename'];
181     $fancy    = !empty($_POST['fancy']);
182     $server = $_SERVER['HTTP_HOST'];
183     $path = substr(dirname($_SERVER['PHP_SELF']), 1);
184
185 ?>
186     <dl class="system_notice">
187         <dt>Page notice</dt>
188         <dd>
189             <ul>
190 <?php
191         $fail = false;
192
193     if (empty($host)) {
194         updateStatus("No hostname specified.", true);
195                 $fail = true;
196     }
197
198     if (empty($database)) {
199         updateStatus("No database specified.", true);
200                 $fail = true;
201     }
202
203     if (empty($username)) {
204         updateStatus("No username specified.", true);
205                 $fail = true;
206     }
207
208 //     if (empty($password)) {
209 //         updateStatus("No password specified.", true);
210 //              $fail = true;
211 //     }
212
213     if (empty($sitename)) {
214         updateStatus("No sitename specified.", true);
215                 $fail = true;
216     }
217
218     if($fail){
219             showForm();
220         return;
221     }
222
223     // FIXME: use PEAR::DB or PDO instead of our own switch
224
225     switch($dbtype) {
226         case 'mysql':
227             $db = mysql_db_installer($host, $database, $username, $password);
228             break;
229         case 'pgsql':
230             $db = pgsql_db_installer($host, $database, $username, $password);
231             break;
232         default:
233     }
234
235     if (!$db) {
236         // database connection failed, do not move on to create config file.
237         return false;
238     }
239
240     updateStatus("Writing config file...");
241     $res = writeConf($sitename, $server, $path, $fancy, $db);
242
243     if (!$res) {
244         updateStatus("Can't write config file.", true);
245         showForm();
246         return;
247     }
248
249     /*
250         TODO https needs to be considered
251     */
252     $link = "http://".$server.'/'.$path;
253
254     updateStatus("StatusNet has been installed at $link");
255     updateStatus("You can visit your <a href='$link'>new StatusNet site</a>.");
256 ?>
257
258 <?php
259 }
260
261 function pgsql_db_installer($host, $database, $username, $password) {
262   $connstring = "dbname=$database host=$host user=$username";
263
264   //No password would mean trust authentication used.
265   if (!empty($password)) {
266     $connstring .= " password=$password";
267   }
268   updateStatus("Starting installation...");
269   updateStatus("Checking database...");
270   $conn = pg_connect($connstring);
271
272   if ($conn ===false) {
273     updateStatus("Failed to connect to database: $connstring");
274     showForm();
275     return false;
276   }
277
278   //ensure database encoding is UTF8
279   $record = pg_fetch_object(pg_query($conn, 'SHOW server_encoding'));
280   if ($record->server_encoding != 'UTF8') {
281     updateStatus("StatusNet requires UTF8 character encoding. Your database is ". htmlentities($record->server_encoding));
282     showForm();
283     return false;
284   }
285
286   updateStatus("Running database script...");
287   //wrap in transaction;
288   pg_query($conn, 'BEGIN');
289   $res = runDbScript(INSTALLDIR.'/db/statusnet_pg.sql', $conn, 'pgsql');
290
291   if ($res === false) {
292       updateStatus("Can't run database script.", true);
293       showForm();
294       return false;
295   }
296   foreach (array('sms_carrier' => 'SMS carrier',
297                 'notice_source' => 'notice source',
298                 'foreign_services' => 'foreign service')
299           as $scr => $name) {
300       updateStatus(sprintf("Adding %s data to database...", $name));
301       $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn, 'pgsql');
302       if ($res === false) {
303           updateStatus(sprintf("Can't run %d script.", $name), true);
304           showForm();
305           return false;
306       }
307   }
308   pg_query($conn, 'COMMIT');
309
310   if (empty($password)) {
311     $sqlUrl = "pgsql://$username@$host/$database";
312   }
313   else {
314     $sqlUrl = "pgsql://$username:$password@$host/$database";
315   }
316
317   $db = array('type' => 'pgsql', 'database' => $sqlUrl);
318
319   return $db;
320 }
321
322 function mysql_db_installer($host, $database, $username, $password) {
323   updateStatus("Starting installation...");
324   updateStatus("Checking database...");
325
326   $conn = mysql_connect($host, $username, $password);
327   if (!$conn) {
328       updateStatus("Can't connect to server '$host' as '$username'.", true);
329       showForm();
330       return false;
331   }
332   updateStatus("Changing to database...");
333   $res = mysql_select_db($database, $conn);
334   if (!$res) {
335       updateStatus("Can't change to database.", true);
336       showForm();
337       return false;
338   }
339   updateStatus("Running database script...");
340   $res = runDbScript(INSTALLDIR.'/db/statusnet.sql', $conn);
341   if ($res === false) {
342       updateStatus("Can't run database script.", true);
343       showForm();
344       return false;
345   }
346   foreach (array('sms_carrier' => 'SMS carrier',
347                 'notice_source' => 'notice source',
348                 'foreign_services' => 'foreign service')
349           as $scr => $name) {
350       updateStatus(sprintf("Adding %s data to database...", $name));
351       $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
352       if ($res === false) {
353           updateStatus(sprintf("Can't run %d script.", $name), true);
354           showForm();
355           return false;
356       }
357   }
358
359       $sqlUrl = "mysqli://$username:$password@$host/$database";
360       $db = array('type' => 'mysql', 'database' => $sqlUrl);
361       return $db;
362 }
363
364 function writeConf($sitename, $server, $path, $fancy, $db)
365 {
366     // assemble configuration file in a string
367     $cfg =  "<?php\n".
368             "if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }\n\n".
369
370             // site name
371             "\$config['site']['name'] = '$sitename';\n\n".
372
373             // site location
374             "\$config['site']['server'] = '$server';\n".
375             "\$config['site']['path'] = '$path'; \n\n".
376
377             // checks if fancy URLs are enabled
378             ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
379
380             // database
381             "\$config['db']['database'] = '{$db['database']}';\n\n".
382             ($type == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n":'').
383             "\$config['db']['type'] = '{$db['type']}';\n\n".
384
385             "?>";
386     // write configuration file out to install directory
387     $res = file_put_contents(INSTALLDIR.'/config.php', $cfg);
388
389     return $res;
390 }
391
392 function runDbScript($filename, $conn, $type = 'mysql')
393 {
394     $sql = trim(file_get_contents($filename));
395     $stmts = explode(';', $sql);
396     foreach ($stmts as $stmt) {
397         $stmt = trim($stmt);
398         if (!mb_strlen($stmt)) {
399             continue;
400         }
401         // FIXME: use PEAR::DB or PDO instead of our own switch
402         switch ($type) {
403         case 'mysql':
404             $res = mysql_query($stmt, $conn);
405             if ($res === false) {
406                 $error = mysql_error();
407             }
408             break;
409         case 'pgsql':
410             $res = pg_query($conn, $stmt);
411             if ($res === false) {
412                 $error = pg_last_error();
413             }
414             break;
415         default:
416             updateStatus("runDbScript() error: unknown database type ". $type ." provided.");
417         }
418         if ($res === false) {
419             updateStatus("ERROR ($error) for SQL '$stmt'");
420             return $res;
421         }
422     }
423     return true;
424 }
425
426 ?>
427 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
428 <!DOCTYPE html
429 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
430        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
431 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
432     <head>
433         <title>Install StatusNet</title>
434         <link rel="shortcut icon" href="favicon.ico"/>
435         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
436         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
437         <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
438         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/default/css/ie.css?version=0.8" /><![endif]-->
439         <script src="js/jquery.min.js"></script>
440         <script src="js/install.js"></script>
441     </head>
442     <body id="install">
443         <div id="wrap">
444             <div id="header">
445                 <address id="site_contact" class="vcard">
446                     <a class="url home bookmark" href=".">
447                         <img class="logo photo" src="theme/default/logo.png" alt="StatusNet"/>
448                         <span class="fn org">StatusNet</span>
449                     </a>
450                 </address>
451             </div>
452             <div id="core">
453                 <div id="content">
454                     <h1>Install StatusNet</h1>
455 <?php main(); ?>
456                 </div>
457             </div>
458         </div>
459     </body>
460 </html>