/
install.lib.php
571 lines (501 loc) · 19.8 KB
/
install.lib.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
<?php
//Instalation Related Functions
/*
* Creates or replaces the database configuration file
*
*/
function write_db_config($host, $name, $user, $pass, $prefix, $file){
$buffer = <<<EOF
<?php
define("DB_HOST", "$host");
define("DB_NAME", "$name");
define("DB_USER", "$user");
define("DB_PASS", "$pass");
define("TB_PRFX", "$prefix");
?>
EOF;
if (!file_put_contents($file, $buffer)){throw new Exception("Could not wrtite configuration.", 2);}
}
/*
* Creates or replaces the wallet configuration file
*
*/
function write_wallet_config($type, $host, $port, $user, $pass, $address_version, $coldwallet_file, $encr, $file){
//Hot or Cold Wallet
if($type == 'cold'){$host = ""; $port = "";}
//Clean up the variables a bit
if(!strcmp($host,"1. RPC Server (default - localhost)")){$host = "localhost";}
if(!strcmp($user,"2. RPC Username.")){$user = "";}
if(!strcmp($pass,"3. RPC Password.")){$pass = "";}
if(!strcmp($port,"4. RPC Port")){$port = "";}
if(!strcmp($address_version,"5. Address Version")){$address_version = "";}
if(!strcmp($encr,"7. If wallet encrypted, Enter PASS")){$encr = "";}
if(!strcmp($coldwallet_file,"6. Cold Wallet Datafile")){$coldwallet_file = "";}
$buffer = <<<EOF
<?php
define("ADDRESS_VERSION", "$address_version"); // Found in src/base58.h:CBitcoinAddress.PUBKEY_ADDRESS
//Hot Wallet Authetication Info
//The cold wallet also has a username/password component that's used by the push/pull client and server
define("PAYMENT_GW_RPC_USER", "$user");
define("PAYMENT_GW_RPC_PASS", "$pass");
//For cold Wallet
define("PAYMENT_GW_DATAFILE", "$coldwallet_file");
//For Hot Wallet - Leave PAYMENT_GW_RPC_HOST empty to use Cold Wallet
define("PAYMENT_GW_RPC_HOST", "$host");
define("PAYMENT_GW_RPC_PORT", "$port");
// If the wallet is encrypted, enter the PASSPHRASE here. Leave it blank otherwise!
define("PAYMENT_GW_RPC_ENCR", "$encr");
?>
EOF;
if (!file_put_contents($file, $buffer)){throw new Exception("Could not wrtite configuration.", 2);}
//Make sure we have what we need
if(empty($coldwallet_file) && $type == 'cold'){throw new Exception("No Datafile");}
if(empty($port) && $type == 'hot'){throw new Exception("No Port Given");}
if($type == 'cold' && (empty($user) || empty($pass))){throw new Exception("Cold wallet needs user and password");}
}
/*
* Creates or replaces the faucet configuration file
*
*/
function write_faucet_config($settings, $file){
//Clean up the format of some variables
$settings['use_promo_codes'] = $settings['use_promo_codes'] == true ? 'true' : 'false';
$settings['captcha'] = $settings['use_captcha'] == 'false' ? '' : $settings['use_captcha'];
$settings['use_captcha'] = $settings['use_captcha'] == 'false' ? 'false' : 'true';
$buffer = <<<EOF
<?php
// Modify these settings to suit your needs.
\$config = array(
"mysql_table_prefix" => TB_PRFX, // table prefix to use
"minimum_payout" => {$settings['minimum_payout']}, // minimum to be awarded
"maximum_payout" => {$settings['maximum_payout']}, // maximum to be awarded
"payout_threshold" => {$settings['payout_threshold']}, // payout threshold, if the faucet contains less than this, display the 'dry_faucet' message
"payout_interval" => "{$settings['payout_interval']}", // payout interval, the wait time for a user between payouts. Type any numerical value with either a "m" (minutes), "h" (hours), or "d" (days), attached. Examples: 50m for a 50 minute delay, 7h for a 7 hour delay, etc.
// this option has 3 possible values: "ip_address", "wallet_address", and "both". It defines what to check for when a user enters an address in order to decide whether or not to award to this user.
// "ip_address": checks the user IP address in the payout history.
// "wallet_address": checks the wallet address in the payout history.
// "both": check both the IP and wallet address in the payout history.
"user_check" => "{$settings['user_check']}",
"use_captcha" => {$settings['use_captcha']}, // require the user to enter a captcha
"captcha" => "{$settings['captcha']}", // which CAPTCHA to use, possible values are: "recaptcha", "solvemedia", and "simple-captcha".
"captcha_config" => array(
//Simple Captcha Session Name
"simple_captcha_session_name" => "{$settings['captcha_config']['simple_captcha_session_name']}",
// if you're using reCAPTCHA, enter your private and public keys here:
"recpatcha_private_key" => "{$settings['captcha_config']['recpatcha_private_key']}",
"recpatcha_public_key" => "{$settings['captcha_config']['recpatcha_public_key']}",
// if you're using Solve MEDIA, enter your private, challenge, and hash keys here:
"solvemedia_private_key" => "{$settings['captcha_config']['solvemedia_private_key']}",
"solvemedia_challenge_key" => "{$settings['captcha_config']['solvemedia_challenge_key']}",
"solvemedia_hash_key" => "{$settings['captcha_config']['solvemedia_hash_key']}",
),
// promo codes:
"use_promo_codes" => {$settings['use_promo_codes']}, // accept promo codes
// Donation address:
"donation_address" => "{$settings['donation_address']}", // donation address to display
// Faucet look and feel:
"title" => "{$settings['title']}", // page title, may be used by the template too
"sitename" => "{$settings['sitename']}", // page title, may be used by the template too
"sitedesc" => "{$settings['sitedesc']}", // page title, may be used by the template too
"coin_code" => "{$settings['coin_code']}",
"template" => "{$settings['template']}", // template to use (see the templates directory)
"lang" => "{$settings['lang']}",
);
?>
EOF;
if (!file_put_contents($file, $buffer)){throw new Exception("Could not wrtite configuration.", 2);}
//Validate some settings
//CAPTCHA - Yeah, it's complicated
//Normalize Variables
$settings['captcha_config']['solvemedia_private_key'] = $settings['captcha_config']['solvemedia_private_key'] == 'PRIVATE_KEY_HERE' ? '' : $settings['captcha_config']['solvemedia_private_key'];
$settings['captcha_config']['solvemedia_challenge_key'] = $settings['captcha_config']['solvemedia_challenge_key'] == 'CHALLENGE_KEY_HERE' ? '' : $settings['captcha_config']['solvemedia_challenge_key'];
$settings['captcha_config']['solvemedia_hash_key'] = $settings['captcha_config']['solvemedia_hash_key'] == 'HASH_KEY_HERE' ? '' : $settings['captcha_config']['solvemedia_hash_key'];
$settings['captcha_config']['recpatcha_private_key'] = $settings['captcha_config']['recpatcha_private_key'] == 'PRIVATE_KEY_HERE' ? '' : $settings['captcha_config']['recpatcha_private_key'];
$settings['captcha_config']['recpatcha_public_key'] = $settings['captcha_config']['recpatcha_public_key'] == 'PUBLIC_KEY_HERE' ? '' : $settings['captcha_config']['recpatcha_public_key'];
//Ensure we have the required settings
if(
//Solve Media
($settings['captcha'] == 'solvemedia' &&
(empty($settings['captcha_config']['solvemedia_private_key']) ||
empty($settings['captcha_config']['solvemedia_challenge_key']) ||
empty($settings['captcha_config']['solvemedia_hash_key']))
) ||
//re-CAPTCHA
($settings['captcha'] == 'recaptcha' &&
(empty($settings['captcha_config']['recpatcha_private_key']) ||
empty($settings['captcha_config']['recpatcha_public_key']))
) ||
//Simple Captcha
($settings['captcha'] == 'simple-captcha' &&
empty($settings['captcha_config']['simple_captcha_session_name'])
)
){
throw new Exception("CAPTACH configuration incomplete.", 3);
}
}
function check_wallet($type){
//Attempt to verify wallet
//Load Required Libraries (Yeah we need ALL this stuff)
require_once("classes/wallet.interface.php");
if($type == 'hot'){
//Load Hot Wallet Libraries
require_once("classes/jsonrpc.class.php");
require_once("classes/hotwallet.class.php");
}
else{
//Attempt to connect to database
include('classes/db.class.php');
include('includes/database.inc.php');
//Load Cold Wallet Libraries
require_once("classes/CoinAddressValidator.class.php");
require_once("classes/coldwallet.class.php");
}
//Create Wallet
include('includes/wallet.inc.php');
//Check by calling a simple test procedure
$PAYMENT_GATEWAY->test();
}
/*
* Creates required database tables
*
*/
function install_database_schema(){
//Attempt to connect to database
include('classes/db.class.php');
include('includes/database.inc.php');
//Build Queries
$queries[] = sprintf("CREATE TABLE `%spayouts` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`ip_address` VARCHAR(45) NOT NULL DEFAULT '',
`payout_amount` FLOAT NOT NULL,
`payout_address` VARCHAR(34) NOT NULL DEFAULT '',
`promo_code` VARCHAR(80) NOT NULL DEFAULT '',
`promo_payout_amount` FLOAT NOT NULL,
`txid` VARCHAR(80) NULL DEFAULT NULL,
`timestamp` DATETIME NOT NULL,
`lastupdate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;",
TB_PRFX);
$queries[] = sprintf("CREATE TABLE `%spromo_codes` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` VARCHAR(80) NOT NULL DEFAULT '',
`minimum_payout` FLOAT NOT NULL,
`maximum_payout` FLOAT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `unique_code` (`code`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;",
TB_PRFX);
//Run Queries
foreach($queries as $query){
$DB->query($query);
}
}
function finish_install($clean_install){
//Installation complete, create a touch file
if($clean_install){
if (!file_put_contents(APPLICATION_CONFDIR . '.install_complete', date('r'))){throw new Exception("Could not wrtite configuration.", 2);}
}
}
function create_datastore($path){
if(!is_dir($path)) {
mkdir($path, 0777);
}
}
function install_get_content($step){
$template = 'modules' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR;
$template .= 'install' . $step. '.php';
if(!is_file($template)) {
$template = '';
}
return $template;
}
function install_get_breadcrumb($step){
$page = 'modules' . DIRECTORY_SEPARATOR . 'install' . DIRECTORY_SEPARATOR;
$page .= "install-nav.tpl.php";
return $page;
}
function install_get_page_heading($step){
$heading = 'Step ';
switch($step){
case 2:
$heading .= '2 - Database Configuration.';
break;
case 3:
case 4:
$heading .= "$step - " . APPLICATION_NAME . ' Wallet Configuration.';
break;
case 5:
$heading .= "$step - " . APPLICATION_NAME . ' Configuration.';
break;
case 6:
$heading = APPLICATION_NAME . ' Installed.';
break;
default:
$heading = 'Welcome to ' . APPLICATION_NAME . '. ' . $heading;
$heading .= '1 - Legal Agreement.';
}
return $heading;
}
function install_get_page_title($step){
$title = APPLICATION_NAME . ' - Install';
if($step) {
$title .= " Step $step";
}
return $title;
}
function install_process_post_request($step, &$vars = array()){
$redirect_complete = "";
try{
switch($step){
case 2:
//Check if Config directory is writable
if(!@touch(APPLICATION_CONFDIR)){throw new Exception("Configuration directory is not writable", 2);}
break;
case 3:
//Database Setup
write_db_config($_POST['baseserver'],
$_POST['basename'],
$_POST['baseuser'],
$_POST['basepass'],
$_POST['tableprefix'],
APPLICATION_CONFDIR . 'db.conf.php'
);
if($vars['clean_install']){install_database_schema();}
break;
case 4:
//Wallet Setup
write_wallet_config($_POST['wallet_type'],
$_POST['rpcserver'],
$_POST['rpcport'],
$_POST['rpcuser'],
$_POST['rpcpass'],
$_POST['addressV'],
$_POST['coldwallet_file'],
$_POST['hotwallet_encrypt'],
APPLICATION_CONFDIR . 'wallet.conf.php'
);
//Check Connection/Permissions
check_wallet($_POST['wallet_type']);
break;
case 5:
//Basic Site Preferences
$settings = array(
'minimum_payout' => $_POST['minimum_payout'],
'maximum_payout' => $_POST['maximum_payout'],
'payout_threshold' => $_POST['payout_threshold'],
'payout_interval' => $_POST['payout_interval'],
'user_check' => $_POST['user_check'],
'use_captcha' => $_POST['captcha_type'],
'captcha_config' => array(
'simple_captcha_session_name' => $_POST['simple_captcha_session_name'],
'recpatcha_private_key' => $_POST['recpatcha_private_key'],
'recpatcha_public_key' => $_POST['recpatcha_public_key'],
'solvemedia_private_key' => $_POST['solvemedia_private_key'],
'solvemedia_challenge_key' => $_POST['solvemedia_challenge_key'],
'solvemedia_hash_key' => $_POST['solvemedia_hash_key']
),
'use_promo_codes' => isset($_POST['use_promo_codes']),
'donation_address' => $_POST['donation_address'],
'title' => $_POST['title'],
'sitename' => $_POST['sitename'],
'sitedesc' => $_POST['sitedesc'],
'template' => $_POST['template'],
'coin_code' => $_POST['coin_code'],
'lang' => $_POST['lang'],
);
//Save
write_faucet_config($settings, APPLICATION_CONFDIR . 'faucet.conf.php');
break;
case 6:
//Done Installing
if($_POST['install_complete']){finish_install(@$vars['clean_install']);}
break;
default:
//Do nothing
}
}
catch(Exception $e){
//Default error code is 1
$error = ($e->getCode() == 0) ? 1 : $e->getCode();
$step--;
//If this fails, the redirect back to previous step and specyfy error code
if($step <=1 ){
//Redirect back to first page
$redirect_complete = $_SERVER['PHP_SELF'] . "?error=$error";
}
else{
//Redirect to previous page
$redirect_complete = $_SERVER['PHP_SELF'] . "?step=$step&error=$error";
}
}
return $redirect_complete;
}
function install_process_get_request($step, &$vars = array()){
$redirect_complete = "";
//Check if this is a clean instalation or we only are reviewing/modifying the configuration, this prevents certain initalization functions
if (isset($_GET['_'])){
$vars['clean_install'] = ($_GET['_'] == 'conf') ? false : true;
}
else{
$vars['clean_install'] = true;
}
//Get any error flags
$vars['error'] = isset($_GET['error']) ? $_GET['error'] : "";
switch($step){
case 2:
//Load Existing database configuration if present
$vars['DB'] = install_get_db_config();
break;
case 3:
//Load Existing database configuration if present
$vars['WALLET'] = install_get_wallet_config();
break;
case 4:
//Load Existing database configuration if present
$vars['FAUCET'] = install_get_faucet_config();
break;
default:
//Check for valid step
$redirect_complete = install_get_content($step) ? "" : "index.php";
}
return $redirect_complete;
}
function install_get_db_config(){
$constantsBeforeInclude = getUserDefinedConstants();
@include(APPLICATION_CONFDIR . 'db.conf.php');
$constantsAfterInclude = getUserDefinedConstants();
return array_diff_assoc($constantsAfterInclude, $constantsBeforeInclude);
}
function install_get_wallet_config(){
$constantsBeforeInclude = getUserDefinedConstants();
@include(APPLICATION_CONFDIR . 'wallet.conf.php');
$constantsAfterInclude = getUserDefinedConstants();
return array_diff_assoc($constantsAfterInclude, $constantsBeforeInclude);
}
function install_get_faucet_config(){
$config = array();
$faucet = array('CAPTCHA' => array(),
'SETTINGS' => array(),
'TEMPLATE' => array(),
'CHECK' => array(),
);
//Default Values:
$faucet['SETTINGS'] = array(
"title" => "ZetaCoin Faucet",
"sitename" => "ZET Faucet",
"sitedesc" => "ZetaCoin",
"coin_code" => "ZET",
"minimum_payout" => 0.01,
"maximum_payout" => 10,
"payout_threshold" => 250,
"payout_interval" => "7h",
"donation_address" => "ZK6kdE5H5q7H6QRNRAuqLF6RrVD4cFbiNX",
);
/*
$captcha_config_default = array(
'simple_captcha_session_name' => 'multifaucet',
'recpatcha_private_key' => 'PRIVATE_KEY_HERE',
'recpatcha_public_key' => 'PUBLIC_KEY_HERE',
'solvemedia_private_key' => 'PRIVATE_KEY_HERE',
'solvemedia_challenge_key' => 'CHALLENGE_KEY_HERE',
'solvemedia_hash_key' => 'HASH_KEY_HERE',
)*/
//Load exisitng settings
@include(APPLICATION_CONFDIR . 'faucet.conf.php');
//Seprate out Captacha settings to it's own variable and merge with loaded values, set defaults if needed
$faucet['CAPTCHA']['use_captcha'] = isset($config['use_captcha']) ? $config['use_captcha'] : true;
$faucet['CAPTCHA']['captcha'] = isset($config['captcha']) ? $config['captcha'] : 'simple-captcha';
$faucet['CAPTCHA']['captcha_config'] = isset($config['captcha_config']) ? $config['captcha_config'] : array();
$faucet['CAPTCHA']['captcha_config']['simple_captcha_session_name'] = isset($config['captcha_config']['simple_captcha_session_name']) ? $config['captcha_config']['simple_captcha_session_name'] : 'multifaucet';
$faucet['CAPTCHA']['captcha_config']['recpatcha_private_key'] = isset($config['captcha_config']['recpatcha_private_key']) ? $config['captcha_config']['recpatcha_private_key'] : 'PRIVATE_KEY_HERE';
$faucet['CAPTCHA']['captcha_config']['recpatcha_public_key'] = isset($config['captcha_config']['recpatcha_public_key']) ? $config['captcha_config']['recpatcha_public_key'] : 'PUBLIC_KEY_HERE';
$faucet['CAPTCHA']['captcha_config']['solvemedia_private_key'] = isset($config['captcha_config']['solvemedia_private_key']) ? $config['captcha_config']['solvemedia_private_key'] : 'PRIVATE_KEY_HERE';
$faucet['CAPTCHA']['captcha_config']['solvemedia_challenge_key'] = isset($config['captcha_config']['solvemedia_challenge_key']) ? $config['captcha_config']['solvemedia_challenge_key'] : 'CHALLENGE_KEY_HERE';
$faucet['CAPTCHA']['captcha_config']['solvemedia_hash_key'] = isset($config['captcha_config']['solvemedia_hash_key']) ? $config['captcha_config']['solvemedia_hash_key'] : 'HASH_KEY_HERE';
//Seperate out only basic Settings by ignoring the other stuff
foreach($config as $key => $conf){
switch($key){
case 'mysql_table_prefix':
case 'use_captcha':
case 'captcha':
case 'captcha_config':
case 'use_promo_codes':
case 'wallet_passphrase':
case 'template':
case 'user_check':
break;
default:
//Merge Loaded Settings
$faucet['SETTINGS'][$key] = $conf;
}
}
//Theme - Default if not set
$faucet['TEMPLATE']['options'] = getAvaiableTemplates();
$faucet['TEMPLATE']['current'] = isset($config['template']) ? $config['template'] : 'default';
//User Check setup
$faucet['CHECK']['options'] = array('ip_address', 'wallet_address', 'both');
$faucet['CHECK']['current'] = isset($config['user_check']) ? $config['user_check'] : 'both';
//Language - Default if not set
$faucet['LANG']['options'] = getAvaiableLanguages();
$faucet['LANG']['current'] = isset($config['template']) ? $config['template'] : 'en';
//Other options
$faucet['PROMO'] = isset($config['use_promo_codes']) ? $config['use_promo_codes'] : true;
return $faucet;
}
function getAvaiableTemplates(){
$themes = array();
try{
//Read The theme directory contents:
if ($entries = scandir('themes')) {
foreach($entries as $entry) {
//Check for the 'page.tpl.php' file under each directory
if ($entry != "." &&
$entry != ".." &&
is_dir('themes' . DIRECTORY_SEPARATOR . $entry) &&
file_exists('themes' . DIRECTORY_SEPARATOR . $entry . DIRECTORY_SEPARATOR . 'page.tpl.php')) {
$themes[] = $entry;
}
}
}
}
catch(Exception $e){
//Upon failure just return a single default theme
$themes = array('default');
}
return $themes;
}
function getAvaiableLanguages(){
$langs = array();
try{
//Read The theme directory contents:
if ($entries = scandir('langs')) {
foreach($entries as $entry) {
//Check for the 'lang.php' file under each directory
if ($entry != "." &&
$entry != ".." &&
is_dir('langs' . DIRECTORY_SEPARATOR . $entry) &&
file_exists('langs' . DIRECTORY_SEPARATOR . $entry . DIRECTORY_SEPARATOR . 'lang.php')) {
$langs[] = $entry;
}
}
}
}
catch(Exception $e){
//Upon failure just return english
$langs = array('en');
}
return $langs;
}
function getLicense(){
return file_get_contents('LICENSE.md');
}
function getUserDefinedConstants() {
$constants = get_defined_constants(true);
return (isset($constants['user']) ? $constants['user'] : array());
}
?>