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