RecordManager.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. class RecordManager{
  3. //Constructor just creates the RecordFile.data.php
  4. function RecordManager(){
  5. if(!file_exists(RECORD_FILE)){
  6. $fp = fopen(RECORD_FILE, "w");
  7. fclose($fp);
  8. }
  9. }
  10. //Called everytime you run DirectoryServer.php to expire record values.
  11. function expireRecords(){
  12. //IMPORTANT
  13. //Include the var_export of the variable $records
  14. include("RecordFile.data.php");
  15. if(!empty($records)){
  16. foreach($records as $index => $game){
  17. $secs = time() - $game["__SEC_AFTER_EPOCH_SINCE_LAST_UPDATE"];
  18. if($secs > TIMEOUT){
  19. unset($records[$index]);
  20. }
  21. }
  22. //Save the file
  23. $fp = fopen(RECORD_FILE, "w");
  24. fwrite($fp, '<?php $records = ');
  25. fwrite($fp, var_export($records, true));
  26. fwrite($fp, '; ?>');
  27. fclose($fp);
  28. }
  29. }
  30. function uploadRecords($post_body){
  31. //IMPORTANT
  32. //Include the var_export of the variable $records
  33. include("RecordFile.data.php");
  34. $output = "";
  35. $post_array = array();
  36. //Modified code to handle multiple rows separated by \002
  37. $post_body_rows = explode("\002", $post_body);
  38. foreach($post_body_rows as $row_num => $post_body_row){
  39. //row columns and values are separated by \001
  40. //Even indexes are columns, the following odd value is the column_value
  41. $post_body_array = explode("\001", $post_body_row);
  42. foreach($post_body_array as $index => $post_body_item){
  43. if($index % 2 == 0){
  44. $post_array[$row_num][$post_body_item] = $post_body_array[$index+1];
  45. }
  46. }
  47. }
  48. //This places each value pair into a PHP array to be outputted to the Recordfile
  49. foreach($post_array as $post_item){
  50. if(isset($post_item["__GAME_PORT"]) && isset($post_item["__GAME_NAME"]) ){
  51. $record = array();
  52. foreach($post_item as $key => $value){
  53. //Decode because post values are send with url encoded symbols like %20
  54. $record[$key] = rawurldecode($value);
  55. }
  56. //Store the IP address if not included in the POST
  57. if(!isset($record["__System_Address"])){
  58. $record["__System_Address"] = $_SERVER["REMOTE_ADDR"];
  59. }
  60. $record["__SEC_AFTER_EPOCH_SINCE_LAST_UPDATE"] = time();
  61. //Search the $records for a matching record. If the GAME_PORT, GAME_NAME, and System Address match replace it
  62. //Otherwise just add the record to the $records array.
  63. $record_found = false;
  64. if(!empty($records)){
  65. foreach($records as $index => $save_record){
  66. if($save_record["__GAME_PORT"] == $record["__GAME_PORT"]
  67. && $save_record["__GAME_NAME"] == $record["__GAME_NAME"]
  68. && $save_record["__System_Address"] == $record["__System_Address"]){
  69. //We found the record so replace it here.
  70. $records[$index] = $record;
  71. $record_found = true;
  72. break;
  73. }
  74. }
  75. }
  76. //Record couldn't be found simply add a new record
  77. if(!$record_found){
  78. $records[] = $record;
  79. }
  80. //Save the file
  81. $fp = fopen(RECORD_FILE, "w");
  82. fwrite($fp, '<?php $records = ');
  83. fwrite($fp, var_export($records, true));
  84. fwrite($fp, '; ?>');
  85. fclose($fp);
  86. }
  87. else{
  88. $output .= "\003".microtime(true)."\003";
  89. $output .= "__GAME_PORT and __GAME_NAME must be provided";
  90. }
  91. }
  92. return $output;
  93. }
  94. function downloadRecords(){
  95. //IMPORTANT
  96. //Include the var_export of the variable $records
  97. include("RecordFile.data.php");
  98. //Comment prefix
  99. $output = "\003".microtime(true)."\003";
  100. //Output the records. Traverse and output rows separated by \002 and values seperated by \001
  101. if(!empty($records)){
  102. $row_count = 0;
  103. foreach($records as $game){
  104. if(!empty($game)){
  105. if($row_count > 0){
  106. $output .= "\002";
  107. }
  108. $count = 0;
  109. foreach($game as $key => $value){
  110. if($count > 0){
  111. $output .= "\001";
  112. }
  113. $output .= "$key"."\001"."$value";
  114. $count++;
  115. }
  116. }
  117. $row_count++;
  118. }
  119. }
  120. return $output;
  121. }
  122. function generateCss(){
  123. $output = "<html>
  124. <head>
  125. <style type='text/css' media='all'>
  126. .game{
  127. border:1px solid #1E3C64;
  128. background:#4A8AD1;
  129. padding:5px 10px;
  130. color:#EFF7FF;
  131. margin-bottom:5px;
  132. }
  133. </style>
  134. </head>
  135. <body>";
  136. return $output;
  137. }
  138. function viewRecords(){
  139. $output = "";
  140. //IMPORTANT
  141. //Include the var_export of the variable $records
  142. include("RecordFile.data.php");
  143. if(!empty($records)){
  144. foreach($records as $game){
  145. $vars = "";
  146. $secs = $game["__SEC_AFTER_EPOCH_SINCE_LAST_UPDATE"] + TIMEOUT - time();
  147. $output .= "<div class='game' >
  148. <p>Game Port: {$game["__GAME_PORT"]}</p>
  149. <p>Game Name: {$game["__GAME_NAME"]}</p>
  150. <p>System Address: {$game["__System_Address"]}</p>
  151. <p>Time Until Expiration: $secs secs</p>
  152. ";
  153. foreach($game as $key => $value){
  154. if($key != "__GAME_PORT" && $key != "__GAME_NAME"
  155. && $key != "__System_Address" && $key != "__SEC_AFTER_EPOCH_SINCE_LAST_UPDATE" ){
  156. $vars .= "<li>".$key.": $value</li>";
  157. }
  158. elseif($key == "0"){
  159. $vars .= "<li>".$key.": $value</li>";
  160. }
  161. }
  162. if(!empty($vars)){
  163. $output .= "<ul>$vars</ul>";
  164. }
  165. $output .= "</div>";
  166. }
  167. }
  168. else{
  169. $output .= "<div class='game' >
  170. <p>Record Table is Empty.</p>
  171. </div>";
  172. }
  173. $output .= "</body></html>";
  174. return $output;
  175. }
  176. }
  177. ?>
粤ICP备19079148号