Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active December 14, 2015 00:09
Show Gist options
  • Save miniplay/4996722 to your computer and use it in GitHub Desktop.
Save miniplay/4996722 to your computer and use it in GitHub Desktop.
Miniplay API - A template sandbox for non-hosted games. Javascript API is automatically loaded if detected. Parameters signature is automatically validated.
<?php
/* 1. Game api_key =================================================================================================== */
if (!empty($_GET['mp_game_devel']) && !!$_GET['mp_game_devel']) {
define("API_KEY","## YOUR_SECURE_DEVELOPMENT_API_KEY ##"); /* Set this to the development api_key we've provided you. DO NOT DISPLAY IT!!! */
} else {
define("API_KEY","## YOUR_SECURE_PRODUCTION_API_KEY ##"); /* Set this to the production api_key we've provided you. DO NOT DISPLAY IT!!! */
}
/* 2. Parameters decoding ============================================================================================ */
// Only if your system configuration or language doesn't automatically decodes it
// foreach ($_GET as $key => $value) $_GET[$key] = urldecode($value);
/* 3. Parameters validation (mp_*) =================================================================================== */
/* This section must be removed if your game is external (won't run inside our sites and mini_signature will be empty) */
/* 3.1. Check signature */
if (!isset($_GET['mini_signature']) || $_GET['mini_signature'] != mpSignParameters(API_KEY,$_GET)) {
/* Invalid signature. Application dies now */
die("The 'mini_signature' is invalid.");
}
/* 3.2. Helper function definition, yeah, it will look so good inside a class :) */
/**
* Signs an array of mp_* parameters by concatenating the values to the api key
* @param string $apiKey
* @param array $parameters
* @return string signature
*/
function mpSignParameters($apiKey, array $parameters) {
ksort($parameters); /* Sort array alphabetically by its keys (Although they should be already sorted by key) */
$signatureString = "";
foreach ($parameters as $key=>$value) {
if (substr($key,0,3)==="mp_") $signatureString.=(string)$value;
}
return md5($apiKey.$signatureString);
}
/* 4. Known received parameters from the Miniplay sandbox with their default counterparts ============================ */
/* If your game is external, you'll only receive mp_user_id & mp_user_token GET parameters, the rest won't exist */
$game_id = !empty($_GET['mp_game_id']) ? $_GET['mp_game_id'] : null;
$game_uid = !empty($_GET['mp_game_uid']) ? $_GET['mp_game_uid'] : null;
$game_devel = !empty($_GET['mp_game_devel']) ? !!$_GET['mp_game_devel'] : false;
$game_url = !empty($_GET['mp_game_url']) ? $_GET['mp_game_url'] : null;
$site_url = !empty($_GET['mp_site_url']) ? ($_GET['mp_site_url']) : "http://www.miniplay.com/";
$api_id = !empty($_GET['mp_api_id']) ? $_GET['mp_api_id'] : null;
$api_js_url = !empty($_GET['mp_api_js_url']) ? ($_GET['mp_api_js_url']) : null; // Url of the javascript api to use
$api_js_url_bck = !empty($_GET['mp_api_js_url_bck']) ? ($_GET['mp_api_js_url_bck']) : null; // Url for js api connections
$api_as3_url = !empty($_GET['mp_api_as3_url']) ? ($_GET['mp_api_as3_url']) : null; // Url of the as3 api to use
$api_as3_url_bck = !empty($_GET['mp_api_as3_url_bck']) ? ($_GET['mp_api_as3_url_bck']) : null; // Url for as3 api connections
$api_user_id = !empty($_GET['mp_api_user_id']) ? $_GET['mp_api_user_id'] : null;
$api_user_token = !empty($_GET['mp_api_user_token']) ? $_GET['mp_api_user_token'] : null;
$locale = !empty($_GET['mp_locale']) ? $_GET['mp_locale'] : "en_US";
$timezone = !empty($_GET['mp_timezone']) ? $_GET['mp_timezone'] : "GMT";
?>
<html>
<head>
<!-- ## YOUR OWN HEAD ## -->
<?php if ($api_js_url && $api_id):?>
<!-- Load MINIPLAY JS API Synchronously! -->
<script id='LeChuckAPIjs' src="<?php echo $api_js_url;?>"></script>
<script type='text/javascript'>
lechuck = new LeChuckAPI({
<?php if($game_devel) echo "debug: true,";?>
id: "<?php echo $api_id?>",
site: "<?php echo $site_url?>",
user_id: "<?php echo $api_user_id?>",
user_token: "<?php echo $api_user_token?>",
locale: "<?php echo $locale?>",
url: "<?php echo $api_js_url_bck?>"
});
</script>
<?php endif;?>
</head>
<body>
<!-- ## YOUR BODY ## -->
<iframe src='http://the.url.of.your.game' width="100%" height="100%" seamless="seamless">
<!--
If your game has navigation or redirections inside it, wrap the game in this iframe, the API
doesn't support navigation because of the Cross-Domain bridge.
You can access the API through the parent JS object "parent.lechuck" (remember to use the same domain
for the iframe to prevent Cross-Domain issues)
Otherwise, if you have a game with no navigation you can just delete this iframe and use the body as usual.
-->
</iframe>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment