3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2009, Control Yourself, Inc.
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.
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.
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/>.
20 define('INSTALLDIR', dirname(__FILE__));
29 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36 function checkPrereqs()
40 if (file_exists(INSTALLDIR.'/config.php')) {
41 ?><p class="error">Config file "config.php" already exists.</p>
46 if (version_compare(PHP_VERSION, '5.2.3', '<')) {
47 ?><p class="error">Require PHP version 5.2.3 or greater.</p><?php
51 $reqs = array('gd', 'curl',
52 'xmlwriter', 'mbstring',
55 foreach ($reqs as $req) {
56 if (!checkExtension($req)) {
57 ?><p class="error">Cannot load required extension: <code><?php echo $req; ?></code></p><?php
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
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>
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>
88 function checkExtension($name)
90 if (!extension_loaded($name)) {
91 if (!dl($name.'.so')) {
104 <dl id="page_notice" class="system_notice">
107 <div class="instructions">
108 <p>Enter your database connection information below to initialize the database.</p>
112 <form method="post" action="install.php" class="form_settings" id="form_install">
114 <legend>Connection settings</legend>
115 <ul class="form_data">
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>
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>
128 <label for="host">Hostname</label>
129 <input type="text" id="host" name="host" />
130 <p class="form_guide">Database hostname</p>
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>
141 <label for="database">Name</label>
142 <input type="text" id="database" name="database" />
143 <p class="form_guide">Database name</p>
146 <label for="username">Username</label>
147 <input type="text" id="username" name="username" />
148 <p class="form_guide">Database username</p>
151 <label for="password">Password</label>
152 <input type="password" id="password" name="password" />
153 <p class="form_guide">Database password (optional)</p>
156 <input type="submit" name="submit" class="submit" value="Submit" />
163 function updateStatus($status, $error=false)
166 <li <?php echo ($error) ? 'class="error"': ''; ?>><?print $status;?></li>
171 function handlePost()
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']);
184 <dl class="system_notice">
192 updateStatus("No hostname specified.", true);
196 if (empty($database)) {
197 updateStatus("No database specified.", true);
201 if (empty($username)) {
202 updateStatus("No username specified.", true);
206 // if (empty($password)) {
207 // updateStatus("No password specified.", true);
211 if (empty($sitename)) {
212 updateStatus("No sitename specified.", true);
222 case 'mysql': mysql_db_installer($host, $database, $username, $password, $sitename);
224 case 'pgsql': pgsql_db_installer($host, $database, $username, $password, $sitename);
228 if ($path) $path .= '/';
229 updateStatus("You can visit your <a href='/$path'>new Laconica site</a>.");
235 function pgsql_db_installer($host, $database, $username, $password, $sitename) {
236 $connstring = "dbname=$database host=$host user=$username";
238 //No password would mean trust authentication used.
239 if (!empty($password)) {
240 $connstring .= " password=$password";
242 updateStatus("Starting installation...");
243 updateStatus("Checking database...");
244 $conn = pg_connect($connstring);
246 updateStatus("Running database script...");
247 //wrap in transaction;
248 pg_query($conn, 'BEGIN');
249 $res = runDbScript(INSTALLDIR.'/db/laconica_pg.sql', $conn, 'pgsql');
251 if ($res === false) {
252 updateStatus("Can't run database script.", true);
256 foreach (array('sms_carrier' => 'SMS carrier',
257 'notice_source' => 'notice source',
258 'foreign_services' => 'foreign service')
260 updateStatus(sprintf("Adding %s data to database...", $name));
261 $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn, 'pgsql');
262 if ($res === false) {
263 updateStatus(sprintf("Can't run %d script.", $name), true);
268 pg_query($conn, 'COMMIT');
270 updateStatus("Writing config file...");
271 if (empty($password)) {
272 $sqlUrl = "pgsql://$username@$host/$database";
275 $sqlUrl = "pgsql://$username:$password@$host/$database";
277 $res = writeConf($sitename, $sqlUrl, $fancy, 'pgsql');
279 updateStatus("Can't write config file.", true);
283 updateStatus("Done!");
287 function mysql_db_installer($host, $database, $username, $password, $sitename) {
288 updateStatus("Starting installation...");
289 updateStatus("Checking database...");
291 $conn = mysql_connect($host, $username, $password);
293 updateStatus("Can't connect to server '$host' as '$username'.", true);
297 updateStatus("Changing to database...");
298 $res = mysql_select_db($database, $conn);
300 updateStatus("Can't change to database.", true);
304 updateStatus("Running database script...");
305 $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
306 if ($res === false) {
307 updateStatus("Can't run database script.", true);
311 foreach (array('sms_carrier' => 'SMS carrier',
312 'notice_source' => 'notice source',
313 'foreign_services' => 'foreign service')
315 updateStatus(sprintf("Adding %s data to database...", $name));
316 $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
317 if ($res === false) {
318 updateStatus(sprintf("Can't run %d script.", $name), true);
324 updateStatus("Writing config file...");
325 $sqlUrl = "mysqli://$username:$password@$host/$database";
326 $res = writeConf($sitename, $sqlUrl, $fancy);
328 updateStatus("Can't write config file.", true);
332 updateStatus("Done!");
334 function writeConf($sitename, $sqlUrl, $fancy, $type='mysql')
336 $res = file_put_contents(INSTALLDIR.'/config.php',
338 "if (!defined('LACONICA')) { exit(1); }\n\n".
339 "\$config['site']['name'] = \"$sitename\";\n\n".
340 ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
341 "\$config['db']['database'] = \"$sqlUrl\";\n\n".
342 ($type == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n" .
343 "\$config['db']['type'] = \"$type\";\n\n" : '').
348 function runDbScript($filename, $conn, $type='mysql')
350 $sql = trim(file_get_contents($filename));
351 $stmts = explode(';', $sql);
352 foreach ($stmts as $stmt) {
354 if (!mb_strlen($stmt)) {
357 if ($type == 'mysql') {
358 $res = mysql_query($stmt, $conn);
359 } elseif ($type=='pgsql') {
360 $res = pg_query($conn, $stmt);
362 if ($res === false) {
363 updateStatus("FAILED SQL: $stmt");
371 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
373 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
374 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
375 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
377 <title>Install Laconica</title>
378 <link rel="shortcut icon" href="favicon.ico"/>
379 <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
380 <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
381 <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
382 <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/default/css/ie.css?version=0.8" /><![endif]-->
383 <script src="js/jquery.min.js"></script>
384 <script src="js/install.js"></script>
389 <address id="site_contact" class="vcard">
390 <a class="url home bookmark" href=".">
391 <img class="logo photo" src="theme/default/logo.png" alt="Laconica"/>
392 <span class="fn org">Laconica</span>
398 <h1>Install Laconica</h1>