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