corrade-http-templates – Rev 82

Subversion Repositories:
Rev:
<?php

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      ##
###########################################################################
## This is a script that listens for Corrade's "terse" notification and  ##
## then uses Corrade's "getavatardata" command in order to determine the ##
## shape gender and to store that data as a CSV of UUIDs by gender.      ##
###########################################################################

# Check if this is the terse notification, otherwise bail.
if(!isset($_POST['type']) || $_POST['type'] != 'avatars') return;
# Check if this is an avatar terse notification, otherwise bail.
if(!isset($_POST['entity']) || $_POST['entity'] != 'Avatar') return;

###########################################################################
##                            CONFIGURATION                              ##
###########################################################################

# Set this to the name of the group.
$GROUP = 'My Group';
# Set this to the group password.
$PASSWORD = 'mypassword';
# Set this to Corrade's HTTP Server URL.
$URL = 'http://corrade.local.site:8080';
# Visitors file.
$VISITOR_FILE = 'visitors.log';

###########################################################################
##                               INTERNALS                               ##
###########################################################################

require_once('vendor/was/utilities/src/formats/kvp/kvp.php');
require_once('vendor/was/utilities/src/IO/IO.php');
require_once('vendor/was/utilities/src/formats/csv/csv.php');

$visitors = array();
if(file_exists($VISITOR_FILE)) {
    $visitors = explode(
        PHP_EOL, 
        atomized_get_contents(
            $VISITOR_FILE
        )
    );
    array_walk(
        $visitors, 
        function($e, $k) {
            if(wasCSVToArray($e)[0] == $_POST['id']) die;
        }
    );
}

# This constructs the command as an array of key-value pairs.
$params = array(
    'command' => 'getavatardata',
    'group' => $GROUP,
    'password' => $PASSWORD,
    'agent' => $_POST['id'],
    'data' => 'VisualParameters'
);

# We now escape each key and value: this is very important, because the 
# data we send to Corrade may contain the '&' or '=' characters (we don't 
# in this example though but this will not hurt anyone).
array_walk($params,
    function(&$value, $key) {
        $value = urlencode($key)."=".urlencode($value);
    }
);
$postvars = implode('&', $params);

# Set the options, send the request and then display the outcome
if (!($curl = curl_init())) {
    print 0;
    return;
}

curl_setopt($curl, CURLOPT_URL, $URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
$return = curl_exec($curl);
curl_close($curl);

$success = urldecode(
    wasKeyValueGet(
        "success", 
        $return
    )
);

if($success == 'False') {
    # DEBUG: This will be triggered if getting the avatar data fails.
    #print $uuid." ".urldecode(
    #    wasKeyValueGet(
    #        'error', 
    #        $return
    #    )
    #)."\n";
    die;
}

$visual = wasCSVToArray(
    urldecode(
        wasKeyValueGet(
            "data", 
            $return
        )
    )
);

$new = array();
array_push($new, $_POST['id']);
switch($visual[32]) {
    case 0:
        # DEBUG
        #print $uuid.' is female '."\n";
        array_push($new, 'female');
        break;
    default:
        # DEBUG
        #print $uuid.' is male '."\n";
        array_push($new, 'male');
        break;
}

array_push($visitors, wasArrayToCSV($new));

atomized_put_contents(
    $VISITOR_FILE, 
    implode(
        PHP_EOL, 
        $visitors
    )
);