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

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