+++ /dev/null
-<?php
-
-// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
-
-/**
- * (Delegated) Implementation of hook_civicrm_config
- */
-function _clickatell_civix_civicrm_config(&$config = NULL) {
- static $configured = FALSE;
- if ($configured) return;
- $configured = TRUE;
-
- $template =& CRM_Core_Smarty::singleton();
-
- $extRoot = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
- $extDir = $extRoot . 'templates';
-
- if ( is_array( $template->template_dir ) ) {
- array_unshift( $template->template_dir, $extDir );
- } else {
- $template->template_dir = array( $extDir, $template->template_dir );
- }
-
- $include_path = $extRoot . PATH_SEPARATOR . get_include_path( );
- set_include_path( $include_path );
-}
-
-/**
- * (Delegated) Implementation of hook_civicrm_xmlMenu
- *
- * @param $files array(string)
- */
-function _clickatell_civix_civicrm_xmlMenu(&$files) {
- foreach (glob(__DIR__ . '/xml/Menu/*.xml') as $file) {
- $files[] = $file;
- }
-}
-
-/**
- * Implementation of hook_civicrm_install
- */
-function _clickatell_civix_civicrm_install() {
- _clickatell_civix_civicrm_config();
- if ($upgrader = _clickatell_civix_upgrader()) {
- return $upgrader->onInstall();
- }
-}
-
-/**
- * Implementation of hook_civicrm_uninstall
- */
-function _clickatell_civix_civicrm_uninstall() {
- _clickatell_civix_civicrm_config();
- if ($upgrader = _clickatell_civix_upgrader()) {
- return $upgrader->onUninstall();
- }
-}
-
-/**
- * (Delegated) Implementation of hook_civicrm_enable
- */
-function _clickatell_civix_civicrm_enable() {
- _clickatell_civix_civicrm_config();
- if ($upgrader = _clickatell_civix_upgrader()) {
- if (is_callable(array($upgrader, 'onEnable'))) {
- return $upgrader->onEnable();
- }
- }
-}
-
-/**
- * (Delegated) Implementation of hook_civicrm_disable
- */
-function _clickatell_civix_civicrm_disable() {
- _clickatell_civix_civicrm_config();
- if ($upgrader = _clickatell_civix_upgrader()) {
- if (is_callable(array($upgrader, 'onDisable'))) {
- return $upgrader->onDisable();
- }
- }
-}
-
-/**
- * (Delegated) Implementation of hook_civicrm_upgrade
- *
- * @param $op string, the type of operation being performed; 'check' or 'enqueue'
- * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
- *
- * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
- * for 'enqueue', returns void
- */
-function _clickatell_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
- if ($upgrader = _clickatell_civix_upgrader()) {
- return $upgrader->onUpgrade($op, $queue);
- }
-}
-
-/**
- * @return null
- */
-function _clickatell_civix_upgrader() {
- if (!file_exists(__DIR__.'/CRM/Clickatell/Upgrader.php')) {
- return NULL;
- } else {
- return CRM_Clickatell_Upgrader_Base::instance();
- }
-}
-
-/**
- * Search directory tree for files which match a glob pattern
- *
- * @param $dir string, base dir
- * @param $pattern string, glob pattern, eg "*.txt"
- * @return array(string)
- */
-function _clickatell_civix_find_files($dir, $pattern) {
- $todos = array($dir);
- $result = array();
- while (!empty($todos)) {
- $subdir = array_shift($todos);
- foreach (glob("$subdir/$pattern") as $match) {
- if (!is_dir($match)) {
- $result[] = $match;
- }
- }
- if ($dh = opendir($subdir)) {
- while (FALSE !== ($entry = readdir($dh))) {
- $path = $subdir . DIRECTORY_SEPARATOR . $entry;
- if ($entry == '.' || $entry == '..') {
- } elseif (is_dir($path)) {
- $todos[] = $path;
- }
- }
- closedir($dh);
- }
- }
- return $result;
-}
-/**
- * (Delegated) Implementation of hook_civicrm_managed
- *
- * Find any *.mgd.php files, merge their content, and return.
- */
-function _clickatell_civix_civicrm_managed(&$entities) {
- $mgdFiles = _clickatell_civix_find_files(__DIR__, '*.mgd.php');
- foreach ($mgdFiles as $file) {
- $es = include $file;
- foreach ($es as $e) {
- if (empty($e['module'])) {
- $e['module'] = 'org.civicrm.sms.clickatell';
- }
- $entities[] = $e;
- }
- }
-}
+++ /dev/null
-<?php
-
-require_once 'clickatell.civix.php';
-
-/**
- * Implementation of hook_civicrm_config
- */
-function clickatell_civicrm_config(&$config) {
- _clickatell_civix_civicrm_config($config);
-}
-
-/**
- * Implementation of hook_civicrm_xmlMenu
- *
- * @param $files array(string)
- */
-function clickatell_civicrm_xmlMenu(&$files) {
- _clickatell_civix_civicrm_xmlMenu($files);
-}
-
-/**
- * Implementation of hook_civicrm_install
- */
-function clickatell_civicrm_install() {
- $groupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup','sms_provider_name','id','name');
- $params =
- array('option_group_id' => $groupID,
- 'label' => 'Clickatell',
- 'value' => 'org.civicrm.sms.clickatell',
- 'name' => 'clickatell',
- 'is_default' => 1,
- 'is_active' => 1,
- 'version' => 3,);
- require_once 'api/api.php';
- civicrm_api( 'option_value','create', $params );
-
- return _clickatell_civix_civicrm_install();
-}
-
-/**
- * Implementation of hook_civicrm_uninstall
- */
-function clickatell_civicrm_uninstall() {
- $optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','clickatell','id','name');
- if ($optionID)
- CRM_Core_BAO_OptionValue::del($optionID);
-
- $filter = array('name' => 'org.civicrm.sms.clickatell');
- $Providers = CRM_SMS_BAO_Provider::getProviders(False, $filter, False);
- if ($Providers){
- foreach($Providers as $key => $value){
- CRM_SMS_BAO_Provider::del($value['id']);
- }
- }
- return _clickatell_civix_civicrm_uninstall();
-}
-
-/**
- * Implementation of hook_civicrm_enable
- */
-function clickatell_civicrm_enable() {
- $optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','clickatell' ,'id','name');
- if ($optionID)
- CRM_Core_BAO_OptionValue::setIsActive($optionID, TRUE);
-
- $filter = array('name' => 'org.civicrm.sms.clickatell');
- $Providers = CRM_SMS_BAO_Provider::getProviders(False, $filter, False);
- if ($Providers){
- foreach($Providers as $key => $value){
- CRM_SMS_BAO_Provider::setIsActive($value['id'], TRUE);
- }
- }
- return _clickatell_civix_civicrm_enable();
-}
-
-/**
- * Implementation of hook_civicrm_disable
- */
-function clickatell_civicrm_disable() {
- $optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','clickatell','id','name');
- if ($optionID)
- CRM_Core_BAO_OptionValue::setIsActive($optionID, FALSE);
-
- $filter = array('name' => 'org.civicrm.sms.clickatell');
- $Providers = CRM_SMS_BAO_Provider::getProviders(False, $filter, False);
- if ($Providers){
- foreach($Providers as $key => $value){
- CRM_SMS_BAO_Provider::setIsActive($value['id'], FALSE);
- }
- }
- return _clickatell_civix_civicrm_disable();
-}
-
-/**
- * Implementation of hook_civicrm_upgrade
- *
- * @param $op string, the type of operation being performed; 'check' or 'enqueue'
- * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
- *
- * @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
- * for 'enqueue', returns void
- */
-function clickatell_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
- return _clickatell_civix_civicrm_upgrade($op, $queue);
-}
-
-/**
- * Implementation of hook_civicrm_managed
- *
- * Generate a list of entities to create/deactivate/delete when this module
- * is installed, disabled, uninstalled.
- */
-function clickatell_civicrm_managed(&$entities) {
- return _clickatell_civix_civicrm_managed($entities);
-}
+++ /dev/null
-<?php
-/*
- +--------------------------------------------------------------------+
- | CiviCRM version 4.5 |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014 |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM. |
- | |
- | CiviCRM is free software; you can copy, modify, and distribute it |
- | under the terms of the GNU Affero General Public License |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
- | |
- | CiviCRM is distributed in the hope that it will be useful, but |
- | WITHOUT ANY WARRANTY; without even the implied warranty of |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
- | See the GNU Affero General Public License for more details. |
- | |
- | You should have received a copy of the GNU Affero General Public |
- | License and the CiviCRM Licensing Exception along |
- | with this program; if not, contact CiviCRM LLC |
- | at info[AT]civicrm[DOT]org. If you have questions about the |
- | GNU Affero General Public License or the licensing of CiviCRM, |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing |
- +--------------------------------------------------------------------+
-*/
-
-/**
- *
- * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
- * $Id$
- *
- */
-class org_civicrm_sms_clickatell extends CRM_SMS_Provider {
-
- /**
- * api type to use to send a message
- * @var string
- */
- protected $_apiType = 'http';
-
- /**
- * provider details
- * @var string
- */
- protected $_providerInfo = array();
-
- /**
- * Clickatell API Server Session ID
- *
- * @var string
- */
- protected $_sessionID = NULL;
-
- /**
- * Curl handle resource id
- *
- */
- protected $_ch;
-
- /**
- * Temporary file resource id
- * @var resource
- */
- protected $_fp;
-
- public $_apiURL = "https://api.clickatell.com";
-
- protected $_messageType = array(
- 'SMS_TEXT',
- 'SMS_FLASH',
- 'SMS_NOKIA_OLOGO',
- 'SMS_NOKIA_GLOGO',
- 'SMS_NOKIA_PICTURE',
- 'SMS_NOKIA_RINGTONE',
- 'SMS_NOKIA_RTTL',
- 'SMS_NOKIA_CLEAN',
- 'SMS_NOKIA_VCARD',
- 'SMS_NOKIA_VCAL',
- );
-
- protected $_messageStatus = array(
- '001' => 'Message unknown',
- '002' => 'Message queued',
- '003' => 'Delivered',
- '004' => 'Received by recipient',
- '005' => 'Error with message',
- '006' => 'User cancelled message delivery',
- '007' => 'Error delivering message',
- '008' => 'OK',
- '009' => 'Routing error',
- '010' => 'Message expired',
- '011' => 'Message queued for later delivery',
- '012' => 'Out of credit',
- );
-
- /**
- * We only need one instance of this object. So we use the singleton
- * pattern and cache the instance in this variable
- *
- * @var object
- * @static
- */
- static private $_singleton = array();
-
- /**
- * Constructor
- *
- * Create and auth a Clickatell session.
- *
- * @param array $provider
- * @param bool $skipAuth
- *
- * @return \org_civicrm_sms_clickatell
- */
- function __construct($provider = array( ), $skipAuth = FALSE) {
- // initialize vars
- $this->_apiType = CRM_Utils_Array::value('api_type', $provider, 'http');
- $this->_providerInfo = $provider;
-
- if ($skipAuth) {
- return TRUE;
- }
-
- // first create the curl handle
-
- /**
- * Reuse the curl handle
- */
- $this->_ch = curl_init();
- if (!$this->_ch || !is_resource($this->_ch)) {
- return PEAR::raiseError('Cannot initialise a new curl handle.');
- }
-
- curl_setopt($this->_ch, CURLOPT_TIMEOUT, 20);
- curl_setopt($this->_ch, CURLOPT_VERBOSE, 1);
- curl_setopt($this->_ch, CURLOPT_FAILONERROR, 1);
- if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off') {
- curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, 1);
- }
- curl_setopt($this->_ch, CURLOPT_COOKIEJAR, "/dev/null");
- curl_setopt($this->_ch, CURLOPT_SSL_VERIFYHOST, 2);
- curl_setopt($this->_ch, CURLOPT_USERAGENT, 'CiviCRM - http://civicrm.org/');
-
- $this->authenticate();
- }
-
- /**
- * singleton function used to manage this object
- *
- * @param array $providerParams
- * @param bool $force
- * @return object
- * @static
- */
- static function &singleton($providerParams = array(
- ), $force = FALSE) {
- $providerID = CRM_Utils_Array::value('provider_id', $providerParams);
- $skipAuth = $providerID ? FALSE : TRUE;
- $cacheKey = (int) $providerID;
-
- if (!isset(self::$_singleton[$cacheKey]) || $force) {
- $provider = array();
- if ($providerID) {
- $provider = CRM_SMS_BAO_Provider::getProviderInfo($providerID);
- }
- self::$_singleton[$cacheKey] = new org_civicrm_sms_clickatell($provider, $skipAuth);
- }
- return self::$_singleton[$cacheKey];
- }
-
- /**
- * Authenticate to the Clickatell API Server.
- *
- * @return mixed true on sucess or PEAR_Error object
- * @access public
- * @since 1.1
- */
- function authenticate() {
- $url = $this->_providerInfo['api_url'] . "/http/auth";
-
- $postDataArray = array(
- 'user' => $this->_providerInfo['username'],
- 'password' => $this->_providerInfo['password'],
- 'api_id' => $this->_providerInfo['api_params']['api_id']
- );
-
- if (array_key_exists('is_test', $this->_providerInfo['api_params']) &&
- $this->_providerInfo['api_params']['is_test'] == 1 ) {
- $response = array('data' => 'OK:' . rand());
- } else {
- $postData = $this->urlEncode($postDataArray);
- $response = $this->curl($url, $postData);
- }
- if (PEAR::isError($response)) {
- return $response;
- }
- $sess = explode(":", $response['data']);
-
- $this->_sessionID = trim($sess[1]);
-
- if ($sess[0] == "OK") {
- return TRUE;
- }
- else {
- return PEAR::raiseError($response['data']);
- }
- }
-
- /**
- * @param $url
- * @param $postDataArray
- * @param null $id
- *
- * @return object|string
- */
- function formURLPostData($url, &$postDataArray, $id = NULL) {
- $url = $this->_providerInfo['api_url'] . $url;
- $postDataArray['session_id'] = $this->_sessionID;
- if ($id) {
- if (strlen($id) < 32 || strlen($id) > 32) {
- return PEAR::raiseError('Invalid API Message Id');
- }
- $postDataArray['apimsgid'] = $id;
- }
- return $url;
- }
-
- /**
- * Send an SMS Message via the Clickatell API Server
- *
- * @param $recipients
- * @param $header
- * @param $message
- * @param null $jobID
- * @param null $userID
- * @internal param \the $array message with a to/from/text
- *
- * @return mixed true on sucess or PEAR_Error object
- * @access public
- */
- function send($recipients, $header, $message, $jobID = NULL, $userID = NULL) {
- if ($this->_apiType == 'http') {
- $postDataArray = array( );
- $url = $this->formURLPostData("/http/sendmsg", $postDataArray);
-
- if (array_key_exists('from', $this->_providerInfo['api_params'])) {
- $postDataArray['from'] = $this->_providerInfo['api_params']['from'];
- }
- if (array_key_exists('concat', $this->_providerInfo['api_params'])) {
- $postDataArray['concat'] = $this->_providerInfo['api_params']['concat'];
- }
- //TODO:
- $postDataArray['to'] = $header['To'];
- $postDataArray['text'] = utf8_decode(substr($message, 0, 460)); // max of 460 characters, is probably not multi-lingual
- if (array_key_exists('mo', $this->_providerInfo['api_params'])) {
- $postDataArray['mo'] = $this->_providerInfo['api_params']['mo'];
- }
- // sendmsg with callback request:
- $postDataArray['callback'] = 3;
-
- $isTest = 0;
- if (array_key_exists('is_test', $this->_providerInfo['api_params']) &&
- $this->_providerInfo['api_params']['is_test'] == 1
- ) {
- $isTest = 1;
- }
-
- /**
- * Check if we are using a queue when sending as each account
- * with Clickatell is assigned three queues namely 1, 2 and 3.
- */
- if (isset($header['queue']) && is_numeric($header['queue'])) {
- if (in_array($header['queue'], range(1, 3))) {
- $postDataArray['queue'] = $header['queue'];
- }
- }
-
- /**
- * Must we escalate message delivery if message is stuck in
- * the queue at Clickatell?
- */
- if (isset($header['escalate']) && !empty($header['escalate'])) {
- if (is_numeric($header['escalate'])) {
- if (in_array($header['escalate'], range(1, 2))) {
- $postDataArray['escalate'] = $header['escalate'];
- }
- }
- }
-
- if ($isTest == 1) {
- $response = array('data' => 'ID:' . rand());
- }
- else {
- $postData = $this->urlEncode($postDataArray);
- $response = $this->curl($url, $postData);
- }
- if (PEAR::isError($response)) {
- return $response;
- }
- $send = explode(":", $response['data']);
-
- if ($send[0] == "ID") {
- $this->createActivity($send[1], $message, $header, $jobID, $userID);
- return $send[1];
- }
- else {
- // TODO: Should add a failed activity instead.
- CRM_Core_Error::debug_log_message($response['data'] . " - for phone: {$postDataArray['to']}");
- return PEAR::raiseError($response['data'], null, PEAR_ERROR_RETURN);
- }
- }
- }
-
- /**
- * @return bool
- */
- function callback() {
- $apiMsgID = $this->retrieve('apiMsgId', 'String');
-
- $activity = new CRM_Activity_DAO_Activity();
- $activity->result = $apiMsgID;
-
- if ($activity->find(TRUE)) {
- $actStatusIDs = array_flip(CRM_Core_OptionGroup::values('activity_status'));
-
- $status = $this->retrieve('status', 'String');
- switch ($status) {
- case "001":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Message Unknown";
- break;
-
- case "002":
- $statusID = $actStatusIDs['Scheduled'];
- $clickStat = $this->_messageStatus[$status] . " - Message Queued";
- break;
-
- case "003":
- $statusID = $actStatusIDs['Completed'];
- $clickStat = $this->_messageStatus[$status] . " - Delivered to Gateway";
- break;
-
- case "004":
- $statusID = $actStatusIDs['Completed'];
- $clickStat = $this->_messageStatus[$status] . " - Received by Recipient";
- break;
-
- case "005":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Error with Message";
- break;
-
- case "006":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - User cancelled message";
- break;
-
- case "007":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Error delivering message";
- break;
-
- case "008":
- $statusID = $actStatusIDs['Completed'];
- $clickStat = $this->_messageStatus[$status] . " - Ok, Message Received by Gateway";
- break;
-
- case "009":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Routing Error";
- break;
-
- case "010":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Message Expired";
- break;
-
- case "011":
- $statusID = $actStatusIDs['Scheduled'];
- $clickStat = $this->_messageStatus[$status] . " - Message Queued for Later";
- break;
-
- case "012":
- $statusID = $actStatusIDs['Cancelled'];
- $clickStat = $this->_messageStatus[$status] . " - Out of Credit";
- break;
- }
-
- if ($statusID) {
- // update activity with status + msg in location
- $activity->status_id = $statusID;
- $activity->location = $clickStat;
- $activity->activity_date_time = CRM_Utils_Date::isoToMysql($activity->activity_date_time);
- $activity->save();
- CRM_Core_Error::debug_log_message("SMS Response updated for apiMsgId={$apiMsgID}.");
- return TRUE;
- }
- }
-
- // if no update is done
- CRM_Core_Error::debug_log_message("Could not update SMS Response for apiMsgId={$apiMsgID}.");
- return FALSE;
- }
-
- /**
- * @return $this|null|object
- */
- function inbound() {
- $like = "";
- $fromPhone = $this->retrieve('from', 'String');
- $fromPhone = $this->formatPhone($this->stripPhone($fromPhone), $like, "like");
-
- return parent::processInbound($fromPhone, $this->retrieve('text', 'String'), NULL, $this->retrieve('moMsgId', 'String'));
- }
-
- /**
- * Perform curl stuff
- *
- * @param string URL to call
- * @param string HTTP Post Data
- *
- * @return mixed HTTP response body or PEAR Error Object
- * @access private
- */
- function curl($url, $postData) {
- $this->_fp = tmpfile();
-
- curl_setopt($this->_ch, CURLOPT_URL, $url);
- curl_setopt($this->_ch, CURLOPT_POST, 1);
- curl_setopt($this->_ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($this->_ch, CURLOPT_FILE, $this->_fp);
-
- $status = curl_exec($this->_ch);
- $response['http_code'] = curl_getinfo($this->_ch, CURLINFO_HTTP_CODE);
-
- if (empty($response['http_code'])) {
- return PEAR::raiseError('No HTTP Status Code was returned.');
- }
- elseif ($response['http_code'] === 0) {
- return PEAR::raiseError('Cannot connect to the Clickatell API Server.');
- }
-
- if ($status) {
- $response['error'] = curl_error($this->_ch);
- $response['errno'] = curl_errno($this->_ch);
- }
-
- rewind($this->_fp);
-
- $pairs = "";
- while ($str = fgets($this->_fp, 4096)) {
- $pairs .= $str;
- }
- fclose($this->_fp);
-
- $response['data'] = $pairs;
- unset($pairs);
- asort($response);
-
- return ($response);
- }
-}
-