Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. file1.php
  2. require_once(ABSPATH.'wp-config.php' );
  3. require_once(ABSPATH.'functions.php' );
  4.  
  5. $domain = $_SERVER['HTTP_HOST'];
  6. $uri = parse_url($_SERVER['HTTP_REFERER']);
  7. $r_domain = $uri['host'];
  8.  
  9. add_action( 'admin_post_table', 'table' );
  10. function table() {
  11. if( ! check_ajax_referer( 'nonce', 'securitycheck' ) ) {
  12. wp_send_json_error('Security check failed');
  13. }
  14. if( ! empty($_POST['submission']) ) {
  15. wp_send_json_error('Security check failed, regular form entries only!');
  16. }
  17. if ( $domain == $r_domain ) {
  18. $conn = f_sqlConnect();
  19. if ( !f_tableExists($conn, $table) ) {
  20. try {
  21. $sql = "CREATE TABLE $table (
  22. ID int NOT NULL AUTO_INCREMENT,
  23. PRIMARY KEY(ID),
  24. ip int NOT NULL
  25. )";
  26.  
  27. $result = $conn->exec($sql);
  28. } catch(PDOException $e) {
  29. echo $e->getMessage(); //Remove or change message in production code
  30. echo "Create table error";
  31. }
  32. }
  33.  
  34. functions.php
  35. /*Connects to and selects the specified database with the specified user.*/
  36. function f_sqlConnect() {
  37. $host = DB_HOST;
  38. $db = DB_NAME;
  39. try {
  40. $conn = new PDO("mysql:host=$host;dbname=$db", DB_USER, DB_PASSWORD);
  41. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42. echo "Connected successfully";
  43. }
  44. Catch(PDOException $e)
  45. {
  46. echo "Connection failed: " . $e->getMessage();
  47. }
  48. }
  49.  
  50. /*Checks to see if the specified table exists and if it doesn't, creates it.*/
  51. function f_tableExists($conn, $table) {
  52. //$table = preg_replace('/[^da-z_]/i', '', $table);
  53. // Try a select statement against the table
  54. // Run it in try/catch in case PDO is in ERRMODE_EXCEPTION.
  55. try {
  56. $query = "SELECT 1 FROM $table LIMIT 1";
  57. $result = $conn->exec($query);
  58.  
  59. } catch (Exception $e) {
  60. echo "We got an exception == table not found";
  61. return FALSE;
  62. }
  63. echo "Result is either boolean FALSE (no table found) or PDOStatement Object (table found)";
  64. return $result !== FALSE;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement