PageRenderTime 68ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/v1.9/index.php

https://bitbucket.org/rev22/timekoin
PHP | 1919 lines | 1549 code | 263 blank | 107 comment | 361 complexity | 7a6173da1978694e1309410bf2ecd409 MD5 | raw file
  1. <?PHP
  2. include 'templates.php';
  3. session_name("timekoin");
  4. session_start();
  5. if($_SESSION["valid_login"] == FALSE && $_GET["action"] != "login")
  6. {
  7. sleep(1); // One second delay to help prevent brute force attack
  8. $_SESSION["valid_session"] = TRUE;
  9. if($_SESSION["valid_session"] == TRUE)
  10. {
  11. // Not logged in, display login page
  12. login_screen();
  13. }
  14. exit;
  15. }
  16. include 'configuration.php';
  17. if($_SESSION["valid_session"] == TRUE && $_GET["action"] == "login")
  18. {
  19. $http_username = $_POST["timekoin_username"];
  20. $http_password = $_POST["timekoin_password"];
  21. if(empty($http_username) == FALSE && empty($http_password) == FALSE)
  22. {
  23. if(mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
  24. {
  25. login_screen('Could Not Connect To Database');
  26. exit;
  27. }
  28. if(mysql_select_db(MYSQL_DATABASE) == FALSE)
  29. {
  30. login_screen('Could Not Select Database');
  31. exit;
  32. }
  33. $username_hash = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'username' LIMIT 1"),0,"field_data");
  34. $password_hash = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'password' LIMIT 1"),0,"field_data");
  35. if(hash('sha256', $http_username) == $username_hash)
  36. {
  37. //Username match, check password
  38. if(hash('sha256', $http_password) == $password_hash)
  39. {
  40. // All match, set login variable and store username in cookie
  41. $_SESSION["login_username"] = $http_username;
  42. $_SESSION["valid_login"] = TRUE;
  43. header("Location: index.php?menu=home");
  44. exit;
  45. }
  46. }
  47. }
  48. sleep(1); // One second delay to help prevent brute force attack
  49. login_screen("Login Failed");
  50. exit;
  51. }
  52. if($_SESSION["valid_login"] == TRUE)
  53. {
  54. include 'function.php';
  55. //****************************************************************************
  56. if(mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
  57. {
  58. home_screen('ERROR', '<font color="red"><strong>Could Not Connect To Database</strong></font>', '', '');
  59. exit;
  60. }
  61. if(mysql_select_db(MYSQL_DATABASE) == FALSE)
  62. {
  63. home_screen('ERROR','<font color="red"><strong>Could Not Select Database</strong></font>', '', '');
  64. exit;
  65. }
  66. //****************************************************************************
  67. if($_GET["menu"] == "home" || empty($_GET["menu"]) == TRUE)
  68. {
  69. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  70. $body_string = '<table border="0" cellspacing="10" cellpadding="2" bgcolor="#FFFFFF"><tr><td align="center"><strong>Status</strong></td>
  71. <td align="center"><strong>Program</strong></td><td align="left"><strong>Message</strong></td></tr>';
  72. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  73. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,"field_data");
  74. if($script_loop_active > 0)
  75. {
  76. // Main should still be active
  77. if((time() - $script_last_heartbeat) > 30) // Greater than triple the loop time, something is wrong
  78. {
  79. $main_msg = last_debug_msg("main");
  80. if ($main_msg) {
  81. $main_msg = "Stalled: " . $main_msg;
  82. } else {
  83. $main_msg = "Program Stalled.";
  84. }
  85. // Main has stop was unexpected
  86. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Main Program Processor</strong></font></td>
  87. <td><strong>' . $main_msg . '</strong></td></tr>';
  88. }
  89. else
  90. {
  91. $main_msg = last_debug_msg("main");
  92. if (!$main_msg) { $main_msg = "Normal Operations"; }
  93. // Main processor script is working properly
  94. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Main Program Processor</strong></font></td>
  95. <td><strong>' . $main_msg . '</strong></td></tr>';
  96. }
  97. }
  98. else
  99. {
  100. $body_string .= '<tr><td align="center"><img src="img/stop.gif" alt="" /></td><td><font color="red"><strong>Main Program Processor</strong></font></td>
  101. <td><strong>Main Program Offline</strong></td></tr>';
  102. }
  103. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,"field_data");
  104. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_last_heartbeat' LIMIT 1"),0,"field_data");
  105. if($script_loop_active > 0)
  106. {
  107. // Treasurer should still be active
  108. if((time() - $script_last_heartbeat) > 60)
  109. {
  110. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Treasurer Processor</strong></font></td>
  111. <td><strong>Program Stalled.</strong></td></tr>';
  112. }
  113. else
  114. {
  115. // Script is working properly
  116. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Treasurer Processor</strong></font></td>
  117. <td><strong>Examining Transactions for Accuracy...</strong></td></tr>';
  118. }
  119. }
  120. else
  121. {
  122. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Treasurer Processor</strong></font></td>
  123. <td><strong>Idle</strong></td></tr>';
  124. }
  125. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,"field_data");
  126. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_last_heartbeat' LIMIT 1"),0,"field_data");
  127. if($script_loop_active > 0)
  128. {
  129. // Peerlist should still be active
  130. if((time() - $script_last_heartbeat) > 60)
  131. {
  132. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Peer Processor</strong></font></td>
  133. <td><strong>Program Stalled.</strong></td></tr>';
  134. }
  135. else
  136. {
  137. // Script is working properly
  138. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Peer Processor</strong></font></td>
  139. <td><strong>Talking to Peers...</strong></td></tr>';
  140. }
  141. }
  142. else
  143. {
  144. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Peer Processor</strong></font></td>
  145. <td><strong>Idle</strong></td></tr>';
  146. }
  147. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  148. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_last_heartbeat' LIMIT 1"),0,"field_data");
  149. if($script_loop_active > 0)
  150. {
  151. // Queueclerk should still be active
  152. if((time() - $script_last_heartbeat) > 90)
  153. {
  154. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Transaction Queue Clerk</strong></font></td>
  155. <td><strong>Program Stalled.</strong></td></tr>';
  156. }
  157. else
  158. {
  159. // Script is working properly
  160. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Transaction Queue Clerk</strong></font></td>
  161. <td><strong>Consulting with Peers...</strong></td></tr>';
  162. }
  163. }
  164. else
  165. {
  166. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Transaction Queue Clerk</strong></font></td>
  167. <td><strong>Idle</strong></td></tr>';
  168. }
  169. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,"field_data");
  170. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_last_heartbeat' LIMIT 1"),0,"field_data");
  171. if($script_loop_active > 0)
  172. {
  173. // Genpeer should still be active
  174. if((time() - $script_last_heartbeat) > 90)
  175. {
  176. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Generation Peer Manager</strong></font></td>
  177. <td><strong>Program Stalled.</strong></td></tr>';
  178. }
  179. else
  180. {
  181. // Script is working properly
  182. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Generation Peer Manager</strong></font></td>
  183. <td><strong>Consulting with Peers...</strong></td></tr>';
  184. }
  185. }
  186. else
  187. {
  188. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Generation Peer Manager</strong></font></td>
  189. <td><strong>Idle</strong></td></tr>';
  190. }
  191. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,"field_data");
  192. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_last_heartbeat' LIMIT 1"),0,"field_data");
  193. if($script_loop_active > 0)
  194. {
  195. // Generation should still be active
  196. if((time() - $script_last_heartbeat) > 60)
  197. {
  198. // Generation has stop was unexpected
  199. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Generation Processor</strong></font></td>
  200. <td><strong>Program Stalled.</strong></td></tr>';
  201. }
  202. else
  203. {
  204. // Generation processor script is working properly
  205. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Generation Processor</strong></font></td>
  206. <td><strong>Doing Crypto Magic...</strong></td></tr>';
  207. }
  208. }
  209. else
  210. {
  211. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Generation Processor</strong></font></td>
  212. <td><strong>Idle</strong></td></tr>';
  213. }
  214. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  215. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_last_heartbeat' LIMIT 1"),0,"field_data");
  216. if($script_loop_active > 0)
  217. {
  218. // Transclerk should still be active
  219. if((time() - $script_last_heartbeat) > 120)
  220. {
  221. // Script has stop was unexpected
  222. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Transaction Clerk</strong></font></td>
  223. <td><strong>Program Stalled.</strong></td></tr>';
  224. }
  225. else
  226. {
  227. // Script is working properly
  228. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Transaction Clerk</strong></font></td>
  229. <td><strong>Consulting with Peers...</strong></td></tr>';
  230. }
  231. }
  232. else
  233. {
  234. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Transaction Clerk</strong></font></td>
  235. <td><strong>Idle</strong></td></tr>';
  236. }
  237. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,"field_data");
  238. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_last_heartbeat' LIMIT 1"),0,"field_data");
  239. if($script_loop_active > 0)
  240. {
  241. // Foundation should still be active
  242. if((time() - $script_last_heartbeat) > 100)
  243. {
  244. // Script has stop was unexpected
  245. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Foundation Manager</strong></font></td>
  246. <td><strong>Program Stalled.</strong></td></tr>';
  247. }
  248. else
  249. {
  250. // Script is working properly
  251. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Foundation Manager</strong></font></td>
  252. <td><strong>Inspecting Transaction Foundations...</strong></td></tr>';
  253. }
  254. }
  255. else
  256. {
  257. $body_string .= '<tr><td align="center"><img src="img/arrow.gif" alt="" /></td><td><font color="#b0a454"><strong>Foundation Manager</strong></font></td>
  258. <td><strong>Idle</strong></td></tr>';
  259. }
  260. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  261. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,"field_data");
  262. if($script_loop_active > 0)
  263. {
  264. // Watchdog should still be active
  265. if((time() - $script_last_heartbeat) > 60) // Greater than double the loop time, something is wrong
  266. {
  267. // Script has stop was unexpected
  268. $body_string .= '<tr><td align="center"><img src="img/stalled.gif" alt="" /></td><td><font color="red"><strong>Watchdog</strong></font></td>
  269. <td><strong>Program Stalled.</strong></td></tr>';
  270. }
  271. else
  272. {
  273. // Script is working properly
  274. $body_string .= '<tr><td align="center"><img src="img/wait16trans.gif" alt="" /></td><td><font color="green"><strong>Watchdog</strong></font></td>
  275. <td><strong>Active</strong></td></tr>';
  276. }
  277. }
  278. else
  279. {
  280. $body_string .= '<tr><td align="center"><img src="img/stop.gif" alt="" /></td><td><font color="#b0a454"><strong>Watchdog</strong></font></td>
  281. <td><strong>Disabled</strong></td></tr>';
  282. }
  283. $body_string = $body_string . '</table>';
  284. $display_balance = db_cache_balance($my_public_key);
  285. $firewall_blocked = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'firewall_blocked_peer' LIMIT 1"),0,"field_data");
  286. if($firewall_blocked == "1")
  287. {
  288. $firewall_blocked = '<tr><td colspan="3"><font color="#827f00"><strong>*** Operating in Outbound Only Mode ***</strong></font></td></tr>';
  289. }
  290. else
  291. {
  292. $firewall_blocked = NULL;
  293. }
  294. $time_sync_error = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'time_sync_error' LIMIT 1"),0,"field_data");
  295. if($time_sync_error == "1")
  296. {
  297. $time_sync_error = '<tr><td colspan="3"><font color="red"><strong>*** Timekoin Might Be Out of Sync with the Network Peers ***</strong></font></td></tr>';
  298. }
  299. else
  300. {
  301. $time_sync_error = NULL;
  302. }
  303. $text_bar = '<table border="0"><tr><td width="250"><strong>Current Server Balance: <font color="green">' . number_format($display_balance) . '</font></strong></td>
  304. <td width="180"><strong>Peer Time: <font color="blue">' . time() . '</font></strong></td>
  305. <td><strong><font color="#827f00">' . tk_time_convert(transaction_cycle(1) - time()) . '</font> until next cycle</strong></td></tr>
  306. ' . $firewall_blocked . $time_sync_error . '</table>';
  307. $quick_info = 'Check on the Status of the Timekoin inner workings.';
  308. $home_update = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'refresh_realtime_home' LIMIT 1"),0,"field_data");
  309. home_screen("Realtime Server Status", $text_bar, $body_string, $quick_info , $home_update);
  310. exit;
  311. }
  312. //****************************************************************************
  313. if($_GET["menu"] == "peerlist")
  314. {
  315. if($_GET["remove"] == "peer")
  316. {
  317. // Manually remove this peer
  318. $sql = "DELETE FROM `active_peer_list` WHERE `active_peer_list`.`IP_Address` = '" . $_POST["ip"] . "' AND `active_peer_list`.`domain` = '" . $_POST["domain"] . "' LIMIT 1";
  319. mysql_query($sql);
  320. }
  321. if($_GET["save"] == "peer" && empty($_POST["edit_port"]) == FALSE)
  322. {
  323. // Save manual peer edit
  324. if($_POST["perm_peer"] == "perm")
  325. {
  326. $join_peer_list = '0';
  327. }
  328. else
  329. {
  330. $join_peer_list = 'UNIX_TIMESTAMP()';
  331. }
  332. $sql = "UPDATE `active_peer_list` SET `last_heartbeat` = UNIX_TIMESTAMP() ,`join_peer_list` = $join_peer_list , `failed_sent_heartbeat` = '0',
  333. `IP_Address` = '" . $_POST["edit_ip"] . "', `domain` = '" . $_POST["edit_domain"] . "', `subfolder` = '" . $_POST["edit_subfolder"] . "', `port_number` = '" . $_POST["edit_port"] . "'
  334. WHERE `active_peer_list`.`IP_Address` = '" . $_POST["update_ip"] . "' AND `active_peer_list`.`domain` = '" . $_POST["update_domain"] . "' LIMIT 1";
  335. mysql_query($sql);
  336. }
  337. if($_GET["save"] == "newpeer" && empty($_POST["edit_port"]) == FALSE)
  338. {
  339. // Manually insert new peer
  340. $sql = "INSERT INTO `active_peer_list` (`IP_Address` ,`domain` ,`subfolder` ,`port_number` ,`last_heartbeat` ,`join_peer_list` ,`failed_sent_heartbeat`)
  341. VALUES ('" . $_POST["edit_ip"] . "', '" . $_POST["edit_domain"] . "', '" . $_POST["edit_subfolder"] . "', '" . $_POST["edit_port"] . "', UNIX_TIMESTAMP() , UNIX_TIMESTAMP() , '0')";
  342. mysql_query($sql);
  343. }
  344. if($_GET["save"] == "firstcontact")
  345. {
  346. // Wipe Current First Contact Server List and Save the New List
  347. $field_numbers = intval($_POST["field_numbers"]);
  348. if($field_numbers > 0)
  349. {
  350. mysql_query("DELETE FROM `options` WHERE `options`.`field_name` = 'first_contact_server'");
  351. while($field_numbers > 0)
  352. {
  353. if(empty($_POST["first_contact_ip$field_numbers"]) == FALSE || empty($_POST["first_contact_domain$field_numbers"]) == FALSE)
  354. {
  355. $sql = "INSERT INTO `options` (`field_name` ,`field_data`)
  356. VALUES ('first_contact_server', '---ip=" . $_POST["first_contact_ip$field_numbers"] .
  357. "---domain=" . $_POST["first_contact_domain$field_numbers"] .
  358. "---subfolder=" . $_POST["first_contact_subfolder$field_numbers"] .
  359. "---port=" . $_POST["first_contact_port$field_numbers"] . "---end')";
  360. mysql_query($sql);
  361. }
  362. $field_numbers--;
  363. }
  364. }
  365. }
  366. if($_GET["edit"] == "peer")
  367. {
  368. $body_string = '<div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>IP Address</th>
  369. <th>Domain</th><th>Subfolder</th><th>Port Number</th><th></th><th></th></tr>';
  370. if($_GET["type"] == "new")
  371. {
  372. // Manually add a peer
  373. $body_string .= '<FORM ACTION="index.php?menu=peerlist&save=newpeer" METHOD="post"><tr>
  374. <td class="style2"><input type="text" name="edit_ip" size="13" /></td>
  375. <td class="style2"><input type="text" name="edit_domain" size="20" /></td>
  376. <td class="style2"><input type="text" name="edit_subfolder" size="10" /></td>
  377. <td class="style2"><input type="text" name="edit_port" size="5" /></td>
  378. <td><input type="image" src="img/save-icon.gif" name="submit1" border="0"></FORM></td><td>
  379. <FORM ACTION="index.php?menu=peerlist" METHOD="post">
  380. <input type="image" src="img/stop.gif" name="submit2" border="0"></FORM>
  381. </td></tr>';
  382. $body_string .= '</table></div>';
  383. }
  384. else if($_GET["type"] == "firstcontact")
  385. {
  386. $sql = "SELECT * FROM `options` WHERE `field_name` = 'first_contact_server'";
  387. $sql_result = mysql_query($sql);
  388. $sql_num_results = mysql_num_rows($sql_result) + 2;
  389. $counter = 1;
  390. $body_string .= '<FORM ACTION="index.php?menu=peerlist&save=firstcontact" METHOD="post">';
  391. for ($i = 0; $i < $sql_num_results; $i++)
  392. {
  393. $sql_row = mysql_fetch_array($sql_result);
  394. $peer_ip = find_string("---ip=", "---domain", $sql_row["field_data"]);
  395. $peer_domain = find_string("---domain=", "---subfolder", $sql_row["field_data"]);
  396. $peer_subfolder = find_string("---subfolder=", "---port", $sql_row["field_data"]);
  397. $peer_port_number = find_string("---port=", "---end", $sql_row["field_data"]);
  398. $body_string .= '<tr><td class="style2"><input type="text" name="first_contact_ip' . $counter . '" size="13" value="' . $peer_ip . '" /></br></br></td>
  399. <td class="style2" valign="top"><input type="text" name="first_contact_domain' . $counter . '" size="20" value="' . $peer_domain . '" /></td>
  400. <td class="style2" valign="top"><input type="text" name="first_contact_subfolder' . $counter . '" size="10" value="' . $peer_subfolder . '" /></td>
  401. <td class="style2" valign="top"><input type="text" name="first_contact_port' . $counter . '" size="5" value="' . $peer_port_number . '" /></td>
  402. </td></tr>';
  403. $counter++;
  404. }
  405. $body_string .= '<input type="hidden" name="field_numbers" value="' . ($counter - 1) . '">
  406. <tr><td colspan="2"><input type="submit" value="Save First Contact Servers"/></FORM></td></tr>';
  407. $body_string .= '</table></div>';
  408. }
  409. else
  410. {
  411. // Manually edit this peer
  412. $sql = "SELECT * FROM `active_peer_list` WHERE `IP_Address` = '" . $_POST["ip"] ."' AND `domain` = '" . $_POST["domain"] ."' LIMIT 1";
  413. $sql_result = mysql_query($sql);
  414. $sql_row = mysql_fetch_array($sql_result);
  415. $body_string .= '<FORM ACTION="index.php?menu=peerlist&save=peer" METHOD="post"><tr>
  416. <td class="style2"><input type="text" name="edit_ip" size="13" value="' . $sql_row["IP_Address"] . '" /></br></br>
  417. <select name="perm_peer"><option value="expires" SELECTED>Purge When Inactive</option><option value="perm">Do Not Purge</select></td>
  418. <td class="style2" valign="top"><input type="text" name="edit_domain" size="20" value="' . $sql_row["domain"] . '" /></td>
  419. <td class="style2" valign="top"><input type="text" name="edit_subfolder" size="10" value="' . $sql_row["subfolder"] . '" /></td>
  420. <td class="style2" valign="top"><input type="text" name="edit_port" size="5" value="' . $sql_row["port_number"] . '" /></td>
  421. <td valign="top"><input type="hidden" name="update_ip" value="' . $sql_row["IP_Address"] . '">
  422. <input type="hidden" name="update_domain" value="' . $sql_row["domain"] . '">
  423. <input type="image" src="img/save-icon.gif" name="submit1" border="0"></FORM></td>
  424. <td valign="top"><FORM ACTION="index.php?menu=peerlist" METHOD="post">
  425. <input type="image" src="img/stop.gif" name="submit2" border="0"></FORM>
  426. </td></tr>';
  427. $body_string .= '</table></div>';
  428. }
  429. $sql = "SELECT * FROM `active_peer_list`";
  430. $active_peers = mysql_num_rows(mysql_query($sql));
  431. $sql = "SELECT * FROM `new_peers_list`";
  432. $new_peers = mysql_num_rows(mysql_query($sql));
  433. $peer_number_bar = '<strong>Active Peers: <font color="green">' . $active_peers . '</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Peers in Reserve: <font color="blue">' . $new_peers . '</font></strong>';
  434. $quick_info = 'Shows all Active Peers.</br></br>
  435. You can manually delete or edit peers in this section.</br></br>
  436. <font color="blue">First Contact Servers</font> can be changed, deleted, or new ones added to the bottom of the list.';
  437. home_screen('Realtime Network Peer List', $peer_number_bar, $body_string , $quick_info);
  438. }
  439. else
  440. {
  441. // Default screen
  442. $body_string = '<div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr>
  443. <th><p style="font-size:10px;">IP Address</p></th><th><p style="font-size:10px;">Domain</p></th>
  444. <th><p style="font-size:10px;">Subfolder</p></th><th><p style="font-size:10px;">Port Number</p></th>
  445. <th><p style="font-size:10px;">Last Heartbeat</p></th><th><p style="font-size:10px;">Joined</p></th>
  446. <th><p style="font-size:10px;">Failed Heartbeat</p></th><th></th><th></th></tr>';
  447. if($_GET["show"] == "reserve")
  448. {
  449. $sql = "SELECT * FROM `new_peers_list`";
  450. }
  451. else
  452. {
  453. $sql = "SELECT * FROM `active_peer_list`";
  454. }
  455. $sql_result = mysql_query($sql);
  456. $sql_num_results = mysql_num_rows($sql_result);
  457. for ($i = 0; $i < $sql_num_results; $i++)
  458. {
  459. $sql_row = mysql_fetch_array($sql_result);
  460. if($_GET["show"] != "reserve")
  461. {
  462. $last_heartbeat = time() - $sql_row["last_heartbeat"];
  463. $last_heartbeat = tk_time_convert($last_heartbeat);
  464. if($sql_row["join_peer_list"] == 0)
  465. {
  466. $joined = 'P';
  467. $permanent1 = '<font color="blue">';
  468. $permanent2 = '</font>';
  469. }
  470. else
  471. {
  472. $joined = time() - $sql_row["join_peer_list"];
  473. $joined = tk_time_convert($joined);
  474. $permanent1 = NULL;
  475. $permanent2 = NULL;
  476. }
  477. }
  478. $body_string .= '<tr>
  479. <td class="style2"><p style="word-wrap:break-word; width:85px; font-size:10px;">' . $permanent1 . $sql_row["IP_Address"] . $permanent2 . '</p></td>
  480. <td class="style2"><p style="word-wrap:break-word; width:130px; font-size:10px;">' . $permanent1 . $sql_row["domain"] . $permanent2 . '</p></td>
  481. <td class="style2"><p style="word-wrap:break-word; width:55px; font-size:10px;">' . $permanent1 . $sql_row["subfolder"] . $permanent2 . '</p></td>
  482. <td class="style2"><p style="word-wrap:break-word; font-size:10px;">' . $permanent1 . $sql_row["port_number"] . $permanent2 . '</p></td>
  483. <td class="style2"><p style="word-wrap:break-word; font-size:11px;">' . $permanent1 . $last_heartbeat . $permanent2 . '</p></td>
  484. <td class="style2"><p style="word-wrap:break-word; font-size:11px;">' . $permanent1 . $joined . $permanent2 . '</p></td>
  485. <td class="style2"><p style="word-wrap:break-word; font-size:11px;">' . $permanent1 . $sql_row["failed_sent_heartbeat"] . $permanent2 . '</p></td>';
  486. if($_GET["show"] == "reserve")
  487. {
  488. $body_string .= '<td></td><td></td></tr>';
  489. }
  490. else
  491. {
  492. $body_string .= '<td><FORM ACTION="index.php?menu=peerlist&remove=peer" METHOD="post"><input type="image" src="img/stop.gif" name="remove' . $i . '" border="0">
  493. <input type="hidden" name="ip" value="' . $sql_row["IP_Address"] . '">
  494. <input type="hidden" name="domain" value="' . $sql_row["domain"] . '">
  495. </FORM></td><td>
  496. <FORM ACTION="index.php?menu=peerlist&edit=peer" METHOD="post"><input type="image" src="img/edit-icon.gif" name="edit' . $i . '" border="0">
  497. <input type="hidden" name="ip" value="' . $sql_row["IP_Address"] . '">
  498. <input type="hidden" name="domain" value="' . $sql_row["domain"] . '">
  499. </FORM>
  500. </td></tr>';
  501. }
  502. }
  503. $body_string .= '<tr><td colspan="2"><FORM ACTION="index.php?menu=peerlist&show=reserve" METHOD="post"><input type="submit" value="Show Reserve Peers"/></FORM></td>
  504. <td colspan="3"><FORM ACTION="index.php?menu=peerlist&edit=peer&type=new" METHOD="post"><input type="submit" value="Add New Peer"/></FORM></td>
  505. <td colspan="4"><FORM ACTION="index.php?menu=peerlist&edit=peer&type=firstcontact" METHOD="post"><input type="submit" value="First Contact Servers"/></FORM></td></tr></table></div>';
  506. $sql = "SELECT * FROM `new_peers_list`";
  507. $new_peers = mysql_num_rows(mysql_query($sql));
  508. if($_GET["show"] == "reserve")
  509. {
  510. $sql = "SELECT * FROM `active_peer_list`";
  511. $sql_num_results = mysql_num_rows(mysql_query($sql));
  512. }
  513. $peer_number_bar = '<strong>Active Peers: <font color="green">' . $sql_num_results . '</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Peers in Reserve: <font color="blue">' . $new_peers . '</font></strong>';
  514. $quick_info = 'Shows all Active Peers.</br></br>You can manually delete or edit peers in this section.
  515. </br></br>Peers in <font color="blue">Blue</font> will not expire after 5 minutes of inactivity.';
  516. $peerlist_update = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'refresh_realtime_peerlist' LIMIT 1"),0,"field_data");
  517. if($_GET["show"] == "reserve")
  518. {
  519. home_screen('Reserve Peer List', $peer_number_bar, $body_string , $quick_info);
  520. }
  521. else
  522. {
  523. home_screen('Realtime Network Peer List', $peer_number_bar, $body_string , $quick_info, $peerlist_update);
  524. }
  525. }
  526. exit;
  527. }
  528. //****************************************************************************
  529. if($_GET["menu"] == "system")
  530. {
  531. if($_GET["peer_settings"] == "change")
  532. {
  533. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["max_peers"] . "' WHERE `options`.`field_name` = 'max_active_peers' LIMIT 1";
  534. if(mysql_query($sql) == TRUE)
  535. {
  536. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["max_new_peers"] . "' WHERE `options`.`field_name` = 'max_new_peers' LIMIT 1";
  537. if(mysql_query($sql) == TRUE)
  538. {
  539. $server_code = '</br><font color="green"><strong>Peer Settings Updated...</strong></font></br></br>';
  540. }
  541. }
  542. }
  543. if($_GET["server_settings"] == "change")
  544. {
  545. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["domain"] . "' WHERE `options`.`field_name` = 'server_domain' LIMIT 1";
  546. if(mysql_query($sql) == TRUE)
  547. {
  548. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["subfolder"] . "' WHERE `options`.`field_name` = 'server_subfolder' LIMIT 1";
  549. if(mysql_query($sql) == TRUE)
  550. {
  551. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["port"] . "' WHERE `options`.`field_name` = 'server_port_number' LIMIT 1";
  552. if(mysql_query($sql) == TRUE)
  553. {
  554. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["max_request"] . "' WHERE `options`.`field_name` = 'server_request_max' LIMIT 1";
  555. if(mysql_query($sql) == TRUE)
  556. {
  557. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["allow_LAN"] . "' WHERE `options`.`field_name` = 'allow_LAN_peers' LIMIT 1";
  558. if(mysql_query($sql) == TRUE)
  559. {
  560. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["allow_ambient"] . "' WHERE `options`.`field_name` = 'allow_ambient_peer_restart' LIMIT 1";
  561. if(mysql_query($sql) == TRUE)
  562. {
  563. $server_code = '</br><font color="blue"><strong>Server Settings Updated...</strong></font></br></br>';
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. }
  571. if($_GET["stop"] == "watchdog")
  572. {
  573. $watchdog_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  574. $watchdog_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,"field_data");
  575. if($watchdog_loop_active > 0)
  576. {
  577. // Watchdog should still be active
  578. if((time() - $watchdog_last_heartbeat) > 60) // Greater than double the loop time, something is wrong
  579. {
  580. // Watchdog stop was unexpected
  581. $sql = "UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1";
  582. if(mysql_query($sql) == TRUE)
  583. {
  584. $server_code = '</br><font color="red"><strong>Watchdog was already Stopped...</strong></font></br></br>';
  585. }
  586. }
  587. else
  588. {
  589. // Set database to flag watchdog to stop
  590. $sql = "UPDATE `main_loop_status` SET `field_data` = '3' WHERE `main_loop_status`.`field_name` = 'watchdog_heartbeat_active' LIMIT 1";
  591. if(mysql_query($sql) == TRUE)
  592. {
  593. $server_code = '</br><font color="blue"><strong>Watchdog Stopping...</strong></font></br></br>';
  594. }
  595. }
  596. }
  597. else
  598. {
  599. $server_code = '</br><font color="red"><strong>Watchdog was already Stopped...</strong></font></br></br>';
  600. }
  601. }
  602. if($_GET["stop"] == "main")
  603. {
  604. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  605. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,"field_data");
  606. if($script_loop_active > 0)
  607. {
  608. // Main should still be active
  609. if((time() - $script_last_heartbeat) > 30) // Greater than triple the loop time, something is wrong
  610. {
  611. // Main stop was unexpected
  612. $sql = "UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1";
  613. if(mysql_query($sql) == TRUE)
  614. {
  615. $server_code = '</br><font color="red"><strong>Timekoin Main Processor was already Stopped...</strong></font></br></br>';
  616. // Clear transaction queue to avoid unnecessary peer confusion
  617. mysql_query("TRUNCATE TABLE `transaction_queue`");
  618. }
  619. }
  620. else
  621. {
  622. // Set database to flag watchdog to stop
  623. $sql = "UPDATE `main_loop_status` SET `field_data` = '3' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1";
  624. if(mysql_query($sql) == TRUE)
  625. {
  626. $server_code = '</br><font color="blue"><strong>Timekoin Main Processor Stopping...</strong></font></br></br>';
  627. // Clear transaction queue to avoid unnecessary peer confusion
  628. mysql_query("TRUNCATE TABLE `transaction_queue`");
  629. }
  630. }
  631. }
  632. else
  633. {
  634. $server_code = '</br><font color="red"><strong>Timekoin Main Processor was already Stopped...</strong></font></br></br>';
  635. // Clear transaction queue to avoid unnecessary peer confusion
  636. mysql_query("TRUNCATE TABLE `transaction_queue`");
  637. }
  638. }
  639. if($_GET["stop"] == "emergency")
  640. {
  641. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  642. $script_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_last_heartbeat' LIMIT 1"),0,"field_data");
  643. if($script_loop_active > 0)
  644. {
  645. // Main should still be active
  646. if((time() - $script_last_heartbeat) > 30) // Greater than triple the loop time, something is wrong
  647. {
  648. // Main stop was unexpected
  649. $sql = "UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1";
  650. if(mysql_query($sql) == TRUE)
  651. {
  652. $server_code = '</br><font color="red"><strong>Entire Timekoin System has been Halted!</strong></font></br></br>';
  653. activate(TIMEKOINSYSTEM, 0);
  654. }
  655. }
  656. else
  657. {
  658. // Set database to flag watchdog to stop
  659. $sql = "UPDATE `main_loop_status` SET `field_data` = '3' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1";
  660. if(mysql_query($sql) == TRUE)
  661. {
  662. $server_code = '</br><font color="red"><strong>Entire Timekoin System has been Halted!</strong></font></br></br>';
  663. activate(TIMEKOINSYSTEM, 0);
  664. }
  665. }
  666. }
  667. else
  668. {
  669. $server_code = '</br><font color="red"><strong>Entire Timekoin System has been Halted!</strong></font></br></br>';
  670. activate(TIMEKOINSYSTEM, 0);
  671. }
  672. }
  673. if($_GET["code"] == "1")
  674. {
  675. $server_code = '</br><font color="green"><strong>Main Timekoin Processing Started...</strong></font></br></br>';
  676. }
  677. if($_GET["code"] == "99")
  678. {
  679. $server_code = '</br><font color="blue"><strong>Timekoin Already Active...</strong></font></br></br>';
  680. }
  681. if($_GET["code"] == "2")
  682. {
  683. $server_code = '</br><font color="green"><strong>Watchdog Started...</strong></font></br></br>';
  684. }
  685. if($_GET["code"] == "89")
  686. {
  687. $server_code = '</br><font color="blue"><strong>Watchdog Already Active...</strong></font></br></br>';
  688. }
  689. if($_GET["time"] == "poll")
  690. {
  691. $context = stream_context_create(array('http' => array('header'=>'Connection: close'))); // Force close socket after complete
  692. ini_set('user_agent', 'Timekoin Server (GUI) v' . TIMEKOIN_VERSION);
  693. ini_set('default_socket_timeout', 3); // Timeout for request in seconds
  694. $body_string = '<div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" >
  695. <tr><th>Peer</th><th>Time</th><th>Variance</th></tr>';
  696. // Add more possible peers to the new peer list by polling what the active peers have
  697. $sql = "SELECT * FROM `active_peer_list`";
  698. $sql_result = mysql_query($sql);
  699. $sql_num_results = mysql_num_rows($sql_result);
  700. $response_counter = 0;
  701. $variance_total = 0;
  702. for ($i = 0; $i < $sql_num_results; $i++)
  703. {
  704. $sql_row = mysql_fetch_array($sql_result);
  705. $ip_address = $sql_row["IP_Address"];
  706. $domain = $sql_row["domain"];
  707. $subfolder = $sql_row["subfolder"];
  708. $port_number = $sql_row["port_number"];
  709. if(empty($domain) == TRUE)
  710. {
  711. $site_address = $ip_address;
  712. }
  713. else
  714. {
  715. $site_address = $domain;
  716. }
  717. if($port_number == 443)
  718. {
  719. $ssl = "s";
  720. }
  721. else
  722. {
  723. $ssl = NULL;
  724. }
  725. //Use site address name to poll
  726. $poll_peer = filter_sql(file_get_contents("http$ssl://$site_address:$port_number/$subfolder/peerlist.php?action=polltime", FALSE, $context, NULL, 12));
  727. $my_time = time();
  728. if($my_time == $poll_peer && empty($poll_peer) == FALSE)
  729. {
  730. $variance = '0 seconds';
  731. $response_counter++;
  732. }
  733. else if(empty($poll_peer) == FALSE)
  734. {
  735. $variance = $my_time - $poll_peer;
  736. $response_counter++;
  737. $variance_total = $variance_total + abs($variance);
  738. if($variance > 1)
  739. {
  740. $variance = '+' . $variance . ' seconds';
  741. }
  742. else if($variance == 1)
  743. {
  744. $variance = '+' . $variance . ' second';
  745. }
  746. else if($variance == -1)
  747. {
  748. $variance = $variance . ' second';
  749. }
  750. else
  751. {
  752. $variance = $variance . ' seconds';
  753. }
  754. }
  755. else
  756. {
  757. $variance = 'No Response';
  758. }
  759. $body_string .= '<tr><td class="style2"><p style="word-wrap:break-word; font-size:12px;">' . $site_address . ':' . $port_number . '/' . $subfolder . '</p></td>';
  760. $body_string .= '<td class="style2"><p style="font-size:12px;">' . $poll_peer . '</p></td>';
  761. $body_string .= '<td class="style2"><p style="font-size:12px;">' . $variance . '</p></td></tr>';
  762. }
  763. $body_string .= '</table></div>';
  764. $variance_average = round($variance_total / $response_counter);
  765. if($variance_average > 15)
  766. {
  767. $variance_average = '<font color="red">' . $variance_average . '</font> seconds';
  768. }
  769. else if($variance_average == 1)
  770. {
  771. $variance_average = '<font color="green">' . $variance_average . '</font> second';
  772. }
  773. else if($variance_average <= 15 && $variance_average > 1)
  774. {
  775. $variance_average = '<font color="blue">' . $variance_average . '</font> seconds';
  776. }
  777. else
  778. {
  779. $variance_average = '<font color="green">' . $variance_average . '</font> seconds';
  780. }
  781. $body_string .= '<strong>Variance Average: ' . $variance_average . '</strong></br></br>';
  782. }
  783. else
  784. {
  785. $body_string = system_screen();
  786. $body_string .= $server_code;
  787. }
  788. $quick_info = '<strong>Start</strong> will activate all Timekoin Processing.</br></br>
  789. <strong>Stop</strong> will halt Timekoin from further processing.</br></br>
  790. <strong>Emergency Stop</strong> will halt Timekoin from further processing and Block all Peer Internet activity.</br></br>
  791. <strong>Max Peer Query</strong> is the per 10 seconds limit imposed on each individual peer before being banned for 24 hours.</br></br>
  792. <strong>Allow LAN Peers</strong> controls if LAN peers will be allowed to populate the peer list.</br></br>
  793. <strong>Allow Ambient Peer Restarts</strong> controls if other peers can restart Timekoin from unknown failures.</br></br>
  794. <strong>Variance</strong> of 15 seconds or less with the other peers is good.';
  795. home_screen('System Settings', system_service_bar(), $body_string , $quick_info);
  796. exit;
  797. }
  798. //****************************************************************************
  799. if($_GET["menu"] == "options")
  800. {
  801. if($_GET["menu"] == "options" && $_GET["password"] == "change")
  802. {
  803. if(empty($_POST["current_username"]) == FALSE && empty($_POST["new_username"]) == FALSE && empty($_POST["confirm_username"]) == FALSE)
  804. {
  805. // Attemping to change username
  806. if($_POST["current_username"] == $_SESSION["login_username"])
  807. {
  808. // Right username, does the new username match the confirmation username?
  809. if($_POST["new_username"] == $_POST["confirm_username"])
  810. {
  811. // Write new hash to database for username and change the session username
  812. $username_hash = hash('sha256', $_POST["confirm_username"]);
  813. $sql = "UPDATE `options` SET `field_data` = '$username_hash' WHERE `options`.`field_name` = 'username' LIMIT 1";
  814. if(mysql_query($sql) == TRUE)
  815. {
  816. // Update success, now change the session username
  817. $_SESSION["login_username"] = $_POST["confirm_username"];
  818. $username_change = TRUE;
  819. }
  820. }
  821. }
  822. }
  823. if(empty($_POST["current_password"]) == FALSE && empty($_POST["new_password"]) == FALSE && empty($_POST["confirm_password"]) == FALSE)
  824. {
  825. $password_hash = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'password' LIMIT 1"),0,"field_data");
  826. $current_password_hash = hash('sha256', $_POST["current_password"]);
  827. $new_password_hash = hash('sha256', $_POST["new_password"]);
  828. // Attemping to change password
  829. if($current_password_hash == $password_hash)
  830. {
  831. // Right password, does the new password match the confirmation password?
  832. if($_POST["new_password"] == $_POST["confirm_password"])
  833. {
  834. // Write new hash to database for username and change the session username
  835. $sql = "UPDATE `options` SET `field_data` = '$new_password_hash' WHERE `options`.`field_name` = 'password' LIMIT 1";
  836. if(mysql_query($sql) == TRUE)
  837. {
  838. $password_change = TRUE;
  839. }
  840. }
  841. }
  842. }
  843. $body_text = options_screen2();
  844. if($username_change == TRUE)
  845. {
  846. $body_text = $body_text . '<font color="blue"><strong>Username Change Complete!</strong></font></br>';
  847. }
  848. else
  849. {
  850. $body_text = $body_text . '<strong>Username Has Not Been Changed</strong></br>';
  851. }
  852. if($password_change == TRUE)
  853. {
  854. $body_text = $body_text . '<font color="blue"><strong>Password Change Complete!</strong></font>';
  855. }
  856. else
  857. {
  858. $body_text = $body_text . '<strong>Password Has Not Been Changed</strong>';
  859. }
  860. } // End username/password change check
  861. if($_GET["menu"] == "options" && $_GET["refresh"] == "change")
  862. {
  863. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["home_update"] . "' WHERE `options`.`field_name` = 'refresh_realtime_home' LIMIT 1";
  864. if(mysql_query($sql) == TRUE)
  865. {
  866. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["peerlist_update"] . "' WHERE `options`.`field_name` = 'refresh_realtime_peerlist' LIMIT 1";
  867. if(mysql_query($sql) == TRUE)
  868. {
  869. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["queue_update"] . "' WHERE `options`.`field_name` = 'refresh_realtime_queue' LIMIT 1";
  870. if(mysql_query($sql) == TRUE)
  871. {
  872. $hash_code = $_POST["hash_code"];
  873. // Sanitization of message !#$%&'*+-/=?^_`{|}~@.[] allowed
  874. $hash_code = filter_var($hash_code, FILTER_SANITIZE_EMAIL);
  875. // Filter symbols that might lead to an HTML access error
  876. $symbols = array("'", "%", "*", "$", "`", "?", "=", "~", "&", "#", "/", "+",);
  877. $hash_code = str_replace($symbols, "", $hash_code);
  878. $sql = "UPDATE `options` SET `field_data` = '" . $hash_code . "' WHERE `options`.`field_name` = 'server_hash_code' LIMIT 1";
  879. if(mysql_query($sql) == TRUE)
  880. {
  881. $refresh_change = TRUE;
  882. }
  883. }
  884. }
  885. }
  886. $body_text = options_screen2();
  887. if($refresh_change == TRUE)
  888. {
  889. $body_text .= '<font color="blue"><strong>Refresh Settings & Hash Code Update Saved!</strong></font></br>';
  890. }
  891. else
  892. {
  893. $body_text .= '<strong>Refresh / Hash Code Update ERROR...</strong></br>';
  894. }
  895. } // End refresh update save
  896. else if(empty($_GET["password"]) == TRUE && empty($_GET["refresh"]) == TRUE)
  897. {
  898. $body_text = options_screen2();
  899. }
  900. $quick_info = 'You may change the username and password individually or at the same time.</br></br>
  901. Remember that usernames and passwords are Case Sensitive.
  902. </br></br><strong>Hash Code</strong> is a private code you create for any external program or server that request access to more advanced features of your Timekoin server.';
  903. home_screen("Options & Personal Settings", options_screen(), $body_text , $quick_info);
  904. exit;
  905. }
  906. //****************************************************************************
  907. if($_GET["menu"] == "generation")
  908. {
  909. if($_GET["generate"] == "enable")
  910. {
  911. $sql = "UPDATE `options` SET `field_data` = '1' WHERE `options`.`field_name` = 'generate_currency' LIMIT 1";
  912. mysql_query($sql);
  913. }
  914. else if($_GET["generate"] == "disable")
  915. {
  916. $sql = "UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'generate_currency' LIMIT 1";
  917. mysql_query($sql);
  918. }
  919. $sql = "SELECT * FROM `generating_peer_queue`";
  920. $generate_peer_queue = mysql_num_rows(mysql_query($sql));
  921. $generate_currency_enabled = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'generate_currency' LIMIT 1"),0,"field_data");
  922. $sql = "SELECT * FROM `generating_peer_list`";
  923. $sql_result = mysql_query($sql);
  924. $sql_num_results = mysql_num_rows($sql_result);
  925. $generating_peers_now = $sql_num_results;
  926. if($generate_currency_enabled == "1")
  927. {
  928. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  929. $join_peer_list = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$my_public_key' LIMIT 1"),0,"join_peer_list");
  930. $last_generation = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$my_public_key' LIMIT 1"),0,"last_generation");
  931. if(time() - $join_peer_list < 3600)
  932. {
  933. // Can't generate yet
  934. $generate_currency = 'Generation <font color="green"><strong>Enabled</strong></font>';
  935. $generate_rate = '@ <font color="green"><strong>' . peer_gen_amount($my_public_key) . '</strong></font> per Cycle';
  936. $continuous_production = '<font color="blue">Generation not allowed for ' . tk_time_convert(3600 - (time() - $join_peer_list)) . '</font>';
  937. }
  938. else if($join_peer_list === FALSE)
  939. {
  940. // Not elected to the generating peer list yet
  941. $generate_currency = 'Generation <font color="green"><strong>Enabled</strong></font>';
  942. $generate_rate = '@ <font color="green"><strong>' . peer_gen_amount($my_public_key) . '</strong></font> per Cycle';
  943. $continuous_production = '<font color="red"><strong>This Peer Has Not</br> Been Elected Yet</strong></font>';
  944. }
  945. else
  946. {
  947. $production_time = tk_time_convert(time() - $join_peer_list);
  948. $last_generation = tk_time_convert(time() - $last_generation);
  949. $generate_currency = 'Generation <font color="green"><strong>Enabled</strong></font>';
  950. $generate_rate = '@ <font color="green"><strong>' . peer_gen_amount($my_public_key) . '</strong></font> per Cycle';
  951. $continuous_production = 'Continuous Production for ' . $production_time . '</br>Last Generated ' . $last_generation . ' ago';
  952. }
  953. }
  954. else
  955. {
  956. $generate_currency = 'Generation <font color="red">Disabled</strong></font>';
  957. }
  958. $body_string = generation_body($generate_currency_enabled);
  959. if($_GET["generate"] == "showlist")
  960. {
  961. $default_public_key_font = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'public_key_font_size' LIMIT 1"),0,"field_data");
  962. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  963. $body_string = $body_string . '<hr></hr><strong>Current Generation List</strong>
  964. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Public Key</th><th>Joined</th><th>Last Generated</th></tr>';
  965. $sql = "SELECT * FROM `generating_peer_list` ORDER BY `join_peer_list` ASC";
  966. $sql_result = mysql_query($sql);
  967. $sql_num_results = mysql_num_rows($sql_result);
  968. for ($i = 0; $i < $sql_num_results; $i++)
  969. {
  970. $sql_row = mysql_fetch_array($sql_result);
  971. if($my_public_key == $sql_row["public_key"])
  972. {
  973. $public_key = '<p style="font-size:12px;"><font color="green"><strong>My Public Key</strong></font>';
  974. }
  975. else
  976. {
  977. $public_key = '<p style="word-wrap:break-word; width:325px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($sql_row["public_key"]);
  978. }
  979. $body_string .= '<tr>
  980. <td class="style2">' . $public_key . '</p></td>
  981. <td class="style2"><p style="font-size:10px;">' . unix_timestamp_to_human($sql_row["join_peer_list"]) . '</p></td>
  982. <td class="style2"><p style="font-size:10px;">' . tk_time_convert(time() - $sql_row["last_generation"]) . ' ago</p></td></tr>';
  983. }
  984. $body_string .= '</table></div>';
  985. }
  986. if($_GET["generate"] == "showqueue")
  987. {
  988. $default_public_key_font = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'public_key_font_size' LIMIT 1"),0,"field_data");
  989. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  990. $body_string .= '<hr></hr><strong>Election Queue List</strong>
  991. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Public Key</th><th>Join Queue</th></tr>';
  992. $sql = "SELECT * FROM `generating_peer_queue` ORDER BY `timestamp` ASC";
  993. $sql_result = mysql_query($sql);
  994. $sql_num_results = mysql_num_rows($sql_result);
  995. for ($i = 0; $i < $sql_num_results; $i++)
  996. {
  997. $sql_row = mysql_fetch_array($sql_result);
  998. if($my_public_key == $sql_row["public_key"])
  999. {
  1000. $public_key = '<p style="font-size:12px;"><font color="green"><strong>My Public Key</strong></font>';
  1001. }
  1002. else
  1003. {
  1004. $public_key = '<p style="word-wrap:break-word; width:425px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($sql_row["public_key"]);
  1005. }
  1006. $body_string .= '<tr>
  1007. <td class="style2">' . $public_key . '</p></td>
  1008. <td class="style2"><p style="font-size:10px;">' . tk_time_convert(time() - $sql_row["timestamp"]) . ' ago</p></td></tr>';
  1009. }
  1010. $body_string .= '</table></div>';
  1011. }
  1012. // Next Election Calculator
  1013. // Determine when to run this by comparing the last digit the current block and
  1014. // the 3rd digit the generation time; when they match, run the gen key scoring.
  1015. $max_cycles_ahead = 200;
  1016. for ($i = 0; $i < $max_cycles_ahead; $i++)
  1017. {
  1018. $current_generation_cycle = transaction_cycle($i);
  1019. $str = strval($current_generation_cycle);
  1020. $last3_gen = $str[strlen($str)-3];
  1021. $current_generation_block = transaction_cycle($i, TRUE);
  1022. TKRandom::seed($current_generation_block);
  1023. $tk_random_number = TKRandom::num(0, 9);
  1024. if($last3_gen + $tk_random_number > 14)
  1025. {
  1026. $time_election = '<font color="blue"><strong>' . tk_time_convert($current_generation_cycle - time());
  1027. break;
  1028. }
  1029. }
  1030. if(empty($time_election) == TRUE)
  1031. {
  1032. $time_election = 'more than <font color="blue"><strong>' . tk_time_convert($max_cycles_ahead * 300);
  1033. }
  1034. $text_bar = '<table cellspacing="10" border="0"><tr><td valign="top" width="230">' . $generate_currency . '</td><td>Generating Peers: <font color="green"><strong>' . $generating_peers_now . '</strong></font></br>
  1035. Queue for Election: <font color="blue"><strong>' . $generate_peer_queue . '</strong></font></td></tr>
  1036. <tr><td align="right">' . $continuous_production . '</td><td>' . $generate_rate . '</td></tr></table>';
  1037. $quick_info = 'You must remain online to generate currency.</br></br>The longer your server participates, the more it will be allowed to generate as time progresses.</br></br>
  1038. If your server is offline for more than 2 hours, your server will have to rejoin the peer list and any time status will be lost.</br></br>
  1039. Next Peer Election in ' . $time_election . '</strong></font>';
  1040. home_screen('Crypto Currency Generation', $text_bar, $body_string , $quick_info);
  1041. exit;
  1042. }
  1043. //****************************************************************************
  1044. if($_GET["menu"] == "send")
  1045. {
  1046. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  1047. if($_GET["check"] == "key")
  1048. {
  1049. $send_amount = $_POST["send_amount"];
  1050. $public_key_64 = $_POST["send_public_key"];
  1051. $public_key_to = base64_decode($public_key_64);
  1052. $current_balance = db_cache_balance($my_public_key);
  1053. if($send_amount > $current_balance)
  1054. {
  1055. // Can't send this much silly
  1056. $display_balance = db_cache_balance($my_public_key);
  1057. $body_string = send_receive_body($public_key_64);
  1058. $body_string .= '<hr></hr><font color="red"><strong>This exceeds your current balance, send failed...</strong></font></br></br>';
  1059. }
  1060. else
  1061. {
  1062. if($my_public_key == $public_key_to)
  1063. {
  1064. // Can't send to yourself
  1065. $display_balance = db_cache_balance($my_public_key);
  1066. $body_string = send_receive_body();
  1067. $body_string .= '<hr></hr><font color="red"><strong>Can not send to yourself, send failed...</strong></font></br></br>';
  1068. }
  1069. else
  1070. {
  1071. // Check if public key is valid by searching for any transactions
  1072. // that reference it
  1073. $valid_key_test = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `public_key_from` = '$public_key_to' OR `public_key_to` = '$public_key_to' LIMIT 1"),0,"timestamp");
  1074. if(empty($valid_key_test) == TRUE)
  1075. {
  1076. // No key history, might not be valid
  1077. $message = $_POST["send_message"];
  1078. $display_balance = db_cache_balance($my_public_key);
  1079. $body_string = send_receive_body($public_key_64, $send_amount, TRUE, NULL, $message);
  1080. $body_string .= '<hr></hr><font color="red"><strong>This public key may not be valid as it has no existing history of transactions.</br>
  1081. There is no way to recover timekoins sent to the wrong public key.</br>
  1082. Click "Send Timekoins" to send now.</strong></font></br></br>';
  1083. }
  1084. else
  1085. {
  1086. // Key has a valid history
  1087. $message = $_POST["send_message"];
  1088. $display_balance = db_cache_balance($my_public_key);
  1089. $body_string = send_receive_body($public_key_64, $send_amount, TRUE, NULL, $message);
  1090. $body_string .= '<hr></hr><font color="blue"><strong>This public key is valid.</font></br>
  1091. <font color="red">There is no way to recover timekoins sent to the wrong public key.</font></br>
  1092. <font color="blue">Click "Send Timekoins" to send now.</strong></font></br></br>';
  1093. }
  1094. } // End self check
  1095. } // End balance check
  1096. }
  1097. else
  1098. {
  1099. if($_GET["complete"] == "send")
  1100. {
  1101. // Build the transaction and insert into the queue
  1102. $send_amount = $_POST["send_amount"];
  1103. $public_key_64 = $_POST["send_public_key"];
  1104. $message = $_POST["send_message"];
  1105. $public_key_to = base64_decode($public_key_64);
  1106. $current_balance = db_cache_balance($my_public_key);
  1107. if($send_amount > $current_balance)
  1108. {
  1109. // Can't send this much silly
  1110. $display_balance = db_cache_balance($my_public_key);
  1111. $body_string = send_receive_body($public_key_64);
  1112. $body_string .= '<hr></hr><font color="red"><strong>This exceeds your current balance, send failed...</strong></font></br></br>';
  1113. }
  1114. else
  1115. {
  1116. if($my_public_key == $public_key_to)
  1117. {
  1118. // Can't send to yourself
  1119. $display_balance = db_cache_balance($my_public_key);
  1120. $body_string = send_receive_body();
  1121. $body_string .= '<hr></hr><font color="red"><strong>Can not send to yourself, send failed...</strong></font></br></br>';
  1122. }
  1123. else
  1124. {
  1125. // Now it's time to send the transaction
  1126. $my_private_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_private_key' LIMIT 1"),0,"field_data");
  1127. if(send_timekoins($my_private_key, $my_public_key, $public_key_to, $send_amount, $message) == TRUE)
  1128. {
  1129. $display_balance = db_cache_balance($my_public_key);
  1130. $body_string = send_receive_body($public_key_64, $send_amount);
  1131. $body_string .= '<hr></hr><font color="green"><strong>You just sent ' . $send_amount . ' timekoins to the above public key.</font></br>
  1132. Your balance will not reflect this until the transation is recorded across the entire network.</strong></br></br>';
  1133. }
  1134. else
  1135. {
  1136. $display_balance = db_cache_balance($my_public_key);
  1137. $body_string = send_receive_body($public_key_64, $send_amount);
  1138. $body_string .= '<hr></hr><font color="red"><strong>Send failed...</strong></font></br></br>';
  1139. }
  1140. } // End duplicate self check
  1141. } // End Balance Check
  1142. } // End check send command
  1143. else
  1144. {
  1145. if($_GET["easykey"] == "grab")
  1146. {
  1147. ini_set('user_agent', 'Timekoin Server (GUI) v' . TIMEKOIN_VERSION);
  1148. ini_set('default_socket_timeout', 10); // Timeout for request in seconds
  1149. $message = $_POST["send_message"];
  1150. $easy_key = filter_sql($_POST["easy_key"]); // Filter SQL just in case
  1151. $last_easy_key = filter_sql($_POST["easy_key"]); // Filter SQL just in case
  1152. // Translate Easy Key to Public Key and fill in field with
  1153. $context = stream_context_create(array('http' => array('header'=>'Connection: close'))); // Force close socket after complete
  1154. $easy_key = filter_sql(file_get_contents("http://timekoin.net/easy.php?s=$easy_key", FALSE, $context, NULL, 500));
  1155. $easy_key = filter_sql($easy_key);
  1156. if($easy_key == "ERROR" || empty($easy_key) == TRUE)
  1157. {
  1158. $server_message = '<font color="red"><strong>' . $last_easy_key . ' Not Found. Check Your Spelling.</strong></font>';
  1159. $easy_key = NULL;
  1160. }
  1161. else
  1162. {
  1163. $server_message = '<font color="blue"><strong>Easy Key Found</strong></font>';
  1164. }
  1165. }
  1166. // No selections made, default screen
  1167. $display_balance = db_cache_balance($my_public_key);
  1168. $body_string = send_receive_body($easy_key, NULL, NULL, $last_easy_key, $message);
  1169. $body_string .= $server_message;
  1170. }
  1171. }
  1172. $text_bar = '<table border="0" cellpadding="6"><tr><td><strong>Current Server Balance: <font color="green">' . number_format($display_balance) . '</font></strong></td></tr>
  1173. <tr><td><strong><font color="green">Public Key</font> to receive:</strong></td></tr>
  1174. <tr><td><textarea readonly="readonly" rows="6" cols="75">' . base64_encode($my_public_key) . '</textarea></td></tr></table>';
  1175. $quick_info = 'Send your own Timekoins to someone else.</br></br>
  1176. Your server will attempt to verify if the public key is valid by examing the transaction history before sending.</br></br>
  1177. New public keys with no history could appear invalid for this reason, so always double check.</br></br>
  1178. You can enter an <strong>Easy Key</strong> and Timekoin will fill in the Public Key field for you.</br></br>
  1179. Messages encoded into your transaction are limited to <strong>64</strong> characters and are visible to anyone.</br>No | ? = \' ` * % characters allowed.';
  1180. home_screen('Send / Receive Timekoins', $text_bar, $body_string , $quick_info);
  1181. exit;
  1182. }
  1183. //****************************************************************************
  1184. if($_GET["menu"] == "history")
  1185. {
  1186. if($_GET["receive"] == "listmore" || $_GET["send"] == "listmore")
  1187. {
  1188. if(empty($_GET["send"]) == TRUE)
  1189. {
  1190. $show_last = $_POST["show_more_receive"];
  1191. $hide_send = TRUE;
  1192. }
  1193. else
  1194. {
  1195. $show_last = $_POST["show_more_send"];
  1196. $hide_receive = TRUE;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. $show_last = 5; // Default number of last items to show
  1202. }
  1203. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  1204. if($_GET["font"] == "public_key")
  1205. {
  1206. if(empty($_POST["font_size"]) == FALSE)
  1207. {
  1208. // Save value in database
  1209. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["font_size"] . "' WHERE `options`.`field_name` = 'public_key_font_size' LIMIT 1";
  1210. mysql_query($sql);
  1211. $default_public_key_font = $_POST["font_size"];
  1212. }
  1213. }
  1214. else
  1215. {
  1216. $default_public_key_font = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'public_key_font_size' LIMIT 1"),0,"field_data");
  1217. }
  1218. if($hide_receive == FALSE)
  1219. {
  1220. if(empty($_POST['filter']) == FALSE)
  1221. {
  1222. $filter_results;
  1223. $filter_GUI;
  1224. switch($_POST['filter'])
  1225. {
  1226. case "transactions":
  1227. $filter_results = "AND `attribute` = 'T'";
  1228. $filter_GUI = "Transactions";
  1229. break;
  1230. case "generation":
  1231. $filter_results = "AND `attribute` = 'G'";
  1232. $filter_GUI = "Currency Generation";
  1233. break;
  1234. }
  1235. }
  1236. else
  1237. {
  1238. $filter_GUI = "Transactions & Currency Generation";
  1239. }
  1240. $body_string = '<strong>Showing Last <font color="blue">' . $show_last . '</font> ' . $filter_GUI . ' <font color="green">Sent To</font> This Server</strong></br>
  1241. <FORM ACTION="index.php?menu=history&receive=listmore" METHOD="post"><select name="filter"><option value="transactions">Transactions Only</option>
  1242. <option value="generation">Generation Only</option><option value="all" SELECTED>Both</option></option></select></br>
  1243. </br><div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Date</th>
  1244. <th>Sent From</th><th>Amount</th><th>Verification Level</th><th>Message</th></tr>';
  1245. // Find the last 5 transactions sent to this public key
  1246. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_to` = '$my_public_key' $filter_results ORDER BY `transaction_history`.`timestamp` DESC LIMIT $show_last";
  1247. $sql_result = mysql_query($sql);
  1248. $sql_num_results = mysql_num_rows($sql_result);
  1249. for ($i = 0; $i < $sql_num_results; $i++)
  1250. {
  1251. $sql_row = mysql_fetch_array($sql_result);
  1252. $crypt3 = $sql_row["crypt_data3"];
  1253. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $sql_row["public_key_from"]);
  1254. $transaction_amount = find_string("AMOUNT=", "---TIME", $transaction_info);
  1255. // Any encoded messages?
  1256. $inside_message = find_string("---MSG=", "", $transaction_info, TRUE);
  1257. if($sql_row["public_key_from"] == $my_public_key)
  1258. {
  1259. // Self Generated
  1260. $public_key_from = '<td class="style2">Self Generated';
  1261. }
  1262. else
  1263. {
  1264. // Everyone else
  1265. $public_key_from = '<td class="style1"><p style="word-wrap:break-word; width:150px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($sql_row["public_key_from"]) . '</p>';
  1266. }
  1267. // How many cycles back did this take place?
  1268. $cycles_back = intval((time() - $sql_row["timestamp"]) / 300);
  1269. $body_string .= '<tr>
  1270. <td class="style2"><p style="font-size: 11px;">' . unix_timestamp_to_human($sql_row["timestamp"]) . '</p></td>'
  1271. . $public_key_from . '</td>
  1272. <td class="style2"><p style="font-size: 11px;">' . $transaction_amount . '</p></td>
  1273. <td class="style2"><p style="font-size: 11px;">' . $cycles_back . '</p></td>
  1274. <td class="style2"><p style="word-wrap:break-word; width:110px; font-size: 11px;">' . $inside_message . '</p></td></tr>';
  1275. }
  1276. $body_string .= '<tr><td colspan="5"><input type="text" size="5" name="show_more_receive" value="' . $show_last .'" /><input type="submit" name="Submit1" value="Show Last" /></FORM></td></tr>';
  1277. $body_string .= '</table></div>';
  1278. } // End hide check for receive
  1279. if($hide_send == FALSE)
  1280. {
  1281. $body_string .= '<strong>Showing Last <font color="blue">' . $show_last . '</font> Transactions <font color="blue">Sent From</font> This Server</strong></br></br><div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Date</th>
  1282. <th>Sent To</th><th>Amount</th><th>Verification Level</th><th>Message</th></tr>';
  1283. // Find the last 5 transactions from to this public key
  1284. $sql = "SELECT * FROM `transaction_history` WHERE `public_key_from` = '$my_public_key' AND `public_key_to` != '$my_public_key' ORDER BY `transaction_history`.`timestamp` DESC LIMIT $show_last";
  1285. $sql_result = mysql_query($sql);
  1286. $sql_num_results = mysql_num_rows($sql_result);
  1287. for ($i = 0; $i < $sql_num_results; $i++)
  1288. {
  1289. $sql_row = mysql_fetch_array($sql_result);
  1290. $crypt3 = $sql_row["crypt_data3"];
  1291. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $sql_row["public_key_from"]);
  1292. $transaction_amount = find_string("AMOUNT=", "---TIME", $transaction_info);
  1293. // Any encoded messages?
  1294. $inside_message = find_string("---MSG=", "", $transaction_info, TRUE);
  1295. // Everyone else
  1296. $public_key_from = '<td class="style1"><p style="word-wrap:break-word; width:150px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($sql_row["public_key_to"]) . '</p>';
  1297. // How many cycles back did this take place?
  1298. $cycles_back = intval((time() - $sql_row["timestamp"]) / 300);
  1299. $body_string .= '<tr>
  1300. <td class="style2"><p style="font-size: 11px;">' . unix_timestamp_to_human($sql_row["timestamp"]) . '</p></td>'
  1301. . $public_key_from . '</td>
  1302. <td class="style2"><p style="font-size: 11px;">' . $transaction_amount . '</p></td>
  1303. <td class="style2"><p style="font-size: 11px;">' . $cycles_back . '</p></td>
  1304. <td class="style2"><p style="word-wrap:break-word; width:110px; font-size: 11px;">' . $inside_message . '</p></td></tr>';
  1305. }
  1306. $body_string .= '<tr><td colspan="5"><FORM ACTION="index.php?menu=history&send=listmore" METHOD="post"><input type="text" size="5" name="show_more_send" value="' . $show_last .'" /><input type="submit" name="Submit2" value="Show Last" /></FORM></td></tr>';
  1307. $body_string .= '</table></div>';
  1308. } // End hide check for send
  1309. $text_bar = '<FORM ACTION="index.php?menu=history&font=public_key" METHOD="post">
  1310. <table border="0" cellspacing="4"><tr><td><strong>Default Public Key Font Size</strong></td><td><input type="text" size="2" name="font_size" value="' . $default_public_key_font .'" /><input type="submit" name="Submit3" value="Save" /></td></tr></table></FORM>';
  1311. $quick_info = 'Verification Level represents how deep in the transaction history the transaction exist.</br></br>
  1312. The larger the number, the more time that all the peers have examined it and agree that it is a valid transaction.';
  1313. home_screen('Transaction History', $text_bar, $body_string , $quick_info);
  1314. exit;
  1315. }
  1316. //****************************************************************************
  1317. if($_GET["menu"] == "queue")
  1318. {
  1319. if($_GET["font"] == "public_key")
  1320. {
  1321. if(empty($_POST["font_size"]) == FALSE)
  1322. {
  1323. // Save value in database
  1324. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["font_size"] . "' WHERE `options`.`field_name` = 'public_key_font_size' LIMIT 1";
  1325. mysql_query($sql);
  1326. header("Location: index.php?menu=queue");
  1327. exit;
  1328. }
  1329. }
  1330. else
  1331. {
  1332. $default_public_key_font = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'public_key_font_size' LIMIT 1"),0,"field_data");
  1333. }
  1334. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  1335. // Find the last X amount of transactions sent to this public key
  1336. $sql = "SELECT * FROM `transaction_queue` ORDER BY `transaction_queue`.`timestamp` DESC";
  1337. $sql_result = mysql_query($sql);
  1338. $sql_num_results = mysql_num_rows($sql_result);
  1339. $body_string = '<strong><font color="blue">( ' . number_format($sql_num_results) . ' )</font> Network Transactions Waiting for Processing</strong></br></br><div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Date</th>
  1340. <th>Sent From</th><th>Sent To</th><th>Amount</th></tr>';
  1341. for ($i = 0; $i < $sql_num_results; $i++)
  1342. {
  1343. $sql_row = mysql_fetch_array($sql_result);
  1344. $crypt1 = $sql_row["crypt_data1"];
  1345. $crypt2 = $sql_row["crypt_data2"];
  1346. $crypt3 = $sql_row["crypt_data3"];
  1347. $public_key_trans = $sql_row["public_key"];
  1348. // Decode the public key this transaction is being sent to
  1349. openssl_public_decrypt(base64_decode($crypt1), $public_key_to_1, $public_key_trans);
  1350. openssl_public_decrypt(base64_decode($crypt2), $public_key_to_2, $public_key_trans);
  1351. $public_key_trans_to = $public_key_to_1 . $public_key_to_2;
  1352. // Decode Amount
  1353. openssl_public_decrypt(base64_decode($crypt3), $transaction_info, $public_key_trans);
  1354. $transaction_amount = find_string("AMOUNT=", "---TIME", $transaction_info);
  1355. if($public_key_trans == $my_public_key)
  1356. {
  1357. if($public_key_trans_to == $my_public_key)
  1358. {
  1359. // Currency Generation
  1360. $public_key_from = '<td class="style2"><font color="blue">Currency Generation</font>';
  1361. $public_key_to = '<td class="style2"><font color="green">Self</font>';
  1362. }
  1363. else
  1364. {
  1365. // Self Generated to someone else
  1366. $public_key_from = '<td class="style2"><font color="blue">Self Generated Transaction</font>';
  1367. $public_key_to = '<td class="style1"><p style="word-wrap:break-word; width:175px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($public_key_trans_to) . '</p>';
  1368. }
  1369. }
  1370. else
  1371. {
  1372. // Everyone else
  1373. if($sql_row["attribute"] == "G")
  1374. {
  1375. $public_key_to = '<td class="style2"><font color="green">Currency Generation</font>';
  1376. }
  1377. else
  1378. {
  1379. $public_key_to = '<td class="style1"><p style="word-wrap:break-word; width:170px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($public_key_trans_to) . '</p>';
  1380. }
  1381. $public_key_from = '<td class="style1"><p style="word-wrap:break-word; width:170px; font-size:' . $default_public_key_font . 'px;">' . base64_encode($public_key_trans) . '</p>';
  1382. }
  1383. if($sql_row["attribute"] == "R")
  1384. {
  1385. $transaction_amount = "R";
  1386. $public_key_to = '<td class="style1"><p style="font-size:12px;"><strong><font color="blue">Election Request</font></strong></p>';
  1387. }
  1388. $body_string .= '<tr>
  1389. <td class="style2">' . unix_timestamp_to_human($sql_row["timestamp"]) . '</td>'
  1390. . $public_key_from . '</td>'
  1391. . $public_key_to . '</td>
  1392. <td class="style2">' . $transaction_amount . '</td></tr>';
  1393. }
  1394. $body_string .= '</table></div>';
  1395. $text_bar = '<FORM ACTION="index.php?menu=queue&font=public_key" METHOD="post">
  1396. <table border="0" cellspacing="4"><tr><td><strong>Default Public Key Font Size</strong></td><td><input type="text" size="2" name="font_size" value="' . $default_public_key_font .'" /><input type="submit" name="Submit3" value="Save" /></td></tr></table></FORM>';
  1397. $quick_info = 'This section contains all the network transactions that are queued to be stored in the transaction history.';
  1398. $queue_update = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'refresh_realtime_queue' LIMIT 1"),0,"field_data");
  1399. home_screen('Realtime Transactions in Network Queue', $text_bar, $body_string , $quick_info, $queue_update);
  1400. exit;
  1401. }
  1402. //****************************************************************************
  1403. if($_GET["menu"] == "tools")
  1404. {
  1405. if($_GET["action"] == "walk_history")
  1406. {
  1407. set_time_limit(100);
  1408. $body_string = '<strong>History Walk from Block #<font color="blue">' . $_POST["walk_history"] . '</font> can take some time, please be patient...</font></strong></br></br>
  1409. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>History Walk</th></tr>';
  1410. $block_end = $_POST["walk_history"] + 500;
  1411. $body_string .= visual_walkhistory($_POST["walk_history"], $block_end);
  1412. $body_string .= '</table></div>';
  1413. }
  1414. if($_GET["action"] == "schedule_check")
  1415. {
  1416. $sql = "UPDATE `options` SET `field_data` = '" . $_POST["schedule_check"] . "' WHERE `options`.`field_name` = 'transaction_history_block_check' LIMIT 1";
  1417. if(mysql_query($sql) == TRUE)
  1418. {
  1419. $body_string = '<strong>A Block Check has been scheduled for #<font color="blue">' . $_POST["schedule_check"] . '</font></strong>';
  1420. write_log("A History Check was Scheduled for Block #" . $_POST["schedule_check"], "GU");
  1421. }
  1422. else
  1423. {
  1424. $body_string = '<strong><font color="red">There was a Database ERROR to schedule Block #<font color="blue">' . $_POST["schedule_check"] . '</font></strong></font>';
  1425. }
  1426. }
  1427. if($_GET["action"] == "repair")
  1428. {
  1429. set_time_limit(100);
  1430. $body_string = '<strong>Start Repair from Block #<font color="blue">' . $_POST["repair_from"] . '</font></br>
  1431. This can take some time, please be patient...</strong></br></br>
  1432. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Repair History</th></tr>';
  1433. $body_string .= visual_repair($_POST["repair_from"]);
  1434. $body_string .= '</table></div>';
  1435. write_log("A History Block Repair was started from #" . $_POST["repair_from"], "GU");
  1436. }
  1437. if($_GET["action"] == "check_tables")
  1438. {
  1439. set_time_limit(120);
  1440. write_log("A Check of the Entire Database & Tables Was Started.", "GU");
  1441. $body_string = '<strong>Checking All Database Tables</strong></font></br></br>
  1442. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Check Database Results</th></tr><tr><td>';
  1443. $db_check = mysql_query("CHECK TABLE `activity_logs` , `generating_peer_list` , `generating_peer_queue` , `my_keys` , `my_transaction_queue` , `options` , `transaction_foundation` , `transaction_history` , `transaction_queue`");
  1444. $db_check_info = mysql_fetch_array($db_check);
  1445. $db_check_count = 0;
  1446. while(empty($db_check_info["$db_check_count"]) == FALSE)
  1447. {
  1448. $body_string .= $db_check_info["$db_check_count"] . " ";
  1449. $db_check_count++;
  1450. if(empty($db_check_info["$db_check_count"]) == TRUE)
  1451. {
  1452. // Move to next array
  1453. $db_check_info = mysql_fetch_array($db_check);
  1454. $db_check_count = 0;
  1455. $body_string .= "</td></tr><tr><td>";
  1456. }
  1457. }
  1458. $body_string .= '<strong>CHECK COMPLETE</strong></td></tr></table></div>';
  1459. write_log("A Check of the Entire Database & Tables Was Finished.", "GU");
  1460. }
  1461. if($_GET["action"] == "repair_tables")
  1462. {
  1463. set_time_limit(300);
  1464. write_log("A REPAIR of the Entire Database & Tables Was Started.", "GU");
  1465. $body_string = '<strong>Repair All Database Tables</strong></font></br></br>
  1466. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Check Database Results</th></tr><tr><td>';
  1467. $db_check = mysql_query("REPAIR TABLE `activity_logs` , `generating_peer_list` , `generating_peer_queue` , `my_keys` , `my_transaction_queue` , `options` , `transaction_foundation` , `transaction_history` , `transaction_queue`");
  1468. $db_check_info = mysql_fetch_array($db_check);
  1469. $db_check_count = 0;
  1470. while(empty($db_check_info["$db_check_count"]) == FALSE)
  1471. {
  1472. $body_string .= $db_check_info["$db_check_count"] . " ";
  1473. $db_check_count++;
  1474. if(empty($db_check_info["$db_check_count"]) == TRUE)
  1475. {
  1476. // Move to next array
  1477. $db_check_info = mysql_fetch_array($db_check);
  1478. $db_check_count = 0;
  1479. $body_string .= "</td></tr><tr><td>";
  1480. }
  1481. }
  1482. $body_string .= '<strong>REPAIR FINISHED</strong></td></tr></table></div>';
  1483. write_log("A REPAIR of the Entire Database & Tables Was Finished.", "GU");
  1484. }
  1485. if($_GET["logs"] == "listmore")
  1486. {
  1487. $show_last = $_POST["show_more_logs"];
  1488. }
  1489. else
  1490. {
  1491. $show_last = 5; // Default number of last logs to show
  1492. }
  1493. if($_GET["logs"] == "clear")
  1494. {
  1495. mysql_query("TRUNCATE TABLE `activity_logs`");
  1496. }
  1497. if(empty($_GET["action"]) == TRUE)
  1498. {
  1499. // Show log history
  1500. if(empty($_POST["filter"]) == FALSE)
  1501. {
  1502. $filter_by;
  1503. switch($_POST["filter"])
  1504. {
  1505. case "FO":
  1506. $filter_by = ' (Filtered by <strong>Foundation Manager</strong>)';
  1507. break;
  1508. case "G":
  1509. $filter_by = ' (Filtered by <strong>Generation Events</strong>)';
  1510. break;
  1511. case "GP":
  1512. $filter_by = ' (Filtered by <strong>Generation Peer Manager</strong>)';
  1513. break;
  1514. case "R":
  1515. $filter_by = ' (Filtered by <strong>Generation Request</strong>)';
  1516. break;
  1517. case "MA":
  1518. $filter_by = ' (Filtered by <strong>Main Program</strong>)';
  1519. break;
  1520. case "PL":
  1521. $filter_by = ' (Filtered by <strong>Peer Processor</strong>)';
  1522. break;
  1523. case "QC":
  1524. $filter_by = ' (Filtered by <strong>Transaction Queue Clerk</strong>)';
  1525. break;
  1526. case "TC":
  1527. $filter_by = ' (Filtered by <strong>Transaction Queue Clerk</strong>)';
  1528. break;
  1529. case "T":
  1530. $filter_by = ' (Filtered by <strong>Transactions</strong>)';
  1531. break;
  1532. case "TR":
  1533. $filter_by = ' (Filtered by <strong>Treasurer Processor</strong>)';
  1534. break;
  1535. case "WA":
  1536. $filter_by = ' (Filtered by <strong>Watchdog</strong>)';
  1537. break;
  1538. }
  1539. }
  1540. $body_string = '<strong>Showing Last <font color="blue">' . $show_last . '</font> Log Events</strong>' . $filter_by . '<table border="0" cellspacing="5"><tr><td>
  1541. Filter By:</td><td><FORM ACTION="index.php?menu=tools&logs=listmore" METHOD="post"><select name="filter"><option value="all" SELECTED>Show All</option>
  1542. <option value="FO">Foundation Manager</option><option value="G">Generation Events</option><option value="GP">Generation Peer Manager</option>
  1543. <option value="R">Generation Request</option><option value="MA">Main Program</option><option value="PL">Peer Processor</option><option value="QC">Transaction Queue Clerk</option>
  1544. <option value="TC">Transaction Clerk</option><option value="T">Transactions</option><option value="TR">Treasurer Processor</option><option value="WA">Watchdog</option></select></td></tr></table>
  1545. <div class="table"><table class="listing" border="0" cellspacing="0" cellpadding="0" ><tr><th>Date</th><th>Log</th><th>Attribute</th></tr>';
  1546. // Find the last X amount of log events
  1547. if($_POST["filter"] == "all" || empty($_POST["filter"]) == TRUE)
  1548. {
  1549. $sql = "SELECT * FROM `activity_logs` ORDER BY `activity_logs`.`timestamp` DESC LIMIT $show_last";
  1550. }
  1551. else
  1552. {
  1553. $sql = "SELECT * FROM `activity_logs` WHERE `attribute` = '" . $_POST["filter"] . "' ORDER BY `activity_logs`.`timestamp` DESC LIMIT $show_last";
  1554. }
  1555. $sql_result = mysql_query($sql);
  1556. $sql_num_results = mysql_num_rows($sql_result);
  1557. for ($i = 0; $i < $sql_num_results; $i++)
  1558. {
  1559. $sql_row = mysql_fetch_array($sql_result);
  1560. $body_string .= '<tr>
  1561. <td class="style2"><p style="width:160px;">' . unix_timestamp_to_human($sql_row["timestamp"]) . '</p></td>
  1562. <td class="style2"><p style="word-wrap:break-word; width:360px;">' . $sql_row["log"] . '</p></td>
  1563. <td class="style2">' . $sql_row["attribute"] . '</td></tr>';
  1564. }
  1565. $body_string .= '<tr><td><input type="text" size="5" name="show_more_logs" value="' . $show_last .'" /><input type="submit" name="show_last" value="Show Last" /></FORM></td>
  1566. <td colspan="2"><FORM ACTION="index.php?menu=tools&logs=clear" METHOD="post"><input type="submit" name="clear_logs" value="Clear All Logs" /></FORM></td></tr>';
  1567. $body_string .= '</table></div>';
  1568. }
  1569. $text_bar = tools_bar();
  1570. $quick_info = '<strong>History Walk</strong> will manually test all transactions starting at the specified block and give a status for each block.</br></br>
  1571. <strong>Schedule Check</strong> will schedule Timekoin to check and repair the specified block.</br></br>
  1572. <strong>Repair</strong> will force Timekoin to recalculate all verification hashes from the specified block to now.</br></br>
  1573. <strong>Check Database</strong> will check the data integrity of all tables in the database.</br></br>
  1574. <strong>Repair Database</strong> will attempt to repair all tables in the database.</br></br>
  1575. <i>Note:</i> The repair utilities can take a long time to process and complete.';
  1576. home_screen('Tools & Utilities', $text_bar, $body_string , $quick_info);
  1577. exit;
  1578. }
  1579. //****************************************************************************
  1580. if($_GET["menu"] == "backup")
  1581. {
  1582. if($_GET["dorestore"] == "private" && empty($_POST["restore_private_key"]) == FALSE)
  1583. {
  1584. $sql = "UPDATE `my_keys` SET `field_data` = '" . base64_decode($_POST["restore_private_key"]) . "' WHERE `my_keys`.`field_name` = 'server_private_key' LIMIT 1";
  1585. if(mysql_query($sql) == TRUE)
  1586. {
  1587. $server_message = '</br><font color="blue"><strong>Private Key Restore Complete!</strong></font></br></br>';
  1588. }
  1589. else
  1590. {
  1591. $server_message = '</br><font color="red"><strong>Private Key Restore FAILED!</strong></font></br></br>';
  1592. }
  1593. }
  1594. if($_GET["dorestore"] == "public" && empty($_POST["restore_public_key"]) == FALSE)
  1595. {
  1596. $sql = "UPDATE `my_keys` SET `field_data` = '" . base64_decode($_POST["restore_public_key"]) . "' WHERE `my_keys`.`field_name` = 'server_public_key' LIMIT 1";
  1597. if(mysql_query($sql) == TRUE)
  1598. {
  1599. $server_message = '</br><font color="blue"><strong>Public Key Restore Complete!</strong></font></br></br>';
  1600. }
  1601. else
  1602. {
  1603. $server_message = '</br><font color="red"><strong>Public Key Restore FAILED!</strong></font></br></br>';
  1604. }
  1605. }
  1606. $my_private_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_private_key' LIMIT 1"),0,"field_data");
  1607. $my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
  1608. if($_GET["restore"] == "private" && empty($_POST["restore_private_key"]) == FALSE)
  1609. {
  1610. $body_string = backup_body($_POST["restore_private_key"], NULL, TRUE, NULL);
  1611. }
  1612. else if($_GET["restore"] == "public" && empty($_POST["restore_public_key"]) == FALSE)
  1613. {
  1614. $body_string = backup_body(NULL, $_POST["restore_public_key"], NULL, TRUE);
  1615. }
  1616. else
  1617. {
  1618. $body_string = backup_body();
  1619. }
  1620. $body_string .= $server_message;
  1621. $text_bar = '<table border="0" cellpadding="6"><tr><td><strong><font color="blue">Private Key</font> to send transactions:</strong></td></tr>
  1622. <tr><td><textarea readonly="readonly" rows="8" cols="75">' . base64_encode($my_private_key) . '</textarea></td></tr></table>
  1623. <table border="0" cellpadding="6"><tr><td><strong><font color="green">Public Key</font> to receive:</strong></td></tr>
  1624. <tr><td><textarea readonly="readonly" rows="6" cols="75">' . base64_encode($my_public_key) . '</textarea></td></tr></table>';
  1625. $quick_info = '<strong>Do Not</strong> share your Private Key with anyone for any reason.</br></br>
  1626. The Private Key encrypts all transactions from your server.</br></br>
  1627. You should make a backup of both keys in case you want to transfer your balance to a new server or restore from a server failure.</br></br>
  1628. Save both keys in a password protected text file or external device that you can secure (CD, Flash Drive, Printed Paper, etc.)';
  1629. home_screen('Backup & Restore Keys', $text_bar, $body_string , $quick_info);
  1630. exit;
  1631. }
  1632. //****************************************************************************
  1633. if($_GET["menu"] == "logoff")
  1634. {
  1635. unset($_SESSION["valid_login"]);
  1636. unset($_SESSION["login_username"]);
  1637. header("Location: index.php");
  1638. exit;
  1639. }
  1640. //****************************************************************************
  1641. } // End Valid Login Check
  1642. //****************************************************************************
  1643. //****************************************************************************
  1644. ?>