]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Updated typography and layout
[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     updateStatus("Starting installation...");
134     updateStatus("Checking database...");
135     $conn = mysql_connect($host, $username, $password);
136     if (!$conn) {
137         updateStatus("Can't connect to server '$host' as '$username'.", true);
138         showForm();
139         return;
140     }
141     updateStatus("Changing to database...");
142     $res = mysql_select_db($database, $conn);
143     if (!$res) {
144         updateStatus("Can't change to database.", true);
145         showForm();
146         return;
147     }
148     updateStatus("Running database script...");
149     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
150     if ($res === false) {
151         updateStatus("Can't run database script.", true);
152         showForm();
153         return;
154     }
155     updateStatus("Writing config file...");
156     $sqlUrl = "mysqli://$username:$password@$host/$database";
157     $res = writeConf($sitename, $sqlUrl);
158     if (!$res) {
159         updateStatus("Can't write config file.", true);
160         showForm();
161         return;
162     }
163     updateStatus("Done!");
164 ?>
165         </ul>
166 <?
167 }
168
169 function writeConf($sitename, $sqlUrl)
170 {
171     $res = file_put_contents(INSTALLDIR.'/config.php',
172                              "<?\n".
173                              "\$config['site']['name'] = \"$sitename\";\n\n".
174                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
175     return $res;
176 }
177
178 function runDbScript($filename, $conn)
179 {
180     $sql = trim(file_get_contents($filename));
181     $stmts = explode(';', $sql);
182     foreach ($stmts as $stmt) {
183         $stmt = trim($stmt);
184         if (!mb_strlen($stmt)) {
185             continue;
186         }
187         $res = mysql_query($stmt, $conn);
188         if ($res === false) {
189             return $res;
190         }
191     }
192     return true;
193 }
194
195 ?>
196 <html>
197 <head>
198         <title>Install Laconica</title>
199         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.7.1" media="screen, projection, tv"/>
200         <link rel="stylesheet" type="text/css" href="theme/base/css/modal.css?version=0.7.1" media="screen, projection, tv"/>
201         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.7.1" media="screen, projection, tv"/>
202 </head>
203 <body>
204         <div id="wrap">
205         <div id="core">
206         <div id="content">
207         <h1>Install Laconica</h1>
208 <? main() ?>
209         </div>
210         </div>
211         </div>
212 </body>
213 </html>