corrade-http-templates – Blame information for rev 15
?pathlinks?
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'); |
||
19 | require_once('functions.php'); |
||
20 | |||
21 | ########################################################################### |
||
22 | ## INTERNALS ## |
||
23 | ########################################################################### |
||
24 | |||
25 | #### |
||
26 | # I. First call will always return "My Inventory" so handle it early on. |
||
27 | if($inventoryFolder == 'init') { |
||
28 | echo '{ "id" : "/", "parent" : "#", "text" : "My Inventory", "data" : { "type" : "folder" }, "children" : true, "opened" : false }'; |
||
29 | return; |
||
30 | } |
||
31 | |||
32 | #### |
||
33 | # II. Send the "inventory" command to list the folder contents. |
||
34 | $params = array( |
||
35 | 'command' => 'inventory', |
||
36 | 'group' => $GROUP, |
||
37 | 'password' => $PASSWORD, |
||
38 | 'action' => 'ls', |
||
39 | 'path' => $inventoryFolder |
||
40 | ); |
||
41 | |||
42 | # We now escape each key and value: this is very important, because the |
||
43 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
44 | # in this example though but this will not hurt anyone). |
||
45 | array_walk($params, |
||
46 | function(&$value, $key) { |
||
47 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
48 | } |
||
49 | ); |
||
50 | $postvars = implode('&', $params); |
||
51 | |||
52 | # Set the options, send the request and then display the outcome |
||
53 | if (!($curl = curl_init())) { |
||
54 | print 0; |
||
55 | return; |
||
56 | } |
||
57 | |||
58 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
59 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
60 | curl_setopt($curl, CURLOPT_POST, true); |
||
61 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
62 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
63 | $result = curl_exec($curl); |
||
64 | curl_close($curl); |
||
65 | |||
66 | #### |
||
67 | # III. Return if the command did not succeed or the data is empty. |
||
68 | $success = urldecode( |
||
69 | wasKeyValueGet( |
||
70 | "success", |
||
71 | $result |
||
72 | ) |
||
73 | ); |
||
74 | |||
75 | if($success == "False") |
||
76 | return; |
||
77 | |||
78 | $data = urldecode( |
||
79 | wasKeyValueGet( |
||
80 | "data", |
||
81 | $result |
||
82 | ) |
||
83 | ); |
||
84 | |||
85 | if(!trim($data)) |
||
86 | return; |
||
87 | |||
88 | #### |
||
89 | # IV. Walk through the CSV list of the items in the directory and build a |
||
90 | # jstree-compatible array to be passed back to the javascript. |
||
91 | $contents = array(); |
||
92 | array_walk( |
||
93 | array_chunk( |
||
94 | str_getcsv($data), |
||
95 | 10 |
||
96 | ), |
||
97 | function($item) use(&$contents, $inventoryFolder) { |
||
98 | $data = array_combine( |
||
99 | wasArrayStride( |
||
100 | $item, |
||
101 | 2 |
||
102 | ), |
||
103 | wasArrayStride( |
||
104 | array_slice( |
||
105 | $item, |
||
106 | 1 |
||
107 | ), |
||
108 | 2 |
||
109 | ) |
||
110 | ); |
||
111 | array_push($contents, |
||
112 | array( |
||
113 | "id" => $inventoryFolder == '/' ? '/'.$data['item'] : $inventoryFolder.'/'.$data['item'], |
||
114 | "parent" => $inventoryFolder, |
||
115 | "data" => array( |
||
116 | 'type' => $data['type'], |
||
117 | 'time' => $data['time'] |
||
118 | ), |
||
119 | "text" => $data['name'], |
||
120 | "children" => $data['type'] == 'folder', |
||
121 | "icon" => 'images/icons/'.$data['type'].'.png', |
||
122 | "opened" => "false" |
||
123 | ) |
||
124 | ); |
||
125 | } |
||
126 | ); |
||
127 | |||
128 | #### |
||
129 | # V. Dump the array to JSON to be processed by jstree. |
||
130 | echo json_encode($contents); |
||
131 | |||
132 | ?> |
||
133 |