function run()
 {
     $provider = CRM_SMS_Provider::singleton($_REQUEST);
     if (array_key_exists('status', $_REQUEST)) {
         $provider->callback();
     } elseif (array_key_exists('api_id', $_REQUEST) && array_key_exists('moMsgId', $_REQUEST)) {
         $provider->inbound();
     }
 }
Exemple #2
0
 public function run()
 {
     $provider = CRM_SMS_Provider::singleton($_REQUEST);
     if (array_key_exists('status', $_REQUEST)) {
         $provider->callback();
     } else {
         $provider->inbound();
     }
 }
 function setDefaultValues()
 {
     $defaults = array();
     $providerValues = CRM_SMS_Provider::singleton(array('provider' => 'Clickatell'));
     $defaults['api_url'] = $providerValues->_apiURL;
     if (!$this->_id) {
         $defaults['is_active'] = $defaults['is_default'] = 1;
         return $defaults;
     }
     $dao = new CRM_SMS_DAO_Provider();
     $dao->id = $this->_id;
     if (!$dao->find(TRUE)) {
         return $defaults;
     }
     CRM_Core_DAO::storeValues($dao, $defaults);
     return $defaults;
 }
Exemple #4
0
 /**
  * Set the default values of various form elements.
  *
  * @return array
  */
 public function setDefaultValues()
 {
     $defaults = array();
     $name = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE, NULL);
     if ($name) {
         $defaults['name'] = $name;
         $provider = CRM_SMS_Provider::singleton(array('provider' => $name));
         $defaults['api_url'] = $provider->_apiURL;
     }
     if (!$this->_id) {
         $defaults['is_active'] = $defaults['is_default'] = 1;
         return $defaults;
     }
     $dao = new CRM_SMS_DAO_Provider();
     $dao->id = $this->_id;
     if ($name) {
         $dao->name = $name;
     }
     if (!$dao->find(TRUE)) {
         return $defaults;
     }
     CRM_Core_DAO::storeValues($dao, $defaults);
     return $defaults;
 }
 /**
  * Send the sms message to a specific contact.
  *
  * @param int $toID
  *   The contact id of the recipient.
  * @param $tokenText
  * @param array $smsParams
  *   The params used for sending sms.
  * @param int $activityID
  *   The activity ID that tracks the message.
  * @param int $userID
  *
  * @return bool|PEAR_Error
  *   true on success or PEAR_Error object
  */
 public static function sendSMSMessage($toID, &$tokenText, $smsParams = array(), $activityID, $userID = NULL)
 {
     $toDoNotSms = "";
     $toPhoneNumber = "";
     if ($smsParams['To']) {
         $toPhoneNumber = trim($smsParams['To']);
     } elseif ($toID) {
         $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0);
         $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters);
         // To get primary mobile phonenumber,if not get the first mobile phonenumber
         if (!empty($toPhoneNumbers)) {
             $toPhoneNumerDetails = reset($toPhoneNumbers);
             $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumerDetails);
             // Contact allows to send sms
             $toDoNotSms = 0;
         }
     }
     // make sure both phone are valid
     // and that the recipient wants to receive sms
     if (empty($toPhoneNumber) or $toDoNotSms) {
         return PEAR::raiseError('Recipient phone number is invalid or recipient does not want to receive SMS', NULL, PEAR_ERROR_RETURN);
     }
     $recipient = $smsParams['To'];
     $smsParams['contact_id'] = $toID;
     $smsParams['parent_activity_id'] = $activityID;
     $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id']));
     $sendResult = $providerObj->send($recipient, $smsParams, $tokenText, NULL, $userID);
     if (PEAR::isError($sendResult)) {
         return $sendResult;
     }
     $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
     $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
     // add activity target record for every sms that is send
     $activityTargetParams = array('activity_id' => $activityID, 'contact_id' => $toID, 'record_type_id' => $targetID);
     CRM_Activity_BAO_ActivityContact::create($activityTargetParams);
     return TRUE;
 }
 /**
  * @param $fields
  * @param $mailing
  * @param $mailer
  * @param $job_date
  * @param $attachments
  *
  * @return bool|null
  * @throws Exception
  */
 public function deliverGroup(&$fields, &$mailing, &$mailer, &$job_date, &$attachments)
 {
     static $smtpConnectionErrors = 0;
     if (!is_object($mailer) || empty($fields)) {
         CRM_Core_Error::fatal();
     }
     // get the return properties
     $returnProperties = $mailing->getReturnProperties();
     $params = $targetParams = $deliveredParams = array();
     $count = 0;
     /**
      * CRM-15702: Sending bulk sms to contacts without e-mail address fails.
      * Solution is to skip checking for on hold
      */
     $skipOnHold = TRUE;
     //do include a statement to check wether e-mail address is on hold
     if ($mailing->sms_provider_id) {
         $skipOnHold = FALSE;
         //do not include a statement to check wether e-mail address is on hold
     }
     foreach ($fields as $key => $field) {
         $params[] = $field['contact_id'];
     }
     $details = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, TRUE, NULL, $mailing->getFlattenedTokens(), get_class($this), $this->id);
     $config = CRM_Core_Config::singleton();
     foreach ($fields as $key => $field) {
         $contactID = $field['contact_id'];
         if (!array_key_exists($contactID, $details[0])) {
             $details[0][$contactID] = array();
         }
         /* Compose the mailing */
         $recipient = $replyToEmail = NULL;
         $replyValue = strcmp($mailing->replyto_email, $mailing->from_email);
         if ($replyValue) {
             $replyToEmail = $mailing->replyto_email;
         }
         $message =& $mailing->compose($this->id, $field['id'], $field['hash'], $field['contact_id'], $field['email'], $recipient, FALSE, $details[0][$contactID], $attachments, FALSE, NULL, $replyToEmail);
         if (empty($message)) {
             // lets keep the message in the queue
             // most likely a permissions related issue with smarty templates
             // or a bad contact id? CRM-9833
             continue;
         }
         /* Send the mailing */
         $body =& $message->get();
         $headers =& $message->headers();
         if ($mailing->sms_provider_id) {
             $provider = CRM_SMS_Provider::singleton(array('mailing_id' => $mailing->id));
             $body = $provider->getMessage($message, $field['contact_id'], $details[0][$contactID]);
             $headers = $provider->getRecipientDetails($field, $details[0][$contactID]);
         }
         // make $recipient actually be the *encoded* header, so as not to baffle Mail_RFC822, CRM-5743
         $recipient = $headers['To'];
         $result = NULL;
         // disable error reporting on real mailings (but leave error reporting for tests), CRM-5744
         if ($job_date) {
             $errorScope = CRM_Core_TemporaryErrorScope::ignoreException();
         }
         $result = $mailer->send($recipient, $headers, $body, $this->id);
         if ($job_date) {
             unset($errorScope);
         }
         if (is_a($result, 'PEAR_Error') && !$mailing->sms_provider_id) {
             // CRM-9191
             $message = $result->getMessage();
             if (strpos($message, 'Failed to write to socket') !== FALSE || strpos($message, 'Failed to set sender') !== FALSE) {
                 // lets log this message and code
                 $code = $result->getCode();
                 CRM_Core_Error::debug_log_message("SMTP Socket Error or failed to set sender error. Message: {$message}, Code: {$code}");
                 // these are socket write errors which most likely means smtp connection errors
                 // lets skip them
                 $smtpConnectionErrors++;
                 if ($smtpConnectionErrors <= 5) {
                     continue;
                 }
                 // seems like we have too many of them in a row, we should
                 // write stuff to disk and abort the cron job
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 CRM_Core_Error::debug_log_message("Too many SMTP Socket Errors. Exiting");
                 CRM_Utils_System::civiExit();
             }
             /* Register the bounce event */
             $params = array('event_queue_id' => $field['id'], 'job_id' => $this->id, 'hash' => $field['hash']);
             $params = array_merge($params, CRM_Mailing_BAO_BouncePattern::match($result->getMessage()));
             CRM_Mailing_Event_BAO_Bounce::create($params);
         } elseif (is_a($result, 'PEAR_Error') && $mailing->sms_provider_id) {
             // Handle SMS errors: CRM-15426
             $job_id = intval($this->id);
             $mailing_id = intval($mailing->id);
             CRM_Core_Error::debug_log_message("Failed to send SMS message. Vars: mailing_id: {$mailing_id}, job_id: {$job_id}. Error message follows.");
             CRM_Core_Error::debug_log_message($result->getMessage());
         } else {
             /* Register the delivery event */
             $deliveredParams[] = $field['id'];
             $targetParams[] = $field['contact_id'];
             $count++;
             if ($count % CRM_Core_DAO::BULK_MAIL_INSERT_COUNT == 0) {
                 $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
                 $count = 0;
                 // hack to stop mailing job at run time, CRM-4246.
                 // to avoid making too many DB calls for this rare case
                 // lets do it when we snapshot
                 $status = CRM_Core_DAO::getFieldValue('CRM_Mailing_DAO_MailingJob', $this->id, 'status', 'id', TRUE);
                 if ($status != 'Running') {
                     return FALSE;
                 }
             }
         }
         unset($result);
         // seems like a successful delivery or bounce, lets decrement error count
         // only if we have smtp connection errors
         if ($smtpConnectionErrors > 0) {
             $smtpConnectionErrors--;
         }
         // If we have enabled the Throttle option, this is the time to enforce it.
         if (isset($config->mailThrottleTime) && $config->mailThrottleTime > 0) {
             usleep((int) $config->mailThrottleTime);
         }
     }
     $result = $this->writeToDB($deliveredParams, $targetParams, $mailing, $job_date);
     return $result;
 }
 function inbound()
 {
     $like = "";
     $fromPhone = $this->retrieve('From', 'String');
     return parent::processInbound($fromPhone, $this->retrieve('Body', 'String'), NULL, $this->retrieve('SmsSid', 'String'));
 }
 function inbound()
 {
     $like = "";
     $fromPhone = $this->retrieve('from', 'String');
     $fromPhone = $this->formatPhone($this->stripPhone($fromPhone), $like, "like");
     return parent::inbound($fromPhone, $this->retrieve('text', 'String'), NULL, $this->retrieve('moMsgId', 'String'));
 }
 /**
  * send the sms message to a specific contact
  *
  * @param int    $toID              the contact id of the recipient
  * @param int    $activityID        the activity ID that tracks the message
  * @param array  $smsParams         the params used for sending sms
  *
  * @return boolean                  true if successfull else false.
  * @access public
  * @static
  */
 static function sendSMSMessage($toID, &$tokenText, &$tokenHtml, $smsParams = array(), $activityID)
 {
     $toDoNotSms = "";
     $toPhoneNumber = "";
     if ($smsParams['To']) {
         $toPhoneNumber = trim($smsParams['To']);
     } elseif ($toID) {
         $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0);
         $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters);
         //to get primary mobile ph,if not get a first mobile ph
         if (!empty($toPhoneNumbers)) {
             $toPhoneNumerDetails = reset($toPhoneNumbers);
             $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumerDetails);
             //contact allows to send sms
             $toDoNotSms = 0;
         }
     }
     // make sure both phone are valid
     // and that the recipient wants to receive sms
     if (empty($toPhoneNumber) or $toDoNotSms) {
         return FALSE;
     }
     $message = $tokenHtml ? $tokenHtml : $tokenText;
     $recipient = $smsParams['To'];
     $smsParams['contact_id'] = $toID;
     $smsParams['parent_activity_id'] = $activityID;
     $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id']));
     if (!$providerObj->send($recipient, $smsParams, $message, NULL)) {
         return FALSE;
     }
     // add activity target record for every sms that is send
     $activityTargetParams = array('activity_id' => $activityID, 'target_contact_id' => $toID);
     self::createActivityTarget($activityTargetParams);
     return TRUE;
 }