Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include 'customer.php';
- include 'databaseConfig.php';
- /**
- * Action addAsyncQuery.
- *
- * @param $name string customer name
- * @param $password string customer password
- * @param $database string database_name
- * @param $query string query to execute
- *
- * return string
- */
- function addAsyncQuery($name, $password, $database, $query)
- {
- //Connects to local database
- $con = mysqli_connect('localhost', 'root', '123456', 'wsdl');
- //Setting return messages
- $ok = 'Status ok';
- $not_ok = 'Error';
- $not_found = 'Not found';
- //Searches for customer
- $customer = getCustomer($con, $name);
- //Returns error if no customer found
- if (empty($customer)) {
- return $not_found;
- }
- //Authenticates the user and checks status
- if ($password != $customer['password'] || $customer['status'] == 0) {
- return 'Unauthorized access!';
- }
- $customer_id = $customer['id'];
- //Gets database information
- $customer_database = getDatabaseConfigsByCustomerData($con, $database, $customer_id);
- //Returns error if no database found
- if (empty($customer_database)) {
- return $not_found;
- }
- //Assigning database information
- $database_name = $customer_database[0]['database_name'];
- $database_host = $customer_database[0]['host'];
- $database_user = $customer_database[0]['user'];
- $database_password = $customer_database[0]['password'];
- $database_port = $customer_database[0]['port'];
- //Assigning params to add message to queue
- $queue = 'wsdl';
- $msg = 'Message sent';
- $headers = array('database_name' => $database_name, 'database_host' => $database_host, 'database_user' => $database_user, 'database_password' => $database_password, 'database_port' => $database_port, 'query' => $query);
- //Adds the message to the queue
- $stomp = new Stomp('tcp://localhost:61613');
- if ($stomp->send($queue, $msg, $headers)) {
- return $ok;
- }
- return $not_ok;
- }
- /**
- * Action executeQuery.
- *
- * @param $name string customer name
- * @param $password string customer password
- * @param $database string database_name
- * @param $query string query to execute
- *
- * return string
- */
- function executeQuery($name, $password, $database, $query)
- {
- //Connects to local database
- $con = mysqli_connect('localhost', 'root', '123456', 'wsdl');
- //Setting return messages
- $ok = 'Status ok';
- $not_ok = 'Error';
- $not_found = 'Not found';
- //Searches for customer
- $customer = getCustomer($con, $name);
- //Returns error if no customer found
- if (empty($customer)) {
- return $not_found;
- }
- //Authenticates the user and checks status
- if ($password != $customer['password'] || $customer['status'] == 0) {
- return 'Unauthorized access!';
- }
- $customer_id = $customer['id'];
- //Gets database information
- $customer_database = getDatabaseConfigsByCustomerData($con, $database, $customer_id);
- //Returns error if no database found
- if (empty($customer_database)) {
- return $not_found;
- }
- //Assigning database information
- $database_name = $customer_database[0]['database_name'];
- $database_host = $customer_database[0]['host'];
- $database_user = $customer_database[0]['user'];
- $database_password = $customer_database[0]['password'];
- $database_port = $customer_database[0]['port'];
- //Decodes the query
- $query_decoded = base64_decode($query);
- //Connects to the chosen database
- $user_con = mysqli_connect("$database_host:$database_port", "$database_user", "$database_password", "$database_name");
- //Returns error if couldn't connect
- if (mysqli_connect_errno()) {
- return 'Failed to connect to MySQL: '.mysqli_connect_error();
- }
- //Sends the query to the chosen database
- if (mysqli_multi_query($user_con, $query_decoded)) {
- mysqli_close($user_con);
- return $ok;
- } else {
- mysqli_close($user_con);
- return 'Error while executing the query';
- }
- return $not_ok;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement