]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Updated JS to show/hide attachment thumbnails with timers. Minor
[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         $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', 'mysql', '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
62         if (!is_writable(INSTALLDIR)) {
63          ?><p class="error">Cannot write config file to: <code><?php echo INSTALLDIR; ?></code></p>
64                <p>On your server, try this command: <code>chmod a+w <?php echo INSTALLDIR; ?></code>
65          <?php
66              $pass = false;
67         }
68
69         if (!is_writable(INSTALLDIR.'/avatar/')) {
70          ?><p class="error">Cannot write avatar directory: <code><?php echo INSTALLDIR; ?>/avatar/</code></p>
71                <p>On your server, try this command: <code>chmod a+w <?php echo INSTALLDIR; ?>/avatar/</code></p>
72          <?
73              $pass = false;
74         }
75
76         return $pass;
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     $config_path = htmlentities(trim(dirname($_SERVER['REQUEST_URI']), '/'));
92     echo<<<E_O_T
93         </ul>
94     </dd>
95 </dl>
96 <dl id="page_notice" class="system_notice">
97     <dt>Page notice</dt>
98     <dd>
99         <div class="instructions">
100             <p>Enter your database connection information below to initialize the database.</p>
101         </div>
102     </dd>
103 </dl>
104 <form method="post" action="install.php" class="form_settings" id="form_install">
105     <fieldset>
106         <legend>Connection settings</legend>
107         <ul class="form_data">
108             <li>
109                 <label for="sitename">Site name</label>
110                 <input type="text" id="sitename" name="sitename" />
111                 <p class="form_guide">The name of your site</p>
112             </li>
113             <li>
114                 <label for="fancy-enable">Fancy URLs</label>
115                 <input type="radio" name="fancy" id="fancy-enable" value="enable" checked='checked' /> enable<br />
116                 <input type="radio" name="fancy" id="fancy-disable" value="" /> disable<br />
117                 <p class="form_guide" id='fancy-form_guide'>Enable fancy (pretty) URLs. Auto-detection failed, it depends on Javascript.</p>
118             </li>
119             <li>
120                 <label for="host">Site path</label>
121                 <input type="text" id="path" name="path" value="$config_path" />
122                 <p class="form_guide">Site path, following the "/" after the domain name in the URL. Empty is fine. Field should be filled automatically.</p>
123             </li>
124             <li>
125                 <label for="host">Hostname</label>
126                 <input type="text" id="host" name="host" />
127                 <p class="form_guide">Database hostname</p>
128             </li>
129             <li>
130                 <label for="host">Database</label>
131                 <input type="text" id="database" name="database" />
132                 <p class="form_guide">Database name</p>
133             </li>
134             <li>
135                 <label for="username">Username</label>
136                 <input type="text" id="username" name="username" />
137                 <p class="form_guide">Database username</p>
138             </li>
139             <li>
140                 <label for="password">Password</label>
141                 <input type="password" id="password" name="password" />
142                 <p class="form_guide">Database password</p>
143             </li>
144         </ul>
145         <input type="submit" name="submit" class="submit" value="Submit" />
146     </fieldset>
147 </form>
148
149 E_O_T;
150 }
151
152 function updateStatus($status, $error=false)
153 {
154 ?>
155                 <li <?php echo ($error) ? 'class="error"': ''; ?>><?print $status;?></li>
156
157 <?php
158 }
159
160 function handlePost()
161 {
162 ?>
163
164 <?php
165     $host     = $_POST['host'];
166     $database = $_POST['database'];
167     $username = $_POST['username'];
168     $password = $_POST['password'];
169     $sitename = $_POST['sitename'];
170     $path     = $_POST['path'];
171     $fancy    = !empty($_POST['fancy']);
172 ?>
173     <dl class="system_notice">
174         <dt>Page notice</dt>
175         <dd>
176             <ul>
177 <?php
178         $fail = false;
179         
180     if (empty($host)) {
181         updateStatus("No hostname specified.", true);
182                 $fail = true;
183     }
184
185     if (empty($database)) {
186         updateStatus("No database specified.", true);
187                 $fail = true;
188     }
189
190     if (empty($username)) {
191         updateStatus("No username specified.", true);
192                 $fail = true;
193     }
194
195     if (empty($password)) {
196         updateStatus("No password specified.", true);
197                 $fail = true;
198     }
199
200     if (empty($sitename)) {
201         updateStatus("No sitename specified.", true);
202                 $fail = true;
203     }
204
205         if($fail){
206                 showForm();
207             return;
208         }
209
210     updateStatus("Starting installation...");
211     updateStatus("Checking database...");
212     $conn = mysql_connect($host, $username, $password);
213     if (!$conn) {
214         updateStatus("Can't connect to server '$host' as '$username'.", true);
215         showForm();
216         return;
217     }
218     updateStatus("Changing to database...");
219     $res = mysql_select_db($database, $conn);
220     if (!$res) {
221         updateStatus("Can't change to database.", true);
222         showForm();
223         return;
224     }
225     updateStatus("Running database script...");
226     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
227     if ($res === false) {
228         updateStatus("Can't run database script.", true);
229         showForm();
230         return;
231     }
232     foreach (array('sms_carrier' => 'SMS carrier',
233                    'notice_source' => 'notice source',
234                    'foreign_services' => 'foreign service')
235              as $scr => $name) {
236         updateStatus(sprintf("Adding %s data to database...", $name));
237         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
238         if ($res === false) {
239             updateStatus(sprintf("Can't run %d script.", $name), true);
240             showForm();
241             return;
242         }
243     }
244     updateStatus("Writing config file...");
245     $sqlUrl = "mysqli://$username:$password@$host/$database";
246     $res = writeConf($sitename, $sqlUrl, $fancy, $path);
247     if (!$res) {
248         updateStatus("Can't write config file.", true);
249         showForm();
250         return;
251     }
252     updateStatus("Done!");
253     if ($path) $path .= '/';
254     updateStatus("You can visit your <a href='/$path'>new Laconica site</a>.");
255 ?>
256
257 <?php
258 }
259
260 function writeConf($sitename, $sqlUrl, $fancy, $path)
261 {
262     $res = file_put_contents(INSTALLDIR.'/config.php',
263                              "<?php\n".
264                              "if (!defined('LACONICA')) { exit(1); }\n\n".
265                              "\$config['site']['name'] = \"$sitename\";\n\n".
266                              ($fancy ? "\$config['site']['fancy'] = true;\n\n":'').
267                              "\$config['site']['path'] = \"$path\";\n\n".
268                              "\$config['db']['database'] = \"$sqlUrl\";\n\n".
269                              "?>");
270     return $res;
271 }
272
273 function runDbScript($filename, $conn)
274 {
275     $sql = trim(file_get_contents($filename));
276     $stmts = explode(';', $sql);
277     foreach ($stmts as $stmt) {
278         $stmt = trim($stmt);
279         if (!mb_strlen($stmt)) {
280             continue;
281         }
282         $res = mysql_query($stmt, $conn);
283         if ($res === false) {
284             return $res;
285         }
286     }
287     return true;
288 }
289
290 ?>
291 <?php echo"<?"; ?> xml version="1.0" encoding="UTF-8" <?php echo "?>"; ?>
292 <!DOCTYPE html
293 PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
294        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
295 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
296     <head>
297         <title>Install Laconica</title>
298         <link rel="shortcut icon" href="favicon.ico"/>
299         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.8" media="screen, projection, tv"/>
300         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/base/css/ie.css?version=0.8" /><![endif]-->
301         <!--[if lte IE 6]><link rel="stylesheet" type="text/css" theme/base/css/ie6.css?version=0.8" /><![endif]-->
302         <!--[if IE]><link rel="stylesheet" type="text/css" href="theme/default/css/ie.css?version=0.8" /><![endif]-->
303         <script src="js/jquery.min.js"></script>
304         <script src="js/install.js"></script>
305     </head>
306     <body id="install">
307         <div id="wrap">
308             <div id="header">
309                 <address id="site_contact" class="vcard">
310                     <a class="url home bookmark" href=".">
311                         <img class="logo photo" src="theme/default/logo.png" alt="Laconica"/>
312                         <span class="fn org">Laconica</span>
313                     </a>
314                 </address>
315             </div>
316             <div id="core">
317                 <div id="content">
318                     <h1>Install Laconica</h1>
319 <?php main(); ?>
320                 </div>
321             </div>
322         </div>
323     </body>
324 </html>