Skip to content
drucall.module 5.76 KiB
Newer Older
Daniel Pocock's avatar
Daniel Pocock committed
<?php



function drucall_menu() {
  $items['drucall'] = array(
    'title' => 'DruCall',
    'page callback' => 'drucall_call',
    'access callback' => TRUE,
    'expanded' => TRUE,
  );

  $items['admin/config/drucall'] = array(
   'title' => 'DruCall WebRTC',
   'description' => 'Configure DruCall.',
   'position' => 'right',
   'weight' => -15,
   'page callback' => 'system_admin_menu_block_page',
   'access arguments' => array('administer drucall'),
   'file' => 'system.admin.inc',
   'file path' => drupal_get_path('module', 'system'),
  );

  $items['admin/config/drucall/settings'] = array(
    'title' => 'Settings',
    'description' => 'Administer DruCall server settings.',
    'weight' => 0,
    'page callback' => 'drupal_get_form',
    'page arguments' => array('drucall_admin'),
    'access arguments' => array('administer drucall'),
    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
    'file' => 'drucall.admin.inc',
  );


  return $items;
}

function drucall_theme() {
  return array(
    'drucall_phone' => array(
      'render element' => 'element',
      'template' => 'drucall-phone',
    ),
  );
}

function drucall_call() {
  global $language;
  // Make sure the necessary jQuery UI components are available on
  // the page, Drupal loads core jQuery automatically but not jQuery UI
  drupal_add_library('system', 'ui.draggable');
  drupal_add_library('system', 'ui.resizable');

  // We use the `libraries' module to load javascript dependencies:
  foreach ([ "jssip", "jscommunicator", "arbiterjs", "jqueryi18nproperties", "fontawesome" ] as $libname) {
    if (!($library = libraries_detect($libname))) {
      $error = "failed to load an essential component";
      $error_msg = "failed to load $libname - please install it (check the DruCall instructions)";
    }
    if($library && empty($library['installed'])) {
      $error = $library['error'];
      $error_msg = $library['error message'];
    }
    if(isset($error)) {
      drupal_set_message($error_msg, $error, FALSE);
      return $error;
    } 
    libraries_load($libname);
  $caller_domain = variable_get('sip_domain');
  $display_name = '';
  $caller_uri = '';
  $caller_auth_user = '';
  $caller_password = '';
  $sip_register = FALSE;
  if($user->uid != 0 && !empty($caller_domain)) {
    // A user is logged in
    $display_name = $user->name;
    // FIXME: should check that Drupal username is valid for SIP
    $caller_uri = 'sip:' . $user->name . '@' . $caller_domain;
    $sip_register = TRUE;
  } else {
    // Guest user
    $display_name = variable_get('display_name');
    $caller_uri = variable_get('from_uri');
    $caller_auth_user = variable_get('auth_user');
    $caller_password = variable_get('auth_password');
  }

  $ws_cookie_secret = variable_get('ws_cookie_secret');
  $websocket_server_url = variable_get('websocket_server_url');
  $ws_cookies_in_url = variable_get('ws_cookies_in_url');
  if(!empty($ws_cookie_secret)) {
    $ws_cookie_timeout = variable_get('ws_cookie_timeout');  // seconds
    $sip_from = explode(':', $caller_uri)[1];
    $sip_to = '*@*';
    $ws_cookie_domain = variable_get('ws_cookie_domain');
    $ws_url = parse_url($websocket_server_url);
    if(empty($ws_cookie_domain)) {
      $ws_cookie_domain = $ws_url['host'];
    }

    $time_limit = REQUEST_TIME + $ws_cookie_timeout;  // seconds
    $cookie_value = '1:' . REQUEST_TIME . ':' . $time_limit . ':' . $sip_from . ':' . $sip_to;
    $cookie_value_encoded = urlencode($cookie_value);
    $extra_value = '';   // TODO - a shopping cart ID,
                         // order ID, customer ID or some other value
    // Example sending the Drupal session ID through SIP
    if(!empty($user->ssid))
      $extra_value = 'drupal:ssid:' . $user->ssid;
    else
      $extra_value = 'drupal:sid:' . $user->sid;
    $extra_value_encoded = urlencode($extra_value);
    $digest_input = $cookie_value . ':' . $extra_value;
    $cookie_mac = hash_hmac ('sha1', $digest_input, $ws_cookie_secret);

    setrawcookie("WSSessionInfo", $cookie_value_encoded, $time_limit, '/', $ws_cookie_domain);
    setrawcookie("WSSessionExtra", $extra_value_encoded, $time_limit, '/', $ws_cookie_domain);
    setrawcookie("WSSessionMAC", $cookie_mac, $time_limit, '/', $ws_cookie_domain);
    if($ws_cookies_in_url) {
      if(empty($ws_url['path']))
        $websocket_server_url = $websocket_server_url . '/';
      $websocket_server_url = $websocket_server_url . ';WSSessionInfo=' . $cookie_value_encoded . ';WSSessionExtra=' . $extra_value_encoded . ';WSSessionMAC=' . $cookie_mac;
    }
  }

Daniel Pocock's avatar
Daniel Pocock committed
  $my_settings = array(
      'mod_path' => drupal_get_path('module', 'drucall'),
Daniel Pocock's avatar
Daniel Pocock committed
      'phone_number' => variable_get('default_destination'),
      'enable_audio' => variable_get('enable_audio'),
      'enable_video' => variable_get('enable_video'),
      'enable_chat' => variable_get('enable_chat'),
      'enable_dtmf_pad' => variable_get('enable_dtmf_pad'),
      'display_name' => $display_name,
      'impi' => $caller_auth_user,
      'impu' => $caller_uri,
      'password' => $caller_password,
Daniel Pocock's avatar
Daniel Pocock committed
      'realm' => variable_get('auth_realm'),
      'websocket_server_url' => $websocket_server_url,
Daniel Pocock's avatar
Daniel Pocock committed
      'sip_outboundproxy_url' => variable_get('sip_outboundproxy_url'),
      'turn_server_url' => variable_get('turn_server_url'),
      'turn_username' => variable_get('turn_username'),
      'turn_password' => variable_get('turn_password'),
      'language_code' => $language->language,
      'sip_register' => $sip_register,
      'extra_header_value' => $extra_value,
Daniel Pocock's avatar
Daniel Pocock committed
  );

  drupal_add_js(
    array('drucall' => $my_settings),
    'setting');

  drupal_add_js(drupal_get_path('module', 'drucall') . '/js/drucall.js');
  drupal_add_css(drupal_get_path('module', 'drucall') . '/css/jscommunicator.css', array('group' => CSS_DEFAULT, 'type' => 'file'));

Daniel Pocock's avatar
Daniel Pocock committed
  return theme('drucall_phone');
}