-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscriptbill.php
4819 lines (4145 loc) · 210 KB
/
scriptbill.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Scriptbill Cryptonote
* Plugin URI: https://scriptbank.com.ng
* Description: A Cryptocurrency Based On Cryptonote Technology.
* Version: 0.0.1
* Tags: point, credit, loyalty program, engagement, reward, woocommerce rewards, Cryptocurrency, decentralization, custom credit, custom stocks, investment, auto investment, real time dividends, buy and earn, profit sharing, loans, unlimited loans, unlimited investment, one click loan, marketing, marketing strategy, subscription, trading, crediting, credit trading, stock trading, exchange market, bond trading, forex trading, buddypress, woocommerce, mycred
* Author: Scriptbank
* Author URI: https://scriptbank.com.ng
* Author Email: admin@scriptbank.com.ng
* Requires at least: WP 4.8
* Tested up to: WP 6.0
* Text Domain: scriptbill
* Domain Path: /lang
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if( ! class_exists( 'Scriptbill' ) ){
class Scriptbill {
//plugin version
public $version = '0.0.1';
private $default_note = array();
// Instnace
protected static $_instance = NULL;
//whether or not we are registering a Business Manager note
public static $isBusinessManagerNote = false;
//whether or not the current user is a business manager
public static $isBusinessManager = false;
public static function instance(){
if( is_null( self::$_instance ) ){
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Not allowed
* @since 0.1
* @version 0.1
*/
public function __clone() { _doing_it_wrong( __FUNCTION__, 'Cheatin’ huh?', '0.0.1' ); }
/**
* Not allowed
* @since 0.1
* @version 0.1
*/
public function __wakeup() { _doing_it_wrong( __FUNCTION__, 'Cheatin’ huh?', '0.0.1' ); }
public function __construct(){
if( ! function_exists('mycred') ) {
add_action( 'admin_notices', array( $this, 'scriptbill_notices__mycred_error' ) );
} else {
add_action( 'admin_notices', array( $this, 'scriptbill_notices__mycred_success' ) );
}
$defaults = array(
'walletID' => '',//this is the unique wallet ID of the user, this data is used to cryptographically link all the account of a particular user to a wallet. So that it can be recognized everywhere it is found.
'noteID' => '0000',//this a unique nonce of the note, it increase everytime there is a transaction using this note
'noteAddress' => '', //this is the public key of the note, it is used to encrypt data that should be read by this note alone.
'noteSecret' => '', //this is the private key of the note, used by the note owner to recieve funds sent to this note address. If you think your note Secret is compromized, you can recreate it and change your note address. To aviod it not affecting business, you can use the connectedNote parameter to link your new note to the compromized note and maintain the security on your note.
'noteKey' => '', //this is the increment value of the nonce above, this means the noteKey divided by the current note ID should give us the total transaction done by this note.
'noteValue' => 0, //this is the value of the note.
'noteType' => 'SBCRD',//this is the unique code of the note you are using, anything that changes this note type would change the transactional value of the note to the new note type.
'transValue' => 0, //this is the last transaction value of the note.
'transTime' => 0, //this is the last time stamp of the transaction on the note.
'transType' => 'CREATE',//this is the type of transaction conducted by the note.
'transHash' => '',//this is half of the hash of the last transaction block this note created. To create a new transaction block, then this note must verify this hash.
'transKey' => '', //this is the private key of the last bock produced by this note.
'profitKeys' => [],//this is the private key of the product block held by the note for profit sharing.
'noteServer' => "https://".$_SERVER['HTTP_HOST'], //the server where the note is hosted. The note will be found of sending request to this server to connect to the network.
'noteHash' => '',//this is the last hash value of the note
'noteSign' => '', //this is the signature built using the noteHash and the secret of the note.
'noteSubs' => [], //this is an array of the total subsription on this note
'noteBudgets' => [], //this is an array of the total budget on this note.
'agreements' => [], //this is an array of private keys of agreements held by this note.
'blockKey' => '',//this is the private key to the note's current block. Used to verify that the note actually signed the block it created.
'blockHash' => '',//this is the hash of the total hash of the transaction block concerned. This is required to verify if the note created the transaction block it is processing wiith.
'BMKey' => '', //this is the note address of the business manager that controls this note network.
);
$this->default_note = $defaults;
//first we create theScriptbill database tables we will use to query Scriptbill Transactional data.
//$this->createTables();
$this->define();
$this->actions();
$this->filters();
$this->add_pages();
}
public function define(){
define('SCRIPTBANK_URL', 'https://scriptbank.com.ng');
define('SCRIPTBANK_BIZ_URL', SCRIPTBANK_URL . '?businessManagerQuery=TRUE');
define('SCRIPTBANK_BIZ_MANAGER', $this->get_site_business_manager_id());
}
public function actions(){
//register_activation_hook( __FILE__, array( $this, 'add_pages' ) );
add_action( 'init', array( $this, 'recieve_data' ) );
add_action('init', array( $this, 'check_user_online_status' ) );
//add_action( 'init', array( $this, 'recieve_user_block' ) );
//add_action( 'mycred_update_user_balance', array( $this, 'check_balance_updates' ), 20, 4 );
add_action( 'user_register', array( $this, 'create_new_note' ) );
//add_action( 'init', array( $this, 'login_user_note' ) );
add_action( 'wp_footer', array( $this, 'get_current_user_note' ) );
add_action( 'init', array( $this, 'get_current_user_note_init' ) );
add_action( 'init', array( $this, 'authenticate_user_login' ) );
//add_action( 'login_form', array( $this, 'upload_user_note' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scriptbill_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scriptbill_scripts' ) );
add_action( 'login_enqueue_scripts', array( $this, 'enqueue_scriptbill_scripts' ) );
add_action( 'init', array( $this, 'share_blocks' ) );
//add_action( 'register_form', array( $this, 'add_scriptbill_wallet_address' ) );
//add_action( 'woocommerce_product_meta_start', array( $this, 'check_product_meta' ) );
add_action( 'admin_menu', array( $this, 'add_admin_pages' ) );
add_action( 'init', array( $this, 'add_scriptbill_ranks' ) );
add_action( 'woocommerce_product_options_general_product_data', array($this, 'add_woocommerce_fields') );
add_action('woocommerce_after_add_to_cart_button', array( $this, 'invest' ), 50);
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
add_action('woocommerce_thankyou', array( $this, 'process_woocommerce_payment' ), 20);
add_action( 'woocommerce_process_product_meta', array( $this, 'save_budget_woocommerce' ), 20 );
add_action( 'wp_logout', array( $this, 'clear_scriptbill_keys' ) );
add_action('woocommerce_checkout_process', array( $this, 'process_scriptbill_payment'), 20 );
add_action( 'woocommerce_checkout_update_order_meta', array( $this,'scriptbill_payment_update_order_meta' ), 20 );
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this,'scriptbill_checkout_field_display_admin_order_meta' ), 20, 1 );
//adding shortcodes
add_shortcode( 'scriptbill_transaction_confirmation', array( $this, 'scriptbill_transaction_confirmation' ) );
add_shortcode('note_management', array( $this, 'manage_scriptbill_note' ));
}
public function filters(){
//add_filter('mycred_add', array( $this, 'check_add_data' ), 20, 3 );
add_filter( 'auth_cookie_expiration', array( $this, 'check_user_session_expiration' ), 99, 3 );
//add_filter('wp_authenticate_user', array( $this, 'authenticate_user_login' ), 20, 2 );
add_filter( 'pre_user_login', array( $this, 'add_wallet_id_to_username' ), 20, 1 );
add_filter('mycred_buy_args', array($this, 'check_Scriptbill_buy_user'), 20, 3 );
add_filter( 'woocommerce_payment_gateways', array( $this, 'scriptbill_gateway_class' ) );
add_filter( 'woocommerce_currency_symbol', array( $this, 'add_scriptbill_currency_symbol' ), 10, 2 );
add_filter( 'woocommerce_currencies', array( $this, 'add_scriptbill_currency' ) );
}
public function add_pages(){
$buy_page_title = "Scriptbill Buy";
$exchange_page = $this->post_exists( $buy_page_title );
$args = array();
if( ! $exchange_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $buy_page_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($buy_page_title)));
$args['post_content'] = '[scriptbill_buy_form]';
$args['post_type'] = 'page';
$args['post_status'] = 'publsh';
$exchange_page = wp_insert_post( $args );
}
//next we get the Sell Scriptbills Page. [mycred_cashcred]
$sell_page_title = "Sell Scriptbills";
$sell_page = $this->post_exists( $sell_page_title );
if( ! $sell_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $sell_page_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($sell_page_title)));
$args['post_content'] = '[scriptbill_cashcred]';
$args['post_type'] = 'page';
$args['post_status'] = 'publish';
$sell_page = wp_insert_post( $args );
}
$transfer_page_title = "Transfer Scriptbills";
$transfer_page = $this->post_exists( $transfer_page_title );
if( ! $transfer_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $transfer_page_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($transfer_page_title)));
$args['post_content'] = '[scriptbill_transfer]';
$args['post_type'] = 'page';
$args['post_status'] = 'publish';
$transfer_page = wp_insert_post( $args );
}
$balance_page_title = "My Scriptbill Balance";
$balance_page = $this->post_exists( $balance_page_title );
if( ! $balance_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $balance_page_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($balance_page_title)));
$args['post_content'] = '[scriptbill_my_balance]';
$args['post_type'] = 'page';
$args['post_status'] = 'publish';
$balance_page = wp_insert_post( $args );
}
$transaction_con_title = "Scriptbill Transaction Confirmation";
$trans_con_page = $this->post_exists( $transaction_con_title );
if( ! $trans_con_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $transaction_con_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($transaction_con_title)));
$args['post_content'] = '[scriptbill_transaction_confirmation]';
$args['post_type'] = 'page';
$args['post_status'] = 'publish';
$trans_con_page = wp_insert_post( $args );
}
$note_management_title = "Scriptbill Note Management";
$note_manage_page = $this->post_exists( $note_management_title );
if( ! $note_manage_page ) {
$args['comment_status'] = 'close';
$args['ping_status'] = 'close';
$args['post_author'] = 1;
$args['post_title'] = $note_management_title;
$args['post_name'] = strtolower(str_replace(' ', '-', trim($note_management_title)));
$args['post_content'] = '[note_management]';
$args['post_type'] = 'page';
$args['post_status'] = 'publish';
$note_manage_page = wp_insert_post( $args );
}
}
public function enqueue_scriptbill_scripts(){
global $site_types;//kxi\Naf.
wp_enqueue_script('jsencrypt', plugin_dir_url( __FILE__ ) . 'js/jsencrypt.min.js', array(), '1.0.0');
wp_enqueue_script('cryptojs', plugin_dir_url( __FILE__ ) . 'js/crypto-js.js', array(), '1.0.0');
wp_enqueue_script('scriptbillScripts', plugin_dir_url( __FILE__ ) . 'js/scriptbill.js', array('cryptojs', 'jsencrypt'), '1.0.1');
wp_enqueue_script('scriptBallin', plugin_dir_url( __FILE__ ) . 'js/toRecieve.js', array('scriptbillScripts'), '1.0.1', true);
wp_enqueue_script('ScriptbillLogin', plugin_dir_url( __FILE__ ) . 'js/checkLogin.js', array('scriptbillScripts', 'cryptojs', 'scriptBallin'), '1.0.1', true);
wp_enqueue_style('scriptstyles', plugin_dir_url(__FILE__) . 'css/css.css');
//wp_enqueue_script('buyScriptCrypto', plugin_dir_url( __FILE__ ) . 'js/buycred-crypto.js', array('scriptbillScripts', 'cryptojs', 'scriptBallin'), '1.0.0', true);
//wp_enqueue_script('buyScriptPayPal', plugin_dir_url( __FILE__ ) . 'js/buycred-paypal.js', array('scriptbillScripts', 'cryptojs', 'scriptBallin'), '1.0.0', true);
$user = wp_get_current_user();
$user_id = $user->ID;
$blocks = get_user_meta( $user_id , 'to_recieve_blocks' );
$noteAddress = get_user_meta( $user_id, 'user_scriptbill_note', true );
$walletID = get_user_meta( $user_id, 'user_scriptbill_wallet', true );
if( $blocks ){
delete_user_meta( $user_id, 'to_recieve_blocks' );
}
$nonce = wp_create_nonce('scriptbill-nonce');
$invest_rate = get_site_option( 'siteInvestSharingRate' );
$profit_rate = get_site_option( 'siteProfitSharingRate' );
$businessMWallet = get_site_option( 'businessManagerWallet' );
$businessMNote = get_site_option( 'businessManagerNote' );
$credit_type = get_site_option( 'site_credit_type' );
$credit_abbr = get_site_option( 'site_credit_type' );
$credit_pref = get_site_option( 'site_credit_prefix' );
$site_budget = get_site_option( 'scriptbill_site_budget' );
$uploadedNote = get_user_meta( $user->ID, 'current_user_uploaded_note', true );
$block = get_site_option("current_block");
$currentNote = get_user_meta( $user->ID, 'current_scriptbill_note', true );
$currentKey = get_user_meta( $user->ID, 'current_scriptbill_key', true );
wp_localize_script(
'scriptBallin',
'local',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => $nonce,
'toRecieveBlocks' => json_encode( $blocks ),
'noteAddress' => $noteAddress,
'walletID' => $walletID,
'user_pass' => $user->user_pass,
'user_id' => $user->ID,
'invest_rate' => $invest_rate,
'profit_rate' => $profit_rate,
'BM_KEY' => $businessMWallet,
'site_credit' => $credit_type,
'credit_type' => $credit_abbr,
'credit_pref' => $credit_pref,
'uploadedNote' => $uploadedNote,
'BM_Note' => $businessMNote,
'site_budget' => json_encode( $site_budget ),
'currentBlock' => json_encode($block),
'currentNote' => $currentNote,
'currentKey' => $currentKey,
'site_credits' => json_encode( $site_types )
)
);
}
public function share_blocks(){
global $wpdb;
//get blocks from the database.
$blocks = $this->get_blocks('latest', 100);
if( $blocks ){
$block_servers = get_site_option("scriptbill_servers");
if( ! empty( $block_servers ) ){
$block_servers = unserialize( $block_servers );
}
else {
$block_servers = array("https://scriptbank.ml", "https://dev-cmbf-bank.pantheonsite.io");
}
foreach( $block_servers as $server ){
foreach($blocks as $block){
if( ! in_array( $block['noteServer'], $block_servers ) ){
array_push( $block_servers, $block['noteServer'] );
}
if( ! empty( $block['budgetID'] ) ){
$Tblock = $this->get_block( $block['budgetID'], "budgetID", "budget");
if( ! empty( $Tblock ) || $Tblock !== false )
$block['agreement'] = $Tblock;
}
if( ! empty( $block['productID'] ) ){
$Tblock = $this->get_block( $block['productID'], "productID", "product");
if( ! empty( $Tblock ) || $Tblock !== false )
$block['agreement'] = $Tblock;
}
if( ! empty( $block['agreements'] ) ) {
$agreements = unserialize( $block['agreements'] );
if( count( $agreements ) > 0 ) {
$block['agreements'] = array();
foreach( $agreements as $agreeID ){
$agreement = $this->get_block( $agreeID, "agreeID", "agreement" );
if( ! empty( $agreement ) || $agreement !== false ) {
array_push( $block['agreements'], $agreement );
}
}
}
}
$blockData = json_encode( $block );
$url = add_query_arg( 'blockData', $blockData, $server );
$args = array(
"method" => "post",
"body" => array( "blockData" => $blockData ),
);
wp_remote_post( $server, $args );
}
}
update_site_option( "scriptbill_servers", serialize( $block_servers ) );
}
}
public function get_current_user_note(){
?>
<script type="text/javascript">
if( sessionStorage && sessionStorage.currentNote ){
let url = new URL( window.location.origin );
url.searchParams.set( 'noteString', sessionStorage.currentNote );
fetch(url);
}
</script>
<?php
}
public function get_current_user_note_init(){
$user = wp_get_current_user();
$user_id = $user->ID;
if( isset( $_GET['noteString'] ) ){
$note = json_decode( $_GET['noteString'] );
if( isset( $note->noteAddress ) ){
$file_name = ABSPATH . $user->user_login . '.script';
touch( $file_name );
$string = $this->encrypt( $_GET['noteString'], $user->user_pass );
file_put_contents( $file_name, $string );
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_name));
flush(); // Flush system output buffer
readfile($file_name);
exit;
if( get_uset_meta( $user_id, 'user_scriptbill_note', true ) == $note->noteAddress ) return;
update_user_meta( $user_id, 'user_scriptbill_note', $note->noteAddress );
update_user_meta( $user_id, 'user_scriptbill_wallet', $note->walletID );
}
}
}
public function authenticate_user_login(){
global $site_types;
if( is_null( $site_types ) ) {
$site_types = array();
}
$user = wp_get_current_user();
$nonce = null;
$dirname = ABSPATH . 'auth/';
if( ! is_dir( $dirname ) ){
mkdir( $dirname );
}
$filename = $dirname . $user->user_login . '.txt';
if( ! file_exists( $filename ) ) {
touch( $filename );
}
if( isset( $_POST['ajax_nonce'] ) ){
$nonce = $_POST['ajax_nonce'];
}
elseif( isset( $_GET['ajax_nonce'] ) ){
$nonce = $_GET['ajax_nonce'];
}
file_put_contents( $filename, 'request sent for '. $user->user_login . ' nonce gotten: ' . $nonce . ' at time: ' . time(), FILE_APPEND );
if( ! wp_verify_nonce( $nonce, 'scriptbill-nonce' ) ) return;
if( isset( $_GET['items'] ) ) {
$items = str_replace('\\', '', $_GET['items'] );
$item = json_decode( $items );
$budgets = get_site_option("scriptbill_site_budget");
if( ! $item ){
echo json_encode( array( 'done' => false, 'item_found' => $item, 'getable' => $_GET['items'], 'decoded' => urldecode( $_GET['items'] ), 'tried' => 'no' ) );
exit;
}
if( ! $budgets )
$budgets = array();
if( ! $budgets['budgetItems'] )
$budgets['budgetItems'] = array();
if( $item->itemID == '' ){
$item->itemID = strval( time() );
}
$budgets['budgetItems'][ $item->itemID ] = $item;
update_site_option( "scriptbill_site_budget", $budgets );
echo json_encode( array('done' => true, 'itemID' => $item->itemID, 'item_string' => $items ) );
exit;
}
if( isset( $_GET['buy_transaction_id'] ) && $user ) {
update_user_meta( $user->ID, 'buy_cred_transaction_id', esc_attr( $_GET['buy_transaction_id'] ) );
}
if( isset( $_GET['userSearchID'] ) ){
$userSearch = esc_attr( $_GET['userSearchID'] );
$sql = "SELECT * FROM {$wpdb->prefix}users WHERE user_login LIKE '%%{$userSearch}%%'";
$result = $wpdb->get_row( $sql, ARRAY_A );
$echo = array();
if( $result ){
$echo['userID'] = $result['ID'];
$echo['success'] = true;
$echo['userName'] = $result['user_login'];
echo json_encode( $echo );
exit;
}
else {
$echo['error'] = true;
echo json_encode( $echo );
}
}
if( isset( $_GET['prodSearch'] ) ) {
$products = $this->post_search( esc_attr( $_GET['prodSearch'] ), 'product' );
if( $products ){
$product_data = array();
foreach( $products as $product ){
$product_data[ strval( $product->ID ) ] = $product->post_title;
}
echo json_encode( $product_data );
exit;
}
else {
echo 'NOT FOUND';
exit;
}
}
if( isset( $_GET['site_budget'] ) ){
$budget = json_decode( esc_attr( $_GET['site_budget'] ) );
if( $budget ){
update_site_option("scriptbill_site_budget", $budget);
echo json_encode( array( 'budgetSet' => 'TRUE' ) );
exit;
}
}
if( isset( $_GET['logoutUser'] ) && $_GET['logoutUser'] == 'TRUE' ){
$uploadedNote = get_user_meta( $user->ID, 'current_user_uploaded_note', true );
if( $uploadedNote )
delete_user_meta( $user->ID, 'current_user_uploaded_note' );
echo json_encode( array('loggedout' => 'true') );
wp_logout();
exit;
}
if( isset( $_GET['businessManagerNote'] ) ){
$BM_Note = esc_attr( $_GET['businessManagerNote'] );
$BM_Note = json_decode( $BM_Note );
$BM_Wallet = get_site_option( 'businessManagerWallet' );
if( $BM_Note && $BM_Note->noteAddress && $BM_Note->walletID == $BM_Wallet ){
update_site_option( 'businessManagerNote', json_encode( $BM_Note ) );
echo json_encode(array('set' => 'true'));
exit;
}
}
if( isset( $_GET['uploadedNote'] ) ){
update_user_meta( $user->ID, 'current_user_uploaded_note', esc_attr( $_GET['uploadedNote'] ) );
echo json_encode(array('set' => 'true'));
exit;
}
if( isset( $_GET['exValue'] ) ){
$exPrefs = get_site_option("mycred_pref_buycreds");
if( isset( $_GET['exCred'] ) ) {
if( $exPrefs['gateway_prefs']['bank']['currency'] != esc_attr( $_GET['exCred'] ) )
$exPrefs['gateway_prefs']['bank']['currency'] = esc_attr( $_GET['exCred'] );
}
$exPrefs['gateway_prefs']['bank']['exchange']['scriptbill'] = floatval( $_GET['exValue'] );
update_site_option( "mycred_pref_buycreds", $exPrefs );
}
if( isset( $_GET['check_credit'] ) ){
$credit = esc_attr( $_GET['check_credit'] );
if( $credit == 'mycred_default' || $credit == 'sbcrd' ){
echo json_encode( array( 'success' => 'true' ) );
exit;
}
$credit = $this->credit_exists( strtoupper( $credit ) );
if( $credit ){
echo json_encode( array( 'success' => 'true' ) );
exit;
}
else {
echo json_encode( array( 'error' => 'true' ) );
exit;
}
}
if( isset( $_GET['search'] ) && isset( $_GET['productID'] ) ) {
$productID = absint( $_GET['productID'] );
$search = esc_attr( $_GET['search'] );
if( $search == 'value'){
$price = get_post_meta( $productID, '_regular_price', true );
echo json_encode( array( 'value' => $price ) );
exit;
}
}
if( isset( $_GET['productID'] ) && isset( $_GET['productValue'] ) ){
$productID = absint( $_GET['productID'] );
$sale_value = floatval( $_GET['productValue'] );
if( $sale_value ){
update_post_meta( $productID, '_regular_price', $sale_value );
echo json_encode( array('done' => 'TRUE') );
exit;
}
}
if( isset( $_GET['noteValue'] ) && isset( $_GET['noteType'] ) ) {
//$mycred = mycred();
$key = strtolower(esc_attr( $_GET['noteType'] ) );
if( array_key_exists( $key, $site_types ) ){
//$this->set_users_balance( $user->ID, floatval( $_GET['noteValue'] ), $key );
update_user_meta( $user->ID, $key, floatval( $_GET['noteValue'] ) );
}
else {
//$this->set_users_balance( $user->ID, floatval( $_GET['noteValue'] ) );
update_user_meta( $user->ID, 'scriptbill_balance', floatval( $_GET['noteValue'] ) );
}
update_user_meta( $user->ID, 'user_scriptbill_note_type', esc_attr( $_GET['noteType'] ) );
/* update_user_meta( $user->ID, 'current_block_id', esc_attr( $_GET['noteBlockID'] ) );
update_user_meta( $user->ID, 'user_scriptbill_note', esc_attr( $_GET['noteAddress'] ) );
update_user_meta( $user->ID, 'user_scriptbill_wallet', esc_attr( $_GET['noteWallet'] ) ); */
if( isset( $_GET['noteBlockID'] ) ) {
update_user_meta( $user->ID, 'current_block_id', esc_attr( $_GET['noteBlockID'] ) );
}
if( isset( $_GET['noteAddress'] ) ) {
update_user_meta( $user->ID, 'user_scriptbill_note', esc_attr( $_GET['noteAddress'] ) );
}
if( isset( $_GET['noteWallet'] ) ) {
update_user_meta( $user->ID, 'user_scriptbill_wallet', esc_attr( $_GET['noteWallet'] ) );
}
echo json_encode(array('set' => 'true', 'balance' => 'updated'));
exit;
}
if( isset( $_GET['noteID'] ) ){
update_user_meta( $user->ID, 'user_scriptbill_ID', esc_attr( $_GET['noteID'] ) );
echo json_encode(array('set' => 'true'));
exit;
}
if( isset( $_GET['budgetID'] ) ){
$result = $this->get_block( esc_attr( $_GET['budgetID'], 'budgetID' ) );
if( $result && is_array( $result ) && isset( $result['blockID'] ) ){
echo json_encode( $result );
exit;
}
else {
echo json_encode( array( 'error' => 'NO BLOCK FOUND ASSOCIATED WITH ' . $_GET['budgetID'] ) );
exit;
}
}
if( isset( $_GET['productBlockID'] ) ){
$result = $this->get_block( esc_attr( $_GET['productBlockID'], 'productID' ) );
if( $result && is_array( $result ) && isset( $result['blockID'] ) ){
echo json_encode( $result );
exit;
}
else {
echo json_encode( array( 'error' => 'NO BLOCK FOUND ASSOCIATED WITH ' . $_GET['productBlockID'] ) );
exit;
}
}
if( isset( $_GET['walletHASH'] ) ){
$result = $this->get_block( esc_attr( $_GET['walletHASH'], 'walletID' ) );
if( $result && is_array( $result ) && isset( $result['blockID'] ) ){
echo json_encode( $result );
exit;
}
else {
echo json_encode( array( 'error' => 'NO BLOCK FOUND ASSOCIATED WITH ' . $_GET['walletHASH'] ) );
exit;
}
}
}
public function get_block( $block_id, $type = 'blockID', $table = "block", $keys = array() ){
if( $table == "block" )
$table = "scriptTransactions";
else if( $table == "budget" )
$table = "scriptBudgets";
else if( $table == "exchange" )
$table = "scriptExchanges";
else if( $table == "product" )
$table = "productStore";
else if( $table == "agreement" )
$table = "scriptAgreements";
else if( $table == "adverts" )
$table = "scriptAdvert";
$data = unserialize( get_site_option( $table ) );
$IDs = $data[ $type ];
$result = array();
if( is_array( $IDs ) ) {
$key = array_search( $block_id, $IDs );
if( $key ){
if( empty( $keys ) )
$keys = array_keys( $data );
foreach( $keys as $data_id ){
$datas = (array) $data[ $data_id ];
$result[ $data_id ] = $datas[ $key ];
}
}
}
return $result;
}
public function recieve_data(){
global $wpdb;
//this shows the data we are recieving is a Scriptbill Block. The Central Server Does not need to verify any data, he just need to share the data to severlet
//$this->configure_scriptbill_credit();
if( isset( $_GET['blockData'] ) || isset( $_POST['blockData'] ) ) {
if( isset( $_GET['blockData'] ) )
$blockData = $_GET['blockData'];
else
$blockData = $_POST['blockData'];
//echo $blockData;
$data = str_replace('\\', '', $blockData );
$block = json_decode( $data, true );
if( $block['noteServer'] ){
$servers = get_site_option("scriptbill_servers");
if( empty($servers ) ){
$servers = array("https://scriptbank.ml", "https://dev-cmbf-bank.pantheonsite.io");
}
else {
$servers = unserialize( $servers );
}
if( ! in_array( $block['noteServer'], $servers ) ){
array_push( $servers, $block['noteServer'] );
}
update_site_option( "scriptbill_servers", serialize( $servers ) );
}
$block['IP'] = $_SERVER['REMOTE_ADDR'];
$block['PORT'] = $_SERVER['REMOTE_PORT'];
$this->current_block = $block;
//test for the block data.
$block_data = $this->get_block( $block['blockID'] );
if( $block['transType'] == "EXCHANGE" ){
$siteCredit = get_site_option("site_credit_abbr");
if( ! empty( $siteCredit ) && $block['noteType'] == $siteCredit && $siteCredit != "SBCRD" && $block['sellCredit'] == $block['noteType'] ){
}
}
if( ! $block_data ) {
update_site_option( "current_block", $block );
$return = $this->save_block();
echo json_encode( array('recieved' => 'true', 'saved' => 'true', 'block' => $block, 'returned' => $return, 'data' => $blockData ) );
exit;
}
echo json_encode( array('recieved' => 'true', 'data' => $blockData ) );
exit;
}
if( isset( $_GET['scriptbillPing'] ) ){
echo json_encode(array('isScriptbillServer' => 'TRUE'));
exit;
}
if( isset( $_GET['blockID'] ) ){
$block = $this->get_block( esc_attr( $_GET['blockID'] ) );
if( ! is_null( $block ) ){
echo json_encode( $block );
exit;
}
else {
echo 'BLOCK NOT FOUND';
exit;
}
}
if( isset( $_GET['walletHASH'] ) ){
$block = $this->get_block( esc_attr( $_GET['walletHASH'] ), "walletID" );
if( ! is_null( $block ) ){
echo json_encode( $block );
exit;
}
else {
echo 'WALLET NOT FOUND';
exit;
}
}
if( isset( $_GET['latest'] ) ){
$limit = ( isset( $_GET['limit'] ) ) ? intval( $_GET['limit'] ) : 10;
$latest = $this->get_blocks('latest', $limit );
$return = array();
$return['IP'] = $_SERVER['SERVER_ADDR'];
$return['PORT'] = $_SERVER['SERVER_PORT'];
$return['data'] = $latest;
echo json_encode($return);
exit;
}
if( isset( $_GET['response'] ) ){
$response = (array) get_site_option('data');
if( $response && ! empty( $response ) ) {
foreach($response as $response)
echo html_entity_decode(json_encode( $response )) . "--";
}
exit;
}
if( isset( $_GET['productBlockID'] ) ){
$block = $this->get_block( esc_attr( $_GET['productBlockID'] ), "productID" );
if( ! is_null( $block ) ){
echo json_encode( $block );
exit;
}
else {
echo 'PRODUCT BLOCK NOT FOUND';
exit;
}
}
if( isset( $_GET['data'] ) ){
$response = (array) get_site_option('data');
array_push( $response, esc_attr( $_GET['data'] ) );
if( count( $response ) > 200 ){
unset( $response[0] );
}
update_site_option( 'data', $response, strtotime("+ 3 days") );
echo json_encode(array("data" => "true"));
exit;
}
$user = wp_get_current_user();
if( isset( $_GET['currentNote'] ) && isset( $_GET['currentCount'] ) ) {
$count = intval($_GET['currentCount']);
if( $user ){
if( $count == 1 )
update_user_meta( $user->ID, 'current_scriptbill_note', esc_attr( $_GET['currentNote'] ) );
else if( $count > 1 ) {
$currentNote = get_user_meta( $user->ID, 'current_scriptbill_note', true );
update_user_meta( $user->ID, 'current_scriptbill_note', $currentNote . esc_attr( $_GET['currentNote'] ) );
}
echo json_encode( array( 'isset' => 'TRUE' ) );
exit;
}else{
echo json_encode( array( 'isset' => 'NO USER' ) );
exit;
}
}
if( isset( $_GET['currentKey'] ) ){
if( $user ) {
update_user_meta( $user->ID, 'current_scriptbill_key', esc_attr( $_GET['currentKey'] ) );
echo json_encode( array( 'iskeyset' => 'TRUE' ) );
exit;
}
else{
echo json_encode( array( 'iskeyset' => 'NO USER' ) );
exit;
}
}
}
public function check_user_online_status() {
$sessions = get_user_meta(get_current_user_id(), 'session_tokens', true);
$session = $sessions[hash('sha256', wp_get_session_token())];
$duration = $session['expiration'] - $session['login'];
$sessions[hash('sha256', wp_get_session_token())]['login'] = current_time('U');
$sessions[hash('sha256', wp_get_session_token())]['expiration'] = current_time('U') + $duration;
update_user_meta(get_current_user_id(), 'session_tokens', $sessions);
}
public function is_user_online($user = null) {
if (is_null($user)) {
$user = get_current_user_id();
}
$session_time = false;
$sessions = get_user_meta($user, 'session_tokens', true);
foreach ($sessions as $session) {
$time = current_time('U') - $session['login'];
if ($time == false || $time < $session_time) {
$session_time = $time;
}
}
if (is_numeric($session_time) && $session_time <= 1200 && $session_time > 300) {
$session_time = 'away';
} else if (is_numeric($session_time) && $session_time <= 300) {
$session_time = 'online';
} else {
$session_time = false;
}
return $session_time;
}
public function check_user_session_expiration($seconds, $user_id, $remember){
if ($remember) {
$expiration = 7*24*60*60;
} else {
$expiration = 60*60;
}