Location: PHPKode > projects > Murmuradmin > functions.php
<?php
/*  Murmur WebAdministrator - Administration of Murmur Server on web for end users.
    Copyright (C) 2009  Kevin Traub

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


$action = "nothing";
$serverid = "1";
$username = "0";
$password = "0";
$userid = 0;
$logged = "0";

define ('acl', 1);
define ('trav', 2);
define ('enter', 4);
define ('speak', 8);
define ('mute', 16);
define ('move', 32);
define ('make', 64);
define ('link', 128);
define ('alt', 256);

session_start();
//ob_start();

if (isset($_SESSION['server'])) {
	$serverid = $_SESSION['server'];
}
if (isset($_POST['ServerSelect'])) {
	// Server id changed... make them re-login.
	$serverid = $_REQUEST['server'];
	$_SESSION['server'] = $_REQUEST['server'];
	if (!isset($_SESSION['username'])) {
		if ($_SESSION['SuperAdmin'] != "1") {
			echo "<META HTTP-EQUIV='refresh' CONTENT='1;URL=/login.php'>";
		}
	}
}
if (isset($_REQUEST['action'])) {
	$action = $_REQUEST['action'];
}
if (isset($_REQUEST['password'])) {
	$password = $_REQUEST['password'];
}
if (isset($_REQUEST['username'])) {
	$username = $_REQUEST['username'];
}
if (isset($_SESSION['userid'])) {
	$userid = $_SESSION['userid'];
}

if (isset($_SESSION['logged'])) {
	$logged = $_SESSION['logged'];
}

$newlist = array();
//error_reporting(E_ALL);
error_reporting(E_ALL ^ E_NOTICE);

// Load ICE...
Ice_loadProfile();

$confitems = array();
$confbig = array();
$confadmin = array();
$confitems['password']="Password for unregistered users";
$confitems['timeout']="Timeout before kicking dead connections";
$confitems['welcometext']="Welcome message";
$confbig['welcometext']=1;
$confitems['registername']="Public Server Name";
$confitems['registerpassword']="Your Password";
$confitems['registerhostname']="Your Server DNS or IP";
$confitems['registerurl']="Your HTTP URL";
$confitems['certificate']="PEM Encoded SSL Certificate";
$confbig['certificate']=1;
$confitems['key']="PEM Encoded SSL Key";
$confbig['key']=1;
$confadmin['host']="IP Address";
$confadmin['port']="Port";
$confadmin['bandwidth']="Maximum per-client bandwidth bytes/sec";
$confadmin['users']="Maximum number of users on server";
$confadmin['type']="Server Type ( Free/Paid )";
$confadmin['expire']="Expiry Date (Timestamp)";

$base = $ICE->stringToProxy("Meta:tcp -h 127.0.0.1 -p 6502");
$meta = $base->ice_checkedCast("::Murmur::Meta");
$server = $meta->getServer($serverid + 0);


function createacllist($atr) {
	echo "<td><table id='hor-minimalist-c' style='width: 80;'>";
	echo "<tr><td>Write<INPUT TYPE=CHECKBOX NAME='acl' ";
	if (readbit((int)$atr, 1) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td>Traverse<INPUT TYPE=CHECKBOX NAME='trav' ";
	if (readbit((int)$atr, 2) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td><span>Enter<INPUT TYPE=CHECKBOX NAME='enter' ";
	if (readbit((int)$atr, 3) == 1) { echo "checked"; }
	echo " ></span></td></tr>\n";
	echo "<tr><td>Speak<INPUT TYPE=CHECKBOX NAME='speak' ";
	if (readbit((int)$atr, 4) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td>Mute<INPUT TYPE=CHECKBOX NAME='mute' ";
	if (readbit((int)$atr, 5) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td><span>Move<INPUT TYPE=CHECKBOX NAME='move' ";
	if (readbit((int)$atr, 6) == 1) { echo "checked"; }
	echo " ></span></td></tr>\n";
	echo "<tr><td>Make<INPUT TYPE=CHECKBOX NAME='make' ";
	if (readbit((int)$atr, 7) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td>Link<INPUT TYPE=CHECKBOX NAME='link' ";
	if (readbit((int)$atr, 8) == 1) { echo "checked"; }
	echo " ></td></tr>\n";
	echo "<tr><td>AltSpeak<INPUT TYPE=CHECKBOX NAME='alt' ";
	if (readbit((int)$atr, 9) == 1) { echo "checked"; }
	echo " ></td></tr>\n</table></td>\n";
}

//
function readacllist($atr) {

}

//BitWise Functions..
function setbit($val, $bit) {
	if (readbit($val, $bit)) return $val;
	return $val += '0x'.dechex(1<<($bit-1));
}

function clearbit($val, $bit) {
	if (!readbit($val, $bit)) return $val;
	return $val^(0+('0x'.dechex(1<<($bit-1))));
}

function readbit($val, $bit) {
	return ($val&(0+('0x'.dechex(1<<($bit-1)))))?'1':'0';
}

function debug($var, $bitlength=32) {
	for ($j=$bitlength;$j>0;$j--) {
		echo readbit($var, $j);
		if ($j%4 == 1) echo ' ';
	}
}


function GetServerList($meta)
{
	$servers = $meta->getAllServers();
	//$booted = $meta->getBootedServers();
	$count = 0;
	foreach($servers as $s) {
    	$hostname = $s->getConf("registername");
    	$hostport = $s->getConf('port');
    	$hostip = $s->getConf('host');
    	$id = $s->id();

		if ($hostname == "") {
			$hostname = "NotSet";
			$s->setConf('registername', 'NotSet');
			}
		if ($hostport == "") {
			$hostport = 64737 + $id;
			$s->setConf('port', (string)$hostport);
			}
		//echo "<br />Server: $id, $hostname, $hostip, $hostport <hr /><br />\n";
		$list = array($id, $hostname, $hostip, $hostport);
		$newlist[$count] = $list;
		//print_r($newlist);
		$count++;
	}
	return $newlist;
}

function ServerStatus($server)
{
	$Serverstatus = $s->isRunning();
	if ($Serverstatus == 1) {
		return 'Server Running<br>';
	} else {
		return 'Server Stopped<br>';
	}
}


// Random Password Generator
function generate_password()
{
	$letterlist = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
	$firstletter = $letterlist[rand(0,count($letterlist) - 1)];
	$secondletter = $letterlist[rand(0,count($letterlist) - 1)];
	$thirdletter = $letterlist[rand(0,count($letterlist) - 1)];
	$fourthletter = $letterlist[rand(0,count($letterlist) - 1)];
	$fifthletter = $letterlist[rand(0,count($letterlist) - 1)];
	$sixthletter = $letterlist[rand(0,count($letterlist) - 1)];
	$number1 = rand(10,99);
	$number2 = rand(10,99);
	$word = $firstletter.$number1.$secondletter.$thirdletter.$fourthletter.$number2.$fifthletter.$sixthletter;
	return $word;
}

function SendEmail($emailaddr, $subject, $body)
{
	$header = "From: Mumble Registration <hide@address.com>\r\n"; //optional headerfields
	mail($emailaddr, $subject, $body, $header);
}

function basecss() {
	include 'header.php';
}

function basemenu($loggedin, $meta, $serverid)
{
	echo "<div class='h_mnu_01'>\n";
	echo "<ul>\n";
	echo "<li><a href='login.php'>Home</a></li>";
	echo "<li><a href='register.php'>Register</a></li>\n";
	echo "<li><a href='order.php'>Order</a></li>\n";
	echo "<li><a href='serverlist.php'>Server List</a></li>\n";
	if (empty($loggedin)) {
		echo "<li><a href='login.php'>Login</a></li>\n";
	} else {
		echo "<li><a href='liveview.php'>LiveView</a></li>\n";
		echo "<li><a href='account.php'>Account</a></li>\n";
		echo "<li><a href='logout.php'>Logout</a></li>\n";
	}
	if ($_SESSION['IsAdmin'] == "1") {
		echo "<li><a href='users.php'>Users</a></li>\n";
		echo "<li><a href='channels.php'>Channels</a></li>\n";
		echo "<li><a href='serverconfig.php'>Server</a></li>\n";
		echo "<li><a href='viewlog.php'>Log</a></li>\n";
	}
	if ($_SESSION['SuperAdmin'] == "1") {
		$arr = GetServerList($meta);
		echo "<li><form method='POST' name='serverlist' action='".$_SERVER['PHP_SELF']."'>";
		echo ServerSelectBox($arr,$serverid);
		echo " <input type='submit' value='Go' name='ServerSelect'>";
		echo "</li>";
	}
	echo "</ul>\n";
	echo "</div>\n";
	echo "<div class='main' style='min-width:600px;' >\n";
}

function ServerSelectBox($list,$serverid)
{
	echo "<select  name='server' tabindex='-1'>\n";
	foreach($list as $s) {
		echo "<option ";
		if ($s[0] == $serverid) {
			echo "selected='selected' ";
		}
		echo "value='$s[0]'>$s[1], $s[2]:$s[3]</option>\n";
	}
	echo "</select>\n";
}


//Mainfunction: print tree structure
function print_channellist($channels, $parentid) {

	global $level;
	global $printline;


	$children = children($channels, $parentid);

	if(!empty($children)){
		foreach($children as $child => $name){

			$has_children = children($channels,$child);

			if(!empty($has_children)){
				$has_children = 1;
			}
			else{
				$has_children = 0;
			}

			if($name == end($children)){
				$last_channel = 1;
			}
			//echo "\n <!-- New channelline -->\n";
			echo "<div class='div_clear'></div>";

			if ($name == end($children)){
				$printline[$level[$child]] = 0;
			}

			else{
				$printline[$level[$child]] = 1;
			}


			for ($i = 1; $i <= $level[$child] - 1; $i++){

				if($printline[$i] == 1){
					print "<div class='div_space'><img src = 'images/list_tree_line.gif' class='image' alt=''/></div>";
				}

				else{
					print "<div class='div_space'><img src = 'images/list_empty.png' class='image' alt=''/></div>";
				}
			}

			//last branch infront of channel
			if($last_channel){
				echo "<div class='div_space'><img src = 'images/list_tree_end.gif' class='image' alt=''/></div>";
			}
			else{
				echo "<div class='div_space'><img src = 'images/list_tree_mid.gif' class='image' alt=''/></div>";
			}

			//channel symbol and channel name
			echo "<div class ='div_channel'>";
			echo "<img src = 'images/list_channel.png' class='image' alt=''/>";
			if ($_SESSION['IsAdmin'] != "1") {
				echo "<span class='text_channel'>$name";
			} else {
				echo "<span class='text_channel'><a href='".$_SERVER['PHP_SELF']."?Submit=editchan&chanid=$child'>$name</a>&nbsp&nbsp&nbsp";
				echo "&nbsp;&nbsp;&nbsp;";
				//echo "<a href='".$_SERVER['PHP_SELF']."?Submit=editchan&chanid=$child'><img src = 'images/file.gif' class='image' alt='Edit Channel' title='Edit Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=dcv&chanid=$child'><img src = 'images/trash.gif' class='image' alt='Delete Channel' title='Delete Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=addchan&chanid=$child'><img src = 'images/add.gif' class='image' alt='Add Sub-Channel' title='Add Sub-Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=chanmes&chanid=$child'><img src = 'images/chat.gif' class='image' alt='Send Message' title='Send Message' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
			}
			echo "</span></div>";
			//echo "\n <!-- End channelline -->\n";

			print_channellist($channels, $child);
			if ($showusers = 1) {
				print_users($child,has_children($child));
			}
		}
	}
}

//Mainfunction: print tree structure
function print_tree($channels, $parentid) {

	global $level;
	global $printline;


	$children = children($channels, $parentid);


	if(!empty($children)){
		foreach($children as $child => $name){

			$has_children = children($channels,$child);

			if(!empty($has_children)){
				$has_children = 1;
			}
			else{
				$has_children = 0;
			}

			if($name == end($children)){
				$last_channel = 1;
			}
			echo "\n <!-- New channelline -->\n";
			echo "<div class='div_clear'></div>";
			if ($name == end($children)){
				$printline[$level[$child]] = 0;
			}

			else{
				$printline[$level[$child]] = 1;
			}


			for ($i = 1; $i <= $level[$child] - 1; $i++){

				if($printline[$i] == 1){
					print "<div class='div_space'><img src = 'images/list_tree_line.gif' class='image' alt=''/></div>";
				}

				else{
					print "<div class='div_space'><img src = 'images/list_empty.png' class='image' alt=''/></div>";
				}
			}

			//last branch infront of channel
			if($last_channel){
				echo "<div class='div_space'><img src = 'images/list_tree_end.gif' class='image' alt=''/></div>";
			}
			else{
				echo "<div class='div_space'><img src = 'images/list_tree_mid.gif' class='image' alt=''/></div>";
			}

			//channel symbol and channel name
			echo "<div class ='div_channel'>";
			echo "<img src = 'images/list_channel.png' class='image' alt=''/>";
			if ($_SESSION['IsAdmin'] != "1") {
				echo "<span class='text_channel'>$name";
			} else {
				echo "<span class='text_channel'><a href='".$_SERVER['PHP_SELF']."?Submit=editchan&chanid=$child'>$name</a>&nbsp&nbsp&nbsp";
				echo "&nbsp;&nbsp;&nbsp;";
				//echo "<a href='".$_SERVER['PHP_SELF']."?Submit=editchan&chanid=$child'><img src = 'images/file.gif' class='image' alt='Edit Channel' title='Edit Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=dcv&chanid=$child'><img src = 'images/trash.gif' class='image' alt='Delete Channel' title='Delete Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=addchan&chanid=$child'><img src = 'images/add.gif' class='image' alt='Add Sub-Channel' title='Add Sub-Channel' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
				echo "<a href='".$_SERVER['PHP_SELF']."?Submit=chanmes&chanid=$child'><img src = 'images/chat.gif' class='image' alt='Send Message' title='Send Message' BORDER=0 HEIGHT=16 WIDTH=20/></a>";
			}
			echo "</span></div>";
			echo "\n <!-- End channelline -->\n";


			print_users($child,has_children($child),$server);
			print_tree($channels, $child);
		}
	}
}


//Return all children of an ID
function children($channels, $id){
	global $level;

	foreach($channels as $c){


		//if pid = id, push into array
		if($c->parent == $id){

			$children[$c->id] = $c->name;

			//set level of child
			if (isset($c->id)) {
				$level[$c->id] = $level[$id] + 1;
			}
		}
	}


	if(!empty($children)){
		asort($children);

	}


	if(!empty($children)){
		return $children;
	} else {
		return null;
	}


}

//display all users of a given channel
function print_users($channelid, $has_children){
	global $players;
	global $level;
	global $printline;

	//Look for players in channel and sort them
	foreach($players as $player){

		if($player->channel == $channelid){
			$users[$player->session] = $player->name;
		}

		if(!empty($users)){
		asort($users);

		}
	}

	//display users
	if(!empty($users)){
		foreach($users as $session => $username){
			echo "\n <!-- New userline -->\n";
			echo "<div class='div_clear'></div>";
			//Spaces
			for ($i = 1; $i <= $level[$channelid]; $i++){

				if($printline[$i] == 1){
					print "<div class='div_space'><img src = 'images/list_tree_line.gif' class='image' alt=''/></div>\n";
				}

				else{
					print "<div class='div_space'><img src = 'images/list_empty.png' class='image' alt=''/></div>\n";
				}
			}
			//last branch infront of an user
			if($username == end($users) && $has_children == 0){
				echo "<div class='div_space'><img src = 'images/list_tree_end.gif' class='image' alt=''/></div>\n";
			}
			else{
				echo "<div class='div_space'><img src = 'images/list_tree_mid.gif' class='image' alt=''/></div>\n";
			}

			//html chars for users;
			$username = html_chars($username);

			echo "<div class='div_user' title=\"header=[<img src='images/stats_header.png' class = 'image_stats'/>Stats for ";
			echo "$username";
			echo "] body=[<img src='images/stats_online_time.png' class = 'image_stats'/><b>Time online:</b> ";

			get_timeonline($session);

			echo "<br/><img src = 'images/stats_visitor.png' class = 'image_stats'/><b>Visitor:</b> # $session]\">";
			echo "<img src = 'images/list_user.png' class='image'/>\n";
			echo "<span class='text_user'>$username</span>\n";

			print_userflags($session);

			echo "<div class = 'div_clear'></div>\n";
			echo "\n<!-- End userline -->\n";
		}
	}
}

//display all userflags
function print_userflags($session){
	global $players;
	$uid = $players[$session]->uid;
	$uname = $players[$session]->name;

	if($players[$session]->playerid != -1){
		echo "<img src = 'images/flag_authenticated.png' class = 'image_flags' alt = ''/>\n";
	}

	if($players[$session]->mute == 1){
		echo "<img src = 'images/flag_muted_server.png' class = 'image_flags' alt = ''/>\n";
	}

	if($players[$session]->deaf == 1){
		echo "<img src = 'images/flag_deafened_server.png' class = 'image_flags' alt = ''/>\n";
	}

	if($players[$session]->selfMute == 1){
		echo "<img src = 'images/flag_muted_self.png' class = 'image_flags' alt = ''/>\n";
	}

	if($players[$session]->selfDeaf == 1){
		echo "<img src = 'images/flag_deafened_self.png' class = 'image_flags' alt = ''/>\n";
	}

	echo "</div>";
	if ($_SESSION['IsAdmin'] == "1") {
		echo "&nbsp;&nbsp;&nbsp;\n";
		//echo "<a href='moduser.php?Submit=ban&uname=$uname'><img src = 'images/ban.gif' class='image' alt='Ban User' title='Ban User' BORDER=0/></a>";
		echo "<a href='moduser.php?Submit=kick&uname=$uname&sess=$session'><img src = 'images/kick.gif' class='image' alt='Kick User' title='Kick User' BORDER=0/></a>";
		//echo "<a href='moduser.php?Submit=email&uname=$uname'><img src = 'images/email.gif' class='image' alt='Email User' title='Email User' BORDER=0/></a>";
		echo "<a href='moduser.php?Submit=send&uname=$uname&sess=$session'><img src = 'images/chat.gif' class='image' alt='Send Message' title='Send Message' BORDER=0/></a>";
	}
}

//boolean, if channeld has children
function has_children($id){
	global $channels;
	$has_children = children($channels, $id);

	if(!empty($has_children)){
		return true;
	}

	return false;
}

//onlinetime of an user
function get_timeonline($session){
	global $players;
	$onlinesecs = $players[$session]->onlinesecs;

		$onlinesecs  = $onlinesecs % (60 * 60 * 24);
        $hours = intval($onlinesecs / (60 * 60));
        $onlinesecs  = $onlinesecs % (60 * 60);
        $mins  = intval($onlinesecs / 60);
        $onlinesecs  = $onlinesecs % 60;

        if(strlen($hours)==1){
          $hours = "0".$hours;
        }
        if(strlen($mins)==1){
          $mins = "0".$mins;
        }
        if(strlen($onlinesecs)==1){
          $onlinesecs = "0".$onlinesecs;
        }
        $time = $hours.":".$mins.":".$onlinesecs;
        echo $time;

}

//correct html chars
function html_chars($string){
	$string = str_replace ("ü", "&uuml;", $string);
	$string = str_replace ("Ü", "&Uuml;", $string);
	$string = str_replace ("ö", "&ouml;", $string);
	$string = str_replace ("Ö", "&Ouml;", $string);
	$string = str_replace ("ä", "&auml;", $string);
	$string = str_replace ("Ä", "&Auml;", $string);
	$string = str_replace ("ß", "&szlig;", $string);

	return $string;
}

// Form functions...
//print a hidden textfield
function input_hidden($element_name, $values) {
     print '<input type="hidden" name="' . $element_name .'" value="';
     print htmlentities($values[$element_name]) . '">';
}

// print a single-line text box
function input_text($element_name, $values) {
     print '<input type="text" name="' . $element_name .'" value="';
     print htmlentities($values[$element_name]) . '">';
}

//print a textarea
function input_textarea($element_name, $values) {
     print '<textarea name="' . $element_name .'">';
     print htmlentities($values[$element_name]) . '</textarea>';
}

//print a radio button or checkbox
function input_radiocheck($type, $element_name,
                           $values, $element_value) {
     print '<input type="' . $type . '" name="' .
           $element_name .'" value="' . $element_value . '" ';
     if ($element_value == $values[$element_name]) {
         print ' checked="checked"';
     }
     print '/>';
}

//print a submit button
function input_submit($element_name, $label) {
     print '<input type="submit" name="' . $element_name .'" value="';
     print htmlentities($label) .'"/>';
}

// IP -> dec -> IP Functions
function ip2dec($ipaddr) {
  $base=explode(".",$ipaddr);
  $decimal=(double) $base[0]*16777216;
  $decimal+=$base[1]*65536;
  $decimal+=$base[2]*256;
  $decimal+=$base[3];
  if($decimal>2147483647) {
    $decimal-=4294967296;
  }
  return (int) $decimal;
}

function dec2ip($dec) {
  if($dec<0) {
    $dec=(double) 4294967296+$dec;
  }
  if($dec>16777215) {
    $ip=$dec-(intval($dec/256)*256);
    $dec=(double) intval($dec/256);
  } else $ip="0";
  if($dec>65535) {
    $ip=($dec-(intval($dec/256)*256)).".".$ip;
    $dec=(double) intval($dec/256);
  } else $ip="0.".$ip;
  if($dec>255) {
    $ip=($dec-(intval($dec/256)*256)).".".$ip;
    $dec=(double) intval($dec/256);
  } else $ip="0.".$ip;
  $ip=$dec.".".$ip;
  return (string) $ip;
}

// Clean user inputs...

function clean($value)
{
	if (get_magic_quotes_gpc()) $value = stripslashes($value);
	if (!is_numeric($value)) $value = mysql_real_escape_string($value);
	if (!is_numeric($value)) $value = htmlspecialchars($value);
	$value = ereg_replace("[^@A-Za-z0-9\-]", "", $value);
	return $value;
}


array_walk($_GET,'clean');
array_walk($_POST,'clean');
array_walk($_COOKIE,'clean');
extract($_GET,EXTR_PREFIX_ALL,'get');
extract($_POST,EXTR_PREFIX_ALL,'post');
extract($_COOKIE,EXTR_PREFIX_ALL,'cookie');

?>
Return current item: Murmuradmin