]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Attachments and their list now provide "ajax" view. Also added a few sidebars relatin...
[quix0rs-gnu-social.git] / install.php
1 <?php
2 /**
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2009, Controlez-Vous, 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     if (file_exists(INSTALLDIR.'/config.php')) {
39          ?><p class="error">Config file &quot;config.php&quot; already exists.</p>
40          <?php
41         return false;
42     }
43
44     if (version_compare(PHP_VERSION, '5.0.0', '<')) {
45             ?><p class="error">Require PHP version 5 or greater.</p><?php
46                     return false;
47     }
48
49     $reqs = array('gd', 'mysql', 'curl',
50                   'xmlwriter', 'mbstring',
51                   'gettext');
52
53     foreach ($reqs as $req) {
54         if (!checkExtension($req)) {
55             ?><p class="error">Cannot load required extension: <code><?php echo $req; ?></code></p><?php
56                     return false;
57         }
58     }
59
60         if (!is_writable(INSTALLDIR)) {
61          ?><p class="error">Cannot write config file to: <code><?php echo INSTALLDIR; ?></code></p>
62                <p>On your server, try this command: <code>chmod a+w <?php echo INSTALLDIR; ?></code>
63          <?php
64              return false;
65         }
66
67         if (!is_writable(INSTALLDIR.'/avatar/')) {
68          ?><p class="error">Cannot write avatar directory: <code><?php echo INSTALLDIR; ?>/avatar/</code></p>
69                <p>On your server, try this command: <code>chmod a+w <?php echo INSTALLDIR; ?>/avatar/</code></p>
70          <?
71              return false;
72         }
73
74         return true;
75 }
76
77 function checkExtension($name)
78 {
79     if (!extension_loaded($name)) {
80         if (!dl($name.'.so')) {
81             return false;
82         }
83     }
84     return true;
85 }
86
87 function showForm()
88 {
89 ?>
90         </ul>
91     </dd>
92 </dl>
93 <dl id="page_notice" class="system_notice">
94     <dt>Page notice</dt>
95     <dd>
96         <div class="instructions">
97             <p>Enter your database connection information below to initialize the database.</p>
98         </div>
99     </dd>
100 </dl>
101 <form method="post" action="install.php" class="form_settings" id="form_install">
102     <fieldset>
103         <legend>Connection settings</legend>
104         <ul class="form_data">
105             <li>
106                 <label for="sitename">Site name</label>
107                 <input type="text" id="sitename" name="sitename" />
108                 <p class="form_guide">The name of your site</p>
109             </li>
110             <li>
111             <li>
112                 <label for="host">Hostname</label>
113                 <input type="text" id="host" name="host" />
114                 <p class="form_guide">Database hostname</p>
115             </li>
116             <li>
117                 <label for="host">Database</label>
118                 <input type="text" id="database" name="database" />
119                 <p class="form_guide">Database name</p>
120             </li>
121             <li>
122                 <label for="username">Username</label>
123                 <input type="text" id="username" name="username" />
124                 <p class="form_guide">Database username</p>
125             </li>
126             <li>
127                 <label for="password">Password</label>
128                 <input type="password" id="password" name="password" />
129                 <p class="form_guide">Database password</p>
130             </li>
131         </ul>
132         <input type="submit" name="submit" class="submit" value="Submit" />
133     </fieldset>
134 </form>
135 <?php
136 }
137
138 function updateStatus($status, $error=false)
139 {
140 ?>
141                 <li <?php echo ($error) ? 'class="error"': ''; ?>><?print $status;?></li>
142
143 <?php
144 }
145
146 function handlePost()
147 {
148 ?>
149
150 <?php
151     $host = $_POST['host'];
152     $database = $_POST['database'];
153     $username = $_POST['username'];
154     $password = $_POST['password'];
155     $sitename = $_POST['sitename'];
156 ?>
157     <dl class="system_notice">
158         <dt>Page notice</dt>
159         <dd>
160             <ul>
161 <?php
162     if (empty($host)) {
163         updateStatus("No hostname specified.", true);
164         showForm();
165         return;
166     }
167
168     if (empty($database)) {
169         updateStatus("No database specified.", true);
170         showForm();
171         return;
172     }
173
174     if (empty($username)) {
175         updateStatus("No username specified.", true);
176         showForm();
177         return;
178     }
179
180     if (empty($password)) {
181         updateStatus("No password specified.", true);
182         showForm();
183         return;
184     }
185
186     if (empty($sitename)) {
187         updateStatus("No sitename specified.", true);
188         showForm();
189         return;
190     }
191
192     updateStatus("Starting installation...");
193     updateStatus("Checking database...");
194     $conn = mysql_connect($host, $username, $password);
195     if (!$conn) {
196         updateStatus("Can't connect to server '$host' as '$username'.", true);
197         showForm();
198         return;
199     }
200     updateStatus("Changing to database...");
201     $res = mysql_select_db($database, $conn);
202     if (!$res) {
203         updateStatus("Can't change to database.", true);
204         showForm();
205         return;
206     }
207     updateStatus("Running database script...");
208     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
209     if ($res === false) {
210         updateStatus("Can't run database script.", true);
211         showForm();
212         return;
213     }
214     foreach (array('sms_carrier' => 'SMS carrier',
215                    'notice_source' => 'notice source',
216                    'foreign_services' => 'foreign service')
217              as $scr => $name) {
218         updateStatus(sprintf("Adding %s data to database...", $name));
219         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
220         if ($res === false) {
221             updateStatus(sprintf("Can't run %d script.", $name), true);
222             showForm();
223             return;
224         }
225     }
226     updateStatus("Writing config file...");
227     $sqlUrl = "mysqli://$username:$password@$host/$database";
228     $res = writeConf($sitename, $sqlUrl);
229     if (!$res) {
230         updateStatus("Can't write config file.", true);
231         showForm();
232         return;
233     }
234     updateStatus("Done!");
235 ?>
236
237 <?php
238 }
239
240 function writeConf($sitename, $sqlUrl)
241 {
242     $res = file_put_contents(INSTALLDIR.'/config.php',
243                              "<?php\n".
244                              "\$config['site']['name'] = \"$sitename\";\n\n".
245                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
246     return $res;
247 }
248
249 function runDbScript($filename, $conn)
250 {
251     $sql = trim(file_get_contents($filename));
252     $stmts = explode(';', $sql);
253     foreach ($stmts as $stmt) {
254         $stmt = trim($stmt);
255         if (!mb_strlen($stmt)) {
256             continue;
257         }
258         $res = mysql_query($stmt, $conn);
259         if ($res === false) {
260             return $res;
261         }
262     }
263     return true;
264 }
265
266 ?>
267 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
268 <!DOCTYPE html
269 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
270        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
271 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
272     <head>
273         <title>Install Laconica</title>
274         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.8" media="screen, projection, tv"/>
275         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
276         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
277         <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
278         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/earthy/css/ie.css?version=0.8" /><![endif]-->
279     </head>
280     <body id="install">
281         <div id="wrap">
282             <div id="header">
283                 <address id="site_contact" class="vcard">
284                     <a class="url home bookmark" href=".">
285                         <img class="logo photo" src="theme/default/logo.png" alt="Laconica"/>
286                         <span class="fn org">Laconica</span>
287                     </a>
288                 </address>
289             </div>
290             <div id="core">
291                 <div id="content">
292                     <h1>Install Laconica</h1>
293 <?php main(); ?>
294                 </div>
295             </div>
296         </div>
297     </body>
298 </html>