corrade-http-templates – Blame information for rev 2

Subversion Repositories:
Rev:
Rev Author Line No. Line
2 eva 1 <?php
2  
3 ###########################################################################
4 ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ##
5 ###########################################################################
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 ##
8 ## DataTables JQuery extension. ##
9 ###########################################################################
10  
11 ###########################################################################
12 ## CONFIGURATION ##
13 ###########################################################################
14  
15 # Set this to the name of the group.
16 $GROUP = 'My Group';
17 # Set this to the group password.
18 $PASSWORD = 'mypassword';
19 # Set this to Corrade's HTTP Server URL.
20 $URL = 'http://corrade.internal.site:8080';
21  
22 ###########################################################################
23 ## INTERNALS ##
24 ###########################################################################
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  
80 ####
81 # I. Get the top scripts.
82 $params = array(
83 'command' => 'getregiontop',
84 'group' => $GROUP,
85 'password' => $PASSWORD,
86 'type' => 'scripts'
87 );
88 array_walk($params,
89 function(&$value, $key) {
90 $value = rawurlencode($key)."=".rawurlencode($value);
91 }
92 );
93 $postvars = implode('&', $params);
94 if (!($curl = curl_init())) {
95 print 0;
96 return;
97 }
98 curl_setopt($curl, CURLOPT_URL, $URL);
99 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
100 curl_setopt($curl, CURLOPT_POST, true);
101 curl_setopt($curl, CURLOPT_POSTFIELDS, $postvars);
102 $return = curl_exec($curl);
103 curl_close($curl);
104  
105 ####
106 # II. Check for success.
107 $success = urldecode(
108 wasKeyValueGet(
109 "success",
110 $return
111 )
112 );
113 if($success == 'False') {
114 echo 'Unable to query the top scripts.';
115 die;
116 }
117  
118 ####
119 # III. Dump JSON for DataTables.
120 echo json_encode(
121 array(
122 "data" =>
123 array_chunk(
124 wasCSVToArray(
125 urldecode(
126 wasKeyValueGet(
127 "data",
128 $return
129 )
130 )
131 ),
132 5 # We split the CSV in 5: score, task name, UUID, Owner and Position
133 )
134 )
135 );
136  
137 ?>
138  
139