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');
54 foreach ($reqs as $req) {
55 if (!checkExtension($req)) {
56 ?><p class="error">Cannot load required extension: <code><?php echo $req; ?></code></p><?php
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
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>
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>
87 function checkExtension($name)
89 if (!extension_loaded($name)) {
90 if (!dl($name.'.so')) {
103 <dl id="page_notice" class="system_notice">
106 <div class="instructions">
107 <p>Enter your database connection information below to initialize the database.</p>
111 <form method="post" action="install.php" class="form_settings" id="form_install">
113 <legend>Connection settings</legend>
114 <ul class="form_data">
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>
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>
127 <label for="host">Hostname</label>
128 <input type="text" id="host" name="host" />
129 <p class="form_guide">Database hostname</p>
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>
140 <label for="database">Name</label>
141 <input type="text" id="database" name="database" />
142 <p class="form_guide">Database name</p>
145 <label for="username">Username</label>
146 <input type="text" id="username" name="username" />
147 <p class="form_guide">Database username</p>
150 <label for="password">Password</label>
151 <input type="password" id="password" name="password" />
152 <p class="form_guide">Database password (optional)</p>
155 <input type="submit" name="submit" class="submit" value="Submit" />
162 function updateStatus($status, $error=false)
165 <li <?php echo ($error) ? 'class="error"': ''; ?>><?php echo $status;?></li>
170 function handlePost()
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);
186 <dl class="system_notice">
194 updateStatus("No hostname specified.", true);
198 if (empty($database)) {
199 updateStatus("No database specified.", true);
203 if (empty($username)) {
204 updateStatus("No username specified.", true);
208 // if (empty($password)) {
209 // updateStatus("No password specified.", true);
213 if (empty($sitename)) {
214 updateStatus("No sitename specified.", true);
225 $db = mysql_db_installer($host, $database, $username, $password);
228 $db = pgsql_db_installer($host, $database, $username, $password);
234 // database connection failed, do not move on to create config file.
238 updateStatus("Writing config file...");
239 $res = writeConf($sitename, $server, $path, $fancy, $db);
242 updateStatus("Can't write config file.", true);
248 TODO https needs to be considered
250 $link = "http://".$server.'/'.$path;
252 updateStatus("Laconica has been installed at $link");
253 updateStatus("You can visit your <a href='$link'>new Laconica site</a>.");
259 function pgsql_db_installer($host, $database, $username, $password) {
260 $connstring = "dbname=$database host=$host user=$username";
262 //No password would mean trust authentication used.
263 if (!empty($password)) {
264 $connstring .= " password=$password";
266 updateStatus("Starting installation...");
267 updateStatus("Checking database...");
268 $conn = pg_connect($connstring);
270 if ($conn ===false) {
271 updateStatus("Failed to connect to database: $connstring");
276 //ensure database encoding is UTF8
277 $record = pg_fetch_object(pg_query($conn, 'SHOW server_encoding'));
278 if ($record->server_encoding != 'UTF8') {
279 updateStatus("Laconica requires UTF8 character encoding. Your database is ". htmlentities($record->server_encoding));
284 updateStatus("Running database script...");
285 //wrap in transaction;
286 pg_query($conn, 'BEGIN');
287 $res = runDbScript(INSTALLDIR.'/db/laconica_pg.sql', $conn, 'pgsql');
289 if ($res === false) {
290 updateStatus("Can't run database script.", true);
294 foreach (array('sms_carrier' => 'SMS carrier',
295 'notice_source' => 'notice source',
296 'foreign_services' => 'foreign service')
298 updateStatus(sprintf("Adding %s data to database...", $name));
299 $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn, 'pgsql');
300 if ($res === false) {
301 updateStatus(sprintf("Can't run %d script.", $name), true);
306 pg_query($conn, 'COMMIT');
308 if (empty($password)) {
309 $sqlUrl = "pgsql://$username@$host/$database";
312 $sqlUrl = "pgsql://$username:$password@$host/$database";
315 $db = array('type' => 'pgsql', 'database' => $sqlUrl);
320 function mysql_db_installer($host, $database, $username, $password) {
321 updateStatus("Starting installation...");
322 updateStatus("Checking database...");
324 $conn = mysql_connect($host, $username, $password);
326 updateStatus("Can't connect to server '$host' as '$username'.", true);
330 updateStatus("Changing to database...");
331 $res = mysql_select_db($database, $conn);
333 updateStatus("Can't change to database.", true);
337 updateStatus("Running database script...");
338 $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
339 if ($res === false) {
340 updateStatus("Can't run database script.", true);
344 foreach (array('sms_carrier' => 'SMS carrier',
345 'notice_source' => 'notice source',
346 'foreign_services' => 'foreign service')
348 updateStatus(sprintf("Adding %s data to database...", $name));
349 $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
350 if ($res === false) {
351 updateStatus(sprintf("Can't run %d script.", $name), true);
357 $sqlUrl = "mysqli://$username:$password@$host/$database";
358 $db = array('type' => 'mysql', 'database' => $sqlUrl);
362 function writeConf($sitename, $server, $path, $fancy, $db)
364 // assemble configuration file in a string
366 "if (!defined('LACONICA')) { exit(1); }\n\n".
369 "\$config['site']['name'] = '$sitename';\n\n".
372 "\$config['site']['server'] = '$server';\n".
373 "\$config['site']['path'] = '$path'; \n\n".
375 // checks if fancy URLs are enabled
376 ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
379 "\$config['db']['database'] = '{$db['database']}';\n\n".
380 ($db['type'] == 'pgsql' ? "\$config['db']['quote_identifiers'] = true;\n\n":'').
381 "\$config['db']['type'] = '{$db['type']}';\n\n".
384 // write configuration file out to install directory
385 $res = file_put_contents(INSTALLDIR.'/config.php', $cfg);
390 function runDbScript($filename, $conn, $type = 'mysql')
392 $sql = trim(file_get_contents($filename));
393 $stmts = explode(';', $sql);
394 foreach ($stmts as $stmt) {
396 if (!mb_strlen($stmt)) {
401 $res = mysql_query($stmt, $conn);
404 $res = pg_query($conn, $stmt);
407 updateStatus("runDbScript() error: unknown database type ". $type ." provided.");
409 if ($res === false) {
410 updateStatus("FAILED SQL: $stmt");
418 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
420 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
422 <title>Install Laconica</title>
423 <link rel="shortcut icon" href="favicon.ico"/>
424 <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
425 <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
426 <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
427 <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/default/css/ie.css?version=0.8" /><![endif]-->
428 <script src="js/jquery.min.js"></script>
429 <script src="js/install.js"></script>
434 <address id="site_contact" class="vcard">
435 <a class="url home bookmark" href=".">
436 <img class="logo photo" src="theme/default/logo.png" alt="Laconica"/>
437 <span class="fn org">Laconica</span>
443 <h1>Install Laconica</h1>