corrade-http-templates – Diff between revs 12 and 53

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 12 Rev 53
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 to get the avatar positions and ## 6 ## This is a script that uses Corrade to get the avatar positions and ##
7 ## then aggregates that data to a JSON object that will be read by AJAX. ## 7 ## then aggregates that data to a JSON object that will be read by AJAX. ##
8 ## ## 8 ## ##
9 ## The output JSON object that will be read via AJAX is of the form: ## 9 ## The output JSON object that will be read via AJAX is of the form: ##
10 ## { ## 10 ## { ##
11 ## header: ["x", "y", "z"], ## 11 ## header: ["x", "y", "z"], ##
12 ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ## 12 ## positions: [[x1, y1, z1], [x2, y2, z2], ...], ##
13 ## names: ["First Resident", "Second Resident", ...], ## 13 ## names: ["First Resident", "Second Resident", ...], ##
14 ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ## 14 ## colors: [[0.5, 1, 0.75], [0.3, 0.55, 0.34], ...] ##
15 ## } ## 15 ## } ##
16 ########################################################################### 16 ###########################################################################
17   17  
18 ########################################################################### 18 ###########################################################################
19 ## CONFIGURATION ## 19 ## CONFIGURATION ##
20 ########################################################################### 20 ###########################################################################
21   21  
22 # The configuration file for this script containing the settings. 22 # The configuration file for this script containing the settings.
23 require_once("config.php"); 23 require_once("config.php");
24 require_once("functions.php") 24 require_once("functions.php");
25   25  
26 ########################################################################### 26 ###########################################################################
27 ## INTERNALS ## 27 ## INTERNALS ##
28 ########################################################################### 28 ###########################################################################
29   29  
30 #### 30 ####
31 # I. Get the avatar positions. 31 # I. Get the avatar positions.
32 $params = array( 32 $params = array(
33 'command' => 'getavatarpositions', 33 'command' => 'getavatarpositions',
34 'group' => $GROUP, 34 'group' => $GROUP,
35 'entity' => 'region', 35 'entity' => 'region',
36 'password' => $PASSWORD 36 'password' => $PASSWORD
37 ); 37 );
38   38  
39 # We now escape each key and value: this is very important, because the 39 # We now escape each key and value: this is very important, because the
40 # data we send to Corrade may contain the '&' or '=' characters (we don't 40 # data we send to Corrade may contain the '&' or '=' characters (we don't
41 # in this example though but this will not hurt anyone). 41 # in this example though but this will not hurt anyone).
42 array_walk($params, 42 array_walk($params,
43 function(&$value, $key) { 43 function(&$value, $key) {
44 $value = urlencode($key)."=".urlencode($value); 44 $value = urlencode($key)."=".urlencode($value);
45 } 45 }
46 ); 46 );
47 $postvars = implode('&', $params); 47 $postvars = implode('&', $params);
48   48  
49 # Set the options, send the request and then display the outcome 49 # Set the options, send the request and then display the outcome
50 if (!($curl = curl_init())) { 50 if (!($curl = curl_init())) {
51 print 0; 51 print 0;
52 return; 52 return;
53 } 53 }
54   54  
55 curl_setopt($curl, CURLOPT_URL, $URL); 55 curl_setopt($curl, CURLOPT_URL, $URL);
56 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 56 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
57 curl_setopt($curl, CURLOPT_POST, true); 57 curl_setopt($curl, CURLOPT_POST, true);
58 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); 58 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
59 $result = curl_exec($curl); 59 $result = curl_exec($curl);
60 curl_close($curl); 60 curl_close($curl);
61   61  
62 $success = urldecode( 62 $success = urldecode(
63 wasKeyValueGet( 63 wasKeyValueGet(
64 "success", 64 "success",
65 $result 65 $result
66 ) 66 )
67 ); 67 );
68   68  
69 // Unable to get avatar positions? 69 // Unable to get avatar positions?
70 if($success == 'False') return -1; 70 if($success == 'False') return -1;
71   71  
72 $data = wasCSVToArray( 72 $data = wasCSVToArray(
73 urldecode( 73 urldecode(
74 wasKeyValueGet( 74 wasKeyValueGet(
75 "data", 75 "data",
76 $result 76 $result
77 ) 77 )
78 ) 78 )
79 ); 79 );
80   80  
81 #### 81 ####
82 # II. Construct and echo the JSON object. 82 # II. Construct and echo the JSON object.
83 echo json_encode( 83 echo json_encode(
84 array( 84 array(
85 'header' => array('x', 'y', 'z'), 85 'header' => array('x', 'y', 'z'),
86 'positions' => array_values( 86 'positions' => array_values(
87 array_map( 87 array_map(
88 function($e) { 88 function($e) {
89 $e = preg_replace('/</', '', $e); 89 $e = preg_replace('/</', '', $e);
90 $e = preg_replace('/>/', '', $e); 90 $e = preg_replace('/>/', '', $e);
91 $e = explode(',', $e); 91 $e = explode(',', $e);
92 return array($e[0], $e[1], $e[2]); 92 return array($e[0], $e[1], $e[2]);
93 }, 93 },
94 wasArrayStride( 94 wasArrayStride(
95 array_slice($data, 2), 95 array_slice($data, 2),
96 3 96 3
97 ) 97 )
98 ) 98 )
99 ), 99 ),
100 'names' => array_values( 100 'names' => array_values(
101 wasArrayStride( 101 wasArrayStride(
102 $data, 102 $data,
103 3 103 3
104 ) 104 )
105 ), 105 ),
106 'colors' => array_values( 106 'colors' => array_values(
107 array_map( 107 array_map(
108 function($e) { 108 function($e) {
109 $e = hex2RGB(wasColorHash($e)); 109 $e = hex2RGB(wasColorHash($e));
110 return array( 110 return array(
111 round ( 111 round (
112 mapValueToRange($e['red'], 0, 255, 0, 1), 2 112 mapValueToRange($e['red'], 0, 255, 0, 1), 2
113 ), 113 ),
114 round( 114 round(
115 mapValueToRange($e['green'], 0, 255, 0, 1), 2 115 mapValueToRange($e['green'], 0, 255, 0, 1), 2
116 ), 116 ),
117 round( 117 round(
118 mapValueToRange($e['blue'], 0, 255, 0, 1), 2 118 mapValueToRange($e['blue'], 0, 255, 0, 1), 2
119 ) 119 )
120 ); 120 );
121 }, 121 },
122 wasArrayStride( 122 wasArrayStride(
123 $data, 123 $data,
124 3 124 3
125 ) 125 )
126 ) 126 )
127 ) 127 )
128 ), 128 ),
129 JSON_NUMERIC_CHECK 129 JSON_NUMERIC_CHECK
130 ); 130 );
131   131  
132 return 0; 132 return 0;
133   133  
134 ?> 134 ?>
135   135  
136   136