Skip to content
mebeam_chat.module 11.2 KiB
Newer Older
regx's avatar
regx committed
<?php
regx's avatar
regx committed
// $Id$
regx's avatar
regx committed

/**
 * @file: mebeam_chat.module
regx's avatar
regx committed
 * Adds a MeBeam audio/Video/Text chat page to your Drupal Site.
regx's avatar
regx committed
 * @author: Gary Varnell <gary@dgswa.com>
 * @copyright: 2009 DGS
*/


#================================================================================
define('MEBEAM_CHAT_DEBUG', 1);

/**
 * Implementation of hook_help
 */
regx's avatar
regx committed
function mebeam_chat_help($section) {
  switch ($section) {
    case 'admin/help#mebeam_chat':
      $output = '<p>'. t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.') .'</p>';
      return $output;
      break;
    case 'admin/modules#description':
      return t('Adds a MeBeam audio/Video/Text chat page to your Drupal Site.');
      break;
  }
regx's avatar
regx committed
}
//-----------------------------------------

/**
 * Implementation of hook_perm
 */
regx's avatar
regx committed
function mebeam_chat_perm() {
regx's avatar
regx committed
  return array('view_mebeam_chat');
}
//-----------------------------------------



/**
 * Implementation of hook_menu().
 */
function mebeam_chat_menu($may_cache) {
regx's avatar
regx committed
  
  if (!$may_cache) {
    # non-cachable menus
regx's avatar
regx committed
  }
  else{
    # cachable menu items
    # admin menus
    $items[] = array( 'path' => 'admin/settings/mebeam-chat',
regx's avatar
regx committed
                      'title' => t('MeBeam Chat Settings'),
                      'description' => t('Configure MeBeam Chat Settings.'),
                      'access' => user_access('administer nodes'),
                      'callback' => 'mebeam_chat_show_settings_form',
                      'type' => MENU_NORMAL_ITEM,
regx's avatar
regx committed
                    );
    # user menus
    $items[] = array( 'path' => 'mebeam-chat',
                      'title' => t('MeBeam Chat'),
                      'callback' => 'mebeam_chat_view_chat',
                      'access' => user_access('view_mebeam_chat'),
                      'type' => MENU_NORMAL_ITEM,
                    );
  }
  return $items;
}
//-----------------------------------------


/**
 * Implementation of hook_cron
 */
regx's avatar
regx committed
function mebeam_chat_cron() {
  $mebeam_chat_rm_name = variable_get('mebeam_chat_rm_name', '');
  if (variable_get('mebeam_chat_rm_name_random', 0)) {
    $mebeam_chat_rm_name = mebeam_chat_randomize_name($mebeam_chat_rm_name);
    variable_set('mebeam_chat_rm_rname', $mebeam_chat_rm_name);
  }
regx's avatar
regx committed
}
//-----------------------------------------

regx's avatar
regx committed
function mebeam_chat_randomize_name($name) {
regx's avatar
regx committed
  # generate a unique room name
  $id = uniqid();
  $id = md5($id);
  $name = "$name-$id";
regx's avatar
regx committed
  $name = substr($name, 0, 15);
regx's avatar
regx committed
  return $name;
}
//-----------------------------------------

regx's avatar
regx committed
function mebeam_chat_view_chat() {
    $default = mebeam_chat_get_defaults();
regx's avatar
regx committed
    $pg_title = variable_get('mebeam_chat_pg_title', $default['pg_title']);
    if (!empty($pg_title)) {
      drupal_set_title($pg_title);
    }
regx's avatar
regx committed
    
regx's avatar
regx committed
    $pg_header = variable_get('mebeam_chat_pg_header', $default['pg_header']);
    $pg_footer = variable_get('mebeam_chat_pg_footer', $default['pg_footer']);
regx's avatar
regx committed
    $rm_name = variable_get('mebeam_chat_rm_name', $default['rm_name']);
    if (variable_get('mebeam_chat_rm_name_random', 0)) {
      $rm_name = variable_get('mebeam_chat_rm_rname', substr(time(),0,10));
regx's avatar
regx committed
    }
    # clean up room name
regx's avatar
regx committed
    $rm_name = preg_replace('|[^\d\w-]|', '', $rm_name);
    $rm_name = substr($rm_name, 0, 15);
    $rm_show = variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']);
    if ($rm_show) {
      $rm_show = "<div class='room_name'>$rm_name</div>";
    }
    else{
      $rm_show = '';
    }
regx's avatar
regx committed
    $rm_width = variable_get('mebeam_chat_rm_width', $default['rm_width']);
    $rm_height = variable_get('mebeam_chat_rm_height', $default['rm_height']);
regx's avatar
regx committed
    
  $output = <<<EOF
    $pg_header
 <center>
 <div id='mebeam-chat'>
    $rm_show
regx's avatar
regx committed
 <center>
 <div id='hotroom'>
 <object  width='$rm_width' height='$rm_height' id='uhzy' align='middle'>
 <param name='allowScriptAccess' value='never' >
 <param name='movie' value='http://www.mebeam.com/mebeam.swf'>
 <param name='quality' value='best' >
 <param name='scale' value='noscale' >
 <param name='salign' value='t' >
 <param name='flashvars' value='room=$rm_name'>
 <embed flashvars='room=$rm_name' src='http://www.mebeam.com/mebeam.swf'
  quality='best' scale='noscale' salign='t'
  width='$rm_width' height='$rm_height' name='uhzy' align='middle'
  allowScriptAccess='never'
  type='application/x-shockwave-flash'
  pluginspage='http://www.macromedia.com/go/getflashplayer' >
 </object>
 </center>
 </div>
regx's avatar
regx committed
EOF;

  return $output;
}

regx's avatar
regx committed
function mebeam_chat_get_defaults() {
 #so we don't have defaults spread around - user might not goto module settings
regx's avatar
regx committed
 $default['rm_name'] = variable_get('site_name', 'undefined') .'-chat';
 $default['pg_title'] = variable_get('site_name', '') .'Live Chat';
 $default['pg_header'] = t("Provided by <a href=\"http://www.mebeam.com\" target=\"_blank\">MeBeam.com</a>.<br/>
                            The site owner has no control over who enters this chat room.");
 $default['pg_footer'] = t("Mouse over your image to control your audio and video settings<br/>
                            Mouse over a user and click Mute or Fade to ignore that user.");
 $default['rm_name_random'] = FALSE;
 $default['rm_show_room'] = TRUE;
 $default['rm_width'] = 640;
 $default['rm_height'] = 540;
 
 return $default;
}
regx's avatar
regx committed
//-----------------------------------------
// Administration form
//-----------------------------------------
regx's avatar
regx committed
function mebeam_chat_show_settings_form() {
regx's avatar
regx committed
  return drupal_get_form('mebeam_chat_settings_form');
}

regx's avatar
regx committed
function mebeam_chat_settings_form() {
  $default = mebeam_chat_get_defaults();
regx's avatar
regx committed
  $rname = variable_get('mebeam_chat_rm_rname', $default['rm_name']);
regx's avatar
regx committed

regx's avatar
regx committed
  # Certification Request User Messages
  $form['page'] = array(
    '#title' => t('Page Options'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  $form['page']['mebeam_chat_pg_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Page Title'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_pg_title', $default['pg_title']),
    '#description' => t("Title for your chat page. Leave blank to use the menu title"),
regx's avatar
regx committed
  );
  
  $form['page']['mebeam_chat_pg_header'] = array(
    '#type' => 'textarea',
    '#title' => t('Page Header'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_pg_header', $default['pg_header']),
    '#description' => t("Title for your chat page. Leave blank for no header"),
regx's avatar
regx committed
  );
  
  $form['page']['mebeam_chat_pg_footer'] = array(
    '#type' => 'textarea',
    '#title' => t('Page Footer'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_pg_footer', $default['pg_footer']),
    '#description' => t("Footer for your chat page. Leave blank for no footer"),
regx's avatar
regx committed
  );
  
  # widget options
  $form['widget'] = array(
    '#title' => t('MeBeam Widget Options'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['widget']['mebeam_chat_rm_name'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam Room Name'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_rm_name', $default['rm_name']),
    '#description' => t("The name of your chat room - must be between 4 and 16 characters.
    Also, if you do not have randomize turned on you should use a dash in your room name. ex. chatroom-01 or chatroom-b"),
regx's avatar
regx committed
  );

  $form['widget']['mebeam_chat_rm_name_random'] = array(
    '#type' => 'checkbox',
    '#title' => t('Randomize'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_rm_name_random', $default['rm_name_random']),
    '#description' => t("Randomize the room name - Randomly generates a new room name everytime cron.php is run. This helps keep your room somewhat private.
     A dash and the randomized string will be added to your room name, and the entire string will be truncated to 16 characters. For a completely random room name, leave the room name blank and select the Randomize option.
regx's avatar
regx committed
     <br/>%rname", array('%rname' => $rname)),
regx's avatar
regx committed
  );
  $form['widget']['mebeam_chat_rm_show_room'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show Room Name'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_rm_show_room', $default['rm_show_room']),
    '#description' => t("Show the room name above the chat widget"),
regx's avatar
regx committed
  $form['widget']['mebeam_chat_rm_width'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam widget width'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_rm_width', $default['rm_width']),
    '#description' => t("The width of the mebeam widget on your page in pixels"),
regx's avatar
regx committed
  );
  $form['widget']['mebeam_chat_rm_height'] = array(
    '#type' => 'textfield',
    '#title' => t('MeBeam widget width'),
regx's avatar
regx committed
    '#default_value' => variable_get('mebeam_chat_rm_height', $default['rm_height']),
    '#description' => t("The height of the mebeam widget on your page in pixels"),
regx's avatar
regx committed
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Settings'),
  );
  return $form;
}
//-----------------------------------------

regx's avatar
regx committed
function mebeam_chat_settings_form_validate($form_id, $form_values) {
    if (strlen($form_values['mebeam_chat_rm_name']) >= 16) {
      form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be less than 16 characters."));
regx's avatar
regx committed
    }
regx's avatar
regx committed
    if (strlen($form_values['mebeam_chat_rm_name']) <= 4) {
      form_set_error('mebeam_chat_rm_name', t("MeBeam Room Name must be more than 4 characters."));
regx's avatar
regx committed
    }
regx's avatar
regx committed
    if (!is_numeric($form_values['mebeam_chat_rm_width'])) {
      form_set_error('mebeam_chat_rm_width', t("MeBeam widget width must be numeric."));
regx's avatar
regx committed
    }
regx's avatar
regx committed
    if (!is_numeric($form_values['mebeam_chat_rm_height'])) {
      form_set_error('mebeam_chat_rm_height', t("MeBeam widget height must be numeric."));
regx's avatar
regx committed
    }
}
//---------------------------------------------------------

regx's avatar
regx committed
function mebeam_chat_settings_form_submit($form_id, $form_values) {
regx's avatar
regx committed
    variable_set('mebeam_chat_pg_title', $form_values['mebeam_chat_pg_title']);
    variable_set('mebeam_chat_pg_header', $form_values['mebeam_chat_pg_header']);
    variable_set('mebeam_chat_pg_footer', $form_values['mebeam_chat_pg_footer']);
    
    variable_set('mebeam_chat_rm_name', $form_values['mebeam_chat_rm_name']);
regx's avatar
regx committed
    if ($form_values['mebeam_chat_rm_name_random']) {
regx's avatar
regx committed
      $rname = mebeam_chat_randomize_name($form_values['mebeam_chat_rm_name']);
      variable_set('mebeam_chat_rm_rname', $rname);
    }
    else{
regx's avatar
regx committed
      variable_set('mebeam_chat_rm_rname', '');
regx's avatar
regx committed
    }
    variable_set('mebeam_chat_rm_name_random', $form_values['mebeam_chat_rm_name_random']);
    variable_set('mebeam_chat_rm_show_room', $form_values['mebeam_chat_rm_show_room']);
regx's avatar
regx committed
    variable_set('mebeam_chat_rm_width', $form_values['mebeam_chat_rm_width']);
    variable_set('mebeam_chat_rm_height', $form_values['mebeam_chat_rm_height']);
    
    $msg = t("Form options saved successfully."); 
regx's avatar
regx committed
    if ($form_values['mebeam_chat_rm_name_random']) {
      $msg .= t('Your random room name is currently %rname and will be updated automatically the next time cron runs', array('%rname' => $rname));
regx's avatar
regx committed
    }
regx's avatar
regx committed
    drupal_set_message($msg, 'status');
regx's avatar
regx committed
}
//---------------------------------------------------------

###########################################################################
# Helper Functions


regx's avatar
regx committed
function mebeam_chat_debug($msg) {
  if (MEBEAM_CHAT_DEBUG) {
    if (is_array($msg) || is_object($msg)) {
      drupal_set_message('<pre>'. print_r($msg, true) .'</pre>', 'debug');
regx's avatar
regx committed
    }
   else{
regx's avatar
regx committed
      drupal_set_message('mebeam_chat: '. $msg, 'debug');
regx's avatar
regx committed
    }
  }
}