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