]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - install.php
a34214c48a45c12fc14e00fb7b4497e887df1c15
[quix0rs-gnu-social.git] / install.php
1 <?
2 define('INSTALLDIR', dirname(__FILE__));
3
4 function main()
5 {
6     checkPrereqs();
7     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
8         handlePost();
9     } else {
10         showForm();
11     }
12 }
13
14 function checkPrereqs()
15 {
16 }
17
18 function showForm()
19 {
20 ?>
21 <p>Enter your database connection information below to initialize the database.</p>
22 <form method='post' action='install.php'>
23         <fieldset>
24         <ul class='form_data'>
25         <li>
26         <label for='sitename'>Site name</label>
27         <input type='text' id='sitename' name='sitename' />
28         <p>The name of your site</p>
29         </li>
30         <li>
31         <li>
32         <label for='host'>Hostname</label>
33         <input type='text' id='host' name='host' />
34         <p>Database hostname</p>
35         </li>
36         <li>
37         <label for='host'>Database</label>
38         <input type='text' id='database' name='database' />
39         <p>Database name</p>
40         </li>
41         <li>
42         <label for='username'>Username</label>
43         <input type='text' id='username' name='username' />
44         <p>Database username</p>
45         </li>
46         <li>
47         <label for='password'>Password</label>
48         <input type='password' id='password' name='password' />
49         <p>Database password</p>
50         </li>
51         </ul>
52         <input type='submit' name='submit' value='Submit'>
53         </fieldset>
54 </form>
55 <?
56 }
57
58 function updateStatus($status, $error=false)
59 {
60 ?>
61         <li>
62 <?
63     print $status;
64 ?>
65         </li>
66 <?
67 }
68
69 function handlePost()
70 {
71 ?>
72         <ul>
73 <?
74     $host = $_POST['host'];
75     $database = $_POST['database'];
76     $username = $_POST['username'];
77     $password = $_POST['password'];
78     $sitename = $_POST['sitename'];
79
80     updateStatus("Starting installation...");
81     updateStatus("Checking database...");
82     $conn = mysql_connect($host, $username, $password);
83     if (!$conn) {
84         updateStatus("Can't connect to server '$host' as '$username'.", true);
85         showForm();
86         return;
87     }
88     updateStatus("Changing to database...");
89     $res = mysql_select_db($database, $conn);
90     if (!$res) {
91         updateStatus("Can't change to database.", true);
92         showForm();
93         return;
94     }
95     updateStatus("Running database script...");
96     $res = runDbScript(INSTALLDIR.'/db/laconica.sql', $conn);
97     if ($res === false) {
98         updateStatus("Can't run database script.", true);
99         showForm();
100         return;
101     }
102     updateStatus("Writing config file...");
103     $sqlUrl = "mysqli://$username:$password@$host/$database";
104     $res = writeConf($sitename, $sqlUrl);
105     if (!$res) {
106         updateStatus("Can't write config file.", true);
107         showForm();
108         return;
109     }
110     updateStatus("Done!");
111 ?>
112         </ul>
113 <?
114 }
115
116 function writeConf($sitename, $sqlUrl)
117 {
118     $res = file_put_contents(INSTALLDIR.'/config.php',
119                              "<?\n".
120                              "\$config['site']['name'] = \"$sitename\";\n\n".
121                              "\$config['db']['database'] = \"$sqlUrl\";\n\n");
122     return $res;
123 }
124
125 function runDbScript($filename, $conn)
126 {
127     $sql = trim(file_get_contents($filename));
128     $stmts = explode(';', $sql);
129     foreach ($stmts as $stmt) {
130         $stmt = trim($stmt);
131         if (!mb_strlen($stmt)) {
132             continue;
133         }
134         $res = mysql_query($stmt, $conn);
135         if ($res === false) {
136             return $res;
137         }
138     }
139     return true;
140 }
141
142 ?>
143 <html>
144 <head>
145         <title>Install Laconica</title>
146         <link rel="stylesheet" type="text/css" href="theme/base/css/display.css?version=0.7.1" media="screen, projection, tv"/>
147         <link rel="stylesheet" type="text/css" href="theme/base/css/modal.css?version=0.7.1" media="screen, projection, tv"/>
148         <link rel="stylesheet" type="text/css" href="theme/default/css/display.css?version=0.7.1" media="screen, projection, tv"/>
149 </head>
150 <body>
151         <div id="wrap">
152         <div id="core">
153         <div id="content">
154         <h1>Install Laconica</h1>
155 <? main() ?>
156         </div>
157         </div>
158         </div>
159 </body>
160 </html>