Advertisement
Guest User

Webservice SmartPing (v2.0)

a guest
Aug 26th, 2015
1,668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.38 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Mping\CoreBundle\Fftt;
  4.  
  5. /**
  6.  * @author VincentBab vincentbab@gmail.com
  7.  */
  8. class Service
  9. {
  10.     /**
  11.      * @var string $appId ID de l'application fourni par la FFTT (ex: AM001)
  12.      */
  13.     protected $appId;
  14.    
  15.     /**
  16.      * @var string $appKey Mot de passe fourni par la FFTT
  17.      */
  18.     protected $appKey;
  19.    
  20.     /**
  21.      * @var string $serial Serial de l'utilisateur
  22.      */
  23.     protected $serial;
  24.    
  25.     /**
  26.      * @var object $cache
  27.      */
  28.     protected $cache;
  29.    
  30.     /**
  31.      * @var object $logger
  32.      */
  33.     protected $logger;
  34.    
  35.     /**
  36.      * @var string $ipSource
  37.      */
  38.     protected $ipSource;
  39.  
  40.     public function __construct($appId, $appKey)
  41.     {
  42.         $this->appId = $appId;
  43.         $this->appKey = $appKey;
  44.        
  45.         libxml_use_internal_errors(true);
  46.     }
  47.    
  48.     public function getAppId()
  49.     {
  50.         return $this->appId;
  51.     }
  52.    
  53.     public function getAppKey()
  54.     {
  55.         return $this->appKey;
  56.     }
  57.    
  58.     public function setSerial($serial)
  59.     {
  60.         $this->serial = $serial;
  61.        
  62.         return $this;
  63.     }
  64.    
  65.     public function getSerial()
  66.     {
  67.         return $this->serial;
  68.     }
  69.    
  70.     public function setCache($cache)
  71.     {
  72.         $this->cache = $cache;
  73.        
  74.         return $this;
  75.     }
  76.    
  77.     public function getCache()
  78.     {
  79.         return $this->cache;
  80.     }
  81.    
  82.     public function setLogger($logger)
  83.     {
  84.         $this->logger = $logger;
  85.        
  86.         return $this;
  87.     }
  88.    
  89.     public function getLogger()
  90.     {
  91.         return $this->logger;
  92.     }
  93.    
  94.     public function setIpSource($ipSource)
  95.     {
  96.         $this->ipSource = $ipSource;
  97.        
  98.         return $this;
  99.     }
  100.    
  101.     public function getIpSource()
  102.     {
  103.         return $this->ipSource;
  104.     }
  105.  
  106.     public function initialization()
  107.     {
  108.         return Service::getObject($this->getData('http://www.fftt.com/mobile/pxml/xml_initialisation.php', array()));
  109.     }
  110.  
  111.     public function getClubsByDepartement($departement)
  112.     {
  113.         return $this->getCachedData("clubs_{$departement}", 3600*24*7, function($service) use ($departement) {
  114.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_club_dep2.php', array('dep' => $departement)), 'club');
  115.         });
  116.     }
  117.  
  118.     public function getClub($numero)
  119.     {
  120.         return $this->getCachedData("club_{$numero}", 3600*24*7, function($service) use ($numero) {
  121.             return Service::getObject($service->getData('http://www.fftt.com/mobile/pxml/xml_club_detail.php', array('club' => $numero)), 'club');
  122.         });
  123.     }
  124.     public function cleanClub($numero)
  125.     {
  126.         if (!$this->cache) {
  127.             return;
  128.         }
  129.        
  130.         $this->cache->delete("club_{$numero}");
  131.         $this->cache->delete("clubjoueurs_{$numero}");
  132.         $this->cache->delete("clubequipes_{$numero}_M");
  133.         $this->cache->delete("clubequipes_{$numero}_F");
  134.         $this->cache->delete("clubequipes_{$numero}_");
  135.     }
  136.  
  137.     public function getJoueur($licence)
  138.     {
  139.         $joueur = $this->getCachedData("joueur_{$licence}", 3600*24*7, function($service) use ($licence) {
  140.             return Service::getObject($service->getData('http://www.fftt.com/mobile/pxml/xml_joueur.php', array('licence' => $licence, 'auto' => 1)), 'joueur');
  141.         });
  142.  
  143.         if (!isset($joueur['licence'])) {
  144.             return null;
  145.         }
  146.  
  147.         if (empty($joueur['natio'])) {
  148.             $joueur['natio'] = 'F';
  149.         }
  150.  
  151.         $joueur['photo'] = "http://www.fftt.com/espacelicencie/photolicencie/{$joueur['licence']}_.jpg";
  152.         $joueur['progmois'] = round($joueur['point'] - $joueur['apoint'], 2); // Progression mensuelle
  153.         $joueur['progann'] = round($joueur['point'] - $joueur['valinit'], 2); // Progression annuelle
  154.  
  155.         return $joueur;
  156.     }
  157.     public function cleanJoueur($licence)
  158.     {
  159.         if (!$this->cache) {
  160.             return;
  161.         }
  162.        
  163.         $this->cache->delete("joueur_{$licence}");
  164.         $this->cache->delete("joueurparties_{$licence}");
  165.         $this->cache->delete("joueurspid_{$licence}");
  166.     }
  167.  
  168.     public function getJoueurParties($licence)
  169.     {
  170.         return $this->getCachedData("joueurparties_{$licence}", 3600*24*7, function($service) use ($licence) {
  171.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_partie_mysql.php', array('licence' => $licence, 'auto' => 1)), 'partie');
  172.         });
  173.     }
  174.  
  175.     public function getJoueurPartiesSpid($licence)
  176.     {
  177.         return $this->getCachedData("joueurspid_{$licence}", 3600*24*1, function($service) use ($licence) {
  178.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_partie.php', array('numlic' => $licence)), 'resultat');
  179.         });
  180.     }
  181.    
  182.     public function getJoueurHistorique($licence)
  183.     {
  184.         return $this->getCachedData("joueur_historique_{$licence}", 3600*24*2, function($service) use ($licence) {
  185.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_histo_classement.php', array('numlic' => $licence)), 'histo');
  186.         });
  187.     }
  188.  
  189.     public function getJoueursByName($nom, $prenom= '')
  190.     {
  191.         return $this->getCachedData("joueurs_{$nom}_{$prenom}", 3600*24*7, function($service) use ($nom, $prenom) {
  192.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_liste_joueur.php', array('nom' => $nom, 'prenom' => $prenom)), 'joueur');
  193.         });
  194.     }
  195.  
  196.     public function getJoueursByClub($club)
  197.     {
  198.         return $this->getCachedData("clubjoueurs_{$club}", 3600*24*7, function($service) use ($club) {
  199.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_liste_joueur.php', array('club' => $club)), 'joueur');
  200.         });
  201.     }
  202.  
  203.     public function getEquipesByClub($club, $type = null)
  204.     {
  205.         if ($type && !in_array($type, array('M', 'F'))) {
  206.             $type = 'M';
  207.         }
  208.  
  209.         $teams = $this->getCachedData("clubequipes_{$club}_{$type}", 3600*24*7, function($service) use ($club, $type) {
  210.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_equipe.php', array('numclu' => $club, 'type' => $type)), 'equipe');
  211.         });
  212.  
  213.         foreach($teams as &$team) {
  214.             $params = array();
  215.             parse_str($team['liendivision'], $params);
  216.  
  217.             $team['idpoule'] = $params['cx_poule'];
  218.             $team['iddiv'] = $params['D1'];
  219.         }
  220.  
  221.         return $teams;
  222.     }
  223.  
  224.     public function getPoules($division)
  225.     {
  226.         $poules = $this->getCachedData("poules_{$division}", 3600*24*7, function($service) use ($division) {
  227.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_equ.php', array('action' => 'poule', 'D1' => $division)), 'poule');
  228.         });
  229.  
  230.         foreach($poules as &$poule) {
  231.             $params = array();
  232.             parse_str($poule['lien'], $params);
  233.  
  234.             $poule['idpoule'] = $params['cx_poule'];
  235.             $poule['iddiv'] = $params['D1'];
  236.         }
  237.  
  238.         return $poules;
  239.     }
  240.  
  241.     public function getPouleClassement($division, $poule = null)
  242.     {
  243.         return $this->getCachedData("pouleclassement_{$division}_{$poule}", 3600*24*1, function($service) use ($division, $poule) {
  244.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_equ.php', array('auto' => 1, 'action' => 'classement', 'D1' => $division, 'cx_poule' => $poule)), 'classement');
  245.         });
  246.     }
  247.  
  248.     public function getPouleRencontres($division, $poule = null)
  249.     {
  250.         return $this->getCachedData("poulerencontres_{$division}_{$poule}", 3600*24*1, function($service) use ($division, $poule) {
  251.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_equ.php', array('auto' => 1, 'D1' => $division, 'cx_poule' => $poule)), 'tour');
  252.         });
  253.     }
  254.    
  255.     public function getIndivGroupes($division)
  256.     {
  257.         $groupes = $this->getCachedData("groupes_{$division}", 3600*24*7, function($service) use ($division) {
  258.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_indiv.php', array('action' => 'poule', 'res_division' => $division)), 'tour');
  259.         });
  260.        
  261.         foreach($groupes as &$groupe) {
  262.             $params = array();
  263.             parse_str($groupe['lien'], $params);
  264.  
  265.             if (isset($params['cx_tableau'])) {
  266.                 $groupe['idgroupe'] = $params['cx_tableau'];
  267.             } else {
  268.                 $groupe['idgroupe'] = null;
  269.             }
  270.             $groupe['iddiv'] = $params['res_division'];
  271.         }
  272.  
  273.         return $groupes;
  274.     }
  275.    
  276.     public function getGroupeClassement($division, $groupe = null)
  277.     {
  278.         return $this->getCachedData("groupeclassement_{$division}_{$groupe}", 3600*24*1, function($service) use ($division, $groupe) {
  279.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_indiv.php', array('action' => 'classement', 'res_division' => $division, 'cx_tableau' => $groupe)), 'classement');
  280.         });
  281.     }
  282.  
  283.     public function getGroupeRencontres($division, $groupe = null)
  284.     {
  285.         return $this->getCachedData("grouperencontres_{$division}_{$groupe}", 3600*24*1, function($service) use ($division, $groupe) {
  286.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_result_indiv.php', array('action' => 'partie', 'res_division' => $division, 'cx_tableau' => $groupe)), 'partie');
  287.         });
  288.     }
  289.  
  290.     public function getOrganismes($type)
  291.     {
  292.         // Zone / Ligue / Departement
  293.         if (!in_array($type, array('Z', 'L', 'D'))) {
  294.             $type = 'L';
  295.         }
  296.  
  297.         return $this->getCachedData("organismes_{$type}", 3600*24*30, function($service) use ($type) {
  298.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_organisme.php', array('type' => $type)), 'organisme');
  299.         });
  300.     }
  301.  
  302.     public function getEpreuves($organisme, $type)
  303.     {
  304.         // Equipe / Individuelle
  305.         if (!in_array($type, array('E', 'I'))) {
  306.             $type = 'E';
  307.         }
  308.  
  309.         return $this->getCachedData("epreuves_{$organisme}_{$type}", 3600*24*30, function($service) use ($organisme, $type) {
  310.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_epreuve.php', array('type' => $type, 'organisme' => $organisme)), 'epreuve');
  311.         });
  312.     }
  313.  
  314.     public function getDivisions($organisme, $epreuve, $type = 'E')
  315.     {
  316.         // Equipe / Individuelle
  317.         if (!in_array($type, array('E', 'I'))) {
  318.             $type = 'E';
  319.         }
  320.        
  321.         return $this->getCachedData("divisions_{$organisme}_{$epreuve}_{$type}", 3600*24*7, function($service) use ($organisme, $epreuve, $type) {
  322.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_division.php', array('organisme' => $organisme, 'epreuve' => $epreuve, 'type' => $type)), 'division');
  323.         });
  324.     }
  325.  
  326.     public function getRencontre($link)
  327.     {
  328.         $params = array();
  329.         parse_str($link, $params);
  330.  
  331.         return $this->getCachedData("rencontre_".sha1($link), 3600*24*1, function($service) use ($params) {
  332.             return Service::getObject($service->getData('http://www.fftt.com/mobile/pxml/xml_chp_renc.php', $params), null);
  333.         });
  334.     }
  335.  
  336.     public function getLicencesByName($nom, $prenom= '')
  337.     {
  338.         return $this->getCachedData("licences_{$nom}_{$prenom}", 3600*24*2, function($service) use ($nom, $prenom) {
  339.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_liste_joueur_o.php', array('nom' => strtoupper($nom), 'prenom' => ucfirst($prenom))), 'joueur');
  340.         });
  341.     }
  342.  
  343.     public function getLicencesByClub($club)
  344.     {
  345.         return $this->getCachedData("licencesclub_{$club}", 3600*24*2, function($service) use ($club) {
  346.             return Service::getCollection($service->getData('http://www.fftt.com/mobile/pxml/xml_liste_joueur_o.php', array('club' => $club)), 'joueur');
  347.         });
  348.     }
  349.  
  350.     public function getLicence($licence)
  351.     {
  352.         return $this->getCachedData("licence_{$licence}", 3600*24*2, function($service) use ($licence) {
  353.             return Service::getObject($service->getData('http://www.fftt.com/mobile/pxml/xml_licence.php', array('licence' => $licence)), 'licence');
  354.         });
  355.     }
  356.  
  357.     protected function getCachedData($key, $lifeTime, $callback)
  358.     {
  359.         if (!$this->cache) {
  360.             return $callback($this);
  361.         }
  362.  
  363.         if (false === ($data = $this->cache->fetch($key))) {
  364.             $data = $callback($this);
  365.            
  366.             if ($data !== false) {
  367.                 $this->cache->save($key, $data, $lifeTime);
  368.             }
  369.         }
  370.  
  371.         return $data;
  372.     }
  373.  
  374.     public function getData($url, $params = array(), $generateHash = true)
  375.     {
  376.         if ($generateHash) {
  377.             $params['serie'] = $this->getSerial();
  378.             $params['id'] = $this->getAppId();
  379.             $params['tm'] = date('YmdHis') . substr(microtime(), 2, 3);
  380.             $params['tmc'] =  hash_hmac('sha1', $params['tm'], hash('md5', $this->getAppKey(), false));
  381.         }
  382.        
  383.         if (!empty($params)) {
  384.             $url .= '?' . http_build_query($params);
  385.         }
  386.  
  387.         $curl = curl_init();
  388.         curl_setopt($curl, CURLOPT_URL, $url);
  389.         if ($this->getIpSource()) {
  390.             curl_setopt($curl, CURLOPT_INTERFACE, $this->getIpSource());
  391.         }
  392.         curl_setopt($curl, CURLOPT_RETURNTRANSFER , true);
  393.         curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  394.             "Accept:", // Suprime le header par default de cUrl (Accept: */*)
  395.             "User-agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)",
  396.             "Content-Type: application/x-www-form-urlencoded",
  397.             "Accept-Encoding: gzip",
  398.             "Connection: Keep-Alive",
  399.         ));
  400.         $data = curl_exec($curl);
  401.         curl_close($curl);
  402.  
  403.         if ($this->logger) {
  404.             $this->logger->log($url, $data);
  405.         }
  406.  
  407.         $xml = simplexml_load_string($data);
  408.  
  409.         if (!$xml) {
  410.             return false;
  411.         }
  412.  
  413.         // Petite astuce pour transformer simplement le XML en tableau
  414.         return json_decode(json_encode($xml), true);
  415.     }
  416.  
  417.     public static function getCollection($data, $key = null)
  418.     {
  419.         if (empty($data)) {
  420.             return array();
  421.         }
  422.  
  423.         if ($key) {
  424.             if (!array_key_exists($key, $data)) {
  425.                 return array();
  426.             }
  427.             $data = $data[$key];
  428.         }
  429.  
  430.         return isset($data[0]) ? $data : array($data);
  431.     }
  432.  
  433.     public static function getObject($data, $key = null)
  434.     {
  435.         if ($key && $data !== false) {
  436.             return array_key_exists($key, $data) ? $data[$key] : null;
  437.         } else {
  438.             return empty($data) ? null : $data;
  439.         }
  440.     }
  441.    
  442.     public static function generateSerial()
  443.     {
  444.         $serial = '';
  445.         for($i=0; $i<15; $i++) {
  446.             $serial .= chr(mt_rand(65, 90)); //(A-Z)
  447.         }
  448.        
  449.         return $serial;
  450.     }
  451.  
  452. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement