<?php
/*
 * TribesNext Delegated Community Enhancement Server
 *
 * Browser JSON API
 *
 * This script provides a JSON interface to the browser systems.
 *
 * In this system, players can fill in a descriptive profile, manage a buddy list,
 * show off their favorite website, display a list of clans they are a member of (along with
 * rank and title), select an image out of a small subset to show on their profile, create
 * clans, invite players to join a clan, kick players from a clan, change a clan profile,
 * select a new tag for use by a clan, leave a clan, set clan member ranks and titles, disband
 * the clan, search for clans by name, search for players by name, and so forth. The users'
 * and clans' profiles would also show a history of when such actions were taken.
 *
 * The clan recruitment and invitation process was managed by sending special messages to a
 * prospect via the tmail system that included specially formatted links.
 */

require_once '../lib/challenge.php';
require_once 
'../lib/database.php';
require_once 
'../lib/browser.php';

/*
 * -------------------------------------------------------------------------------------------------------------------------
 *
 * Data Accessor Methods -- These wouldn't require authentication to process, but all API calls require an active session.
 *
 * -------------------------------------------------------------------------------------------------------------------------
 */

/**
 * Search for clans by name.
 * Input JSON: {"q":"name"}
 * Output JSON: [{"id": "clanid", "name": "clan name"}]
 */
function tn_json_browser_clan_searchByName($payload)
{
    
$array json_decode($payloadtrue);
    
$name = (isset($array['q']) ? $array['q'] : "");
    if (
strlen($name) == 0)
        return 
json_encode(array());
    return 
json_encode(tn_browser_searchClans($name));
}

/**
 * View clan details, suitable for showing a clan profile page.
 * Input JSON: {"id":"numeric clan id"}
 * Output JSON: {"id":"clanid", "name": "clan name", "tag": "clan tag", "append": "tag at end of name?",
 *   "recruiting": "clan recruiting?", "website": "clan website", "info": "clan profile page text",
 *   "creation": "UNIX timestamp for clan creation time", "picture": "in-game picture shown in profile",
 *   "active": "whether or not this clan is active or abandoned/disbanded"
 *   "members": [{"guid":"member GUID", "name":"member name", "tag":"member's active tag",
 *      "append":"member's tag at end of name?", "rank": "member's rank in this clan",
 *      "title": "member's title in this clan", "online": "player is currently online?"}]}
 */
function tn_json_browser_clan_viewClan($payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    if (!
preg_match("/[0-9]+/"$id))
        return 
json_encode(array());
    return 
json_encode(tn_browser_viewClan($id));
}

/**
 * View clan audit history. This can be large, so it is not included as part of the standard profile packet.
 * Input JSON: {"id":"numeric clan id"}
 * Output JSON: (see separate notes on history payloads)
 */
function tn_json_browser_clan_viewHistory($payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    if (!
preg_match("/[0-9]+/"$id))
        return 
json_encode(array());
    return 
json_encode(tn_browser_clanViewHistory($id));
}

/**
 * Search for players by name. This will match on player names that start with the query name.
 * Input JSON: {"q":"player name"}
 * Output JSON: [{"guid":"account GUID", "name":"player name", "tag":"player active tag", "append":"tag at end?"}]
 */
function tn_json_browser_user_searchByName($payload)
{
    
$array json_decode($payloadtrue);
    
$name = (isset($array['q']) ? $array['q'] : "");
    if (
strlen($name) == 0)
        return 
json_encode(array());
    return 
json_encode(tn_browser_searchPlayers($name));
}

/**
 * View user details, suitable for showing a user profile page.
 * Input JSON: {"id":"account GUID"}
 * Output JSON: {"guid":"account GUID", "name": "player name", "tag": "active tag", "append": "tag at end of name?",
 *    "creation": "UNIX timestamp of account creation", "website": "player's website", "info": "Player profile text..."
 *    "online": "is player online?", "memberships": [{"id":"clanid","name":"clan name","rank":"clan rank",
 *        "title":"clan title", "tag","tag of clan", "append":"is clan's tag appended?"}]}
 */
function tn_json_browser_user_viewUser($payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    if (!
preg_match("/[0-9]+/"$id))
        return 
json_encode(array());
    return 
json_encode(tn_browser_playerViewProfile($id));
}

/**
 * View user audit history. This is potentially large, so it is not transferred as part
 * of the standard profile packet.
 * Input JSON: {"id":"account GUID"}
 * Output JSON: (see separate notes on history payloads)
 */
function tn_json_browser_user_viewHistory($payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    if (!
preg_match("/[0-9]+/"$id))
        return 
json_encode(array());
    return 
json_encode(tn_browser_playerViewHistory($id));
}

/*
 * -------------------------------------------------------------------------------------------------------------------------
 *
 * Clan Management Functions -- The user must be authenticated and be a member of a clan to invoke these.
 *
 * -------------------------------------------------------------------------------------------------------------------------
 */

/**
 * Set clan recruiting status.
 * Input JSON: {"id":"clan ID","v":"true/false recruitment status"}
 */
function tn_json_browser_clan_setRecruiting($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$bool = (isset($array['v']) ? $array['v'] : "0");
    if (
$bool == "true" || $bool == "yes" || $bool == 1)
        
$bool 1;
    else
        
$bool 0;

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanRecruiting($id$guid$bool);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set the clan browser page info.
 * Input JSON: {"id","clan ID","v","Text for the clan's info page."}
 */
function tn_json_browser_clan_setInfo($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$val = (isset($array['v']) ? $array['v'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanInfo($id$guid$val);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set the tag on a clan.
 * Input JSON: {"id":"clan ID","tag":"tag string","append":"true/false boolean controlling tag at start/end of name"}
 */
function tn_json_browser_clan_setTag($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$tag = (isset($array['tag']) ? $array['tag'] : "");
    
$bool = (isset($array['append']) ? $array['append'] : "0");
    if (
$bool == "true" || $bool == "yes" || $bool == 1)
        
$append 1;
    else
        
$append 0;

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanTag($id$guid$tag$append);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set the clan website.
 * Input JSON: {"id":"clan ID","v":"www.example.com"}
 */
function tn_json_browser_clan_setWebsite($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$val = (isset($array['v']) ? $array['v'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanWebsite($id$guid$val);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Rename a clan.
 * Input JSON: {"id":"clan ID","v":"New Clan Name"}
 */
function tn_json_browser_clan_setName($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$val = (isset($array['v']) ? $array['v'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanName($id$guid$val);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set which picture is used on the clan info page for the in-game GUIs.
 * Input JSON: {"id":"clan ID","v":"path/to/in-game-img.png"}
 */
function tn_json_browser_clan_setPicture($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$val = (isset($array['v']) ? $array['v'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_setClanPicture($id$guid$val);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Send an invitation to join the clan to another player.
 * Input JSON: {"id":"clan ID","to":"invitee GUID"}
 */
function tn_json_browser_clan_invite($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$invitee = (isset($array['to']) ? $array['to'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_sendInvite($id$guid$invitee);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * View outstanding invites to a clan.
 * Input JSON: {"id":"clan ID"}
 * Output JSON: {"status": "success", "payload":
 *    [{"sender": {"guid": "sender GUID", "name": "sender name", "tag": "sender tag", "append": "append tag?"},
 *      "recipient": {"guid": "invitee GUID", "name": "invitee name", "tag": "invitee tag", "append": "append tag?"},
 *      "expire": "UNIX timestamp for invitation expiration"}]}
 */
function tn_json_browser_clan_viewInvites($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out = array();
        
$out['status'] = "error";
        
$out['msg'] = "Access to invitation list denied.";
        return 
json_encode($out);
    }

    
// this data access method doesn't do access control -- have to do it here
    
$viewer tn_browser_getRankInfo($id$guid);
    if (!
tn_browser_rankCanRecruit($viewer['rank']))
    {
        
$out = array();
        
$out['status'] = "error";
        
$out['msg'] = "Access to invitation list denied.";
        return 
json_encode($out);
    }

    
$out = array();
    
$out['status'] = "success";
    
$out['payload'] = tn_browser_viewPendingInvites($id);
    return 
json_encode($out);
}

/**
 * Change rank/title of a player in a clan. Can be on self.
 * Input JSON: {"id":"clan ID","to":"target GUID","rank":"integer 0 to 4","title":"Text Title in Clan"}
 */
function tn_json_browser_clan_changeRank($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$target = (isset($array['to']) ? $array['to'] : "");
    
$rank = (isset($array['rank']) ? $array['rank'] : "5");
    
$title = (isset($array['title']) ? $array['title'] : "[null]");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id) || !preg_match("/[0-9]+/"$target))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_adjustClanRank($id$guid$target$rank$title);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Kick a player from a clan.
 * Input JSON: {"id":"clan ID","to":"kickee"}
 */
function tn_json_browser_clan_kick($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$target = (isset($array['to']) ? $array['to'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id) || !preg_match("/[0-9]+/"$target))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_kickFromClan($id$guid$target);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Authorize disband of a clan, or retract that authorization (if the clan is still around).
 * Input JSON: {"id":"clan ID","v":"yes/no disband authorization status"}
 */
function tn_json_browser_clan_disband($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");
    
$bool = (isset($array['v']) ? $array['v'] : "1");
    if (
$bool == "true" || $bool == "yes" || $bool == 1)
        
$bool 1;
    else
        
$bool 0;

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_authorizeDisband($id$guid$bool);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/*
 * -------------------------------------------------------------------------------------------------------------------------
 *
 * Player Management Functions
 *
 * -------------------------------------------------------------------------------------------------------------------------
 */

/**
 * Request an account name change.
 * Input JSON: {"name": "New desired account name"}
 */
function tn_json_browser_user_changeName($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$name = (isset($array['name']) ? $array['name'] : "");

    
$out = array();

    
$ret tn_browser_playerChangeName($guid$name);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set which clan tag is active. Client must be a member of a clan to set the tag. Supplying -1 will set no active tag.
 * Input JSON: {"id", "clanid or -1"}
 */
function tn_json_browser_user_setActiveClan($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");

    
$out = array();
    
$ret tn_browser_playerSetActiveClan($guid$id);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set player profile website link.
 * Input JSON: {"site": "www.example.com"}
 */
function tn_json_browser_user_setWebsite($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$site = (isset($array['site']) ? $array['site'] : "");

    
$out = array();

    
$ret tn_browser_playerSetWebsite($guid$site);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Set player profile page text contents.
 * Input JSON: {"info": "Profile page contents..."}
 */
function tn_json_browser_user_setInfo($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$info = (isset($array['info']) ? $array['info'] : "");

    
$out = array();

    
$ret tn_browser_playerSetInfo($guid$info);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Get a list of invitations for this player.
 * Input JSON: (none)
 * Output JSON: [{"sender":{"guid":"inviter GUID", "tag": "inviter tag", "append": "inviter tag append?",
 *   "name": "inviter name"}, "clan":{"name": "clan name", "id": "clanid", "tag": "clan tag",
 *   "append": "clan tag appended?"}, "expire":"UNIX timestamp for when invitation expires"}]
 */
function tn_json_browser_user_viewInvites($guid$payload)
{
    
$ret tn_browser_viewPlayerInvites($guid);
    
$out = array();
    
$out['status'] = "success";
    
$out['payload'] = $ret;
    return 
json_encode($out);
}

/**
 * Accept a clan join invitation.
 * Input JSON: {"id": "clanid from the invitation"}
 */
function tn_json_browser_user_acceptInvite($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_acceptInvitation($id$guid);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Decline a clan join invitation.
 * Input JSON: {"id": "clan id from the invitation"}
 */
function tn_json_browser_user_rejectInvite($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_rejectInvitation($id$guid);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Leave a clan.
 * Input JSON: {"id", "clanid to leave"}
 */
function tn_json_browser_user_leaveClan($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$id = (isset($array['id']) ? $array['id'] : "");

    
$out = array();
    if (!
preg_match("/[0-9]+/"$id))
    {
        
$out['status'] = "error";
        
$out['msg'] = "invalid argument";
        return 
json_encode($out);
    }

    
$ret tn_browser_leaveClan($id$guid);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/**
 * Create a new clan.
 * Input JSON: {"tag": "desired clan tag", "append": "yes/no/true/false",
 *    "name": "desired clan name", "recruiting": "yes/no/true/false",
 *    "info": "initial clan profile page"}
 */
function tn_json_browser_user_createClan($guid$payload)
{
    
$array json_decode($payloadtrue);
    
$tag = (isset($array['tag']) ? $array['tag'] : "");
    
$bool = (isset($array['append']) ? $array['append'] : "0");
    if (
$bool == "true" || $bool == "yes" || $bool == 1)
        
$append 1;
    else
        
$append 0;

    
$name = (isset($array['name']) ? $array['name'] : "");
    
$bool = (isset($array['recruiting']) ? $array['recruiting'] : "1");
    if (
$bool == "true" || $bool == "yes" || $bool == 1)
        
$recru 1;
    else
        
$recru 0;

    
$info = (isset($array['info']) ? $array['info'] : "");

    
$out = array();
    
$ret tn_browser_createClan($guid$tag$append$name$recru$info);

    if (
stripos($ret"ERR: ") !== false)
    {
        
$out['status'] = "error";
        
$out['msg'] = substr($ret5);
    }
    else
    {
        
$out['status'] = "success";
    }
    return 
json_encode($out);
}

/*
 * -------------------------------------------------------------------------------------------------------------------------
 *
 * Request Handler
 *
 * -------------------------------------------------------------------------------------------------------------------------
 */

$link database_connectDatabase();

function 
tn_json_browser_die($type)
{
    
header("HTTP/1.1 " $type);
    print(
"<h1>Fatal Error</h1><h2>" $type "</h2>");
    die();
}

if (!isset(
$_REQUEST['guid']))
{
    
tn_json_browser_die("401 Authentication Required");
}
else if (!isset(
$_REQUEST['uuid']))
{
    
tn_json_browser_die("401 Authentication Required");
}
else if (!isset(
$_REQUEST['method']))
{
    
tn_json_browser_die("501 Not Implemented");
}
else
{
    
$guid $_REQUEST['guid'];
    
$uuid $_REQUEST['uuid'];
    
$method $_REQUEST['method'];
    
$payload = (isset($_REQUEST['payload']) ? $_REQUEST['payload'] : "");

    if (!
tn_challenge_isAuthorized($guid$uuid))
        
tn_json_browser_die("401 Authentication Required");

    
//header("Content-Type: application/json");
    
$output "";
    switch (
$method)
    {
        
// clan methods
        
case "clansearch":
            
$output tn_json_browser_clan_searchByName($payload);
            break;
        case 
"clanview":
            
$output tn_json_browser_clan_viewClan($payload);
            break;
        case 
"clanhistory":
            
$output tn_json_browser_clan_viewHistory($payload);
            break;
        case 
"clanrecruit":
            
$output tn_json_browser_clan_setRecruiting($guid$payload);
            break;
        case 
"claninfo":
            
$output tn_json_browser_clan_setInfo($guid$payload);
            break;
        case 
"clantag":
            
$output tn_json_browser_clan_setTag($guid$payload);
            break;
        case 
"clansite":
            
$output tn_json_browser_clan_setWebsite($guid$payload);
            break;
        case 
"clanname":
            
$output tn_json_browser_clan_setName($guid$payload);
            break;
        case 
"clanpicture":
            
$output tn_json_browser_clan_setPicture($guid$payload);
            break;
        case 
"claninvite":
            
$output tn_json_browser_clan_invite($guid$payload);
            break;
        case 
"clanviewinvites":
            
$output tn_json_browser_clan_viewInvites($guid$payload);
            break;
        case 
"clanrank":
            
$output tn_json_browser_clan_changeRank($guid$payload);
            break;
        case 
"clankick":
            
$output tn_json_browser_clan_kick($guid$payload);
            break;
        case 
"clandisband":
            
$output tn_json_browser_clan_disband($guid$payload);
            break;

        
// player methods
        
case "usersearch":
            
$output tn_json_browser_user_searchByName($payload);
            break;
        case 
"userview":
            
$output tn_json_browser_user_viewUser($payload);
            break;
        case 
"userhistory":
            
$output tn_json_browser_user_viewHistory($payload);
            break;
        case 
"username":
            
$output tn_json_browser_user_changeName($guid$payload);
            break;
        case 
"userclan":
            
$output tn_json_browser_user_setActiveClan($guid$payload);
            break;
        case 
"usersite":
            
$output tn_json_browser_user_setWebsite($guid$payload);
            break;
        case 
"userinfo":
            
$output tn_json_browser_user_setInfo($guid$payload);
            break;
        case 
"userinvites":
            
$output tn_json_browser_user_viewInvites($guid$payload);
            break;
        case 
"useraccept":
            
$output tn_json_browser_user_acceptInvite($guid$payload);
            break;
        case 
"userreject":
            
$output tn_json_browser_user_rejectInvite($guid$payload);
            break;
        case 
"userleave":
            
$output tn_json_browser_user_leaveClan($guid$payload);
            break;
        case 
"createclan":
            
$output tn_json_browser_user_createClan($guid$payload);
            break;

        default:
            
tn_json_browser_die("501 Not Implemented");
    }
    if (isset(
$_REQUEST['jsonp']))
    {
        
// avoid content injection exploits on jsonp parameter
        
$jsonp $_REQUEST['jsonp'];
        if (
strlen($jsonp) < 50 && preg_match("/[a-zA-Z0-9_]+/"$jsonp))
        {
            
$output $jsonp "(" $output ");";
        }
    }
    print(
$output);
}

database_disconnectDatabase($link);
?>