Form.class.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. class AC_Form extends ActiveCampaign {
  3. public $version;
  4. public $url_base;
  5. public $url;
  6. public $api_key;
  7. function __construct($version, $url_base, $url, $api_key) {
  8. $this->version = $version;
  9. $this->url_base = $url_base;
  10. $this->url = $url;
  11. $this->api_key = $api_key;
  12. }
  13. function getforms($params) {
  14. $request_url = "{$this->url}&api_action=form_getforms&api_output={$this->output}";
  15. $response = $this->curl($request_url);
  16. return $response;
  17. }
  18. function html($params) {
  19. $request_url = "{$this->url}&api_action=form_html&api_output={$this->output}&{$params}";
  20. $response = $this->curl($request_url);
  21. return $response;
  22. }
  23. function embed($params) {
  24. $params_array = explode("&", $params);
  25. $params_ = array();
  26. foreach ($params_array as $expression) {
  27. // IE: css=1
  28. list($var, $val) = explode("=", $expression);
  29. $params_[$var] = $val;
  30. }
  31. $id = (isset($params_["id"])) ? (int)$params_["id"] : 0;
  32. $css = (isset($params_["css"])) ? (int)$params_["css"] : 1;
  33. $ajax = (isset($params_["ajax"])) ? (int)$params_["ajax"] : 0;
  34. // to set the current page as the action, pass "action=", or "action=[THIS URL]"
  35. $action = (isset($params_["action"])) ? ($params_["action"] ? $params_["action"] : "this") : "";
  36. $html = $this->html("id={$id}");
  37. if (is_object($html) && !(int)$html->success) {
  38. return $html->error;
  39. }
  40. if ($html) {
  41. if ($action) {
  42. if ($action != "this") {
  43. // replace the action attribute with the one provided
  44. $action_val = urldecode($action);
  45. $html = preg_replace("/action=['\"][^'\"]+['\"]/", "action='{$action_val}'", $html);
  46. }
  47. else {
  48. $action_val = "";
  49. }
  50. }
  51. else {
  52. // find the action attribute value (URL)
  53. // should be the proc.php URL (at this point in the script)
  54. $action_val = preg_match("/action=['\"][^'\"]+['\"]/", $html, $m);
  55. $action_val = $m[0];
  56. $action_val = substr($action_val, 8, strlen($action_val) - 9);
  57. }
  58. if (!$css) {
  59. // remove all CSS
  60. $html = preg_replace("/<style[^>]*>(.*)<\/style>/s", "", $html);
  61. }
  62. if (!$ajax) {
  63. // replace the Submit button to be an actual submit type
  64. $html = preg_replace("/input type='button'/", "input type='submit'", $html);
  65. // if action = "this", remove the action attribute completely
  66. if (!$action_val) {
  67. $html = preg_replace("/action=['\"][^'\"]+['\"]/", "", $html);
  68. }
  69. }
  70. else {
  71. // if using Ajax, remove the <form> action attribute completely
  72. $html = preg_replace("/action=['\"][^'\"]+['\"]/", "", $html);
  73. // replace the Submit button to be a button type (for ajax).
  74. // forms come out of AC now with a "submit" button (it used to be "button").
  75. $html = preg_replace("/input type='submit'/", "input type='button'", $html);
  76. $action_val = urldecode($action_val);
  77. // add jQuery stuff
  78. $extra = "<script type='text/javascript'>
  79. var \$j = jQuery.noConflict();
  80. \$j(document).ready(function() {
  81. \$j('#_form_{$id} input[type*=\"button\"]').click(function() {
  82. // rename the radio options for Subscribe/Unsubscribe, since they conflict with the hidden field.
  83. \$j('input[type=radio][name=act]').attr('name','act_radio');
  84. var form_data = {};
  85. \$j('#_form_{$id}').each(function() {
  86. form_data = \$j(this).serialize();
  87. });
  88. var geturl;
  89. geturl = \$j.ajax({
  90. url: '{$action_val}',
  91. type: 'POST',
  92. dataType: 'json',
  93. data: form_data,
  94. error: function(jqXHR, textStatus, errorThrown) {
  95. console.log(errorThrown);
  96. },
  97. success: function(data) {
  98. \$j('#form_result_message').html(data.message);
  99. var result_class = (data.success) ? 'form_result_success' : 'form_result_error';
  100. \$j('#form_result_message').removeClass('form_result_success form_result_error').addClass(result_class);
  101. }
  102. });
  103. });
  104. });
  105. </script>";
  106. $html = $html . $extra;
  107. }
  108. }
  109. return $html;
  110. }
  111. function process($params) {
  112. $r = array();
  113. if ($_SERVER["REQUEST_METHOD"] != "POST") return $r;
  114. $sync = 0;
  115. $captcha_in_form = 0;
  116. if ($params) {
  117. $params_array = explode("&", $params);
  118. $params_ = array();
  119. foreach ($params_array as $expression) {
  120. // IE: css=1
  121. list($var, $val) = explode("=", $expression);
  122. $params_[$var] = $val;
  123. }
  124. $sync = (isset($params_["sync"])) ? (int)$params_["sync"] : 0;
  125. $captcha_in_form = (isset($params_["captcha"])) ? (int)$params_["captcha"] : 0;
  126. }
  127. $formid = $_POST["f"];
  128. // sub or unsub
  129. $act = isset($_POST["act"]) ? $_POST["act"] : "sub";
  130. if (isset($_POST["act_radio"])) {
  131. // the radio options for Subscribe/Unsubscribe.
  132. $act = $_POST["act_radio"];
  133. }
  134. $email = $_POST["email"];
  135. $phone = isset($_POST["phone"]) ? $_POST["phone"] : "";
  136. $lists = (isset($_POST["nlbox"]) && $_POST["nlbox"]) ? $_POST["nlbox"] : array();
  137. if ($captcha_in_form) {
  138. // Captcha is part of the form.
  139. // Get the captcha value the user entered.
  140. $captcha = "";
  141. if (isset($_POST["captcha"])) {
  142. $captcha = md5(strtoupper((string)$_POST["captcha"]));
  143. }
  144. if (!isset($_SESSION["image_random_value"]) || !isset($_SESSION["image_random_value"][$captcha])) {
  145. return json_encode(array("success" => 0, "message" => "Invalid captcha"));
  146. }
  147. }
  148. if (isset($_POST["fullname"])) {
  149. $fullname = explode(" ", $_POST["fullname"]);
  150. $firstname = array_shift($fullname);
  151. $lastname = implode(" ", $fullname);
  152. }
  153. else {
  154. $firstname = trim($_POST["firstname"]);
  155. $lastname = trim($_POST["lastname"]);
  156. if ($firstname == "" && isset($_POST["first_name"])) $firstname = trim($_POST["first_name"]);
  157. if ($lastname == "" && isset($_POST["last_name"])) $lastname = trim($_POST["last_name"]);
  158. }
  159. $fields = (isset($_POST["field"])) ? $_POST["field"] : array();
  160. $contact = array(
  161. "form" => $formid,
  162. "email" => $email,
  163. "first_name" => $firstname,
  164. "last_name" => $lastname,
  165. "phone" => $phone,
  166. );
  167. foreach ($fields as $ac_field_id => $field_value) {
  168. $contact["field"][$ac_field_id . ",0"] = $field_value;
  169. }
  170. // add lists
  171. $status = ($act == "unsub") ? 2 : 1;
  172. foreach ($lists as $listid) {
  173. $contact["p[{$listid}]"] = $listid;
  174. $contact["status[{$listid}]"] = $status;
  175. }
  176. if (!$sync) {
  177. // do add/edit
  178. $contact_exists = $this->api("contact/view?email={$email}", $contact);
  179. if ( !isset($contact_exists->id) ) {
  180. // contact does not exist - add them
  181. $contact_request = $this->api("contact/add", $contact);
  182. if ((int)$contact_request->success) {
  183. // successful request
  184. $contact_id = (int)$contact_request->subscriber_id;
  185. $r = array(
  186. "success" => 1,
  187. "message" => $contact_request->result_message,
  188. "contact_id" => $contact_id,
  189. );
  190. }
  191. else {
  192. // request failed
  193. $r = array(
  194. "success" => 0,
  195. "message" => $contact_request->error,
  196. );
  197. }
  198. }
  199. else {
  200. // contact already exists - edit them
  201. $contact_id = $contact_exists->id;
  202. $contact["id"] = $contact_id;
  203. $contact_request = $this->api("contact/edit?overwrite=0", $contact);
  204. }
  205. }
  206. else {
  207. // perform sync (add or edit)
  208. $contact_request = $this->api("contact/sync", $contact);
  209. }
  210. if ((int)$contact_request->success) {
  211. // successful request
  212. //$contact_id = (int)$contact_request->contact_id;
  213. $r = array(
  214. "success" => 1,
  215. "message" => $contact_request->result_message,
  216. //"contact_id" => $contact_id,
  217. );
  218. }
  219. else {
  220. // request failed
  221. $r = array(
  222. "success" => 0,
  223. "message" => $contact_request->error,
  224. );
  225. }
  226. return json_encode($r);
  227. }
  228. }
  229. ?>