corrade-http-templates – Blame information for rev 82

Subversion Repositories:
Rev:
Rev Author Line No. Line
15 eva 1 <?php
2  
3 ###########################################################################
4 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
5 ###########################################################################
6 ## This is a script that uses Corrade's "getregiondata" command in order ##
7 ## to retrieve statistics on the current simulator. ##
8 ###########################################################################
9  
10 if(!isset($_POST["folder"])) return;
11  
12 $inventoryFolder = $_POST["folder"];
13  
14 ###########################################################################
15 ## CONFIGURATION ##
16 ###########################################################################
17  
18 require_once('config.php');
72 office 19 require_once('vendor/was/utilities/src/formats/kvp/kvp.php');
20 require_once('vendor/was/utilities/src/collections/arrays/arrays.php');
15 eva 21  
22 ###########################################################################
23 ## INTERNALS ##
24 ###########################################################################
25  
26 ####
20 zed 27 # I. First call will always return "My Inventory" and "Library" for root.
15 eva 28 if($inventoryFolder == 'init') {
20 zed 29  
30 echo <<< EOL
31 [
32 { "id" : "/My Inventory", "parent" : "#", "text" : "My Inventory", "data" : { "type" : "folder" }, "children" : true, "opened" : false },
33 { "id" : "/Library", "parent" : "#", "text" : "Library", "data" : { "type" : "folder" }, "children" : true, "opened" : false }
34 ]
35 EOL;
36  
15 eva 37 return;
38 }
39  
40 ####
41 # II. Send the "inventory" command to list the folder contents.
42 $params = array(
43 'command' => 'inventory',
44 'group' => $GROUP,
45 'password' => $PASSWORD,
46 'action' => 'ls',
47 'path' => $inventoryFolder
48 );
49  
50 # We now escape each key and value: this is very important, because the
51 # data we send to Corrade may contain the '&' or '=' characters (we don't
52 # in this example though but this will not hurt anyone).
53 array_walk($params,
54 function(&$value, $key) {
82 office 55 $value = urlencode($key)."=".urlencode($value);
15 eva 56 }
57 );
58 $postvars = implode('&', $params);
59  
60 # Set the options, send the request and then display the outcome
61 if (!($curl = curl_init())) {
62 print 0;
63 return;
64 }
65  
66 curl_setopt($curl, CURLOPT_URL, $URL);
67 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
68 curl_setopt($curl, CURLOPT_POST, true);
69 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
70 curl_setopt($curl, CURLOPT_ENCODING, true);
71 $result = curl_exec($curl);
72 curl_close($curl);
73  
74 ####
75 # III. Return if the command did not succeed or the data is empty.
76 $success = urldecode(
77 wasKeyValueGet(
78 "success",
79 $result
80 )
81 );
82  
83 if($success == "False")
84 return;
85  
86 $data = urldecode(
87 wasKeyValueGet(
88 "data",
89 $result
90 )
91 );
92  
93 if(!trim($data))
94 return;
95  
96 ####
97 # IV. Walk through the CSV list of the items in the directory and build a
98 # jstree-compatible array to be passed back to the javascript.
99 $contents = array();
100 array_walk(
101 array_chunk(
102 str_getcsv($data),
103 10
104 ),
105 function($item) use(&$contents, $inventoryFolder) {
106 $data = array_combine(
107 wasArrayStride(
108 $item,
109 2
110 ),
111 wasArrayStride(
112 array_slice(
113 $item,
114 1
115 ),
116 2
117 )
118 );
119 array_push($contents,
120 array(
121 "id" => $inventoryFolder == '/' ? '/'.$data['item'] : $inventoryFolder.'/'.$data['item'],
122 "parent" => $inventoryFolder,
123 "data" => array(
55 office 124 'type' => strtolower($data['type']),
15 eva 125 'time' => $data['time']
126 ),
127 "text" => $data['name'],
55 office 128 "children" => strcasecmp($data['type'], 'folder') == 0,
129 "icon" => 'images/icons/'.strtolower($data['type']).'.png',
15 eva 130 "opened" => "false"
131 )
132 );
133 }
134 );
135  
136 ####
137 # V. Dump the array to JSON to be processed by jstree.
138 echo json_encode($contents);
139  
140 ?>
141