]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
Merge branch '0.7.x' of git://gitorious.org/laconica/dev into 0.7.x
[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     foreach (array('sms_carrier' => 'SMS carrier',
186                    'notice_source' => 'notice source',
187                    'foreign_services' => 'foreign service')
188              as $scr => $name) {
189         updateStatus(sprintf("Adding %s data to database...", $name));
190         $res = runDbScript(INSTALLDIR.'/db/'.$scr.'.sql', $conn);
191         if ($res === false) {
192             updateStatus(sprintf("Can't run %d script.", $name), true);
193             showForm();
194             return;
195         }
196     }
197     updateStatus("Writing config file...");
198     $sqlUrl = "mysqli://$username:$password@$host/$database";
199     $res = writeConf($sitename, $sqlUrl);
200     if (!$res) {
201         updateStatus("Can't write config file.", true);
202         showForm();
203         return;
204     }
205     updateStatus("Done!");
206 ?>
207         </ul>
208 <?
209 }
210
211 function writeConf($sitename, $sqlUrl)
212 {
213     $res = file_put_contents(INSTALLDIR.'/config.php',
214                              "<?\n".
215                              "\$config['site']['name'] = \"$sitename\";\n\n".
216                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
217     return $res;
218 }
219
220 function runDbScript($filename, $conn)
221 {
222     $sql = trim(file_get_contents($filename));
223     $stmts = explode(';', $sql);
224     foreach ($stmts as $stmt) {
225         $stmt = trim($stmt);
226         if (!mb_strlen($stmt)) {
227             continue;
228         }
229         $res = mysql_query($stmt, $conn);
230         if ($res === false) {
231             return $res;
232         }
233     }
234     return true;
235 }
236
237 ?>
238 <html>
239 <head>
240         <title>Install Laconica</title>
241         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.7.1" media="screen, projection, tv"/>
242         <link rel="stylesheet" type="text/css" href="theme/base/css/modal.css?version=0.7.1" media="screen, projection, tv"/>
243         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.7.1" media="screen, projection, tv"/>
244 </head>
245 <body>
246         <div id="wrap">
247         <div id="core">
248         <div id="content">
249         <h1>Install Laconica</h1>
250 <? main() ?>
251         </div>
252         </div>
253         </div>
254 </body>
255 </html>