Source

timekoin / v1.9 / function.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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
<?PHP
include '/var/lib/timekoin/status.php';

define("TRANSACTION_EPOCH","1338576300"); // Epoch timestamp: 1338576300
define("TIMEKOIN_VERSION","1.9"); // Software Version
define("ARBITRARY_KEY","01110100011010010110110101100101"); // Space filler for non-encryption data
define("SHA256TEST","8c49a2b56ebd8fc49a17956dc529943eb0d73c00ee6eafa5d8b3ba1274eb3ea4"); // Known SHA256 Test Result

error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR); // Disable most error reporting except for fatal errors
ini_set('display_errors', FALSE);

//***********************************************************************************
//***********************************************************************************
global $timekoin_debug;
$timekoin_debug = TRUE;
function timekoin_debug_msg($stream, $string)
{
  global $timekoin_debug;
  if ($timekoin_debug) {
    $fh = fopen("/var/log/timekoin/" . $stream . ".debug.log", 'a');
    if (!$fh) { return FALSE; }
    fwrite($fh, $string);
    fwrite($fh, "\n");
    fclose($fh);
  };
}
function last_debug_msg($stream)
{
  global $timekoin_debug;
  $fh = fopen("/var/log/timekoin/" . $stream . ".debug.log", 'r');
  if (!$fh) { return FALSE; }
  fseek($fh, -75, SEEK_END);
  $s = fread($fh, 75);
  fclose($fh);
  return
    preg_replace(
      "/[^:]*: */", "",
      preg_replace(
	"/([^\n]*\n)*/", "",
	preg_replace(
	  "/\n[^\n]*\\z/", "",
	  $s)));
}
//***********************************************************************************
//***********************************************************************************
//* Execute a source file in the background
function source_bg($file, $nice = FALSE)
{
  if(getenv("OS") == "Windows_NT") {
    pclose(popen("start /B " . ($nice ? "/BELOWNORMAL " : "") . "php " . $file, "r"));
  } else {
    exec("php " . $file . " > /dev/null &> /dev/null &");
  }			
}
//***********************************************************************************
//***********************************************************************************
function filter_sql($string)
{
	// Filter symbols that might lead to an SQL injection attack
	$symbols = array("'", "%", "*", "`");
	$string = str_replace($symbols, "", $string);

	return $string;
}
//***********************************************************************************
//***********************************************************************************
function activate($component = "SYSTEM", $on_or_off = 1)
{
	// Turn the entire or a single script on or off
	$build_file = '<?PHP ';

	// Check what the current constants are
	if($component != "TIMEKOINSYSTEM")	{ $build_file = $build_file . ' define("TIMEKOIN_DISABLED","' . TIMEKOIN_DISABLED . '"); '; }
	if($component != "FOUNDATION") { $build_file = $build_file . ' define("FOUNDATION_DISABLED","' . FOUNDATION_DISABLED . '"); '; }
	if($component != "GENERATION") { $build_file = $build_file . ' define("GENERATION_DISABLED","' . GENERATION_DISABLED . '"); '; }
	if($component != "GENPEER") { $build_file = $build_file . ' define("GENPEER_DISABLED","' . GENPEER_DISABLED . '"); '; }
	if($component != "PEERLIST") { $build_file = $build_file . ' define("PEERLIST_DISABLED","' . PEERLIST_DISABLED . '"); '; }
	if($component != "QUEUECLERK") { $build_file = $build_file . ' define("QUEUECLERK_DISABLED","' . QUEUECLERK_DISABLED . '"); '; }
	if($component != "TRANSCLERK") { $build_file = $build_file . ' define("TRANSCLERK_DISABLED","' . TRANSCLERK_DISABLED . '"); '; }
	if($component != "TREASURER") { $build_file = $build_file . ' define("TREASURER_DISABLED","' . TREASURER_DISABLED . '"); '; }

	switch($component)
	{
		case "TIMEKOINSYSTEM":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("TIMEKOIN_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("TIMEKOIN_DISABLED","0"); ';
			}
			break;

		case "FOUNDATION":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("FOUNDATION_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("FOUNDATION_DISABLED","0"); ';
			}
			break;

		case "GENERATION":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("GENERATION_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("GENERATION_DISABLED","0"); ';
			}
			break;

		case "GENPEER":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("GENPEER_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("GENPEER_DISABLED","0"); ';
			}
			break;

		case "PEERLIST":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("PEERLIST_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("PEERLIST_DISABLED","0"); ';
			}
			break;

		case "QUEUECLERK":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("QUEUECLERK_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("QUEUECLERK_DISABLED","0"); ';
			}
			break;

		case "TRANSCLERK":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("TRANSCLERK_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("TRANSCLERK_DISABLED","0"); ';
			}
			break;

		case "TREASURER":
			if($on_or_off == 0)
			{
				$build_file = $build_file . ' define("TREASURER_DISABLED","1"); ';
			}
			else
			{
				$build_file = $build_file . ' define("TREASURER_DISABLED","0"); ';
			}
			break;			
	}

	$build_file = $build_file . ' ?' . '>';

	// Save status.php file to the same directory the script was
	// called from.
        $fname = uniqid("/var/lib/timekoin/status.php.");
	$fh = fopen($fname, 'w');

	if($fh != FALSE)
	{
		if(fwrite($fh, $build_file) > 0)
		{
			if(fclose($fh) == TRUE)
			{
                                return (rename($fname, "/var/lib/timekoin/status.php") == TRUE);
			}
		}
	}

	return FALSE;
}
//***********************************************************************************
//***********************************************************************************
function find_string($start_tag, $end_tag, $full_string, $end_match = FALSE)
{
	$delimiter = '|';
	
	if($end_match == FALSE)
	{
		$regex = $delimiter . preg_quote($start_tag, $delimiter) . '(.*?)'  . preg_quote($end_tag, $delimiter)  . $delimiter  . 's';
	}
	else
	{
		$regex = $delimiter . preg_quote($start_tag, $delimiter) . '(.*)'  . preg_quote($end_tag, $delimiter)  . $delimiter  . 's';
	}

	preg_match_all($regex,$full_string,$matches);

	foreach($matches[1] as $found_string)
	{
	}
	
	return $found_string;
}
//***********************************************************************************
//***********************************************************************************
function write_log($message, $type)
{
	// Write Log Entry
	mysql_query("INSERT INTO `activity_logs` (`timestamp` ,`log` ,`attribute`)	
		VALUES ('" . time() . "', '" . substr($message, 0, 256) . "', '$type')");
	return;
}
//***********************************************************************************
//***********************************************************************************
function transaction_cycle($past_or_future = 0, $transacton_cycles_only = 0)
{
	$transacton_cycles = (time() - TRANSACTION_EPOCH) / 300;

	// Return the last transaction cycle
	if($transacton_cycles_only == TRUE)
	{
		return intval($transacton_cycles + $past_or_future);
	}
	else
	{
		return TRANSACTION_EPOCH + (intval($transacton_cycles + $past_or_future) * 300);
	}
}
//***********************************************************************************
//***********************************************************************************
function foundation_cycle($past_or_future = 0, $foundation_cycles_only = 0)
{
	$foundation_cycles = (time() - TRANSACTION_EPOCH) / 150000;

	// Return the last transaction cycle
	if($foundation_cycles_only == TRUE)
	{
		return intval($foundation_cycles + $past_or_future);
	}
	else
	{
		return TRANSACTION_EPOCH + (intval($foundation_cycles + $past_or_future) * 150000);
	}
}
//***********************************************************************************
//***********************************************************************************
function walkhistory($block_start = 0, $block_end = 0)
{
	$current_generation_cycle = transaction_cycle(0);
	$current_generation_block = transaction_cycle(0, TRUE);	
	
	$wrong_timestamp = 0;
	$wrong_hash = 0;

	$first_wrong_block = 0;

	if($block_end == 0)
	{
		$block_counter = $current_generation_block;
	}
	else
	{
		$block_counter = $block_end + 1;
	}

	if($block_start == 0)
	{
		$next_timestamp = TRANSACTION_EPOCH;
	}
	else
	{
		$next_timestamp = TRANSACTION_EPOCH + ($block_start * 300);
	}

	for ($i = $block_start; $i < $block_counter; $i++)
	{
		$time1 = transaction_cycle(0 - $current_generation_block + $i);
		$time2 = transaction_cycle(0 - $current_generation_block + 1 + $i);	

		$time3 = transaction_cycle(0 - $current_generation_block + 1 + $i);
		$time4 = transaction_cycle(0 - $current_generation_block + 2 + $i);
		$next_hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time3 AND `timestamp` < $time4 AND `attribute` = 'H' LIMIT 1"),0,"hash");

		$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";

		$sql_result = mysql_query($sql);
		$sql_num_results = mysql_num_rows($sql_result);
		$my_hash = 0;

		$timestamp = 0;

		for ($h = 0; $h < $sql_num_results; $h++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			
			if($sql_row["attribute"] == "H" || $sql_row["attribute"] == "B")
			{
				$timestamp = $sql_row["timestamp"];
			}

			$my_hash .= $sql_row["hash"];
		}		

		if($next_timestamp != $timestamp)
		{
			$wrong_timestamp++;

			if($first_wrong_block == 0)
			{
				$first_wrong_block = $i;
			}
		}
		
		$next_timestamp = $next_timestamp + 300;

		$my_hash = hash('sha256', $my_hash);

		if($my_hash == $next_hash)
		{
			// Good match for hash
		}
		else
		{
			// Wrong match for hash
			$wrong_hash++;

			if($first_wrong_block == 0)
			{
				$first_wrong_block = $i;
			}			
		}
	}

	if($wrong_timestamp > 0 || $wrong_hash > 0)
	{
		// Range of history walk contains errors, return the first block that the error
		// started at
		return $first_wrong_block;
	}
	else
	{
		// No errors found
		return 0;
	}
}
//***********************************************************************************
//***********************************************************************************
function check_crypt_balance_range($public_key, $block_start = 0, $block_end = 0)
{
	if($block_start == 0 && $block_end == 0)
	{
		// Find every Time Koin sent to this public Key
		$sql = "SELECT * FROM `transaction_history` WHERE `public_key_to` = '$public_key'";
	}
	else
	{
		// Find every Time Koin sent to this public Key in a certain time range.
		// Covert block to time.
		$start_time_range = TRANSACTION_EPOCH + ($block_start * 300);
		$end_time_range = TRANSACTION_EPOCH + ($block_end * 300);
		$sql = "SELECT * FROM `transaction_history` WHERE `public_key_to` = '$public_key' AND `timestamp` >= '$start_time_range' AND `timestamp` < '$end_time_range'";
	}

	$sql_result = mysql_query($sql);
	$sql_num_results = mysql_num_rows($sql_result);

	$crypto_balance = 0;	

	for ($i = 0; $i < $sql_num_results; $i++)
	{
		$sql_row = mysql_fetch_array($sql_result);
		
		$public_key_from = $sql_row["public_key_from"];
		$public_key_to = $sql_row["public_key_to"];		
		$crypt1 = $sql_row["crypt_data1"];
		$crypt2 = $sql_row["crypt_data2"];
		$crypt3 = $sql_row["crypt_data3"];
		$hash = $sql_row["hash"];
		$attribute = $sql_row["attribute"];

		if($attribute == "G" && $public_key_from == $public_key_to && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
		{
			// Currency Generation
			// Find destination public key
			openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
			openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);				
			$public_key_to = $public_key_to_1 . $public_key_to_2;

			// Decrypt transaction information
			openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);

			$transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
			$transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);

			// Check if a message is encoded in this data as well
			if(strlen($transaction_hash) != 64)
			{
				// A message is also encoded
				$transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
			}

			if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
			{
				// Transaction hash and real hash match
				$crypto_balance += $transaction_amount_sent;
			}
		}

		if($attribute == "T" && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
		{
			// Decrypt transaction --
			// Find destination public key
			openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
			openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);				
			$public_key_to = $public_key_to_1 . $public_key_to_2;

			// Decrypt transaction information
			openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);

			$transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
			$transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);

			// Check if a message is encoded in this data as well
			if(strlen($transaction_hash) != 64)
			{
				// A message is also encoded
				$transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
			}

			if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
			{
				// Transaction hash and real hash match
				$crypto_balance += $transaction_amount_sent;
			}
		}
	}
// END - Find every Time Koin sent to this public Key

 // Find every Time Koin sent FROM this public Key
	if($block_start == 0 && $block_end == 0)
	{
		// Find every Time Koin sent to this public Key
		$sql = "SELECT * FROM `transaction_history` WHERE `public_key_from` = '$public_key'";
	}
	else
	{
		// Find every Time Koin sent to this public Key in a certain time range
		$sql = "SELECT * FROM `transaction_history` WHERE `public_key_from` = '$public_key' AND `timestamp` >= '$start_time_range' AND `timestamp` < '$end_time_range'";
	}

	$sql_result = mysql_query($sql);
	$sql_num_results = mysql_num_rows($sql_result);

	for ($i = 0; $i < $sql_num_results; $i++)
	{
		$sql_row = mysql_fetch_array($sql_result);
		
		$public_key_from = $sql_row["public_key_from"];
		$public_key_to = $sql_row["public_key_to"];		
		$crypt1 = $sql_row["crypt_data1"];
		$crypt2 = $sql_row["crypt_data2"];
		$crypt3 = $sql_row["crypt_data3"];
		$hash = $sql_row["hash"];
		$attribute = $sql_row["attribute"];

		if($attribute == "T" && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
		{
			// Decrypt transaction --
			// Find destination public key
			openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
			openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);				
			$public_key_to = $public_key_to_1 . $public_key_to_2;

			// Decrypt transaction information
			openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);

			$transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
			$transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);

			// Check if a message is encoded in this data as well
			if(strlen($transaction_hash) != 64)
			{
				// A message is also encoded
				$transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
			}

			if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
			{
				// Transaction hash and real hash match
				$crypto_balance -= $transaction_amount_sent;
			}
		}
	}
// END - Find every Time Koin sent FROM this public Key

	return $crypto_balance;
}
//***********************************************************************************
//***********************************************************************************
function check_crypt_balance($public_key)
{
	if(empty($public_key) == TRUE)
	{
		return 0;
	}

	// Do we already have an index to reference for faster access?
	$public_key_hash = hash('md5', $public_key);
	$current_generation_block = transaction_cycle(0, TRUE);
	$current_foundation_block = foundation_cycle(0, TRUE);

	// Check to make sure enough lead time exist in advance to building
	// another balance index. (60 blocks) or 5 hours
	if($current_generation_block - ($current_foundation_block * 500) > 60)
	{
		// -1 Foundation Blocks (Standard)
		$previous_foundation_block = foundation_cycle(-1, TRUE);
	}
	else
	{
		// -2 Foundation Blocks - Buffers 5 hours after the newest foundation block
		$previous_foundation_block = foundation_cycle(-2, TRUE);
	}

	$sql = "SELECT * FROM `balance_index` WHERE `block` = $previous_foundation_block AND `public_key_hash` = '$public_key_hash' LIMIT 1";
	$sql_result = mysql_query($sql);
	$sql_row = mysql_fetch_array($sql_result);

	if(empty($sql_row["block"]) == TRUE)
	{
		// No index exist yet, so after the balance check is complete, record the result
		// for later use
		$crypto_balance = 0;

		// Create time range
		$end_time_range = $previous_foundation_block * 500;
		$index_balance1 = check_crypt_balance_range($public_key, 0, $end_time_range);

		// Check balance between the last block and now
		$start_time_range = $end_time_range;
		$end_time_range = transaction_cycle(0, TRUE);
		$index_balance2 = check_crypt_balance_range($public_key, $start_time_range, $end_time_range);

		// Store index in database for future access
		$sql = "INSERT INTO `balance_index` (`block` ,`public_key_hash` ,`balance`)
		VALUES ('$previous_foundation_block', '$public_key_hash', '$index_balance1')";
		
		mysql_query($sql);

		return ($index_balance1 + $index_balance2);
	}
	else
	{
		$crypto_balance = $sql_row["balance"];

		// Check balance between the last block and now
		$start_time_range = $previous_foundation_block * 500;
		$end_time_range = transaction_cycle(0, TRUE);
		$index_balance = check_crypt_balance_range($public_key, $start_time_range, $end_time_range);		

		return ($crypto_balance + $index_balance);
	}
}
//***********************************************************************************
//***********************************************************************************
function peer_gen_amount($public_key)
{
	// 1 week = 604,800 seconds
	$join_peer_list = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$public_key' LIMIT 1"),0,"join_peer_list");

	if(empty($join_peer_list) == TRUE || $join_peer_list < TRANSACTION_EPOCH)
	{
		// Not found in the generating peer list
		return 0;
	}
	else
	{
		// How many weeks has this public key been in the peer list
		$peer_age = time() - $join_peer_list;
		$peer_age = intval($peer_age / 604800);

		$amount = 0;

		switch($peer_age)
		{
			case 0:
				$amount = 1;
				break;

			case 1:
				$amount = 2;
				break;

			case ($peer_age >= 2 && $peer_age <= 3):
				$amount = 3;
				break;

			case ($peer_age >= 4 && $peer_age <= 7):
				$amount = 4;
				break;

			case ($peer_age >= 8 && $peer_age <= 15):
				$amount = 5;
				break;

			case ($peer_age >= 16 && $peer_age <= 31):
				$amount = 6;
				break;

			case ($peer_age >= 32 && $peer_age <= 63):
				$amount = 7;
				break;

			case ($peer_age >= 64 && $peer_age <= 127):
				$amount = 8;
				break;

			case ($peer_age >= 128 && $peer_age <= 255):
				$amount = 9;
				break;

			case ($peer_age >= 256):
				$amount = 10;
				break;

			default:
				$amount = 1;
				break;				
		}
	}

	return $amount;
}
//***********************************************************************************
//***********************************************************************************
class TKRandom
{
	// random seed
	private static $RSeed = 0;
	// set seed
	public static function seed($s = 0)
  	{
		self::$RSeed = abs(intval($s)) % 9999999 + 1;
		self::num();
	}
	// generate random number
	public static function num($min = 0, $max = 9999999)
  	{
		if (self::$RSeed == 0) self::seed(mt_rand());
		self::$RSeed = (self::$RSeed * 125) % 2796203;
		return self::$RSeed % ($max - $min + 1) + $min;
	}
}
//***********************************************************************************
//***********************************************************************************
function getCharFreq($str,$chr=false)
{
	$c = Array();
	if ($chr!==false) return substr_count($str, $chr);
	foreach(preg_split('//',$str,-1,1)as$v)($c[$v])?$c[$v]++ :$c[$v]=1;
	return $c;
}
//***********************************************************************************
//***********************************************************************************
function scorePublicKey($public_key)
{
	$current_generation_block = transaction_cycle(0, TRUE);	

	TKRandom::seed($current_generation_block);

	$public_key_score = 0;
	$tkrandom_num = 0;
	$character = 0;

	for ($i = 0; $i < 18; $i++)
	{
		$tkrandom_num = TKRandom::num(1, 35);
		$character = base_convert($tkrandom_num, 10, 36);  // Base 10 to Base 36 conversion
		$public_key_score += getCharFreq($public_key, $character);
	}

	return $public_key_score;
}
//***********************************************************************************
//***********************************************************************************
function tk_time_convert($time)
{
	if($time < 0)
	{
		return "0 sec";
	}
	
	if($time < 60)
	{
		if($time == 1)
		{
			$time .= " sec";
		}
		else
		{
			$time .= " secs";
		}
	}
	else if($time >= 60 && $time < 3600)
	{
		if($time >= 60 && $time < 120)
		{
			$time = intval($time / 60) . " min";
		}
		else
		{
			$time = intval($time / 60) . " mins";
		}
	}
	else if($time >= 3600 && $time < 86400)
	{
		if($time >= 3600 && $time < 7200)
		{
			$time = intval($time / 3600) . " hour";
		}
		else
		{
			$time = intval($time / 3600) . " hours";
		}
	}
	else if($time >= 86400)
	{
		if($time >= 86400 && $time < 172800)
		{
			$time = intval($time / 86400) . " day";
		}
		else
		{
			$time = intval($time / 86400) . " days";
		}		
	}

	return $time;
}
//***********************************************************************************
//***********************************************************************************
function db_cache_balance($my_public_key)
{
	// Check server balance via custom memory index
	$my_server_balance = mysql_result(mysql_query("SELECT * FROM `balance_index` WHERE `public_key_hash` = 'server_timekoin_balance' LIMIT 1"),0,"balance");
	$my_server_balance_last = mysql_result(mysql_query("SELECT * FROM `balance_index` WHERE `public_key_hash` = 'server_timekoin_balance' LIMIT 1"),0,"block");

	if($my_server_balance === FALSE)
	{
		// Does not exist, needs to be created
		$sql = "INSERT INTO `timekoin`.`balance_index` (`block` ,`public_key_hash` ,`balance`)VALUES ('0', 'server_timekoin_balance', '0')";
		mysql_query($sql);

		// Update record with the latest balance
		$display_balance = check_crypt_balance($my_public_key);

		$sql = "UPDATE `balance_index` SET `block` = '" . time() . "' , `balance` = '$display_balance' WHERE `balance_index`.`public_key_hash` = 'server_timekoin_balance' LIMIT 1";
		mysql_query($sql);
	}
	else
	{
		if($my_server_balance_last < transaction_cycle(0) && time() - transaction_cycle(0) > 25) // Generate 25 seconds after cycle
		{
			// Last generated balance is older than the current cycle, needs to be updated
			// Update record with the latest balance
			$display_balance = check_crypt_balance($my_public_key);

			$sql = "UPDATE `balance_index` SET `block` = '" . time() . "' , `balance` = '$display_balance' WHERE `balance_index`.`public_key_hash` = 'server_timekoin_balance' LIMIT 1";
			mysql_query($sql);
		}
		else
		{
			$display_balance = $my_server_balance;
		}
	}

	return $display_balance;
}
//***********************************************************************************
//***********************************************************************************
function send_timekoins($my_private_key, $my_public_key, $send_to_public_key, $amount, $message)
{
	$arr1 = str_split($send_to_public_key, 181);
	openssl_private_encrypt($arr1[0], $encryptedData1, $my_private_key);
	$encryptedData64_1 = base64_encode($encryptedData1);
	openssl_private_encrypt($arr1[1], $encryptedData2, $my_private_key);
	$encryptedData64_2 = base64_encode($encryptedData2);

	if(empty($message) == TRUE)
	{
		$transaction_data = "AMOUNT=$amount---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2);
	}
	else
	{
		// Sanitization of message
		// Filter symbols that might lead to a transaction hack attack
		$symbols = array("|", "?", "="); // SQL + URL
		$message = str_replace($symbols, "", $message);

		// Trim any message to 64 characters max and filter any sql
		$message = filter_sql(substr($message, 0, 64));
		
		$transaction_data = "AMOUNT=$amount---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2) . "---MSG=$message";
	}

	openssl_private_encrypt($transaction_data, $encryptedData3, $my_private_key);
	$encryptedData64_3 = base64_encode($encryptedData3);
	$triple_hash_check = hash('sha256', $encryptedData64_1 . $encryptedData64_2 . $encryptedData64_3);

	$sql = "INSERT INTO `my_transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)
VALUES ('" . time() . "', '$my_public_key', '$encryptedData64_1', '$encryptedData64_2' , '$encryptedData64_3', '$triple_hash_check' , 'T')";

	if(mysql_query($sql) == TRUE)
	{
		// Success code
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
//***********************************************************************************
//***********************************************************************************
function unix_timestamp_to_human($timestamp = "", $format = 'D d M Y - H:i:s')
{
	 if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
	 return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
}
//***********************************************************************************
//***********************************************************************************
function visual_walkhistory($block_start = 0, $block_end = 0)
{
	$output;

	$current_generation_block = transaction_cycle(0, TRUE);

	if($block_end <= $block_start)
	{
		$block_end = $block_start + 1;
	}

	if($block_end > $current_generation_block)
	{
		$block_end = $current_generation_block;
	}	

	$wrong_timestamp = 0;
	$wrong_block_numbers = NULL;
	$wrong_hash = 0;
	$wrong_hash_numbers = NULL;

	$next_timestamp = TRANSACTION_EPOCH + ($block_start * 300);

	for ($i = $block_start; $i < $block_end; $i++)
	{
		$output = $output . '<tr><td class="style2">Block # ' . $i;
		$time1 = transaction_cycle(0 - $current_generation_block + $i);
		$time2 = transaction_cycle(0 - $current_generation_block + 1 + $i);	

		$time3 = transaction_cycle(0 - $current_generation_block + 1 + $i);
		$time4 = transaction_cycle(0 - $current_generation_block + 2 + $i);
		
		$next_hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time3 AND `timestamp` < $time4 AND `attribute` = 'H' LIMIT 1"),0,"hash");

		$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";

		$sql_result = mysql_query($sql);
		$sql_num_results = mysql_num_rows($sql_result);
		$my_hash = 0;
		$timestamp = 0;

		for ($h = 0; $h < $sql_num_results; $h++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			
			if($sql_row["attribute"] == "H" || $sql_row["attribute"] == "B")
			{
				$timestamp = $sql_row["timestamp"];
			}

			$my_hash .= $sql_row["hash"];
		}		

		if($next_timestamp != $timestamp)
		{
			$output = $output . '</br><strong><font color=red>Hash Timestamp Sequence Wrong... Should Be: ' . $next_timestamp . '</font></strong>';
			$wrong_timestamp++;
			$wrong_block_numbers .= " " . $i;
		}
		
		$next_timestamp = $next_timestamp + 300;

		$my_hash = hash('sha256', $my_hash);

		$output .= '</br>Timestamp in Database: ' . $timestamp;
		$output .= '</br>Calculated Hash: ' . $my_hash;
		$output .= '</br>&nbsp;Database Hash : ' . $next_hash;

		if($my_hash == $next_hash)
		{
			$output .= '</br><font color=green>Hash Match...</font>';
		}
		else
		{
			$output .= '</br><strong><font color=red>Hash MISMATCH</font></strong></td></tr>';
			$wrong_hash++;
			$wrong_hash_numbers = $wrong_hash_numbers . " " . $i;			
		}
	}

	if(empty($wrong_block_numbers) == TRUE)
	{
		$wrong_block_numbers = '<font color="blue">None</font>';
	}

	if(empty($wrong_hash_numbers) == TRUE)
	{
		$wrong_hash_numbers = '<font color="blue">None</font>';
	}

	$output .= '<tr><td class="style2"><strong><font color="blue">Total Wrong Sequence: ' . $wrong_timestamp . '</strong></font>';
	$output .= '</br><strong><font color="red">Blocks Wrong:</font> ' . $wrong_block_numbers . '</strong></td></tr>';
	$output .= '<tr><td class="style2"><strong><font color="blue">Total Wrong Hash: ' . $wrong_hash . '</strong></font>';
	$output .= '</br><strong><font color="red">Blocks Wrong:</font> ' . $wrong_hash_numbers . '</strong></td></tr>';	

	return $output;

}
//***********************************************************************************
//***********************************************************************************
function visual_repair($block_start = 0)
{
	$current_generation_block = transaction_cycle(0, TRUE);
	$output;

	// Wipe all blocks ahead
	$time_range = transaction_cycle(0 - $current_generation_block + $block_start);

	$sql = "DELETE FROM `transaction_history` WHERE `transaction_history`.`timestamp` >= $time_range AND `attribute` = 'H'";

	if(mysql_query($sql) == TRUE)
	{
		$output .= '<tr><td class="style2">Clearing Hash Timestamps Ahead of Block #' . $block_start . '</td></tr>';
	}
	else
	{
		return '<tr><td class="style2">Database ERROR, stopping repair process...</td></tr>';
	}

	$generation_arbitrary = ARBITRARY_KEY;

	for ($t = $block_start; $t < $current_generation_block; $t++)
	{
		$output .= "<tr><td><strong>Repairing Block# $t</strong>";

		$time1 = transaction_cycle(0 - $current_generation_block - 1 + $t);
		$time2 = transaction_cycle(0 - $current_generation_block + $t);

		$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";

		$sql_result = mysql_query($sql);
		$sql_num_results = mysql_num_rows($sql_result);
		$hash = 0;

		for ($i = 0; $i < $sql_num_results; $i++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			$hash .= $sql_row["hash"];
		}

		// Transaction hash
		$hash = hash('sha256', $hash);

		$sql = "INSERT INTO `transaction_history` (`timestamp` ,`public_key_from` ,`public_key_to` ,`crypt_data1` ,`crypt_data2` ,`crypt_data3` ,`hash` ,`attribute`)
		VALUES ('$time2', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$hash', 'H')";

		if(mysql_query($sql) == FALSE)
		{
			// Something failed
			$output .= '</br><strong><font color="red">Repair ERROR in Database</font></strong></td></tr>';
		}
		else
		{
			$output .= '</br><strong><font color="blue">Repair Complete...</font></strong></td></tr>';
		}
	} // End for loop

	return $output;
}
//***********************************************************************************
//***********************************************************************************
function is_private_ip($ip, $ignore = FALSE)
{
	if(empty($ip) == TRUE)
	{
		return FALSE;
	}
	
	if($ignore == TRUE)
	{
		$result = FALSE;
	}
	else
	{
		if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) == FALSE)
		{
			$result = TRUE;
		}
	}
	
	return $result;
}
//***********************************************************************************
//***********************************************************************************
function initialization_database()
{
	// Clear IP Activity and Banlist for next start
	mysql_query("TRUNCATE TABLE `ip_activity`");
	mysql_query("TRUNCATE TABLE `ip_banlist`");

	// Clear Active & New Peers List
	mysql_query("DELETE FROM `active_peer_list` WHERE `active_peer_list`.`join_peer_list` != 0"); // Permanent Peers Ignored
	mysql_query("TRUNCATE TABLE `new_peers_list`");

	// Record when started
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'timekoin_start_time' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('timekoin_start_time', '" . time() . "')");
	}
	else
	{
		// Save start time for uptime timer
		mysql_query("UPDATE `options` SET `field_data` = '" . time() . "' WHERE `options`.`field_name` = 'timekoin_start_time' LIMIT 1");
	}

	// Allow LAN IPs in the Peer List
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'allow_LAN_peers' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('allow_LAN_peers', '0')");
	}

	// Allow Ambient Peer Restart
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'allow_ambient_peer_restart' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('allow_ambient_peer_restart', '0')");
	}

	// Max Queries for Flood Protection
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_request_max' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('server_request_max', '200')");
	}

	// Hash code for External Access
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_hash_code' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('server_hash_code', '0')");
	}

	// Time Sync Polling
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'time_sync_error' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('time_sync_error', '0')");
	}
//**************************************
	// Main Loop Status Setup
	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('genpeer_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('genpeer_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peerlist_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peerlist_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('queueclerk_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('queueclerk_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transclerk_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transclerk_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('treasurer_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('treasurer_last_heartbeat', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_heartbeat_active', '0')");
	}

	$new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,0);
	if($new_record_check === FALSE)
	{
		// Does not exist, create it
		mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_last_heartbeat', '0')");
	}
//**************************************
	return 0;
}
//***********************************************************************************
//***********************************************************************************
?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.