colissimo

colissimo.inc

  1. <?php
  2. // vim:filetype=php expandtab tabstop=2 softtabstop=2 shiftwidth=2 autoindent smartindent
  3. // $Id$
  4.  
  5. function tribune_colissimo_info() {
  6.   return t('Donne l\'état d\'un colis colissimo.');
  7. }
  8.  
  9. function tribune_colissimo_filter(&$post) {
  10.   $answer = array(
  11.     'info' => variable_get('tribune_colissimo_name', "Colissimo"),
  12.   );
  13.  
  14.   if (variable_get('tribune_colissimo_authentified', FALSE)) {
  15.     $answer['login'] = $answer['info'];
  16.   }
  17.  
  18.   if (preg_match(':^/'. t('colissimo') .'(.*)$:', $post['message'], $matches)) {
  19.     $args = $matches[1];
  20.  
  21.     $response = _tribune_colissimo_status($args);
  22.  
  23.     if ($response['error']) {
  24.       $answer['message'] = $response['error'];
  25.     } else {
  26.       $answer['message'] = $response['text'];
  27.     }
  28.  
  29.     $answer['message'] = tribune_filters_print_clock($post) ." ". $answer['message'];
  30.     return array($answer);
  31.   }
  32. }
  33.  
  34. function _tribune_colissimo_status($id) {
  35.   $url = "http://www.coliposte.net/ec/suivi_ec.jsp?colispart=". urlencode($id);
  36.  
  37.   $ch = curl_init();
  38.   curl_setopt($ch, CURLOPT_URL, $url);
  39.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  40.   $result = str_replace("\n", " ", curl_exec($ch));
  41.   $result = iconv("ISO-8859-1", "UTF-8", $result);
  42.   $result = html_entity_decode($result, ENT_QUOTES, "UTF-8");
  43.  
  44.   preg_match_all(':<tr class="tabtxt">.*'.
  45.       '<td.*>(.*)</td>.*'.
  46.       '<td.*>(.*)</td>.*'.
  47.       '<td.*>(.*)</td>.*'.
  48.       '<td.*>(.*)</td>.*'.
  49.       '</tr>:Um', $result, $matches, PREG_SET_ORDER);
  50.  
  51.   $status = "";
  52.   foreach ($matches as $match) {
  53.     $date   = trim($match[1]);
  54.     $cp     = trim($match[2]);
  55.     $desc   = trim($match[3]);
  56.     $centre = trim($match[4]);
  57.  
  58.     if (preg_match(':(.*)\W*\((.*)\).*:Um', $centre, $array)) {
  59.       $centre = trim($array[1])." (".preg_replace("/[^0-9]/", "", $array[2]).")";
  60.     }
  61.  
  62.     $status .= t("<b>!date</b> - <i>!centre</i> !desc ", array('!date' => $date, '!desc' => $desc, '!centre' => $centre));
  63.   }
  64.  
  65.   return array('error' => FALSE, 'text' => $status);
  66. }
  67.  
  68. function tribune_colissimo_settings() {
  69.   $form = array();
  70.  
  71.   $form['tribune_colissimo_name'] = array(
  72.     '#type'           => "textfield",
  73.     '#title'          => t("Display name"),
  74.     '#default_value'  => variable_get('tribune_colissimo_name', "Colissimo"),
  75.   );
  76.  
  77.   $form['tribune_colissimo_authentified'] = array(
  78.     '#type'           => "checkbox",
  79.     '#title'          => t("Appear to be authentified"),
  80.     '#default_value'  => variable_get('tribune_colissimo_authentified', FALSE),
  81.     '#description'    => t("Whether this filter will appear to be 'anonymous' or 'authentified'. Since the filter does not correspond to a real user, posting as authentified may result in 'nickname collisions'."),
  82.   );
  83.  
  84.   return system_settings_form($form);
  85. }
  86.  
  87.