forked from endtwist/AjaxIM
/
install.php
572 lines (480 loc) · 25.8 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<!DOCTYPE html>
<html>
<head>
<title>Ajax IM — Instant Messaging Framework</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
}
body {
background: #fff;
font: 62.5% Helvetica Neue, Helvetica, Arial, Tahoma, sans-serif;
}
input, button, select {
font: 1.25em/1.25em Helvetica Neue, Helvetica, Arial, Tahoma, sans-serif;
}
input:focus {
outline: none;
}
code {
font-size: 1.1em;
font-family: Consolas, Inconsolata, Courier New, Courier, monospace;
}
/* [begin] Upper */
div.upper {
display: block;
width: 100%;
padding: 1em 0;
margin: 0 0 2em;
background: #444;
color: #fff;
}
/* [end] Upper */
/* [begin] Header */
div#header {
display: block;
margin: 0 0 0 6.67em;
font-size: 1.5em;
line-height: 1em;
overflow: hidden;
}
div#header a {
font-weight: 700;
color: #444;
}
h1 {
text-indent: -10000px;
background: url(http://ajaxim.com/images/logo.png);
width: 146px;
height: 44px;
margin: 0 0.5em 0 0;
padding: 0;
float: left;
}
h2 {
margin: 0;
font-size: 1.2em;
line-height: 41px;
}
/* [end] Header */
/* [begin] Install form */
form {
font-size: 1.5em;
margin-left: 6.67em;
}
form fieldset {
position: relative;
border: 0;
font-size: 1em;
padding: 0 0 0.5em;
margin: 0 0 2em;
background: #eee;
width: 600px;
}
form fieldset legend {
background: #eee;
padding: 0.6em 1em 1em;
font-weight: 700;
}
form fieldset p {
margin: 0;
padding: 0 1em 1.25em 1em;
}
form fieldset p label {
display: block;
font-weight: 700;
}
form fieldset p span:first-child {
font-weight: 700;
}
form fieldset p span.desc {
display: block;
font-weight: 200;
font-size: 0.8em;
margin-bottom: 0.25em;
}
input#install_db + label {
display: inline;
}
form fieldset ul li {
margin: 1em 0 0;
}
form fieldset ul li p {
padding: 0;
}
form fieldset ul li pre {
border-left: 2px solid #dedede;
padding-left: 1em;
margin-left: -1em;
width: 540px;
}
fieldset#mysql-config, fieldset#nodejs-config, p#please-install-db {
display: none;
}
form h3 {
font-size: 2em;
margin: 0 0 0.5em;
}
p#install {
text-align: right;
width: 600px;
margin: 0 0 3em;
}
p#install input {
font-size: 2em;
}
/* [end] Install form */
/* [begin] Footer */
p#footer {
font-size: 1.2em;
color: #666;
margin-left: 8.307em;
}
p#footer a {
color: #777;
}
p#footer a:visited {
color: #666;
}
/* [end] Footer */
</style>
<script type="text/javascript">
$(function() {
$('#server, #db').click();
$('#cookie_length, #session_length').change(function() {
if(parseFloat($('#session_length').val()) < parseFloat($('#cookie_length').val())) {
$('#session_length').val($('#cookie_length').val());
}
}).keypress();
});
</script>
</head>
<body>
<div class="upper">
<div id="header">
<h1>Ajax IM</h1>
<h2>instant messaging framework</h2>
</div>
</div>
<?php
// file_array() by Jamon Holmgren. Exclude files by putting them in the $exclude
// string separated by pipes. Returns an array with filenames as strings.
function file_array($path, $exclude = ".|..", $recursive = false) {
$path = rtrim($path, "/") . "/";
$folder_handle = opendir($path);
$exclude_array = explode("|", $exclude);
$result = array();
while(false !== ($filename = readdir($folder_handle))) {
if(!in_array(strtolower($filename), $exclude_array)) {
if(is_dir($path . $filename . "/")) {
if($recursive) $result[] = file_array($path, $exclude, true);
} else {
$result[] = $filename;
}
}
}
return $result;
}
function library_array($path, $exclude, $recursive = false) {
$files = file_array($path, $exclude, $recursive);
$libs = array();
foreach($files as $f) {
$library_src = preg_replace('/[\n\r]+/', "\n", file_get_contents(rtrim($path, "/") . "/" . $f));
preg_match('/^<' . '\?php[^\/]*\/\*(.+?)\*\//sm', $library_src, $library_header);
$header_data = array(
'Library' => $f,
'Author' => 'Unknown',
'Version' => '1.0',
'Date' => '',
'Description' => '',
'Requirements' => array()
);
if(strlen($library_header[1])) {
$header_keys = array('Library', 'Author', 'Version', 'Date', 'Description', 'Requirements');
$header_lines = explode("\n", $library_header[1]);
$last_key = false;
foreach($header_lines as $ln) {
$ln_kv = explode(':', substr($ln, 3), 2);
$key = ucwords(strtolower($ln_kv[0]));
if(in_array($key, $header_keys)) {
$header_data[$ln_kv[0]] = trim($ln_kv[1]);
$last_key = $key;
} else if($last_key && strlen(trim($ln)) > 0) {
$header_data[$last_key] .= substr($ln, 3) . ' ';
}
}
if(strlen($header_data['Requirements'])) {
$reqs = explode(',', $header_data['Requirements']);
foreach($reqs as &$req) $req = trim($req);
$header_data['Requirements'] = $reqs;
}
}
$libs[$f] = $header_data;
}
return $libs;
}
function get_available_servers() {
return library_array('./libraries/server', '.|..|base.php');
}
function get_available_db_engines() {
return library_array('./libraries/db', '.|..|base.php');
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$configphp = file_get_contents('config.php');
// Replace the server type
$configphp = preg_replace("/define\('(IM_LIBRARY)', '[^']+?'\);/", "define('$1', '" . str_replace('.php', '', $_POST['server']) . "');", $configphp);
// Replace the database type
$configphp = preg_replace("/define\('(DB_ENGINE)', '[^']+?'\);/", "define('$1', '" . str_replace('.php', '', $_POST['db']) . "');", $configphp);
// Replace the cookie name
$configphp = preg_replace("/define\('(COOKIE_NAME)', '[^']+?'\);/", "define('$1', '" . $_POST['cookie'] . "');", $configphp);
// Replace the cookie storage length
$configphp = preg_replace("/define\('(COOKIE_PERIOD)', [^)]+?\);/", "define('$1', '" . $_POST['cookie_length'] . "');", $configphp);
// Replace the MySQL information
$configphp = preg_replace("/define\('(MYSQL_DATABASE)', '[^']+?'\);/", "define('$1', '" . $_POST['mysqldb'] . "');", $configphp);
$configphp = preg_replace("/define\('(MYSQL_HOSTNAME)', '[^']+?'\);/", "define('$1', '" . $_POST['mysqlhost'] . "');", $configphp);
$configphp = preg_replace("/define\('(MYSQL_USERNAME)', '[^']+?'\);/", "define('$1', '" . $_POST['mysqluser'] . "');", $configphp);
$configphp = preg_replace("/define\('(MYSQL_PASSWORD)', '[^']+?'\);/", "define('$1', '" . $_POST['mysqlpass'] . "');", $configphp);
$configphp = preg_replace("/define\('(MYSQL_PREFIX)', '[^']+?'\);/", "define('$1', '" . $_POST['mysqlprefix'] . "');", $configphp);
if(intval($_POST['needs_nodejs']) == 1) {
$configjs = preg_replace("/[\r\n]+/", "\n", file_get_contents('server/config.js'));
// Replace the cookie name
$configjs = preg_replace("/(name: )'[^']+?',/", "$1'" . $_POST['cookie'] . "',", $configjs);
// Replace the session period
$configjs = preg_replace("/(period: )\d{1,6}/", "period: " . round(floatval($_POST['session_length']) * 24), $configjs);
// Replace the host and port numbers
$public_host = strlen(trim($_POST['public_host'])) ? "'" . trim($_POST['public_host']) . "'" : "null";
$private_host = strlen(trim($_POST['private_host'])) ? "'" . trim($_POST['private_host']) . "'" : "null";
$configjs = preg_replace("/(public: )\[8000, 'localhost'\],/", "$1[" . $_POST['public_port'] . ", " . $public_host . "],", $configjs);
$configjs = preg_replace("/(private: )\[11998, 'localhost'\]/", "$1[" . $_POST['private_port'] . ", " . $private_host . "],", $configjs);
$cjs = fopen('server/config.js', 'w');
fwrite($cjs, $configjs);
fclose($cjs);
$configphp = preg_replace('/(\$nodejs_memcache_server = array)\(\'[^\']+\', [^)]+\);/', "$1('" . ($private_host == 'null' ? 'localhost' : $_POST['private_host']) . "', " . $_POST['private_port'] . ");", $configphp);
if(!strlen(trim($_POST['public_host']))) {
$url = parse_url($_POST['url']);
$_POST['public_host'] = $url['host'];
}
$node_url = $_POST['public_host'] . ":" . $_POST['public_port'];
$imloadjs = file_get_contents('js/im.load.js');
$imloadjs = preg_replace("/(var nodehost = )'';/", "$1'" . $node_url . "';", $imloadjs);
$imljs = fopen('js/im.load.js', 'w');
fwrite($imljs, $imloadjs);
fclose($imljs);
}
$cphp = fopen('config.php', 'w');
fwrite($cphp, $configphp);
fclose($cphp);
if(isset($_POST['install_db'])) {
// Get DB src from file, run against database
$db_library_src = preg_replace("/[\r\n]+/", "\n", file_get_contents('libraries/db/MySQL.php'));
preg_match('/\/\*\s+SQL:(.+?)\*\//sm', $db_library_src, $db_sql);
$db_sql = explode(';', str_replace("\n", '', $db_sql[1]));
$sql = new PDO('mysql:dbname=' . $_POST['mysqldb'] . ';host=' . $_POST['mysqlhost'], $_POST['mysqluser'], $_POST['mysqlpass']);
foreach($db_sql as $statement) {
$sql->exec(str_replace('ajaxim_', $_POST['mysqlprefix'], $statement));
}
}
?>
<form method="post">
<h3>Installation</h3>
<fieldset>
<legend>Installation has completed successfully!</legend>
<ul>
<li>For security purposes, please delete <code>install.php</code>.</li>
<?php if(!$_POST['need_nodejs'] == 1) { ?>
<li>The Node.js server host has been added to <code>js/im.load.js</code>; please confirm that the URL generated is correct.</li>
<li>
<p>To get the standalone Node.js server running, you will need to run the following command on the command line of your server:</p>
<pre>node server/server.js</pre>
<p>However, it is recommended that you move the <code>server</code> folder outside of the main installation directory before going live.</p>
</li>
<li>
<p>Now that Ajax IM is installed, please see the <a href="http://ajaxim.com/documentation/basics/quick-start-nodejs/">Quick Start with Node.js</a> guide to complete set up.</p>
</li>
<?php } else { ?>
<li>Since you did <strong>not</strong> setup Ajax IM with the Node.js server, it is recommended that you delete the <code>server</code> folder (containing the files <code>config.js</code> and <code>server.js</code>).</li>
<li>
<p>Now that Ajax IM is installed, please see the <a href="http://ajaxim.com/documentation/basics/quick-start/">Quick Start</a> guide to complete set up.</p>
</li>
<?php } ?>
</ul>
</fieldset>
</form>
<?php
} else {
?>
<form method="post">
<h3>Installation</h3>
<p>For assistance, please see the <a href="http://ajaxim.com/installation/">installation tutorial</a>.</p>
<fieldset>
<legend>Basic Configuration</legend>
<p>
<span>Is <code>config.php</code> writable?</span>
<?php if(is_writable('config.php')): ?>
<span class="writable">Yes.</span>
<?php else: ?>
<span class="problem"><strong>No.</strong> Please change the permissions of <code>config.php</code> to "writable" or 755 (on Unix-based systems) before continuing!</span>
<?php endif; ?>
</p>
<p>
<span>Is <code>js/im.load.js</code> writable?</span>
<?php if(is_writable('js/im.load.js')): ?>
<span class="writable">Yes.</span>
<?php else: ?>
<span class="problem"><strong>No.</strong> Please change the permissions of <code>js/im.load.js</code> to "writable" or 755 (on Unix-based systems) before continuing!</span>
<?php endif; ?>
</p>
<p>
<label for="url">What is the URL of the installation?</label>
<input type="text" size="50" name="url" id="url" value="<?php print ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['SERVER_NAME'] . str_replace('install.php', '', $_SERVER['REQUEST_URI']); ?>">
</p>
<p>
<label for="server">Please select a server type.</label>
<select name="server" id="server">
<?php $servers = get_available_servers();
foreach($servers as $file => $server):
?>
<option value="<?php print $file; ?>"><?php print $server['Library'] . ' — ' . $server['Version']; ?> (by <?php print $server['Author']; ?>)</option>
<?php endforeach; ?>
</select>
<input type="hidden" name="needs_nodejs" id="needs_nodejs" value="0">
<script type="text/javascript">
var servers = <?php print json_encode($servers); ?>;
servers['None'] = {Description: 'None', Requirements: ['None']};
var selected_server = servers['None'];
var update_server_list = function(e) {
if(!(server = $('#server').val()))
server = 'None';
$('#server-select .description').text(servers[server].Description);
$('#server-select .requirements').text(servers[server].Requirements.join(', '));
selected_server = servers[server];
};
$('#server').live('click', update_server_list).live('change', update_server_list);
</script>
</p>
<p id="server-select">
<em>Description:</em> <span class="description">None</span><br>
<em>Requirements:</em> <span class="requirements">None</span>
</p>
<p>
<label for="db">If you will be using a database engine, please select one.</label>
<select name="db" id="db">
<option value="">No database engine selected</option>
<?php $dbs = get_available_db_engines();
foreach($dbs as $file => $db):
?>
<option value="<?php print $file; ?>"><?php print $db['Library'] . ' — ' . $db['Version']; ?> (by <?php print $db['Author']; ?>)</option>
<?php endforeach; ?>
</select>
<script type="text/javascript">
var db_engines = <?php print json_encode($dbs); ?>;
var engine = 'None';
db_engines['None'] = {Description: 'None', Requirements: ['None']};
var update_db_list = function(e) {
if(!(engine = $('#db').val()))
engine = 'None';
$('#db-select .description').text(db_engines[engine].Description);
$('#db-select .requirements').text(db_engines[engine].Requirements.join(', '));
};
$('#db').live('click', update_db_list).live('change', update_db_list);
var update_reqs = function() {
setTimeout(function() {
if(engine == 'MySQL.php' && $.inArray('Database', selected_server.Requirements) != -1) {
$('#please-install-db').fadeIn();
$('#mysql-config').slideDown();
} else {
$('#please-install-db').fadeOut();
$('#mysql-config').slideUp();
}
if($.inArray('Node.js', selected_server.Requirements) != -1) {
$('#nodejs-config').slideDown();
$('#needs_nodejs').val('1');
} else {
$('#nodejs-config').slideUp();
$('#needs_nodejs').val('0');
}
}, 100);
};
$('#db, #server').live('click', update_reqs).live('change', update_reqs);
</script>
</p>
<p id="db-select">
<em>Description:</em> <span class="description">None</span><br>
<em>Requirements:</em> <span class="requirements">None</span>
</p>
<p id="please-install-db">
<input type="checkbox" name="install_db" id="install_db" value="yes" checked="checked">
<label for="install_db">Please install the database for me.</label>
</p>
<p>
<label for="cookie">Cookie name</label>
<span class="desc">Ajax IM uses a cookie to keep track of the authenticated user's session. You can set the cookie name and session storage length.</span>
<input type="text" name="cookie" id="cookie" value="ajaxim_session">
</p>
<p>
<label for="cookie_length">Cookie storage length</label>
<span class="desc">The number of days for which to store the cookie. Setting it to 0 makes it a session cookie (deleted when the browser is closed). If Ajax IM is integrated with another authentication system, this cookie should be kept as long or longer than that cookie.</span>
<input type="text" maxlength="4" size="4" name="cookie_length" id="cookie_length" value="365"> days
</p>
</fieldset>
<fieldset id="mysql-config">
<legend>MySQL Configuration</legend>
<p>
<label for="mysqldb">Database</label>
<input type="text" name="mysqldb" id="mysqldb">
</p>
<p>
<label for="mysqlhost">Hostname</label>
<span class="desc">Note: Usually, you will not need to change this value.</span>
<input type="text" name="mysqlhost" id="mysqlhost" value="localhost">
</p>
<p>
<label for="mysqluser">Username</label>
<input type="text" name="mysqluser" id="mysqluser">
</p>
<p>
<label for="mysqlpass">Password</label>
<input type="text" name="mysqlpass" id="mysqlpass">
</p>
<p>
<label for="mysqluser">Table Prefix</label>
<input type="text" name="mysqlprefix" id="mysqlprefix" value="ajaxim_">
</p>
</fieldset>
<fieldset id="nodejs-config">
<legend>Standalone (Node.js) Server Configuration</legend>
<p>After configuration is complete, please see the <a href="http://ajaxim.com/installation">installation tutorial</a> to complete the standalone server setup.</p>
<p>
<span>Is <code>server/config.js</code> writable?</span>
<?php if(is_writable('server/config.js')) { ?>
<span class="writable">Yes.</span>
<?php } else { ?>
<span class="problem"><strong>No.</strong> Please change the permissions of <code>server/config.js</code> to "writable" or 755 (on Unix-based systems) before continuing!</span>
<?php } ?>
</p>
<p>
<label for="public_port">Public host and port</label>
<span class="desc">Host is optional; set to blank for none. This is the port number that the client will use to communicate with the server.</span>
<input type="text" size="15" name="public_host" id="public_host" value="localhost"> :
<input type="text" maxlength="5" size="5" name="public_port" id="public_port" value="31411">
</p>
<p>
<label for="public_port">Private (API) host and port</label>
<span class="desc">Host is optional; set to blank for none. This is the port number that the <code>server.php</code> script will use, as well as the port you will use for the <a href="http://ajaxim.com/documentation/api">Memcache-based API</a>.</span>
<input type="text" size="15" name="private_host" id="private_host" value="localhost"> :
<input type="text" maxlength="5" size="5" name="private_port" id="private_port" value="11211">
</p>
<p>
<label for="session_length">Session storage length</label>
<span class="desc">The number of days for which to store the session — which is different than the cookie. The cookie holds on to the session identifier, but if the session doesn't exist any longer, the cookie is useless. Therefore, the session needs to be kept as long, or longer, than the cookie. Minimum 0.042 days (approx. one hour).</span>
<input type="text" maxlength="4" size="4" name="session_length" id="session_length" value="365"> days
</p>
</fieldset>
<p id="install"><input type="submit" value="Install" name="install"></p>
</form>
<?php } ?>
<p id="footer"><a href="http://ajaxim.com">Ajax IM</a> © 2005 – 2010, <a href="http://unwieldy.net">Joshua Gross</a></p></p>
</body>
</html>