[r193]: trunk / setup.php Maximize Restore History

Download this file

setup.php    497 lines (435 with data), 17.5 kB

  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
<?php
/**
* @author Paul Bukowski <pbukowski@telaxus.com>
* @version 1.0
* @copyright Copyright &copy; 2007, Telaxus LLC
* @license MIT
* @package epesi-base
*/
error_reporting(E_ALL & ~E_NOTICE);
ob_start();
ini_set('arg_separator.output','&');
@define('SYSTEM_TIMEZONE',date_default_timezone_get());
date_default_timezone_set(SYSTEM_TIMEZONE);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>EPESI setup</title>
<link href="setup.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<table id="banner" border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="image">&nbsp;</td>
<td class="back">&nbsp;</td>
</tr>
</table>
<br>
<center>
<table id="main" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<?php
define('_VALID_ACCESS',1);
require_once('include/data_dir.php');
/**
* Check access to working directories
*/
if(file_exists('easyinstall.php')){
unlink('easyinstall.php');
}
if(trim(ini_get("safe_mode")))
die('You cannot use EPESI with PHP safe mode turned on - please disable it. Please notice this feature is deprecated since PHP 5.3 and will be removed in PHP 6.0.');
if(file_exists(DATA_DIR.'/config.php'))
die('Cannot write into '.DATA_DIR.'/config.php file. Please delete this file.');
if(!is_writable(DATA_DIR))
die('Cannot write into "'.DATA_DIR.'" directory. Please fix privileges.');
@define("_VALID_ACCESS", true);
require_once('modules/Libs/QuickForm/requires.php');
if(!isset($_GET['license'])) {
print('<h1>Welcome to EPESI setup!<br></h1><div class="license">');
license();
print('</div><CENTER><br><h2>By clicking on Accept button you agree to the above License terms.</h2>');
print('<br><a class="button" href="setup.php?license=1">Accept</a></CENTER>');
} elseif(!isset($_GET['htaccess'])) {
ob_start();
print('<h1>Welcome to EPESI setup!<br></h1><h2>Hosting compatibility:</h2><br><div class="license">');
if(check_htaccess()) {
$_GET['htaccess'] = 1;
ob_end_clean();
} else {
print('</div><br><a class="button" href="setup.php?license=1&htaccess=1">Ok</a>');
ob_end_flush();
}
}
if(isset($_GET['htaccess']) && isset($_GET['license'])) {
$form = new HTML_QuickForm('serverform','post',$_SERVER['PHP_SELF'].'?'.http_build_query($_GET));
$form -> addElement('header', null, 'Database server settings');
$form -> addElement('text', 'host', 'Database server address');
$form -> addRule('host', 'Field required', 'required');
$form -> addElement('select', 'engine', 'Database engine',array('postgres'=>'PostgreSQL', 'mysqlt'=>'MySQL'));
$form -> addRule('engine', 'Field required', 'required');
$form -> addElement('text', 'user', 'Database server user');
$form -> addRule('user', 'Field required', 'required');
$form -> addElement('password', 'password', 'Database server password');
$form -> addRule('password', 'Field required', 'required');
$form -> addElement('text', 'db', 'Database name');
$form -> addRule('db', 'Field required', 'required');
$form -> addElement('select', 'newdb', 'Create new database',array(0=>'No',1=>'Yes'),array('onChange'=>'if(this.value==1)alert("WARNING: Make sure you have CREATE access level to do this!","warning");'));
$form -> addRule('newdb', 'Field required', 'required');
// $form -> addElement('select', 'newuser', 'Create new user',array(1=>'Yes', 0=>'No'));
// $form -> addRule('newuser', 'Field required', 'required');
$form -> addElement('submit', 'submit', 'Next');
$form -> setDefaults(array('engine'=>'mysqlt','db'=>'epesi','host'=>'localhost'));
$form->setRequiredNote('<span class="required_note_star">*</span> <span class="required_note">denotes required field</span>');
$form -> addElement('html','<tr><td colspan=2><br /><b>Any existing tables will be dropped!</b><br />The database will be populated with data.<br />This operation can take several minutes.</td></tr>');
if($form -> validate()) {
$engine = $form -> exportValue('engine');
switch($engine) {
case 'postgres':
$host = $form -> exportValue('host');
$user = $form -> exportValue('user');
$pass = $form -> exportValue('password');
if(!function_exists('pg_connect')) {
echo('Please enable postgresql extension in php.ini.');
} else {
$link = pg_connect("host=$host user=$user password=$pass dbname=postgres");
if(!$link) {
echo('Could not connect.');
} else {
$dbname = $form -> exportValue('db');
if($form->exportValue('newdb')==1) {
$sql = 'CREATE DATABASE '.$dbname;
if (pg_query($link, $sql)) {
//echo "Database '$dbname' created successfully\n";
write_config($host,$user,$pass,$dbname,$engine);
} else {
echo 'Error creating database: ' . pg_last_error() . "\n";
}
pg_close($link);
} else {
include_once('libs/adodb/adodb.inc.php');
$ado = & NewADOConnection('postgres');
if(!@$ado->Connect($host,$user,$pass,$dbname)) {
echo 'Database does not exist.'."\n";
echo '<br />Please create the database first <br />or select option <b>Create new database</b>';
} else {
write_config($host, $user, $pass, $dbname, $engine);
}
}
}
}
break;
case 'mysqlt':
$host = $form->exportValue('host');
$user = $form->exportValue('user');
$pass = $form->exportValue('password');
if(!function_exists('mysql_connect')) {
echo('Please enable mysql extension in php.ini.');
} else {
$link = @mysql_connect($host,$user,$pass);
if (!$link) {
echo('Could not connect: ' . mysql_error());
} else {
$dbname = $form->exportValue('db');
if($form->exportValue('newdb')==1) {
$sql = 'CREATE DATABASE `'.$dbname.'` CHARACTER SET utf8 COLLATE utf8_unicode_ci';
if(mysql_query($sql, $link)) {
//echo "Database '$dbname' created successfully\n";
write_config($host,$user,$pass,$dbname,$engine);
}
else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
mysql_close($link);
} else {
$result=mysql_select_db($dbname, $link);
if (!$result) {
echo 'Database does not exist: ' . mysql_error() . "\n";
echo '<br />Please create the database first <br />or select option <b>Create new database</b>';
} else {
write_config($host, $user, $pass, $dbname, $engine);
}
}
}
}
break;
}
}
$renderer =& $form->defaultRenderer();
$renderer->setHeaderTemplate("\n\t<tr>\n\t\t<td style=\"white-space: nowrap; height: 20px; vertical-align: middle; background-color: #336699; background-image: url('images/header-blue.png'); background-repeat: repeat-x; color: #FFFFFF; font-weight: normal; text-align: center;\" align=\"left\" valign=\"top\" colspan=\"2\">{header}</td>\n\t</tr>");
$renderer->setElementTemplate("\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required -->{label}</td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>");
$form->accept($renderer);
print($renderer->toHtml());
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
function check_htaccess() {
$dir = trim(dirname($_SERVER['SCRIPT_NAME']),'/');
$epesi_dir = '/'.$dir.($dir?'/':'');
$protocol = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS'])!== "off") ? 'https://' : 'http://';
$test_url = $protocol.$_SERVER['HTTP_HOST'].$epesi_dir.'data/test.php';
file_put_contents('data/test.php','<?php'."\n".'print("OK"); ?>');
copy('htaccess.txt','data/.htaccess');
if(ini_get('allow_url_fopen'))
$ret = @file_get_contents($test_url);
elseif (extension_loaded('curl')) { // Test if curl is loaded
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $test_url);
$ret = curl_exec($ch);
curl_close($ch);
}
if(!isset($ret)) {
unlink('data/.htaccess');
unlink('data/test.php');
print('Unable to check EPESI root .htaccess file hosting compatibility. You should tweak it yourself. <br>Suggested .htaccess file is:<pre>'.file_get_contents('htaccess.txt').'</pre>');
return false;
}
if($ret!=="OK") {
file_put_contents('data/.htaccess',"Options -Indexes\nSetEnv PHPRC ".dirname(__FILE__)."\n");
if(ini_get('allow_url_fopen'))
$ret = @file_get_contents($test_url);
elseif (extension_loaded('curl')) { // Test if curl is loaded
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $test_url);
$ret = curl_exec($ch);
curl_close($ch);
}
if($ret!=="OK") {
unlink('data/.htaccess');
unlink('data/test.php');
print('Your hosting is not compatible with default EPESI root .htaccess file. You should tweak it yourself. <br>Default .htaccess file is:<pre>'.file_get_contents('htaccess.txt').'</pre>');
return false;
}
}
if(!is_writable('.')) {
unlink('data/test.php');
print('Your hosting is compatible with default EPESI root .htaccess file, but installer cannot write to EPESI root directory. You should paste following text to .htaccess file manually.<pre>'.file_get_contents('data/.htaccess').'</pre>');
unlink('data/.htaccess');
return false;
}
unlink('data/test.php');
rename('data/.htaccess','.htaccess');
return true;
}
function write_config($host, $user, $pass, $dbname, $engine) {
$local_dir = dirname(dirname(str_replace('\\','/',__FILE__)));
$script_filename = str_replace('\\','/',$_SERVER['SCRIPT_FILENAME']);
$epesi_dir = '';
if(strcmp($local_dir,substr($script_filename,0,strlen($local_dir))))
$epesi_dir = '
define("EPESI_DIR","'.str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])).'");';
$c = & fopen(DATA_DIR.'/config.php', 'w');
fwrite($c, '<?php
/**
* Config file
*
* This file contains database configuration.
*/
defined(\'_VALID_ACCESS\') || die(\'Direct access forbidden\');
/**
* Address of SQL server.
*/
define(\'DATABASE_HOST\',\''.addcslashes($host, '\'\\').'\');
/**
* User to log in to SQL server.
*/
define(\'DATABASE_USER\',\''.addcslashes($user, '\'\\').'\');
/**
* User password to authorize SQL server.
*/
define(\'DATABASE_PASSWORD\',\''.addcslashes($pass, '\'\\').'\');
/**
* Database to use.
*/
define(\'DATABASE_NAME\',\''.addcslashes($dbname, '\'\\').'\');
/**
* Database driver.
*/
define(\'DATABASE_DRIVER\',\''.addcslashes($engine, '\'\\').'\');
/*
* Turns on transfer reduction: not everything is sent to the client
*/
define(\'REDUCING_TRANSFER\',1);
/*
* Turn on common cache.
*/
define(\'CACHE_COMMON_FILES\',0);
/*
* A lot of debug info, starting with what modules are changed, what module variables are set... etc.
*/
define(\'DEBUG\',0);
/*
* Show module loading time ... = module + all children times
*/
define(\'MODULE_TIMES\',0);
/*
* Show queries execution time.
*/
define(\'SQL_TIMES\',0);
/*
* If you have got good server, but poor connection, turn it on.
*/
define(\'STRIP_OUTPUT\',0);
/*
* Display errors on page.
*/
define(\'DISPLAY_ERRORS\',1);
/*
* Notify all errors, including E_NOTICE, etc. Developer should use it!
*/
define(\'REPORT_ALL_ERRORS\',1);
/*
* Compress history
*/
define(\'GZIP_HISTORY\',1);
/*
* Minify compression
*/
define(\'MINIFY_ENCODE\',1);
/*
* Show donation links in EPESI
*/
define(\'SUGGEST_DONATION\',1);
/*
* automatically check for new version
*/
define(\'CHECK_VERSION\',1);
/*
* Disable some administrator preferences.
*/
define(\'DEMO_MODE\',0);
'.$epesi_dir.'
?>');
fclose($c);
ob_start();
ob_start('rm_config');
//fill database
clean_database();
install_base();
ob_end_flush();
if(file_exists(DATA_DIR.'/config.php'))
header('Location: index.php');
ob_end_flush();
}
//////////////////////////////////////////////
function rm_config($x) {
if($x) {
unlink(dirname(__FILE__).'/'.DATA_DIR.'/config.php');
clean_database();
}
return $x;
}
function clean_database() {
require_once('include/config.php');
require_once('include/database.php');
$tables_db = DB::MetaTables();
$tables = array();
if(DATABASE_DRIVER=='mysqlt' || DATABASE_DRIVER=='mysqli')
DB::Execute('SET FOREIGN_KEY_CHECKS=0');
if(DATABASE_DRIVER=='postgres' && strpos(DB::GetOne('SELECT version()'),'PostgreSQL 8.2')!==false) {
foreach ($tables_db as $t) {
$idxs = DB::Execute('SELECT t.tgargs as args FROM pg_trigger t,pg_class c,pg_proc p WHERE t.tgenabled AND t.tgrelid = c.oid AND t.tgfoid = p.oid AND p.proname = \'RI_FKey_check_ins\' AND c.relname = \''.strtolower($t).'\' ORDER BY t.tgrelid');
$matches = array(1=>array());
while ($i = $idxs->FetchRow()) {
$data = explode(chr(0), $i[0]);
$matches[1][] = $data[0];
}
$num_keys = count($matches[1]);
for ( $i = 0; $i < $num_keys; $i ++ )
DB::Execute('ALTER TABLE '.$t.' DROP CONSTRAINT '.$matches[1][$i]);
}
}
foreach($tables_db as $t) {
DB::DropTable($t);
}
if(DATABASE_DRIVER=='mysqlt' || DATABASE_DRIVER=='mysqli')
DB::Execute('SET FOREIGN_KEY_CHECKS=1');
}
function install_base() {
require_once('include/config.php');
require_once('include/database.php');
$ret = DB::CreateTable('modules',"name C(128) KEY,version I NOTNULL, priority I NOTNULL DEFAULT 0");
if($ret===false)
die('Invalid SQL query - Setup module (modules table)');
$ret = DB::CreateTable('session',"name C(32) NOTNULL," .
"expires I NOTNULL DEFAULT 0, data B",array('constraints'=>', PRIMARY KEY(name)'));
if($ret===false)
die('Invalid SQL query - Database module (session table)');
$ret = DB::CreateTable('session_client',"session_name C(32) NOTNULL, client_id I2," .
"data B",array('constraints'=>', FOREIGN KEY(session_name) REFERENCES session(name), PRIMARY KEY(client_id,session_name)'));
if($ret===false)
die('Invalid SQL query - Database module (session_client table)');
$ret = DB::CreateTable('history',"session_name C(32) NOTNULL, page_id I, client_id I2," .
"data B",array('constraints'=>', FOREIGN KEY(session_name) REFERENCES session(name), PRIMARY KEY(client_id,session_name,page_id)'));
if($ret===false)
die('Invalid SQL query - Database module (history table)');
DB::CreateIndex('history__session_name__client_id__idx', 'history', 'session_name, client_id');
$ret = DB::CreateTable('variables',"name C(32) KEY,value X");
if($ret===false)
die('Invalid SQL query - Database module (variables table)');
$ret = DB::Execute("insert into variables values('default_module',%s)",array(serialize('FirstRun')));
if($ret === false)
die('Invalid SQL query - Setup module (populating variables)');
$ret = DB::Execute("insert into variables values('version',%s)",array(serialize(EPESI_VERSION)));
if($ret === false)
die('Invalid SQL query - Setup module (populating variables)');
//phpgacl
require( "libs/adodb/adodb-xmlschema.inc.php" );
$errh = DB::$ado->raiseErrorFn;
DB::$ado->raiseErrorFn = false;
$schema = new adoSchema(DB::$ado);
$schema->ParseSchema('libs/phpgacl/schema.xml');
$schema->ContinueOnError(TRUE);
$ret = $schema->ExecuteSchema();
if($ret===false)
die('Invalid SQL query - Setup module (phpgacl tables)');
DB::$ado->raiseErrorFn = $errh;
require_once('include/acl.php');
Acl::$gacl->add_object_section('Administration','Administration',1,0,'aco');
Acl::$gacl->add_object_section('Data','Data',2,0,'aco');
Acl::$gacl->add_object('Administration','Main','Main',1,0,'aco');
Acl::$gacl->add_object('Administration','Modules','Modules',2,0,'aco');
Acl::$gacl->add_object('Data','Moderation','Moderation',1,0,'aco');
Acl::$gacl->add_object('Data','View','View',2,0,'aco');
Acl::$gacl->add_object_section('Users','Users',1,0,'aro');
$user_id = Acl::$gacl->add_group('User','User');
$moderator_id = Acl::$gacl->add_group('Moderator','Moderator', $user_id);
$administrator_id = Acl::$gacl->add_group('Administrator','Administrator', $moderator_id);
$sa_id = Acl::$gacl->add_group('Super administrator','Super administrator', $administrator_id);
Acl::$gacl->add_acl(array('Administration' =>array('Main')), array(), array($sa_id), NULL, NULL,1,1,'','','user');
Acl::$gacl->add_acl(array('Administration' =>array('Modules')), array(), array($administrator_id), NULL, NULL,1,1,'','','user');
Acl::$gacl->add_acl(array('Data' =>array('Moderation')), array(), array($moderator_id), NULL, NULL,1,1,'','','user');
Acl::$gacl->add_acl(array('Data' =>array('View')), array(), array($user_id), NULL, NULL,1,1,'','','user');
}
//////////////////////////////////////////////
function license() {
$fp = @fopen('license.html', 'r');
if ($fp){
$license_txt = fread($fp,filesize('license.html'));
}
fclose($fp);
print $license_txt;
}
?>
</td>
</tr>
</table>
</center>
<br>
<center>
<span class="footer">Copyright &copy; <?php echo date('Y'); ?> &bull; <a href="http://www.telaxus.com">Telaxus LLC</a></span>
<br>
<p><a href="http://www.epesi.org"><img src="images/epesi-powered.png" border="0"></a></p>
</center>
</body>
</html>
<?php
ob_end_flush();
?>