corrade-http-templates – Diff between revs 72 and 82

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