PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/v1.9/function.php

https://bitbucket.org/rev22/timekoin
PHP | 1222 lines | 1064 code | 82 blank | 76 comment | 87 complexity | b3cf4b3f9cd6d3f1f71009eb4c72fafc MD5 | raw file
  1. <?PHP
  2. include '/var/lib/timekoin/status.php';
  3. define("TRANSACTION_EPOCH","1338576300"); // Epoch timestamp: 1338576300
  4. define("TIMEKOIN_VERSION","1.9"); // Software Version
  5. define("ARBITRARY_KEY","01110100011010010110110101100101"); // Space filler for non-encryption data
  6. define("SHA256TEST","8c49a2b56ebd8fc49a17956dc529943eb0d73c00ee6eafa5d8b3ba1274eb3ea4"); // Known SHA256 Test Result
  7. error_reporting(E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR); // Disable most error reporting except for fatal errors
  8. ini_set('display_errors', FALSE);
  9. //***********************************************************************************
  10. //***********************************************************************************
  11. global $timekoin_debug;
  12. $timekoin_debug = TRUE;
  13. function timekoin_debug_msg($stream, $string)
  14. {
  15. global $timekoin_debug;
  16. if ($timekoin_debug) {
  17. $fh = fopen("/var/log/timekoin/" . $stream . ".debug.log", 'a');
  18. if (!$fh) { return FALSE; }
  19. fwrite($fh, $string);
  20. fwrite($fh, "\n");
  21. fclose($fh);
  22. };
  23. }
  24. function last_debug_msg($stream)
  25. {
  26. global $timekoin_debug;
  27. $fh = fopen("/var/log/timekoin/" . $stream . ".debug.log", 'r');
  28. if (!$fh) { return FALSE; }
  29. fseek($fh, -75, SEEK_END);
  30. $s = fread($fh, 75);
  31. fclose($fh);
  32. return
  33. preg_replace(
  34. "/[^:]*: */", "",
  35. preg_replace(
  36. "/([^\n]*\n)*/", "",
  37. preg_replace(
  38. "/\n[^\n]*\\z/", "",
  39. $s)));
  40. }
  41. //***********************************************************************************
  42. //***********************************************************************************
  43. //* Execute a source file in the background
  44. function source_bg($file, $nice = FALSE)
  45. {
  46. if(getenv("OS") == "Windows_NT") {
  47. pclose(popen("start /B " . ($nice ? "/BELOWNORMAL " : "") . "php " . $file, "r"));
  48. } else {
  49. exec("php " . $file . " > /dev/null &> /dev/null &");
  50. }
  51. }
  52. //***********************************************************************************
  53. //***********************************************************************************
  54. function filter_sql($string)
  55. {
  56. // Filter symbols that might lead to an SQL injection attack
  57. $symbols = array("'", "%", "*", "`");
  58. $string = str_replace($symbols, "", $string);
  59. return $string;
  60. }
  61. //***********************************************************************************
  62. //***********************************************************************************
  63. function activate($component = "SYSTEM", $on_or_off = 1)
  64. {
  65. // Turn the entire or a single script on or off
  66. $build_file = '<?PHP ';
  67. // Check what the current constants are
  68. if($component != "TIMEKOINSYSTEM") { $build_file = $build_file . ' define("TIMEKOIN_DISABLED","' . TIMEKOIN_DISABLED . '"); '; }
  69. if($component != "FOUNDATION") { $build_file = $build_file . ' define("FOUNDATION_DISABLED","' . FOUNDATION_DISABLED . '"); '; }
  70. if($component != "GENERATION") { $build_file = $build_file . ' define("GENERATION_DISABLED","' . GENERATION_DISABLED . '"); '; }
  71. if($component != "GENPEER") { $build_file = $build_file . ' define("GENPEER_DISABLED","' . GENPEER_DISABLED . '"); '; }
  72. if($component != "PEERLIST") { $build_file = $build_file . ' define("PEERLIST_DISABLED","' . PEERLIST_DISABLED . '"); '; }
  73. if($component != "QUEUECLERK") { $build_file = $build_file . ' define("QUEUECLERK_DISABLED","' . QUEUECLERK_DISABLED . '"); '; }
  74. if($component != "TRANSCLERK") { $build_file = $build_file . ' define("TRANSCLERK_DISABLED","' . TRANSCLERK_DISABLED . '"); '; }
  75. if($component != "TREASURER") { $build_file = $build_file . ' define("TREASURER_DISABLED","' . TREASURER_DISABLED . '"); '; }
  76. switch($component)
  77. {
  78. case "TIMEKOINSYSTEM":
  79. if($on_or_off == 0)
  80. {
  81. $build_file = $build_file . ' define("TIMEKOIN_DISABLED","1"); ';
  82. }
  83. else
  84. {
  85. $build_file = $build_file . ' define("TIMEKOIN_DISABLED","0"); ';
  86. }
  87. break;
  88. case "FOUNDATION":
  89. if($on_or_off == 0)
  90. {
  91. $build_file = $build_file . ' define("FOUNDATION_DISABLED","1"); ';
  92. }
  93. else
  94. {
  95. $build_file = $build_file . ' define("FOUNDATION_DISABLED","0"); ';
  96. }
  97. break;
  98. case "GENERATION":
  99. if($on_or_off == 0)
  100. {
  101. $build_file = $build_file . ' define("GENERATION_DISABLED","1"); ';
  102. }
  103. else
  104. {
  105. $build_file = $build_file . ' define("GENERATION_DISABLED","0"); ';
  106. }
  107. break;
  108. case "GENPEER":
  109. if($on_or_off == 0)
  110. {
  111. $build_file = $build_file . ' define("GENPEER_DISABLED","1"); ';
  112. }
  113. else
  114. {
  115. $build_file = $build_file . ' define("GENPEER_DISABLED","0"); ';
  116. }
  117. break;
  118. case "PEERLIST":
  119. if($on_or_off == 0)
  120. {
  121. $build_file = $build_file . ' define("PEERLIST_DISABLED","1"); ';
  122. }
  123. else
  124. {
  125. $build_file = $build_file . ' define("PEERLIST_DISABLED","0"); ';
  126. }
  127. break;
  128. case "QUEUECLERK":
  129. if($on_or_off == 0)
  130. {
  131. $build_file = $build_file . ' define("QUEUECLERK_DISABLED","1"); ';
  132. }
  133. else
  134. {
  135. $build_file = $build_file . ' define("QUEUECLERK_DISABLED","0"); ';
  136. }
  137. break;
  138. case "TRANSCLERK":
  139. if($on_or_off == 0)
  140. {
  141. $build_file = $build_file . ' define("TRANSCLERK_DISABLED","1"); ';
  142. }
  143. else
  144. {
  145. $build_file = $build_file . ' define("TRANSCLERK_DISABLED","0"); ';
  146. }
  147. break;
  148. case "TREASURER":
  149. if($on_or_off == 0)
  150. {
  151. $build_file = $build_file . ' define("TREASURER_DISABLED","1"); ';
  152. }
  153. else
  154. {
  155. $build_file = $build_file . ' define("TREASURER_DISABLED","0"); ';
  156. }
  157. break;
  158. }
  159. $build_file = $build_file . ' ?' . '>';
  160. // Save status.php file to the same directory the script was
  161. // called from.
  162. $fname = uniqid("/var/lib/timekoin/status.php.");
  163. $fh = fopen($fname, 'w');
  164. if($fh != FALSE)
  165. {
  166. if(fwrite($fh, $build_file) > 0)
  167. {
  168. if(fclose($fh) == TRUE)
  169. {
  170. return (rename($fname, "/var/lib/timekoin/status.php") == TRUE);
  171. }
  172. }
  173. }
  174. return FALSE;
  175. }
  176. //***********************************************************************************
  177. //***********************************************************************************
  178. function find_string($start_tag, $end_tag, $full_string, $end_match = FALSE)
  179. {
  180. $delimiter = '|';
  181. if($end_match == FALSE)
  182. {
  183. $regex = $delimiter . preg_quote($start_tag, $delimiter) . '(.*?)' . preg_quote($end_tag, $delimiter) . $delimiter . 's';
  184. }
  185. else
  186. {
  187. $regex = $delimiter . preg_quote($start_tag, $delimiter) . '(.*)' . preg_quote($end_tag, $delimiter) . $delimiter . 's';
  188. }
  189. preg_match_all($regex,$full_string,$matches);
  190. foreach($matches[1] as $found_string)
  191. {
  192. }
  193. return $found_string;
  194. }
  195. //***********************************************************************************
  196. //***********************************************************************************
  197. function write_log($message, $type)
  198. {
  199. // Write Log Entry
  200. mysql_query("INSERT INTO `activity_logs` (`timestamp` ,`log` ,`attribute`)
  201. VALUES ('" . time() . "', '" . substr($message, 0, 256) . "', '$type')");
  202. return;
  203. }
  204. //***********************************************************************************
  205. //***********************************************************************************
  206. function transaction_cycle($past_or_future = 0, $transacton_cycles_only = 0)
  207. {
  208. $transacton_cycles = (time() - TRANSACTION_EPOCH) / 300;
  209. // Return the last transaction cycle
  210. if($transacton_cycles_only == TRUE)
  211. {
  212. return intval($transacton_cycles + $past_or_future);
  213. }
  214. else
  215. {
  216. return TRANSACTION_EPOCH + (intval($transacton_cycles + $past_or_future) * 300);
  217. }
  218. }
  219. //***********************************************************************************
  220. //***********************************************************************************
  221. function foundation_cycle($past_or_future = 0, $foundation_cycles_only = 0)
  222. {
  223. $foundation_cycles = (time() - TRANSACTION_EPOCH) / 150000;
  224. // Return the last transaction cycle
  225. if($foundation_cycles_only == TRUE)
  226. {
  227. return intval($foundation_cycles + $past_or_future);
  228. }
  229. else
  230. {
  231. return TRANSACTION_EPOCH + (intval($foundation_cycles + $past_or_future) * 150000);
  232. }
  233. }
  234. //***********************************************************************************
  235. //***********************************************************************************
  236. function walkhistory($block_start = 0, $block_end = 0)
  237. {
  238. $current_generation_cycle = transaction_cycle(0);
  239. $current_generation_block = transaction_cycle(0, TRUE);
  240. $wrong_timestamp = 0;
  241. $wrong_hash = 0;
  242. $first_wrong_block = 0;
  243. if($block_end == 0)
  244. {
  245. $block_counter = $current_generation_block;
  246. }
  247. else
  248. {
  249. $block_counter = $block_end + 1;
  250. }
  251. if($block_start == 0)
  252. {
  253. $next_timestamp = TRANSACTION_EPOCH;
  254. }
  255. else
  256. {
  257. $next_timestamp = TRANSACTION_EPOCH + ($block_start * 300);
  258. }
  259. for ($i = $block_start; $i < $block_counter; $i++)
  260. {
  261. $time1 = transaction_cycle(0 - $current_generation_block + $i);
  262. $time2 = transaction_cycle(0 - $current_generation_block + 1 + $i);
  263. $time3 = transaction_cycle(0 - $current_generation_block + 1 + $i);
  264. $time4 = transaction_cycle(0 - $current_generation_block + 2 + $i);
  265. $next_hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time3 AND `timestamp` < $time4 AND `attribute` = 'H' LIMIT 1"),0,"hash");
  266. $sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";
  267. $sql_result = mysql_query($sql);
  268. $sql_num_results = mysql_num_rows($sql_result);
  269. $my_hash = 0;
  270. $timestamp = 0;
  271. for ($h = 0; $h < $sql_num_results; $h++)
  272. {
  273. $sql_row = mysql_fetch_array($sql_result);
  274. if($sql_row["attribute"] == "H" || $sql_row["attribute"] == "B")
  275. {
  276. $timestamp = $sql_row["timestamp"];
  277. }
  278. $my_hash .= $sql_row["hash"];
  279. }
  280. if($next_timestamp != $timestamp)
  281. {
  282. $wrong_timestamp++;
  283. if($first_wrong_block == 0)
  284. {
  285. $first_wrong_block = $i;
  286. }
  287. }
  288. $next_timestamp = $next_timestamp + 300;
  289. $my_hash = hash('sha256', $my_hash);
  290. if($my_hash == $next_hash)
  291. {
  292. // Good match for hash
  293. }
  294. else
  295. {
  296. // Wrong match for hash
  297. $wrong_hash++;
  298. if($first_wrong_block == 0)
  299. {
  300. $first_wrong_block = $i;
  301. }
  302. }
  303. }
  304. if($wrong_timestamp > 0 || $wrong_hash > 0)
  305. {
  306. // Range of history walk contains errors, return the first block that the error
  307. // started at
  308. return $first_wrong_block;
  309. }
  310. else
  311. {
  312. // No errors found
  313. return 0;
  314. }
  315. }
  316. //***********************************************************************************
  317. //***********************************************************************************
  318. function check_crypt_balance_range($public_key, $block_start = 0, $block_end = 0)
  319. {
  320. if($block_start == 0 && $block_end == 0)
  321. {
  322. // Find every Time Koin sent to this public Key
  323. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_to` = '$public_key'";
  324. }
  325. else
  326. {
  327. // Find every Time Koin sent to this public Key in a certain time range.
  328. // Covert block to time.
  329. $start_time_range = TRANSACTION_EPOCH + ($block_start * 300);
  330. $end_time_range = TRANSACTION_EPOCH + ($block_end * 300);
  331. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_to` = '$public_key' AND `timestamp` >= '$start_time_range' AND `timestamp` < '$end_time_range'";
  332. }
  333. $sql_result = mysql_query($sql);
  334. $sql_num_results = mysql_num_rows($sql_result);
  335. $crypto_balance = 0;
  336. for ($i = 0; $i < $sql_num_results; $i++)
  337. {
  338. $sql_row = mysql_fetch_array($sql_result);
  339. $public_key_from = $sql_row["public_key_from"];
  340. $public_key_to = $sql_row["public_key_to"];
  341. $crypt1 = $sql_row["crypt_data1"];
  342. $crypt2 = $sql_row["crypt_data2"];
  343. $crypt3 = $sql_row["crypt_data3"];
  344. $hash = $sql_row["hash"];
  345. $attribute = $sql_row["attribute"];
  346. if($attribute == "G" && $public_key_from == $public_key_to && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
  347. {
  348. // Currency Generation
  349. // Find destination public key
  350. openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
  351. openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);
  352. $public_key_to = $public_key_to_1 . $public_key_to_2;
  353. // Decrypt transaction information
  354. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);
  355. $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
  356. $transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
  357. // Check if a message is encoded in this data as well
  358. if(strlen($transaction_hash) != 64)
  359. {
  360. // A message is also encoded
  361. $transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
  362. }
  363. if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
  364. {
  365. // Transaction hash and real hash match
  366. $crypto_balance += $transaction_amount_sent;
  367. }
  368. }
  369. if($attribute == "T" && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
  370. {
  371. // Decrypt transaction --
  372. // Find destination public key
  373. openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
  374. openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);
  375. $public_key_to = $public_key_to_1 . $public_key_to_2;
  376. // Decrypt transaction information
  377. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);
  378. $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
  379. $transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
  380. // Check if a message is encoded in this data as well
  381. if(strlen($transaction_hash) != 64)
  382. {
  383. // A message is also encoded
  384. $transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
  385. }
  386. if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
  387. {
  388. // Transaction hash and real hash match
  389. $crypto_balance += $transaction_amount_sent;
  390. }
  391. }
  392. }
  393. // END - Find every Time Koin sent to this public Key
  394. // Find every Time Koin sent FROM this public Key
  395. if($block_start == 0 && $block_end == 0)
  396. {
  397. // Find every Time Koin sent to this public Key
  398. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_from` = '$public_key'";
  399. }
  400. else
  401. {
  402. // Find every Time Koin sent to this public Key in a certain time range
  403. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_from` = '$public_key' AND `timestamp` >= '$start_time_range' AND `timestamp` < '$end_time_range'";
  404. }
  405. $sql_result = mysql_query($sql);
  406. $sql_num_results = mysql_num_rows($sql_result);
  407. for ($i = 0; $i < $sql_num_results; $i++)
  408. {
  409. $sql_row = mysql_fetch_array($sql_result);
  410. $public_key_from = $sql_row["public_key_from"];
  411. $public_key_to = $sql_row["public_key_to"];
  412. $crypt1 = $sql_row["crypt_data1"];
  413. $crypt2 = $sql_row["crypt_data2"];
  414. $crypt3 = $sql_row["crypt_data3"];
  415. $hash = $sql_row["hash"];
  416. $attribute = $sql_row["attribute"];
  417. if($attribute == "T" && $hash == hash('sha256', $crypt1 . $crypt2 . $crypt3))
  418. {
  419. // Decrypt transaction --
  420. // Find destination public key
  421. openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_from);
  422. openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_from);
  423. $public_key_to = $public_key_to_1 . $public_key_to_2;
  424. // Decrypt transaction information
  425. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_from);
  426. $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
  427. $transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
  428. // Check if a message is encoded in this data as well
  429. if(strlen($transaction_hash) != 64)
  430. {
  431. // A message is also encoded
  432. $transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
  433. }
  434. if($transaction_hash == hash('sha256', $crypt1 . $crypt2))
  435. {
  436. // Transaction hash and real hash match
  437. $crypto_balance -= $transaction_amount_sent;
  438. }
  439. }
  440. }
  441. // END - Find every Time Koin sent FROM this public Key
  442. return $crypto_balance;
  443. }
  444. //***********************************************************************************
  445. //***********************************************************************************
  446. function check_crypt_balance($public_key)
  447. {
  448. if(empty($public_key) == TRUE)
  449. {
  450. return 0;
  451. }
  452. // Do we already have an index to reference for faster access?
  453. $public_key_hash = hash('md5', $public_key);
  454. $current_generation_block = transaction_cycle(0, TRUE);
  455. $current_foundation_block = foundation_cycle(0, TRUE);
  456. // Check to make sure enough lead time exist in advance to building
  457. // another balance index. (60 blocks) or 5 hours
  458. if($current_generation_block - ($current_foundation_block * 500) > 60)
  459. {
  460. // -1 Foundation Blocks (Standard)
  461. $previous_foundation_block = foundation_cycle(-1, TRUE);
  462. }
  463. else
  464. {
  465. // -2 Foundation Blocks - Buffers 5 hours after the newest foundation block
  466. $previous_foundation_block = foundation_cycle(-2, TRUE);
  467. }
  468. $sql = "SELECT * FROM `balance_index` WHERE `block` = $previous_foundation_block AND `public_key_hash` = '$public_key_hash' LIMIT 1";
  469. $sql_result = mysql_query($sql);
  470. $sql_row = mysql_fetch_array($sql_result);
  471. if(empty($sql_row["block"]) == TRUE)
  472. {
  473. // No index exist yet, so after the balance check is complete, record the result
  474. // for later use
  475. $crypto_balance = 0;
  476. // Create time range
  477. $end_time_range = $previous_foundation_block * 500;
  478. $index_balance1 = check_crypt_balance_range($public_key, 0, $end_time_range);
  479. // Check balance between the last block and now
  480. $start_time_range = $end_time_range;
  481. $end_time_range = transaction_cycle(0, TRUE);
  482. $index_balance2 = check_crypt_balance_range($public_key, $start_time_range, $end_time_range);
  483. // Store index in database for future access
  484. $sql = "INSERT INTO `balance_index` (`block` ,`public_key_hash` ,`balance`)
  485. VALUES ('$previous_foundation_block', '$public_key_hash', '$index_balance1')";
  486. mysql_query($sql);
  487. return ($index_balance1 + $index_balance2);
  488. }
  489. else
  490. {
  491. $crypto_balance = $sql_row["balance"];
  492. // Check balance between the last block and now
  493. $start_time_range = $previous_foundation_block * 500;
  494. $end_time_range = transaction_cycle(0, TRUE);
  495. $index_balance = check_crypt_balance_range($public_key, $start_time_range, $end_time_range);
  496. return ($crypto_balance + $index_balance);
  497. }
  498. }
  499. //***********************************************************************************
  500. //***********************************************************************************
  501. function peer_gen_amount($public_key)
  502. {
  503. // 1 week = 604,800 seconds
  504. $join_peer_list = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$public_key' LIMIT 1"),0,"join_peer_list");
  505. if(empty($join_peer_list) == TRUE || $join_peer_list < TRANSACTION_EPOCH)
  506. {
  507. // Not found in the generating peer list
  508. return 0;
  509. }
  510. else
  511. {
  512. // How many weeks has this public key been in the peer list
  513. $peer_age = time() - $join_peer_list;
  514. $peer_age = intval($peer_age / 604800);
  515. $amount = 0;
  516. switch($peer_age)
  517. {
  518. case 0:
  519. $amount = 1;
  520. break;
  521. case 1:
  522. $amount = 2;
  523. break;
  524. case ($peer_age >= 2 && $peer_age <= 3):
  525. $amount = 3;
  526. break;
  527. case ($peer_age >= 4 && $peer_age <= 7):
  528. $amount = 4;
  529. break;
  530. case ($peer_age >= 8 && $peer_age <= 15):
  531. $amount = 5;
  532. break;
  533. case ($peer_age >= 16 && $peer_age <= 31):
  534. $amount = 6;
  535. break;
  536. case ($peer_age >= 32 && $peer_age <= 63):
  537. $amount = 7;
  538. break;
  539. case ($peer_age >= 64 && $peer_age <= 127):
  540. $amount = 8;
  541. break;
  542. case ($peer_age >= 128 && $peer_age <= 255):
  543. $amount = 9;
  544. break;
  545. case ($peer_age >= 256):
  546. $amount = 10;
  547. break;
  548. default:
  549. $amount = 1;
  550. break;
  551. }
  552. }
  553. return $amount;
  554. }
  555. //***********************************************************************************
  556. //***********************************************************************************
  557. class TKRandom
  558. {
  559. // random seed
  560. private static $RSeed = 0;
  561. // set seed
  562. public static function seed($s = 0)
  563. {
  564. self::$RSeed = abs(intval($s)) % 9999999 + 1;
  565. self::num();
  566. }
  567. // generate random number
  568. public static function num($min = 0, $max = 9999999)
  569. {
  570. if (self::$RSeed == 0) self::seed(mt_rand());
  571. self::$RSeed = (self::$RSeed * 125) % 2796203;
  572. return self::$RSeed % ($max - $min + 1) + $min;
  573. }
  574. }
  575. //***********************************************************************************
  576. //***********************************************************************************
  577. function getCharFreq($str,$chr=false)
  578. {
  579. $c = Array();
  580. if ($chr!==false) return substr_count($str, $chr);
  581. foreach(preg_split('//',$str,-1,1)as$v)($c[$v])?$c[$v]++ :$c[$v]=1;
  582. return $c;
  583. }
  584. //***********************************************************************************
  585. //***********************************************************************************
  586. function scorePublicKey($public_key)
  587. {
  588. $current_generation_block = transaction_cycle(0, TRUE);
  589. TKRandom::seed($current_generation_block);
  590. $public_key_score = 0;
  591. $tkrandom_num = 0;
  592. $character = 0;
  593. for ($i = 0; $i < 18; $i++)
  594. {
  595. $tkrandom_num = TKRandom::num(1, 35);
  596. $character = base_convert($tkrandom_num, 10, 36); // Base 10 to Base 36 conversion
  597. $public_key_score += getCharFreq($public_key, $character);
  598. }
  599. return $public_key_score;
  600. }
  601. //***********************************************************************************
  602. //***********************************************************************************
  603. function tk_time_convert($time)
  604. {
  605. if($time < 0)
  606. {
  607. return "0 sec";
  608. }
  609. if($time < 60)
  610. {
  611. if($time == 1)
  612. {
  613. $time .= " sec";
  614. }
  615. else
  616. {
  617. $time .= " secs";
  618. }
  619. }
  620. else if($time >= 60 && $time < 3600)
  621. {
  622. if($time >= 60 && $time < 120)
  623. {
  624. $time = intval($time / 60) . " min";
  625. }
  626. else
  627. {
  628. $time = intval($time / 60) . " mins";
  629. }
  630. }
  631. else if($time >= 3600 && $time < 86400)
  632. {
  633. if($time >= 3600 && $time < 7200)
  634. {
  635. $time = intval($time / 3600) . " hour";
  636. }
  637. else
  638. {
  639. $time = intval($time / 3600) . " hours";
  640. }
  641. }
  642. else if($time >= 86400)
  643. {
  644. if($time >= 86400 && $time < 172800)
  645. {
  646. $time = intval($time / 86400) . " day";
  647. }
  648. else
  649. {
  650. $time = intval($time / 86400) . " days";
  651. }
  652. }
  653. return $time;
  654. }
  655. //***********************************************************************************
  656. //***********************************************************************************
  657. function db_cache_balance($my_public_key)
  658. {
  659. // Check server balance via custom memory index
  660. $my_server_balance = mysql_result(mysql_query("SELECT * FROM `balance_index` WHERE `public_key_hash` = 'server_timekoin_balance' LIMIT 1"),0,"balance");
  661. $my_server_balance_last = mysql_result(mysql_query("SELECT * FROM `balance_index` WHERE `public_key_hash` = 'server_timekoin_balance' LIMIT 1"),0,"block");
  662. if($my_server_balance === FALSE)
  663. {
  664. // Does not exist, needs to be created
  665. $sql = "INSERT INTO `timekoin`.`balance_index` (`block` ,`public_key_hash` ,`balance`)VALUES ('0', 'server_timekoin_balance', '0')";
  666. mysql_query($sql);
  667. // Update record with the latest balance
  668. $display_balance = check_crypt_balance($my_public_key);
  669. $sql = "UPDATE `balance_index` SET `block` = '" . time() . "' , `balance` = '$display_balance' WHERE `balance_index`.`public_key_hash` = 'server_timekoin_balance' LIMIT 1";
  670. mysql_query($sql);
  671. }
  672. else
  673. {
  674. if($my_server_balance_last < transaction_cycle(0) && time() - transaction_cycle(0) > 25) // Generate 25 seconds after cycle
  675. {
  676. // Last generated balance is older than the current cycle, needs to be updated
  677. // Update record with the latest balance
  678. $display_balance = check_crypt_balance($my_public_key);
  679. $sql = "UPDATE `balance_index` SET `block` = '" . time() . "' , `balance` = '$display_balance' WHERE `balance_index`.`public_key_hash` = 'server_timekoin_balance' LIMIT 1";
  680. mysql_query($sql);
  681. }
  682. else
  683. {
  684. $display_balance = $my_server_balance;
  685. }
  686. }
  687. return $display_balance;
  688. }
  689. //***********************************************************************************
  690. //***********************************************************************************
  691. function send_timekoins($my_private_key, $my_public_key, $send_to_public_key, $amount, $message)
  692. {
  693. $arr1 = str_split($send_to_public_key, 181);
  694. openssl_private_encrypt($arr1[0], $encryptedData1, $my_private_key);
  695. $encryptedData64_1 = base64_encode($encryptedData1);
  696. openssl_private_encrypt($arr1[1], $encryptedData2, $my_private_key);
  697. $encryptedData64_2 = base64_encode($encryptedData2);
  698. if(empty($message) == TRUE)
  699. {
  700. $transaction_data = "AMOUNT=$amount---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2);
  701. }
  702. else
  703. {
  704. // Sanitization of message
  705. // Filter symbols that might lead to a transaction hack attack
  706. $symbols = array("|", "?", "="); // SQL + URL
  707. $message = str_replace($symbols, "", $message);
  708. // Trim any message to 64 characters max and filter any sql
  709. $message = filter_sql(substr($message, 0, 64));
  710. $transaction_data = "AMOUNT=$amount---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2) . "---MSG=$message";
  711. }
  712. openssl_private_encrypt($transaction_data, $encryptedData3, $my_private_key);
  713. $encryptedData64_3 = base64_encode($encryptedData3);
  714. $triple_hash_check = hash('sha256', $encryptedData64_1 . $encryptedData64_2 . $encryptedData64_3);
  715. $sql = "INSERT INTO `my_transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)
  716. VALUES ('" . time() . "', '$my_public_key', '$encryptedData64_1', '$encryptedData64_2' , '$encryptedData64_3', '$triple_hash_check' , 'T')";
  717. if(mysql_query($sql) == TRUE)
  718. {
  719. // Success code
  720. return TRUE;
  721. }
  722. else
  723. {
  724. return FALSE;
  725. }
  726. }
  727. //***********************************************************************************
  728. //***********************************************************************************
  729. function unix_timestamp_to_human($timestamp = "", $format = 'D d M Y - H:i:s')
  730. {
  731. if (empty($timestamp) || ! is_numeric($timestamp)) $timestamp = time();
  732. return ($timestamp) ? date($format, $timestamp) : date($format, $timestamp);
  733. }
  734. //***********************************************************************************
  735. //***********************************************************************************
  736. function visual_walkhistory($block_start = 0, $block_end = 0)
  737. {
  738. $output;
  739. $current_generation_block = transaction_cycle(0, TRUE);
  740. if($block_end <= $block_start)
  741. {
  742. $block_end = $block_start + 1;
  743. }
  744. if($block_end > $current_generation_block)
  745. {
  746. $block_end = $current_generation_block;
  747. }
  748. $wrong_timestamp = 0;
  749. $wrong_block_numbers = NULL;
  750. $wrong_hash = 0;
  751. $wrong_hash_numbers = NULL;
  752. $next_timestamp = TRANSACTION_EPOCH + ($block_start * 300);
  753. for ($i = $block_start; $i < $block_end; $i++)
  754. {
  755. $output = $output . '<tr><td class="style2">Block # ' . $i;
  756. $time1 = transaction_cycle(0 - $current_generation_block + $i);
  757. $time2 = transaction_cycle(0 - $current_generation_block + 1 + $i);
  758. $time3 = transaction_cycle(0 - $current_generation_block + 1 + $i);
  759. $time4 = transaction_cycle(0 - $current_generation_block + 2 + $i);
  760. $next_hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time3 AND `timestamp` < $time4 AND `attribute` = 'H' LIMIT 1"),0,"hash");
  761. $sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";
  762. $sql_result = mysql_query($sql);
  763. $sql_num_results = mysql_num_rows($sql_result);
  764. $my_hash = 0;
  765. $timestamp = 0;
  766. for ($h = 0; $h < $sql_num_results; $h++)
  767. {
  768. $sql_row = mysql_fetch_array($sql_result);
  769. if($sql_row["attribute"] == "H" || $sql_row["attribute"] == "B")
  770. {
  771. $timestamp = $sql_row["timestamp"];
  772. }
  773. $my_hash .= $sql_row["hash"];
  774. }
  775. if($next_timestamp != $timestamp)
  776. {
  777. $output = $output . '</br><strong><font color=red>Hash Timestamp Sequence Wrong... Should Be: ' . $next_timestamp . '</font></strong>';
  778. $wrong_timestamp++;
  779. $wrong_block_numbers .= " " . $i;
  780. }
  781. $next_timestamp = $next_timestamp + 300;
  782. $my_hash = hash('sha256', $my_hash);
  783. $output .= '</br>Timestamp in Database: ' . $timestamp;
  784. $output .= '</br>Calculated Hash: ' . $my_hash;
  785. $output .= '</br>&nbsp;Database Hash : ' . $next_hash;
  786. if($my_hash == $next_hash)
  787. {
  788. $output .= '</br><font color=green>Hash Match...</font>';
  789. }
  790. else
  791. {
  792. $output .= '</br><strong><font color=red>Hash MISMATCH</font></strong></td></tr>';
  793. $wrong_hash++;
  794. $wrong_hash_numbers = $wrong_hash_numbers . " " . $i;
  795. }
  796. }
  797. if(empty($wrong_block_numbers) == TRUE)
  798. {
  799. $wrong_block_numbers = '<font color="blue">None</font>';
  800. }
  801. if(empty($wrong_hash_numbers) == TRUE)
  802. {
  803. $wrong_hash_numbers = '<font color="blue">None</font>';
  804. }
  805. $output .= '<tr><td class="style2"><strong><font color="blue">Total Wrong Sequence: ' . $wrong_timestamp . '</strong></font>';
  806. $output .= '</br><strong><font color="red">Blocks Wrong:</font> ' . $wrong_block_numbers . '</strong></td></tr>';
  807. $output .= '<tr><td class="style2"><strong><font color="blue">Total Wrong Hash: ' . $wrong_hash . '</strong></font>';
  808. $output .= '</br><strong><font color="red">Blocks Wrong:</font> ' . $wrong_hash_numbers . '</strong></td></tr>';
  809. return $output;
  810. }
  811. //***********************************************************************************
  812. //***********************************************************************************
  813. function visual_repair($block_start = 0)
  814. {
  815. $current_generation_block = transaction_cycle(0, TRUE);
  816. $output;
  817. // Wipe all blocks ahead
  818. $time_range = transaction_cycle(0 - $current_generation_block + $block_start);
  819. $sql = "DELETE FROM `transaction_history` WHERE `transaction_history`.`timestamp` >= $time_range AND `attribute` = 'H'";
  820. if(mysql_query($sql) == TRUE)
  821. {
  822. $output .= '<tr><td class="style2">Clearing Hash Timestamps Ahead of Block #' . $block_start . '</td></tr>';
  823. }
  824. else
  825. {
  826. return '<tr><td class="style2">Database ERROR, stopping repair process...</td></tr>';
  827. }
  828. $generation_arbitrary = ARBITRARY_KEY;
  829. for ($t = $block_start; $t < $current_generation_block; $t++)
  830. {
  831. $output .= "<tr><td><strong>Repairing Block# $t</strong>";
  832. $time1 = transaction_cycle(0 - $current_generation_block - 1 + $t);
  833. $time2 = transaction_cycle(0 - $current_generation_block + $t);
  834. $sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";
  835. $sql_result = mysql_query($sql);
  836. $sql_num_results = mysql_num_rows($sql_result);
  837. $hash = 0;
  838. for ($i = 0; $i < $sql_num_results; $i++)
  839. {
  840. $sql_row = mysql_fetch_array($sql_result);
  841. $hash .= $sql_row["hash"];
  842. }
  843. // Transaction hash
  844. $hash = hash('sha256', $hash);
  845. $sql = "INSERT INTO `transaction_history` (`timestamp` ,`public_key_from` ,`public_key_to` ,`crypt_data1` ,`crypt_data2` ,`crypt_data3` ,`hash` ,`attribute`)
  846. VALUES ('$time2', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$hash', 'H')";
  847. if(mysql_query($sql) == FALSE)
  848. {
  849. // Something failed
  850. $output .= '</br><strong><font color="red">Repair ERROR in Database</font></strong></td></tr>';
  851. }
  852. else
  853. {
  854. $output .= '</br><strong><font color="blue">Repair Complete...</font></strong></td></tr>';
  855. }
  856. } // End for loop
  857. return $output;
  858. }
  859. //***********************************************************************************
  860. //***********************************************************************************
  861. function is_private_ip($ip, $ignore = FALSE)
  862. {
  863. if(empty($ip) == TRUE)
  864. {
  865. return FALSE;
  866. }
  867. if($ignore == TRUE)
  868. {
  869. $result = FALSE;
  870. }
  871. else
  872. {
  873. if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) == FALSE)
  874. {
  875. $result = TRUE;
  876. }
  877. }
  878. return $result;
  879. }
  880. //***********************************************************************************
  881. //***********************************************************************************
  882. function initialization_database()
  883. {
  884. // Clear IP Activity and Banlist for next start
  885. mysql_query("TRUNCATE TABLE `ip_activity`");
  886. mysql_query("TRUNCATE TABLE `ip_banlist`");
  887. // Clear Active & New Peers List
  888. mysql_query("DELETE FROM `active_peer_list` WHERE `active_peer_list`.`join_peer_list` != 0"); // Permanent Peers Ignored
  889. mysql_query("TRUNCATE TABLE `new_peers_list`");
  890. // Record when started
  891. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'timekoin_start_time' LIMIT 1"),0,0);
  892. if($new_record_check === FALSE)
  893. {
  894. // Does not exist, create it
  895. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('timekoin_start_time', '" . time() . "')");
  896. }
  897. else
  898. {
  899. // Save start time for uptime timer
  900. mysql_query("UPDATE `options` SET `field_data` = '" . time() . "' WHERE `options`.`field_name` = 'timekoin_start_time' LIMIT 1");
  901. }
  902. // Allow LAN IPs in the Peer List
  903. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'allow_LAN_peers' LIMIT 1"),0,0);
  904. if($new_record_check === FALSE)
  905. {
  906. // Does not exist, create it
  907. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('allow_LAN_peers', '0')");
  908. }
  909. // Allow Ambient Peer Restart
  910. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'allow_ambient_peer_restart' LIMIT 1"),0,0);
  911. if($new_record_check === FALSE)
  912. {
  913. // Does not exist, create it
  914. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('allow_ambient_peer_restart', '0')");
  915. }
  916. // Max Queries for Flood Protection
  917. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_request_max' LIMIT 1"),0,0);
  918. if($new_record_check === FALSE)
  919. {
  920. // Does not exist, create it
  921. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('server_request_max', '200')");
  922. }
  923. // Hash code for External Access
  924. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_hash_code' LIMIT 1"),0,0);
  925. if($new_record_check === FALSE)
  926. {
  927. // Does not exist, create it
  928. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('server_hash_code', '0')");
  929. }
  930. // Time Sync Polling
  931. $new_record_check = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'time_sync_error' LIMIT 1"),0,0);
  932. if($new_record_check === FALSE)
  933. {
  934. // Does not exist, create it
  935. mysql_query("INSERT INTO `options` (`field_name` ,`field_data`)VALUES ('time_sync_error', '0')");
  936. }
  937. //**************************************
  938. // Main Loop Status Setup
  939. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,0);
  940. if($new_record_check === FALSE)
  941. {
  942. // Does not exist, create it
  943. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_heartbeat_active', '0')");
  944. }
  945. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_last_heartbeat' LIMIT 1"),0,0);
  946. if($new_record_check === FALSE)
  947. {
  948. // Does not exist, create it
  949. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('foundation_last_heartbeat', '0')");
  950. }
  951. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,0);
  952. if($new_record_check === FALSE)
  953. {
  954. // Does not exist, create it
  955. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_heartbeat_active', '0')");
  956. }
  957. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_last_heartbeat' LIMIT 1"),0,0);
  958. if($new_record_check === FALSE)
  959. {
  960. // Does not exist, create it
  961. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('generation_last_heartbeat', '0')");
  962. }
  963. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,0);
  964. if($new_record_check === FALSE)
  965. {
  966. // Does not exist, create it
  967. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('genpeer_heartbeat_active', '0')");
  968. }
  969. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_last_heartbeat' LIMIT 1"),0,0);
  970. if($new_record_check === FALSE)
  971. {
  972. // Does not exist, create it
  973. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('genpeer_last_heartbeat', '0')");
  974. }
  975. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,0);
  976. if($new_record_check === FALSE)
  977. {
  978. // Does not exist, create it
  979. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_heartbeat_active', '0')");
  980. }
  981. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,0);
  982. if($new_record_check === FALSE)
  983. {
  984. // Does not exist, create it
  985. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('main_last_heartbeat', '0')");
  986. }
  987. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,0);
  988. if($new_record_check === FALSE)
  989. {
  990. // Does not exist, create it
  991. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peerlist_heartbeat_active', '0')");
  992. }
  993. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_last_heartbeat' LIMIT 1"),0,0);
  994. if($new_record_check === FALSE)
  995. {
  996. // Does not exist, create it
  997. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('peerlist_last_heartbeat', '0')");
  998. }
  999. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,0);
  1000. if($new_record_check === FALSE)
  1001. {
  1002. // Does not exist, create it
  1003. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('queueclerk_heartbeat_active', '0')");
  1004. }
  1005. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_last_heartbeat' LIMIT 1"),0,0);
  1006. if($new_record_check === FALSE)
  1007. {
  1008. // Does not exist, create it
  1009. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('queueclerk_last_heartbeat', '0')");
  1010. }
  1011. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,0);
  1012. if($new_record_check === FALSE)
  1013. {
  1014. // Does not exist, create it
  1015. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transclerk_heartbeat_active', '0')");
  1016. }
  1017. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_last_heartbeat' LIMIT 1"),0,0);
  1018. if($new_record_check === FALSE)
  1019. {
  1020. // Does not exist, create it
  1021. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('transclerk_last_heartbeat', '0')");
  1022. }
  1023. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,0);
  1024. if($new_record_check === FALSE)
  1025. {
  1026. // Does not exist, create it
  1027. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('treasurer_heartbeat_active', '0')");
  1028. }
  1029. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_last_heartbeat' LIMIT 1"),0,0);
  1030. if($new_record_check === FALSE)
  1031. {
  1032. // Does not exist, create it
  1033. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('treasurer_last_heartbeat', '0')");
  1034. }
  1035. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,0);
  1036. if($new_record_check === FALSE)
  1037. {
  1038. // Does not exist, create it
  1039. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_heartbeat_active', '0')");
  1040. }
  1041. $new_record_check = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,0);
  1042. if($new_record_check === FALSE)
  1043. {
  1044. // Does not exist, create it
  1045. mysql_query("INSERT INTO `main_loop_status` (`field_name` ,`field_data`)VALUES ('watchdog_last_heartbeat', '0')");
  1046. }
  1047. //**************************************
  1048. return 0;
  1049. }
  1050. //***********************************************************************************
  1051. //***********************************************************************************
  1052. ?>