Advertisement
agusl88

bbb-plugin

Oct 23rd, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 63.83 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: BigBlueButton
  4. Plugin URI: http://blindsidenetworks.com/integrations/wordpress
  5. Description: BigBlueButton is an open source web conferencing system. This plugin integrates BigBlueButton into WordPress allowing bloggers to create and manage meeting rooms to interact with their readers. It was developed and is maintained by <a href="http://blindsidenetworks.com/" target="_blank">Blindside Networks</a>. For more information on setting up your own BigBlueButton server or for using an external hosting provider visit <a href= "http://bigbluebutton.org/support" target="_blank">BigBlueButton support</a>.
  6.  
  7. Version: 1.3.9
  8. Author: Blindside Networks
  9. Author URI: http://blindsidenetworks.com/
  10. License: GPLv2 or later
  11. License URI: http://www.gnu.org/licenses/gpl-2.0.html
  12. */
  13.  
  14. //================================================================================
  15. //---------------------------Standard Plugin definition---------------------------
  16. //================================================================================
  17.  
  18. //validate
  19. global $wp_version;
  20. $exit_msg = "This plugin has been designed for Wordpress 2.5 and later, please upgrade your current one.";
  21. if(version_compare($wp_version, "2.5", "<")) {
  22.     exit($exit_msg);
  23. }
  24.  
  25. //constant definition
  26. define("BIGBLUEBUTTON_DIR", WP_PLUGIN_URL . '/bigbluebutton/' );
  27. define('BIGBLUEBUTTON_PLUGIN_VERSION', bigbluebutton_get_version());
  28. define('BIGBLUEBUTTON_PLUGIN_URL', plugin_dir_url( __FILE__ ));
  29.  
  30. //constant message definition
  31. define('BIGBLUEBUTTON_STRING_WELCOME', '<br>Cambiar este mensaje no es tan sencillo, y no es dinamico. Por lo que deberá ser algo genérico para todas las charlas.<br>');
  32. define('BIGBLUEBUTTON_STRING_MEETING_RECORDED', '<br><br>Esta sesion esta siendo grabada.');
  33.  
  34. //constant internal definition
  35. define("BIGBLUEBUTTON_FORM_IN_WIDGET", TRUE );
  36.  
  37. //================================================================================
  38. //------------------Required Libraries and Global Variables-----------------------
  39. //================================================================================
  40. require('php/bbb_api.php');
  41.  
  42. //================================================================================
  43. //------------------Code for development------------------------------------------
  44. //================================================================================
  45. if(!function_exists('_log')){
  46.     function _log( $message ) {
  47.         if( WP_DEBUG === true ){
  48.             if( is_array( $message ) || is_object( $message ) ){
  49.                 error_log( print_r( $message, true ) );
  50.             } else {
  51.                 error_log( $message );
  52.             }
  53.         }
  54.     }
  55. }
  56. _log('Loading the plugin');
  57.  
  58. //================================================================================
  59. //------------------------------------Main----------------------------------------
  60. //================================================================================
  61. //hook definitions
  62. register_activation_hook(__FILE__, 'bigbluebutton_install' ); //Runs the install script (including the databse and options set up)
  63. //register_deactivation_hook(__FILE__, 'bigbluebutton_uninstall') ); //Runs the uninstall function (it includes the database and options delete)
  64. register_uninstall_hook(__FILE__, 'bigbluebutton_uninstall' ); //Runs the uninstall function (it includes the database and options delete)
  65.  
  66. //shortcode definitions
  67. add_shortcode('bigbluebutton', 'bigbluebutton_shortcode');
  68. add_shortcode('bigbluebutton_recordings', 'bigbluebutton_recordings_shortcode');
  69.  
  70. //action definitions
  71. add_action('init', 'bigbluebutton_init');
  72. add_action('admin_menu', 'bigbluebutton_add_pages', 1);
  73. add_action('admin_init', 'bigbluebutton_admin_init', 1);
  74. add_action('plugins_loaded', 'bigbluebutton_update' );
  75. add_action('plugins_loaded', 'bigbluebutton_widget_init' );
  76. set_error_handler("bigbluebutton_warning_handler", E_WARNING);
  77.  
  78.  
  79. //================================================================================
  80. //------------------------------ Main Functions ----------------------------------
  81. //================================================================================
  82. // Sessions are required by the plugin to work.
  83. function bigbluebutton_init() {
  84.     bigbluebutton_init_sessions();
  85.     bigbluebutton_init_scripts();
  86.     bigbluebutton_init_styles();
  87.    
  88.     //Attaches the plugin's stylesheet to the plugin page just created
  89.     add_action('wp_print_styles', 'bigbluebutton_admin_styles');
  90. }
  91.  
  92. function bigbluebutton_init_sessions() {
  93.     if (!session_id()) {
  94.         session_start();
  95.     }
  96. }
  97.  
  98. function bigbluebutton_init_scripts() {
  99.     if (!is_admin()) {
  100.         wp_enqueue_script('jquery');
  101.     }
  102. }
  103.  
  104. //Registers the plugin's stylesheet
  105. function bigbluebutton_init_styles() {
  106.     wp_register_style('bigbluebuttonStylesheet', WP_PLUGIN_URL.'/bigbluebutton/css/bigbluebutton_stylesheet.css');
  107. }
  108.  
  109. //Registers the plugin's stylesheet
  110. function bigbluebutton_admin_init() {
  111.     bigbluebutton_init_styles();
  112. }
  113.  
  114. //Adds the plugin stylesheet to wordpress
  115. function bigbluebutton_admin_styles(){
  116.     wp_enqueue_style('bigbluebuttonStylesheet');
  117. }
  118.  
  119. //Registers the bigbluebutton widget
  120. function bigbluebutton_widget_init(){
  121.     wp_register_sidebar_widget('bigbluebuttonsidebarwidget', __('BigBlueButton'), 'bigbluebutton_sidebar', array( 'description' => 'Displays a BigBlueButton login form in a sidebar.'));
  122. }
  123.  
  124. //Inserts the plugin pages in the admin panel
  125. function bigbluebutton_add_pages() {
  126.  
  127.     //Add a new submenu under Settings
  128.     $page = add_options_page(__('BigBlueButton','menu-test'), __('BigBlueButton','menu-test'), 'manage_options', 'bigbluebutton_general', 'bigbluebutton_general_options');
  129.  
  130.     //Attaches the plugin's stylesheet to the plugin page just created
  131.     add_action('admin_print_styles-' . $page, 'bigbluebutton_admin_styles');
  132.  
  133. }
  134.  
  135. //Sets up the bigbluebutton table to store meetings in the wordpress database
  136. function bigbluebutton_install () {
  137.     global $wp_roles;
  138.     // Load roles if not set
  139.     if ( ! isset( $wp_roles ) )
  140.         $wp_roles = new WP_Roles();
  141.  
  142.     //Installation code
  143.     if( !get_option('bigbluebutton_plugin_version') ){
  144.         bigbluebutton_init_database();
  145.     }
  146.      
  147.     ////////////////// Initialize Settings //////////////////
  148.     if( !get_option('bigbluebutton_url') ) update_option( 'bigbluebutton_url', 'http://test-install.blindsidenetworks.com/bigbluebutton/' );
  149.     if( !get_option('bigbluebutton_salt') ) update_option( 'bigbluebutton_salt', '8cd8ef52e8e101574e400365b55e11a6' );
  150.     if( !get_option('bigbluebutton_permissions') ){
  151.         $roles = $wp_roles->role_names;
  152.         $roles['anonymous'] = 'Anonymous';
  153.         foreach($roles as $key => $value) {
  154.             $permissions[$key]['participate'] = true;
  155.             if($value == "Administrator"){
  156.                 $permissions[$key]['manageRecordings'] = true;
  157.                 $permissions[$key]['defaultRole'] = "moderator";
  158.             } else if($value == "Anonymous"){
  159.                 $permissions[$key]['manageRecordings'] = false;
  160.                 $permissions[$key]['defaultRole'] = "none";
  161.             } else {
  162.                 $permissions[$key]['manageRecordings'] = false;
  163.                 $permissions[$key]['defaultRole'] = "attendee";
  164.             }
  165.  
  166.         }
  167.  
  168.         update_option( 'bigbluebutton_permissions', $permissions );
  169.  
  170.     }
  171.  
  172.     update_option( "bigbluebutton_plugin_version", BIGBLUEBUTTON_PLUGIN_VERSION );
  173.  
  174. }
  175.  
  176. function bigbluebutton_update() {
  177.     global $wpdb, $wp_roles;
  178.     // Load roles if not set
  179.     if ( ! isset( $wp_roles ) )
  180.         $wp_roles = new WP_Roles();
  181.  
  182.     //Sets the name of the table
  183.     $table_name = $wpdb->prefix . "bigbluebutton";
  184.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  185.  
  186.     ////////////////// Updates for version 1.3.1 and earlier //////////////////
  187.     $bigbluebutton_plugin_version_installed = get_option('bigbluebutton_plugin_version');
  188.     if( !$bigbluebutton_plugin_version_installed                                                                 //It's 1.0.2 or earlier
  189.             || (strcmp("1.3.1", $bigbluebutton_plugin_version_installed) <= 0 && get_option("bbb_db_version")) ){  //It's 1.3.1 not updated
  190.         ////////////////// Update Database //////////////////
  191.         /// Initialize database will create the tables added for the new version
  192.         bigbluebutton_init_database();
  193.         /// Transfer the data from old table to the new one
  194.         $table_name_old = $wpdb->prefix . "bbb_meetingRooms";
  195.         $listOfMeetings = $wpdb->get_results("SELECT * FROM ".$table_name_old." ORDER BY id");
  196.         foreach ($listOfMeetings as $meeting) {
  197.             $sql = "INSERT INTO " . $table_name . " (meetingID, meetingName, meetingVersion, attendeePW, moderatorPW) VALUES ('".bigbluebutton_generateToken()."','".$meeting->meetingID."', '".$meeting->meetingVersion."', '".$meeting->attendeePW."', '".$meeting->moderatorPW."');";
  198.             $wpdb->query($sql);
  199.         }
  200.         /// Remove the old table
  201.         $wpdb->query("DROP TABLE IF EXISTS $table_name_old");
  202.  
  203.         ////////////////// Update Settings //////////////////
  204.         if( !get_option('mt_bbb_url') ) {
  205.             update_option( 'bigbluebutton_url', 'http://test-install.blindsidenetworks.com/bigbluebutton/' );
  206.         } else {
  207.             update_option( 'bigbluebutton_url', get_option('mt_bbb_url') );
  208.             delete_option('mt_bbb_url');
  209.         }
  210.  
  211.         if( !get_option('mt_salt') ) {
  212.             update_option( 'bigbluebutton_salt', '8cd8ef52e8e101574e400365b55e11a6' );
  213.         } else {
  214.             update_option( 'bigbluebutton_salt', get_option('mt_salt') );
  215.             delete_option('mt_salt');
  216.         }
  217.  
  218.         delete_option('mt_waitForModerator'); //deletes this option because it is no longer needed, it has been incorportated into the table.
  219.         delete_option('bbb_db_version'); //deletes this option because it is no longer needed, the versioning pattern has changed.
  220.     }
  221.      
  222.     //Set the new permission schema
  223.     if( $bigbluebutton_plugin_version_installed && strcmp($bigbluebutton_plugin_version_installed, "1.3.3") < 0 ){
  224.         $roles = $wp_roles->role_names;
  225.         $roles['anonymous'] = 'Anonymous';
  226.  
  227.         if( !get_option('bigbluebutton_permissions') ){
  228.             foreach($roles as $key => $value) {
  229.                 $permissions[$key]['participate'] = true;
  230.                 if($value == "Administrator"){
  231.                     $permissions[$key]['manageRecordings'] = true;
  232.                     $permissions[$key]['defaultRole'] = "moderator";
  233.                 } else if($value == "Anonymous"){
  234.                     $permissions[$key]['manageRecordings'] = false;
  235.                     $permissions[$key]['defaultRole'] = "none";
  236.                 } else {
  237.                     $permissions[$key]['manageRecordings'] = false;
  238.                     $permissions[$key]['defaultRole'] = "attendee";
  239.                 }
  240.             }
  241.  
  242.         } else {
  243.             $old_permissions = get_option('bigbluebutton_permissions');
  244.             foreach($roles as $key => $value) {
  245.                 if( !isset($old_permissions[$key]['participate']) ){
  246.                     $permissions[$key]['participate'] = true;
  247.                     if($value == "Administrator"){
  248.                         $permissions[$key]['manageRecordings'] = true;
  249.                         $permissions[$key]['defaultRole'] = "moderator";
  250.                     } else if($value == "Anonymous"){
  251.                         $permissions[$key]['manageRecordings'] = false;
  252.                         $permissions[$key]['defaultRole'] = "none";
  253.                     } else {
  254.                         $permissions[$key]['manageRecordings'] = false;
  255.                         $permissions[$key]['defaultRole'] = "attendee";
  256.                     }
  257.                 } else {
  258.                     $permissions[$key] = $old_permissions[$key];
  259.                 }
  260.             }
  261.  
  262.         }
  263.  
  264.         update_option( 'bigbluebutton_permissions', $permissions );
  265.  
  266.     }
  267.  
  268.     ////////////////// Set new bigbluebutton_plugin_version value //////////////////
  269.     update_option( "bigbluebutton_plugin_version", BIGBLUEBUTTON_PLUGIN_VERSION );
  270.  
  271. }
  272.  
  273. function bigbluebutton_uninstall () {
  274.     global $wpdb;
  275.  
  276.     //In case is deactivateing an overwritten version
  277.     if( get_option('bbb_db_version') ){
  278.         $table_name_old = $wpdb->prefix . "bbb_meetingRooms";
  279.         $wpdb->query("DROP TABLE IF EXISTS $table_name_old");
  280.         delete_option('bbb_db_version');
  281.         delete_option('mt_bbb_url');
  282.         delete_option('mt_salt');
  283.     }
  284.      
  285.     //Delete the options stored in the wordpress db
  286.     delete_option('bigbluebutton_plugin_version');
  287.     delete_option('bigbluebutton_url');
  288.     delete_option('bigbluebutton_salt');
  289.     delete_option('bigbluebutton_permissions');
  290.  
  291.     //Sets the name of the table
  292.     $table_name = $wpdb->prefix . "bigbluebutton";
  293.     $wpdb->query("DROP TABLE IF EXISTS $table_name");
  294.  
  295.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  296.     $wpdb->query("DROP TABLE IF EXISTS $table_logs_name");
  297.  
  298. }
  299.  
  300. //Creates the bigbluebutton tables in the wordpress database
  301. function bigbluebutton_init_database(){
  302.     require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  303.  
  304.     global $wpdb;
  305.  
  306.     //Sets the name of the table
  307.     $table_name = $wpdb->prefix . "bigbluebutton";
  308.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  309.  
  310.     //Execute sql
  311.     $sql = "CREATE TABLE " . $table_name . " (
  312.    id mediumint(9) NOT NULL AUTO_INCREMENT,
  313.    meetingID text NOT NULL,
  314.    meetingName text NOT NULL,
  315.    meetingVersion int NOT NULL,
  316.    attendeePW text NOT NULL,
  317.    moderatorPW text NOT NULL,
  318.    waitForModerator BOOLEAN NOT NULL DEFAULT FALSE,
  319.    recorded BOOLEAN NOT NULL DEFAULT FALSE,
  320.    UNIQUE KEY id (id)
  321.    );";
  322.     dbDelta($sql);
  323.  
  324.     $sql = "INSERT INTO " . $table_name . " (meetingID, meetingName, meetingVersion, attendeePW, moderatorPW)
  325.    VALUES ('".bigbluebutton_generateToken()."','Demo meeting', '".time()."', 'ap', 'mp');";
  326.     dbDelta($sql);
  327.  
  328.     $sql = "INSERT INTO " . $table_name . " (meetingID, meetingName, meetingVersion, attendeePW, moderatorPW, recorded)
  329.    VALUES ('".bigbluebutton_generateToken()."','Demo meeting (recorded)', '".time()."', 'ap', 'mp', TRUE);";
  330.     dbDelta($sql);
  331.  
  332.     $sql = "CREATE TABLE " . $table_logs_name . " (
  333.    id mediumint(9) NOT NULL AUTO_INCREMENT,
  334.    meetingID text NOT NULL,
  335.    recorded BOOLEAN NOT NULL DEFAULT FALSE,
  336.    timestamp int NOT NULL,
  337.    event text NOT NULL,
  338.    UNIQUE KEY id (id)
  339.    );";
  340.     dbDelta($sql);
  341.  
  342. }
  343.  
  344. //Returns current plugin version.
  345. function bigbluebutton_get_version() {
  346.     if ( !function_exists( 'get_plugins' ) )
  347.         require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  348.     $plugin_folder = get_plugins( '/' . plugin_basename( dirname( __FILE__ ) ) );
  349.     $plugin_file = basename( ( __FILE__ ) );
  350.  
  351.     return $plugin_folder[$plugin_file]['Version'];
  352. }
  353.  
  354.  
  355. //================================================================================
  356. //------------------------------Error Handler-------------------------------------
  357. //================================================================================
  358. function bigbluebutton_warning_handler($errno, $errstr) {
  359.     //Do Nothing
  360. }
  361.  
  362.  
  363. //================================================================================
  364. //---------------------------------ShortCode functions----------------------------
  365. //================================================================================
  366. //Inserts a bigbluebutton form on a post or page of the blog
  367. function bigbluebutton_shortcode($args) {
  368.     extract($args);
  369.  
  370.     return bigbluebutton_form($args);
  371.  
  372. }
  373.  
  374. function bigbluebutton_recordings_shortcode($args) {
  375.     extract($args);
  376.  
  377.     return bigbluebutton_list_recordings((isset($args['title']))? $args['title']: null);
  378.  
  379. }
  380.  
  381.  
  382. //================================================================================
  383. //---------------------------------Widget-----------------------------------------
  384. //================================================================================
  385. //Inserts a bigbluebutton widget on the siderbar of the blog
  386. function bigbluebutton_sidebar($args) {
  387.     extract($args);
  388.  
  389.     echo $before_widget;
  390.     echo $before_title.'BigBlueButton'.$after_title;
  391.     echo bigbluebutton_form($args, BIGBLUEBUTTON_FORM_IN_WIDGET);
  392.     echo $after_widget;
  393. }
  394.  
  395. //================================================================================
  396. //Create the form called by the Shortcode and Widget functions
  397. function bigbluebutton_form($args, $bigbluebutton_form_in_widget = false) {
  398.     global $wpdb, $wp_version, $current_site, $current_user, $wp_roles;
  399.     $table_name = $wpdb->prefix . "bigbluebutton";
  400.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  401.    
  402.     $token = isset($args['token']) ?$args['token']: null;
  403.     $submit = isset($args['submit']) ?$args['submit']: null;
  404.    
  405.     //Initializes the variable that will collect the output
  406.     $out = '';
  407.  
  408.     //Set the role for the current user if is logged in
  409.     $role = null;
  410.     if( $current_user->ID ) {
  411.         $role = "unregistered";
  412.         foreach($wp_roles->role_names as $_role => $Role) {
  413.             if (array_key_exists($_role, $current_user->caps)){
  414.                 $role = $_role;
  415.                 break;
  416.             }
  417.         }
  418.     } else {
  419.         $role = "anonymous";
  420.     }
  421.  
  422.     //Read in existing option value from database
  423.     $url_val = get_option('bigbluebutton_url');
  424.     $salt_val = get_option('bigbluebutton_salt');
  425.     //Read in existing permission values from database
  426.     $permissions = get_option('bigbluebutton_permissions');
  427.  
  428.     //Gets all the meetings from wordpress database
  429.     $listOfMeetings = $wpdb->get_results("SELECT meetingID, meetingName, meetingVersion, attendeePW, moderatorPW FROM ".$table_name." ORDER BY meetingName");
  430.      
  431.     $dataSubmitted = false;
  432.     $meetingExist = false;
  433.     if( isset($_POST['SubmitForm']) ) { //The user has submitted his login information
  434.         $dataSubmitted = true;
  435.         $meetingExist = true;
  436.  
  437.         $meetingID = $_POST['meetingID'];
  438.  
  439.         $found = $wpdb->get_row("SELECT * FROM ".$table_name." WHERE meetingID = '".$meetingID."'");
  440.         if( $found ){
  441.             $found->meetingID = bigbluebutton_normalizeMeetingID($found->meetingID);
  442.  
  443.             if( !$current_user->ID ) {
  444.                 $name = isset($_POST['display_name']) && $_POST['display_name']?$_POST['display_name']: $role;
  445.                
  446.                 if( bigbluebutton_validate_defaultRole($role, 'none') ) {
  447.                     $password = $_POST['pwd'];
  448.                 } else {
  449.                     $password = $permissions[$role]['defaultRole'] == 'none'? $found->moderatorPW: $found->attendeePW;
  450.                 }
  451.                    
  452.             } else {
  453.                 if( $current_user->display_name != '' ){
  454.                     $name = $current_user->display_name;
  455.                 } else if( $current_user->user_firstname != '' || $current_user->user_lastname != '' ){
  456.                     $name = $current_user->user_firstname != ''? $current_user->user_firstname.' ': '';
  457.                     $name .= $current_user->user_lastname != ''? $current_user->user_lastname.' ': '';
  458.                 } else if( $current_user->user_login != ''){
  459.                     $name = $current_user->user_login;
  460.                 } else {
  461.                     $name = $role;
  462.                 }
  463.                 if( bigbluebutton_validate_defaultRole($role, 'none') ) {
  464.                     $password = $_POST['pwd'];
  465.                 } else {
  466.                     $password = $permissions[$role]['defaultRole'] == 'moderator'? $found->moderatorPW: $found->attendeePW;
  467.                 }
  468.  
  469.             }
  470.  
  471.             //Extra parameters
  472.             $recorded = $found->recorded;
  473.             $welcome = (isset($args['welcome']))? html_entity_decode($args['welcome']): BIGBLUEBUTTON_STRING_WELCOME;
  474.             if( $recorded ) $welcome .= BIGBLUEBUTTON_STRING_MEETING_RECORDED;
  475.             $duration = 0;
  476.             $voicebridge = 0;
  477.             $logouturl = (is_ssl()? "https://": "http://") . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
  478.  
  479.             //Metadata for tagging recordings
  480.             $metadata = Array(
  481.                     'meta_origin' => 'WordPress',
  482.                     'meta_originversion' => $wp_version,
  483.                     'meta_origintag' => 'wp_plugin-bigbluebutton '.BIGBLUEBUTTON_PLUGIN_VERSION,
  484.                     'meta_originservername' => home_url(),
  485.                     'meta_originservercommonname' => get_bloginfo('name'),
  486.                     'meta_originurl' => $logouturl
  487.             );
  488.             //Call for creating meeting on the bigbluebutton server
  489.             $response = BigBlueButton::createMeetingArray($name, $found->meetingID, $found->meetingName, $welcome, $found->moderatorPW, $found->attendeePW, $salt_val, $url_val, $logouturl, $recorded? 'true':'false', $duration, $voicebridge, $metadata );
  490.  
  491.             //Analyzes the bigbluebutton server's response
  492.             if(!$response || $response['returncode'] == 'FAILED' ){//If the server is unreachable, or an error occured
  493.                 $out .= "Sorry an error occured while joining the meeting.";
  494.                 return $out;
  495.                  
  496.             } else{ //The user can join the meeting, as it is valid
  497.                 if( !isset($response['messageKey']) || $response['messageKey'] == '' ){
  498.                     // The meeting was just created, insert the create event to the log
  499.                     $rows_affected = $wpdb->insert( $table_logs_name, array( 'meetingID' => $found->meetingID, 'recorded' => $found->recorded, 'timestamp' => time(), 'event' => 'Create' ) );
  500.                 }
  501.  
  502.                 $bigbluebutton_joinURL = BigBlueButton::getJoinURL($found->meetingID, $name, $password, $salt_val, $url_val );
  503.                 //If the meeting is already running or the moderator is trying to join or a viewer is trying to join and the
  504.                 //do not wait for moderator option is set to false then the user is immediately redirected to the meeting
  505.                 if ( (BigBlueButton::isMeetingRunning( $found->meetingID, $url_val, $salt_val ) && ($found->moderatorPW == $password || $found->attendeePW == $password ) )
  506.                         || $response['moderatorPW'] == $password
  507.                         || ($response['attendeePW'] == $password && !$found->waitForModerator)  ){
  508.                     //If the password submitted is correct then the user gets redirected
  509.                     $out .= '<script type="text/javascript">window.location = "'.$bigbluebutton_joinURL.'";</script>'."\n";
  510.                     return $out;
  511.                 }
  512.                 //If the viewer has the correct password, but the meeting has not yet started they have to wait
  513.                 //for the moderator to start the meeting
  514.                 else if ($found->attendeePW == $password){
  515.                     //Stores the url and salt of the bigblubutton server in the session
  516.                     $_SESSION['mt_bbb_url'] = $url_val;
  517.                     $_SESSION['mt_salt'] = $salt_val;
  518.                     //Displays the javascript to automatically redirect the user when the meeting begins
  519.                     $out .= bigbluebutton_display_redirect_script($bigbluebutton_joinURL, $found->meetingID, $found->meetingName, $name);
  520.                     return $out;
  521.                 }
  522.             }
  523.         }
  524.     }
  525.  
  526.     //If a valid meeting was found the login form is displayed
  527.     if(sizeof($listOfMeetings) > 0){
  528.         //Alerts the user if the password they entered does not match
  529.         //the meeting's password
  530.        
  531.         if($dataSubmitted && !$meetingExist){
  532.             $out .= "***".$meetingID." no longer exists.***";
  533.         }
  534.         else if($dataSubmitted){
  535.             $out .= "***Incorrect Password***";
  536.         }
  537.  
  538.         if ( bigbluebutton_can_participate($role) ){
  539.             $out .= '
  540.            <form id="bbb-join-form'.($bigbluebutton_form_in_widget?'-widget': '').'" class="bbb-join" name="form1" method="post" action="">';
  541.  
  542.             if(sizeof($listOfMeetings) > 1 && !$token ){
  543.                 $out .= '
  544.                <label>Meeting:</label>
  545.                <select name="meetingID">';
  546.  
  547.                 foreach ($listOfMeetings as $meeting) {
  548.                     $out .= '
  549.                    <option value="'.$meeting->meetingID.'">'.$meeting->meetingName.'</option>';
  550.                 }
  551.  
  552.                 $out .= '
  553.                </select>';
  554.             } else if ($token) {
  555.                 $out .= '
  556.                <input type="hidden" name="meetingID" id="meetingID" value="'.$token.'" />';
  557.                
  558.             } else {
  559.                 $meeting = reset($listOfMeetings);
  560.                 $out .= '
  561.                <input type="hidden" name="meetingID" id="meetingID" value="'.$meeting->meetingID.'" />';
  562.  
  563.             }
  564.  
  565.             if( !$current_user->ID ) {
  566.                 $out .= '
  567.                <label>Name:</label>
  568.                <input type="text" id="name" name="display_name" size="10">';
  569.             }
  570.             if( bigbluebutton_validate_defaultRole($role, 'none') ) {
  571.                 $out .= '
  572.                <label>Password:</label>
  573.                <input type="password" name="pwd" size="10">';
  574.             }
  575.             $out .= '
  576.            </table>';
  577.             if(sizeof($listOfMeetings) > 1 && !$token ){
  578.                 $out .= '
  579.                
  580.                <input type="submit" name="SubmitForm" value="'.($submit? $submit: 'Join').'">';
  581.             } else if ($token) {
  582.                 foreach ($listOfMeetings as $meeting) {
  583.                     if($meeting->meetingID == $token ){
  584.                         $out .= '
  585.                <input type="submit" name="SubmitForm" value="'.($submit? $submit: 'Join '.$meeting->meetingName).'">';
  586.                         break;
  587.                     }
  588.                 }
  589.                
  590.                 if($meeting->meetingID != $token ){
  591.                     $out .= '
  592.                <div>Invalid meeting token</div>';
  593.                 }
  594.                
  595.             } else {
  596.                 $out .= '
  597.                <input type="submit" name="SubmitForm" value="'.($submit? $submit: 'Join '.$meeting->meetingName).'">';
  598.  
  599.             }
  600.             $out .= '
  601.            </form>';
  602.  
  603.         } else {
  604.             $out .= $role." users are not allowed to participate in meetings";
  605.  
  606.         }
  607.  
  608.     } else if($dataSubmitted){
  609.         //Alerts the user if the password they entered does not match
  610.         //the meeting's password
  611.         $out .= "***".$meetingID." no longer exists.***<br />";
  612.         $out .= "No meeting rooms are currently available to join.";
  613.  
  614.     } else{
  615.         $out .= "No meeting rooms are currently available to join.";
  616.  
  617.     }
  618.    
  619.     return $out;
  620. }
  621.  
  622.  
  623. //Displays the javascript that handles redirecting a user, when the meeting has started
  624. //the meetingName is the meetingID
  625. function bigbluebutton_display_redirect_script($bigbluebutton_joinURL, $meetingID, $meetingName, $name){
  626.     $out = '
  627.    <script type="text/javascript">
  628.        function bigbluebutton_ping() {
  629.            jQuery.ajax({
  630.                url : "./wp-content/plugins/bigbluebutton/php/broker.php?action=ping&meetingID='.urlencode($meetingID).'",
  631.                async : true,
  632.                dataType : "xml",
  633.                success : function(xmlDoc){
  634.                    $xml = jQuery( xmlDoc ), $running = $xml.find( "running" );
  635.                    if($running.text() == "true"){
  636.                        window.location = "'.$bigbluebutton_joinURL.'";
  637.                    }
  638.                },
  639.                error : function(xmlHttpRequest, status, error) {
  640.                    console.debug(xmlHttpRequest);
  641.                }
  642.            });
  643.  
  644.        }
  645.  
  646.        setInterval("bigbluebutton_ping()", 5000);
  647.    </script>';
  648.  
  649.     $out .= '
  650.    <table>
  651.      <tbody>
  652.        <tr>
  653.          <td>
  654.            Welcome '.$name.'!<br /><br />
  655.            '.$meetingName.' session has not been started yet.<br /><br />
  656.            <div align="center"><img src="./wp-content/plugins/bigbluebutton/images/polling.gif" /></div><br />
  657.            (Your browser will automatically refresh and join the meeting when it starts.)
  658.          </td>
  659.        </tr>
  660.      </tbody>
  661.    </table>';
  662.  
  663.     return $out;
  664. }
  665.  
  666.  
  667. //================================================================================
  668. //---------------------------------bigbluebutton Page--------------------------------------
  669. //================================================================================
  670. //The main page where the user specifies the url of the bigbluebutton server and its salt
  671. function bigbluebutton_general_options() {
  672.  
  673.     //Checks to see if the user has the sufficient persmissions and capabilities
  674.     if (!current_user_can('manage_options'))
  675.     {
  676.         wp_die( __('You do not have sufficient permissions to access this page.') );
  677.     }
  678.  
  679.     echo bigbluebutton_general_settings();
  680.     /* If the bigbluebutton server url and salt are empty then it does not
  681.      display the create meetings, and list meetings sections.*/
  682.     $url_val = get_option('bigbluebutton_url');
  683.     $salt_val = get_option('bigbluebutton_salt');
  684.     if($url_val == '' || $salt_val == ''){
  685.         $out .= '</div>';
  686.  
  687.     } else {
  688.         echo bigbluebutton_permission_settings();
  689.  
  690.         echo bigbluebutton_create_meetings();
  691.  
  692.         echo bigbluebutton_list_meetings();
  693.  
  694.         echo bigbluebutton_list_recordings('List of Recordings');
  695.  
  696.     }
  697.  
  698. }
  699.  
  700. //================================================================================
  701. //------------------------------General Settings----------------------------------
  702. //================================================================================
  703. // The page allows the user specifies the url of the bigbluebutton server and its salt
  704. function bigbluebutton_general_settings() {
  705.  
  706.     //Initializes the variable that will collect the output
  707.     $out = '';
  708.  
  709.     //Displays the title of the page
  710.     $out .= '<div class="wrap">';
  711.     $out .= "<h2>BigBlueButton General Settings</h2>";
  712.  
  713.     $url_val = get_option('bigbluebutton_url');
  714.     $salt_val = get_option('bigbluebutton_salt');
  715.  
  716.     //Obtains the meeting information of the meeting that is going to be terminated
  717.     if( isset($_POST['SubmitSettings']) && $_POST['SubmitSettings'] == 'Save Settings') {
  718.          
  719.         //Reads their posted value
  720.         $url_val = $_POST[ 'bigbluebutton_url' ];
  721.         $salt_val = $_POST[ 'bigbluebutton_salt' ];
  722.  
  723.         //
  724.         if(strripos($url_val, "/bigbluebutton/") == false){
  725.             if(substr($url_val, -1) == "/"){
  726.                 $url_val .= "bigbluebutton/";
  727.             }
  728.             else{
  729.                 $url_val .= "/bigbluebutton/";
  730.             }
  731.         }
  732.          
  733.         // Save the posted value in the database
  734.         update_option('bigbluebutton_url', $url_val );
  735.         update_option('bigbluebutton_salt', $salt_val );
  736.  
  737.         // Put an settings updated message on the screen
  738.         $out .= '<div class="updated"><p><strong>Settings saved.</strong></p></div>';
  739.  
  740.     }
  741.  
  742.     if($url_val == "http://test-install.blindsidenetworks.com/bigbluebutton/" ){
  743.         $out .= '<div class="updated"><p><strong>You are using a test BigBlueButton server provided by <a href="http://blindsidenetworks.com/" target="_blank">Blindside Networks</a>. For more information on setting up your own BigBlueButton server see <i><a href="http://bigbluebutton.org/support" target="_blank">http://bigbluebutton.org/support.</a></i></strong></div>';
  744.     }
  745.     //Form to update the url of the bigbluebutton server, and it`s salt
  746.  
  747.     $out .= '
  748.    <form name="form1" method="post" action="">
  749.    <p>URL of BigBlueButton server:<input type="text" name="bigbluebutton_url" value="'.$url_val.'" size="60"><br> eg. \'http://test-install.blindsidenetworks.com/bigbluebutton/\'
  750.    </p>
  751.    <p>Salt of BigBlueButton server:<input type="text" name="bigbluebutton_salt" value="'.$salt_val.'" size="40"><br> It can be found in /var/lib/tomcat6/webapps/bigbluebutton/WEB-INF/classes/bigbluebutton.properties.<br>eg. \'8cd8ef52e8e101574e400365b55e11a6\'.
  752.    </p>
  753.  
  754.    <p class="submit">
  755.    <input type="submit" name="SubmitSettings" class="button-primary" value="Save Settings" />
  756.    </p>
  757.  
  758.    </form>
  759.    <hr />';
  760.  
  761.     return $out;
  762.  
  763. }
  764.  
  765. //================================================================================
  766. //------------------------------Permisssion Settings----------------------------------
  767. //================================================================================
  768. // The page allows the user grants permissions for accessing meetings
  769. function bigbluebutton_permission_settings() {
  770.     global $wp_roles;
  771.     $roles = $wp_roles->role_names;
  772.     $roles['anonymous'] = 'Anonymous';
  773.  
  774.     //Initializes the variable that will collect the output
  775.     $out = '';
  776.  
  777.     if( isset($_POST['SubmitPermissions']) && $_POST['SubmitPermissions'] == 'Save Permissions' ) {
  778.         foreach($roles as $key => $value) {
  779.             if( !isset($_POST[$value.'-defaultRole']) ){
  780.                 if( $value == "Administrator" ) {
  781.                     $permissions[$key]['defaultRole'] = 'moderator';
  782.                 } else if ( $value == "Anonymous" ) {
  783.                     $permissions[$key]['defaultRole'] = 'none';
  784.                 } else {
  785.                     $permissions[$key]['defaultRole'] = 'attendee';
  786.                 }
  787.             } else {
  788.                 $permissions[$key]['defaultRole'] = $_POST[$value.'-defaultRole'];
  789.             }
  790.                
  791.             if( !isset($_POST[$value.'-participate']) ){
  792.                 $permissions[$key]['participate'] = false;
  793.             } else {
  794.                 $permissions[$key]['participate'] = true;
  795.             }
  796.  
  797.             if( !isset($_POST[$value.'-manageRecordings']) ){
  798.                 $permissions[$key]['manageRecordings'] = false;
  799.             } else {
  800.                 $permissions[$key]['manageRecordings'] = true;
  801.             }
  802.  
  803.                
  804.         }
  805.         update_option( 'bigbluebutton_permissions', $permissions );
  806.  
  807.     } else {
  808.         $permissions = get_option('bigbluebutton_permissions');
  809.  
  810.     }
  811.  
  812.     //Displays the title of the page
  813.     $out .= "<h2>BigBlueButton Permission Settings</h2>";
  814.  
  815.     $out .= '</br>';
  816.  
  817.     $out .= '
  818.    <form name="form1" method="post" action="">
  819.    <table class="stats" cellspacing="5">
  820.    <tr>
  821.    <th class="hed" colspan="1">Role</td>
  822.    <th class="hed" colspan="1">Manage Recordings</th>
  823.    <th class="hed" colspan="1">Participate</th>
  824.    <th class="hed" colspan="1">Join as Moderator</th>
  825.    <th class="hed" colspan="1">Join as Attendee</th>
  826.    <th class="hed" colspan="1">Join with Password</th>
  827.    </tr>';
  828.  
  829.     foreach($roles as $key => $value) {
  830.         $out .= '
  831.        <tr>
  832.        <td>'.$value.'</td>
  833.        <td><input type="checkbox" name="'.$value.'-manageRecordings" '.($permissions[$key]['manageRecordings']?'checked="checked"': '').' /></td>
  834.        <td><input type="checkbox" name="'.$value.'-participate" '.($permissions[$key]['participate']?'checked="checked"': '').' /></td>
  835.        <td><input type="radio" name="'.$value.'-defaultRole" value="moderator" '.($permissions[$key]['defaultRole']=="moderator"?'checked="checked"': '').' /></td>
  836.        <td><input type="radio" name="'.$value.'-defaultRole" value="attendee" '.($permissions[$key]['defaultRole']=="attendee"?'checked="checked"': '').' /></td>
  837.        <td><input type="radio" name="'.$value.'-defaultRole" value="none" '.($permissions[$key]['defaultRole']=="none"?'checked="checked"': '').' /></td>
  838.        </tr>';
  839.     }
  840.  
  841.     $out .= '
  842.    </table>
  843.    <p class="submit"><input type="submit" name="SubmitPermissions" class="button-primary" value="Save Permissions" /></p>
  844.    </form>
  845.    <hr />';
  846.  
  847.     return $out;
  848.  
  849. }
  850.  
  851. //================================================================================
  852. //-----------------------------Create a Meeting-----------------------------------
  853. //================================================================================
  854. //This page allows the user to create a meeting
  855. function bigbluebutton_create_meetings() {
  856.     global $wpdb;
  857.  
  858.     //Initializes the variable that will collect the output
  859.     $out = '';
  860.  
  861.     //Displays the title of the page
  862.     $out .= "<h2>Create a Meeting Room</h2>";
  863.  
  864.     $url_val = get_option('bigbluebutton_url');
  865.     $salt_val = get_option('bigbluebutton_salt');
  866.  
  867.     //Obtains the meeting information of the meeting that is going to be created
  868.     if( isset($_POST['SubmitCreate']) && $_POST['SubmitCreate'] == 'Create' ) {
  869.          
  870.         /// Reads the posted values
  871.         $meetingName = stripcslashes($_POST[ 'meetingName' ]);
  872.         $attendeePW = $_POST[ 'attendeePW' ]? $_POST[ 'attendeePW' ]: bigbluebutton_generatePasswd(6, 2);
  873.         $moderatorPW = $_POST[ 'moderatorPW' ]? $_POST[ 'moderatorPW' ]: bigbluebutton_generatePasswd(6, 2, $attendeePW);
  874.         $waitForModerator = (isset($_POST[ 'waitForModerator' ]) && $_POST[ 'waitForModerator' ] == 'True')? true: false;
  875.         $recorded = (isset($_POST[ 'recorded' ]) && $_POST[ 'recorded' ] == 'True')? true: false;
  876.         $meetingVersion = time();
  877.         /// Assign a random seed to generate unique ID on a BBB server
  878.         $meetingID = bigbluebutton_generateToken();
  879.  
  880.  
  881.         //Checks to see if the meeting name, attendee password or moderator password was left blank
  882.         if($meetingName == '' || $attendeePW == '' || $moderatorPW == ''){
  883.             //If the meeting name was left blank, the user is prompted to fill it out
  884.             $out .= '<div class="updated">
  885.            <p>
  886.            <strong>All fields must be filled.</strong>
  887.            </p>
  888.            </div>';
  889.              
  890.         } else {
  891.             $alreadyExists = false;
  892.              
  893.             //Checks the meeting to be created to see if it already exists in wordpress database
  894.             $table_name = $wpdb->prefix . "bigbluebutton";
  895.             $listOfMeetings = $wpdb->get_results("SELECT meetingID, meetingName FROM ".$table_name);
  896.              
  897.             foreach ($listOfMeetings as $meeting) {
  898.                 if($meeting->meetingName == $meetingName){
  899.                     $alreadyExists = true;
  900.                     //Alerts the user to choose a different name
  901.                     $out .= '<div class="updated">
  902.                    <p>
  903.                    <strong>'.$meetingName.' meeting room already exists. Please select a different name.</strong>
  904.                    </p>
  905.                    </div>';
  906.                     break;
  907.                 }
  908.             }
  909.              
  910.             //If the meeting doesn't exist in the wordpress database then create it
  911.             if(!$alreadyExists){
  912.                 $rows_affected = $wpdb->insert( $table_name, array( 'meetingID' => $meetingID, 'meetingName' => $meetingName, 'meetingVersion' => $meetingVersion, 'attendeePW' => $attendeePW, 'moderatorPW' => $moderatorPW, 'waitForModerator' => $waitForModerator? 1: 0, 'recorded' => $recorded? 1: 0) );
  913.  
  914.                 $out .= '<div class="updated">
  915.                <p>
  916.                <strong>Meeting Room Created.</strong>
  917.                </p>
  918.                </div>';
  919.  
  920.             }
  921.              
  922.         }
  923.  
  924.     }
  925.  
  926.     //Form to create a meeting, the fields are the meeting name, and the optional fields are the attendee password and moderator password
  927.     $out .= '
  928.    <form name="form1" method="post" action="">
  929.    <p>Meeting Room Name: <input type="text" name="meetingName" value="" size="20"></p>
  930.    <p>Attendee Password: <input type="text" name="attendeePW" value="" size="20"></p>
  931.    <p>Moderator Password: <input type="text" name="moderatorPW" value="" size="20"></p>
  932.    <p>Wait for moderator to start meeting: <input type="checkbox" name="waitForModerator" value="True" /></p>
  933.    <p>Recorded meeting: <input type="checkbox" name="recorded" value="True" /></p>
  934.    <p class="submit"><input type="submit" name="SubmitCreate" class="button-primary" value="Create" /></p>
  935.    </form>
  936.    <hr />';
  937.  
  938.     return $out;
  939.  
  940. }
  941.  
  942. //================================================================================
  943. //---------------------------------List Meetings----------------------------------
  944. //================================================================================
  945. // Displays all the meetings available in the bigbluebutton server
  946. function bigbluebutton_list_meetings() {
  947.     global $wpdb, $wp_version, $current_site, $current_user;
  948.     $table_name = $wpdb->prefix . "bigbluebutton";
  949.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  950.  
  951.     //Initializes the variable that will collect the output
  952.     $out = '';
  953.  
  954.     //Displays the title of the page
  955.     $out .= "<h2>List of Meeting Rooms</h2>";
  956.  
  957.     $url_val = get_option('bigbluebutton_url');
  958.     $salt_val = get_option('bigbluebutton_salt');
  959.  
  960.     if( isset($_POST['SubmitList']) ) { //Creates then joins the meeting. If any problems occur the error is displayed
  961.         // Read the posted value and delete
  962.         $meetingID = $_POST['meetingID'];
  963.         $found = $wpdb->get_row("SELECT * FROM ".$table_name." WHERE meetingID = '".$meetingID."'");
  964.         if( $found ){
  965.             $found->meetingID = bigbluebutton_normalizeMeetingID($found->meetingID);
  966.            
  967.             //---------------------------------------------------JOIN-------------------------------------------------
  968.             if($_POST['SubmitList'] == 'Join'){
  969.                 //Extra parameters
  970.                 $duration = 0;
  971.                 $voicebridge = 0;
  972.                 $logouturl = (is_ssl()? "https://": "http://") . $_SERVER['HTTP_HOST']  . $_SERVER['REQUEST_URI'];
  973.            
  974.                 //Metadata for tagging recordings
  975.                 $metadata = array(
  976.                         'meta_origin' => 'WordPress',
  977.                         'meta_originversion' => $wp_version,
  978.                         'meta_origintag' => 'wp_plugin-bigbluebutton '.BIGBLUEBUTTON_PLUGIN_VERSION,
  979.                         'meta_originservername' => home_url(),
  980.                         'meta_originservercommonname' => get_bloginfo('name'),
  981.                         'meta_originurl' => $logouturl
  982.                 );
  983.            
  984.                 //Calls create meeting on the bigbluebutton server
  985.                 $welcome = BIGBLUEBUTTON_STRING_WELCOME;
  986.                 if( $recorded ) $welcome .= BIGBLUEBUTTON_STRING_MEETING_RECORDED;
  987.                 $response = BigBlueButton::createMeetingArray($current_user->display_name, $found->meetingID, $found->meetingName, $welcome, $found->moderatorPW, $found->attendeePW, $salt_val, $url_val, $logouturl, ($found->recorded? 'true':'false'), $duration, $voicebridge, $metadata );
  988.            
  989.                 $createNew = false;
  990.                 //Analyzes the bigbluebutton server's response
  991.                 if(!$response){//If the server is unreachable, then prompts the user of the necessary action
  992.                     $out .= '<div class="updated"><p><strong>Unable to join the meeting. Please check the url of the bigbluebutton server AND check to see if the bigbluebutton server is running.</strong></p></div>';
  993.                 }
  994.                 else if( $response['returncode'] == 'FAILED' ) { //The meeting was not created
  995.                     if($response['messageKey'] == 'idNotUnique'){
  996.                         $createNew = true;
  997.                     }
  998.                     else if($response['messageKey'] == 'checksumError'){
  999.                         $out .= '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
  1000.                     }
  1001.                     else{
  1002.                         $out .= '<div class="updated"><p><strong>'.$response['message'].'</strong></p></div>';
  1003.                     }
  1004.                 }
  1005.                 else{
  1006.                     if( !isset($response['messageKey']) || $response['messageKey'] == '' ){
  1007.                         // The meeting was just created, insert the create event to the log
  1008.                         $rows_affected = $wpdb->insert( $table_logs_name, array( 'meetingID' => $found->meetingID, 'recorded' => $found->recorded, 'timestamp' => time(), 'event' => 'Create' ) );
  1009.                     }
  1010.            
  1011.                     $bigbluebutton_joinURL = BigBlueButton::getJoinURL($found->meetingID, $current_user->display_name, $found->moderatorPW, $salt_val, $url_val );
  1012.                     $out .= '<script type="text/javascript">window.location = "'.$bigbluebutton_joinURL.'"; </script>'."\n";
  1013.                 }
  1014.                  
  1015.             }
  1016.             //---------------------------------------------------END-------------------------------------------------
  1017.             else if($_POST['SubmitList'] == 'End' ) { //Obtains the meeting information of the meeting that is going to be terminated
  1018.                  
  1019.                 //Calls endMeeting on the bigbluebutton server
  1020.                 $response = BigBlueButton::endMeeting($found->meetingID, $found->moderatorPW, $url_val, $salt_val );
  1021.            
  1022.                 //Analyzes the bigbluebutton server's response
  1023.                 if(!$response){//If the server is unreachable, then prompts the user of the necessary action
  1024.                     $out .= '<div class="updated"><p><strong>Unable to terminate the meeting. Please check the url of the bigbluebutton server AND check to see if the bigbluebutton server is running.</strong></p></div>';
  1025.                 }
  1026.                 else if( $response['returncode'] == 'SUCCESS' ) { //The meeting was terminated
  1027.                     $out .= '<div class="updated"><p><strong>'.$found->meetingName.' meeting has been terminated.</strong></p></div>';
  1028.            
  1029.                     //In case the meeting is created again it sets the meeting version to the time stamp. Therefore the meeting can be recreated before the 1 hour rule without any problems.
  1030.                     $meetingVersion = time();
  1031.                     $wpdb->update( $table_name, array( 'meetingVersion' => $meetingVersion), array( 'meetingID' => $found->meetingID ));
  1032.                      
  1033.                 }
  1034.                 else{ //If the meeting was unable to be termindated
  1035.                     if($response['messageKey'] == 'checksumError'){
  1036.                         $out .= '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
  1037.                     }
  1038.                     else{
  1039.                         $out .= '<div class="updated"><p><strong>'.$response['message'].'</strong></p></div>';
  1040.                     }
  1041.                 }
  1042.                  
  1043.                  
  1044.                  
  1045.             }
  1046.             //---------------------------------------------------DELETE-------------------------------------------------
  1047.             else if($_POST['SubmitList'] == 'Delete' ) { //Obtains the meeting information of the meeting that is going to be delete
  1048.            
  1049.                 //Calls endMeeting on the bigbluebutton server
  1050.                 $response = BigBlueButton::endMeeting($found->meetingID, $found->moderatorPW, $url_val, $salt_val );
  1051.            
  1052.                 //Analyzes the bigbluebutton server's response
  1053.                 if(!$response){//If the server is unreachable, then prompts the user of the necessary action
  1054.                     $out .= '<div class="updated"><p><strong>Unable to delete the meeting. Please check the url of the bigbluebutton server AND check to see if the bigbluebutton server is running.</strong></p></div>';
  1055.                 }
  1056.                 else if( $response['returncode'] != 'SUCCESS' && $response['messageKey'] != 'notFound' ) { //If the meeting was unable to be deleted due to an error
  1057.                     if($response['messageKey'] == 'checksumError'){
  1058.                         $out .= '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
  1059.                     }
  1060.                     else{
  1061.                         $out .= '<div class="updated"><p><strong>'.$response['message'].'</strong></p></div>';
  1062.                     }
  1063.                 }
  1064.                 else { //The meeting was terminated
  1065.                     $wpdb->query("DELETE FROM ".$table_name." WHERE meetingID = '".$meetingID."'");
  1066.                     $out .= '<div class="updated"><p><strong>'.$found->meetingName.' meeting has been deleted.</strong></p></div>';
  1067.                 }
  1068.                  
  1069.             }
  1070.         }
  1071.     }
  1072.  
  1073.     //Gets all the meetings from the wordpress db
  1074.     $listOfMeetings = $wpdb->get_results("SELECT * FROM ".$table_name." ORDER BY id");
  1075.  
  1076.     //Checks to see if there are no meetings in the wordpress db and if so alerts the user
  1077.     if(count($listOfMeetings) == 0){
  1078.         $out .= '<div class="updated"><p><strong>There are no meeting rooms.</strong></p></div>';
  1079.         return $out;
  1080.     }
  1081.  
  1082.     //Iinitiallizes the table
  1083.     $printed = false;
  1084.     //Displays the meetings in the wordpress database that have not been created yet. Avoids displaying
  1085.     //duplicate meetings, meaning if the same meeting already exists in the bigbluebutton server then it is
  1086.     //not displayed again in this for loop
  1087.     foreach ($listOfMeetings as $meeting) {
  1088.         $info = BigBlueButton::getMeetingInfoArray( bigbluebutton_normalizeMeetingID($meeting->meetingID), $meeting->moderatorPW, $url_val, $salt_val);
  1089.         //Analyzes the bigbluebutton server's response
  1090.         if(!$info){//If the server is unreachable, then prompts the user of the necessary action
  1091.             $out .= '<div class="updated"><p><strong>Unable to display the meetings. Please check the url of the bigbluebutton server AND check to see if the bigbluebutton server is running.</strong></p></div>';
  1092.             return $out;
  1093.         } else if( $info['returncode'] == 'FAILED' && $info['messageKey'] != 'notFound' && $info['messageKey'] != 'invalidPassword') { /// If the meeting was unable to be deleted due to an error
  1094.             if($info['messageKey'] == 'checksumError'){
  1095.                 $out .= '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
  1096.             }
  1097.             else{
  1098.                 $out .= '<div class="updated"><p><strong>'.$info['message'].'</strong></p></div>';
  1099.             }
  1100.             return $out;
  1101.         } else if( $info['returncode'] == 'FAILED' && ($info['messageKey'] == 'notFound' || $info['messageKey'] != 'invalidPassword') ){ /// The meeting exists only in the wordpress db
  1102.             if(!$printed){
  1103.                 $out .= bigbluebutton_print_table_header();
  1104.                 $printed = true;
  1105.             }
  1106.             $out .= '
  1107.            <form name="form1" method="post" action="">
  1108.            <input type="hidden" name="meetingID" value="'.$meeting->meetingID.'">
  1109.            <tr>
  1110.            <td>'.$meeting->meetingName.'</td>
  1111.            <td>'.$meeting->meetingID.'</td>
  1112.            <td>'.$meeting->attendeePW.'</td>
  1113.            <td>'.$meeting->moderatorPW.'</td>
  1114.            <td>'.($meeting->waitForModerator? 'Yes': 'No').'</td>
  1115.            <td>'.($meeting->recorded? 'Yes': 'No').'</td>
  1116.            <td><input type="submit" name="SubmitList" class="button-primary" value="Join" />&nbsp;
  1117.            <input type="submit" name="SubmitList" class="button-primary" value="Delete" onClick="return confirm(\'Are you sure you want to delete the meeting?\')" />
  1118.            </td>
  1119.            </tr>
  1120.            </form>';
  1121.         } else { /// The meeting exists in the bigbluebutton server
  1122.  
  1123.             if(!$printed){
  1124.                 $out .= bigbluebutton_print_table_header();
  1125.                 $printed = true;
  1126.             }
  1127.  
  1128.             $out .= '
  1129.            <form name="form1" method="post" action="">
  1130.            <input type="hidden" name="meetingID" value="'.$meeting->meetingID.'">
  1131.            <tr>
  1132.            <td>'.$meeting->meetingName.'</td>
  1133.            <td>'.$meeting->meetingID.'</td>
  1134.            <td>'.$meeting->attendeePW.'</td>
  1135.            <td>'.$meeting->moderatorPW.'</td>
  1136.            <td>'.($meeting->waitForModerator? 'Yes': 'No').'</td>
  1137.            <td>'.($meeting->recorded? 'Yes': 'No').'</td>';
  1138.             if( isset($info['hasBeenForciblyEnded']) && $info['hasBeenForciblyEnded']=='false'){
  1139.                 $out .= '
  1140.                <td><input type="submit" name="SubmitList" class="button-primary" value="Join" />&nbsp;
  1141.                <input type="submit" name="SubmitList" class="button-primary" value="End" onClick="return confirm(\'Are you sure you want to end the meeting?\')" />&nbsp;
  1142.                <input type="submit" name="SubmitList" class="button-primary" value="Delete" onClick="return confirm(\'Are you sure you want to delete the meeting?\')" />
  1143.                </td>';
  1144.             } else {
  1145.                 $out .= '
  1146.                <td>
  1147.                <!-- Meeting has ended and is temporarily unavailable. -->
  1148.                <input type="submit" name="SubmitList" class="button-primary" value="Join" />&nbsp;
  1149.                <input type="submit" name="SubmitList" class="button-primary" value="Delete" onClick="return confirm(\'Are you sure you want to delete the meeting?\')" />&nbsp;
  1150.                </td>';
  1151.             }
  1152.             $out .= '   </tr>
  1153.            </form>';
  1154.         }
  1155.     }
  1156.  
  1157.     $out .= '
  1158.    </table>
  1159.    </div><hr />';
  1160.  
  1161.     return $out;
  1162. }
  1163.  
  1164. //================================================================================
  1165. //---------------------------------List Recordings----------------------------------
  1166. //================================================================================
  1167. // Displays all the recordings available in the bigbluebutton server
  1168. function bigbluebutton_list_recordings($title=null) {
  1169.     global $wpdb, $wp_roles, $current_user;
  1170.     $table_name = $wpdb->prefix . "bigbluebutton";
  1171.     $table_logs_name = $wpdb->prefix . "bigbluebutton_logs";
  1172.  
  1173.     //Initializes the variable that will collect the output
  1174.     $out = '';
  1175.  
  1176.     //Set the role for the current user if is logged in
  1177.     $role = null;
  1178.     if( $current_user->ID ) {
  1179.         $role = "unregistered";
  1180.         foreach($wp_roles->role_names as $_role => $Role) {
  1181.             if (array_key_exists($_role, $current_user->caps)){
  1182.                 $role = $_role;
  1183.                 break;
  1184.             }
  1185.         }
  1186.     } else {
  1187.         $role = "anonymous";
  1188.     }
  1189.  
  1190.     $url_val = get_option('bigbluebutton_url');
  1191.     $salt_val = get_option('bigbluebutton_salt');
  1192.  
  1193.     $_SESSION['mt_bbb_url'] = $url_val;
  1194.     $_SESSION['mt_salt'] = $salt_val;
  1195.  
  1196.     //Gets all the meetings from wordpress database
  1197.     $listOfMeetings = $wpdb->get_results("SELECT DISTINCT meetingID FROM ".$table_logs_name." WHERE recorded = 1 ORDER BY timestamp;");
  1198.  
  1199.     $meetingIDs = '';
  1200.     $listOfRecordings = Array();
  1201.     if($listOfMeetings){
  1202.         foreach ($listOfMeetings as $meeting) {
  1203.             if( $meetingIDs != '' ) $meetingIDs .= ',';
  1204.             $meetingIDs .= $meeting->meetingID;
  1205.         }
  1206.     }
  1207.  
  1208.     $listOfRecordings = Array();
  1209.     if( $meetingIDs != '' ){
  1210.         $recordingsArray = BigBlueButton::getRecordingsArray($meetingIDs, $url_val, $salt_val);
  1211.         if( $recordingsArray['returncode'] == 'SUCCESS' && !$recordingsArray['messageKey'] ){
  1212.             $listOfRecordings = $recordingsArray['recordings'];
  1213.         }
  1214.     }
  1215.  
  1216.     //Checks to see if there are no meetings in the wordpress db and if so alerts the user
  1217.     if(count($listOfRecordings) == 0){
  1218.         $out .= '<div class="updated"><p><strong>There are no recordings available.</strong></p></div>';
  1219.         return $out;
  1220.     }
  1221.  
  1222.     //Displays the title of the page
  1223.     if($title)
  1224.         $out .= "<h2>".$title."</h2>";
  1225.  
  1226.     if ( bigbluebutton_can_manageRecordings($role) ) {
  1227.         $out .= '
  1228.        <script type="text/javascript">
  1229.            wwwroot = \''.get_bloginfo('url').'\'
  1230.            function actionCall(action, recordingid) {
  1231.  
  1232.                action = (typeof action == \'undefined\') ? \'publish\' : action;
  1233.        
  1234.                if (action == \'publish\' || (action == \'delete\' && confirm("Are you sure to delete this recording?"))) {
  1235.                    if (action == \'publish\') {
  1236.                        var el_a = document.getElementById(\'actionbar-publish-a-\'+ recordingid);
  1237.                        if (el_a) {
  1238.                            var el_img = document.getElementById(\'actionbar-publish-img-\'+ recordingid);
  1239.                            if (el_a.title == \'Hide\' ) {
  1240.                                action = \'unpublish\';
  1241.                                el_a.title = \'Show\';
  1242.                                el_img.src = wwwroot + \'/wp-content/plugins/bigbluebutton/images/show.gif\';
  1243.                            } else {
  1244.                                action = \'publish\';
  1245.                                el_a.title = \'Hide\';
  1246.                                el_img.src = wwwroot + \'/wp-content/plugins/bigbluebutton/images/hide.gif\';
  1247.                            }
  1248.                        }
  1249.                    } else {
  1250.                        // Removes the line from the table
  1251.                        jQuery(document.getElementById(\'actionbar-tr-\'+ recordingid)).remove();
  1252.                    }
  1253.                    actionurl = wwwroot + "/wp-content/plugins/bigbluebutton/php/broker.php?action=" + action + "&recordingID=" + recordingid;
  1254.                    jQuery.ajax({
  1255.                            url : actionurl,
  1256.                            async : false,
  1257.                            success : function(response){
  1258.                            },
  1259.                            error : function(xmlHttpRequest, status, error) {
  1260.                                console.debug(xmlHttpRequest);
  1261.                            }
  1262.                        });
  1263.                }
  1264.            }
  1265.        </script>';
  1266.     }
  1267.  
  1268.  
  1269.     //Print begining of the table
  1270.     $out .= '
  1271.    <div id="bbb-recordings-div" class="bbb-recordings">
  1272.    <table class="stats" cellspacing="5">
  1273.      <tr>
  1274.        <th class="hed" colspan="1">Recording</td>
  1275.        <th class="hed" colspan="1">Meeting Room Name</td>
  1276.        <th class="hed" colspan="1">Date</td>
  1277.        <th class="hed" colspan="1">Duration</td>';
  1278.     if ( bigbluebutton_can_manageRecordings($role) ) {
  1279.         $out .= '
  1280.        <th class="hedextra" colspan="1">Toolbar</td>';
  1281.     }
  1282.     $out .= '
  1283.      </tr>';
  1284.     foreach( $listOfRecordings as $recording){
  1285.         if ( bigbluebutton_can_manageRecordings($role) || $recording['published'] == 'true') {
  1286.             /// Prepare playback recording links
  1287.             $type = '';
  1288.             foreach ( $recording['playbacks'] as $playback ){
  1289.                 if ($recording['published'] == 'true'){
  1290.                     $type .= '<a href="'.$playback['url'].'" target="_new">'.$playback['type'].'</a>&#32;';
  1291.                 } else {
  1292.                     $type .= $playback['type'].'&#32;';
  1293.                 }
  1294.             }
  1295.  
  1296.             /// Prepare duration
  1297.             $endTime = isset($recording['endTime'])? floatval($recording['endTime']):0;
  1298.             $endTime = $endTime - ($endTime % 1000);
  1299.             $startTime = isset($recording['startTime'])? floatval($recording['startTime']):0;
  1300.             $startTime = $startTime - ($startTime % 1000);
  1301.             $duration = intval(($endTime - $startTime) / 60000);
  1302.  
  1303.             /// Prepare date
  1304.             //Make sure the startTime is timestamp
  1305.             if( !is_numeric($recording['startTime']) ){
  1306.                 $date = new DateTime($recording['startTime']);
  1307.                 $recording['startTime'] = date_timestamp_get($date);
  1308.             } else {
  1309.                 $recording['startTime'] = ($recording['startTime'] - $recording['startTime'] % 1000) / 1000;
  1310.             }
  1311.  
  1312.             //Format the date
  1313.             //$formatedStartDate = gmdate("M d Y H:i:s", $recording['startTime']);
  1314.             $formatedStartDate = date_i18n( "M d Y H:i:s", $recording['startTime'], false );
  1315.  
  1316.             //Print detail
  1317.             $out .= '
  1318.            <tr id="actionbar-tr-'.$recording['recordID'].'">
  1319.              <td>'.$type.'</td>
  1320.              <td>'.$recording['meetingName'].'</td>
  1321.              <td>'.$formatedStartDate.'</td>
  1322.              <td>'.$duration.' min</td>';
  1323.  
  1324.             /// Prepare actionbar if role is allowed to manage the recordings
  1325.             if ( bigbluebutton_can_manageRecordings($role) ) {
  1326.                 $action = ($recording['published'] == 'true')? 'Hide': 'Show';
  1327.                 $actionbar = "<a id=\"actionbar-publish-a-".$recording['recordID']."\" title=\"".$action."\" href=\"#\"><img id=\"actionbar-publish-img-".$recording['recordID']."\" src=\"".get_bloginfo('url')."/wp-content/plugins/bigbluebutton/images/".strtolower($action).".gif\" class=\"iconsmall\" onClick=\"actionCall('publish', '".$recording['recordID']."'); return false;\" /></a>";
  1328.                 $actionbar .= "<a id=\"actionbar-delete-a-".$recording['recordID']."\" title=\"Delete\" href=\"#\"><img id=\"actionbar-delete-img-".$recording['recordID']."\" src=\"".get_bloginfo('url')."/wp-content/plugins/bigbluebutton/images/delete.gif\" class=\"iconsmall\" onClick=\"actionCall('delete', '".$recording['recordID']."'); return false;\" /></a>";
  1329.                 $out .= '
  1330.                <td>'.$actionbar.'</td>';
  1331.             }
  1332.  
  1333.             $out .= '
  1334.            </tr>';
  1335.         }
  1336.     }
  1337.  
  1338.     //Print end of the table
  1339.     $out .= '  </table>
  1340.    </div>';
  1341.  
  1342.     return $out;
  1343.  
  1344. }
  1345.  
  1346.  
  1347. //Begins the table of list meetings with the number of columns specified
  1348. function bigbluebutton_print_table_header(){
  1349.     return '
  1350.    <div>
  1351.    <table class="stats" cellspacing="5">
  1352.      <tr>
  1353.        <th class="hed" colspan="1">Meeting Room Name</td>
  1354.        <th class="hed" colspan="1">Meeting Token</td>
  1355.        <th class="hed" colspan="1">Attendee Password</td>
  1356.        <th class="hed" colspan="1">Moderator Password</td>
  1357.        <th class="hed" colspan="1">Wait for Moderator</td>
  1358.        <th class="hed" colspan="1">Recorded</td>
  1359.        <th class="hedextra" colspan="1">Actions</td>
  1360.      </tr>';
  1361. }
  1362.  
  1363. //================================================================================
  1364. //------------------------------- Helping functions ------------------------------
  1365. //================================================================================
  1366. //Validation methods
  1367. function bigbluebutton_can_participate($role){
  1368.     $permissions = get_option('bigbluebutton_permissions');
  1369.     if( $role == 'unregistered' ) $role = 'anonymous';
  1370.     return ( isset($permissions[$role]['participate']) && $permissions[$role]['participate'] );
  1371.  
  1372. }
  1373.  
  1374. function bigbluebutton_can_manageRecordings($role){
  1375.     $permissions = get_option('bigbluebutton_permissions');
  1376.     if( $role == 'unregistered' ) $role = 'anonymous';
  1377.     return ( isset($permissions[$role]['manageRecordings']) && $permissions[$role]['manageRecordings'] );
  1378.  
  1379. }
  1380.  
  1381. function bigbluebutton_validate_defaultRole($wp_role, $bbb_role){
  1382.     $permissions = get_option('bigbluebutton_permissions');
  1383.     if( $wp_role == null || $wp_role == 'unregistered' || $wp_role == '' )
  1384.         $role = 'anonymous';
  1385.     else
  1386.         $role = $wp_role;
  1387.     return ( isset($permissions[$role]['defaultRole']) && $permissions[$role]['defaultRole'] == $bbb_role );
  1388. }
  1389.  
  1390. function bigbluebutton_generateToken($tokenLength=6){
  1391.     $token = '';
  1392.    
  1393.     if(function_exists('openssl_random_pseudo_bytes')) {
  1394.         $token .= bin2hex(openssl_random_pseudo_bytes($tokenLength));
  1395.     } else {
  1396.         //fallback to mt_rand if php < 5.3 or no openssl available
  1397.         $characters = '0123456789abcdef';
  1398.         $charactersLength = strlen($characters)-1;
  1399.         $tokenLength *= 2;
  1400.        
  1401.         //select some random characters
  1402.         for ($i = 0; $i < $tokenLength; $i++) {
  1403.             $token .= $characters[mt_rand(0, $charactersLength)];
  1404.         }
  1405.     }
  1406.      
  1407.     return $token;
  1408. }
  1409.  
  1410. function bigbluebutton_generatePasswd($numAlpha=6, $numNonAlpha=2, $salt=''){
  1411.     $listAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  1412.     $listNonAlpha = ',;:!?.$/*-+&@_+;./*&?$-!,';
  1413.    
  1414.     $pepper = '';
  1415.     do{
  1416.         $pepper = str_shuffle( substr(str_shuffle($listAlpha),0,$numAlpha) . substr(str_shuffle($listNonAlpha),0,$numNonAlpha) );
  1417.     } while($pepper == $salt);
  1418.    
  1419.     return $pepper;
  1420. }
  1421.  
  1422. function bigbluebutton_normalizeMeetingID($meetingID){
  1423.     return (strlen($meetingID) == 12)? sha1(home_url().$meetingID): $meetingID;
  1424. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement