Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Description: This is where the email address given will be posted once the user has filled it in on the newsletter widget
  4.  */
  5.  
  6. // Include file with auth vars
  7. require_once('../../../wp-config.php');
  8.  
  9. // Define variables
  10. $useremail = "";
  11. $apiemail = get_option('apiemail');
  12. $apipassword = get_option('apipassword');
  13. $listID = get_option('listID');
  14. $xml_string = "";
  15. $input_unsuccessful_slug = get_option('slugUnsuccess');
  16. $input_successful_slug = get_option('slugSuccess');
  17.  
  18. // Tidies slug slashes
  19. $unsuccessful_slug = str_replace('/', '', $input_unsuccessful_slug);
  20. $successful_slug = str_replace('/', '', $input_successful_slug);
  21.  
  22. // Create un/success URLs
  23. $success_url = site_url() . '/' . $successful_slug;
  24. $unsuccess_url = site_url() . '/' . $unsuccessful_slug;
  25.  
  26. // Test for validation
  27. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  28.   $useremail = test_input($_POST["useremail"]);
  29. }
  30.  
  31. // Tidy input
  32. function test_input($data) {
  33.   $data = trim($data);
  34.   $data = stripslashes($data);
  35.   $data = htmlspecialchars($data);
  36.   return $data;
  37. }
  38.  
  39. // Sanitise email
  40. $clean_useremail = filter_var($useremail, FILTER_SANITIZE_EMAIL);
  41.  
  42. // Build URL
  43. $auth_url = 'https://api.dotmailer.com/v2/address-books/' . $listID .'/contacts';
  44.  
  45. // Create the XML data to be added
  46. $xml = array(
  47.     'id' => 'generic',
  48.     'email' => $clean_useremail,
  49.     'optInType' => 'Unknown',
  50.     'emailType' => 'Html',
  51.     'dataFields' => ':null',
  52.     'status' => 'Subscribed'
  53. );
  54.  
  55. // JSON
  56. $xml_string = json_encode($xml);  
  57.  
  58. // The request
  59. $ch = curl_init();
  60. curl_setopt($ch, CURLOPT_URL,$auth_url);
  61. curl_setopt($ch,CURLOPT_POST, count($xml));
  62. curl_setopt($ch,CURLOPT_POSTFIELDS, $xml_string);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  64. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  65. curl_setopt($ch, CURLOPT_USERPWD, "$apiemail:$apipassword");
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
  67.     'Content-Type: application/json',                                                                                
  68.     'Content-Length: ' . strlen($xml_string))                                                                      
  69. );
  70. $result = curl_exec ($ch);
  71. $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
  72. curl_close ($ch);
  73.  
  74. // User feedback
  75. if (isset($status_code)) {
  76.     if ($status_code == '200'){
  77.         header('Location:' . $success_url);
  78.     }
  79.     else if ($status_code =='400'){
  80.         header('Location:' . $unsuccess_url);
  81.     }
  82.     else{
  83.         echo 'Submission success undetermined. HTTP response = ' . $status_code . '<br />Please contact the site administrator';
  84.     }
  85. };
  86.  
  87.  
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement