You are here

sms_muse.module in SMS Framework 5

File

modules/sms_muse/sms_muse.module
View source
<?php

function sms_muse_gateway_info() {
  $options['sms_muse_user'] = array(
    '#type' => 'textfield',
    '#title' => t('User'),
    '#description' => t('The username of your Muse account.'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => variable_get('sms_muse_user', ''),
    '#required' => true,
  );
  $options['sms_muse_password'] = array(
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#description' => t('The current password on your Muse account.'),
    '#size' => 30,
    '#maxlength' => 64,
    '#default_value' => variable_get('sms_muse_password', ''),
    '#required' => true,
  );
  $options['sms_muse_soap'] = array(
    '#type' => 'textfield',
    '#title' => t('SOAP'),
    '#description' => t('The SOAP access point.'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => variable_get('sms_muse_soap', ''),
    '#required' => true,
  );
  $options['sms_muse_service'] = array(
    '#type' => 'textfield',
    '#title' => t('Service name'),
    '#description' => t('The service that you would like to use with this account.'),
    '#size' => 30,
    '#maxlength' => 255,
    '#default_value' => variable_get('sms_muse_service', ''),
    '#required' => true,
  );
  drupal_add_css(drupal_get_path('module', 'sms_muse') . '/sms_muse.css');
  drupal_add_js(drupal_get_path('module', 'sms_muse') . '/sms_muse.js');
  $options['sms_muse_validate'] = array(
    '#type' => 'button',
    '#value' => t('Validate'),
    '#weight' => 10,
  );
  return array(
    'muse' => array(
      'name' => 'Muse',
      'identifier' => 'muse',
      'options' => $options,
      // 'validate' => 'sms_muse_validate',
      'send' => 'sms_muse_send',
      // 'send_form' => 'sms_muse_send_form',
      'get_callback' => 'sms_muse_get_callback',
      'set_callback' => 'sms_muse_set_callback',
    ),
  );
}
function sms_muse_send_form() {

  /* $form['country'] = array(
      '#type' => 'select',
      '#title' => t('Country'),
      '#multiple' => FALSE,
      '#options' => sms_muse_country_codes(),
      '#default_value' => -1,
      // '#required' => TRUE,
    );*/
  $form = array();
  return $form;
}
function sms_muse_send($number, $message) {
  $number = array(
    $number,
  );
  return sms_muse_command('sendmsg', array(
    'number' => $number,
    'message' => $message,
  ));
}
function sms_muse_balance() {
  return sms_muse_command('getbalance');
}
function sms_muse_set_callback($url) {
  return sms_muse_command('setcallback', array(
    'url' => $url,
  ));
}
function sms_muse_get_callback() {
  return sms_muse_command('getcallback');
}

/*
 * Execute commands against the Muse server
 */
function sms_muse_command($command = 'auth', $data = array(), $config = NULL) {
  $gateway = sms_gateways('gateway', 'muse');
  if ($config == NULL) {
    $config = $gateway['configuration'];
  }
  $usesoap = $config['sms_muse_soap'];
  $smsservicename = $config['sms_muse_service'];
  try {
    $client = new SoapClient($usesoap);
  } catch (Exception $error) {
    return array(
      'faultcode' => $error->faultcode,
      'faultstring' => $error->faultstring,
    );
  }
  switch ($command) {
    case 'auth':
      $userid = $config['sms_muse_user'];
      $pwd = $config['sms_muse_password'];
      try {
        $result = $client
          ->userAuthenticate($userid, $pwd);

        //  Remember the token returned in order to send the message
        variable_set('sms_muse_session_id', $result->AuthenticateToken);
        variable_set('sms_clickatell_session_id_timestamp', time());
        return true;
      } catch (Exception $error) {
        return array(
          'faultcode' => $error->faultcode,
          'faultstring' => $error->faultstring,
        );
      }
      break;
    case 'sendmsg':
      if (variable_get('sms_clickatell_session_id_timestamp', 0) < strtotime('-50 mins')) {
        sms_muse_command();
      }
      $user = variable_get('sms_muse_session_id', '');
      try {
        $phonenums = $client
          ->CleanPhoneList($user, $data['number'], $smsservicename);
      } catch (Exception $error) {
        return array(
          'faultcode' => $error->faultcode,
          'faultstring' => $error->faultstring,
        );
      }
      try {
        $result = $client
          ->sendSMS($user, null, $smsservicename, $data['number'], $data['message'], "TEST");
        drupal_set_message("Send message was successful.");
        return true;
      } catch (Exception $error) {
        return array(
          'faultcode' => $error->faultcode,
          'faultstring' => $error->faultstring,
        );
      }
      break;
    case 'setcallback':
      if (variable_get('sms_clickatell_session_id_timestamp', 0) < strtotime('-50 mins')) {
        sms_muse_command();
      }
      $user = variable_get('sms_muse_session_id', '');
      $url = $data['url'];
      try {
        return $client
          ->setCallBackURL($user, null, $smsservicename, $url);
      } catch (Exception $error) {
        drupal_set_message($error->faultstring);
        return array(
          'faultcode' => $error->faultcode,
          'faultstring' => $error->faultstring,
        );
      }
      break;
    case 'getcallback':
      if (variable_get('sms_clickatell_session_id_timestamp', 0) < strtotime('-50 mins')) {
        sms_muse_command();
      }
      $user = variable_get('sms_muse_session_id', '');
      try {
        return $client
          ->getCallBackURL($user, $smsservicename);
      } catch (Exception $error) {
        return $result = array(
          'faultcode' => $error->faultcode,
          'faultstring' => $error->faultstring,
        );
      }
      break;
  }
}

/** Get the session ID
 *
 */
function sms_muse_get_session_id() {
  if (variable_get('sms_muse_session_id_timestamp', 0) < strtotime('-10 mins')) {
    if ($result = sms_muse_command()) {
      if ($result['status'] == 'OK') {
        variable_set('sms_muse_session_id', $result['data']);
        variable_set('sms_muse_session_id_timestamp', time());
        watchdog('sms', t('Muse session ID refreshed: ' . $result['data']));
      }
    }
  }
  return variable_get('sms_muse_session_id', 0);
}
function sms_muse_error_codes() {
  return array(
    1 => 'AUTHENTICATION_FAULT',
    2 => 'SEND_FAULT',
    3 => 'SUBSCRIPTION_FAULT',
    4 => 'SMSSEND_FAULT',
    5 => 'SMSPUSH_FAULT',
    6 => 'CALLBACK_FAULT',
    7 => 'TRANSCODESUBMIT_FAULT',
    8 => 'PHONENUMBER_FAULT',
    9 => 'CREDIT_FAULT',
    10 => 'INVALID_COMPONENT_FAULT',
    11 => 'CONTROL_FAULT',
    12 => 'LBSQUERY_FAULT',
    13 => 'REST_FAULT',
  );
}

/**
 * Create menu callback for JS
 */
function sms_muse_menu() {
  $items[] = array(
    'path' => 'sms/in/auth',
    'type' => MENU_CALLBACK,
    'access' => TRUE,
    'callback' => sms_muse_authenticate,
  );
  return $items;
}

/**
 * Menu callback for Javascript validate.
 */
function sms_muse_authenticate() {
  $config = array(
    'sms_muse_user' => $_GET['username'],
    'sms_muse_password' => $_GET['password'],
    'sms_muse_soap' => $_GET['soap'],
    'sms_muse_service' => $_GET['service'],
  );
  $auth = sms_muse_command('auth', array(), $config);
  if ($auth == '1') {
    $result = 1;
  }
  else {
    $result = 0;
    $error = $auth['faultstring'];
  }
  print drupal_to_js(array(
    'authenticated' => $result,
    'error' => $error,
  ));
}

/**
 * Implementation of sms_$gateway_receive_parse()
 * Tells sms_receive how to parse incoming messages
 */
function sms_muse_receive_parse() {

  // hook for for sms_receive.module
  $result = array();
  $result['type'] = check_plain($_GET['Type']);

  // will be 'sms' unless with media
  $result['from'] = check_plain($_GET['From']);
  $result['text'] = check_plain($_GET['Text']);
  return $result;
}

/**
 * Implementation of sms_$gateway_receive_fields()
 * Tells sms_receive what fields are available
 */
function sms_muse_receive_fields() {
  return array(
    'type',
    'from',
    'text',
  );
}

Functions

Namesort descending Description
sms_muse_authenticate Menu callback for Javascript validate.
sms_muse_balance
sms_muse_command
sms_muse_error_codes
sms_muse_gateway_info
sms_muse_get_callback
sms_muse_get_session_id Get the session ID
sms_muse_menu Create menu callback for JS
sms_muse_receive_fields Implementation of sms_$gateway_receive_fields() Tells sms_receive what fields are available
sms_muse_receive_parse Implementation of sms_$gateway_receive_parse() Tells sms_receive how to parse incoming messages
sms_muse_send
sms_muse_send_form
sms_muse_set_callback