-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathinstall.php
361 lines (306 loc) · 15.4 KB
/
install.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
* Copyright (C) 2012 MyProfile Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
ini_set('memory_limit', '64M');
set_time_limit ( 0 );
// schema file to be used as source
$db_schema = 'dbschema.sql';
define('INCLUDE_CHECK',true);
include 'lib/functions.php';
include 'header.php';
$ret = '';
$ret .= "<div class=\"content relative shadow clearfix main\">\n";
function remove_comments(&$output)
{
$lines = explode("\n", $output);
$output = "";
// try to keep mem. use down
$linecount = count($lines);
$in_comment = false;
for($i = 0; $i < $linecount; $i++)
{
if( preg_match("/^\/\*/", preg_quote($lines[$i])) )
{
$in_comment = true;
}
if( !$in_comment )
{
$output .= $lines[$i] . "\n";
}
if( preg_match("/\*\/$/", preg_quote($lines[$i])) )
{
$in_comment = false;
}
}
unset($lines);
return $output;
}
//
// remove_remarks will strip the sql comment lines out of an uploaded sql file
//
function remove_remarks($sql)
{
$lines = explode("\n", $sql);
// try to keep mem. use down
$sql = "";
$linecount = count($lines);
$output = "";
for ($i = 0; $i < $linecount; $i++)
{
if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0))
{
if (isset($lines[$i][0]) && $lines[$i][0] != "#")
{
$output .= $lines[$i] . "\n";
}
else
{
$output .= "\n";
}
// Trading a bit of speed for lower mem. use here.
$lines[$i] = "";
}
}
return $output;
}
//
// split_sql_file will split an uploaded sql file into single sql statements.
// Note: expects trim() to have already been run on $sql.
//
function split_sql_file($sql, $delimiter)
{
// Split up our string into "possible" SQL statements.
$tokens = explode($delimiter, $sql);
// try to save mem.
$sql = "";
$output = array();
// we don't actually care about the matches preg gives us.
$matches = array();
// this is faster than calling count($oktens) every time thru the loop.
$token_count = count($tokens);
for ($i = 0; $i < $token_count; $i++)
{
// Don't wanna add an empty string as the last thing in the array.
if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0)))
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
$unescaped_quotes = $total_quotes - $escaped_quotes;
// If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
if (($unescaped_quotes % 2) == 0)
{
// It's a complete sql statement.
$output[] = $tokens[$i];
// save memory.
$tokens[$i] = "";
}
else
{
// incomplete sql statement. keep adding tokens until we have a complete one.
// $temp will hold what we have so far.
$temp = $tokens[$i] . $delimiter;
// save memory..
$tokens[$i] = "";
// Do we have a complete statement yet?
$complete_stmt = false;
for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++)
{
// This is the total number of single quotes in the token.
$total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
// Counts single quotes that are preceded by an odd number of backslashes,
// which means they're escaped quotes.
$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
$unescaped_quotes = $total_quotes - $escaped_quotes;
if (($unescaped_quotes % 2) == 1)
{
// odd number of unescaped quotes. In combination with the previous incomplete
// statement(s), we now have a complete statement. (2 odds always make an even)
$output[] = $temp . $tokens[$j];
// save memory.
$tokens[$j] = "";
$temp = "";
// exit the loop.
$complete_stmt = true;
// make sure the outer loop continues at the right point.
$i = $j;
}
else
{
// even number of unescaped quotes. We still don't have a complete statement.
// (1 odd and 1 even always make an odd)
$temp .= $tokens[$j] . $delimiter;
// save memory.
$tokens[$j] = "";
}
} // for..
} // else
}
}
return $output;
}
// Create database tables and write configuration to disk
if (isset($_REQUEST['submit'])) {
/* Database config */
$db_database = trim($_REQUEST['database']);
$db_host = trim($_REQUEST['host']);
$db_user = trim($_REQUEST['user']);
$db_pass = trim($_REQUEST['pass']);
/* SMTP server config */
if (isset($_REQUEST['smtp_auth']))
$smtp_authentication = true;
else
$smtp_authentication = false;
$smtp_email = trim($_REQUEST['smtp_email']);
$smtp_server = trim($_REQUEST['smtp_server']);
$smtp_username = trim($_REQUEST['smtp_user']);
$smtp_passpasswod = trim($_REQUEST['smtp_pass']);
// Establish db connection
if (!mysql_connect($db_host,$db_user,$db_pass))
$ret .= error('Unable to connect to database!');
mysql_select_db($db_database);
mysql_query("SET names UTF8");
// proceed only if we have an empty database
if (mysql_num_rows(mysql_query("SHOW TABLES FROM " . $db_database)) == 0) {
// write configuration to config.php
$cf = fopen('config.php', 'w') or die('Cannot create the config.php file!');
$content = "<?php\n";
$content .= "// ------------- AUTOGENERATED FILE ---------------- //\n";
$content .= "if(!defined('INCLUDE_CHECK')) die('You are not allowed to execute this file directly');\n";
$content .= "\n";
$content .= "// ------------- USER STUFF ---------------- //\n";
$content .= "/* Password for CA private key (used to generate client certs) */\n";
$content .= 'define (\'CA_PASS\', \'' . trim($_REQUEST['capass']) . '\');' . "\n";
$content .= "/* OpenSSL config file location */\n";
$content .= 'define (\'SSL_CONF\', \'' . trim($_REQUEST['openssl']) . '\');' . "\n";
$content .= "\n";
$content .= "/* IDP address */\n";
$content .= 'define (\'IDP\', \'' . trim($_REQUEST['idp']) . '\');' . "\n";
$content .= "\n";
$content .= "/* Cache time to live - default 48h */\n";
$content .= 'define (\'CACHE_TTL\', \'' . 48 * 60 * 60 . '\');' . "\n";
$content .= "\n";
$content .= "/* SPARQL endpoint */\n";
$content .= 'define (\'SPARQL_ENDPOINT\', \'' . trim($_REQUEST['endpoint']) . '\');' . "\n";
$content .= "\n";
$content .= "/* Database config */\n";
$content .= 'define (\'DB_DATABASE\', \'' . $db_database . '\');' . "\n";
$content .= 'define (\'DB_HOST\', \'' . $db_host . '\');' . "\n";
$content .= 'define (\'DB_USER\', \'' . $db_user . '\');' . "\n";
$content .= 'define (\'DB_PASS\', \'' . $db_pass . '\');' . "\n";
$content .= "\n";
$content .= "/* SMTP config */\n";
$content .= 'define (\'SMTP_EMAIL\', \'' . $smtp_email . '\');' . "\n";
$content .= 'define (\'SMTP_AUTHENTICATION\', \'' . $smtp_authentication . '\');' . "\n";
$content .= 'define (\'SMTP_SERVER\', \'' . $smtp_server . '\');' . "\n";
$content .= 'define (\'SMTP_USERNAME\', \'' . $smtp_username . '\');' . "\n";
$content .= 'define (\'SMTP_PASSWORD\', \'' . $smtp_passpasswod . '\');' . "\n";
$content .= "\n";
$content .= "// Establish db connection\n";
$content .= 'mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(\'Unable to establish a database connection\');' . "\n";
$content .= 'mysql_select_db(DB_DATABASE);' . "\n";
$content .= "mysql_query(\"SET names UTF8\");\n";
$content .= "?>";
fwrite($cf, $content);
fclose($cf);
$cf_status = "<p><font color=\"green\"><strong>Success!</strong></font> Configuration file has been saved to disk.</p>\n";
// create cache dir if it doesn't exist
if (!is_dir('cache/')) {
if (!mkdir('cache/', 0775))
die('Failed to create cache/ dir...');
$cache_status = "<p><font color=\"green\"><strong>Success!</strong></font> Cache dir has been created.</p>\n";
} else {
$cache_status = "<p><strong>Skipped.</strong> Cache dir already exists.</p>\n";
}
// create logs dir if it doesn't exist
if (!is_dir('logs/')) {
if (!mkdir('logs/', 0775))
die('Failed to create logs/ dir...');
$logs_status = "<p><font color=\"green\"><strong>Success!</strong></font> Logs dir has been created.</p>\n";
} else {
$logs_status = "<p><strong>Skipped.</strong> Logs dir already exists.</p>\n";
}
// create dir where we store profiles if it doesn't exist
if (!is_dir('people/')) {
if (!mkdir('people/', 0775))
die('Failed to create people/ dir...');
$people_status = "<p><font color=\"green\"><strong>Success!</strong></font> Profile root dir has been created.</p>\n";
} else {
$people_status = "<p><strong>Skipped.</strong> Profile root dir already exists.</p>\n";
}
// create database tables
$sql_query = @fread(@fopen($db_schema, 'r'), @filesize($db_schema)) or die('problem ');
$sql_query = remove_remarks($sql_query);
$sql_query = split_sql_file($sql_query, ';');
foreach($sql_query as $sql){
mysql_query($sql) or die('error in query');
}
$sql_status = "<p><font color=\"green\"><strong>Success!</strong></font> " . mysql_num_rows(mysql_query("SHOW TABLES FROM " . $db_database)) . " database tables have been created.</p>\n";
// display success
$ret .= "<p><font align=\"left\" style=\"font-size: 2em; text-shadow: 0 1px 1px #cccccc;\">MyProfile Installation</font></p>\n";
$ret .= success('Your installation is complete!');
$ret .= "<br/><div>\n";
$ret .= "<form action=\"index.php\" method=\"POST\">\n";
$ret .= $sql_status;
$ret .= $cf_status;
$ret .= $cache_status;
$ret .= $logs_status;
$ret .= $people_status;
$ret .= "<br/><p><input class=\"btn btn-primary\" type=\"submit\" name=\"submit\" value=\" Take me to the main page! \"></p>\n";
$ret .= "</form></div>\n";
} else {
$ret .= error('Cannot perform installation on existing database. Please clear database!');
}
} else {
// display form
$ret .= "<div>\n";
$ret .= "<p><font align=\"left\" style=\"font-size: 2em; text-shadow: 0 1px 1px #cccccc;\">MyProfile Installation</font></p>\n";
$ret .= "<form action=\"\" method=\"POST\">\n";
$ret .= "<p>Once the installation is complete, you can safely remove this file (<strong>install.php</strong>) \n";
$ret .= "as well as the database schema file (<strong>dbschema.sql</strong>).</p><br/>\n";
$ret .= "<table>\n";
$ret .= "<tr><td colspan=\"2\"><p><strong>SSL configuration</strong></p><br/></td></tr>\n";
$ret .= "<tr><td>CA key password: </td><td><input type=\"password\" name=\"capass\" value=\"\"> <small><font color=\"grey\">[needed for generating certificates]</font></small></td></tr>\n";
$ret .= "<tr><td>OpenSSL config file: </td><td><input type=\"text\" name=\"openssl\" size=\"50\" placeholder=\"/etc/ssl/private/CA.key\" value=\"\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><br/><p><strong>Delegated authentication</strong></p><br/></td></tr>\n";
$ret .= "<tr><td>IdP address: </td><td><input type=\"text\" name=\"idp\" size=\"50\" value=\"https://auth.my-profile.eu/auth/?authreqissuer=\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><font color=\"grey\">Important note regarding using a different IdP: if you want to use a different IdP, you will have to manually edit the file <i>lib/libAuthentication/Authentication_X509CertRepo.php</i> and add the IdP's certificate in (PEM form) to the array of IdPs.</font></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><br/><p><strong>SPARQL endpoint</strong></p><br/></td></tr>\n";
$ret .= "<tr><td>Endpoint address: </td><td><input type=\"text\" name=\"endpoint\" size=\"50\" value=\"\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><br/><p><strong>Database configuration</strong></p><br/></td></tr>\n";
$ret .= "<tr><td>Database host: </td><td><input type=\"text\" name=\"host\" value=\"localhost\"></td></tr>\n";
$ret .= "<tr><td>Database name: </td><td><input type=\"text\" name=\"database\" value=\"\"></td></tr>\n";
$ret .= "<tr><td>Database user: </td><td><input type=\"text\" name=\"user\" value=\"\"></td></tr>\n";
$ret .= "<tr><td>Database pass: </td><td><input type=\"password\" name=\"pass\" value=\"\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><br/><p><strong>Email server</strong></p><br/></td></tr>\n";
$ret .= "<tr><td>Email address: </td><td><input type=\"text\" name=\"smtp_email\" placeholder=\"[email protected]\" value=\"\"></td></tr>\n";
$ret .= "<tr><td>Email server: </td><td><input type=\"text\" name=\"smtp_server\" value=\"\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\">Does the server require authentication? <input type=\"checkbox\" name=\"smtp_auth\" value=\"\"></td></tr>\n";
$ret .= "<tr><td>Email user: </td><td><input type=\"text\" name=\"smtp_user\" value=\"\"></td></tr>\n";
$ret .= "<tr><td>Email pass: </td><td><input type=\"password\" name=\"smtp_pass\" value=\"\"></td></tr>\n";
$ret .= "<tr><td colspan=\"2\"><p><input class=\"btn btn-primary\" type=\"submit\" name=\"submit\" value=\" Proceed to install \"></p></td></tr>\n";
$ret .= "</table>\n";
$ret .= "</form></div>\n";
}
$ret .= "</div>\n";
echo $ret;
include 'footer.php';
?>