Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
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 | 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)) { | |
75 | $paymentClass = $ext->keyToClass($providerName); | |
44fb209e | 76 | require_once "{$paymentClass}.php"; |
0db6c3e1 TO |
77 | } |
78 | else { | |
6a488035 TO |
79 | CRM_Core_Error::fatal("Could not locate extension for {$providerName}."); |
80 | } | |
81 | ||
0e6e8724 | 82 | self::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force); |
6a488035 TO |
83 | } |
84 | return self::$_singleton[$cacheKey]; | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Send an SMS Message via the API Server. |
54957108 | 89 | * |
90 | * @param array $recipients | |
91 | * @param string $header | |
92 | * @param string $message | |
93 | * @param int $dncID | |
6a488035 | 94 | */ |
2da40d21 | 95 | abstract public function send($recipients, $header, $message, $dncID = NULL); |
6a488035 TO |
96 | |
97 | /** | |
3bdf1f3a | 98 | * Return message text. |
99 | * | |
100 | * Child class could override this function to have better control over the message being sent. | |
101 | * | |
102 | * @param string $message | |
103 | * @param int $contactID | |
104 | * @param array $contactDetails | |
105 | * | |
106 | * @return string | |
6a488035 | 107 | */ |
00be9182 | 108 | public function getMessage($message, $contactID, $contactDetails) { |
6a488035 TO |
109 | $html = $message->getHTMLBody(); |
110 | $text = $message->getTXTBody(); | |
111 | ||
112 | return $html ? $html : $text; | |
113 | } | |
114 | ||
fd0e0077 | 115 | /** |
3bdf1f3a | 116 | * Get recipient details. |
117 | * | |
118 | * @param array $fields | |
119 | * @param array $additionalDetails | |
fd0e0077 EM |
120 | * |
121 | * @return mixed | |
122 | */ | |
00be9182 | 123 | public function getRecipientDetails($fields, $additionalDetails) { |
6a488035 TO |
124 | // we could do more altering here |
125 | $fields['To'] = $fields['phone']; | |
126 | return $fields; | |
127 | } | |
128 | ||
fd0e0077 | 129 | /** |
100fef9d | 130 | * @param int $apiMsgID |
fd0e0077 EM |
131 | * @param $message |
132 | * @param array $headers | |
100fef9d CW |
133 | * @param int $jobID |
134 | * @param int $userID | |
fd0e0077 | 135 | * |
6c552737 | 136 | * @return self|null|object |
fd0e0077 EM |
137 | * @throws CRM_Core_Exception |
138 | */ | |
00be9182 | 139 | public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) { |
6a488035 TO |
140 | if ($jobID) { |
141 | $sql = " | |
142 | SELECT scheduled_id FROM civicrm_mailing m | |
143 | INNER JOIN civicrm_mailing_job mj ON mj.mailing_id = m.id AND mj.id = %1"; | |
144 | $sourceContactID = CRM_Core_DAO::singleValueQuery($sql, array(1 => array($jobID, 'Integer'))); | |
145 | } | |
353ffa53 | 146 | elseif ($userID) { |
44fb209e | 147 | $sourceContactID = $userID; |
e8cb3963 | 148 | } |
6a488035 TO |
149 | else { |
150 | $session = CRM_Core_Session::singleton(); | |
151 | $sourceContactID = $session->get('userID'); | |
152 | } | |
153 | ||
154 | if (!$sourceContactID) { | |
1393e47e | 155 | $sourceContactID = CRM_Utils_Array::value('Contact', $headers); |
6a488035 TO |
156 | } |
157 | if (!$sourceContactID) { | |
44fb209e | 158 | return FALSE; |
6a488035 TO |
159 | } |
160 | ||
e8cb3963 | 161 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'SMS delivery', 'name'); |
6a488035 TO |
162 | // note: lets not pass status here, assuming status will be updated by callback |
163 | $activityParams = array( | |
164 | 'source_contact_id' => $sourceContactID, | |
165 | 'target_contact_id' => $headers['contact_id'], | |
166 | 'activity_type_id' => $activityTypeID, | |
167 | 'activity_date_time' => date('YmdHis'), | |
6a488035 TO |
168 | 'details' => $message, |
169 | 'result' => $apiMsgID, | |
170 | ); | |
171 | return CRM_Activity_BAO_Activity::create($activityParams); | |
172 | } | |
173 | ||
fd0e0077 | 174 | /** |
100fef9d | 175 | * @param string $name |
fd0e0077 EM |
176 | * @param $type |
177 | * @param bool $abort | |
178 | * @param null $default | |
179 | * @param string $location | |
180 | * | |
181 | * @return mixed | |
182 | */ | |
00be9182 | 183 | public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') { |
6a488035 TO |
184 | static $store = NULL; |
185 | $value = CRM_Utils_Request::retrieve($name, $type, $store, | |
186 | FALSE, $default, $location | |
187 | ); | |
188 | if ($abort && $value === NULL) { | |
189 | CRM_Core_Error::debug_log_message("Could not find an entry for $name in $location"); | |
190 | echo "Failure: Missing Parameter<p>"; | |
191 | exit(); | |
192 | } | |
193 | return $value; | |
194 | } | |
195 | ||
fd0e0077 EM |
196 | /** |
197 | * @param $from | |
198 | * @param $body | |
199 | * @param null $to | |
100fef9d | 200 | * @param int $trackID |
fd0e0077 | 201 | * |
6c552737 | 202 | * @return self|null|object |
fd0e0077 EM |
203 | * @throws CRM_Core_Exception |
204 | */ | |
00be9182 | 205 | public function processInbound($from, $body, $to = NULL, $trackID = NULL) { |
353ffa53 TO |
206 | $formatFrom = $this->formatPhone($this->stripPhone($from), $like, "like"); |
207 | $escapedFrom = CRM_Utils_Type::escape($formatFrom, 'String'); | |
e8cb3963 | 208 | $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 | 209 | |
353ffa53 | 210 | if (!$fromContactID) { |
1393e47e | 211 | // unknown mobile sender -- create new contact |
212 | // use fake @mobile.sms email address for new contact since civi | |
213 | // requires email or name for all contacts | |
b2b0530a | 214 | $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
b4f964d9 CW |
215 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
216 | $phoneloc = array_search('Home', $locationTypes); | |
217 | $phonetype = array_search('Mobile', $phoneTypes); | |
1393e47e | 218 | $stripFrom = $this->stripPhone($from); |
b4f964d9 CW |
219 | $contactparams = array( |
220 | 'contact_type' => 'Individual', | |
44fb209e | 221 | 'email' => array( |
353ffa53 TO |
222 | 1 => array( |
223 | 'location_type_id' => $phoneloc, | |
224 | 'email' => $stripFrom . '@mobile.sms', | |
af9b09df | 225 | ), |
353ffa53 | 226 | ), |
44fb209e | 227 | 'phone' => array( |
353ffa53 TO |
228 | 1 => array( |
229 | 'phone_type_id' => $phonetype, | |
230 | 'location_type_id' => $phoneloc, | |
231 | 'phone' => $stripFrom, | |
af9b09df | 232 | ), |
353ffa53 | 233 | ), |
b4f964d9 | 234 | ); |
1393e47e | 235 | $fromContact = CRM_Contact_BAO_Contact::create($contactparams, FALSE, TRUE, FALSE); |
6a488035 TO |
236 | $fromContactID = $fromContact->id; |
237 | } | |
238 | ||
239 | if ($to) { | |
240 | $to = CRM_Utils_Type::escape($to, 'String'); | |
e8cb3963 | 241 | $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 |
242 | } |
243 | else { | |
244 | $toContactID = $fromContactID; | |
245 | } | |
246 | ||
247 | if ($fromContactID) { | |
248 | $actStatusIDs = array_flip(CRM_Core_OptionGroup::values('activity_status')); | |
e8cb3963 | 249 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', 'Inbound SMS', 'name'); |
6a488035 TO |
250 | |
251 | // note: lets not pass status here, assuming status will be updated by callback | |
252 | $activityParams = array( | |
253 | 'source_contact_id' => $toContactID, | |
254 | 'target_contact_id' => $fromContactID, | |
255 | 'activity_type_id' => $activityTypeID, | |
256 | 'activity_date_time' => date('YmdHis'), | |
6a488035 TO |
257 | 'status_id' => $actStatusIDs['Completed'], |
258 | 'details' => $body, | |
21dfd5f5 | 259 | 'phone_number' => $from, |
6a488035 TO |
260 | ); |
261 | if ($trackID) { | |
262 | $trackID = CRM_Utils_Type::escape($trackID, 'String'); | |
263 | $activityParams['result'] = $trackID; | |
264 | } | |
265 | ||
266 | $result = CRM_Activity_BAO_Activity::create($activityParams); | |
267 | CRM_Core_Error::debug_log_message("Inbound SMS recorded for cid={$fromContactID}."); | |
268 | return $result; | |
269 | } | |
270 | } | |
271 | ||
fd0e0077 EM |
272 | /** |
273 | * @param $phone | |
274 | * | |
275 | * @return mixed|string | |
276 | */ | |
00be9182 | 277 | public function stripPhone($phone) { |
6a488035 TO |
278 | $newphone = preg_replace('/[^0-9x]/', '', $phone); |
279 | while (substr($newphone, 0, 1) == "1") { | |
280 | $newphone = substr($newphone, 1); | |
281 | } | |
282 | while (strpos($newphone, "xx") !== FALSE) { | |
283 | $newphone = str_replace("xx", "x", $newphone); | |
284 | } | |
285 | while (substr($newphone, -1) == "x") { | |
286 | $newphone = substr($newphone, 0, -1); | |
287 | } | |
288 | return $newphone; | |
289 | } | |
290 | ||
fd0e0077 EM |
291 | /** |
292 | * @param $phone | |
293 | * @param $kind | |
294 | * @param string $format | |
295 | * | |
296 | * @return mixed|string | |
297 | */ | |
00be9182 | 298 | public function formatPhone($phone, &$kind, $format = "dash") { |
6a488035 TO |
299 | $phoneA = explode("x", $phone); |
300 | switch (strlen($phoneA[0])) { | |
301 | case 0: | |
302 | $kind = "XOnly"; | |
303 | $area = ""; | |
304 | $exch = ""; | |
305 | $uniq = ""; | |
353ffa53 | 306 | $ext = $phoneA[1]; |
6a488035 TO |
307 | break; |
308 | ||
309 | case 7: | |
310 | $kind = $phoneA[1] ? "LocalX" : "Local"; | |
311 | $area = ""; | |
312 | $exch = substr($phone, 0, 3); | |
313 | $uniq = substr($phone, 3, 4); | |
353ffa53 | 314 | $ext = $phoneA[1]; |
6a488035 TO |
315 | break; |
316 | ||
317 | case 10: | |
318 | $kind = $phoneA[1] ? "LongX" : "Long"; | |
319 | $area = substr($phone, 0, 3); | |
320 | $exch = substr($phone, 3, 3); | |
321 | $uniq = substr($phone, 6, 4); | |
353ffa53 | 322 | $ext = $phoneA[1]; |
6a488035 TO |
323 | break; |
324 | ||
325 | default: | |
326 | $kind = "Unknown"; | |
327 | return $phone; | |
328 | } | |
329 | ||
330 | switch ($format) { | |
331 | case "like": | |
332 | $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%'; | |
333 | $newphone = str_replace('%%', '%', $newphone); | |
334 | $newphone = str_replace('%%', '%', $newphone); | |
335 | return $newphone; | |
336 | ||
337 | case "dash": | |
338 | $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext; | |
339 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
340 | return $newphone; | |
341 | ||
342 | case "bare": | |
343 | $newphone = $area . $exch . $uniq . "x" . $ext; | |
344 | $newphone = trim(trim(trim($newphone, "x"), "-")); | |
345 | return $newphone; | |
346 | ||
347 | case "area": | |
348 | return $area; | |
349 | ||
350 | default: | |
351 | return $phone; | |
352 | } | |
353 | } | |
1e2f5f25 | 354 | |
fd0e0077 EM |
355 | /** |
356 | * @param $values | |
357 | * | |
358 | * @return string | |
359 | */ | |
00be9182 | 360 | public function urlEncode($values) { |
1e2f5f25 DS |
361 | $uri = ''; |
362 | foreach ($values as $key => $value) { | |
363 | $value = urlencode($value); | |
364 | $uri .= "&{$key}={$value}"; | |
365 | } | |
366 | if (!empty($uri)) { | |
367 | $uri = substr($uri, 1); | |
368 | } | |
369 | return $uri; | |
370 | } | |
96025800 | 371 | |
6a488035 | 372 | } |