xurrency

xurrency.inc

  1. <?php
  2. // vim:filetype=php expandtab tabstop=2 softtabstop=2 shiftwidth=2 autoindent smartindent
  3. // $Id$
  4.  
  5. function tribune_xurrency_info() {
  6.   $status = class_exists("SoapClient") ? t("available") : t("<strong>not</strong> available");
  7.  
  8.   return t('Converts between currencies using data from !url (needs Soap which is !status)', array('!url' => l('Xurrency', 'http://xurrency.com/api'), '!status' => $status));
  9. }
  10.  
  11. function tribune_xurrency_filter(&$post) {
  12.   $answer = array(
  13.     'info' => variable_get('tribune_xurrency_name', "Xurrency"),
  14.   );
  15.  
  16.   if (variable_get('tribune_xurrency_authentified', FALSE)) {
  17.     $answer['login'] = $answer['info'];
  18.   }
  19.  
  20.   if (preg_match(':([0-9]+([,\.][0-9]{2})?) ?([A-Z]{3}) '. t('[ei]n') .' ([A-Z]{3}):', $post['message'], $matches)) {
  21.     $comma = FALSE;
  22.     $amount = $matches[1];
  23.  
  24.     if (strpos($amount, ',') !== FALSE) {
  25.       $comma = TRUE;
  26.       $amount = str_replace(",", ".", $amount);
  27.     }
  28.  
  29.     $cur1 = $matches[3];
  30.     $cur2 = $matches[4];
  31.  
  32.     $response = array('error' => FALSE, 'text' => "");
  33.     if (_tribune_xurrency_are_symbols_valid(array($cur1, $cur2))) {
  34.       $result = _tribune_xurrency_convert($amount, $cur1, $cur2);
  35.  
  36.       if ($result) {
  37.         if ($comma) {
  38.           $amount = str_replace(".", ",", $amount);
  39.           $result = str_replace(".", ",", $result);
  40.         }
  41.         $answer['message'] = tribune_filters_print_clock($post) ." ". $amount ." ". $cur1 ." = ". $result ." ". $cur2;
  42.  
  43.         return array($answer);
  44.       }
  45.     }
  46.   }
  47. }
  48.  
  49. function _tribune_xurrency_are_symbols_valid($symbols) {
  50.   $xurrency = new SoapClient('http://xurrency.com/api.wsdl');
  51.  
  52.   foreach ($symbols as $symbol) {
  53.     if (!$xurrency->isCurrency(strtolower($symbol))) {
  54.       return FALSE;
  55.     }
  56.   }
  57.  
  58.   return TRUE;
  59. }
  60.  
  61. function _tribune_xurrency_convert($amount, $from, $to) {
  62.   $xurrency = new SoapClient('http://xurrency.com/api.wsdl');
  63.  
  64.   return round($xurrency->getValue($amount, strtolower($from), strtolower($to)), 2);
  65. }
  66.  
  67. function tribune_xurrency_settings() {
  68.   $form = array();
  69.  
  70.   $form['tribune_xurrency_name'] = array(
  71.     '#type'           => "textfield",
  72.     '#title'          => t("Display name"),
  73.     '#default_value'  => variable_get('tribune_xurrency_name', "Xurrency"),
  74.   );
  75.  
  76.   $form['tribune_xurrency_authentified'] = array(
  77.     '#type'           => "checkbox",
  78.     '#title'          => t("Appear to be authentified"),
  79.     '#default_value'  => variable_get('tribune_xurrency_authentified', FALSE),
  80.     '#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'."),
  81.   );
  82.  
  83.   return system_settings_form($form);
  84. }
  85.  
  86.