3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 abstract class CRM_SMS_Provider
{
36 * We only need one instance of this object. So we use the singleton
37 * pattern and cache the instance in this variable
41 static private $_singleton = array();
42 const MAX_SMS_CHAR
= 460;
45 * Singleton function used to manage this object.
47 * @param array $providerParams
52 public static function &singleton($providerParams = array(), $force = FALSE) {
53 $mailingID = CRM_Utils_Array
::value('mailing_id', $providerParams);
54 $providerID = CRM_Utils_Array
::value('provider_id', $providerParams);
55 $providerName = CRM_Utils_Array
::value('provider', $providerParams);
57 if (!$providerID && $mailingID) {
58 $providerID = CRM_Core_DAO
::getFieldValue('CRM_Mailing_DAO_Mailing', $mailingID, 'sms_provider_id', 'id');
59 $providerParams['provider_id'] = $providerID;
62 $providerName = CRM_SMS_BAO_Provider
::getProviderInfo($providerID, 'name');
66 CRM_Core_Error
::fatal('Provider not known or not provided.');
69 $providerName = CRM_Utils_Type
::escape($providerName, 'String');
70 $cacheKey = "{$providerName}_" . (int) $providerID . "_" . (int) $mailingID;
72 if (!isset(self
::$_singleton[$cacheKey]) ||
$force) {
73 $ext = CRM_Extension_System
::singleton()->getMapper();
74 if ($ext->isExtensionKey($providerName)) {
75 $paymentClass = $ext->keyToClass($providerName);
76 require_once "{$paymentClass}.php";
79 CRM_Core_Error
::fatal("Could not locate extension for {$providerName}.");
82 self
::$_singleton[$cacheKey] = $paymentClass::singleton($providerParams, $force);
84 return self
::$_singleton[$cacheKey];
88 * Send an SMS Message via the API Server.
90 * @param array $recipients
91 * @param string $header
92 * @param string $message
95 abstract public function send($recipients, $header, $message, $dncID = NULL);
98 * Return message text.
100 * Child class could override this function to have better control over the message being sent.
102 * @param string $message
103 * @param int $contactID
104 * @param array $contactDetails
108 public function getMessage($message, $contactID, $contactDetails) {
109 $html = $message->getHTMLBody();
110 $text = $message->getTXTBody();
112 return $html ?
$html : $text;
116 * Get recipient details.
118 * @param array $fields
119 * @param array $additionalDetails
123 public function getRecipientDetails($fields, $additionalDetails) {
124 // we could do more altering here
125 $fields['To'] = $fields['phone'];
130 * @param int $apiMsgID
132 * @param array $headers
136 * @return self|null|object
137 * @throws CRM_Core_Exception
139 public function createActivity($apiMsgID, $message, $headers = array(), $jobID = NULL, $userID = NULL) {
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')));
147 $sourceContactID = $userID;
150 $session = CRM_Core_Session
::singleton();
151 $sourceContactID = $session->get('userID');
154 if (!$sourceContactID) {
155 $sourceContactID = CRM_Utils_Array
::value('Contact', $headers);
157 if (!$sourceContactID) {
161 $activityTypeID = CRM_Core_OptionGroup
::getValue('activity_type', 'SMS delivery', 'name');
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'),
168 'details' => $message,
169 'result' => $apiMsgID,
171 return CRM_Activity_BAO_Activity
::create($activityParams);
175 * @param string $name
178 * @param null $default
179 * @param string $location
183 public function retrieve($name, $type, $abort = TRUE, $default = NULL, $location = 'REQUEST') {
184 static $store = NULL;
185 $value = CRM_Utils_Request
::retrieve($name, $type, $store,
186 FALSE, $default, $location
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>";
200 * @param int $trackID
202 * @return self|null|object
203 * @throws CRM_Core_Exception
205 public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
206 $message = new CRM_SMS_Message();
207 $message->from
= $from;
209 $message->body
= $body;
210 $message->trackID
= $trackID;
211 // call hook_civicrm_inboundSMS
212 CRM_Utils_Hook
::inboundSMS($message);
214 if (!$message->fromContactID
) {
215 // find sender by phone number if $fromContactID not set by hook
216 $formatFrom = '%' . $this->formatPhone($this->stripPhone($message->from
), $like, "like");
217 $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(
218 1 => array($formatFrom, 'String')));
221 if (!$message->fromContactID
) {
222 // unknown mobile sender -- create new contact
223 // use fake @mobile.sms email address for new contact since civi
224 // requires email or name for all contacts
225 $locationTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
226 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
227 $phoneloc = array_search('Home', $locationTypes);
228 $phonetype = array_search('Mobile', $phoneTypes);
229 $stripFrom = $this->stripPhone($message->from
);
230 $contactparams = array(
231 'contact_type' => 'Individual',
234 'location_type_id' => $phoneloc,
235 'email' => $stripFrom . '@mobile.sms',
240 'phone_type_id' => $phonetype,
241 'location_type_id' => $phoneloc,
242 'phone' => $stripFrom,
246 $fromContact = CRM_Contact_BAO_Contact
::create($contactparams, FALSE, TRUE, FALSE);
247 $message->fromContactID
= $fromContact->id
;
250 if (!$message->toContactID
) {
251 // find recipient if $toContactID not set by hook
253 $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(
254 1 => array('%' . $message->to
, 'String')));
257 $message->toContactID
= $message->fromContactID
;
261 if ($message->fromContactID
) {
262 $actStatusIDs = array_flip(CRM_Core_OptionGroup
::values('activity_status'));
263 $activityTypeID = CRM_Core_PseudoConstant
::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Inbound SMS');
265 // note: lets not pass status here, assuming status will be updated by callback
266 $activityParams = array(
267 'source_contact_id' => $message->toContactID
,
268 'target_contact_id' => $message->fromContactID
,
269 'activity_type_id' => $activityTypeID,
270 'activity_date_time' => date('YmdHis'),
271 'status_id' => $actStatusIDs['Completed'],
272 'details' => $message->body
,
273 'phone_number' => $message->from
,
275 if ($message->trackID
) {
276 $activityParams['result'] = CRM_Utils_Type
::escape($message->trackID
, 'String');
279 $result = CRM_Activity_BAO_Activity
::create($activityParams);
280 CRM_Core_Error
::debug_log_message("Inbound SMS recorded for cid={$message->fromContactID}.");
288 * @return mixed|string
290 public function stripPhone($phone) {
291 $newphone = preg_replace('/[^0-9x]/', '', $phone);
292 while (substr($newphone, 0, 1) == "1") {
293 $newphone = substr($newphone, 1);
295 while (strpos($newphone, "xx") !== FALSE) {
296 $newphone = str_replace("xx", "x", $newphone);
298 while (substr($newphone, -1) == "x") {
299 $newphone = substr($newphone, 0, -1);
307 * @param string $format
309 * @return mixed|string
311 public function formatPhone($phone, &$kind, $format = "dash") {
312 $phoneA = explode("x", $phone);
313 switch (strlen($phoneA[0])) {
323 $kind = $phoneA[1] ?
"LocalX" : "Local";
325 $exch = substr($phone, 0, 3);
326 $uniq = substr($phone, 3, 4);
331 $kind = $phoneA[1] ?
"LongX" : "Long";
332 $area = substr($phone, 0, 3);
333 $exch = substr($phone, 3, 3);
334 $uniq = substr($phone, 6, 4);
345 $newphone = '%' . $area . '%' . $exch . '%' . $uniq . '%' . $ext . '%';
346 $newphone = str_replace('%%', '%', $newphone);
347 $newphone = str_replace('%%', '%', $newphone);
351 $newphone = $area . "-" . $exch . "-" . $uniq . " x" . $ext;
352 $newphone = trim(trim(trim($newphone, "x"), "-"));
356 $newphone = $area . $exch . $uniq . "x" . $ext;
357 $newphone = trim(trim(trim($newphone, "x"), "-"));
373 public function urlEncode($values) {
375 foreach ($values as $key => $value) {
376 $value = urlencode($value);
377 $uri .= "&{$key}={$value}";
380 $uri = substr($uri, 1);