Guest User

Untitled

a guest
Mar 16th, 2022
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. <?php
  2. /*
  3. */
  4.  
  5. /************************************CONFIG****************************************/
  6. //DATABSE DETAILS//
  7. $DB_ADDRESS="localhost";
  8. $DB_USER="id18579961_easyshoplist";
  9. $DB_PASS="aJR[rO(2d&~8Ldg$";
  10. $DB_NAME="id18579961_db_prodotti";
  11.  
  12. //SETTINGS//
  13. //This code is something you set in the APP so random people cant use it.
  14. $SQLKEY="12345";
  15.  
  16. /************************************CONFIG****************************************/
  17.  
  18. //these are just in case setting headers forcing it to always expire
  19. header('Cache-Control: no-cache, must-revalidate');
  20.  
  21. error_log(print_r($_POST,TRUE));
  22.  
  23. if( isset($_POST['query']) && isset($_POST['key']) ){ //checks if the tag post is there and if its been a proper form post
  24. //set content type to CSV (to be set here to be able to access this page also with a browser)
  25. header('Content-type: text/csv');
  26.  
  27. if($_POST['key']==$SQLKEY){ //validates the SQL key
  28. $query=urldecode($_POST['query']);
  29. if(get_magic_quotes_gpc()){ //check if the worthless pile of crap magic quotes is enabled and if it is, strip the slashes from the query
  30. $query=stripslashes($query);
  31. }
  32. $conn = new mysqli($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME); //connect
  33. mysqli_set_charset($conn, "utf8");
  34. if($conn->connect_error){ //checks connection
  35. header("HTTP/1.0 400 Bad Request");
  36. echo "ERROR Database Connection Failed: " . $conn->connect_error, E_USER_ERROR; //reports a DB connection failure
  37. } else {
  38. $result=$conn->query($query); //runs the posted query
  39. if($result === false){
  40. header("HTTP/1.0 400 Bad Request"); //sends back a bad request error
  41. echo "Wrong SQL: " . $query . " Error: " . $conn->error, E_USER_ERROR; //errors if the query is bad and spits the error back to the client
  42. } else {
  43. if (strlen(stristr($query,"SELECT"))>0) { //tests if it's a SELECT statement
  44. $csv = ''; // bug fix Undefined variable: csv
  45. while ($fieldinfo = $result->fetch_field()) {
  46. $csv .= $fieldinfo->name.",";
  47. }
  48. $csv = rtrim($csv, ",")."\n";
  49. echo $csv; //prints header row
  50. $csv = '';
  51.  
  52. $result->data_seek(0);
  53. while($row = $result->fetch_assoc()){
  54. foreach ($row as $key => $value) {
  55. $csv .= $value.",";
  56. }
  57. $csv = rtrim($csv, ",")."\n";
  58. }
  59. echo $csv; //prints all data rows
  60. } else {
  61. header("HTTP/1.0 201 Rows");
  62. echo "AFFECTED ROWS: " . $conn->affected_rows; //if the query is anything but a SELECT, it will return the number of affected rows
  63. }
  64. }
  65. $conn->close(); //closes the DB
  66. }
  67. } else {
  68. header("HTTP/1.0 400 Bad Request");
  69. echo "Bad Request"; //reports if the secret key was bad
  70. }
  71. } else {
  72. header("HTTP/1.0 400 Bad Request");
  73. echo "Bad Request";
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment