Location: PHPKode > projects > PhpBlock > A9.8/script/fonction.php
<?php
function echoJavascriptTestBrowser() {
	?>
<script language="javascript">
//	if(navigator.userAgent.toLowerCase().indexOf('msie') >= 0) {
	if(false) {
        alert("Your browser is not yet supported. Please get Firefox");
    	document.write("<br/><br/><br/><br/><div align='center'><b>Your browser is not yet supported. Please get <ahref=http://www.mozilla-europe.org/en/products/firefox/>Firefox</a></b><div><br/><br/><br/><br/>");
	}
</script>
	<?php
}
function showRequestInTable($parArrayTitle, $parRequest, $parWidth="100%") {?>
<table border="1" <?php if($parWidth) {echo "width='$parWidth'";}?>>
<?php
	echo "<tr>";
	foreach($parArrayTitle as $locTitle) {
		if(!isATitleColor($locTitle)) {
			if($locNextIsColor) {
				echo "<th colspan='3'>$locTitle</th>";
				$locNextIsColor=false;
			} else {
				echo "<th>$locTitle</th>";
			}
		} else {
			$locNextIsColor=true;
		}
	}
	echo "</tr>";
	$res = executer($parRequest);
	while($row = mysql_fetch_array($res)) {
		echo "<tr>";
		$row=array_unique($row);
		$j=0;
		$locSkipNext=false;
		foreach($parArrayTitle as $locTitle) {
			if($locSkipNext) {
				$locSkipNext = false;
			} else {
				$column = $row[$j];
				if(isATitleColor($locTitle)) {
					$locColor1 = Constante::getColor(intval(substr($column, 0, 2)));
					$locColor2 = Constante::getColor(intval(substr($column, 2, 2)));
					$locColor3 = Constante::getColor(intval(substr($column, 4, 2)));
					echo "<td bgcolor=#$locColor1>&nbsp;&nbsp;</td>";
					echo "<td bgcolor=#$locColor2>&nbsp;&nbsp;</td>";
					echo "<td bgcolor=#$locColor3>&nbsp;&nbsp;</td>";
					$locSkipNext = true;
				} else {
					echo "<td>$column</td>";
				}
				$j++;
			}
		}
		echo "</tr>";
	}?>
</table><?php
}
function isATitleColor($parC) {
	return $parC=="0x";
}
function formatDateInSecondToDHMS($parDateInS) {
	$locDayCount = floor($parDateInS/(24*3600));
	$locRemainingTime = $parDateInS-$locDayCount*24*3600;
	$locHourCount = floor($locRemainingTime/3600);
	$locRemainingTime -= $locHourCount*3600;
	$locMinuteCount = floor($locRemainingTime/60);
	$locRemainingTime -= $locMinuteCount*60;
	$locSecondCount = floor($locRemainingTime);

	if($locDayCount > 0) {
		$locResult = "$locDayCount d ";
	}
	if($locHourCount > 0) {
		$locResult .= "$locHourCount h ";
	}
	$locResult .= "$locMinuteCount m $locSecondCount s";
	return $locResult;
}
/**
 * @return [0.0; 0.9999999999]  
 */
function urand() {
	return mt_rand(0.0, 999999.9)/1000000.0;
}
function getIp(){
	if( isset( $_SERVER ['HTTP_X_FORWARDED_FOR'] ) ){
		$ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
	}elseif( isset( $_SERVER ['HTTP_VIA'] ) ){
		$ip = $_SERVER ['HTTP_VIA'];
	}elseif( isset( $_SERVER ['REMOTE_ADDR'] ) ){
		$ip = $_SERVER ['REMOTE_ADDR'];
	}else{
		$ip = "Unknown" ;
	}
	return $ip;
}
function getGetPost($parArgName, $parDefaultValue=false, $parBdAvailable=true) {
	if(isset($_POST["$parArgName"])) {
		$locValue = $_POST["$parArgName"];
		if($parBdAvailable) {
			$locValue = mysql_real_escape_string($locValue);
		}
	} else {
		if(isset($_GET["$parArgName"])) {
			$locValue = $_GET["$parArgName"];
			if($parBdAvailable) {
				$locValue = mysql_real_escape_string($locValue);
			}
		} else {
			$locValue = $parDefaultValue;
		}
	}
	return $locValue;
}

function getGetPostTrueFalse($parArgName, $parDefaultValue=false) {
	$locValue = getGetPost($parArgName, $parDefaultValue);
	if($locValue == "") {
		return $parDefaultValue;
	} else {
		if((strcasecmp($locValue, "true")==0)||(strcasecmp($locValue, "1")==0)||(strcasecmp($locValue, "on")==0)) {
			return true;
		} else {
			return false;
		}
	}
}

function getGetPostWithNoEmpty($parArgName, $parDefaultValue=false) {
	$locValue = getGetPost($parArgName, $parDefaultValue);
	if($locValue == "") {
		return $parDefaultValue;
	} else {
		return $locValue;
	}
}

function getWhereClauseList($parArray) {
	$locRequestPiece = " (";
	foreach($parArray as $locId)
	{
		$locRequestPiece.="$locId,";
	}
	$locRequestPiece.="-1)";
	return $locRequestPiece;
}

function getLastInsertedId() {
	return mysql_insert_id();
}
/**aide contextuelle*/
function formatNumber($parNumber, $parDigitCount)
{
	$locResult = "";
	if($parNumber == 0)
	{
		$locZeroToAdd = $parDigitCount - 1;
	} else
	{
		$countOfDigitOdNumber = 0;
		$locNumber = $parNumber;
		while($locNumber >= 1)
		{
			$countOfDigitOdNumber ++;
			$locNumber = $locNumber/10;
		}
		$locZeroToAdd = $parDigitCount - $countOfDigitOdNumber;
	}
	for($i = 0; $i < $locZeroToAdd; $i++)
	{
		$locResult .= "0";
	}
	$locResult .= $parNumber;
	return $locResult;
}

function getDirCreate_IFP($parDirPath) {
	if(!is_dir($parDirPath)) {
		if(!mkdir($parDirPath, 0700)) {
			die("mkdir($parDirPath, 0700) failed for an unknown reason");
		}
	}
	$locDir = opendir($parDirPath);
	return $locDir;
}

function getPlayerIdWithGetPost(&$locLogin, &$locPassword) {
	$locLogin = getGetPostWithNoEmpty("login");
	$locPassword = getGetPost("password", "");
	$parPlayerId = Player::getPlayerId($locLogin, $locPassword);
	return $parPlayerId;
}
function initSession(&$parPlayerId, &$parX, &$parY, &$parZ) {
	$parPlayerId = getPlayerIdWithGetPost($locLogin, $locPassword);
	unset($_COOKIE['PHPSESSID']);
	
	if($parPlayerId) {
		//If player correctly initialized
		global $gloObjectManager;
		$locPlayer = $gloObjectManager->getPlayer($parPlayerId);

		$locZoneIdTarget = $locPlayer->getStartZoneId();
		$locBlockTarget = $gloObjectManager->getZone($locZoneIdTarget);
		list($parX, $parY, $parZ) = Zone::getZoneCoord($locBlockTarget->blockId);

		session_start();
		$_SESSION["playerId"] = $parPlayerId;
		session_write_close();

		//Login sucess
		addPlayerConnection($parPlayerId, 0);
	} else {
		//Failure, not playerId initialized
		openSessionFailed("no player with login '$locLogin' and password '$locPassword'");
		exit(0);
	}
	if(isLock($parPlayerId)) {
		openSessionFailed("player with login '$locLogin' and password '$locPassword' is locked, check that you receive mail to be unlock or ask a new email on login page.");
		exit(0);
	}
}
function addPlayerConnection($parPlayerId, $parConnectionType=0) {
	executer("INSERT INTO playerConnection (playerId, connectionType, connectionDate, ip) VALUES($parPlayerId, $parConnectionType, NOW(), '".getIp()."')");
}

function openSessionFailed($parMessage=false) {
	$locURL = "Location: ".Site::$URL_TO_LOGIN_FAILURE;
	if($parMessage) {
		$locURL .= "?msg=$parMessage";
	}
	header($locURL);
}
function beginPhpSession(&$parPlayerId) {
	session_start();
	if(!isset($_SESSION["playerId"])) {
		openSessionFailed("incorrect session");
	}
	$parPlayerId = $_SESSION["playerId"];
}
function closeCurrentSession() {
	session_write_close();
}
function getSessionPlayerId(&$parPlayerId) {
	beginPhpSession($parPlayerId);
	closeCurrentSession();
}
function initLanguageWithPlayer(Player $parPlayer) {
	initLanguage($parPlayer->getLanguage());
}
function initLanguage($parLanguage="en_EN") {
	setlocale(LC_ALL, $parLanguage);
    putenv("LANG=$parLanguage");
	global $PATH_TO_CODE;
	bindtextdomain("default", $PATH_TO_CODE."/script/translation");
}
/**
 * Thanks to http://fr3.php.net/manual/en/function.gettext.php
 * e.g : 
 * $foo = 'Foo';
 * print translate("Found %1\$s Devices on Range %2\$s", "20", $foo)."<P>\n";
 */
function translate($string) {
    $arg = array();
    for($i = 1 ; $i < func_num_args(); $i++) {
        $arg[] = func_get_arg($i);
    }
    return vsprintf(gettext($string), $arg);
}
/**
 * e.g : 
 * $foo = 'Foo';
 * print dtranslate("domain", "Found %1\$s Devices on Range %2\$s", "20", $foo)."<P>\n";
 */
function dtranslate($parDomain, $string) {
    $arg = array();
    for($i = 1 ; $i < func_num_args(); $i++) {
        $arg[] = func_get_arg($i);
	}
    return vsprintf(dgettext($parDomain, $string), $arg);
}
function echoBaseHead() {
	echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n";
	echo "<base href=\"".Site::$INSTALLATION_PATH_TO_BASE."\" />\n";
	echo "<link rel='shortcut icon' href='image/logo/dungeon-32px.png' />";
	echo "<meta name='robots' content='index, follow' />";
}
function &getImageColorFromHex($parImg, $parString) {
	sscanf($parString, "%2x%2x%2x", $locRed, $locGreen, $locBlue);
	return ImageColorAllocate($parImg, $locRed, $locGreen, $locBlue);
}
function &getImageColorFromHexWithAlpha($parImg, $parString, $parAlpha) {
	sscanf($parString, "%2x%2x%2x", $locRed, $locGreen, $locBlue);
	return imagecolorallocatealpha($parImg, $locRed, $locGreen, $locBlue, $parAlpha);
}
//begin of <http://www.anyexample.com/programming/php/php_convert_rgb_from_to_html_hex_color.xml>
function rgb2html($r, $g=-1, $b=-1)
{
	if (is_array($r) && sizeof($r) == 3)
	list($r, $g, $b) = $r;

	$r = intval($r); $g = intval($g);
	$b = intval($b);

	$r = dechex($r<0?0:($r>255?255:$r));
	$g = dechex($g<0?0:($g>255?255:$g));
	$b = dechex($b<0?0:($b>255?255:$b));

	$color = (strlen($r) < 2?'0':'').$r;
	$color .= (strlen($g) < 2?'0':'').$g;
	$color .= (strlen($b) < 2?'0':'').$b;
	return '#'.$color;
}
function html2rgb($color)
{
	if ($color[0] == '#')
	$color = substr($color, 1);

	if (strlen($color) == 6)
	list($r, $g, $b) = array($color[0].$color[1],
	$color[2].$color[3],
	$color[4].$color[5]);
	elseif (strlen($color) == 3)
	list($r, $g, $b) = array($color[0], $color[1], $color[2]);
	else
	return false;

	$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

	return array($r, $g, $b);
}
//end of <http://www.anyexample.com/programming/php/php_convert_rgb_from_to_html_hex_color.xml>

function getCostString($parCost) {
	$locIsPositive=true;
	if($locCost < 0) {
		$locIsPositive=false;
	}
	$locCost = abs($parCost);
	$locLevel = 0;
	$locCurrentCost = $locCost;
	while($locCurrentCost >= 1000) {
		$locCurrentCost = $locCurrentCost/1000;
		$locCost = $locCurrentCost;
		$locLevel++;
	}
	$locCostSvgForDecimalPart = $locCost;
	$locCost = intval($locCost);
	$locCostDecimalPart = intval(($locCostSvgForDecimalPart - $locCost)*10);
	return ($locIsPositive?"":"-").$locCost.".".$locCostDecimalPart.Constante::$DIMENSION_LETTER[$locLevel];
}
function generateURand($parCount) {
	$locResult = 0;
	for($i=0; $i < $parCount; $i++) {
		$locResult += urand();
	}
	return $locResult/$parCount;
}
function getMediumExtendedRandom10($parCount=1, $parRound=false) {
	return getMediumExtendedRandom(10, $parCount, $parRound);
}
function getMediumExtendedRandom($parMax, $parCount=1, $parRound=false) {
	$locResult = 0;
	for($i=0; $i < $parCount; $i++) {
		$locResult += getExtendedRandom($parMax);
	}
    if($parRound) {
        if(mt_rand(0, 1) == 0) {
            return ceil($locResult/$parCount);
        } else {
            return floor($locResult/$parCount);
        }
    } else {
        return ($locResult/$parCount);
    }
}
function getExtendedRandom10() {
	return getExtendedRandom(10);
}
function getExtendedRandom($parMax) {
	$locValue = 0;
	$locMux = 1;
	$locReturn = false;
	$locMuxFind = false;
	do {
		$locRand = mt_rand(1, $parMax);
		//echo "rand=$locRand<br>";
		if($locRand == 1) {
			if($locMuxFind) {
				$locReturn = true;
			} else {
				$locMux = -1;
				$locMuxFind = true;
				$locRand = -1;
			}
		} elseif ($locRand <> $parMax) {
			$locReturn = true;
		} else {
			$locMuxFind = true;
		}
		$locValue += $locRand*$locMux;
	} while(!$locReturn);
	return $locValue;
}
function compareZoneIdWithZ ($z1, $z2) {
	list($locX1, $locY1, $locZ1) = Zone::getZoneCoord($z1);
	list($locX2, $locY2, $locZ2) = Zone::getZoneCoord($z2);
	if ($locZ1 == $locZ2) return 0;
	return ($locZ1 > $locZ2) ? 1 : -1;
}
function getTimeWithTimeStamp($parTimeStamp) {
	return strftime("%e/%m/%G - %H:%M' %S''", $parTimeStamp);
}
function echoOptionsWithPlayerNameAndPlayerId($parIgnorePlayerId=-1, $parOnlyPresentInGame=true) {
	if($parOnlyPresentInGame) {
		$req = "SELECT DISTINCT player.playerName, player.playerId FROM player, object WHERE object.playerId=player.playerId AND player.playerId <> $parIgnorePlayerId AND isLock=0";
	} else {
		$req = "SELECT DISTINCT playerName, playerId FROM player WHERE playerId <> $parIgnorePlayerId AND isLock=0";
	}
	$res = executer($req);
    while($row = mysql_fetch_array($res)) {
        $locOtherPlayerName = $row[0];
        echo "<option value='$row[1]'>$locOtherPlayerName</option>";
    }
}
/**echo html for image with size of 32px*/
function echoImageIcone($parPathToImage) {
	echo "<img  width=\"32px\" height=\"32px\" src=\"$parPathToImage\"/>";
}

?>
Return current item: PhpBlock