<?php
/* 
Module:       Mochi score module for MyArcadePlugin Pro 
Author:       Daniel Bakovic
Author URI:   http://myarcadeplugin.com
*/

$RA272EED39CFB584BB555165E3C9AA882 = dirname( dirname( dirname( dirname( dirname(__FILE__)))));
if ( file_exists($RA272EED39CFB584BB555165E3C9AA882 . '/wp-load.php') ) {
  require_once($RA272EED39CFB584BB555165E3C9AA882 . '/wp-load.php'); 
}

global $wpdb;
if ( !defined('MYARCADE_SCORES_TABLE') ) {
  define (MYARCADE_SCORES_TABLE,    $wpdb->prefix.'myarcadescores');
}
if ( !defined('MYARCADE_HIGHSCORES_TABLE') ) {
  define (MYARCADE_HIGHSCORES_TABLE,    $wpdb->prefix.'myarcadehighscores');
}
if ( !defined('MYARCADE_MEDALS_TABLE') ) {
  define (MYARCADE_MEDALS_TABLE,    $wpdb->prefix.'myarcademedals');
}

// Description from Mochimdia
/*
 * In order to use this for authentication, create a string of all the POST vars
 * and their values, EXCEPT signature. The POST vars and values should be a URL 
 * encoded string, sorted alphabetically by param name.
 * 
 * 1 Populate an array of all parameter names as keys and their values.

 * 2 Remove out the signature parameter.

 * 3 Sort the array alphabetically by the key name.

 * 4 Turn the array into a url encoded string that looks like this:
boardID=1e113c7239048b3f&datatype=number&description=This%20is%20the%20MochiScores&gameID=84993a1de4031cd8&name=rockstar&score=11519&scoreLabel=score&sessionID=sf908uw098urerjw3948&sortOrder=desc&title=My%20Top%20Scores&userID=29281&username=rockstar

 * 5 Append your secret key. For example, if your query string before was ... 
    &username=rockstar 
    
    then it would look like 
    
    &username=rockstarsdcb66e8d7676deb7e6db787cbd98d

 * 6 Compute the MD5 hash with the string, and compare your MD5 hash with the 
 *   signature parameter sent by the Bridge
 */

 
/*
  -- SCORES --
submission - "score" will be passed here. This is used to filter score submissions.
userID - Unique ID of the logged-in player. This will be what you supplied in the embed parameters.
name - Username the user input for the score.
username - Username you supplied in the embed parameters.
sessionID - Returned ID provided through the embed parameters to identify the unique user playing the game.
score - Integer indicating the score the player is submitting
gameID - Unique ID of the game the leaderboard belongs to.
boardID - Unique ID of the leaderboard.
title - Title of the leaderboard
description - Text description of the leaderboard (optionally supplied)
datatype - Value indicating the type of score. Values are either 'number' or 'time'. Note: time is supplied in milliseconds
sortOrder - Value indicating the sort direction of the scores. Values are either 'asc' or 'desc'.
scoreLabel - Label to indicate what the score represents. (ex: 'Track time', 'Gems', or 'Points')

 -- MEDALS --
submission - "medal" will be passed here. This is used to filter medal submissions. 
name - Plain test name of the medal achieved. 
description - Plain text description of the medal achieved. 
thumbnail - Fully qualified URL to a 64x64 pixel image of the medal. 
gameID - Unique ID of the game that this medal is associated with. 
userID - Unique ID of the logged-in player. This will be what you supplied in the embed parameters. 
username - User name of the player. This will be what you supplied in the embed parameters. 
sessionID - Returned ID provided through the embed parameters to identify the unique user playing the game.   
*/ 

if ( !isset($_POST['signature']) ) die();

$R19D8D91C3A4693A3128752A2FBE7FD40 = get_option('myarcade_mochi');

// Create a string
$R36282B9B598D55E20857D3D2FE7EE62F = "";
// Sort the array alphabetically by the key name
ksort($_POST);

// Turn the array into a url encoded string
foreach ($_POST as $R9B395079675C6A66FF23EA9C6C4A668E => $R7D0596C36891967F3BB9D994B4A97C19) {
  if ($R9B395079675C6A66FF23EA9C6C4A668E != 'signature') {
     $R36282B9B598D55E20857D3D2FE7EE62F .= $R9B395079675C6A66FF23EA9C6C4A668E."=".rawurlencode($R7D0596C36891967F3BB9D994B4A97C19)."&";
  }
}

$R36282B9B598D55E20857D3D2FE7EE62F = substr($R36282B9B598D55E20857D3D2FE7EE62F, 0, strlen($R36282B9B598D55E20857D3D2FE7EE62F) - 1);

// Apped the secret key 
$R36282B9B598D55E20857D3D2FE7EE62F .= $R19D8D91C3A4693A3128752A2FBE7FD40['secret_key'];

F627A0F242E003858EA19A5D9DD5EBE5A ( 'Mochi Game Submitted Data: ' . $R36282B9B598D55E20857D3D2FE7EE62F);

// Compare the MD5 with signature
if( md5($R36282B9B598D55E20857D3D2FE7EE62F) == $_POST['signature']) {
  
  // We have received valid submission

  F627A0F242E003858EA19A5D9DD5EBE5A('Mochi Signature OK!');
  
  //
  // SCORE SUBMISSION
  //
  if ( isset($_POST['submission']) && $_POST['submission'] == "score" ) {
    F627A0F242E003858EA19A5D9DD5EBE5A ('Mochi Score submission.');
    
    // Get the score sort order
    if (strtolower($_POST['sortOrder']) == 'desc' ) { 
      $R56EA904D533235F889591E04DE8C6B68 = 'DESC'; 
    } 
    else { 
      $R56EA904D533235F889591E04DE8C6B68 = 'ASC'; 
    }
    
    // Collect needed information
    $RD94C2157798BCCE313068BF1BF25A119 = array(
      'session'   => $_POST['sessionID'],
      'date'      => date('Y-m-d'),
      'datatype'  => $_POST['datatype'],
      'game_tag'  => $_POST['gameID'],
      'user_id'    => $_POST['userID'],
      'score'     => $_POST['score'],
      'sortorder' => $R56EA904D533235F889591E04DE8C6B68
    );  

    F007F675CEB78537E7BF8DAFCBBA7AEF0( $RD94C2157798BCCE313068BF1BF25A119 );      
  } // END Score submission

  //
  // MEDAL SUBMISSION
  //
  if( isset($_POST['submission']) && $_POST['submission'] == "medal" ) {
    
    // Collect needed information
    $R68D6A098DF80CD93C21C99FB4D10746B = array(
      'date'      => date('Y-m-d'),
      'game_tag'  => $_POST['gameID'],
      'user_id'   => $_POST['userID'],
      'score'     => '',
      'name'      => $_POST['name'],
      'description'  => $_POST['description'],
      'thumbnail'   => $_POST['thumbnail']
    );  
    
    FDBEDB1849077773C6F99B8C63F0807EF($R68D6A098DF80CD93C21C99FB4D10746B, $_POST['sessionID']);
    
  }
} // END signature check
else {
  F627A0F242E003858EA19A5D9DD5EBE5A('Mochi Signature NOK!');
}
?>