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 moves a source directory to a target directory ## |
||
7 | ## by UUID in Corrade's inventory. ## |
||
8 | ########################################################################### |
||
9 | |||
10 | ########################################################################### |
||
11 | ## CONFIGURATION ## |
||
12 | ########################################################################### |
||
13 | |||
14 | require_once('config.php'); |
||
15 | require_once('functions.php'); |
||
16 | |||
17 | ########################################################################### |
||
18 | ## INTERNALS ## |
||
19 | ########################################################################### |
||
20 | |||
21 | if(!isset($_POST['source']) || !isset($_POST['target'])) return; |
||
22 | |||
23 | $source = $_POST["source"]; |
||
24 | $target = $_POST["target"]; |
||
25 | |||
26 | ########################################################################### |
||
27 | # jstree uses the hash sign (#) as the root, so normalize it ## |
||
28 | ########################################################################### |
||
29 | if($source == "#") |
||
30 | $source = '/'; |
||
31 | |||
32 | if($target == "#") |
||
33 | $target = '/'; |
||
34 | |||
35 | ########################################################################### |
||
36 | # Moving root to root is funny. ## |
||
37 | ########################################################################### |
||
38 | if($source == '/' && $target == '/') |
||
39 | return; |
||
40 | |||
41 | ########################################################################### |
||
42 | # This template uses UUIDs to refer to inventory items and folders such ## |
||
43 | # that the source and the target path-parts must be valid v4 UUIDs. ## |
||
44 | ########################################################################### |
||
45 | $isSanePath = TRUE; |
||
46 | array_walk( |
||
47 | array_pop( |
||
48 | explode('/', |
||
49 | $source |
||
50 | ) |
||
51 | ), function($part) use(&$isSanePath) { |
||
52 | if(!preg_match("^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$", $part)) |
||
53 | $isSanePath = FALSE; |
||
54 | } |
||
55 | ); |
||
56 | |||
57 | if($isSanePath == FALSE) |
||
58 | return; |
||
59 | |||
60 | $isSanePath = TRUE; |
||
61 | array_walk( |
||
62 | array_pop( |
||
63 | explode('/', |
||
64 | $target |
||
65 | ) |
||
66 | ), function($part) use(&$isSanePath) { |
||
67 | if(!preg_match("^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$", $part)) |
||
68 | $isSanePath = FALSE; |
||
69 | } |
||
70 | ); |
||
71 | |||
72 | if($isSanePath == FALSE) |
||
73 | return; |
||
74 | |||
75 | ########################################################################### |
||
76 | ## CHECK FOR SYSTEM FOLDERS ## |
||
77 | ########################################################################### |
||
78 | #### |
||
79 | # I. Check that the source item is not a system folder. |
||
80 | # This is to prevent accidental moves of the system folders. |
||
81 | if($source != '/') { |
||
82 | $params = array( |
||
83 | 'command' => 'getinventorydata', |
||
84 | 'group' => $GROUP, |
||
85 | 'password' => $PASSWORD, |
||
86 | 'item' => array_pop( |
||
87 | explode('/', $source) |
||
88 | ), |
||
89 | 'data' => wasArrayToCSV( |
||
90 | array( |
||
91 | 'AssetType', |
||
92 | 'PreferredType' |
||
93 | ) |
||
94 | ) |
||
95 | ); |
||
96 | array_walk($params, |
||
97 | function(&$value, $key) { |
||
98 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
99 | } |
||
100 | ); |
||
101 | $postvars = implode('&', $params); |
||
102 | if (!($curl = curl_init())) { |
||
103 | print 0; |
||
104 | return; |
||
105 | } |
||
106 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
107 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
108 | curl_setopt($curl, CURLOPT_POST, true); |
||
109 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
110 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
111 | $result = curl_exec($curl); |
||
112 | curl_close($curl); |
||
113 | |||
114 | $success = urldecode( |
||
115 | wasKeyValueGet( |
||
116 | "success", |
||
117 | $result |
||
118 | ) |
||
119 | ); |
||
120 | |||
121 | if($success == 'False') { |
||
122 | echo 'Unable to get inventory UUID: '.urldecode( |
||
123 | wasKeyValueGet( |
||
124 | "error", |
||
125 | $result |
||
126 | ) |
||
127 | ); |
||
128 | return; |
||
129 | } |
||
130 | |||
131 | $data = str_getcsv( |
||
132 | urldecode( |
||
133 | wasKeyValueGet( |
||
134 | "data", |
||
135 | $result |
||
136 | ) |
||
137 | ) |
||
138 | ); |
||
139 | |||
140 | $data = array_combine( |
||
141 | wasArrayStride( |
||
142 | $data, |
||
143 | 2 |
||
144 | ), |
||
145 | wasArrayStride( |
||
146 | array_slice( |
||
147 | $data, |
||
148 | 1 |
||
149 | ), |
||
150 | 2 |
||
151 | ) |
||
152 | ); |
||
153 | |||
154 | switch($data['PreferredType']) { |
||
155 | case 'RootFolder': |
||
156 | case 'TrashFolder': |
||
157 | case 'SnapshotFolder': |
||
158 | case 'LostAndFoundFolder': |
||
159 | case 'FavoriteFolder': |
||
160 | case 'LinkFolder': |
||
161 | case 'CurrentOutfitFolder': |
||
162 | case 'OutfitFolder': |
||
163 | case 'MyOutfitsFolder': |
||
164 | return; |
||
165 | break; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | #### |
||
170 | # II. Check that the target item is not a system folder. |
||
171 | # This is to prevent accidental moves of the system folders. |
||
172 | if($target != '/') { |
||
173 | $params = array( |
||
174 | 'command' => 'getinventorydata', |
||
175 | 'group' => $GROUP, |
||
176 | 'password' => $PASSWORD, |
||
177 | 'item' => array_pop( |
||
178 | explode('/', $target) |
||
179 | ), |
||
180 | 'data' => wasArrayToCSV( |
||
181 | array( |
||
182 | 'AssetType', |
||
183 | 'PreferredType' |
||
184 | ) |
||
185 | ) |
||
186 | ); |
||
187 | array_walk($params, |
||
188 | function(&$value, $key) { |
||
189 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
190 | } |
||
191 | ); |
||
192 | $postvars = implode('&', $params); |
||
193 | if (!($curl = curl_init())) { |
||
194 | print 0; |
||
195 | return; |
||
196 | } |
||
197 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
198 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
199 | curl_setopt($curl, CURLOPT_POST, true); |
||
200 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
201 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
202 | $result = curl_exec($curl); |
||
203 | curl_close($curl); |
||
204 | |||
205 | $success = urldecode( |
||
206 | wasKeyValueGet( |
||
207 | "success", |
||
208 | $result |
||
209 | ) |
||
210 | ); |
||
211 | |||
212 | if($success == 'False') { |
||
213 | echo 'Unable to get inventory UUID: '.urldecode( |
||
214 | wasKeyValueGet( |
||
215 | "error", |
||
216 | $result |
||
217 | ) |
||
218 | ); |
||
219 | return; |
||
220 | } |
||
221 | |||
222 | $data = str_getcsv( |
||
223 | urldecode( |
||
224 | wasKeyValueGet( |
||
225 | "data", |
||
226 | $result |
||
227 | ) |
||
228 | ) |
||
229 | ); |
||
230 | |||
231 | $data = array_combine( |
||
232 | wasArrayStride( |
||
233 | $data, |
||
234 | 2 |
||
235 | ), |
||
236 | wasArrayStride( |
||
237 | array_slice( |
||
238 | $data, |
||
239 | 1 |
||
240 | ), |
||
241 | 2 |
||
242 | ) |
||
243 | ); |
||
244 | |||
245 | switch($data['PreferredType']) { |
||
246 | case 'RootFolder': |
||
247 | case 'TrashFolder': |
||
248 | case 'SnapshotFolder': |
||
249 | case 'LostAndFoundFolder': |
||
250 | case 'FavoriteFolder': |
||
251 | case 'LinkFolder': |
||
252 | case 'CurrentOutfitFolder': |
||
253 | case 'OutfitFolder': |
||
254 | case 'MyOutfitsFolder': |
||
255 | return; |
||
256 | break; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | ########################################################################### |
||
261 | ## MOVE ITEM ## |
||
262 | ########################################################################### |
||
263 | |||
264 | #### |
||
265 | # III. Send the command to Corrade to move the source into the target. |
||
266 | $params = array( |
||
267 | 'command' => 'inventory', |
||
268 | 'group' => $GROUP, |
||
269 | 'password' => $PASSWORD, |
||
270 | 'action' => 'mv', |
||
271 | 'source' => $source, |
||
272 | 'target' => $target |
||
273 | ); |
||
274 | |||
275 | # We now escape each key and value: this is very important, because the |
||
276 | # data we send to Corrade may contain the '&' or '=' characters (we don't |
||
277 | # in this example though but this will not hurt anyone). |
||
278 | array_walk($params, |
||
279 | function(&$value, $key) { |
||
280 | $value = rawurlencode($key)."=".rawurlencode($value); |
||
281 | } |
||
282 | ); |
||
283 | $postvars = implode('&', $params); |
||
284 | |||
285 | # Set the options, send the request and then display the outcome |
||
286 | if (!($curl = curl_init())) { |
||
287 | print 0; |
||
288 | return; |
||
289 | } |
||
290 | |||
291 | curl_setopt($curl, CURLOPT_URL, $URL); |
||
292 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
293 | curl_setopt($curl, CURLOPT_POST, true); |
||
294 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); |
||
295 | curl_setopt($curl, CURLOPT_ENCODING, true); |
||
296 | $result = curl_exec($curl); |
||
297 | curl_close($curl); |
||
298 | |||
299 | #### |
||
300 | # IV. Grab the status of the command. |
||
301 | $success = urldecode( |
||
302 | wasKeyValueGet( |
||
303 | "success", |
||
304 | $result |
||
305 | ) |
||
306 | ); |
||
307 | |||
308 | if($success == 'False') { |
||
309 | echo 'Unable to move item: '.urldecode( |
||
310 | wasKeyValueGet( |
||
311 | "error", |
||
312 | $result |
||
313 | ) |
||
314 | ); |
||
315 | return; |
||
316 | } |
||
317 | |||
318 | echo "success"; |
||
319 | |||
320 | ?> |
||
321 |