error(get_string('index_checksum_error', 'bigbluebutton'));
        } else {
            error($response['message']);
        }
    }
    $joinURL = BigBlueButton::joinURL($bigbluebutton->meetingid, $username, $modPW, $salt, $url, $userID);
    redirect($joinURL);
} else {
    //
    // Login as a viewer, check if we need to wait
    //
    // "Viewer";
    if ($bigbluebutton->wait) {
        // check if the session is running; if not, user is not allowed to join
        // print "MeeingID: #".$bigbluebutton->meetingid."#<br>";
        $arr = BigBlueButton::getMeetingInfoArray($bigbluebutton->meetingid, $modPW, $url, $salt);
        $joinURL = BigBlueButton::joinURL($bigbluebutton->meetingid, $username, $viewerPW, $salt, $url, $userID);
        // print_object( $arr );
        // print "Is Meeting runnign: #".BigBlueButton::isMeetingRunning( $bigbluebutton->meetingid,  $url, $salt )."#<br>";
        // print "BBB";
        if (BigBlueButton::isMeetingRunning($bigbluebutton->meetingid, $url, $salt) == "true") {
            //
            // since the meeting is already running, we just join the session
            //
            print "<br />" . get_string('view_login_viewer', 'bigbluebutton') . "<br /><br />";
            print "<center><img src='loading.gif' /></center>";
            redirect($joinURL);
        } else {
            print "<br />" . get_string('view_wait', 'bigbluebutton') . "<br /><br />";
            print '<center><img src="polling.gif"></center>';
        }
defined('_JEXEC') or die('Restricted access');
require_once JPATH_COMPONENT . DS . 'includes' . DS . 'bbb_api.php';
$msg = null;
if (!$this->salt || $this->salt == '' || !$this->url || $this->url == '') {
    $msg = "You have to fill out the salt and url in the settings menu before you can begin.";
} else {
    if (count($this->items) > 0) {
        $printed = false;
        $k = 0;
        for ($i = 0, $n = count($this->items); $i < $n; $i++) {
            $row =& $this->items[$i];
            $checked = JHTML::_('grid.id', $i, $row->id);
            $joinLink = JRoute::_('index.php?option=com_bigbluebuttonconferencing&controller=meeting&task=join&id=' . $row->id);
            $endLink = JRoute::_('index.php?option=com_bigbluebuttonconferencing&controller=meeting&task=end&id=' . $row->id);
            $deleteLink = JRoute::_('index.php?option=com_bigbluebuttonconferencing&controller=meeting&task=delete&id=' . $row->id);
            $response = BigBlueButton::getMeetingInfoArray($row->meetingName . '[' . $row->meetingVersion . ']', $row->moderatorPW, $this->url, $this->salt);
            if (!$response) {
                //If the server is unreachable, then prompts the user of the necessary action
                $msg = "Unable to display the meetings. Please check the url of the bigbluebutton server AND check to see if the bigbluebutton server is running.";
                break;
            } else {
                if (isset($response['returncode']) && $response['returncode'] == 'FAILED' && $response['messageKey'] != 'notFound' && $response['messageKey'] != 'invalidPassword') {
                    //The meeting was not created
                    if ($response['messageKey'] == 'checksumError') {
                        $msg = "A checksum error occured. Make sure you entered the correct salt.";
                    } else {
                        $msg = $response['message'];
                    }
                    break;
                } else {
                    if (isset($response['returncode']) && $response['returncode'] == 'FAILED' && $response['messageKey'] == 'notFound' || isset($response['returncode']) && $response['returncode'] == 'FAILED' && $response['messageKey'] == 'invalidPassword' || $response['running'] == 'false') {
    $joinURL = BigBlueButton::joinURL($livewebteaching->meetingid, $username, $modPW, $salt, $url, $userID);
    ?>
	<iframe src ="<?php 
    echo $joinURL;
    ?>
" width="100%" height="100%" style="position: absolute: bottom: 0px; border: 0px;overflow:hidden;"></iframe>
	<?php 
} else {
    //
    // Login as a viewer, check if we need to wait
    //
    // "Viewer";
    if ($livewebteaching->wait) {
        // check if the session is running; if not, user is not allowed to join
        // print "MeeingID: #".$livewebteaching->meetingid."#<br>";
        $arr = BigBlueButton::getMeetingInfoArray($livewebteaching->meetingid, $modPW, $url, $salt);
        $joinURL = BigBlueButton::joinURL($livewebteaching->meetingid, $username, $viewerPW, $salt, $url, $userID);
        // print_object( $arr );
        // print "Is Meeting runnign: #".BigBlueButton::isMeetingRunning( $livewebteaching->meetingid,  $url, $salt )."#<br>";
        // print "BBB";
        if (BigBlueButton::isMeetingRunning($livewebteaching->meetingid, $url, $salt) == "true") {
            //
            // since the meeting is already running, we just join the session
            //
            //print "<br />".get_string('view_login_viewer', 'livewebteaching' )."<br /><br />";
            //print "<center><img src='loading.gif' /></center>";
            ?>
        <iframe src ="<?php 
            echo $joinURL;
            ?>
" width="100%" height="100%" style="position: absolute: bottom: 0px; border: 0px;overflow:hidden;"></iframe>
function bbb_list_meetings()
{
    global $wpdb;
    $table_name = $wpdb->prefix . "bbb_meetingRooms";
    global $url_name, $salt_name, $meetingID_name, $meetingVersion_name, $moderatorPW_name, $current_user, $attendeePW_name;
    //Displays the title of the page
    echo "<h2>List of Meeting Rooms</h2>";
    $url_val = get_option($url_name);
    $salt_val = get_option($salt_name);
    //---------------------------------------------------JOIN-----------------------------------------------
    if (isset($_POST['Submit'])) {
        //Creates then joins the meeting. If any problems occur the error is displayed
        // Read the posted value and delete
        $meetingID = $_POST[$meetingID_name];
        $found = $wpdb->get_row("SELECT * FROM " . $table_name . " WHERE meetingID = '" . $meetingID . "'");
        $moderatorPW = $found->moderatorPW;
        $attendeePW = $found->attendeePW;
        $meetingVersion = $found->meetingVersion;
        if ($_POST['Submit'] == 'Join') {
            //Calls create meeting on the bigbluebutton server
            $response = BigBlueButton::createMeetingArray($current_user->display_name, $meetingID . "[" . $meetingVersion . "]", "", $moderatorPW, $attendeePW, $salt_val, $url_val, get_option('siteurl'));
            $createNew = false;
            //Analyzes the bigbluebutton server's response
            if (!$response) {
                //If the server is unreachable, then prompts the user of the necessary action
                echo '<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>';
            } else {
                if ($response['returncode'] == 'FAILED') {
                    //The meeting was not created
                    if ($response['messageKey'] == 'idNotUnique') {
                        $createNew = true;
                    } else {
                        if ($response['messageKey'] == 'checksumError') {
                            echo '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
                        } else {
                            echo '<div class="updated"><p><strong>' . $response['message'] . '</strong></p></div>';
                        }
                    }
                } else {
                    $bbb_joinURL = BigBlueButton::joinURL($meetingID . "[" . $meetingVersion . "]", $current_user->display_name, $moderatorPW, $salt_val, $url_val);
                    ?>
<script type="text/javascript"> window.location = "<?php 
                    echo $bbb_joinURL;
                    ?>
";</script><?php 
                    return;
                }
            }
        } else {
            if ($_POST['Submit'] == 'End') {
                //Obtains the meeting information of the meeting that is going to be terminated
                //Calls endMeeting on the bigbluebutton server
                $response = BigBlueButton::endMeeting($meetingID . "[" . $meetingVersion . "]", $moderatorPW, $url_val, $salt_val);
                //Analyzes the bigbluebutton server's response
                if (!$response) {
                    //If the server is unreachable, then prompts the user of the necessary action
                    echo '<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>';
                } else {
                    if ($response['returncode'] == 'SUCCESS') {
                        //The meeting was terminated
                        echo '<div class="updated"><p><strong>' . $meetingID . ' meeting has been terminated.</strong></p></div>';
                        //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.
                        $meetingVersion = time();
                        $wpdb->update($table_name, array($meetingVersion_name => $meetingVersion), array($meetingID_name => $meetingID));
                    } else {
                        //If the meeting was unable to be termindated
                        if ($response['messageKey'] == 'checksumError') {
                            echo '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
                        } else {
                            echo '<div class="updated"><p><strong>' . $response['message'] . '</strong></p></div>';
                        }
                    }
                }
            } else {
                if ($_POST['Submit'] == 'Delete') {
                    //Obtains the meeting information of the meeting that is going to be delete
                    //Calls endMeeting on the bigbluebutton server
                    $response = BigBlueButton::endMeeting($meetingID . "[" . $meetingVersion . "]", $moderatorPW, $url_val, $salt_val);
                    //Analyzes the bigbluebutton server's response
                    if (!$response) {
                        //If the server is unreachable, then prompts the user of the necessary action
                        echo '<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>';
                    } else {
                        if ($response['returncode'] != 'SUCCESS' && $response['messageKey'] != 'notFound') {
                            //If the meeting was unable to be deleted due to an error
                            if ($response['messageKey'] == 'checksumError') {
                                echo '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
                            } else {
                                echo '<div class="updated"><p><strong>' . $response['message'] . '</strong></p></div>';
                            }
                        } else {
                            //The meeting was terminated
                            $wpdb->query("DELETE FROM " . $table_name . " WHERE meetingID = '" . $meetingID . "'");
                            echo '<div class="updated"><p><strong>' . $meetingID . ' meeting has been deleted.</strong></p></div>';
                        }
                    }
                }
            }
        }
    }
    //Gets all the meetings from the wordpress db
    $listOfMeetings = $wpdb->get_results("SELECT meetingID, meetingVersion, attendeePW, moderatorPW, waitForModerator FROM " . $table_name . " ORDER BY meetingID");
    //Checks to see if there are no meetings in the wordpress db and if so alerts the user
    if (count($listOfMeetings) == 0) {
        echo '<div class="updated"><p><strong>There are no meeting rooms.</strong></p></div>';
        return;
    }
    //Iinitiallizes the table
    $printed = false;
    //Displays the meetings in the wordpress database that have not been created yet. Avoids displaying
    //duplicate meetings, meaning if the same meeting already exists in the bbb server then it is
    //not displayed again in this for loop
    foreach ($listOfMeetings as $meeting) {
        $info = BigBlueButton::getMeetingInfoArray($meeting->meetingID . "[" . $meeting->meetingVersion . "]", $meeting->moderatorPW, $url_val, $salt_val);
        //Analyzes the bigbluebutton server's response
        if (!$info) {
            //If the server is unreachable, then prompts the user of the necessary action
            echo '<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>';
            return;
        } else {
            if ($info['returncode'] && $info['messageKey'] != 'notFound' && $info['messageKey'] != 'invalidPassword') {
                //If the meeting was unable to be deleted due to an error
                if ($info['messageKey'] == 'checksumError') {
                    echo '<div class="updated"><p><strong>A checksum error occured. Make sure you entered the correct salt.</strong></p></div>';
                } else {
                    echo '<div class="updated"><p><strong>' . $info['message'] . '</strong></p></div>';
                }
                return;
            } else {
                if ($info['returncode'] && ($info['messageKey'] == 'notFound' || $info['messageKey'] != 'invalidPassword')) {
                    //The meeting exists only in the wordpress db
                    if (!$printed) {
                        bbb_print_table_header();
                        $printed = true;
                    }
                    ?>
			<form name="form1" method="post" action="">
				<input type="hidden" name="<?php 
                    echo $meetingID_name;
                    ?>
" value="<?php 
                    echo $meeting->meetingID;
                    ?>
">
				<tr>
					<td><?php 
                    echo $meeting->meetingID;
                    ?>
</td>
					<td><?php 
                    echo $meeting->attendeePW;
                    ?>
</td>
					<td><?php 
                    echo $meeting->moderatorPW;
                    ?>
</td>
					<td>
						<?php 
                    if ($meeting->waitForModerator) {
                        echo "Yes";
                    } else {
                        echo "No";
                    }
                    ?>
					</td>
					<td>No</td>
					<td>
						<input type="submit" name="Submit" class="button-primary" value="Join" />
						<input type="submit" name="Submit" onClick="return confirm('Are you sure you want to delete the meeting?')" class="button-primary" value="Delete"/>
					</td>
				</tr>
			</form>
			<?php 
                } else {
                    //The meeting exists in the bbb server
                    if (!$printed) {
                        bbb_print_table_header();
                        $printed = true;
                    }
                    ?>
			<form name="form1" method="post" action="">
				<input type="hidden" name="<?php 
                    echo $meetingID_name;
                    ?>
" value="<?php 
                    echo $meeting->meetingID;
                    ?>
">
				<tr>
				
					<td><?php 
                    echo $meeting->meetingID;
                    ?>
</td>
					<td><?php 
                    echo $meeting->attendeePW;
                    ?>
</td>
					<td><?php 
                    echo $meeting->moderatorPW;
                    ?>
</td>
					<td>
						<?php 
                    if ($meeting->waitForModerator) {
                        echo "Yes";
                    } else {
                        echo "No";
                    }
                    ?>
					</td>
					<td>
						<?php 
                    if ($info['running'] == 'true') {
                        echo "Yes";
                    } else {
                        echo "No";
                    }
                    ?>
					</td>
					<?php 
                    if ($info['hasBeenForciblyEnded'] == 'false') {
                        ?>
								<td>
									<input type="submit" name="Submit" class="button-primary" value="Join" />
									<input type="submit" name="Submit" onClick="return confirm('Are you sure you want to end the meeting?')" class="button-primary" value="End" />
									<input type="submit" name="Submit" onClick="return confirm('Are you sure you want to delete the meeting?')" class="button-primary" value="Delete" />
								</td>
							<?php 
                    } else {
                        ?>
							<td>
								<!-- Meeting has ended and is temporarily unavailable. -->
								<input type="submit" name="Submit" class="button-primary" value="Join" />
								<input type="submit" name="Submit" onClick="return confirm('Are you sure you want to delete the meeting?')" class="button-primary" value="Delete" />
							</td>
							<?php 
                    }
                    ?>
			
				</tr>
			</form>
			<?php 
                }
            }
        }
    }
    ?>
		</table>
		</div>
	<?php 
}