corrade-http-templates – Rev 15

Subversion Repositories:
Rev:
<?php

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      ##
###########################################################################
## This is a script that uses Corrade's "getregiondata" command in order ##
## to retrieve statistics on the current simulator.                      ##
###########################################################################

if(!isset($_POST["folder"])) return;

$inventoryFolder = $_POST["folder"];

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

require_once('config.php');
require_once('functions.php');

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

####
# I. First call will always return "My Inventory" so handle it early on.
if($inventoryFolder == 'init') {
  echo '{ "id" : "/", "parent" : "#", "text" : "My Inventory", "data" : { "type" : "folder" }, "children" : true, "opened" : false }';
  return;
}

####
# II. Send the "inventory" command to list the folder contents.
$params = array(
    'command' => 'inventory',
    'group' => $GROUP,
    'password' => $PASSWORD,
    'action' => 'ls',
    'path' => $inventoryFolder
);

# 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 = rawurlencode($key)."=".rawurlencode($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);
curl_setopt($curl, CURLOPT_ENCODING, true);
$result = curl_exec($curl);
curl_close($curl);

####
# III. Return if the command did not succeed or the data is empty.
$success = urldecode(
    wasKeyValueGet(
        "success", 
        $result
    )
);

if($success == "False")
    return;

$data = urldecode(
    wasKeyValueGet(
        "data",
        $result
    )
);

if(!trim($data))
    return;

####
# IV. Walk through the CSV list of the items in the directory and build a
# jstree-compatible array to be passed back to the javascript.
$contents = array();
array_walk(
    array_chunk(
        str_getcsv($data),
            10
    ),
    function($item) use(&$contents, $inventoryFolder) {
        $data = array_combine(
            wasArrayStride(
                $item,
                2
            ),
            wasArrayStride(
                array_slice(
                    $item,
                    1
                ),
            2
            )
        );
        array_push($contents,
            array(
                "id" => $inventoryFolder == '/' ? '/'.$data['item'] : $inventoryFolder.'/'.$data['item'],
                "parent" => $inventoryFolder,
                "data" => array(
                    'type' => $data['type'],
                    'time' => $data['time']
                ),
                "text" => $data['name'],
                "children" => $data['type'] == 'folder',
                "icon" => 'images/icons/'.$data['type'].'.png',
                "opened" => "false"
            )
        );
    }
);

####
# V. Dump the array to JSON to be processed by jstree.
echo json_encode($contents);

?>