Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
6a488035 TO |
35 | abstract class CRM_SMS_Provider { |
36 | ||
37 | /** | |
38 | * We only need one instance of this object. So we use the singleton | |
39 | * pattern and cache the instance in this variable | |
40 | * | |
41 | * @var object | |
6a488035 TO |
42 | */ |
43 | static private $_singleton = array(); | |
7da04cde | 44 | const MAX_SMS_CHAR = 460; |
6a488035 TO |
45 | |
46 | /** | |
fe482240 | 47 | * Singleton function used to manage this object. |
6a488035 | 48 | * |
77b97be7 EM |
49 | * @param array $providerParams |
50 | * @param bool $force | |
51 | * | |
6a488035 | 52 | * @return object |
6a488035 | 53 | */ |
00be9182 | 54 | public static function &singleton($providerParams = array(), $force = FALSE) { |
353ffa53 TO |
55 | $mailingID = CRM_Utils_Array::value('mailing_id', $providerParams); |
56 | $providerID = CRM_Utils_Array::value('provider_id', $providerParams); | |
6a488035 TO |
57 | $providerName = CRM_Utils_Array::value('provider', $providerParams); |
58 | ||
59 | if (!$providerID && $mailingID) { | |
60 | $providerID = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id'); | |
61 | $providerParams['provider_id'] = $providerID; | |
62 | } | |
63 | if ($providerID) { | |
64 | $providerName = CRM_SMS_BAO_Provider::getProviderInfo($providerID, 'name'); | |
65 | } | |
66 | ||
67 | if (!$providerName) { | |
68 | CRM_Core_Error::fatal('Provider not known or not provided.'); | |
69 | } | |
70 | ||
71 | $providerName = CRM_Utils_Type::escape($providerName, 'String'); | |
353ffa53 | 72 | $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID; |
6a488035 TO |
73 | |
74 | if (!isset(self::$_singleton[$cacheKey]) || $force) { | |
75 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
76 | if ($ext->isExtensionKey($providerName)) { | |
77 | $paymentClass = $ext->keyToClass($providerName); | |
44fb209e | 78 | require_once "{$paymentClass}.php"; |
0db6c3e1 TO |
79 | } |
80 | else { | |
6a488035 TO |
81 | CRM_Core_Error::fatal("Could not locate extension for {$providerName}."); |
82 | } | |
83 | ||
0e6e8724 | 84 | self::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force); |
6a488035 TO |
85 | } |
86 | return self::$_singleton[$cacheKey]; | |
87 | } | |
88 | ||
89 | /** | |
fe482240 | 90 | * Send an SMS Message via the API Server. |
6a488035 | 91 | */ |
2da40d21 | 92 | abstract public function send($recipients, $header, $message, $dncID = NULL); |
6a488035 TO |
93 | |
94 | /** | |
100fef9d | 95 | * Return message text. Child class could override this function to have better control over the message being sent. |
6a488035 | 96 | */ |
00be9182 | 97 | public function getMessage($message, $contactID, $contactDetails) { |
6a488035 TO |
98 | $html = $message->getHTMLBody(); |
99 | $text = $message->getTXTBody(); | |
100 | ||
101 | return $html ? $html : $text; | |
102 | } | |
103 | ||
fd0e0077 EM |
104 | /** |
105 | * @param $fields | |
106 | * @param $additionalDetails | |
107 | * | |
108 | * @return mixed | |
109 | */ | |
00be9182 | 110 | public function getRecipientDetails($fields, $additionalDetails) { |
6a488035 TO |
111 | // we could do more altering here |
112 | $fields['To'] = $fields['phone']; | |
113 | return $fields; | |
114 | } | |
115 | ||
fd0e0077 | 116 | /** |
100fef9d | 117 | * @param int $apiMsgID |
fd0e0077 EM |
118 | * @param $message |
119 | * @param array $headers | |
100fef9d CW |
120 | * @param int $jobID |
121 | * @param int $userID | |
fd0e0077 | 122 | * |
6c552737 | 123 | * @return self|null|object |
fd0e0077 EM |
124 | * @throws CRM_Core_Exception |
125 | */ | |
00be9182 | 126 | public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) { |
6a488035 TO |
127 | if ($jobID) { |
128 | $sql = " | |
129 | SELECT scheduled_id FROM civicrm_mailing m | |
130 | INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1"; | |
131 | $sourceContactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($jobID, 'Integer'))); | |
132 | } | |
353ffa53 | 133 | elseif ($userID) { |
44fb209e | 134 | $sourceContactID = $userID; |
e8cb3963 | 135 | } |
6a488035 TO |
136 | else { |
137 | $session = CRM_Core_Session::singleton(); | |
138 | $sourceContactID = $session->get('userID'); | |
139 | } | |
140 | ||
141 | if (!$sourceContactID) { | |
1393e47e | 142 | $sourceContactID = CRM_Utils_Array::value('Contact', $headers); |
6a488035 TO |
143 | } |
144 | if (!$sourceContactID) { | |
44fb209e | 145 | return FALSE; |
6a488035 TO |
146 | } |
147 | ||
e8cb3963 | 148 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS delivery', 'name'); |
6a488035 TO |
149 | // note: lets not pass status here, assuming status will be updated by callback |
150 | $activityParams = array( | |
151 | 'source_contact_id' => $sourceContactID, | |
152 | 'target_contact_id' => $headers['contact_id'], | |
153 | 'activity_type_id' => $activityTypeID, | |
154 | 'activity_date_time' => date('YmdHis'), | |
6a488035 TO |
155 | 'details' => $message, |
156 | 'result' => $apiMsgID, | |
157 | ); | |
158 | return CRM_Activity_BAO_Activity::create($activityParams); | |
159 | } | |
160 | ||
fd0e0077 | 161 | /** |
100fef9d | 162 | * @param string $name |
fd0e0077 EM |
163 | * @param $type |
164 | * @param bool $abort | |
165 | * @param null $default | |
166 | * @param string $location | |
167 | * | |
168 | * @return mixed | |
169 | */ | |
00be9182 | 170 | public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') { |
6a488035 TO |
171 | static $store = NULL; |
172 | $value = CRM_Utils_Request::retrieve($name, $type, $store, | |
173 | FALSE, $default, $location | |
174 | ); | |
175 | if ($abort && $value === NULL) { | |
176 | CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location"); | |
177 | echo "Failure: Missing Parameter<p>"; | |
178 | exit(); | |
179 | } | |
180 | return $value; | |
181 | } | |
182 | ||
fd0e0077 EM |
183 | /** |
184 | * @param $from | |
185 | * @param $body | |
186 | * @param null $to | |
100fef9d | 187 | * @param int $trackID |
fd0e0077 | 188 | * |
6c552737 | 189 | * @return self|null|object |
fd0e0077 EM |
190 | * @throws CRM_Core_Exception |
191 | */ | |
00be9182 | 192 | public function processInbound($from, $body, $to = NULL, $trackID = NULL) { |
353ffa53 TO |
193 | $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like"); |
194 | $escapedFrom = CRM_Utils_Type::escape($formatFrom, 'String'); | |
e8cb3963 | 195 | $fromContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $escapedFrom . '"'); |
1393e47e | 196 | |
353ffa53 | 197 | if (!$fromContactID) { |
1393e47e | 198 | // unknown mobile sender -- create new contact |
199 | // use fake @mobile.sms email address for new contact since civi | |
200 | // requires email or name for all contacts | |
b2b0530a | 201 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
b4f964d9 CW |
202 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
203 | $phoneloc = array_search('Home', $locationTypes); | |
204 | $phonetype = array_search('Mobile', $phoneTypes); | |
1393e47e | 205 | $stripFrom = $this->stripPhone($from); |
b4f964d9 CW |
206 | $contactparams = array( |
207 | 'contact_type' => 'Individual', | |
44fb209e | 208 | 'email' => array( |
353ffa53 TO |
209 | 1 => array( |
210 | 'location_type_id' => $phoneloc, | |
211 | 'email' => $stripFrom . '@mobile.sms', | |
af9b09df | 212 | ), |
353ffa53 | 213 | ), |
44fb209e | 214 | 'phone' => array( |
353ffa53 TO |
215 | 1 => array( |
216 | 'phone_type_id' => $phonetype, | |
217 | 'location_type_id' => $phoneloc, | |
218 | 'phone' => $stripFrom, | |
af9b09df | 219 | ), |
353ffa53 | 220 | ), |
b4f964d9 | 221 | ); |
1393e47e | 222 | $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE); |
6a488035 TO |
223 | $fromContactID = $fromContact->id; |
224 | } | |
225 | ||
226 | if ($to) { | |
227 | $to = CRM_Utils_Type::escape($to, 'String'); | |
e8cb3963 | 228 | $toContactID = CRM_Core_DAO::singleValueQuery('SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE "%' . $to . '"'); |
6a488035 TO |
229 | } |
230 | else { | |
231 | $toContactID = $fromContactID; | |
232 | } | |
233 | ||
234 | if ($fromContactID) { | |
235 | $actStatusIDs = array_flip(CRM_Core_OptionGroup::values('activity_status')); | |
e8cb3963 | 236 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound SMS', 'name'); |
6a488035 TO |
237 | |
238 | // note: lets not pass status here, assuming status will be updated by callback | |
239 | $activityParams = array( | |
240 | 'source_contact_id' => $toContactID, | |
241 | 'target_contact_id' => $fromContactID, | |
242 | 'activity_type_id' => $activityTypeID, | |
243 | 'activity_date_time' => date('YmdHis'), | |
6a488035 TO |
244 | 'status_id' => $actStatusIDs['Completed'], |
245 | 'details' => $body, | |
21dfd5f5 | 246 | 'phone_number' => $from, |
6a488035 TO |
247 | ); |
248 | if ($trackID) { | |
249 | $trackID = CRM_Utils_Type::escape($trackID, 'String'); | |
250 | $activityParams['result'] = $trackID; | |
251 | } | |
252 | ||
253 | $result = CRM_Activity_BAO_Activity::create($activityParams); | |
254 | CRM_Core_Error::debug_log_message("Inbound SMS recorded for cid={$fromContactID}."); | |
255 | return $result; | |
256 | } | |
257 | } | |
258 | ||
fd0e0077 EM |
259 | /** |
260 | * @param $phone | |
261 | * | |
262 | * @return mixed|string | |
263 | */ | |
00be9182 | 264 | public function stripPhone($phone) { |
6a488035 TO |
265 | $newphone = preg_replace('/[^0-9x]/', '', $phone); |
266 | while (substr($newphone, 0, 1) == "1") { | |
267 | $newphone = substr($newphone, 1); | |
268 | } | |
269 | while (strpos($newphone, "xx") !== FALSE) { | |
270 | $newphone = str_replace("xx", "x", $newphone); | |
271 | } | |
272 | while (substr($newphone, -1) == "x") { | |
273 | $newphone = substr($newphone, 0, -1); | |
274 | } | |
275 | return $newphone; | |
276 | } | |
277 | ||
fd0e0077 EM |
278 | /** |
279 | * @param $phone | |
280 | * @param $kind | |
281 | * @param string $format | |
282 | * | |
283 | * @return mixed|string | |
284 | */ | |
00be9182 | 285 | public function formatPhone($phone, &$kind, $format = "dash") { |
6a488035 TO |
286 | $phoneA = explode("x", $phone); |
287 | switch (strlen($phoneA[0])) { | |
288 | case 0: | |
289 | $kind = "XOnly"; | |
290 | $area = ""; | |
291 | $exch = ""; | |
292 | $uniq = ""; | |
353ffa53 | 293 | $ext = $phoneA[1]; |
6a488035 TO |
294 | break; |
295 | ||
296 | case 7: | |
297 | $kind = $phoneA[1] ? "LocalX" : "Local"; | |
298 | $area = ""; | |
299 | $exch = substr($phone, 0, 3); | |
300 | $uniq = substr($phone, 3, 4); | |
353ffa53 | 301 | $ext = $phoneA[1]; |
6a488035 TO |
302 | break; |
303 | ||
304 | case 10: | |
305 | $kind = $phoneA[1] ? "LongX" : "Long"; | |
306 | $area = substr($phone, 0, 3); | |
307 | $exch = substr($phone, 3, 3); | |
308 | $uniq = substr($phone, 6, 4); | |
353ffa53 | 309 | $ext = $phoneA[1]; |
6a488035 TO |
310 | break; |
311 | ||
312 | default: | |
313 | $kind = "Unknown"; | |
314 | return $phone; | |
315 | } | |
316 | ||
317 | switch ($format) { | |
318 | case "like": | |
319 | $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%'; | |
320 | $newphone = str_replace('%%', '%', $newphone); | |
321 | $newphone = str_replace('%%', '%', $newphone); | |
322 | return $newphone; | |
323 | ||
324 | case "dash": | |
325 | $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext; | |
326 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
327 | return $newphone; | |
328 | ||
329 | case "bare": | |
330 | $newphone = $area . $exch . $uniq . "x" . $ext; | |
331 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
332 | return $newphone; | |
333 | ||
334 | case "area": | |
335 | return $area; | |
336 | ||
337 | default: | |
338 | return $phone; | |
339 | } | |
340 | } | |
1e2f5f25 | 341 | |
fd0e0077 EM |
342 | /** |
343 | * @param $values | |
344 | * | |
345 | * @return string | |
346 | */ | |
00be9182 | 347 | public function urlEncode($values) { |
1e2f5f25 DS |
348 | $uri = ''; |
349 | foreach ($values as $key => $value) { | |
350 | $value = urlencode($value); | |
351 | $uri .= "&{$key}={$value}"; | |
352 | } | |
353 | if (!empty($uri)) { | |
354 | $uri = substr($uri, 1); | |
355 | } | |
356 | return $uri; | |
357 | } | |
96025800 | 358 | |
6a488035 | 359 | } |