PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Madmimi.php

https://github.com/brettdewoody/Mad-Mimi-for-CodeIgniter
PHP | 221 lines | 184 code | 8 blank | 29 comment | 33 complexity | 4d5fa442e5aa063a03b3bcabe5aad632 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /*
  3. Mad Mimi for PHP
  4. v2.0.1 - 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 (!function_exists('curl_init')) {
  26. die('Mad Mimi for PHP requires the PHP cURL extension.');
  27. }
  28. class MadMimi {
  29. function __construct() {
  30. $this->ci =& get_instance();
  31. $this->ci->load->config('madmimi');
  32. $this->ci->load->library('spyc');
  33. $this->username = $this->ci->config->item('madmimi_email');
  34. $this->api_key = $this->ci->config->item('madmimi_api');
  35. $this->debug = $this->ci->config->item('madmimi_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', $mail = false) {
  41. $url = "";
  42. if ($method == 'GET') {
  43. $request_options = "?";
  44. } else {
  45. $request_options = "";
  46. }
  47. $request_options .= http_build_query($options);
  48. if ($mail == false) {
  49. $url .= "http://api.madmimi.com{$path}";
  50. } else {
  51. $url .= "https://api.madmimi.com{$path}";
  52. }
  53. if ($method == 'GET') {
  54. $url .= $request_options;
  55. }
  56. $ch = curl_init();
  57. curl_setopt($ch, CURLOPT_URL, $url);
  58. if ($return_status == true) {
  59. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  60. } else {
  61. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  62. }
  63. switch($method) {
  64. case 'GET':
  65. break;
  66. case 'POST':
  67. curl_setopt($ch, CURLOPT_POST, TRUE);
  68. curl_setopt($ch, CURLOPT_POSTFIELDS, $request_options);
  69. if (strstr($url, 'https')) {
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  71. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  72. }
  73. break;
  74. }
  75. if ($this->debug == true) {
  76. echo "URL: {$url}<br />";
  77. if ($method == 'POST') {
  78. echo "Request Options: {$request_options}";
  79. }
  80. } else {
  81. $result = curl_exec($ch) or die(curl_error($ch));
  82. }
  83. curl_close($ch);
  84. if ($this->debug == false) {
  85. return $result;
  86. }
  87. }
  88. function build_csv($arr) {
  89. $csv = "";
  90. $keys = array_keys($arr);
  91. foreach ($keys as $key => $value) {
  92. $csv .= $value . ",";
  93. }
  94. $csv = substr($csv, 0, -1);
  95. $csv .= "\n";
  96. foreach ($arr as $key => $value) {
  97. $csv .= $value . ",";
  98. }
  99. $csv = substr($csv, 0, -1);
  100. $csv .= "\n";
  101. return $csv;
  102. }
  103. function Import($csv_data, $return = false) {
  104. $options = array('csv_file' => $csv_data) + $this->default_options();
  105. $request = $this->DoRequest('/audience_members', $options, $return, 'POST');
  106. return $request;
  107. }
  108. function Lists($return = false) {
  109. $request = $this->DoRequest('/audience_lists/lists.xml', $this->default_options(), $return);
  110. return $request;
  111. }
  112. function AddUser($user, $return = false) {
  113. $csv = $this->build_csv($user);
  114. $this->Import($csv, $return);
  115. }
  116. function RemoveUser($email, $list_name, $return = false) {
  117. $options = array('email' => $email) + $this->default_options();
  118. $request = $this->DoRequest('/audience_lists/' . rawurlencode($list_name) . "/remove", $options, $return, 'POST');
  119. return $request;
  120. }
  121. function Memberships($email, $return = false) {
  122. $url = str_replace('%email%', $email, '/audience_members/%email%/lists.xml');
  123. $request = $this->DoRequest($url, $this->default_options(), $return);
  124. return $request;
  125. }
  126. function NewList($list_name, $return = false) {
  127. $options = array('name' => $list_name) + $this->default_options();
  128. $request = $this->DoRequest('/audience_lists', $options, $return, 'POST');
  129. return $request;
  130. }
  131. function DeleteList($list_name, $return = false) {
  132. $options = array('_method' => 'delete') + $this->default_options();
  133. $request = $this->DoRequest('/audience_lists/' . rawurlencode($list_name), $options, $return, 'POST');
  134. return $request;
  135. }
  136. function SendMessage($options, $yaml_body = null, $return = false) {
  137. if (class_exists('Spyc') && $yaml_body != null) {
  138. $options['body'] = Spyc::YAMLDump($yaml_body);
  139. }
  140. $options = $options + $this->default_options();
  141. if (isset($options['list_name'])) {
  142. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST', true);
  143. } else {
  144. $request = $this->DoRequest('/mailer', $options, $return, 'POST', true);
  145. }
  146. return $request;
  147. }
  148. function SendHTML($options, $html, $return = false) {
  149. if ((!strstr($html, '[[tracking_beacon]]')) && (!strstr($html, '[[peek_image]]'))) {
  150. die('Please include either the [[tracking_beacon]] or the [[peek_image]] macro in your HTML.');
  151. }
  152. $options = $options + $this->default_options();
  153. $options['raw_html'] = $html;
  154. if (isset($options['list_name'])) {
  155. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST', true);
  156. } else {
  157. $request = $this->DoRequest('/mailer', $options, $return, 'POST', true);
  158. }
  159. return $request;
  160. }
  161. function SendPlainText($options, $message, $return = false) {
  162. if (!strstr($message, '[[unsubscribe]]')) {
  163. die('Please include the [[unsubscribe]] macro in your text.');
  164. }
  165. $options = $options + $this->default_options();
  166. $options['raw_plain_text'] = $message;
  167. if (isset($options['list_name'])) {
  168. $request = $this->DoRequest('/mailer/to_list', $options, $return, 'POST', true);
  169. } else {
  170. $request = $this->DoRequest('/mailer', $options, $return, 'POST', true);
  171. }
  172. return $request;
  173. }
  174. function SuppressedSince($unix_timestamp, $return = false) {
  175. $request = $this->DoRequest('/audience_members/suppressed_since/' . $unix_timestamp . '.txt', $this->default_options(), $return);
  176. return $request;
  177. }
  178. function Promotions($page = 1, $return = false) {
  179. $options = array('page' => $page) + $this->default_options();
  180. $request = $this->DoRequest('/promotions.xml', $options, $return);
  181. return $request;
  182. }
  183. function MailingStats($promotion_id, $mailing_id, $return = false) {
  184. $url = str_replace("%promotion_id%", $promotion_id, "/promotions/%promotion_id%/mailings/%mailing_id%.xml");
  185. $url = str_replace("%mailing_id%", $mailing_id, $url);
  186. $request = $this->DoRequest($url, $this->default_options(), $return);
  187. return $request;
  188. }
  189. function Search($query_string, $raw = false, $return = false) {
  190. $options = array('query' => $query_string, 'raw' => $raw) + $this->default_options();
  191. $request = $this->DoRequest('/audience_members/search.xml', $options, $return);
  192. return $request;
  193. }
  194. function Events($unix_timestamp, $return = false) {
  195. $request = $this->DoRequest('/audience_members/events_since/' . $unix_timestamp . '.xml', $this->default_options(), $return);
  196. return $request;
  197. }
  198. function Status($transaction_id, $return = false) {
  199. $request = $this->DoRequest('/mailers/status/' . $transaction_id, $this->default_options(), $return);
  200. return $request;
  201. }
  202. function Suppress($email, $return = false) {
  203. $path = str_replace('%email%', $email, '/audience_members/%email%/suppress_email');
  204. $request = $this->DoRequest($path, $this->default_options(), $return, 'POST', false);
  205. return $request;
  206. }
  207. }