Page 2 of 2

Re: ManiaConnect doesn't work (bis)

Posted: 18 Dec 2011, 15:48
by Boss-Bravo
Hello all !

I still have an error on my script for Maniaconnect, and still don't understand why. Kimi and me tried a lot of things !

Code: Select all

<?php session_start();
			
			$api_username='*****|********';
			$api_password='*******';
			
			require_once __DIR__ . '/maniaplanet-ws-sdk/libraries/autoload.php';
			define('API_USERNAME', $api_username);
			define('API_PASSWORD', $api_password);
			define('SCOPE', '');

			try
			{
				$trackmania = new \Maniaplanet\WebServices\ManiaConnect\Player(API_USERNAME, API_PASSWORD);
			
				// URLs to log in and out
				$loginURL = $trackmania->getLoginURL(SCOPE);
				$logoutURL = $trackmania->getLogoutURL();
				
				if(isset($_POST['logout'])) {
					$trackmania->logout();
					header('Location: '.$logoutURL);
					exit;
				}

				// Retrive player information. If the user is not logged in, it will return false
				$player = $trackmania->getPlayer();

			}
			catch(\Maniaplanet\WebServices\Exception $e)
			{
				$player = null;
			}
			if ($player) {
				$_SESSION['player_login']=$player->login;
				$_SESSION['player_nickname']=$player->nickname;
				$_SESSION['player_path']=$player->path;
				$_SESSION['player_id']=$player->id;
			}
			else {
				$_SESSION['player_login'] = 'BUG';
			}
			
		header("Content-type: application/xhtml+xml"); echo "<?xml version='1.0' encoding='utf-8'?>\n";
		echo '<manialink>';
		
		echo '<label posn="0 0 5" text="' . $_SESSION['player_login'] . '"/>';
		echo '<timeout>0</timeout>';
		echo '</manialink>';
		
?>
We don't see the window to allow player to identify. $_SESSION['player_login'] return BUG.
Do you have an idea ?

Thanks.

Re: ManiaConnect doesn't work (bis)

Posted: 18 Dec 2011, 17:16
by gouxim
You never use $loginURL, hence the player is never authentified, so $player is always null.

You either have to put a "login" link that points to $loginURL or to make a redirection to login $URL whenever $player is false (you should be careful about infinite redirect loop though).

Re: ManiaConnect doesn't work (bis)

Posted: 19 Dec 2011, 11:46
by Kimi3
Okay I think I understood why we have to put htmlentities($loginURL).

Thanks for your help again :)

Re: ManiaConnect doesn't work (bis)

Posted: 19 Dec 2011, 12:06
by gouxim
Just a quick note about htmlentities, since I had a bug with it a couple days ago.

You want to use htmlentities to encode your data inside the xml attributes and tags. For example let's say your nickname is "<manialink>". If you put your data in the XML (or HTML) without encoding it you might end up with:

Code: Select all

<manialink>
    <label text="<manialink>" />
</manialink>
This is invalid XML, but I think the Manialink browser will still display it. However if your nickname is ""</manialink>" (note the double quote) your page will be messed up:

Code: Select all

<manialink>
    <label text=""</manialink>" />
</manialink>
So you want to use htmlentities to safely encode all data and avoid XML errors.

However, htmlentites uses ISO8859-1 charset by default, and that will fail with most special characters (that, let's be honest, are found in most Maniaplanet nicknames, including mine :lol: ).

So be sure to force htmlentities() to use UTF-8, like that:

Code: Select all

htmlentities($some_data, ENT_COMPAT, 'UTF-8');
All in all, here's a little example:

Code: Select all

<?php $nickname = 'g๐uxim ツ$o$3c0︽$z$c3fnAdeo$o$3c0︾'; ?>
<manialink>
    <label text="<?php echo htmlentities($nickname, ENT_COMPAT, 'UTF-8'); ?>" />
</manialink>
Hope it helps