corrade-http-templates – Diff between revs 2 and 6

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 2 Rev 6
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 uses Corrade to fetch the top scripts on a region ## 6 ## This is a script uses Corrade to fetch the top scripts on a region ##
7 ## and then passes that data back as a JSON object to be used with the ## 7 ## and then passes that data back as a JSON object to be used with the ##
8 ## DataTables JQuery extension. ## 8 ## DataTables JQuery extension. ##
9 ########################################################################### 9 ###########################################################################
10   10  
11 ########################################################################### 11 ###########################################################################
12 ## CONFIGURATION ## 12 ## CONFIGURATION ##
13 ########################################################################### 13 ###########################################################################
14   -  
15 # Set this to the name of the group. -  
16 $GROUP = 'My Group'; 14  
17 # Set this to the group password. 15 require_once('config.php');
18 $PASSWORD = 'mypassword'; -  
19 # Set this to Corrade's HTTP Server URL. -  
20 $URL = 'http://corrade.internal.site:8080'; 16 require_once('functions.php');
21   17  
22 ########################################################################### 18 ###########################################################################
23 ## INTERNALS ## 19 ## INTERNALS ##
24 ########################################################################### 20 ###########################################################################
25   -  
26 ########################################################################### -  
27 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## -  
28 ########################################################################### -  
29 function wasKeyValueGet($key, $data) { -  
30 return array_reduce( -  
31 explode( -  
32 "&", -  
33 $data -  
34 ), -  
35 function($o, $p) { -  
36 $x = explode("=", $p); -  
37 return array_shift($x) != $o ? $o : array_shift($x); -  
38 }, -  
39 $key -  
40 ); -  
41 } -  
42 /////////////////////////////////////////////////////////////////////////// -  
43 // Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 // -  
44 /////////////////////////////////////////////////////////////////////////// -  
45 function wasCSVToArray($csv) { -  
46 $l = array(); -  
47 $s = array(); -  
48 $m = ""; -  
49 for ($i = 0; $i < strlen($csv); ++$i) { -  
50 switch ($csv{$i}) { -  
51 case ',': -  
52 if (sizeof($s) == 0 || !current($s) == '"') { -  
53 array_push($l, $m); -  
54 $m = ""; -  
55 break; -  
56 } -  
57 $m .= $csv{$i}; -  
58 continue; -  
59 case '"': -  
60 if ($i + 1 < strlen($csv) && $csv{$i} == $csv{$i + 1}) { -  
61 $m .= $csv{$i}; -  
62 ++$i; -  
63 break; -  
64 } -  
65 if (sizeof($s) == 0|| !current($s) == $csv[$i]) { -  
66 array_push($s, $csv{$i}); -  
67 continue; -  
68 } -  
69 array_pop($s); -  
70 break; -  
71 default: -  
72 $m .= $csv{$i}; -  
73 break; -  
74 } -  
75 } -  
76 array_push($l, $m); -  
77 return $l; -  
78 } -  
79   21  
80 #### 22 ####
81 # I. Get the top scripts. 23 # I. Get the top scripts.
82 $params = array( 24 $params = array(
83 'command' => 'getregiontop', 25 'command' => 'getregiontop',
84 'group' => $GROUP, 26 'group' => $GROUP,
85 'password' => $PASSWORD, 27 'password' => $PASSWORD,
86 'type' => 'scripts' 28 'type' => 'scripts'
87 ); 29 );
88 array_walk($params, 30 array_walk($params,
89 function(&$value, $key) { 31 function(&$value, $key) {
90 $value = rawurlencode($key)."=".rawurlencode($value); 32 $value = rawurlencode($key)."=".rawurlencode($value);
91 } 33 }
92 ); 34 );
93 $postvars = implode('&', $params); 35 $postvars = implode('&', $params);
94 if (!($curl = curl_init())) { 36 if (!($curl = curl_init())) {
95 print 0; 37 print 0;
96 return; 38 return;
97 } 39 }
98 curl_setopt($curl, CURLOPT_URL, $URL); 40 curl_setopt($curl, CURLOPT_URL, $URL);
99 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 41 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
100 curl_setopt($curl, CURLOPT_POST, true); 42 curl_setopt($curl, CURLOPT_POST, true);
101 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars); 43 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
-   44 curl_setopt($curl, CURLOPT_ENCODING, true);
102 $return = curl_exec($curl); 45 $return = curl_exec($curl);
103 curl_close($curl); 46 curl_close($curl);
104   47  
105 #### 48 ####
106 # II. Check for success. 49 # II. Check for success.
107 $success = urldecode( 50 $success = urldecode(
108 wasKeyValueGet( 51 wasKeyValueGet(
109 "success", 52 "success",
110 $return 53 $return
111 ) 54 )
112 ); 55 );
113 if($success == 'False') { 56 if($success == 'False') {
114 echo 'Unable to query the top scripts.'; 57 echo 'Unable to query the top scripts.';
115 die; 58 die;
116 } 59 }
117   60  
118 #### 61 ####
119 # III. Dump JSON for DataTables. 62 # III. Dump JSON for DataTables.
120 echo json_encode( 63 echo json_encode(
121 array( 64 array(
122 "data" => 65 "data" =>
123 array_chunk( 66 array_chunk(
124 wasCSVToArray( 67 wasCSVToArray(
125 urldecode( 68 urldecode(
126 wasKeyValueGet( 69 wasKeyValueGet(
127 "data", 70 "data",
128 $return 71 $return
129 ) 72 )
130 ) 73 )
131 ), 74 ),
132 5 # We split the CSV in 5: score, task name, UUID, Owner and Position 75 5 # We split the CSV in 5: score, task name, UUID, Owner and Position
133 ) 76 )
134 ) 77 )
135 ); 78 );
136   79  
137 ?> 80 ?>
138   81  
139   82  
140   83