sync.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // Gasteizko Margolariak API v3 //
  3. // $_GET and $_POST valid parameters
  4. define('POST_USER', 'user');
  5. define('POST_PASS', 'pass');
  6. define('GET_TABLES', 'tables');
  7. define('GET_WHERE', 'where');
  8. // Error messages
  9. define('ERR_USER', '-USER:');
  10. define('ERR_TABLES', '-TABLES:');
  11. define('ERR_WHERE', '-WHERE:');
  12. include('functions.php');
  13. $con = startdb();
  14. // Error control
  15. $error = "";
  16. // Get params
  17. $user = mysqli_real_escape_string($con, $_POST[POST_USER]);
  18. $pass = mysqli_real_escape_string($con, $_POST[POST_PASS]);
  19. $tables = mysqli_real_escape_string($con, $_GET[GET_TABLES]);
  20. $where = mysqli_real_escape_string($con, $_GET[GET_WHERE]);
  21. //Validate user
  22. $uid = login($con, $user, $pass);
  23. if ($uid == -1){
  24. error_log(":SECURITY: Reporting location with wrong credentials (IP $_SERVER[REMOTE_ADDR])");
  25. $error = $error . ERR_USER . mysqli_real_escape_string($con, $_POST[GET_USER]);
  26. error_log($error);
  27. http_response_code(403); // Forbidden
  28. exit(-1);
  29. }
  30. // Ask server to create a SQLdump
  31. $date = date_create();
  32. $fname = date_timestamp_get($date);
  33. include('../../../www/.htpasswd');
  34. exec("mysqldump --single-transaction gm -u $username_ro -p'$pass_ro' > /var/www-dump/$fname.sql");
  35. // Ask server to encrypt the dump.
  36. $pass = mysqli_real_escape_string($con, $_POST[POST_USER]);
  37. exec("gpg --batch --yes --passphrase $pass -o /var/www-admin/dump/$fname.gpg -c /var/www-dump/$fname.sql");
  38. // Make a redirect.
  39. header("Location: /dump/$fname.gpg");
  40. ?>