-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.php
More file actions
326 lines (266 loc) · 11.9 KB
/
Service.php
File metadata and controls
326 lines (266 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
namespace Mping\CoreBundle\Fftt;
use Doctrine\Common\Cache\Cache;
class Service
{
private $cache;
private $logger;
private $ipsource;
private $serials;
private $autoInitialization;
public function __construct(Cache $cache = null, $logger = null, $serials = array(), $ipsource = null)
{
$this->cache = $cache;
$this->logger = $logger; // Must have a method log($url, $data)
$this->ipsource = $ipsource;
$this->serials = $serials;
// Génération d'un serial par défaut (15 lettres majuscules)
if (empty($this->serials)) {
$this->serials = array('');
for($i=0; $i<15; $i++) {
$this->serials[0] .= chr(mt_rand(65, 90)); //(A-Z)
}
}
$this->autoInitialization = true;
libxml_use_internal_errors(true);
}
public function setAutoInitialisation($flag)
{
$this->autoInitialization = (bool)$flag;
}
public function initialization()
{
$serial = $this->serials[mt_rand(0, count($this->serials) - 1)];
$this->getData('http://www.fftt.com/mobile/xml/xml_initialisation.php', array('serie' => $serial, 'os' => 'Android', 'version' => '007'));
$event = self::getObject($this->getData('http://www.fftt.com/mobile/xml/evenement/xml_evenement.php'), 'evenement');
if (!empty($event['logo'])) {
$this->getData($event['logo']);
}
}
public function getClubsByDepartement($departement)
{
return $this->getCachedData("clubs{$departement}", 3600*24*30, function($service) use ($departement) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_club_dep2.php', array('dep' => $departement)), 'club');
});
}
public function getClub($numero)
{
return $this->getCachedData("club_{$numero}", 3600*24*2, function($service) use ($numero) {
return Service::getObject($service->getData('http://www.fftt.com/mobile/xml/xml_club_detail.php', array('club' => $numero)), 'club');
});
}
public function getJoueur($licence)
{
$joueur = $this->getCachedData("joueur_{$licence}", 3600*24*1, function($service) use ($licence) {
return Service::getObject($service->getData('http://www.fftt.com/mobile/xml/xml_joueur.php', array('licence' => $licence, 'auto' => 1)), 'joueur');
});
if (!isset($joueur['licence'])) {
return null;
}
if (empty($joueur['natio'])) {
$joueur['natio'] = 'F';
}
$joueur['photo'] = "http://www.fftt.com/espacelicencie/photolicencie/{$joueur['licence']}_.jpg";
$joueur['progmois'] = round($joueur['point'] - $joueur['apoint'], 2); // Progression mensuelle
$joueur['progann'] = round($joueur['point'] - $joueur['valinit'], 2); // Progression annuelle
return $joueur;
}
public function getJoueurParties($licence)
{
return $this->getCachedData("joueur_parties_{$licence}", 3600*24*1, function($service) use ($licence) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_partie_mysql.php', array('licence' => $licence, 'auto' => 1)), 'partie');
});
}
public function getJoueurPartiesSpid($licence)
{
return $this->getCachedData("joueur_spid_{$licence}", 3600*24*1, function($service) use ($licence) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_partie.php', array('numlic' => $licence)), 'resultat');
});
}
public function getJoueursByName($nom, $prenom= '')
{
return $this->getCachedData("joueurs_{$nom}_{$prenom}", 3600*24*1, function($service) use ($nom, $prenom) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_liste_joueur.php', array('nom' => $nom, 'prenom' => $prenom)), 'joueur');
});
}
public function getJoueursByClub($club)
{
return $this->getCachedData("joueursclub{$club}", 3600*24*1, function($service) use ($club) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_liste_joueur.php', array('club' => $club)), 'joueur');
});
}
public function getEquipesByClub($club, $type = null)
{
if ($type && !in_array($type, array('M', 'F'))) {
$type = 'M';
}
$teams = $this->getCachedData("clubequipes_{$club}_{$type}", 3600*24*3, function($service) use ($club, $type) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_equipe.php', array('numclu' => $club, 'type' => $type)), 'equipe');
});
foreach($teams as &$team) {
$params = array();
parse_str($team['liendivision'], $params);
$team['idpoule'] = $params['cx_poule'];
$team['iddiv'] = $params['D1'];
}
return $teams;
}
public function getPoules($division)
{
$poules = $this->getCachedData("poules_{$division}", 3600*24*30, function($service) use ($division) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_result_equ.php', array('action' => 'poule', 'D1' => $division)), 'poule');
});
foreach($poules as &$poule) {
$params = array();
parse_str($poule['lien'], $params);
$poule['idpoule'] = $params['cx_poule'];
$poule['iddiv'] = $params['D1'];
}
return $poules;
}
public function getPouleClassement($division, $poule = null)
{
return $this->getCachedData("pouleclassement_{$division}_{$poule}", 3600*24*1, function($service) use ($division, $poule) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_result_equ.php', array('auto' => 1, 'action' => 'classement', 'D1' => $division, 'cx_poule' => $poule)), 'classement');
});
}
public function getPouleRencontres($division, $poule = null)
{
return $this->getCachedData("poulerencontres_{$division}_{$poule}", 3600*24*1, function($service) use ($division, $poule) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_result_equ.php', array('auto' => 1, 'D1' => $division, 'cx_poule' => $poule)), 'tour');
});
}
public function getOrganismes($type)
{
// Zone / Ligue / Departement
if (!in_array($type, array('Z', 'L', 'D'))) {
$type = 'L';
}
return $this->getCachedData("organismes_{$type}", 3600*24*30, function($service) use ($type) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_organisme.php', array('type' => $type)), 'organisme');
});
}
public function getEpreuves($organisme, $type)
{
// Equipe / Individuelle
if (!in_array($type, array('E', 'I'))) {
$type = 'E';
}
return $this->getCachedData("epreuves_{$organisme}_{$type}", 3600*24*30, function($service) use ($organisme, $type) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_epreuve.php', array('type' => $type, 'organisme' => $organisme)), 'epreuve');
});
}
public function getDivisions($organisme, $epreuve)
{
return $this->getCachedData("divisions_{$organisme}_{$epreuve}", 3600*24*30, function($service) use ($organisme, $epreuve) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_division.php', array('organisme' => $organisme, 'epreuve' => $epreuve, )), 'division');
});
}
public function getRencontre($link)
{
$params = array();
parse_str($link, $params);
return $this->getCachedData("rencontre_".sha1($link), 3600*24*1, function($service) use ($params) {
return Service::getObject($service->getData('http://www.fftt.com/mobile/xml/xml_chp_renc.php', $params), null);
});
}
public function getLicencesByName($nom, $prenom= '')
{
return $this->getCachedData("licences_{$nom}_{$prenom}", 3600*24*2, function($service) use ($nom, $prenom) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_liste_joueur_o.php', array('nom' => strtoupper($nom), 'prenom' => ucfirst($prenom))), 'joueur');
});
}
public function getLicencesByClub($club)
{
return $this->getCachedData("licencesclub_{$club}", 3600*24*2, function($service) use ($club) {
return Service::getCollection($service->getData('http://www.fftt.com/mobile/xml/xml_liste_joueur_o.php', array('club' => $club)), 'joueur');
});
}
public function getLicence($licence)
{
return $this->getCachedData("licence_{$licence}", 3600*24*1, function($service) use ($licence) {
return Service::getObject($service->getData('http://www.fftt.com/mobile/xml/xml_licence.php', array('licence' => $licence)), 'licence');
});
}
private function getCachedData($key, $lifeTime, $callback)
{
if (!$this->cache) {
return $callback($this);
}
if (false === ($data = $this->cache->fetch($key))) {
if ($this->autoInitialization && false == $this->cache->fetch($initKey = 'xml_initialisation')) {
$this->initialization();
$this->cache->save($initKey, 'init', 3600*4 + mt_rand(0, 3600*4)); // Entre 4 et 8h
}
$data = $callback($this);
$this->cache->save($key, $data, $lifeTime);
}
return $data;
}
public function getData($url, $params = null)
{
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
/*$context = stream_context_create(array(
'socket' => array('bindto' => ($this->ipsource ?: '0').':0'),
'http' => array(
'protocol_version' => 1.1,
'header' => array("User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)",
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: gzip",
"Connection: Close",
),
)
));
//$data = file_get_contents($url, false, $context);
$stream = fopen($url, 'r', false, $context);
$data = stream_get_contents($stream);
fclose($stream);*/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
if ($this->ipsource) {
curl_setopt($curl, CURLOPT_INTERFACE, $this->ipsource);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER , true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"Accept:", // Suprime le header par default de cUrl (Accept: */*)
"User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)",
"Content-Type: application/x-www-form-urlencoded",
"Accept-Encoding: gzip",
"Connection: Keep-Alive",
));
$data = curl_exec($curl);
curl_close($curl);
if ($this->logger) {
$this->logger->log($url, $data);
}
$xml = simplexml_load_string($data);
if (!$xml) {
return false;
}
// Petite astuce pour transformer simplement le XML en tableau
return json_decode(json_encode($xml), true);
}
public static function getCollection($data, $key = null)
{
if (empty($data)) {
return array();
}
if ($key) {
if (!array_key_exists($key, $data)) {
return array();
}
$data = $data[$key];
}
return isset($data[0]) ? $data : array($data);
}
public static function getObject($data, $key = null)
{
if ($key) {
return array_key_exists($key, $data) ? $data[$key] : null;
} else {
return empty($data) ? null : $data;
}
}
}