Skip to content
Snippets Groups Projects
Commit ae240180 authored by Deepak Srivastava's avatar Deepak Srivastava
Browse files

CRM-12399, towards moving twilio extension to github as separate repo.

parent d3899d7c
Branches
Tags
No related merge requests found
info.xml 0 → 100644
<?xml version="1.0"?>
<extension key="org.civicrm.sms.twilio" type="module">
<downloadUrl>https://github.com/civicrm/org.civicrm.sms.twilio/archive/master.zip</downloadUrl>
<file>twilio</file>
<name>twilio</name>
<description>Twilio integration allows delivering short message service (SMS) messages through its Twilio Gateway to mobile phone users.</description>
<urls>
<url desc="Main Extension Page">http://civicrm.org</url>
<url desc="Documentation">http://wiki.civicrm.org/confluence/display/CRMDOC/Setting+up+a+SMS+Provider+for+CiviSMS</url>
<url desc="Support">http://forum.civicrm.org</url>
<url desc="Licensing">http://civicrm.org/licensing</url>
</urls>
<license>AGPL</license>
<maintainer>
<author>Community Contributed</author>
<email>info@civicrm.org</email>
</maintainer>
<releaseDate>2012-11-19</releaseDate>
<version>1.0</version>
<develStage>stable</develStage>
<compatibility>
<ver>4.3</ver>
</compatibility>
<comments>For support, please contact project team on the forums.</comments>
</extension>
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.2 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2012 |
+--------------------------------------------------------------------+
| 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 |
+--------------------------------------------------------------------+
*/
// Load the official Twilio library
require_once 'Services/Twilio.php';
/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2012
* $Id$
*
*/
class org_civicrm_sms_twilio extends CRM_SMS_Provider {
/**
* api type to use to send a message
* @var string
*/
protected $_apiType = 'http';
/**
* provider details
* @var string
*/
protected $_providerInfo = array();
public $_apiURL = "https://api.twilio.com/";
protected $_messageType = array(
);
protected $_messageStatus = array(
);
/**
* Twilio client object
* @var Service_Twilio
*/
protected $_twilioClient = null;
/**
* 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 Twilio session.
* This is not needed for Twilio
*
* @return void
*/
function __construct($provider = array(
), $skipAuth = TRUE) {
// initialize vars
$this->_apiType = CRM_Utils_Array::value('api_type', $provider, 'http');
$this->_providerInfo = $provider;
// Instantiate the Twilio client
if ($this->_apiType == 'http' &&
array_key_exists('username', $this->_providerInfo) &&
array_key_exists('password', $this->_providerInfo)
) {
$sid = $this->_providerInfo['username'];
$token = $this->_providerInfo['password'];
$this->_twilioClient = new Services_Twilio($sid, $token);
}
if ($skipAuth) {
return TRUE;
}
$this->authenticate();
}
/**
* singleton function used to manage this object
*
* @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_twilio($provider, $skipAuth);
}
return self::$_singleton[$cacheKey];
}
/**
* Authenticate to the Twilio Server.
* Not needed in Twilio
* @return boolean TRUE
* @access public
* @since 1.1
*/
function authenticate() {
return (TRUE);
}
/**
* Send an SMS Message via the Twilio API Server
*
* @param array the message with a to/from/text
*
* @return mixed SID on success or PEAR_Error object
* @access public
*/
function send($recipients, $header, $message, $jobID = NULL, $userID = NULL) {
if ($this->_apiType == 'http') {
$from = '';
if (array_key_exists('From', $this->_providerInfo['api_params'])) {
$from = $this->_providerInfo['api_params']['From'];
}
try {
$twilioMessage = $this->_twilioClient->account->sms_messages->create(
$from,
$header['To'],
$message
);
} catch (Exception $e) {
$errMsg = $e->getMessage()
. ' For more information, see '
. $e->getInfo();
return PEAR::raiseError(
$errMsg,
$e->getCode(),
PEAR_ERROR_RETURN
);
}
$sid = $twilioMessage->sid;
$this->createActivity($sid, $message, $header, $jobID, $userID);
return $sid;
}
}
function callback() {
return TRUE;
}
function inbound() {
$like = "";
$fromPhone = $this->retrieve('From', 'String');
return parent::processInbound($fromPhone, $this->retrieve('Body', 'String'), NULL, $this->retrieve('SmsSid', 'String'));
}
}
<?php
// AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file
/**
* (Delegated) Implementation of hook_civicrm_config
*/
function _twilio_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 _twilio_civix_civicrm_xmlMenu(&$files) {
foreach (glob(__DIR__ . '/xml/Menu/*.xml') as $file) {
$files[] = $file;
}
}
/**
* Implementation of hook_civicrm_install
*/
function _twilio_civix_civicrm_install() {
_twilio_civix_civicrm_config();
if ($upgrader = _twilio_civix_upgrader()) {
return $upgrader->onInstall();
}
}
/**
* Implementation of hook_civicrm_uninstall
*/
function _twilio_civix_civicrm_uninstall() {
_twilio_civix_civicrm_config();
if ($upgrader = _twilio_civix_upgrader()) {
return $upgrader->onUninstall();
}
}
/**
* (Delegated) Implementation of hook_civicrm_enable
*/
function _twilio_civix_civicrm_enable() {
_twilio_civix_civicrm_config();
if ($upgrader = _twilio_civix_upgrader()) {
if (is_callable(array($upgrader, 'onEnable'))) {
return $upgrader->onEnable();
}
}
}
/**
* (Delegated) Implementation of hook_civicrm_disable
*/
function _twilio_civix_civicrm_disable() {
_twilio_civix_civicrm_config();
if ($upgrader = _twilio_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 _twilio_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
if ($upgrader = _twilio_civix_upgrader()) {
return $upgrader->onUpgrade($op, $queue);
}
}
function _twilio_civix_upgrader() {
if (!file_exists(__DIR__.'/CRM/Twilio/Upgrader.php')) {
return NULL;
} else {
return CRM_Twilio_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 _twilio_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 _twilio_civix_civicrm_managed(&$entities) {
$mgdFiles = _twilio_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.twilio';
}
$entities[] = $e;
}
}
}
<?php
require_once 'twilio.civix.php';
/**
* Implementation of hook_civicrm_config
*/
function twilio_civicrm_config(&$config) {
_twilio_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function twilio_civicrm_xmlMenu(&$files) {
_twilio_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function twilio_civicrm_install() {
$groupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup','sms_provider_name','id','name');
$params =
array('option_group_id' => $groupID,
'label' => 'Twilio',
'value' => 'org.civicrm.sms.twilio',
'name' => 'twilio',
'is_default' => 1,
'is_active' => 1,
'version' => 3,);
require_once 'api/api.php';
civicrm_api( 'option_value','create', $params );
return _twilio_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function twilio_civicrm_uninstall() {
$optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','twilio','id','name');
if ($optionID)
CRM_Core_BAO_OptionValue::del($optionID);
$filter = array('name' => 'org.civicrm.sms.twilio');
$Providers = CRM_SMS_BAO_Provider::getProviders(False, $filter, False);
if ($Providers){
foreach($Providers as $key => $value){
CRM_SMS_BAO_Provider::del($value['id']);
}
}
return _twilio_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function twilio_civicrm_enable() {
$optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','twilio' ,'id','name');
if ($optionID)
CRM_Core_BAO_OptionValue::setIsActive($optionID, TRUE);
$filter = array('name' => 'org.civicrm.sms.twilio');
$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 _twilio_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function twilio_civicrm_disable() {
$optionID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue','twilio','id','name');
if ($optionID)
CRM_Core_BAO_OptionValue::setIsActive($optionID, FALSE);
$filter = array('name' => 'org.civicrm.sms.twilio');
$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 _twilio_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 twilio_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _twilio_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 twilio_civicrm_managed(&$entities) {
return _twilio_civix_civicrm_managed($entities);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment