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