/
App.php
1079 lines (921 loc) · 45.1 KB
/
App.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
class App {
public $dbh;
public function __construct($dbh) {
$this->dbh = $dbh;
}
public function quote_escape($string) {
return $this->dbh->quote($string);
}
public function set_db($db_name) {
$this->dbh->query("USE ".$db_name) or die("There was an error accessing the '".$db_name."' database.");
}
public function last_insert_id() {
return $this->dbh->lastInsertId();
}
public function run_query($query) {
if ($GLOBALS['show_query_errors'] == TRUE) $result = $this->dbh->query($query) or $this->log_then_die("Error in query: ".$query.", ".$this->dbh->errorInfo()[2]);
else $result = $this->dbh->query($query) or die("Error in query");
return $result;
}
public function log_then_die($message) {
$this->log($message);
die($message);
}
public function utf8_clean($str) {
return iconv('UTF-8', 'UTF-8//IGNORE', $str);
}
public function make_alphanumeric($string, $extrachars) {
$allowed_chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".$extrachars;
$new_string = "";
for ($i=0; $i<strlen($string); $i++) {
if (is_numeric(strpos($allowed_chars, $string[$i])))
$new_string .= $string[$i];
}
return $new_string;
}
public function random_string($length) {
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$bits_per_char = ceil(log(strlen($characters), 2));
$hex_chars_per_char = ceil($bits_per_char/4);
$hex_chars_needed = $length*$hex_chars_per_char;
$rand_data = bin2hex(openssl_random_pseudo_bytes(ceil($hex_chars_needed/2), $crypto_strong));
if(!$crypto_strong) $this->log_then_die("An insecure random string of length ".$length." was generated.");
$string = "";
for ($i=0; $i<$length; $i++) {
$hex_chars = substr($rand_data, $i*$hex_chars_per_char, $hex_chars_per_char);
$rand_num = hexdec($hex_chars);
$rand_index = $rand_num%strlen($characters);
$string .= $characters[$rand_index];
}
return $string;
}
public function normalize_username($username) {
return $this->make_alphanumeric(strip_tags($username), "$-()/!.,:;#@");
}
public function recaptcha_check_answer($recaptcha_privatekey, $ip_address, $g_recaptcha_response) {
$response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_privatekey."&response=".$g_recaptcha_response."&remoteip=".$ip_address), true);
if ($response['success'] == false) return false;
else return true;
}
public function generate_games() {
$q = "SELECT * FROM game_types ORDER BY game_type_id ASC;";
$r = $this->run_query($q);
while ($game_type = $r->fetch(PDO::FETCH_ASSOC)) {
$this->generate_games_by_type($game_type);
}
}
public function generate_games_by_type($game_type) {
$q = "SELECT * FROM games WHERE game_type_id='".$game_type['game_type_id']."' AND game_status IN('editable','published','running');";
$r = $this->run_query($q);
$num_running_games = $r->rowCount();
$needed_games = $game_type['target_open_games'] - $num_running_games;
for ($i=0; $i<$needed_games; $i++) {
$this->generate_game_by_type($game_type);
}
}
public function option_index_to_voting_chars($option_index, $options_in_game) {
$voting_chars = "0123456789abcdefghijklmnopqrstuvwxyz";
$modulus = strlen($voting_chars);
$length = ceil(log($options_in_game, strlen($voting_chars)));
$chars = "";
$current_num = $option_index;
for ($i=0; $i<$length; $i++) {
$remainder = $current_num%$modulus;
$current_num = floor($current_num/$modulus);
$chars .= $voting_chars[$remainder];
}
return strrev($chars);
}
public function generate_game_by_type($game_type) {
$skip_game_type_vars = explode(",", "event_rule,event_entity_type_id,option_group_id,events_per_round,url_identifier,target_open_games,default_vote_effectiveness_function,default_max_voting_fraction,event_type_name,default_option_max_width,default_game_winning_inflation,default_logo_image_id");
$url_identifier = $this->game_url_identifier($game_type['name']);
$q = "INSERT INTO games SET url_identifier=".$this->quote_escape($url_identifier).", game_winning_inflation='".$game_type['default_game_winning_inflation']."', logo_image_id='".$game_type['default_logo_image_id']."', ";
foreach ($game_type AS $var => $val) {
if (!in_array($var, $skip_game_type_vars)) {
if (!empty($val)) $q .= $var.'='.$this->quote_escape($val).', ';
}
}
$q = substr($q, 0, strlen($q)-2).";";
$r = $this->run_query($q);
$game_id = $this->last_insert_id();
if ($game_type['event_rule'] == "entity_type_option_group") {
$q = "SELECT * FROM entity_types WHERE entity_type_id='".$game_type['event_entity_type_id']."';";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
$entity_type = $r->fetch();
$q = "SELECT * FROM option_groups WHERE group_id='".$game_type['option_group_id']."';";
$r = $this->run_query($q);
$option_group = $r->fetch();
$q = "SELECT * FROM entities e JOIN option_group_memberships mem ON e.entity_id=mem.entity_id WHERE mem.option_group_id='".$game_type['option_group_id']."' ORDER BY e.entity_id ASC;";
$r = $this->run_query($q);
$db_option_entities = array();
while ($option_entity = $r->fetch()) {
$db_option_entities[count($db_option_entities)] = $option_entity;
}
$event_i = 0;
$option_i = 0;
$max_voting_chars = 0;
$q = "SELECT * FROM entities WHERE entity_type_id='".$entity_type['entity_type_id']."' ORDER BY entity_id ASC;";
$r = $this->run_query($q);
$options_in_game = $r->rowCount()*count($db_option_entities);
while ($event_entity = $r->fetch()) {
if (count($db_option_entities) == 2 && !empty($db_option_entities[0]['last_name'])) {
$event_type_name = $db_option_entities[0]['last_name']." vs ".$db_option_entities[1]['last_name']." in ".$event_entity['entity_name'];
$event_type_identifier = strtolower($db_option_entities[0]['last_name']."-".$db_option_entities[1]['last_name']."-".$event_entity['entity_name']);
}
else {
$event_type_name = $event_entity['entity_name']." ".ucwords($game_type['event_type_name']);
$event_type_identifier = strtolower($event_entity['entity_name']."-".$game_type['event_type_name']);
}
$qq = "SELECT * FROM event_types WHERE url_identifier=".$this->quote_escape($event_type_identifier).";";
$rr = $this->run_query($qq);
if ($rr->rowCount() > 0) {
$event_type = $rr->fetch();
}
else {
$qq = "INSERT INTO event_types SET option_group_id='".$game_type['option_group_id']."', entity_id='".$event_entity['entity_id']."', name='".$event_type_name."', url_identifier=".$this->quote_escape($event_type_identifier).", num_voting_options='".count($db_option_entities)."', vote_effectiveness_function='".$game_type['default_vote_effectiveness_function']."', max_voting_fraction='".$game_type['default_max_voting_fraction']."';";
$rr = $this->run_query($qq);
$event_type_id = $this->last_insert_id();
$qq = "SELECT * FROM event_types WHERE event_type_id='".$event_type_id."';";
$event_type = $this->run_query($qq)->fetch();
}
$qq = "SELECT * FROM events WHERE game_id='".$game_id."' AND event_type_id='".$event_type['event_type_id']."';";
$rr = $this->run_query($qq);
if ($rr->rowCount() > 0) {
$option_i += count($db_option_entities);
}
else {
$starting_round = floor($event_i/$game_type['events_per_round'])+1;
$event_starting_block = ($starting_round-1)*$game_type['round_length']+1;
$event_final_block = $starting_round*$game_type['round_length'];
$qq = "INSERT INTO events SET game_id='".$game_id."', event_type_id='".$event_type['event_type_id']."', event_starting_block='".$event_starting_block."', event_final_block='".$event_final_block."', event_name=".$this->quote_escape($event_type_name).", option_name=".$this->quote_escape($option_group['option_name']).", option_name_plural=".$this->quote_escape($option_group['option_name_plural']).", option_max_width='".$game_type['default_option_max_width']."';";
$rr = $this->run_query($qq);
$event_id = $this->last_insert_id();
for ($i=0; $i<count($db_option_entities); $i++) {
$voting_character = $this->option_index_to_voting_chars($option_i, $options_in_game);
if (strlen($voting_character) > $max_voting_chars) $max_voting_chars = strlen($voting_character);
$qq = "INSERT INTO options SET event_id='".$event_id."', entity_id='".$db_option_entities[$i]['entity_id']."', membership_id='".$db_option_entities[$i]['membership_id']."', image_id='".$db_option_entities[$i]['default_image_id']."', name='".$db_option_entities[$i]['last_name']." wins ".$event_entity['entity_name']."', voting_character='".$voting_character."';";
$rr = $this->run_query($qq);
$option_i++;
}
}
$event_i++;
}
$q = "UPDATE games SET game_status='published', max_voting_chars='".$max_voting_chars."' WHERE game_id='".$game_id."';";
$r = $this->run_query($q);
}
}
}
public function get_redirect_url($url) {
$q = "SELECT * FROM redirect_urls WHERE url=".$this->quote_escape($url).";";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
$redirect_url = $r->fetch();
}
else {
$q = "INSERT INTO redirect_urls SET url=".$this->quote_escape($url).", time_created='".time()."';";
$r = $this->run_query($q);
$redirect_url_id = $this->last_insert_id();
$q = "SELECT * FROM redirect_urls WHERE redirect_url_id='".$redirect_url_id."';";
$r = $this->run_query($q);
$redirect_url = $r->fetch();
}
return $redirect_url;
}
public function mail_async($email, $from_name, $from, $subject, $message, $bcc, $cc) {
$q = "INSERT INTO async_email_deliveries SET to_email=".$this->quote_escape($email).", from_name=".$this->quote_escape($from_name).", from_email=".$this->quote_escape($from).", subject=".$this->quote_escape($subject).", message=".$this->quote_escape($message).", bcc=".$this->quote_escape($bcc).", cc=".$this->quote_escape($cc).", time_created='".time()."';";
$r = $this->run_query($q);
$delivery_id = $this->last_insert_id();
$command = $this->php_binary_location()." ".realpath(dirname(dirname(__FILE__)))."/scripts/async_email_deliver.php ".$delivery_id." > /dev/null 2>/dev/null &";
exec($command);
/*$curl_url = $GLOBALS['base_url']."/scripts/async_email_deliver.php?delivery_id=".$delivery_id;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);*/
return $delivery_id;
}
public function get_site_constant($constant_name) {
$q = "SELECT * FROM site_constants WHERE constant_name='".$constant_name."';";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
$constant = $r->fetch();
return $constant['constant_value'];
}
else return "";
}
public function set_site_constant($constant_name, $constant_value) {
$q = "SELECT * FROM site_constants WHERE constant_name='".$constant_name."';";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
$constant = $r->fetch();
$q = "UPDATE site_constants SET constant_value='".$constant_value."' WHERE constant_id='".$constant['constant_id']."';";
$r = $this->run_query($q);
}
else {
$q = "INSERT INTO site_constants SET constant_name='".$constant_name."', constant_value='".$constant_value."';";
$r = $this->run_query($q);
}
}
public function to_significant_digits($number, $significant_digits) {
if ($number === 0) return 0;
if ($number < 1) $significant_digits++;
$number_digits = (int)(log10($number));
$returnval = (pow(10, $number_digits - $significant_digits + 1)) * floor($number/(pow(10, $number_digits - $significant_digits + 1)));
return $returnval;
}
public function format_bignum($number) {
if ($number >= 0) $sign = "";
else $sign = "-";
$number = abs($number);
if ($number > 1) $number = $this->to_significant_digits($number, 5);
if ($number > pow(10, 9)) {
return $sign.($number/pow(10, 9))."B";
}
else if ($number > pow(10, 6)) {
return $sign.($number/pow(10, 6))."M";
}
else if ($number > pow(10, 4)) {
return $sign.($number/pow(10, 3))."k";
}
else return $sign.rtrim(rtrim(number_format(sprintf('%.8F', $number), 8), '0'), ".");
}
public function to_ranktext($rank) {
return $rank.date("S", strtotime("1/".$rank."/".date("Y")));
}
public function cancel_transaction($transaction_id, $affected_input_ids, $created_input_ids) {
$q = "DELETE FROM transactions WHERE transaction_id='".$transaction_id."';";
$r = $this->run_query($q);
if (count($affected_input_ids) > 0) {
$q = "UPDATE transaction_ios SET spend_status='unspent', spend_transaction_id=NULL, spend_block_id=NULL WHERE io_id IN (".implode(",", $affected_input_ids).")";
$r = $this->run_query($q);
}
if ($created_input_ids && count($created_input_ids) > 0) {
$q = "DELETE FROM transaction_ios WHERE io_id IN (".implode(",", $created_input_ids).");";
$r = $this->run_query($q);
}
}
public function transaction_coins_in($transaction_id) {
$qq = "SELECT SUM(amount) FROM transaction_ios io JOIN addresses a ON io.address_id=a.address_id WHERE io.spend_transaction_id='".$transaction_id."';";
$rr = $this->run_query($qq);
$coins_in = $rr->fetch(PDO::FETCH_NUM);
if ($coins_in[0] > 0) return $coins_in[0];
else return 0;
}
public function transaction_coins_out($transaction_id) {
$qq = "SELECT SUM(amount) FROM transaction_ios io JOIN addresses a ON io.address_id=a.address_id WHERE io.create_transaction_id='".$transaction_id."';";
$rr = $this->run_query($qq);
$coins_out = $rr->fetch(PDO::FETCH_NUM);
if ($coins_out[0] > 0) return $coins_out[0];
else return 0;
}
public function transaction_voted_coins_out($transaction_id) {
$qq = "SELECT SUM(amount) FROM transaction_ios io JOIN addresses a ON io.address_id=a.address_id WHERE io.create_transaction_id='".$transaction_id."' AND a.option_id > 0;";
$rr = $this->run_query($qq);
$voted_coins_out = $rr->fetch(PDO::FETCH_NUM);
if ($voted_coins_out[0] > 0) return $voted_coins_out[0];
else return 0;
}
public function output_message($status_code, $message, $dump_object) {
if (empty($dump_object)) $dump_object = array("status_code"=>$status_code, "message"=>$message);
else {
$dump_object['status_code'] = $status_code;
$dump_object['message'] = $message;
}
echo json_encode($dump_object);
}
public function try_apply_invite_key($user_id, $invite_key, &$invite_game) {
$reload_page = false;
$invite_key = $this->quote_escape($invite_key);
$q = "SELECT * FROM game_invitations WHERE invitation_key=".$invite_key.";";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
$invitation = $r->fetch();
if ($invitation['used'] == 0 && $invitation['used_user_id'] == "" && $invitation['used_time'] == 0) {
$qq = "UPDATE game_invitations SET used_user_id='".$user_id."', used_time='".time()."', used=1";
if ($GLOBALS['pageview_tracking_enabled']) $q .= ", used_ip='".$_SERVER['REMOTE_ADDR']."'";
$qq .= " WHERE invitation_id='".$invitation['invitation_id']."';";
$rr = $this->run_query($qq);
if ($invitation['giveaway_id'] > 0) {
$qq = "UPDATE game_giveaways SET user_id='".$user_id."', status='claimed' WHERE giveaway_id='".$invitation['giveaway_id']."';";
$rr = $this->run_query($qq);
}
$user = new User($this, $user_id);
$user->ensure_user_in_game($invitation['game_id']);
$invite_game = new Game($this, $invitation['game_id']);
return true;
}
else return false;
}
else return false;
}
public function format_seconds($seconds) {
$seconds = intval($seconds);
$weeks = floor($seconds/(3600*24*7));
$days = floor($seconds/(3600*24));
$hours = floor($seconds / 3600);
$minutes = floor($seconds / 60);
if ($weeks > 0) {
if ($weeks == 1) $str = $weeks." week";
else $str = $weeks." weeks";
$days = $days - 7*$weeks;
if ($days != 1) $str .= " and ".$days." days";
else $str .= " and ".$days." day";
return $str;
}
else if ($days > 1) {
return $days." days";
}
else if ($hours > 0) {
$str = "";
if ($hours != 1) $str .= $hours." hours";
else $str .= $hours." hour";
$remainder_min = round(($seconds - (3600*$hours))/60);
if ($remainder_min > 0) {
$str .= " and ".$remainder_min." ";
if ($remainder_min == '1') $str .= "minute";
else $str .= "minutes";
}
return $str;
}
else if ($minutes > 0) {
$remainder_sec = $seconds-$minutes*60;
$str = "";
if ($minutes != 1) $str .= $minutes." minutes";
else return $str .= $minutes." minute";
if ($remainder_sec > 0) $str .= " and ".$remainder_sec." seconds";
return $str;
}
else {
if ($seconds != 1) return $seconds." seconds";
else return $seconds." second";
}
}
public function game_url_identifier($game_name) {
$url_identifier = "";
$append_index = 0;
$keeplooping = true;
do {
if ($append_index > 0) $append = "(".$append_index.")";
else $append = "";
$url_identifier = $this->make_alphanumeric(str_replace(" ", "-", strtolower($game_name.$append)), "-().:;");
$q = "SELECT * FROM games WHERE url_identifier=".$this->quote_escape($url_identifier).";";
$r = $this->run_query($q);
if ($r->rowCount() == 0) $keeplooping = false;
else $append_index++;
} while ($keeplooping);
return $url_identifier;
}
public function prepend_a_or_an($word) {
$firstletter = strtolower($word[0]);
if (strpos('aeiou', $firstletter)) return "an ".$word;
else return "a ".$word;
}
public function friendly_intval($val) {
if ($val > 0) return $val;
else return 0;
}
public function fetch_game_from_url() {
$login_url_parts = explode("/", rtrim(ltrim($_SERVER['REQUEST_URI'], "/"), "/"));
if ($login_url_parts[0] == "wallet" && count($login_url_parts) > 1) {
$q = "SELECT * FROM games WHERE url_identifier=".$this->quote_escape($login_url_parts[1]).";";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
return $r->fetch();
}
else return false;
}
else return false;
}
/*public function process_join_requests($variation_id) {
$q = "SELECT * FROM event_type_variations WHERE variation_id='".$variation_id."';";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
$variation = $r->fetch();
if (in_array($variation['giveaway_status'], array('public_free', 'public_pay'))) {
$keeplooping = true;
$last_request_id = 0;
do {
$q = "SELECT * FROM game_join_requests WHERE variation_id='".$variation['variation_id']."' AND request_status='outstanding' AND join_request_id > ".$last_request_id." ORDER BY join_request_id ASC LIMIT 1;";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
$join_request = $r->fetch();
$last_request_id = $join_request['join_request_id'];
$join_user = new User($this, $join_request['user_id']);
$qq = "SELECT * FROM event_types WHERE variation_id='".$variation['variation_id']."' AND event_status='published' ORDER BY event_id ASC LIMIT 1;";
$rr = $this->run_query($qq);
if ($rr->rowCount() == 1) {
$db_join_event = $rr->fetch();
$join_user->ensure_user_in_game($db_join_event['event_id']);
$this->run_query("UPDATE game_join_requests SET request_status='complete', event_id='".$db_join_event['event_id']."' WHERE join_request_id='".$join_request['join_request_id']."';");
}
}
else $keeplooping = false;
}
while ($keeplooping);
}
}
}
public function bet_transaction_payback_address($transaction_id) {
$q = "SELECT * FROM transaction_ios i, transactions t, addresses a WHERE t.transaction_id='".$transaction_id."' AND i.spend_transaction_id=t.transaction_id AND i.address_id=a.address_id ORDER BY a.address ASC LIMIT 1;";
$r = $this->run_query($q);
if ($r->rowCount() == 1) {
return $r->fetch();
}
else return false;
}*/
public function latest_currency_price($currency_id) {
$q = "SELECT * FROM currency_prices WHERE currency_id='".$currency_id."' AND reference_currency_id='".$this->get_site_constant('reference_currency_id')."' ORDER BY price_id DESC LIMIT 1;";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
return $r->fetch();
}
else return false;
}
public function get_currency_by_abbreviation($currency_abbreviation) {
$q = "SELECT * FROM currencies WHERE abbreviation='".strtoupper($currency_abbreviation)."';";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
return $r->fetch();
}
else return false;
}
public function get_reference_currency() {
$q = "SELECT * FROM currencies WHERE currency_id='".$this->get_site_constant('reference_currency_id')."';";
$r = $this->run_query($q);
if ($r->rowCount() > 0) return $r->fetch();
else die('Error, reference_currency_id is not set properly in site_constants.');
}
public function update_all_currency_prices() {
$reference_currency_id = $this->get_site_constant('reference_currency_id');
$q = "SELECT * FROM currencies c JOIN oracle_urls o ON c.oracle_url_id=o.oracle_url_id WHERE c.currency_id != '".$reference_currency_id."' GROUP BY o.oracle_url_id;";
$r = $this->run_query($q);
while ($currency_url = $r->fetch()) {
$api_response_raw = file_get_contents($currency_url['url']);
$api_response = json_decode($api_response_raw);
$qq = "SELECT * FROM currencies WHERE oracle_url_id='".$currency_url['oracle_url_id']."';";
$rr = $this->run_query($qq);
while ($currency = $rr->fetch()) {
if ($currency_url['format_id'] == 2) {
$price = $api_response->USD->bid;
}
else if ($currency_url['format_id'] == 1) {
if (!empty($api_response->rates)) {
$api_rates = (array) $api_response->rates;
$price = 1/($api_rates[$currency['abbreviation']]);
}
}
$qqq = "INSERT INTO currency_prices SET currency_id='".$currency['currency_id']."', reference_currency_id='".$reference_currency_id."', price='".$price."', time_added='".time()."';";
$rrr = $this->run_query($qqq);
}
}
}
public function update_currency_price($currency_id) {
$q = "SELECT * FROM currencies WHERE currency_id='".$currency_id."';";
$r = $this->run_query($q);
if ($r->rowCount() > 0) {
$currency = $r->fetch();
if ($currency['abbreviation'] == "BTC") {
$reference_currency = $this->get_reference_currency();
$api_url = "https://api.bitcoinaverage.com/ticker/global/all";
$api_response_raw = file_get_contents($api_url);
$api_response = json_decode($api_response_raw);
$price = $api_response->$reference_currency['abbreviation']->bid;
if ($price > 0) {
$q = "INSERT INTO currency_prices SET currency_id='".$currency_id."', reference_currency_id='".$reference_currency['currency_id']."', price='".$price."', time_added='".time()."';";
$r = $this->run_query($q);
$currency_price_id = $this->last_insert_id();
$q = "SELECT * FROM currency_prices WHERE price_id='".$currency_price_id."';";
$r = $this->run_query($q);
return $r->fetch();
}
else return false;
}
else return false;
}
else return false;
}
public function currency_conversion_rate($numerator_currency_id, $denominator_currency_id) {
$latest_numerator_rate = $this->latest_currency_price($numerator_currency_id);
$latest_denominator_rate = $this->latest_currency_price($denominator_currency_id);
$returnvals['numerator_price_id'] = $latest_numerator_rate['price_id'];
$returnvals['denominator_price_id'] = $latest_denominator_rate['price_id'];
$returnvals['conversion_rate'] = round(pow(10,8)*$latest_denominator_rate['price']/$latest_numerator_rate['price'])/pow(10,8);
return $returnvals;
}
public function historical_currency_conversion_rate($numerator_price_id, $denominator_price_id) {
$q = "SELECT * FROM currency_prices WHERE price_id='".$numerator_price_id."';";
$r = $this->run_query($q);
$numerator_rate = $r->fetch();
$q = "SELECT * FROM currency_prices WHERE price_id='".$denominator_price_id."';";
$r = $this->run_query($q);
$denominator_rate = $r->fetch();
return round(pow(10,8)*$denominator_rate['price']/$numerator_rate['price'])/pow(10,8);
}
public function new_currency_invoice($settle_currency_id, $settle_amount, $user_id, $game_id) {
$q = "SELECT * FROM currencies WHERE currency_id='".$settle_currency_id."';";
$r = $this->run_query($q);
$settle_currency = $r->fetch();
$pay_currency = $this->get_currency_by_abbreviation('btc');
$conversion = $this->currency_conversion_rate($settle_currency_id, $pay_currency['currency_id']);
$settle_curr_per_pay_curr = $conversion['conversion_rate'];
$pay_amount = round(pow(10,8)*$settle_amount/$settle_curr_per_pay_curr)/pow(10,8);
$invoice_address_id = $this->new_invoice_address();
$q = "UPDATE invoice_addresses SET currency_id='".$pay_currency['currency_id']."' WHERE invoice_address_id='".$invoice_address_id."';";
$r = $this->run_query($q);
$time = time();
$q = "INSERT INTO currency_invoices SET time_created='".$time."', invoice_address_id='".$invoice_address_id."', expire_time='".($time+$GLOBALS['invoice_expiration_seconds'])."', game_id='".$game_id."', user_id='".$user_id."', status='unpaid', invoice_key_string='".$this->random_string(32)."', settle_price_id='".$conversion['numerator_price_id']."', settle_currency_id='".$settle_currency['currency_id']."', settle_amount='".$settle_amount."', pay_price_id='".$conversion['denominator_price_id']."', pay_currency_id='".$pay_currency['currency_id']."', pay_amount='".$pay_amount."';";
$r = $this->run_query($q);
$invoice_id = $this->last_insert_id();
$q = "SELECT * FROM currency_invoices WHERE invoice_id='".$invoice_id."';";
$r = $this->run_query($q);
return $r->fetch();
}
public function vote_details_general($mature_balance) {
return "";
/*$html = '
<div class="row">
<div class="col-xs-4">Your balance:</div>
<div class="col-xs-8 greentext">'.number_format(floor($mature_balance/pow(10,5))/1000, 2).' EMP</div>
</div> ';
return $html;*/
}
public function vote_option_details($option, $rank, $confirmed_votes, $unconfirmed_votes, $sum_votes, $losing_streak) {
$html = '
<div class="row">
<div class="col-xs-4">Current rank:</div>
<div class="col-xs-8">'.$this->to_ranktext($rank).'</div>
</div>
<div class="row">
<div class="col-xs-4">Confirmed Votes:</div>
<div class="col-xs-8">'.$this->format_bignum($confirmed_votes/pow(10,8)).' votes ('.(empty($sum_votes)? 0 : (ceil(100*100*$confirmed_votes/$sum_votes)/100)).'%)</div>
</div>
<div class="row">
<div class="col-xs-4">Unconfirmed Votes:</div>
<div class="col-xs-8">'.$this->format_bignum($unconfirmed_votes/pow(10,8)).' votes ('.(empty($sum_votes)? 0 : (ceil(100*100*$unconfirmed_votes/$sum_votes)/100)).'%)</div>
</div>';
/*<div class="row">
<div class="col-xs-4">Last win:</div>
<div class="col-xs-8">';
if ($losing_streak === 0) $html .= "Last round";
else if ($losing_streak) $html .= $losing_streak.' rounds ago';
else $html .= "Never";
$html .= '
</div>
</div>';*/
return $html;
}
public function new_invoice_address() {
$keySet = bitcoin::getNewKeySet();
if (empty($keySet['pubAdd']) || empty($keySet['privWIF'])) {
die("<p>There was an error generating the payment address. Please go back and try again.</p>");
}
$encWIF = bin2hex(bitsci::rsa_encrypt($keySet['privWIF'], $GLOBALS['rsa_pub_key']));
$q = "INSERT INTO invoice_addresses SET currency_id=1, pub_key='".$keySet['pubAdd']."', priv_enc='".$encWIF."';";
$r = $this->run_query($q);
$address_id = $this->last_insert_id();
return $address_id;
}
public function decimal_to_float($number) {
if (strpos($number, ".") === false) return $number;
else return rtrim(rtrim($number, '0'), '.');
}
public function display_featured_games() {
echo '<div class="paragraph">';
$q = "SELECT g.*, c.short_name AS currency_short_name FROM games g LEFT JOIN currencies c ON g.invite_currency=c.currency_id WHERE g.featured=1 AND (g.game_status='published' OR g.game_status='running') ORDER BY g.featured_score DESC;";
$r = $this->run_query($q);
$cell_width = 12;
$counter = 0;
echo '<div class="row">';
while ($db_game = $r->fetch()) {
$featured_game = new Game($this, $db_game['game_id']);
$mining_block_id = $featured_game->last_block_id()+1;
$current_round_id = $featured_game->block_to_round($mining_block_id);
?>
<script type="text/javascript">
games.push(new Game(<?php
echo $db_game['game_id'];
echo ', false';
echo ', false'.', ';
echo 'false';
echo ', ""';
echo ', "'.$db_game['payout_weight'].'"';
echo ', '.$db_game['round_length'];
$bet_round_range = $featured_game->bet_round_range();
$min_bet_round = $bet_round_range[0];
echo ', '.$min_bet_round;
echo ', 0';
echo ', "'.$db_game['url_identifier'].'"';
echo ', "'.$db_game['coin_name'].'"';
echo ', "'.$db_game['coin_name_plural'].'"';
echo ', "home", "'.$featured_game->event_ids().'", "'.$featured_game->logo_image_url().'", "'.$featured_game->vote_effectiveness_function().'"';
?>));
</script>
<?php
echo '<div class="col-md-'.$cell_width.'">';
echo '<center><h1 style="display: inline-block" title="'.$featured_game->game_description().'">'.$featured_game->db_game['name'].'</h1>';
if ($featured_game->db_game['short_description'] != "") echo "<p>".$featured_game->db_game['short_description']."</p>";
echo '<div id="game'.$counter.'_events"></div>';
echo '<script type="text/javascript" id="game'.$counter.'_new_event_js">'.$featured_game->new_event_js($counter, false).'</script>';
echo '<br/><a href="/'.$featured_game->db_game['url_identifier'].'/" class="btn btn-success">Play Now</a>';
echo ' <a href="/explorer/'.$featured_game->db_game['url_identifier'].'/events/" class="btn btn-primary">Blockchain Explorer</a>';
echo '</center><br/>';
if ($counter%(12/$cell_width) == 1) echo '</div><div class="row">';
$counter++;
echo '</div>';
}
echo '</div>';
echo '</div>';
}
public function refresh_utxo_user_ids($only_unspent_utxos) {
$update_user_id_q = "UPDATE transaction_ios io JOIN addresses a ON io.address_id=a.address_id SET io.user_id=a.user_id";
if ($only_unspent_utxos) $update_user_id_q .= " WHERE io.spend_status='unspent'";
$update_user_id_q .= ";";
$update_user_id_r = $this->run_query($update_user_id_q);
}
public function log($message) {
$this->run_query("INSERT INTO log_messages SET message=".$this->quote_escape($message).";");
}
public function update_schema() {
$migrations_path = realpath(dirname(__FILE__)."/../sql");
$migration_id = ((int)$this->get_site_constant("last_migration_id"))+1;
$keep_looping = true;
do {
$fname = $migrations_path."/".$migration_id.".sql";
if (is_file($fname)) {
$cmd = $this->mysql_binary_location()." -u ".$GLOBALS['mysql_user']." -h ".$GLOBALS['mysql_server'];
if ($GLOBALS['mysql_password'] != "") $cmd .= " -p".$GLOBALS['mysql_password'];
$cmd .= " ".$GLOBALS['mysql_database']." < ".$fname;
exec($cmd);
$this->set_site_constant("last_migration_id", $migration_id);
$migration_id++;
}
else $keep_looping = false;
}
while ($keep_looping);
}
public function argv_to_array($argv) {
$arr = array();
$arg_i = 0;
foreach ($argv as $arg) {
if ($arg_i > 0) {
$arg_parts = explode("=", $arg);
if(count($arg_parts) == 2)
$arr[$arg_parts[0]] = $arg_parts[1];
else
$arr[$arg_i-1] = $arg_parts[0];
}
$arg_i++;
}
return $arr;
}
public function mysql_binary_location() {
if (!empty($GLOBALS['mysql_binary_location'])) return $GLOBALS['mysql_binary_location'];
else {
$var = $this->run_query("SHOW VARIABLES LIKE 'basedir';")->fetch();
if (PHP_OS == "WINNT") return $var['Value']."bin/mysql.exe";
else return $var['Value']."/bin/mysql";
}
}
public function php_binary_location() {
if (!empty($GLOBALS['php_binary_location'])) return $GLOBALS['php_binary_location'];
else if (PHP_OS == "WINNT") return dirname(ini_get('extension_dir'))."\php.exe";
else return PHP_BINDIR ."/php";
}
public function start_regular_background_processes($key_string) {
$html = "";
$process_count = 0;
$pipe_config = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
$pipes = array();
if (PHP_OS == "WINNT") $script_path_name = dirname(dirname(__FILE__));
else $script_path_name = realpath(dirname(dirname(__FILE__)));
$cmd = $this->php_binary_location().' "'.$script_path_name.'/cron/load_blocks.php" key='.$key_string;
if (PHP_OS != "WINNT") $cmd .= " 2>&1 >/dev/null";
$block_loading_process = proc_open($cmd, $pipe_config, $pipes);
if (is_resource($block_loading_process)) $process_count++;
else $html .= "Failed to start a process for loading blocks.<br/>\n";
sleep(0.1);
$cmd = $this->php_binary_location().' "'.$script_path_name.'/cron/minutely_main.php" key='.$key_string;
if (PHP_OS != "WINNT") $cmd .= " 2>&1 >/dev/null";
$main_process = proc_open($cmd, $pipe_config, $pipes);
if (is_resource($main_process)) $process_count++;
else $html .= "Failed to start the main process.<br/>\n";
sleep(0.1);
$cmd = $this->php_binary_location().' "'.$script_path_name.'/cron/minutely_check_payments.php" key='.$key_string;
if (PHP_OS != "WINNT") $cmd .= " 2>&1 >/dev/null";
$payments_process = proc_open($cmd, $pipe_config, $pipes);
if (is_resource($payments_process)) $process_count++;
else $html .= "Failed to start a process for processing payments.<br/>\n";
sleep(0.1);
$cmd = $this->php_binary_location().' "'.$script_path_name.'/cron/address_miner.php" key='.$key_string;
if (PHP_OS != "WINNT") $cmd .= " 2>&1 >/dev/null";
$address_miner_process = proc_open($cmd, $pipe_config, $pipes);
if (is_resource($address_miner_process)) $process_count++;
else $html .= "Failed to start a process for mining addresses.<br/>\n";
sleep(0.1);
$cmd = $this->php_binary_location().' "'.$script_path_name.'/cron/fetch_currency_prices.php" key='.$key_string;
if (PHP_OS != "WINNT") $cmd .= " 2>&1 >/dev/null";
$currency_prices_process = proc_open($cmd, $pipe_config, $pipes);
if (is_resource($currency_prices_process)) $process_count++;
else $html .= "Failed to start a process for updating currency prices.<br/>\n";
$html .= $process_count." background processes were successfully started.<br/>\n";
return $html;
}
public function image_url(&$db_image) {
$url = '/images/custom/'.$db_image['image_id'];
if ($db_image['access_key'] != "") $url .= '_'.$db_image['access_key'];
$url .= '.'.$db_image['extension'];
return $url;
}
public function delete_unconfirmable_transactions() {
$start_time = microtime(true);
$unconfirmed_tx_r = $this->run_query("SELECT * FROM transactions WHERE block_id IS NULL ORDER BY game_id ASC;");
$game_id = false;
while ($unconfirmed_tx = $unconfirmed_tx_r->fetch()) {
if ($unconfirmed_tx['game_id'] != $game_id) {
$game_id = $unconfirmed_tx['game_id'];
$game = new Game($this, $game_id);
}
$coins_in = $this->transaction_coins_in($unconfirmed_tx['transaction_id']);
//$coins_out = $this->transaction_coins_out($unconfirmed_tx['transaction_id']);
if ($coins_in == 0) {
$this->run_query("DELETE t.*, io.* FROM transactions t JOIN transaction_ios io ON t.transaction_id=io.create_transaction_id WHERE t.transaction_id='".$unconfirmed_tx['transaction_id']."';");
}
else if ($unconfirmed_tx['fee_amount'] < 0) {
$this->run_query("DELETE t.*, io.* FROM transactions t JOIN transaction_ios io ON t.transaction_id=io.create_transaction_id WHERE t.transaction_id='".$unconfirmed_tx['transaction_id']."';");
}
}
echo "\nTook ".(microtime(true)-$start_time)." sec to delete unconfirmable transactions.\n\n";
}
public function game_admin_row(&$thisuser, $user_game, $selected_game_id) {
$html = '
<div class="row game_row';
if ($user_game['game_id'] == $selected_game_id) $html .= ' boldtext';
$html .= '">
<div class="col-sm-1 game_cell">
'.ucwords($user_game['game_status']).'
</div>
<div class="col-sm-5 game_cell">
<a target="_blank" href="/wallet/'.$user_game['url_identifier'].'/">'.$user_game['name'].'</a>
</div>
<div class="col-sm-3 game_cell">
<a id="fetch_game_link_'.$user_game['game_id'].'" href="" onclick="switch_to_game('.$user_game['game_id'].', \'fetch\'); return false;">Settings</a>
</div>
<div class="col-sm-3 game_cell">';
$perm_to_invite = $thisuser->user_can_invite_game($user_game);
if ($perm_to_invite) {
$html .= '
<a href="" onclick="manage_game_invitations('.$user_game['game_id'].'); return false;">Invitations</a>';
}
$html .= '
</div>
</div>';
return $html;
}
public function game_info_table(&$db_game) {
$html = "";
$blocks_per_hour = 3600/$db_game['seconds_per_block'];
$round_reward = ($db_game['pos_reward']+$db_game['pow_reward']*$db_game['round_length'])/pow(10,8);
$seconds_per_round = $db_game['seconds_per_block']*$db_game['round_length'];
$db_game_url = $GLOBALS['base_url']."/".$db_game['url_identifier'];
$invite_currency = false;
if ($db_game['invite_currency'] > 0) {
$q = "SELECT * FROM currencies WHERE currency_id='".$db_game['invite_currency']."';";
$r = $this->run_query($q);
$invite_currency = $r->fetch();
}
if ($db_game['game_id'] > 0) { // This public function can also be called with a game variation
$html .= '<div class="row"><div class="col-sm-5">Game title:</div><div class="col-sm-7">'.$db_game['name']."</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Game URL:</div><div class="col-sm-7"><a href="'.$db_game_url.'">'.$db_game_url."</a></div></div>\n";
}
$html .= '<div class="row"><div class="col-sm-5">Length of game:</div><div class="col-sm-7">';
if ($db_game['final_round'] > 0) $html .= $db_game['final_round']." rounds (".$this->format_seconds($seconds_per_round*$db_game['final_round']).")";
else $html .= "Game does not end";
$html .= "</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Cost to join:</div><div class="col-sm-7">';
if ($db_game['giveaway_status'] == "invite_pay" || $db_game['giveaway_status'] == "public_pay") $html .= $this->format_bignum($db_game['invite_cost'])." ".$invite_currency['short_name']."s";
else $html .= "Free";
$html .= "</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Additional buy-ins?</div><div class="col-sm-7">';
if ($db_game['buyin_policy'] == "unlimited") $html .= "Unlimited";
else if ($db_game['buyin_policy'] == "none") $html .= "Not allowed";
else if ($db_game['buyin_policy'] == "per_user_cap") $html .= "Up to ".$this->format_bignum($db_game['per_user_buyin_cap'])." ".$invite_currency['short_name']."s per player";
else if ($db_game['buyin_policy'] == "game_cap") $html .= $this->format_bignum($db_game['game_buyin_cap'])." ".$invite_currency['short_name']."s available";
else if ($db_game['buyin_policy'] == "game_and_user_cap") $html .= $this->format_bignum($db_game['per_user_buyin_cap'])." ".$invite_currency['short_name']."s per person until ".$this->format_bignum($db_game['game_buyin_cap'])." ".$invite_currency['short_name']."s are reached";
$html .= "</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Inflation:</div><div class="col-sm-7">';
if ($db_game['inflation'] == "linear") $html .= "Linear (".$this->format_bignum($round_reward)." coins per round)";
else if ($db_game['inflation'] == "fixed_exponential") $html .= "Fixed Exponential (".(100*$db_game['exponential_inflation_rate'])."% per round)";
else $html .= "Exponential<br/>".$this->votes_per_coin($db_game)." votes per coin (".(100*$db_game['exponential_inflation_rate'])."% per round)";
$html .= "</div></div>\n";
$total_inflation_pct = $this->game_final_inflation_pct($db_game);
if ($total_inflation_pct) {
$html .= '<div class="row"><div class="col-sm-5">Total inflation:</div><div class="col-sm-7">'.number_format($total_inflation_pct)."%</div></div>\n";
}
$html .= '<div class="row"><div class="col-sm-5">Distribution:</div><div class="col-sm-7">';
if ($db_game['inflation'] == "linear") $html .= $this->format_bignum($db_game['pos_reward']/pow(10,8))." to voters, ".$this->format_bignum($db_game['pow_reward']*$db_game['round_length']/pow(10,8))." to miners";
else $html .= (100 - 100*$db_game['exponential_inflation_minershare'])."% to voters, ".(100*$db_game['exponential_inflation_minershare'])."% to miners";
$html .= "</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Blocks per round:</div><div class="col-sm-7">'.$db_game['round_length']."</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Block target time:</div><div class="col-sm-7">'.$this->format_seconds($db_game['seconds_per_block'])."</div></div>\n";
$html .= '<div class="row"><div class="col-sm-5">Average time per round:</div><div class="col-sm-7">'.$this->format_seconds($db_game['round_length']*$db_game['seconds_per_block'])."</div></div>\n";
if ($db_game['maturity'] != 0) {
$html .= '<div class="row"><div class="col-sm-5">Transaction maturity:</div><div class="col-sm-7">'.$db_game['maturity']." block";
if ($db_game['maturity'] != 1) $html .= "s";
$html .= "</div></div>\n";
}
return $html;
}
public function game_final_inflation_pct(&$db_game) {
if ($db_game['final_round'] > 0) {
if ($db_game['inflation'] == "fixed_exponential" || $db_game['inflation'] == "exponential") {
$inflation_factor = pow(1+$db_game['exponential_inflation_rate'], $db_game['final_round']);
}
else {
if ($db_game['start_condition'] == "players_joined") {
$db_game['initial_coins'] = $db_game['start_condition_players']*$db_game['giveaway_amount'];
$final_coins = $this->ideal_coins_in_existence_after_round($db_game, $db_game['final_round']);
$inflation_factor = $final_coins/$db_game['initial_coins'];
}
else return false;
}
$inflation_pct = round(($inflation_factor-1)*100);