• Forum
  • Doc
  • Screenshots
  • Download
  • Donate
  • Contributors
  • Contact
  • Follow @phpfreechat
  • DEMO
  • Board index ‹ Version 1.x branch ‹ Contributions (v1.x)
  • Change font size
  • FAQ
  • Register
  • Login

Calc Command

Post a bug fix, a new feature, a theme ...

Moderators: OldWolf, re*s.t.a.r.s.*2

Post a reply
7 posts • Page 1 of 1

Postby Hap » Tue Jun 06, 2006 11:30 pm

The following will give a /calc command, which takes advantage of Google's calculator (http://www.google.com/help/features.html#calculator).

This will take any argument that the Google Calculator would take.

Ex:
simple math: /calc 5+8
conversions: /calc 28 C in F
constants: /calc Mass of Sun
currency: /calc 5 CAD in Euros

Create a file called calc.class.php and save the following in your chat/src/ directory. Copy the following into the file.
Code: Select all
class Calculator {

  var $command;

  function check($text){
    $this->errors = array();
    $this->command= '';
    $this->text   = $text;

   $google = file_get_contents('http://www.google.com/search?q=' . urlencode($this->text));
   $pos = strpos($google,'calc_img.gif');
   if(!$pos)
      return false;
   preg_match_all('{<b>.+= (.+?)</b>}', substr($google,$pos), $matches);
   $this->command['calc'] = strip_tags($matches[0][0], '<sup><sub>');

    return true;
  }

  function printCalc(){
   $parts = explode('=',$this->command['calc']);
    $result = $parts[0].' = <strong>'.$parts[1].'</strong>';

    return $result;
  }

  function error_get(){
    if(!count($this->errors)){
      return '';
    } else {
      return join("<br />n", $this->errors);
    }
  }
 
}

On the page that calls the chat, before $chat->printChat(); copy:
Code: Select all
class pfcCommand_calc extends pfcCommand
{
  function run(&$xml_reponse, $clientid, $msg)
  {
   $c =& $this->c;
   
   $nick      = $c->nick;
   $container =& $c->getContainerInstance();
   $text      = trim($msg);
   
   // Call item
   require_once 'chat/src/calc.class.php';  // EDIT THIS LINE TO SUIT YOUR SETUP
   $calculate = new Calculator();
   if (!$calculate->check($text))
   {
     $result = $calculate->error_get();
     $cmd =& pfcCommand::Factory("error", $c);
     $cmd->run($xml_reponse, $clientid, "Unable to calculate  " . $result);
   }
   else
   {
     $result = $calculate->printCalc();
     $container->writeMsg($nick, $result);
   }
   if ($c->debug) pxlog("Cmd_item[".$c->sessionid."]: msg=".$result, "chat", $c->getId());
  }
}

Be sure to edit:
require_once 'chat/src/calc.class.php';
to match the path to class file.
Last edited by Hap on Wed Jun 07, 2006 11:09 pm, edited 1 time in total.
Hap
New member
 
Posts: 8
Joined: Tue May 30, 2006 12:09 am
Top

Postby phpfreechat » Wed Jun 07, 2006 9:04 am

I repeat myself: please post the code and explainations needed to integrate your Calculator class into phpfreechat.
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby Hap » Wed Jun 07, 2006 11:08 pm

Edited my first post with more instructions.
Hap
New member
 
Posts: 8
Joined: Tue May 30, 2006 12:09 am
Top

Postby phpfreechat » Wed Jun 07, 2006 11:23 pm

Thank you Hap for these nice contributions :)
phpfreechat
Site Admin
 
Posts: 2657
Joined: Tue Feb 07, 2006 3:35 pm
Location: France
Top

Postby machadoug » Wed Oct 31, 2007 5:38 pm

Hap,

Thanks for this class!

I had to make a lot of changes to work with my site and in my server.
I host my acount in a shared server and they don't let me use file_get_contents functon so I have to use the curl functions...

It is all in one file now!
Create a file called calc.class.php and save the following in your chat/src/commands directory. Copy the following into the file.
Code: Select all
<?php

require_once(dirname(__FILE__)."/../pfccommand.class.php");
class Calculator {

  var $command;

  function check($text){
    $this->errors = array();
    $this->command= '';
    $this->text   = $text;
   
    $cGetFile   = new curl();
   
    $google = $cGetFile->getFile('http://www.google.com/search?q=' . urlencode($this->text));
    $pos = strpos($google,'calc_img.gif');
    if(!$pos)
        return false;
    preg_match_all('{<b>.+= (.+?)</b>}', substr($google,$pos), $matches);
    $this->command['calc'] = strip_tags($matches[0][0], '<sup><sub>');

    return true;
  }

  function printCalc(){
    $parts = explode('=',$this->command['calc']);
    $result = $parts[0].' = <strong>'.$parts[1].'</strong>';

    return $result;
  }

  function error_get(){
    if(!count($this->errors)){
      return '';
    } else {
      return join("<br />n", $this->errors);
    }
  }
 
}

class pfcCommand_calc extends pfcCommand{
   var $usage = "/calc {text}";
  function run(&$xml_reponse, $p){
    $clientid    = $p["clientid"];
    $param       = $p["param"];
    $sender      = $p["sender"];
    $recipient   = $p["recipient"];
    $recipientid = $p["recipientid"];

    $c  =& pfcGlobalConfig::Instance();
    $u  =& pfcUserConfig::Instance();
    $ct =& pfcContainer::Instance();

    if (trim($param) == ''){
      // error
      $cmdp = $p;
      $cmdp["param"] = _pfc("Missing parameter");
      $cmdp["param"] .= " (".$this->usage.")";
      $cmd =& pfcCommand::Factory("error");
      $cmd->run($xml_reponse, $cmdp);
      return false;
    }
   
    $text      = trim($param);

    $calculate = new Calculator();
    if (!$calculate->check($text)){
      $result = $calculate->error_get();
      $cmd =& pfcCommand::Factory("error", $c);
      $xml_reponse->script("Não foi possível calcular  " . $result);
    }else{
      $result = $calculate->printCalc();
      $ct->write($recipient, $sender, "send", $result);
    }
    //if ($c->debug) pxlog("Cmd_item[".$c->sessionid."]: msg=".$result, "chat", $c->getId());
  }
}
class curl {
   var $timeout;
   var $url;
   var $file_contents;
   
   function getFile($url,$timeout=3) {
      # use CURL library to fetch remote file
      $ch = curl_init();
      $this->url = $url;
      $this->timeout = $timeout;
      curl_setopt ($ch, CURLOPT_URL, $this->url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
      $this->file_contents = curl_exec($ch);
      if ( curl_getinfo($ch,CURLINFO_HTTP_CODE) !== 200 ) {
         return('Bad Data File '.$this->url);
      } else {
         return $this->file_contents;
      }
   }
}
?>

Best regards from Brazil,
Douglas de Senne Machado
http://invest.fok.com.br
http://joomla.focalizaisso.com.br
machadoug
New member
 
Posts: 8
Joined: Fri Oct 19, 2007 9:17 pm
Location: Joinville - SC - Brazil
  • Website
Top

Postby ninjaw » Sat Nov 03, 2007 11:03 am

The one file only works great but there is a brazilian error message ...

However, I cant see any error message ....

BUT i tweaked the script to use google.fr, and all is working great i can use french questions and got french answers BUT there is no special chars:

11:57:14 ‹Ninjaw› 5 euros = 7,2215 dollars amr0069cain

Any way to fix this ?
ninjaw
Member
 
Posts: 49
Joined: Thu Oct 04, 2007 10:40 pm
Top

Postby unveiled » Wed Nov 11, 2009 2:59 am

Had issues with the original code, but the single file one works great.

There is one bug though, when you do some word queries like "2 times 5" you get this info message:

10 More about calculator.Search Results5x2 (2004)5 x 2: Five Times Two (Canada: English title) (festival title) Cinq fois deux ( France) (alternative spelling) Five Times Two (International: English title) ...www.imdb.com/title/tt0354356/ - Cached - SimilarWikiAnswers - What is 2 times 5 divided by 2 times 2Math question: What is 2 times 5 divided by 2 times 2? (2x5)/(2x2)
11/11/2009 01:49:27 ‹Unveiled› 2 times 8 = 16 More about calculator.Search ResultsWikiAnswers - What does 2 times 8 equalSchool Subjects question: What does 2 times 8 equal? Ok wow why do you not know that 16? 16 16 16 16 16 16 The answer to that is 16. Cause 2X8

How do you remove it?
unveiled
New member
 
Posts: 2
Joined: Wed Nov 11, 2009 2:52 am
Top


Post a reply
7 posts • Page 1 of 1

Return to Contributions (v1.x)

Who is online

Users browsing this forum: No registered users and 17 guests

  • Board index
  • The team • Delete all board cookies • All times are UTC + 1 hour
Powered by phpBB® Forum Software © phpBB Group
Sign in
Wrong credentials
Sign up I forgot my password
.
jeu-gratuit.net | more partners
Fork me on GitHub