From 317fa63aeb4468bfce57beb81840ce753c5b22e2 Mon Sep 17 00:00:00 2001 From: Jacek Kowalski <Jacek@jacekk.info> Date: Sat, 30 Jun 2012 22:08:58 +0000 Subject: [PATCH] Poprawki w module lang - escape'owanie znaku ' przy wysyłaniu żądania do API --- modules/80_lang/handler.php | 62 ++++++++++++++++++++++++++++--- 1 files changed, 56 insertions(+), 6 deletions(-) diff --git a/modules/80_lang/handler.php b/modules/80_lang/handler.php index 05d6280..5dee1a6 100644 --- a/modules/80_lang/handler.php +++ b/modules/80_lang/handler.php @@ -1,7 +1,48 @@ <?php -class bot_lang_module implements BotModule { - private $APPID = ''; +require_once(dirname(__FILE__).'/msapi_config.php'); + +class msapi extends msapi_config { + public $url; + function __construct($url) { + $this->url = $url; + } + + function execute($params) { + if(!is_array($params)) { + throw new Exception('Przekazany parametr nie jest tablicą'); + } + + foreach($params as $name => &$param) { + if(substr($name, 0, 1)!='$' && is_string($param)) { + $param = '\''.strtr($param, array('\'' => '\'\'')).'\''; + } + } + unset($param); + $params['$format'] = 'json'; + + $context = stream_context_create(array( + 'http' => array( + 'request_fulluri' => TRUE, + 'header' => 'Authorization: Basic '.base64_encode(':'.$this->accountKey) + ), + )); + + $content = file_get_contents($this->url.'?'.http_build_query($params, '', '&'), FALSE, $context); + if(!$content) { + return FALSE; + } + + $content = json_decode($content, TRUE); + if(!$content) { + return FALSE; + } + + return $content; + } +} + +class bot_lang_module implements BotModule { function handle($msg, $params) { $args = trim($msg->args); @@ -23,13 +64,22 @@ $data = jsarray::parse($data); if(!$data OR count($data)==0 OR count($data[1])==0) { - $data = file_get_contents('http://api.microsofttranslator.com/v2/Http.svc/Translate?appId='.urlencode($this->APPID).'&text='.urlencode($args).'&from='.$params[0].'&to='.$params[1]); + $api = new msapi('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Translate'); + $data = $api->execute(array( + 'From' => $params[0], + 'To' => $params[1], + 'Text' => $args, + '$skip' => 0, + '$top' => 1 + )); - if(!$data) { - return new BotMsg('Błąd podczas pobierania danych ze słownika. Przepraszamy.'); + if(!$data || !isset($data['d']['results'][0]['Text'])) { + return new BotMsg('Błąd podczas pobierania danych z tłumacza. Przepraszamy.'); } - return new BotMsg('<u>Tłumaczenie (by Microsoft Translator):</u><br />'."\n".strip_tags($data)); + $data = $data['d']['results'][0]['Text']; + + return new BotMsg('<u>Tłumaczenie (by Microsoft Translator):</u><br />'."\n".htmlspecialchars($data)); } else { -- Gitblit v1.9.1