Advertisement
Guest User

Untitled

a guest
May 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. #!/usr/bin/php5
  2. <?php
  3. $username = "db_username";
  4. $password = "db_password";
  5. $servername = "db_host";
  6. $dbname = "db_name";
  7.  
  8. // Create connection
  9. $conn = new mysqli($servername, $username, $password, $dbname);
  10. // Check connection
  11. if ($conn->connect_error) {
  12. echo json_encode(array(
  13. success => FALSE,
  14. decription => "connection failure",
  15. result => array(),
  16. was_debug => FALSE,
  17. ));
  18. exit();
  19. }
  20.  
  21. echo "Content-Type: text/json\n\n";
  22. $msg = stream_get_contents(STDIN);
  23.  
  24. $msg = json_decode($msg);
  25.  
  26. $arguments = json_decode($msg->arguments);
  27.  
  28. $debug_hash = 'kxjsio32@#ekmeXN223SMXSK2k323';
  29. $salt = 'powpl@#@qnqm123MWKEn2m3XXKL@2[w]q[<qq>';
  30.  
  31. $query = $msg->query;
  32.  
  33. if ($msg->groups) {
  34. foreach ($msg->groups as $name => $rtimes) {
  35. // Find group in query. There should only be
  36. // one group in the query.
  37. $spos = strpos($query, "$!" . $name . "$", 0);
  38. $epos = strpos($query, "$!" . $name . "/$", $spos);
  39. $spos_d = $spos + 2 + count($name) + 1;
  40. $epos_d = $epos;
  41. $epos = $epos + 2 + count($name) + 2;
  42. // Extract part and repeat it this many times.
  43. $part = substr($query, $spos_d, $epos_d - $spos_d);
  44. $query = substr_replace($query, str_repeat($part, $rtimes), $epos, $epos - $spos);
  45. }
  46. }
  47. // Only use $debug_hash for the shmac when we are debugging and
  48. // desire to have the hash returned. Otherwise, pass the
  49. // actual SHA512 hash as the shmac and it will be checked.
  50. //
  51. // The $debug_hash just makes implementing a test query easier or
  52. // making changes easier. Once code goes to production it should be
  53. // using the actual hash.
  54. //
  55. // By using the hash instead of the password as salt we can
  56. // prevent anyone from running arbitrary queries.
  57. $_shmac = hash("sha512", $query . $salt, FALSE);
  58. $was_debug = TRUE;
  59. if ($msg->shmac != $debug_hash) {
  60. $was_debug = FALSE;
  61. if ($msg->shmac != $_shmac) {
  62. // This is someone trying to guess the hash. They need
  63. // the salt to compute the hash but they should not have
  64. // it. The only value they should see if the actual SHA512
  65. // hashes unless you left the $salt from debugging.
  66. echo json_encode(array(
  67. success => FALSE,
  68. decription => "hash failure",
  69. result => array(),
  70. was_debug => FALSE,
  71. ));
  72. exit();
  73. }
  74. }
  75.  
  76. $stmt = $conn->prepare($msg->query);
  77.  
  78. $refl = new ReflectionClass('mysqli_stmt');
  79.  
  80. $args = array();
  81. if (count($arguments) > 0) {
  82. array_push($args, $msg->typecodes);
  83. foreach($arguments as $key => $val) {
  84. $args[] = &$arguments[$key];
  85. }
  86.  
  87. call_user_func_array(array($stmt, "bind_param"), $args);
  88. }
  89.  
  90. $stmt->execute();
  91. $stmt->store_result();
  92.  
  93. // Need to build a list of references to our $orow array so that
  94. // it can recieve the columns of each row. The fields are iterated
  95. // so that it is known what fields and all can be fetched.
  96. $oparams = array();
  97. $orow = array();
  98. $meta = $stmt->result_metadata();
  99. while ($field = $meta->fetch_field()) {
  100. array_push($oparams, &$orow[$field->name]);
  101. }
  102.  
  103. // A special function to call a method with arguments from an array.
  104. $method = call_user_func_array(array($stmt, "bind_result"), $oparams);
  105.  
  106. $out = array();
  107. while ($stmt->fetch()) {
  108. array_push($out, json_decode(json_encode($orow)));
  109. }
  110.  
  111. $stmt->close();
  112.  
  113. echo json_encode(array(
  114. success => TRUE,
  115. decription => "success",
  116. result => $out,
  117. // If this was a success then this is the same value that was provided
  118. // or the salt was given so this gives the actual hash to be used in
  119. // place of the salt value.
  120. shmac => $_shmac,
  121. was_debug => $was_debug,
  122. query => $msg->query,
  123. ));
  124.  
  125. $conn->close();
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement