Advertisement
Guest User

Webservice Smartping (V1.1)

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