Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 32 | */ |
6a488035 TO |
33 | abstract class CRM_SMS_Provider { |
34 | ||
35 | /** | |
36 | * We only need one instance of this object. So we use the singleton | |
37 | * pattern and cache the instance in this variable | |
38 | * | |
39 | * @var object | |
6a488035 TO |
40 | */ |
41 | static private $_singleton = array(); | |
7da04cde | 42 | const MAX_SMS_CHAR = 460; |
6a488035 TO |
43 | |
44 | /** | |
fe482240 | 45 | * Singleton function used to manage this object. |
6a488035 | 46 | * |
77b97be7 EM |
47 | * @param array $providerParams |
48 | * @param bool $force | |
49 | * | |
6a488035 | 50 | * @return object |
6a488035 | 51 | */ |
00be9182 | 52 | public static function &singleton($providerParams = array(), $force = FALSE) { |
353ffa53 TO |
53 | $mailingID = CRM_Utils_Array::value('mailing_id', $providerParams); |
54 | $providerID = CRM_Utils_Array::value('provider_id', $providerParams); | |
6a488035 TO |
55 | $providerName = CRM_Utils_Array::value('provider', $providerParams); |
56 | ||
57 | if (!$providerID && $mailingID) { | |
58 | $providerID = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id'); | |
59 | $providerParams['provider_id'] = $providerID; | |
60 | } | |
61 | if ($providerID) { | |
62 | $providerName = CRM_SMS_BAO_Provider::getProviderInfo($providerID, 'name'); | |
63 | } | |
64 | ||
65 | if (!$providerName) { | |
66 | CRM_Core_Error::fatal('Provider not known or not provided.'); | |
67 | } | |
68 | ||
69 | $providerName = CRM_Utils_Type::escape($providerName, 'String'); | |
353ffa53 | 70 | $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID; |
6a488035 TO |
71 | |
72 | if (!isset(self::$_singleton[$cacheKey]) || $force) { | |
73 | $ext = CRM_Extension_System::singleton()->getMapper(); | |
74 | if ($ext->isExtensionKey($providerName)) { | |
36f5faa3 MW |
75 | $providerClass = $ext->keyToClass($providerName); |
76 | require_once "{$providerClass}.php"; | |
0db6c3e1 TO |
77 | } |
78 | else { | |
1a7f0799 MW |
79 | // If we are running unit tests we simulate an SMS provider with the name "CiviTestSMSProvider" |
80 | if ($providerName !== 'CiviTestSMSProvider') { | |
36f5faa3 MW |
81 | CRM_Core_Error::fatal("Could not locate extension for {$providerName}."); |
82 | } | |
1a7f0799 | 83 | $providerClass = 'CiviTestSMSProvider'; |
6a488035 TO |
84 | } |
85 | ||
36f5faa3 | 86 | self::$_singleton[$cacheKey] = $providerClass::singleton($providerParams, $force); |
6a488035 TO |
87 | } |
88 | return self::$_singleton[$cacheKey]; | |
89 | } | |
90 | ||
91 | /** | |
fe482240 | 92 | * Send an SMS Message via the API Server. |
54957108 | 93 | * |
94 | * @param array $recipients | |
95 | * @param string $header | |
96 | * @param string $message | |
97 | * @param int $dncID | |
6a488035 | 98 | */ |
2da40d21 | 99 | abstract public function send($recipients, $header, $message, $dncID = NULL); |
6a488035 TO |
100 | |
101 | /** | |
3bdf1f3a | 102 | * Return message text. |
103 | * | |
104 | * Child class could override this function to have better control over the message being sent. | |
105 | * | |
106 | * @param string $message | |
107 | * @param int $contactID | |
108 | * @param array $contactDetails | |
109 | * | |
110 | * @return string | |
6a488035 | 111 | */ |
00be9182 | 112 | public function getMessage($message, $contactID, $contactDetails) { |
6a488035 TO |
113 | $html = $message->getHTMLBody(); |
114 | $text = $message->getTXTBody(); | |
115 | ||
116 | return $html ? $html : $text; | |
117 | } | |
118 | ||
fd0e0077 | 119 | /** |
3bdf1f3a | 120 | * Get recipient details. |
121 | * | |
122 | * @param array $fields | |
123 | * @param array $additionalDetails | |
fd0e0077 EM |
124 | * |
125 | * @return mixed | |
126 | */ | |
00be9182 | 127 | public function getRecipientDetails($fields, $additionalDetails) { |
6a488035 TO |
128 | // we could do more altering here |
129 | $fields['To'] = $fields['phone']; | |
130 | return $fields; | |
131 | } | |
132 | ||
fd0e0077 | 133 | /** |
100fef9d | 134 | * @param int $apiMsgID |
fd0e0077 EM |
135 | * @param $message |
136 | * @param array $headers | |
100fef9d CW |
137 | * @param int $jobID |
138 | * @param int $userID | |
fd0e0077 | 139 | * |
6c552737 | 140 | * @return self|null|object |
fd0e0077 EM |
141 | * @throws CRM_Core_Exception |
142 | */ | |
00be9182 | 143 | public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) { |
6a488035 TO |
144 | if ($jobID) { |
145 | $sql = " | |
146 | SELECT scheduled_id FROM civicrm_mailing m | |
147 | INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1"; | |
148 | $sourceContactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($jobID, 'Integer'))); | |
149 | } | |
353ffa53 | 150 | elseif ($userID) { |
44fb209e | 151 | $sourceContactID = $userID; |
e8cb3963 | 152 | } |
6a488035 TO |
153 | else { |
154 | $session = CRM_Core_Session::singleton(); | |
155 | $sourceContactID = $session->get('userID'); | |
156 | } | |
157 | ||
158 | if (!$sourceContactID) { | |
1393e47e | 159 | $sourceContactID = CRM_Utils_Array::value('Contact', $headers); |
6a488035 TO |
160 | } |
161 | if (!$sourceContactID) { | |
44fb209e | 162 | return FALSE; |
6a488035 TO |
163 | } |
164 | ||
6a488035 TO |
165 | // note: lets not pass status here, assuming status will be updated by callback |
166 | $activityParams = array( | |
167 | 'source_contact_id' => $sourceContactID, | |
168 | 'target_contact_id' => $headers['contact_id'], | |
36f5faa3 | 169 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS delivery'), |
6a488035 | 170 | 'activity_date_time' => date('YmdHis'), |
6a488035 TO |
171 | 'details' => $message, |
172 | 'result' => $apiMsgID, | |
173 | ); | |
174 | return CRM_Activity_BAO_Activity::create($activityParams); | |
175 | } | |
176 | ||
fd0e0077 | 177 | /** |
100fef9d | 178 | * @param string $name |
fd0e0077 EM |
179 | * @param $type |
180 | * @param bool $abort | |
181 | * @param null $default | |
182 | * @param string $location | |
183 | * | |
184 | * @return mixed | |
185 | */ | |
00be9182 | 186 | public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') { |
6a488035 TO |
187 | static $store = NULL; |
188 | $value = CRM_Utils_Request::retrieve($name, $type, $store, | |
189 | FALSE, $default, $location | |
190 | ); | |
191 | if ($abort && $value === NULL) { | |
36f5faa3 | 192 | Civi::log()->warning("Could not find an entry for $name in $location"); |
6a488035 TO |
193 | echo "Failure: Missing Parameter<p>"; |
194 | exit(); | |
195 | } | |
196 | return $value; | |
197 | } | |
198 | ||
fd0e0077 EM |
199 | /** |
200 | * @param $from | |
201 | * @param $body | |
202 | * @param null $to | |
100fef9d | 203 | * @param int $trackID |
fd0e0077 | 204 | * |
6c552737 | 205 | * @return self|null|object |
fd0e0077 EM |
206 | * @throws CRM_Core_Exception |
207 | */ | |
00be9182 | 208 | public function processInbound($from, $body, $to = NULL, $trackID = NULL) { |
caed3ddc SL |
209 | $message = new CRM_SMS_Message(); |
210 | $message->from = $from; | |
211 | $message->to = $to; | |
212 | $message->body = $body; | |
213 | $message->trackID = $trackID; | |
0613768a | 214 | // call hook_civicrm_inboundSMS |
caed3ddc | 215 | CRM_Utils_Hook::inboundSMS($message); |
0613768a | 216 | |
caed3ddc | 217 | if (!$message->fromContactID) { |
0613768a | 218 | // find sender by phone number if $fromContactID not set by hook |
caed3ddc | 219 | $formatFrom = '%' . $this->formatPhone($this->stripPhone($message->from), $like, "like"); |
d20cd5d4 | 220 | $message->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 %1", array( |
3655bea4 SL |
221 | 1 => array($formatFrom, 'String'), |
222 | )); | |
0613768a | 223 | } |
1393e47e | 224 | |
caed3ddc | 225 | if (!$message->fromContactID) { |
1393e47e | 226 | // unknown mobile sender -- create new contact |
227 | // use fake @mobile.sms email address for new contact since civi | |
228 | // requires email or name for all contacts | |
b2b0530a | 229 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
b4f964d9 CW |
230 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
231 | $phoneloc = array_search('Home', $locationTypes); | |
232 | $phonetype = array_search('Mobile', $phoneTypes); | |
caed3ddc | 233 | $stripFrom = $this->stripPhone($message->from); |
b4f964d9 CW |
234 | $contactparams = array( |
235 | 'contact_type' => 'Individual', | |
44fb209e | 236 | 'email' => array( |
353ffa53 TO |
237 | 1 => array( |
238 | 'location_type_id' => $phoneloc, | |
239 | 'email' => $stripFrom . '@mobile.sms', | |
af9b09df | 240 | ), |
353ffa53 | 241 | ), |
44fb209e | 242 | 'phone' => array( |
353ffa53 TO |
243 | 1 => array( |
244 | 'phone_type_id' => $phonetype, | |
245 | 'location_type_id' => $phoneloc, | |
246 | 'phone' => $stripFrom, | |
af9b09df | 247 | ), |
353ffa53 | 248 | ), |
b4f964d9 | 249 | ); |
1393e47e | 250 | $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE); |
caed3ddc | 251 | $message->fromContactID = $fromContact->id; |
6a488035 TO |
252 | } |
253 | ||
d20cd5d4 | 254 | if (!$message->toContactID) { |
0613768a | 255 | // find recipient if $toContactID not set by hook |
caed3ddc | 256 | if ($message->to) { |
d20cd5d4 | 257 | $message->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 %1", array( |
3655bea4 SL |
258 | 1 => array('%' . $message->to, 'String'), |
259 | )); | |
0613768a EE |
260 | } |
261 | else { | |
d20cd5d4 | 262 | $message->toContactID = $message->fromContactID; |
0613768a | 263 | } |
6a488035 TO |
264 | } |
265 | ||
caed3ddc | 266 | if ($message->fromContactID) { |
6a488035 TO |
267 | // note: lets not pass status here, assuming status will be updated by callback |
268 | $activityParams = array( | |
caed3ddc SL |
269 | 'source_contact_id' => $message->toContactID, |
270 | 'target_contact_id' => $message->fromContactID, | |
36f5faa3 | 271 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound SMS'), |
6a488035 | 272 | 'activity_date_time' => date('YmdHis'), |
36f5faa3 | 273 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
caed3ddc SL |
274 | 'details' => $message->body, |
275 | 'phone_number' => $message->from, | |
6a488035 | 276 | ); |
caed3ddc | 277 | if ($message->trackID) { |
d20cd5d4 | 278 | $activityParams['result'] = CRM_Utils_Type::escape($message->trackID, 'String'); |
6a488035 TO |
279 | } |
280 | ||
281 | $result = CRM_Activity_BAO_Activity::create($activityParams); | |
36f5faa3 | 282 | Civi::log()->info("Inbound SMS recorded for cid={$message->fromContactID}."); |
6a488035 TO |
283 | return $result; |
284 | } | |
285 | } | |
286 | ||
fd0e0077 EM |
287 | /** |
288 | * @param $phone | |
289 | * | |
290 | * @return mixed|string | |
291 | */ | |
00be9182 | 292 | public function stripPhone($phone) { |
6a488035 TO |
293 | $newphone = preg_replace('/[^0-9x]/', '', $phone); |
294 | while (substr($newphone, 0, 1) == "1") { | |
295 | $newphone = substr($newphone, 1); | |
296 | } | |
297 | while (strpos($newphone, "xx") !== FALSE) { | |
298 | $newphone = str_replace("xx", "x", $newphone); | |
299 | } | |
300 | while (substr($newphone, -1) == "x") { | |
301 | $newphone = substr($newphone, 0, -1); | |
302 | } | |
303 | return $newphone; | |
304 | } | |
305 | ||
fd0e0077 EM |
306 | /** |
307 | * @param $phone | |
308 | * @param $kind | |
309 | * @param string $format | |
310 | * | |
311 | * @return mixed|string | |
312 | */ | |
00be9182 | 313 | public function formatPhone($phone, &$kind, $format = "dash") { |
6a488035 TO |
314 | $phoneA = explode("x", $phone); |
315 | switch (strlen($phoneA[0])) { | |
316 | case 0: | |
317 | $kind = "XOnly"; | |
318 | $area = ""; | |
319 | $exch = ""; | |
320 | $uniq = ""; | |
353ffa53 | 321 | $ext = $phoneA[1]; |
6a488035 TO |
322 | break; |
323 | ||
324 | case 7: | |
325 | $kind = $phoneA[1] ? "LocalX" : "Local"; | |
326 | $area = ""; | |
327 | $exch = substr($phone, 0, 3); | |
328 | $uniq = substr($phone, 3, 4); | |
353ffa53 | 329 | $ext = $phoneA[1]; |
6a488035 TO |
330 | break; |
331 | ||
332 | case 10: | |
333 | $kind = $phoneA[1] ? "LongX" : "Long"; | |
334 | $area = substr($phone, 0, 3); | |
335 | $exch = substr($phone, 3, 3); | |
336 | $uniq = substr($phone, 6, 4); | |
353ffa53 | 337 | $ext = $phoneA[1]; |
6a488035 TO |
338 | break; |
339 | ||
340 | default: | |
341 | $kind = "Unknown"; | |
342 | return $phone; | |
343 | } | |
344 | ||
345 | switch ($format) { | |
346 | case "like": | |
347 | $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%'; | |
348 | $newphone = str_replace('%%', '%', $newphone); | |
349 | $newphone = str_replace('%%', '%', $newphone); | |
350 | return $newphone; | |
351 | ||
352 | case "dash": | |
353 | $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext; | |
354 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
355 | return $newphone; | |
356 | ||
357 | case "bare": | |
358 | $newphone = $area . $exch . $uniq . "x" . $ext; | |
359 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
360 | return $newphone; | |
361 | ||
362 | case "area": | |
363 | return $area; | |
364 | ||
365 | default: | |
366 | return $phone; | |
367 | } | |
368 | } | |
1e2f5f25 | 369 | |
fd0e0077 EM |
370 | /** |
371 | * @param $values | |
372 | * | |
373 | * @return string | |
374 | */ | |
00be9182 | 375 | public function urlEncode($values) { |
1e2f5f25 DS |
376 | $uri = ''; |
377 | foreach ($values as $key => $value) { | |
378 | $value = urlencode($value); | |
379 | $uri .= "&{$key}={$value}"; | |
380 | } | |
381 | if (!empty($uri)) { | |
382 | $uri = substr($uri, 1); | |
383 | } | |
384 | return $uri; | |
385 | } | |
96025800 | 386 | |
6a488035 | 387 | } |