corrade-http-templates – Diff between revs 15 and 20

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