MadMimi.class.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. Mad Mimi for PHP
  4. v2.0.3 - Cleaner, faster, and much easier to use and extend. (In my opinion!)
  5. For release notes, see the README that should have been included.
  6. _______________________________________
  7. Copyright (C) 2010 Mad Mimi LLC
  8. Authored by Nicholas Young <nicholas@madmimi.com> ...and a host of contributors.
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. */
  25. if (!class_exists('Spyc')) {
  26. require("Spyc.class.php");
  27. }
  28. if (!function_exists('curl_init')) {
  29. die('Mad Mimi for PHP requires the PHP cURL extension.');
  30. }
  31. class MadMimi {
  32. function __construct($email, $api_key, $debug = false) {
  33. $this->username = $email;
  34. $this->api_key = $api_key;
  35. $this->debug = $debug;
  36. }
  37. function default_options() {
  38. return array('username' => $this->username, 'api_key' => $this->api_key);
  39. }
  40. function DoRequest($path, $options, $return_status = false, $method = 'GET') {
  41. if ($method == 'GET') {
  42. $request_options = "?";
  43. } else {
  44. $request_options = "";
  45. }
  46. $request_options .= http_build_query($options);
  47. $url = "https://api.madmimi.com{$path}";
  48. if ($method == 'GET') {
  49. $url .= $request_options;
  50. }
  51. $ch = curl_init();
  52. curl_setopt($ch, CURLOPT_URL, $url);
  53. // Fix libcurl vs. apache2
  54. curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:"));
  55. if ($return_status == true) {
  56. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  57. } else {
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  59. }
  60. switch($method) {
  61. case 'GET':
  62. break;
  63. case 'POST':
  64. curl_setopt($ch, CURLOPT_POST, TRUE);
  65. curl_setopt($ch, CURLOPT_POSTFIELDS, $request_options);
  66. break;
  67. }
  68. if ($this->debug == true) {
  69. echo "URL: {$url}<br />";
  70. if ($method == 'POST') {
  71. echo "Request Options: {$request_options}";
  72. }
  73. } else {
  74. $result = curl_exec($ch);
  75. if( $result === false){
  76. $error = curl_error($ch);
  77. echo 'Curl error: ', $error, "\n";
  78. die($error);
  79. }
  80. }
  81. curl_close($ch);
  82. if ($this->debug == false) {
  83. return $result;
  84. }
  85. }
  86. // This took years off my life. Seriously. -lkw
  87. function escape_for_csv($s) {
  88. // Watch out! We may have quotes! So quote them.
  89. $s = str_replace('"', '""', $s);
  90. if(preg_match('/,/', $s) || preg_match('/"/', $s) || preg_match("/\n/", $s)) {
  91. // Quote the whole thing b/c we have a newline, comma or quote.
  92. return '"'.$s.'"';
  93. } else {
  94. // False alarm. We're good.
  95. return $s;
  96. }
  97. }
  98. function build_csv($arr) {
  99. $csv = "";
  100. $keys = array_keys($arr);
  101. foreach ($keys as $key => $value) {
  102. $value = $this->escape_for_csv($value);
  103. $csv .= $value . ",";
  104. }
  105. $csv = substr($csv, 0, -1);
  106. $csv .= "\n";
  107. foreach ($arr as $key => $value) {
  108. $value = $this->escape_for_csv($value);
  109. $csv .= $value . ",";
  110. }
  111. $csv = substr($csv, 0, -1);
  112. $csv .= "\n";
  113. return $csv;
  114. }
  115. function Import($csv_data, $return = false) {
  116. $options = array('csv_file' => $csv_data) + $this->default_options();
  117. $request = $this->DoRequest('/audience_members', $options, $return, 'POST');
  118. return $request;
  119. }
  120. function Lists($return = false) {
  121. $request = $this->DoRequest('/audience_lists/lists.xml', $this->default_options(), $return);
  122. return $request;
  123. }
  124. function AddUser($user, $return = false) {
  125. $csv = $this->build_csv($user);
  126. return $this->Import($csv, $return);
  127. }
  128. function RemoveUser($email, $list_name, $return = false) {
  129. $options = array('email' => $email) + $this->default_options();
  130. $request = $this->DoRequest('/audience_lists/' . rawurlencode($list_name) . "/remove", $options, $return, 'POST');
  131. return $request;
  132. }
  133. function Memberships($email, $return = false) {
  134. $url = str_replace('%email%', $email, '/audience_members/%email%/lists.xml');
  135. $request = $this->DoRequest($url, $this->default_options(), $return);
  136. return $request;
  137. }
  138. function NewList($list_name, $return = false) {
  139. $options = array('name' => $list_name) + $this->default_options();
  140. $request = $this->DoRequest('/audience_lists', $options, $return, 'POST');
  141. return $request;
  142. }
  143. function DeleteList($list_name, $return = false) {
  144. $options = array('_method' => 'delete') + $this->default_options();
  145. $request = $this->DoRequest('/audience_lists/' . rawurlencode($list_name), $options, $return, 'POST');
  146. return $request;
  147. }
  148. function SendMessage($options, $yaml_body = null, $return = false) {
  149. if (class_exists('Spyc') && $yaml_body != null) {
  150. $options['body'] = Spyc::YAMLDump($yaml_body);
  151. }
  152. $options = $options + $this->default_options();
  153. if (isset($options['list_name'])) {
  154. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST');
  155. } else {
  156. $request = $this->DoRequest('/mailer', $options, $return, 'POST');
  157. }
  158. return $request;
  159. }
  160. function SendHTML($options, $html, $return = false) {
  161. if ((!strstr($html, '[[tracking_beacon]]')) && (!strstr($html, '[[peek_image]]'))) {
  162. die('Please include either the [[tracking_beacon]] or the [[peek_image]] macro in your HTML.');
  163. }
  164. $options = $options + $this->default_options();
  165. $options['raw_html'] = $html;
  166. if (isset($options['list_name'])) {
  167. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST');
  168. } else {
  169. $request = $this->DoRequest('/mailer', $options, $return, 'POST');
  170. }
  171. return $request;
  172. }
  173. function SendPlainText($options, $message, $return = false) {
  174. if (!strstr($message, '[[unsubscribe]]')) {
  175. die('Please include the [[unsubscribe]] macro in your text.');
  176. }
  177. $options = $options + $this->default_options();
  178. $options['raw_plain_text'] = $message;
  179. if (isset($options['list_name'])) {
  180. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST');
  181. } else {
  182. $request = $this->DoRequest('/mailer', $options, $return, 'POST');
  183. }
  184. return $request;
  185. }
  186. function SuppressedSince($unix_timestamp, $return = false) {
  187. $request = $this->DoRequest('/audience_members/suppressed_since/' . $unix_timestamp . '.txt', $this->default_options(), $return);
  188. return $request;
  189. }
  190. function Promotions($page = 1, $return = false) {
  191. $options = array('page' => $page) + $this->default_options();
  192. $request = $this->DoRequest('/promotions.xml', $options, $return);
  193. return $request;
  194. }
  195. function MailingStats($promotion_id, $mailing_id, $return = false) {
  196. $url = str_replace("%promotion_id%", $promotion_id, "/promotions/%promotion_id%/mailings/%mailing_id%.xml");
  197. $url = str_replace("%mailing_id%", $mailing_id, $url);
  198. $request = $this->DoRequest($url, $this->default_options(), $return);
  199. return $request;
  200. }
  201. function Search($query_string, $raw = false, $return = false) {
  202. $options = array('query' => $query_string, 'raw' => $raw) + $this->default_options();
  203. $request = $this->DoRequest('/audience_members/search.xml', $options, $return);
  204. return $request;
  205. }
  206. function Events($unix_timestamp, $return = false) {
  207. $request = $this->DoRequest('/audience_members/events_since/' . $unix_timestamp . '.xml', $this->default_options(), $return);
  208. return $request;
  209. }
  210. function Status($transaction_id, $return = false) {
  211. $request = $this->DoRequest('/mailers/status/' . $transaction_id, $this->default_options(), $return);
  212. return $request;
  213. }
  214. function Suppress($email, $return = false) {
  215. $path = str_replace('%email%', $email, '/audience_members/%email%/suppress_email');
  216. $request = $this->DoRequest($path, $this->default_options(), $return, 'POST');
  217. return $request;
  218. }
  219. function IsSuppressed($email, $return = false) {
  220. $path = str_replace('%email%', $email, '/audience_members/%email%/is_suppressed');
  221. $request = $this->DoRequest($path, $this->default_options(), $return, 'POST');
  222. return $request;
  223. }
  224. function Unsuppress($email, $return = false) {
  225. $csv_data = "email,opt_out\n{$email},0";
  226. return $this->Import($csv_data, $return);
  227. }
  228. function AddMembership($list_name, $email, $additional = array(), $return = false) {
  229. $options = array('email' => $email) + $additional + $this->default_options();
  230. $path = '/audience_lists/' . rawurlencode($list_name) . '/add';
  231. $request = $this->DoRequest($path, $options, $return, 'POST');
  232. return $request;
  233. }
  234. function RemoveMembership($list_name, $email, $return = false) {
  235. $options = array('email' => $email) + $this->default_options();
  236. $path = '/audience_lists/' . rawurlencode($list_name) . '/remove';
  237. $request = $this->DoRequest($path, $options, $return, 'POST');
  238. return $request;
  239. }
  240. }