<?php
//******************************************************************//
// |     MAP - Mumb1e Admin Plugin   |   http://www.mumb1e.de/    | //    
// |              ___      ___   _________   _________            | //
// |             |   \    /   | |   ___   | |   ___   |           | //
// |             |    \__/    | |  |   |  | |  |   |  |           | //
// |             |  |\    /|  | |  |___|  | |  |___|  |           | //
// |             |  | \__/ |  | |   ___   | |   ______|           | //
// |             |  |      |  | |  |   |  | |  |                  | //
// |             |__|      |__| |__|   |__| |__|                  | //
// |                                                              | //
// | --- VERSION ---                                              | //
// | Build: 419                                                   | //
// | Version: V2.3.1                                              | //
// | Date: 2010-05-30                                             | //
// | --- COPYRIGTH ---                                            | //
// | Build by P.M. and M.H. | Accept the Copyrigths!              | //
// | © by Michael Koch 'aka' P.M. <princemichi@web.de>            | //
// | --- LICENSE ---                                              | //
// | MAP - Mumb1e Admin Plugin is a dual-licensed software        | //
// | MAP is released for private end-users under GPLv3            | //
// |   >>  (visit at: http://www.gnu.org/licenses/gpl-3.0.html)   | //
// | MAP is released for commercial use under a commerial license | //
// |   >>  (visit at: http://www.mumb1e.de/index.php/en/license)  | //
// | --- ATTENTION ---                                            | //
// | Changing, editing or spreading of this sourcecode in other   | //
// | scripts, or on other websites only with permission by PM!    | //
//******************************************************************//
//************************************************************************************************//
// **START**  Deklariert wichtige globale Einstellungen !!!NICHT ÄNDERN!!!
//************************************************************************************************//
// Definiert Ort des Scripts
$dir = "view";
// LÃ¤dt Globale Funktionen
include("../inc/functions.php");
//************************************************************************************************//
// **ENDE**  Deklariert wichtige globale Einstellungen !!!NICHT ÄNDERN!!!
//************************************************************************************************//
//************************************************************************************************//
// Start des Ausgabeinhalts
//************************************************************************************************//

//Übernehme Daten
$server_id = $_GET[id];
$safety_code = $_GET[sc];
$background = $_GET[bgcolour];
$fontcolour = $_GET[fontcolour];
$width = $_GET[width];
$height = $_GET[height];

//Hole Server Daten
$qry = mysql_query("SELECT * FROM `".$database_prefix."map_servers` WHERE server_id = '".$server_id."'");
$ServerData = mysql_fetch_array($qry);

// Hole Channelviewer Settings des Servers
$qry = mysql_query("SELECT * FROM `".$database_prefix."map_channelviewer` WHERE server_id = '".$server_id."'");
$ServerViewSettings = mysql_fetch_array($qry);

// Definition Seitentitel
$seitentitel = _pagetitel_view_channelviewer_external.$ServerData[name];

//Definiere Background falls nich übergeben
if ($background == "") {
	//Setze hintergrundfarbe aus DB, ansonsten mache Hintergrund transparent
	if ($ServerViewSettings[colour] == "") {
		$background = "transparent";
		} else {
			$background = "#" . $ServerViewSettings[colour];
	}
	} else {
		$background = "#" . $background;
}
//Definiere frontcolour falls nich übergeben
if ($fontcolour == "") {	
	$fontcolour = $ServerViewSettings[font_colour];
}
//Definiere width falls nich übergeben
if ($width == "") {	
	$width = $ServerViewSettings[width] - "10";
	} else {
		$width = $width - "10";
}
//Definiere height falls nich übergeben
if ($height == "") {	
	$height = $ServerViewSettings[height];
}

//Definiere Footer Text
$footer = "<a href=\"http://www.mumb1e.de/\" target=\"_top\">powered by MAP</a>";

//Check on Channelviewer extern Eingeschaltet ist
if ($ServerViewSettings[external_on] == "1") {
	//Check ob Sicherheitscode stimmt, ansonsten wird CV nicht dargestellt
	if ($ServerViewSettings[safety_code] == $safety_code) {
		//Check ICE Funktion (an-aus)    
		$qry = mysql_query("SELECT * FROM `".$database_prefix."map_settings` WHERE id = '22'");
		$ice_functions_value = mysql_fetch_array($qry);
		if ($ice_functions_value[value_2] == '1') {
			
			//Versucht nun ICE auszuführen und die kompletten CV Funktionen
			try {	
				//Lade ICE
				Ice_loadProfile();
				
				function viewtree($channelobject,$url,$servername) {
					
					global $Output;
					global $tpldir;
					
					$channeldepth = 0;
					$menustatus = array("1","1");
					$channelobject->c->name = $servername;
					$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/mumble.png>";
					$Output .= " " . $servername;
					if (count($channelobject->children) + count($channelobject->users) > 0) {
						$Output .= "<div class=\"channel\">\n";
						foreach ($channelobject->children as $children) {
							showchannels($children,$channelobject->children[count($channelobject->children)-1]->c->id,$channeldepth+1,$menustatus,$url,$tpldir); 
						}
						foreach ($channelobject->users as $users) {
							showusers($users,$channelobject->users[count($channelobject->users)-1]->userid,$channeldepth+1,$menustatus,$url,$tpldir);
						}
						$Output .= "</div>\n";
					}
				}
				
				function showchannels($channelobject,$lastid,$channeldepth,$menustatus,$url) {
					
					global $Output;
					global $tpldir;
					
					$menustatus[$channeldepth] = 1;
					if ($channelobject->c->id == $lastid) {
						$menustatus[$channeldepth] = 0;
					}
					
					$count = 1;
					while($count < $channeldepth) {
						if ($menustatus[$count] == 0) {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_space.gif>";
						} else {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_line.png>";
						}
					    $count++;
				    }
				
					if (count($channelobject->children) + count($channelobject->users) > 0) {
						if ($channelobject->c->id != $lastid) {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_mid.png>";
							} else {
								$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_end.png>";
						}
						} else {
							if ($channelobject->c->id != $lastid) {
								$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_mid.png>";
								} else {
									$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_end.png>";
							}
					}
					
					$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/channel.png>";
					$Output .= " ".$channelobject->c->name."<br>\n";
				
					if (count($channelobject->children) + count($channelobject->users) > 0) {
						$Output .= "<div class=\"channel\">\n";
						foreach ($channelobject->children as $children) {
							showchannels($children,$channelobject->children[count($channelobject->children)-1]->c->id,$channeldepth+1,$menustatus,$url);
						}
						foreach ($channelobject->users as $users) {
							showusers($users,$channelobject->users[count($channelobject->users)-1]->userid,$channeldepth+1,$menustatus,$url);
						}
						$Output .= "</div>\n";
					}
					return $menustatus;
				}
				
				function showusers($userobject,$lastid,$channeldepth,$menustatus) {
					
					global $Output;
					global $tpldir;
					
					$Output .= "<div class=\"user\">";
				
					$menustatus[$channeldepth] = 1;
					if ($channelobject->c->id == $lastid) {
						$menustatus[$channeldepth] = 0;
					}
					
					$count = 1;
					while($count < $channeldepth) {
						if ($menustatus[$count] == 0) {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_space.gif>";
							} else {
								$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_line.png>";
						}
					    $count++;
				    }
				    
					if ($userobject->userid == $lastid) {
						$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_end.png>";
						} else {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/list_tree_mid.png>";
					}
					
					if ($userobject->bytespersec >= "1") {
						$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/player.png>";
						} else {
							$Output .= "<img border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/player_null.png>";
					}
					
					$Output .= "<script type=\"text/javascript\">jkmegamenu.definemenu(\"user". $userobject->session ."\", \"menu". $userobject->session ."\", \"mouseover\")</script>";
			        $Output .= "<text id=\"user". $userobject->session ."\"> ". $userobject->name ."</text>";
			        $Output .= "<div id=\"menu". $userobject->session ."\" class=\"megamenu\">";
					$Output .= "<div class=\"column\">";
					$Output .= "<h3>" . _view_channelviewer_menu_connection . "</h3>";
					$Output .= "<ul>";
					$Output .= "<li>" . _view_channelviewer_menu_online . makeuserontime($userobject->onlinesecs) . "</li>";
					$Output .= "<li>" . _view_channelviewer_menu_idle . makeuserontime($userobject->idlesecs) . "</li>";
					$Output .= "<li>" . _view_channelviewer_menu_traffic . $userobject->bytespersec ." bps</li>";
					$Output .= "<li>" . _view_channelviewer_menu_client . $userobject->release ."</li>";
					$Output .= "<li>" . _view_channelviewer_menu_os . $userobject->os . " " . $userobject->osversion ."</li>";
					$Output .= "</ul>";
					$Output .= "</div>";
					$Output .= "<br style=\"clear: left\" />";
					$Output .= "</div>";
					
					if ($userobject->userid != -1) {
						$Output .= "<img class=view_status1 border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/auth.png>";
						$class = "1" + "1";	
						} else {
							$class = "1";	
					}
					
					if ($userobject->selfDeaf) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/deafened_self.png>";	
						$class = $class + "1";
					}
					
					if ($userobject->selfMute) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/muted_self.png>";	
						$class = $class + "1";
					}
					
					if ($userobject->suppress) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/suppressed.png>";	
						$class = $class + "1";
					}
					
					if ($userobject->mute) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/muted_server.png>";	
						$class = $class + "1";
					}
					
					if ($userobject->deaf) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/deafened_server.png>";	
						$class = $class + "1";
					}
					
					if ($userobject->comment) {
						$Output .= "<img class=view_status".$class." border=0 src=../inc/tpl/".$tpldir."/images/channelviewer/comment.png>";	
					}
					
					$Output .= "<br></div>"; 
					
					return $menustatus;
				}
		
				$base = $ICE->stringToProxy("Meta:tcp -h ".$SliceHost." -p ".$SlicePort."");
				$meta = $base->ice_checkedCast("::Murmur::Meta");
			
				$servers = $meta->getBootedServers();
				$default = $meta->getDefaultConf();
				
				if($server_id) {
				    $s = $meta->getServer(intval($server_id));
				    $port = $s->getConf('port');
				    if (! $port) {
						$port =  $default['port']+$server_id -1;
				    }
				    
				    $url = $url . $port . '/';
			
				    $name = $s->getConf("registername");
				    if (! $name) {
				      $name =  $default["registername"];
				    }
				   
					$tree = $s->getTree();
					
					$Output .= "<div class=\"channel\">\n";
					viewtree($tree,$url,$name); 
					$Output .= "</div>\n";
					
					//Developers only!!!
					// If you wanna see the entire Mumbletree Object uncomment the following line.  
					//print_r($tree);
				}  
				} catch (Ice_Exception $error) {
					$Output = _view_server_switched_off_short;
			}
			} else {
				$Output = _view_ice_switched_off_short;
		}
		}else {
			$Output = _view_sc_wrong;
	}
	}else {
		$Output = _view_cv_switched_off_short;
}
		
//Daten ausgeben  
echo show("$dir/tree_external", array("head" => _view_channelviewer_head.$ServerData[name],
									  "background" => $background,
									  "fontcolour" => $fontcolour,
									  "width" => $width,
									  "height" => $height,
									  "channel_tree" => $Output,
	                		          "seitentitel" => $seitentitel,
		                              "map_version" => $version_num,
		                              "map_date" => $version_date,
		                              "footer" => $footer
		                              ));	                				 
//************************************************************************************************//
// Ende des Ausgabeinhalts
//************************************************************************************************//
?>