forked from gesman/bitcoin-payments-for-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bwwc-admin.php
467 lines (392 loc) · 20.2 KB
/
bwwc-admin.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
<?php
/*
Bitcoin SV Payments for WooCommerce
https://github.com/mboyd1/bitcoin-sv-payments-for-woocommerce
*/
// Include everything
include(dirname(__FILE__) . '/bwwc-include-all.php');
//===========================================================================
// Global vars.
global $g_BWWC__plugin_directory_url;
$g_BWWC__plugin_directory_url = plugins_url('', __FILE__);
global $g_BWWC__cron_script_url;
$g_BWWC__cron_script_url = $g_BWWC__plugin_directory_url . '/bwwc-cron.php';
//===========================================================================
//===========================================================================
// Global default settings
global $g_BWWC__config_defaults;
$g_BWWC__config_defaults = array(
// ------- Hidden constants
// 'supported_currencies_arr' => array ('USD', 'AUD', 'CAD', 'CHF', 'CNY', 'DKK', 'EUR', 'GBP', 'HKD', 'JPY', 'NZD', 'PLN', 'RUB', 'SEK', 'SGD', 'THB'), // Not used right now.
'database_schema_version' => 1.4,
'assigned_address_expires_in_mins' => 4*60, // 4 hours to pay for order and receive necessary number of confirmations.
'funds_received_value_expires_in_mins' => '5', // 'received_funds_checked_at' is fresh (considered to be a valid value) if it was last checked within 'funds_received_value_expires_in_mins' minutes.
'starting_index_for_new_btc_addresses' => '2', // Generate new addresses for the wallet starting from this index.
'max_blockchains_api_failures' => '3', // Return error after this number of sequential failed attempts to retrieve blockchain data.
'max_unusable_generated_addresses' => '20', // Return error after this number of unusable (non-empty) bitcoin addresses were sequentially generated
'blockchain_api_timeout_secs' => '20', // Connection and request timeouts for curl operations dealing with blockchain requests.
'exchange_rate_api_timeout_secs' => '10', // Connection and request timeouts for curl operations dealing with exchange rate API requests.
'soft_cron_job_schedule_name' => 'minutes_1', // WP cron job frequency
'delete_expired_unpaid_orders' => '1', // Automatically delete expired, unpaid orders from WooCommerce->Orders database
'reuse_expired_addresses' => '1', // True - may reduce anonymouty of store customers (someone may click/generate bunch of fake orders to list many addresses that in a future will be used by real customers).
// False - better anonymouty but may leave many addresses in wallet unused (and hence will require very high 'gap limit') due to many unpaid order clicks.
// In this case it is recommended to regenerate new wallet after 'gap limit' reaches 1000.
'max_unused_addresses_buffer' => 10, // Do not pre-generate more than these number of unused addresses. Pregeneration is done only by hard cron job or manually at plugin settings.
'cache_exchange_rates_for_minutes' => 10, // Cache exchange rate for that number of minutes without re-calling exchange rate API's.
// 'soft_cron_max_loops_per_run' => 2, // NOT USED. Check up to this number of assigned bitcoin addresses per soft cron run. Each loop involves number of DB queries as well as API query to blockchain - and this may slow down the site.
'elists' => array(),
'use_aggregated_api' => '0', // Use aggregated API to efficiently retrieve bitcoin address balance
// ------- General Settings
'license_key' => 'UNLICENSED',
'api_key' => substr(md5(microtime()), -16),
// New, ported from WooCommerce settings pages.
'service_provider' => 'electrum_wallet', // 'blockchain_info'
'electrum_mpk_saved' => '', // Saved, non-normalized value - MPK's separated by space / \n / ,
'electrum_mpks' => array(), // Normalized array of MPK's - derived from saved.
'confs_num' => '4', // number of confirmations required before accepting payment.
'exchange_rate_type' => 'vwap', // 'realtime', 'bestrate'.
'exchange_multiplier' => '1.00',
'delete_db_tables_on_uninstall' => '0',
'autocomplete_paid_orders' => '1',
'enable_soft_cron_job' => '1', // Enable "soft" Wordpress-driven cron jobs.
// New BSV settings
'selected_checkout_icon' => '/images/checkout-icons/orange-1.png',
//'checkout_icon_select' => '',
////////
// ------- Copy of $this->settings of 'BWWC_Bitcoin' class.
// DEPRECATED (only blockchain.info related settings still remain there.)
'gateway_settings' => array('confirmations' => 6),
// ------- Special settings
'exchange_rates' => array('EUR' => array('method|type' => array('time-last-checked' => 0, 'exchange_rate' => 1), 'GBP' => array())),
);
//===========================================================================
//===========================================================================
function BWWC__GetPluginNameVersionEdition($please_donate = true)
{
$return_data = '<h2 style="border-bottom:1px solid #DDD;padding-bottom:10px;margin-bottom:20px;">' .
BWWC_PLUGIN_NAME . ', version: <span style="color:#EE0000;">' .
BWWC_VERSION. '</span> [<span style="color:#EE0000;background-color:#FFFF77;"> ' .
BWWC_EDITION . ' </span> edition]' .
'</h2>';
return $return_data;
}
//===========================================================================
//===========================================================================
function BWWC__GetProUrl()
{
return 'http://google.com';
}
function BWWC__GetProLabel()
{
return '<span style="background-color:#FF4;color:#F44;border:1px solid #F44;padding:2px 6px;font-family:\'Open Sans\',sans-serif;font-size:14px;border-radius:6px;"><a href="' . BWWC__GetProUrl() . '">PRO Only</a></span>';
}
//===========================================================================
//===========================================================================
// These are coming from plugin-specific table.
function BWWC__get_persistent_settings($key=false)
{
////// PERSISTENT SETTINGS CURRENTLY UNUSED
return array();
//////
global $wpdb;
$persistent_settings_table_name = $wpdb->prefix . 'bwwc_persistent_settings';
$sql_query = "SELECT * FROM `$persistent_settings_table_name` WHERE `id` = '1';";
$row = $wpdb->get_row($sql_query, ARRAY_A);
if ($row) {
$settings = @unserialize($row['settings']);
if ($key) {
return $settings[$key];
} else {
return $settings;
}
} else {
return array();
}
}
//===========================================================================
//===========================================================================
function BWWC__update_persistent_settings($bwwc_use_these_settings_array=false)
{
////// PERSISTENT SETTINGS CURRENTLY UNUSED
return;
//////
global $wpdb;
$persistent_settings_table_name = $wpdb->prefix . 'bwwc_persistent_settings';
if (!$bwwc_use_these_settings) {
$bwwc_use_these_settings = array();
}
$db_ready_settings = BWWC__safe_string_escape(serialize($bwwc_use_these_settings_array));
$wpdb->update($persistent_settings_table_name, array('settings' => $db_ready_settings), array('id' => '1'), array('%s'));
}
//===========================================================================
//===========================================================================
// Wipe existing table's contents and recreate first record with all defaults.
function BWWC__reset_all_persistent_settings()
{
////// PERSISTENT SETTINGS CURRENTLY UNUSED
return;
//////
global $wpdb;
global $g_BWWC__config_defaults;
$persistent_settings_table_name = $wpdb->prefix . 'bwwc_persistent_settings';
$initial_settings = BWWC__safe_string_escape(serialize($g_BWWC__config_defaults));
$query = "TRUNCATE TABLE `$persistent_settings_table_name`;";
$wpdb->query($query);
$query = "INSERT INTO `$persistent_settings_table_name`
(`id`, `settings`)
VALUES
('1', '$initial_settings');";
$wpdb->query($query);
}
//===========================================================================
//===========================================================================
function BWWC__get_settings($key=false)
{
global $g_BWWC__plugin_directory_url;
global $g_BWWC__config_defaults;
$bwwc_settings = get_option(BWWC_SETTINGS_NAME);
if (!is_array($bwwc_settings)) {
$bwwc_settings = array();
}
if ($key) {
return (@$bwwc_settings[$key]);
} else {
return ($bwwc_settings);
}
}
//===========================================================================
//===========================================================================
function BWWC__update_settings($bwwc_use_these_settings=false, $also_update_persistent_settings=false)
{
if ($bwwc_use_these_settings) {
if ($also_update_persistent_settings) {
BWWC__update_persistent_settings($bwwc_use_these_settings);
}
update_option(BWWC_SETTINGS_NAME, $bwwc_use_these_settings);
return;
}
global $g_BWWC__config_defaults;
// Load current settings and overwrite them with whatever values are present on submitted form
$bwwc_settings = BWWC__get_settings();
foreach ($g_BWWC__config_defaults as $k=>$v) {
if (isset($_POST[$k])) {
if (!isset($bwwc_settings[$k])) {
$bwwc_settings[$k] = "";
} // Force set to something.
BWWC__update_individual_bwwc_setting($bwwc_settings[$k], $_POST[$k]);
}
// If not in POST - existing will be used.
}
//---------------------------------------
// Validation
//if ($bwwc_settings['aff_payout_percents3'] > 90)
// $bwwc_settings['aff_payout_percents3'] = "90";
//---------------------------------------
// ---------------------------------------
// Post-process variables.
// Array of MPK's. Single MPK = element with idx=0
$bwwc_settings['electrum_mpks'] = preg_split("/[\s,]+/", $bwwc_settings['electrum_mpk_saved']);
// ---------------------------------------
if ($also_update_persistent_settings) {
BWWC__update_persistent_settings($bwwc_settings);
}
update_option(BWWC_SETTINGS_NAME, $bwwc_settings);
}
//===========================================================================
//===========================================================================
// Takes care of recursive updating
function BWWC__update_individual_bwwc_setting(&$bwwc_current_setting, $bwwc_new_setting)
{
if (is_string($bwwc_new_setting)) {
$bwwc_current_setting = BWWC__stripslashes($bwwc_new_setting);
} elseif (is_array($bwwc_new_setting)) { // Note: new setting may not exist yet in current setting: curr[t5] - not set yet, while new[t5] set.
// Need to do recursive
foreach ($bwwc_new_setting as $k=>$v) {
if (!isset($bwwc_current_setting[$k])) {
$bwwc_current_setting[$k] = "";
} // If not set yet - force set it to something.
BWWC__update_individual_bwwc_setting($bwwc_current_setting[$k], $v);
}
} else {
$bwwc_current_setting = $bwwc_new_setting;
}
}
//===========================================================================
//===========================================================================
//
// Reset settings only for one screen
function BWWC__reset_partial_settings($also_reset_persistent_settings=false)
{
global $g_BWWC__config_defaults;
// Load current settings and overwrite ones that are present on submitted form with defaults
$bwwc_settings = BWWC__get_settings();
foreach ($_POST as $k=>$v) {
if (isset($g_BWWC__config_defaults[$k])) {
if (!isset($bwwc_settings[$k])) {
$bwwc_settings[$k] = "";
} // Force set to something.
BWWC__update_individual_bwwc_setting($bwwc_settings[$k], $g_BWWC__config_defaults[$k]);
}
}
update_option(BWWC_SETTINGS_NAME, $bwwc_settings);
if ($also_reset_persistent_settings) {
BWWC__update_persistent_settings($bwwc_settings);
}
}
//===========================================================================
//===========================================================================
function BWWC__reset_all_settings($also_reset_persistent_settings=false)
{
global $g_BWWC__config_defaults;
update_option(BWWC_SETTINGS_NAME, $g_BWWC__config_defaults);
if ($also_reset_persistent_settings) {
BWWC__reset_all_persistent_settings();
}
}
//===========================================================================
//===========================================================================
// Recursively strip slashes from all elements of multi-nested array
function BWWC__stripslashes(&$val)
{
if (is_string($val)) {
return (stripslashes($val));
}
if (!is_array($val)) {
return $val;
}
foreach ($val as $k=>$v) {
$val[$k] = BWWC__stripslashes($v);
}
return $val;
}
//===========================================================================
//===========================================================================
/*
----------------------------------
: Table 'btc_addresses' :
----------------------------------
status "unused" - never been used address with last known zero balance
"assigned" - order was placed and this address was assigned for payment
"revalidate" - assigned/expired, unused or unknown address suddenly got non-zero balance in it. Revalidate it for possible late order payment against meta_data.
"used" - order was placed and this address and payment in full was received. Address will not be used again.
"xused" - address was used (touched with funds) by unknown entity outside of this application. No metadata is present for this address, will not be able to correlated it with any order.
"unknown" - new address was generated but cannot retrieve balance due to blockchain API failure.
*/
function BWWC__create_database_tables($bwwc_settings)
{
global $wpdb;
$bwwc_settings = BWWC__get_settings();
$must_update_settings = false;
///$persistent_settings_table_name = $wpdb->prefix . 'bwwc_persistent_settings';
///$electrum_wallets_table_name = $wpdb->prefix . 'bwwc_electrum_wallets';
$btc_addresses_table_name = $wpdb->prefix . 'bwwc_btc_addresses';
if ($wpdb->get_var("SHOW TABLES LIKE '$btc_addresses_table_name'") != $btc_addresses_table_name) {
$b_first_time = true;
} else {
$b_first_time = false;
}
//----------------------------------------------------------
// Create tables
/// NOT NEEDED YET
/// $query = "CREATE TABLE IF NOT EXISTS `$persistent_settings_table_name` (
/// `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
/// `settings` text,
/// PRIMARY KEY (`id`)
/// );";
/// $wpdb->query ($query);
/// $query = "CREATE TABLE IF NOT EXISTS `$electrum_wallets_table_name` (
/// `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
/// `master_public_key` varchar(255) NOT NULL,
/// PRIMARY KEY (`id`),
/// UNIQUE KEY `master_public_key` (`master_public_key`)
/// );";
/// $wpdb->query ($query);
$query = "CREATE TABLE IF NOT EXISTS `$btc_addresses_table_name` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`btc_address` char(36) NOT NULL,
`origin_id` char(128) NOT NULL DEFAULT '',
`index_in_wallet` bigint(20) NOT NULL DEFAULT '0',
`status` char(16) NOT NULL DEFAULT 'unknown',
`last_assigned_to_ip` char(16) NOT NULL DEFAULT '0.0.0.0',
`assigned_at` bigint(20) NOT NULL DEFAULT '0',
`total_received_funds` DECIMAL( 16, 8 ) NOT NULL DEFAULT '0.00000000',
`received_funds_checked_at` bigint(20) NOT NULL DEFAULT '0',
`address_meta` MEDIUMBLOB NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `btc_address` (`btc_address`),
KEY `index_in_wallet` (`index_in_wallet`),
KEY `origin_id` (`origin_id`),
KEY `status` (`status`)
);";
$wpdb->query($query);
//----------------------------------------------------------
// upgrade bwwc_btc_addresses table, add additional indexes
if (!$b_first_time) {
$version = floatval($bwwc_settings['database_schema_version']);
if ($version < 1.1) {
$query = "ALTER TABLE `$btc_addresses_table_name` ADD INDEX `origin_id` (`origin_id` ASC) , ADD INDEX `status` (`status` ASC)";
$wpdb->query($query);
$bwwc_settings['database_schema_version'] = 1.1;
$must_update_settings = true;
}
if ($version < 1.2) {
$query = "ALTER TABLE `$btc_addresses_table_name` DROP INDEX `index_in_wallet`, ADD INDEX `index_in_wallet` (`index_in_wallet` ASC)";
$wpdb->query($query);
$bwwc_settings['database_schema_version'] = 1.2;
$must_update_settings = true;
}
if ($version < 1.3) {
$query = "ALTER TABLE `$btc_addresses_table_name` CHANGE COLUMN `origin_id` `origin_id` char(128)";
$wpdb->query($query);
$bwwc_settings['database_schema_version'] = 1.3;
$must_update_settings = true;
$mpk = @$bwwc_settings['gateway_settings']['electrum_master_public_key'];
if ($mpk) {
// Replace hashed values of MPK in DB with real MPK values.
$mpk_old_value = 'electrum.mpk.' . md5($mpk);
// UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0;
// UPDATE [table_name] SET [field_name] = REPLACE([field_name], "foo", "bar");
$query = "UPDATE `$btc_addresses_table_name` SET `origin_id` = '$mpk' WHERE `origin_id` = '$mpk_old_value'";
$wpdb->query($query);
// Copy settings from old location to new, if new is empty.
if (!@$bwwc_settings['electrum_mpk_saved']) {
$bwwc_settings['electrum_mpk_saved'] = $mpk;
// 'BWWC__update_settings()' will populate $bwwc_settings['electrum_mpks'].
}
}
}
if ($version < 1.4) {
$query = "ALTER TABLE `$btc_addresses_table_name` MODIFY `address_meta` MEDIUMBLOB";
$wpdb->query($query);
$bwwc_settings['database_schema_version'] = 1.4;
$must_update_settings = true;
}
}
if ($must_update_settings) {
BWWC__update_settings($bwwc_settings);
}
//----------------------------------------------------------
// Seed DB tables with initial set of data
/* PERSISTENT SETTINGS CURRENTLY UNUNSED
if ($b_first_time || !is_array(BWWC__get_persistent_settings()))
{
// Wipes table and then creates first record and populate it with defaults
BWWC__reset_all_persistent_settings();
}
*/
//----------------------------------------------------------
}
//===========================================================================
//===========================================================================
// NOTE: Irreversibly deletes all plugin tables and data
function BWWC__delete_database_tables()
{
global $wpdb;
///$persistent_settings_table_name = $wpdb->prefix . 'bwwc_persistent_settings';
///$electrum_wallets_table_name = $wpdb->prefix . 'bwwc_electrum_wallets';
$btc_addresses_table_name = $wpdb->prefix . 'bwwc_btc_addresses';
///$wpdb->query("DROP TABLE IF EXISTS `$persistent_settings_table_name`");
///$wpdb->query("DROP TABLE IF EXISTS `$electrum_wallets_table_name`");
$wpdb->query("DROP TABLE IF EXISTS `$btc_addresses_table_name`");
}
//===========================================================================