<?php
/**
 * SlideME template for SlideME Developer License.
 * 
 * This template file outlines the basic steps you need to take to implement a licensing
 * server on your web server. You will use this server to hook into SlideME's apps 
 * purchase system and provide users with licenses for your apps.
 * 
 * @author: SlideME
 * @version: 1.0 3/2012
 */

// If you setup HTTP authentication, then in the SlideME app form you need to enter
// http://username:password@yourserver.example.com/path/to/license.php
// Uncomment the following line to enable authentication
//define('HTTP_AUTH_USER', 'username');
//define('HTTP_AUTH_PASS', 'password');

// By default, this script will email you when some problem is detected with the 
// licensing API. The email address below is where emails will be sent. Be sure 
// to update this if you want to get email alerts. You can remove if you're 
// customizing the callback functions below.
// Note: This email address is used by the script only. It is not sent to SlideME
// or shared with anyone else, so you can safely use a personal email.  
// Comment the following line to disable emailing
define('ALERT_EMAIL', 'alert@example.com');

/**
 * Callback function to log a HTTP authentication failure. 
 * 
 * You should customize this function to alert you that you did not setup HTTP 
 * authentication correctly in the SlideMe app for or maybe someone is trying 
 * to hack you server. 
 */
function _slideme_http_auth_failure() {
  if(defined('ALERT_EMAIL')) {
    mail('ALERT_EMAIL', 'Failed authentication', print_r(array('Request' => $_REQUEST, 'Server' => $_SERVER), true));
  }
}

/**
 * Callback function used to handle an unknown SlideMe licensing event.
 * 
 * You should customize this function to alert you that you are using an old 
 * version of the licensing API or maybe there is something wrong with your setup. 
 */
function _slideme_default_action() {
  if(defined('ALERT_EMAIL')) {
    mail('ALERT_EMAIL', 'Unknown action', print_r(array('Request' => $_REQUEST, 'Server' => $_SERVER), true));
  }
}

/**
 * Callback function used to handle a SlideME 'acquire' licensing event.
 * This event is triggered just before the transaction is completed, when
 * the invoice is prepared for the buyer.
 * 
 * You should customize this function to generate a license key. 
 * SlideME HTTP-POST you the information about application, developer, transaction and 
 * mobile device in the following fields: 
 * - developer, developer_id
 * - application, application_id
 * - transaction_id, price, currency
 * - device_mac, device_imei
 *
 * @return string - a valid licensekey for the specified application
 */
function _slideme_acquire_action() {
  // Simple example of generating a license key
  // BEGIN: Customize here
  $licensekey = sprintf('%08X-%08X-%08X', crc32($_GET['package_name']), crc32($_GET['device_mac']), crc32($_GET['device_imei']));
  // store_licensekey_on_server($licensekey); - call to imaginary function to store the licensekey
  // END: Customize here
  return $licensekey;
}

/**
 * Callback function used to handle a SlideME 'release' licensing event.
 * This event is triggered just before the transaction is canceled (for various 
 * reasons: technical, buyer's credit, buyer's explicit request, etc).
 * 
 * You should customize this function to invalidate a license key. 
 * SlideMe HTTP-POST you the information about application, developer, transaction, 
 * mobile device and the license in the following fields: 
 * - developer, developer_id
 * - application, application_id
 * - transaction_id, price, currency
 * - device_mac, device_imei
 * - licensekey
 *
 * @return null
 */
function _slideme_release_action() {
  // Simple example of invalidating a license key
  // BEGIN: Customize here
  $licensekey = $_GET['licensekey'];
  // destroy_licensekey_on_server($licensekey); - call to imaginary function to remove the licensekey
  // END: Customize here
  return $licensekey;
}

/*
 * -----------------------------
 * DO NOT CHANGE BELOW THIS LINE
 * -----------------------------
 */ 

/**
 * Callback function used to handle a SlideME 'ping' licensing event.
 * 
 * Do not change this function. It should always return the license key "[application_id]-[transaction_id]".
 *
 * @return string - always return "[application_id]-[transaction_id]".
 */
function _slideme_ping_action() {
  return $_GET['application_id'] . '-' . $_GET['transaction_id'];
}

/*
 * Request processing
 */
//error_reporting(E_ERROR);
if (defined('HTTP_AUTH_USER')) {
  if (empty($_SERVER['PHP_AUTH_USER']) || empty($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="SlideMe App Licensing"');
    header('HTTP/1.0 401 Unauthorized');
    exit;
  }
  if ($_SERVER['PHP_AUTH_USER'] != HTTP_AUTH_USER || $_SERVER['PHP_AUTH_PW'] != HTTP_AUTH_PASS) {
    header('WWW-Authenticate: Basic realm="SlideMe App Licensing"');
    header('HTTP/1.0 401 Unauthorized');
    _slideme_http_auth_failure();
    exit;
  }
}

if(!empty($_GET['action']) && function_exists($callback = '_slideme_' . $_GET['action'] . '_action')) {
  echo json_encode((object)array('version' => '1.0', 'data' => $callback()));
} else {
  _slideme_default_action();
}