]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Merge branch '0.8.x' into twitter-import
[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 &quot;<?php echo $req; ?>&quot;.</p><?php
56                     return false;
57         }
58     }
59
60         if (!is_writable(INSTALLDIR)) {
61          ?><p class="error">Cannot write config file to &quot;<?php echo INSTALLDIR; ?>&quot;.</p>
62                <p>On your server, try this command:</p>
63                <blockquote>chmod a+w <?php echo INSTALLDIR; ?></blockquote>
64          <?php
65              return false;
66         }
67
68         if (!is_writable(INSTALLDIR.'/avatar/')) {
69          ?><p class="error">Cannot write avatar directory &quot;<?php echo INSTALLDIR; ?>/avatar/&quot;.</p>
70                <p>On your server, try this command:</p>
71                <blockquote>chmod a+w <?php echo INSTALLDIR; ?>/avatar/</blockquote>
72          <?
73              return false;
74         }
75
76         return true;
77 }
78
79 function checkExtension($name)
80 {
81     if (!extension_loaded($name)) {
82         if (!dl($name.'.so')) {
83             return false;
84         }
85     }
86     return true;
87 }
88
89 function showForm()
90 {
91 ?>
92 <p>Enter your database connection information below to initialize the database.</p>
93 <form method='post' action='install.php'>
94         <fieldset>
95         <ul class='form_data'>
96         <li>
97         <label for='sitename'>Site name</label>
98         <input type='text' id='sitename' name='sitename' />
99         <p>The name of your site</p>
100         </li>
101         <li>
102         <li>
103         <label for='host'>Hostname</label>
104         <input type='text' id='host' name='host' />
105         <p>Database hostname</p>
106         </li>
107         <li>
108         <label for='host'>Database</label>
109         <input type='text' id='database' name='database' />
110         <p>Database name</p>
111         </li>
112         <li>
113         <label for='username'>Username</label>
114         <input type='text' id='username' name='username' />
115         <p>Database username</p>
116         </li>
117         <li>
118         <label for='password'>Password</label>
119         <input type='password' id='password' name='password' />
120         <p>Database password</p>
121         </li>
122         </ul>
123         <input type='submit' name='submit' value='Submit'>
124         </fieldset>
125 </form>
126 <?
127 }
128
129 function updateStatus($status, $error=false)
130 {
131 ?>
132         <li>
133 <?
134     print $status;
135 ?>
136         </li>
137 <?
138 }
139
140 function handlePost()
141 {
142 ?>
143         <ul>
144 <?
145     $host = $_POST['host'];
146     $database = $_POST['database'];
147     $username = $_POST['username'];
148     $password = $_POST['password'];
149     $sitename = $_POST['sitename'];
150
151     if (empty($host)) {
152         updateStatus("No hostname specified.", true);
153         showForm();
154         return;
155     }
156
157     if (empty($database)) {
158         updateStatus("No database specified.", true);
159         showForm();
160         return;
161     }
162
163     if (empty($username)) {
164         updateStatus("No username specified.", true);
165         showForm();
166         return;
167     }
168
169     if (empty($password)) {
170         updateStatus("No password specified.", true);
171         showForm();
172         return;
173     }
174
175     if (empty($sitename)) {
176         updateStatus("No sitename specified.", true);
177         showForm();
178         return;
179     }
180
181     updateStatus("Starting installation...");
182     updateStatus("Checking database...");
183     $conn = mysql_connect($host, $username, $password);
184     if (!$conn) {
185         updateStatus("Can't connect to server '$host' as '$username'.", true);
186         showForm();
187         return;
188     }
189     updateStatus("Changing to database...");
190     $res = mysql_select_db($database, $conn);
191     if (!$res) {
192         updateStatus("Can't change to database.", true);
193         showForm();
194         return;
195     }
196     updateStatus("Running database script...");
197     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
198     if ($res === false) {
199         updateStatus("Can't run database script.", true);
200         showForm();
201         return;
202     }
203     foreach (array('sms_carrier' => 'SMS carrier',
204                    'notice_source' => 'notice source',
205                    'foreign_services' => 'foreign service')
206              as $scr => $name) {
207         updateStatus(sprintf("Adding %s data to database...", $name));
208         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
209         if ($res === false) {
210             updateStatus(sprintf("Can't run %d script.", $name), true);
211             showForm();
212             return;
213         }
214     }
215     updateStatus("Writing config file...");
216     $sqlUrl = "mysqli://$username:$password@$host/$database";
217     $res = writeConf($sitename, $sqlUrl);
218     if (!$res) {
219         updateStatus("Can't write config file.", true);
220         showForm();
221         return;
222     }
223     updateStatus("Done!");
224 ?>
225         </ul>
226 <?
227 }
228
229 function writeConf($sitename, $sqlUrl)
230 {
231     $res = file_put_contents(INSTALLDIR.'/config.php',
232                              "<?php\n".
233                              "\$config['site']['name'] = \"$sitename\";\n\n".
234                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
235     return $res;
236 }
237
238 function runDbScript($filename, $conn)
239 {
240     $sql = trim(file_get_contents($filename));
241     $stmts = explode(';', $sql);
242     foreach ($stmts as $stmt) {
243         $stmt = trim($stmt);
244         if (!mb_strlen($stmt)) {
245             continue;
246         }
247         $res = mysql_query($stmt, $conn);
248         if ($res === false) {
249             return $res;
250         }
251     }
252     return true;
253 }
254
255 ?>
256 <html>
257 <head>
258         <title>Install Laconica</title>
259         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.7.1" media="screen, projection, tv"/>
260         <link rel="stylesheet" type="text/css" href="theme/base/css/modal.css?version=0.7.1" media="screen, projection, tv"/>
261         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.7.1" media="screen, projection, tv"/>
262 </head>
263 <body>
264         <div id="wrap">
265         <div id="core">
266         <div id="content">
267         <h1>Install Laconica</h1>
268 <?php main(); ?>
269         </div>
270         </div>
271         </div>
272 </body>
273 </html>