PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/v1.9/main.php

https://bitbucket.org/rev22/timekoin
PHP | 292 lines | 202 code | 41 blank | 49 comment | 54 complexity | c4cbe5830ac772c5b05cc9b9f7ab66e4 MD5 | raw file
  1. <?PHP
  2. include 'configuration.php';
  3. include 'function.php';
  4. if($_GET["action"]=="begin_main")
  5. {
  6. if(mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD) == FALSE)
  7. {
  8. // Database connect error
  9. $datbase_error = TRUE;
  10. }
  11. if(mysql_select_db(MYSQL_DATABASE) == FALSE)
  12. {
  13. // Database select error
  14. $datbase_error = TRUE;
  15. }
  16. // Check last heartbeat and make sure it was more than X seconds ago
  17. $main_heartbeat_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  18. if($main_heartbeat_active == FALSE && $datbase_error == FALSE)
  19. {
  20. // Database Initialization
  21. initialization_database();
  22. mysql_query("UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'main_last_heartbeat' LIMIT 1");
  23. // Set loop at active now
  24. mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
  25. source_bg('main.php');
  26. activate(TIMEKOINSYSTEM, 1); // In case this was disabled from a emergency stop call in the server GUI
  27. header("Location: index.php?menu=system&code=1");
  28. exit;
  29. }
  30. else
  31. {
  32. header("Location: index.php?menu=system&code=99");
  33. exit;
  34. }
  35. }
  36. $mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
  37. mysql_select_db(MYSQL_DATABASE);
  38. $context = stream_context_create(array('http' => array('header'=>'Connection: close'))); // Force close socket after complete
  39. ini_set('user_agent', 'Timekoin Server (Main) v' . TIMEKOIN_VERSION);
  40. ini_set('default_socket_timeout', 3); // Timeout for request in seconds
  41. while(1) // Begin Infinite Loop
  42. {
  43. // Are we to remain active?
  44. $loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  45. if($loop_active === FALSE) // Databaes Error
  46. {
  47. // Database Error, try to re-establish a connection after 5 seconds
  48. mysql_close($mysql_link);
  49. sleep(5);
  50. $mysql_link = mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
  51. mysql_select_db(MYSQL_DATABASE);
  52. // Keep track of errors in case this can't be recovered from
  53. $datbase_error = TRUE;
  54. $database_error_counter++;
  55. }
  56. else
  57. {
  58. $datbase_error = 0;
  59. $database_error_counter = 0;
  60. }
  61. if($loop_active == 1)
  62. {
  63. // Main loop work goes below
  64. // Set the working status of 2
  65. mysql_query("UPDATE `main_loop_status` SET `field_data` = '2' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
  66. timekoin_debug_msg("main", "main loop start");
  67. //*****************************************************************************************************
  68. //*****************************************************************************************************
  69. // Do a random time sync check and report any errors to the user
  70. if(rand(1,25) == 10)
  71. {
  72. source_bg('timecheck.php');
  73. }
  74. //*****************************************************************************************************
  75. //*****************************************************************************************************
  76. // Check for spamming IPs
  77. timekoin_debug_msg("main", "check for spamming IPs");
  78. $sql = "SELECT * FROM `ip_activity` GROUP BY `ip`";
  79. $sql_result = mysql_query($sql);
  80. $sql_num_results = mysql_num_rows($sql_result);
  81. $request_max = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'server_request_max' LIMIT 1"),0,"field_data");
  82. for ($i = 0; $i < $sql_num_results; $i++)
  83. {
  84. $sql_row = mysql_fetch_array($sql_result);
  85. $select_IP = $sql_row["ip"];
  86. $sql = "SELECT * FROM `ip_activity` WHERE `ip` = '$select_IP'";
  87. $sql_num_results2 = mysql_num_rows(mysql_query($sql));
  88. if($sql_num_results2 > $request_max)
  89. {
  90. // More than X request per cycle means something is wrong
  91. // so this IP needs to be banned for a while
  92. mysql_query("INSERT INTO `ip_banlist` (`when` ,`ip`)VALUES (" . time() . ", '$select_IP')");
  93. write_log("IP Address $select_IP was added to the ban list due to excessive traffic. Default max query is $request_max per cycle, IP was doing $sql_num_results2 query per cycle instead.", "MA");
  94. }
  95. }
  96. // Clear IP Activity for next cycle
  97. mysql_query("TRUNCATE TABLE `ip_activity`");
  98. // Clear out ban list of IPs older than 1 day
  99. if(rand(1,60) == 30) // Randomize a little to save DB usage
  100. {
  101. mysql_query("DELETE FROM `ip_banlist` WHERE `ip_banlist`.`when` < " . (time() - 86400));
  102. }
  103. //*****************************************************************************************************
  104. //*****************************************************************************************************
  105. // Check to make sure we are not behind a firewall with no Inbound ports
  106. timekoin_debug_msg("main", "check to make sure we are not behind a firewall with no Inbound port");
  107. if($sql_num_results == 0) // Randomize a little
  108. {
  109. if(rand(1,3) == 2)
  110. {
  111. // No activity from any peer, keep track of this
  112. $no_peer_activity = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'no_peer_activity' LIMIT 1"),0,"field_data");
  113. $no_peer_activity++;
  114. if($no_peer_activity < 9)
  115. {
  116. mysql_query("UPDATE `options` SET `field_data` = '$no_peer_activity' WHERE `options`.`field_name` = 'no_peer_activity' LIMIT 1");
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if(rand(1,3) == 2)
  123. {
  124. $no_peer_activity = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'no_peer_activity' LIMIT 1"),0,"field_data");
  125. if($no_peer_activity > 1)
  126. {
  127. // Disable Firewalled Mode, Inbound is working again
  128. mysql_query("UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'firewall_blocked_peer' LIMIT 1");
  129. mysql_query("UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'no_peer_activity' LIMIT 1");
  130. $no_peer_activity = FALSE;
  131. }
  132. }
  133. }
  134. if($no_peer_activity > 5 && $no_peer_activity < 7)
  135. {
  136. // No Inbound connection working, the only way to submit transactions is out remotely.
  137. // Switch to firewalled mode.
  138. mysql_query("UPDATE `options` SET `field_data` = '1' WHERE `options`.`field_name` = 'firewall_blocked_peer' LIMIT 1");
  139. }
  140. //*****************************************************************************************************
  141. //*****************************************************************************************************
  142. timekoin_debug_msg("main", "checking transaction clerk");
  143. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  144. // Check if script is already running
  145. if($script_loop_active == 0)
  146. {
  147. source_bg('transclerk.php');
  148. }
  149. sleep(1); // 1 second for sanity reasons
  150. timekoin_debug_msg("main", "checking foundation manager");
  151. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'foundation_heartbeat_active' LIMIT 1"),0,"field_data");
  152. // Check if script is already running
  153. if($script_loop_active == 0)
  154. {
  155. source_bg('foundation.php', TRUE);
  156. }
  157. sleep(1); // 1 second for sanity reasons
  158. timekoin_debug_msg("main", "checking generation manager");
  159. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,"field_data");
  160. // Check if script is already running
  161. if($script_loop_active == 0)
  162. {
  163. source_bg('generation.php');
  164. }
  165. sleep(1); // 1 second for sanity reasons
  166. timekoin_debug_msg("main", "checking treasurer processor");
  167. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'treasurer_heartbeat_active' LIMIT 1"),0,"field_data");
  168. // Check if script is already running
  169. if($script_loop_active == 0)
  170. {
  171. source_bg('treasurer.php');
  172. }
  173. sleep(2); // 1 second for sanity reasons
  174. timekoin_debug_msg("main", "checking peer list processor");
  175. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'peerlist_heartbeat_active' LIMIT 1"),0,"field_data");
  176. // Check if script is already running
  177. if($script_loop_active == 0)
  178. {
  179. source_bg('peerlist.php');
  180. }
  181. sleep(1); // 1 second for sanity reasons
  182. timekoin_debug_msg("main", "checking queue clerk");
  183. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"),0,"field_data");
  184. // Check if script is already running
  185. if($script_loop_active == 0 || $script_loop_active == 1)
  186. {
  187. source_bg('queueclerk.php');
  188. }
  189. sleep(2); // 1 second for sanity reasons
  190. timekoin_debug_msg("main", "checking genpeer manager");
  191. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'genpeer_heartbeat_active' LIMIT 1"),0,"field_data");
  192. // Check if script is already running
  193. if($script_loop_active == 0)
  194. {
  195. source_bg('genpeer.php');
  196. }
  197. sleep(1); // 1 second for sanity reasons
  198. timekoin_debug_msg("main", "checking watchdog");
  199. if(rand(1,3) == 2) // Randomize checking to keep database load down
  200. {
  201. // Check watchdog script to make sure it is still running
  202. $script_loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_heartbeat_active' LIMIT 1"),0,"field_data");
  203. $watchdog_last_heartbeat = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'watchdog_last_heartbeat' LIMIT 1"),0,"field_data");
  204. if($script_loop_active > 0)
  205. {
  206. // Watchdog should still be active
  207. if((time() - $watchdog_last_heartbeat) > 60) // Greater than double the loop time, something is wrong
  208. {
  209. // Watchdog stop was unexpected
  210. write_log("Watchdog is Stalled...", "MA");
  211. }
  212. }
  213. }
  214. //*****************************************************************************************************
  215. //*****************************************************************************************************
  216. // (Very Last Thing to do in Script)
  217. timekoin_debug_msg("main", "loop end");
  218. sleep(1);
  219. // Time to wake up and start again
  220. mysql_query("UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'main_last_heartbeat' LIMIT 1");
  221. // Check loop status...
  222. $loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'main_heartbeat_active' LIMIT 1"),0,"field_data");
  223. if($loop_active == 3) // Do a final check to make sure we shouldn't stop running instead
  224. {
  225. // Stop the loop and reset status back to 0
  226. mysql_query("UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
  227. exit;
  228. }
  229. else
  230. {
  231. mysql_query("UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'main_heartbeat_active' LIMIT 1");
  232. }
  233. } // Check if Active
  234. else
  235. {
  236. // Something is not working right, delay to avoid fast infinite loop
  237. if($datbase_error == TRUE && $database_error_counter < 6)
  238. {
  239. timekoin_debug_msg("main", "database connection error");
  240. // Wait 2 seconds for database to come back online
  241. sleep(2);
  242. }
  243. else
  244. {
  245. // Script was called improperly from somewhere or while it was already running, exit to avoid loop stacking
  246. exit;
  247. }
  248. }
  249. }
  250. // End Infinite Loop
  251. ?>