]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
fbfd5e4d119efe4cd628265edeb1d4a8a0782a21
[quix0rs-gnu-social.git] / install.php
1 <?
2 define('INSTALLDIR', dirname(__FILE__));
3
4 function main()
5 {
6     if (!checkPrereqs())
7     {
8         return;
9     }
10
11     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
12         handlePost();
13     } else {
14         showForm();
15     }
16 }
17
18 function checkPrereqs()
19 {
20     if (file_exists(INSTALLDIR.'/config.php')) {
21          ?><p class="error">Config file &quot;config.php&quot; already exists.</p>
22          <?
23         return false;
24     }
25
26     if (version_compare(PHP_VERSION, '5.0.0', '<')) {
27             ?><p class="error">Require PHP version 5 or greater.</p><?
28                     return false;
29     }
30
31     $reqs = array('gd', 'mysql', 'curl',
32                   'xmlwriter', 'mbstring',
33                   'gettext');
34
35     foreach ($reqs as $req) {
36         if (!checkExtension($req)) {
37             ?><p class="error">Cannot load required extension &quot;<?= $req ?>&quot;.</p><?
38                     return false;
39         }
40     }
41
42         if (!is_writable(INSTALLDIR)) {
43          ?><p class="error">Cannot write config file to &quot;<?= INSTALLDIR ?>&quot;.</p>
44                <p>On your server, try this command:</p>
45                <blockquote>chmod a+w <?= INSTALLDIR ?></blockquote>
46          <?
47              return false;
48         }
49
50         if (!is_writable(INSTALLDIR.'/avatar/')) {
51          ?><p class="error">Cannot write avatar directory &quot;<?= INSTALLDIR ?>/avatar/&quot;.</p>
52                <p>On your server, try this command:</p>
53                <blockquote>chmod a+w <?= INSTALLDIR ?>/avatar/</blockquote>
54          <?
55              return false;
56         }
57
58         return true;
59 }
60
61 function checkExtension($name)
62 {
63     if (!extension_loaded($name)) {
64         if (!dl($name.'.so')) {
65             return false;
66         }
67     }
68     return true;
69 }
70
71 function showForm()
72 {
73 ?>
74 <p>Enter your database connection information below to initialize the database.</p>
75 <form method='post' action='install.php'>
76         <fieldset>
77         <ul class='form_data'>
78         <li>
79         <label for='sitename'>Site name</label>
80         <input type='text' id='sitename' name='sitename' />
81         <p>The name of your site</p>
82         </li>
83         <li>
84         <li>
85         <label for='host'>Hostname</label>
86         <input type='text' id='host' name='host' />
87         <p>Database hostname</p>
88         </li>
89         <li>
90         <label for='host'>Database</label>
91         <input type='text' id='database' name='database' />
92         <p>Database name</p>
93         </li>
94         <li>
95         <label for='username'>Username</label>
96         <input type='text' id='username' name='username' />
97         <p>Database username</p>
98         </li>
99         <li>
100         <label for='password'>Password</label>
101         <input type='password' id='password' name='password' />
102         <p>Database password</p>
103         </li>
104         </ul>
105         <input type='submit' name='submit' value='Submit'>
106         </fieldset>
107 </form>
108 <?
109 }
110
111 function updateStatus($status, $error=false)
112 {
113 ?>
114         <li>
115 <?
116     print $status;
117 ?>
118         </li>
119 <?
120 }
121
122 function handlePost()
123 {
124 ?>
125         <ul>
126 <?
127     $host = $_POST['host'];
128     $database = $_POST['database'];
129     $username = $_POST['username'];
130     $password = $_POST['password'];
131     $sitename = $_POST['sitename'];
132
133     if (empty($host)) {
134         updateStatus("No hostname specified.", true);
135         showForm();
136         return;
137     }
138
139     if (empty($database)) {
140         updateStatus("No database specified.", true);
141         showForm();
142         return;
143     }
144
145     if (empty($username)) {
146         updateStatus("No username specified.", true);
147         showForm();
148         return;
149     }
150
151     if (empty($password)) {
152         updateStatus("No password specified.", true);
153         showForm();
154         return;
155     }
156
157     if (empty($sitename)) {
158         updateStatus("No sitename specified.", true);
159         showForm();
160         return;
161     }
162
163     updateStatus("Starting installation...");
164     updateStatus("Checking database...");
165     $conn = mysql_connect($host, $username, $password);
166     if (!$conn) {
167         updateStatus("Can't connect to server '$host' as '$username'.", true);
168         showForm();
169         return;
170     }
171     updateStatus("Changing to database...");
172     $res = mysql_select_db($database, $conn);
173     if (!$res) {
174         updateStatus("Can't change to database.", true);
175         showForm();
176         return;
177     }
178     updateStatus("Running database script...");
179     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
180     if ($res === false) {
181         updateStatus("Can't run database script.", true);
182         showForm();
183         return;
184     }
185     updateStatus("Adding SMS carrier data to database...");
186     $res = runDbScript(INSTALLDIR.'/db/sms_carrier.sql', $conn);
187     if ($res === false) {
188         updateStatus("Can't run SMS carrier script.", true);
189         showForm();
190         return;
191     }
192     updateStatus("Adding notice source data to database...");
193     $res = runDbScript(INSTALLDIR.'/db/notice_source.sql', $conn);
194     if ($res === false) {
195         updateStatus("Can't run notice source script.", true);
196         showForm();
197         return;
198     }
199     updateStatus("Writing config file...");
200     $sqlUrl = "mysqli://$username:$password@$host/$database";
201     $res = writeConf($sitename, $sqlUrl);
202     if (!$res) {
203         updateStatus("Can't write config file.", true);
204         showForm();
205         return;
206     }
207     updateStatus("Done!");
208 ?>
209         </ul>
210 <?
211 }
212
213 function writeConf($sitename, $sqlUrl)
214 {
215     $res = file_put_contents(INSTALLDIR.'/config.php',
216                              "<?\n".
217                              "\$config['site']['name'] = \"$sitename\";\n\n".
218                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
219     return $res;
220 }
221
222 function runDbScript($filename, $conn)
223 {
224     $sql = trim(file_get_contents($filename));
225     $stmts = explode(';', $sql);
226     foreach ($stmts as $stmt) {
227         $stmt = trim($stmt);
228         if (!mb_strlen($stmt)) {
229             continue;
230         }
231         $res = mysql_query($stmt, $conn);
232         if ($res === false) {
233             return $res;
234         }
235     }
236     return true;
237 }
238
239 ?>
240 <html>
241 <head>
242         <title>Install Laconica</title>
243         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.7.1" media="screen, projection, tv"/>
244         <link rel="stylesheet" type="text/css" href="theme/base/css/modal.css?version=0.7.1" media="screen, projection, tv"/>
245         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.7.1" media="screen, projection, tv"/>
246 </head>
247 <body>
248         <div id="wrap">
249         <div id="core">
250         <div id="content">
251         <h1>Install Laconica</h1>
252 <? main() ?>
253         </div>
254         </div>
255         </div>
256 </body>
257 </html>