<?php
/**
* GoogleWAPDictionary class for PHP version 4 and higher.
* (c) 2008 Vagharshak Tozalakyan
*
* The class may be used to simplify Google dictionary service for connections
* with expensive traffic like GPRS.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @version 0.1
* @author Vagharshak Tozalakyan <hide@address.com>
* @license http://www.opensource.org/licenses/mit-license.php
*/
class GoogleWAPDictionary
{
var $servicePath = 'http://translate.google.com/translate_dict';
var $dictionaries = array(
'en|fr' => 'English - French',
'fr|en' => 'French - English',
'en|de' => 'English - German',
'de|en' => 'German - English',
'en|it' => 'English - Italian',
'it|en' => 'Italian - English',
'en|ko' => 'English - Korean',
'ko|en' => 'Korean - English',
'en|es' => 'English - Spanish',
'es|en' => 'Spanish - English',
'en|ru' => 'English - Russian',
'ru|en' => 'Russian - English',
'en|zh-TW' => 'English - Chinese (Traditional)',
'zh-TW|en' => 'Chinese (Traditional) - English',
'en|pt' => 'English - Portuguese',
'pt|en' => 'Portuguese - English'
);
var $defDictionary = 'en|ru';
function process()
{
$q = '';
$langpair = $this->defDictionary;
$results = '';
$path = '';
if (isset($_POST['q'])) {
$q = trim($_POST['q']);
$langpair = $_POST['langpair'];
if (!empty($q) && isset($this->dictionaries[$langpair])) {
$path = $this->servicePath . '?hl=en&q=' . urlencode($q) . '&langpair=' . urlencode($langpair);
$results = file_get_contents($path);
$results = $this->parse($results, $q);
}
}
return array($q, $langpair, $results, str_replace('&', '&', $path));
}
function parse($html, $q)
{
$pos = strpos($html, '<h1>Translation</h1>');
$html = substr($html, $pos + strlen('<h1>Translation</h1>'));
$pos = strpos($html, '<h1>');
if ($pos !== false) {
$html = substr($html, 0, $pos);
} else {
$pos = strpos($html, '<div class="tab_footer">');
$html = substr($html, 0, $pos);
}
$html = str_replace('</h2>', ' - </h2>', $html);
$html = strip_tags($html, '<h2><ol><li>');
return $html;
}
function printWML()
{
$data = $this->process();
$sel = 0;
$markup = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$markup .= '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">' . "\n";
$markup .= '<wml>' . "\n";
$markup .= '<card id="dict" title="Web Dictionary">' . "\n";
$markup .= 'Enter word: <input name="q2" value="' . htmlspecialchars($data[0]) . '" title="Enter word:" emptyok="false"/><br/>' . "\n";
$markup .= 'Dictionary: <select name="langpair2" ivalue="?">' . "\n";
$n = 0;
foreach ($this->dictionaries as $k => $v) {
$n++;
$markup .= '<option value="' . $k . '"';
if ($k == $data[1]) {
$sel = $n;
}
$markup .= '>' . $v . '</option>' . "\n";
}
$markup .= '</select><br/>' . "\n";
$markup .= '<anchor>[Find]' . "\n";
$markup .= '<go method="post" href="' . $_SERVER['PHP_SELF'] . '">' . "\n";
$markup .= '<postfield name="q" value="$(q2)"/>' . "\n";
$markup .= '<postfield name="langpair" value="$(langpair2)"/>' . "\n";
$markup .= '</go>' . "\n";
$markup .= '</anchor>' . "\n";
if (!empty($data[2])) {
$markup .= '<br/><br/>' . "\n";
$markup .= strip_tags($data[2]) . "\n";
$markup .= '<p><a href="' . $data[3] . '">Full Results</a></p>' . "\n";
}
$markup .= '</card>' . "\n";
$markup .= '</wml>' . "\n";
$markup = str_replace(' ivalue="?"', ' ivalue="' . $sel . '"', $markup);
header('Expires: Wed, 02 May 1979 04:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-type: text/vnd.wap.wml; charset=utf-8');
echo $markup;
}
function printXHTML()
{
$data = $this->process();
$markup = '<?xml version="1.0" encoding="utf-8"?>' . "\n";
$markup .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
$markup .= '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
$markup .= '<head>' . "\n";
$markup .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />' . "\n";
$markup .= '<title>Web Dictionary</title>' . "\n";
$markup .= '</head>' . "\n";
$markup .= '<body>' . "\n";
$markup .= '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">' . "\n";
$markup .= 'Enter word: <input type="text" name="q" value="' . htmlspecialchars($data[0]) . '" /><br />' . "\n";
$markup .= 'Dictionary: <select name="langpair">' . "\n";
foreach ($this->dictionaries as $k => $v) {
$markup .= '<option value="' . $k . '"';
if ($k == $data[1]) {
$markup .= ' selected="selected"';
}
$markup .= '>' . $v . '</option>' . "\n";
}
$markup .= '</select><br /><br />' . "\n";
$markup .= '<input type="submit" value="Find" />' . "\n";
$markup .= '</form>' . "\n";
if (!empty($data[2])) {
$markup .= $data[2] . "\n";
$markup .= '<p><a href="' . $data[3] . '">Full Results</a></p>' . "\n";
}
$markup .= '</body>' . "\n";
$markup .= '</html>' . "\n";
header('Expires: Wed, 02 May 1979 04:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Type: text/html; charset=UTF-8');
echo $markup;
}
function show($mode = 'wml')
{
if ($mode == 'wml') {
$this->printWML();
} else {
$this->printXHTML();
}
}
}
?>