Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. <?php
  2. include 'customer.php';
  3. include 'databaseConfig.php';
  4.  
  5. /**
  6. * Action addAsyncQuery.
  7. *
  8. * @param $name string customer name
  9. * @param $password string customer password
  10. * @param $database string database_name
  11. * @param $query string query to execute
  12. *
  13. * return string
  14. */
  15. function addAsyncQuery($name, $password, $database, $query)
  16. {
  17. //Connects to local database
  18. $con = mysqli_connect('localhost', 'root', '123456', 'wsdl');
  19.  
  20. //Setting return messages
  21. $ok = 'Status ok';
  22. $not_ok = 'Error';
  23. $not_found = 'Not found';
  24.  
  25. //Searches for customer
  26. $customer = getCustomer($con, $name);
  27.  
  28. //Returns error if no customer found
  29. if (empty($customer)) {
  30. return $not_found;
  31. }
  32.  
  33. //Authenticates the user and checks status
  34. if ($password != $customer['password'] || $customer['status'] == 0) {
  35. return 'Unauthorized access!';
  36. }
  37.  
  38. $customer_id = $customer['id'];
  39.  
  40. //Gets database information
  41. $customer_database = getDatabaseConfigsByCustomerData($con, $database, $customer_id);
  42.  
  43. //Returns error if no database found
  44. if (empty($customer_database)) {
  45. return $not_found;
  46. }
  47.  
  48. //Assigning database information
  49. $database_name = $customer_database[0]['database_name'];
  50. $database_host = $customer_database[0]['host'];
  51. $database_user = $customer_database[0]['user'];
  52. $database_password = $customer_database[0]['password'];
  53. $database_port = $customer_database[0]['port'];
  54.  
  55. //Assigning params to add message to queue
  56. $queue = 'wsdl';
  57. $msg = 'Message sent';
  58. $headers = array('database_name' => $database_name, 'database_host' => $database_host, 'database_user' => $database_user, 'database_password' => $database_password, 'database_port' => $database_port, 'query' => $query);
  59.  
  60. //Adds the message to the queue
  61. $stomp = new Stomp('tcp://localhost:61613');
  62. if ($stomp->send($queue, $msg, $headers)) {
  63. return $ok;
  64. }
  65.  
  66. return $not_ok;
  67. }
  68.  
  69. /**
  70. * Action executeQuery.
  71. *
  72. * @param $name string customer name
  73. * @param $password string customer password
  74. * @param $database string database_name
  75. * @param $query string query to execute
  76. *
  77. * return string
  78. */
  79. function executeQuery($name, $password, $database, $query)
  80. {
  81.  
  82. //Connects to local database
  83. $con = mysqli_connect('localhost', 'root', '123456', 'wsdl');
  84.  
  85. //Setting return messages
  86. $ok = 'Status ok';
  87. $not_ok = 'Error';
  88. $not_found = 'Not found';
  89.  
  90. //Searches for customer
  91. $customer = getCustomer($con, $name);
  92.  
  93. //Returns error if no customer found
  94. if (empty($customer)) {
  95. return $not_found;
  96. }
  97.  
  98. //Authenticates the user and checks status
  99. if ($password != $customer['password'] || $customer['status'] == 0) {
  100. return 'Unauthorized access!';
  101. }
  102.  
  103. $customer_id = $customer['id'];
  104.  
  105. //Gets database information
  106. $customer_database = getDatabaseConfigsByCustomerData($con, $database, $customer_id);
  107.  
  108. //Returns error if no database found
  109. if (empty($customer_database)) {
  110. return $not_found;
  111. }
  112.  
  113. //Assigning database information
  114. $database_name = $customer_database[0]['database_name'];
  115. $database_host = $customer_database[0]['host'];
  116. $database_user = $customer_database[0]['user'];
  117. $database_password = $customer_database[0]['password'];
  118. $database_port = $customer_database[0]['port'];
  119.  
  120. //Decodes the query
  121. $query_decoded = base64_decode($query);
  122.  
  123. //Connects to the chosen database
  124. $user_con = mysqli_connect("$database_host:$database_port", "$database_user", "$database_password", "$database_name");
  125.  
  126. //Returns error if couldn't connect
  127. if (mysqli_connect_errno()) {
  128. return 'Failed to connect to MySQL: '.mysqli_connect_error();
  129. }
  130.  
  131. //Sends the query to the chosen database
  132. if (mysqli_multi_query($user_con, $query_decoded)) {
  133. mysqli_close($user_con);
  134.  
  135. return $ok;
  136. } else {
  137. mysqli_close($user_con);
  138.  
  139. return 'Error while executing the query';
  140. }
  141.  
  142. return $not_ok;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement