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