1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15
16 App::uses('Component', 'Controller');
17 18 19
20 class XlrFunctionsComponent extends Component {
21
22 public $components = array('GeoIP');
23
24
25
26 27 28 29 30 31 32 33
34 public function getRank($kills) {
35 $ranks = Configure::read('rank');
36 $result = 0;
37 foreach ($ranks as $rank => $value) {
38 if ($value[1] <= $kills) {
39 $result = $rank;
40 }
41 }
42 return $result;
43 }
44
45
46
47 48 49 50 51 52 53
54 public function getRankProgress($kills) {
55 $currentRank = $this->getRank($kills);
56 $killsForCurrentRank = Configure::read('rank.' . $currentRank . '.1');
57
58 $ranks = Configure::read('rank');
59 end($ranks);
60 $highestRank = key($ranks);
61
62 if ($currentRank != $highestRank) {
63 $killsForNextRank = Configure::read('rank.' . ($currentRank + 1) . '.1');
64 } else {
65 return false;
66 }
67
68 $killsNeeded = $killsForNextRank - $kills;
69 $progress = (($kills - $killsForCurrentRank) / ($killsForNextRank - $killsForCurrentRank)) * 100;
70
71 return array(
72 'kills_needed' => $killsNeeded,
73 'progress' => $progress
74 );
75 }
76
77
78
79 80 81 82 83 84
85 public function getFlag($ip) {
86 if ($ip == '') {
87 $flag = array('-', __('Unknown'));
88 } else {
89 $flag = array(strtolower($this->GeoIP->country_code($ip)), $this->GeoIP->country_name($ip));
90 }
91 return $flag;
92 }
93
94
95
96 97 98 99 100 101 102
103 public function getLevel($groupBits) {
104 $levels = Configure::read('level');
105 $playerLevel = array('level' => 0, 'name' => __('Undefined Level'));
106 foreach ($levels as $key => $value) {
107 if ($groupBits == $value[0]) {
108
109 $playerLevel['level'] = $key;
110 $playerLevel['name'] = $value[1];
111 }
112 }
113
114 return $playerLevel;
115 }
116
117
118
119 120 121 122 123 124 125 126 127 128
129 public function getItemName($xlrDB, $item) {
130
131 $XlrServer = new XlrServer();
132 $gameName = (string)$XlrServer->serverStatus($xlrDB)->Game->attributes()->Name;
133 config('games/' . $gameName . '.php');
134 if ($item == 'team') {
135 $temp = getTeamName();
136 } elseif ($item == 'weapon') {
137 $temp = getWeaponName();
138 } elseif ($item == 'map') {
139 $temp = getMapName();
140 } elseif ($item == 'event') {
141 $temp = getEventName();
142 } elseif ($item == 'bodypart') {
143 $temp = getBodypartName();
144 } else {
145 $temp = __('Undefined Item');
146 }
147
148 return $temp;
149 }
150
151
152
153 154 155 156 157 158 159 160
161 public function getLeague($skill = 0) {
162 $result = -1;
163 $leagues = Configure::read('league');
164 foreach ($leagues as $l => $v) {
165 if ($v[0] != 'League.skill') {
166 unset($leagues[$l]);
167 }
168 }
169 foreach ($leagues as $l => $v) {
170
171 if (($v[1] < $skill) && ($skill < $v[2])) {
172 $result = $l;
173 }
174 }
175 return $result;
176 }
177
178
179
180 181 182 183
184 public function showLeagueStars($leagueNumber) {
185 $leagues = Configure::read('league');
186 $leagueCount = count($leagues);
187 $result = '';
188 $star = $this->Html->image('stars/stargold.gif');
189 for ($i = $leagueCount; $i >= $leagueNumber; $i -= 1) {
190 $result .= $star . ' ';
191 }
192 return $result;
193 }
194
195
196
197 198 199 200
201 public function getLicenseDetails($key) {
202 $excl = array(
203 'http://xlr8or.snt.utwente.nl',
204 'http://localhost'
205 );
206 if (in_array(FULL_BASE_URL, $excl)) {
207 return false;
208 }
209 $url = 'aHR0cDovL3hscjhvci5zbnQudXR3ZW50ZS5ubC94bHJzdGF0cy9saWNlbnNlX3NlcnZlci9saWNlbnNlcy9jaGVjaw,,';
210 $url = $this->__base64Decode($url);
211 $params = array();
212 $params['license'] = $key;
213 $params['host'] = FULL_BASE_URL;
214 $params['path'] = ROOT;
215 $params['version'] = XLR_VERSION;
216
217 if (function_exists('curl_version')) {
218
219 $ch = curl_init();
220 curl_setopt($ch, CURLOPT_URL, $url);
221 curl_setopt($ch, CURLOPT_VERBOSE, 0);
222 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
223 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
224 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
225 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
226 curl_setopt($ch, CURLOPT_TIMEOUT, 20);
227 curl_setopt($ch, CURLOPT_POST, true);
228 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
229 $result = curl_exec($ch);
230
231 $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
232 if ($code != 200) {
233 debug('CURLINFO_HTTP_CODE: ' . $code);
234 }
235
236 } else {
237
238 $data = http_build_query($params);
239 $opts = array('http' => array(
240 'method' => 'POST',
241 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
242 'content' => $data));
243
244 $context = stream_context_create($opts);
245 $result = file_get_contents($url, false, $context);
246 }
247
248 return json_decode($result, true);
249 }
250
251
252
253 254 255
256 public function isLicenseValid() {
257
258 if (Configure::check('license.valid')) {
259 return Configure::read('license.valid');
260 } else {
261 $json = Cache::read('licenseKeyInfo', '1week');
262 if (!$json) {
263 $json = Cache::read('licenseKeyInfo', '5min');
264 }
265 if (!$json) {
266 $json = $this->getLicenseDetails(Configure::read('options.license'));
267
268 }
269
270 if (!$json['result']['valid']) {
271
272 Cache::write('licenseKeyInfo', $json, '5min');
273 Configure::write('license.valid', false);
274 return false;
275 } else {
276
277 Cache::write('licenseKeyInfo', $json, '1week');
278 Configure::write('license.valid', true);
279 return true;
280 }
281 }
282 }
283
284
285
286 287 288 289
290 private function __base64Encode($input) {
291 return strtr(base64_encode($input), '+/=', '-_,');
292 }
293
294
295
296 297 298 299
300 private function __base64Decode($input) {
301 return base64_decode(strtr($input, '-_,', '+/='));
302 }
303
304
305
306 307 308 309 310 311 312 313
314 public function fixName($playerName, $fixedName = '', $defaultName = 'Unknown Soldier') {
315 if ($fixedName != '') {
316 $playerName = $fixedName;
317 }
318 if ($playerName == '') {
319 $playerName = $defaultName;
320 }
321
322 $displayName = $this->sanitizeMe($playerName);
323 return $displayName;
324 }
325
326
327
328 329 330 331 332 333 334
335 public function sanitizeMe($str) {
336 return htmlentities($str, ENT_QUOTES, 'UTF-8');
337 }
338 }
339