Skip to content

Instantly share code, notes, and snippets.

@miniplay
Last active December 14, 2015 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miniplay/4996607 to your computer and use it in GitHub Desktop.
Save miniplay/4996607 to your computer and use it in GitHub Desktop.
Miniplay API - A template sandbox for non-hosted Flash Games. Javascript API is automatically loaded if detected. Parameters signature is automatically validated.
<?php
/* 1. Game api_key ================================================================================================== */
define("API_KEY","## YOUR_SECURE_API_KEY ##"); /* Set this to the api_key we've provided you. DO NOT DISPLAY IT!! */
/* 2. Flash game configuration ====================================================================================== */
define("FLASH_GAME_URL","http://your.host/your_flash_game.swf"); /* Set this to your swf file */
define("FLASH_GAME_WIDTH","100%");
define("FLASH_GAME_HEIGHT","100%");
define("FLASH_GAME_SWFVERSION","10.0"); /* For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection. */
define("FLASH_GAME_PLAYERINSTALL","http://api.minijuegos.com/lechuck/static/as3/swfobject/playerProductInstall.swf"); /* To use express install, set to playerProductInstall.swf, otherwise the empty string. */
define("FLASH_GAME_SWFOBJECT_JS","http://api.minijuegos.com/lechuck/static/as3/swfobject/swfobject.js"); /* Url of the swfobject.js file */
/* 3. Parameters decoding =========================================================================================== */
// Only if your system configuration or language doesn't automatically decodes it
// foreach ($_GET as $key => $value) $_GET[$key] = urldecode($value);
/* 4. 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)*/
/* 4.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.");
}
/* 4.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);
}
// 5. 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! -->
<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 ## -->
<script type="text/javascript" src="<?php echo FLASH_GAME_SWFOBJECT_JS?>"></script>
<script type="text/javascript">
var swfVersionStr = "<?php echo FLASH_GAME_SWFVERSION?>"; // For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection.
var xiSwfUrlStr = "<?php echo FLASH_GAME_PLAYERINSTALL?>"; // To use express install, set to playerProductInstall.swf, otherwise the empty string.
var flashvars = {
mp_log_level: <?php echo ( $game_devel ? 1:4 /* LogLevel 1 (ALL) if in development */ ) ?>,
mp_game_id: "<?php echo $game_id?>",
mp_game_url: "<?php echo urlencode($game_url)?>",
mp_site_url: "<?php echo urlencode($site_url)?>",
mp_api_id: "<?php echo $api_id?>",
mp_api_as3_url: "<?php echo urlencode($api_as3_url)?>",
mp_api_as3_url_bck: "<?php echo urlencode($api_as3_url_bck)?>",
mp_api_user_id: "<?php echo $api_user_id?>",
mp_api_user_token: "<?php echo $api_user_token?>",
mp_locale: "<?php echo $locale?>",
mp_timezone: "<?php echo $timezone?>"
/* ## INCLUDE HERE YOUR OWN FLASHVARS ## */
};
<?php if ($game_devel):?>flashvars["mp_game_devel"] = "1"; /* Provide "devel mode" */<?php endif;?>
var params = {};
params.quality = "high";
params.allowscriptaccess = "always";
params.allowfullscreen = "false";
params.wmode = "direct";
var attributes = {};
attributes.id = "MPFlashGame";
attributes.name = "MPFlashGame";
attributes.align = "middle";
swfobject.embedSWF(
"<?php echo FLASH_GAME_URL;?>", "flashContent",
"<?php echo FLASH_GAME_WIDTH?>", "<?php echo FLASH_GAME_HEIGHT;?>",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", ""); // JavaScript enabled so display the flashContent div in case it is not replaced with a swf object.
</script>
<div id='flashContent'><!-- FLASH CONTAINER --></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment