<?php
// ini_set("max_execution_time", 0);
set_time_limit(0);// to infinity for example
class Loghatnameh {
private $translatedHTML;
private $resultArray ;
function __construct(){
$this->translatedHTML = "";
}
public function getResultInArray(){
$html = str_get_html($this->translatedHTML);
if(empty($html))
return "";
$tables_html = $html->find('table.innerTable');
foreach($tables_html as $table_html) {
$table = array();
foreach($table_html->find('tr') as $row)
{
$tr= array();
foreach($row->find('td') as $col){
$td = $col->plaintext;
$tr[] = $td;
}
$table[] = $tr;
}
$tables[] = $table;
}
return $tables;
}
public function translate($word,$ln="DE"){
$ch = curl_init();
try {
curl_setopt($ch, CURLOPT_URL, "http://www.loghatnameh.de/old-version/searchWord.do");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "word=$word&language=$ln");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
// curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds
// curl "http://www.loghatnameh.de/old-version/searchWord.do;jsessionid=15B62007F63440DC3B324520C756755F" -H "Host: www.loghatnameh.de" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Referer: http://www.loghatnameh.de/old-version/searchWord.do" -H "Cookie: JSESSIONID=15B62007F63440DC3B324520C756755F; _ga=GA1.1.36tghjul-2erg-sder-bbm6-43fgh0987jha; _ga=GA1.2.36tghjul-2erg-sder-bbm6-43fgh0987jha; _gat=1; oam.Flash.RENDERMAP.TOKEN=-m6trstgs8; JSESSIONID=0A0BB965B5FB06D25FBCC41186A82C91" -H "Connection: keep-alive" -H "Upgrade-Insecure-Requests: 1" --data "word="%"D8"%"BA"%"D8"%"B0"%"D8"%"A7&language=FA"
$headers = array();
$headers[] = "Host: www.loghatnameh.de";
$headers[] = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Accept-Language: en-US,en;q=0.5";
$headers[] = "Referer: http://www.loghatnameh.de/old-version/searchWord.do";
$headers[] = "Cookie: JSESSIONID=43719310A87BF0EB90BDDBCBCFD1F77A; _ga=GA1.1.36tghjul-2erg-sder-bbm6-43fgh0987jha; oam.Flash.RENDERMAP.TOKEN=-m6trwksku; JSESSIONID=C0330520710DCCB096B625280BF93034; _ga=GA1.2.36tghjul-2erg-sder-bbm6-43fgh0987jha";
$headers[] = "Connection: keep-alive";
$headers[] = "Upgrade-Insecure-Requests: 1";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
// if (curl_errno($ch)) {
// echo 'Error:' . curl_error($ch);
// }
curl_close ($ch);
$this->translatedHTML = $result;
$this->resultArray = $this->getResultInArray();
return $this->resultArray;
} catch ( Exception $e ) {
return "";
}
}
public function translateText($txt,$ln1="DE"){
$ch = curl_init();
if($ln1=="DE")
$ln2 ="FA";
else
$ln2 ="DE";
try {
curl_setopt ( $ch, CURLOPT_URL, "http://www.dicu.ir/tarjomeh2.php" );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, "txt=$txt&lang1=$ln1&lang2=$ln2" );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_ENCODING, 'gzip, deflate' );
$headers = array ();
$headers [] = "Host: www.dicu.ir";
$headers [] = "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
$headers [] = "Accept: */*";
$headers [] = "Accept-Language: en-US,en;q=0.5";
$headers [] = "Content-Type: application/x-www-form-urlencoded";
$headers [] = "X-Requested-With: XMLHttpRequest";
$headers [] = "Referer: http://www.dicu.ir/index.php";
$headers [] = "Cookie: HstCfa2992739=1486424263604; HstCla2992739=1486424263604; HstCmu2992739=1486424263604; HstPn2992739=1; HstPt2992739=1; HstCnv2992739=1; HstCns2992739=1";
$headers [] = "Connection: keep-alive";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
echo $result = curl_exec ( $ch );
if (curl_errno ( $ch )) {
echo 'Error:' . curl_error ( $ch );
}
curl_close ( $ch );
$this->translatedHTML = $result;
// $this->resultArray = $this->getResultInArray();
// return $this->resultArray;
} catch ( Exception $e ) {
return "";
}
}
public function makeTranslationTextMessage() {
$translated ="";
foreach ($this->resultArray as $table){
foreach($table as $tr){
if(!empty($tr[2]))
$translated.= trim($tr[2]).": ".$tr[5].PHP_EOL;
}
}
if($translated="")
$translated ="Keine Ahnung 😭";
else
$translated . " 😉";
return $translated;
}
}