element = array("td", "a", 'title', 'span', "label", "em", "strong", "meta [name=description]", "meta [name=keywords]", "li", "u", "h1", "h2", "h3", "h4", "p", "div [class=jb_post_body]", "div [class=jb_post_footer]", "p [class=MsoNormal]", "div [id=richsnippetsvote]", "div [class=acc-content]", "div [class=tabContent-inner]", "div [class=componentheading]", ); $this->no_translate = array(" "); } function GetLink($main) { global $c; global $limit; $ex = array("/index.php", "/"); $text = @file_get_contents($main); if (!$text) $text = $c->PostURL($main); if (!$text) $this->Log("No sub link for: ".$main); $length = strlen($text); $part = ceil($length/$limit); $link = array(); for($i=0;$i<$part;$i++) { $str = substr($text,$i*$limit,$limit); $html = str_get_html($str); foreach($html->find('a') as $elm) { $l = $elm->href; if (!in_array($l,$ex)) $link[] = $l; } $html->__destruct(); if (count($link) > 1000) { $this->Log(number_format(memory_get_usage())); sleep(2); } } return $link; } function WriteFile($file,$value,$action="w") { $handle = fopen($file,$action); if ($handle) { fwrite($handle, $value); fclose($handle); return true; } else return false; } function TranslateText($main, $dest) { global $c; global $gt; global $limit; do { $this->Log("Getting content of URL: ".$main); $text = @file_get_contents($main); if (!$text) $text = $c->PostURL($main); $length = strlen($text); $this->Log("Content Size: ".$length); sleep(2); } while ($length == 0 || eregi("flood",$text)); $part = ceil($length/$limit); for($z=0;$z<$part;$z++) { $str = substr($text,$z*$limit,$limit); $html = str_get_html($str); for ($i=0;$ielement);$i++) { $el = $this->element[$i]; foreach($html->find($el) as $elm) { if (eregi("meta",$el)) $component = "content"; else $component = "innertext"; $t = $elm->$component; if (trim($t) && !in_array($t,$this->no_translate)) { $key = $this->clean($t); if ($key) { $check = $c->GetQuery("SELECT COUNT(*) FROM translation_old WHERE trans_key = \"".addslashes($key)."\" AND element = '".$el."'"); if (eregi("So what are you waiting for? Feel free to view our web",$key)) $c->dump($key); if ($check[0] == 0) { try { $translate = $gt->translate($t, "en", $dest); } catch (Exception $e) { $this->Log('Caught exception: ', $e->getMessage()); } if (trim($translate)) { $this->Log("Translating (".$el.") \"".addslashes($t)."\" into ".$dest.": \"".$translate."\""); $c->ExecQuery("INSERT INTO translation_old (element, english, `".$dest."`) VALUES ('".$el."', \"".addslashes($t)."\", \"".addslashes($translate)."\")"); } } } } } } $html->__destruct(); } } function replaceLink($elm,$url,$dest) { global $c; $ex = array("/index.php", "/"); if (preg_match("/CourseID=/",$elm->href)) $elm_href = str_replace("index.php",$dest."/index.php",$elm->href); elseif (preg_match("/page=/",$elm->href)) $elm_href = str_replace("main.php",$dest."/index.php",$elm->href); else { if (!preg_match("/mailto/",$elm->href) && !preg_match("/converter.php/",$elm->href)) { $trans = $c->GetQuery("SELECT TRIM(`".$dest."`) FROM `translation_link_".$dest."` WHERE link = \"".addslashes($elm->href)."\""); if ($trans[0]) { if (preg_match("/studyinaustraliauniversity/",$elm->href)) $href = "/index.php?".$elm->href; else $href = $trans[0]; if (substr($href,0,1) == "/") $elm_href = $url.$dest.$href; else $elm_href = $url.$dest."/".$href; } if (in_array($elm->href,$ex)) $elm_href = $url.$dest."/"; } } if (isset($elm_href)) return $elm_href; } function getTranslationIndex($translation_link_id, $lang) { $file = dirname(dirname(dirname(dirname(__FILE__))))."/brightannica_data/index/".$lang."/".$translation_link_id.".html"; if (is_file($file)) { echo $this->ReadFile($file); echo $translation_link_id; exit(); } } function ReadFile($file) { return fread(fopen($file, "r"), filesize($file)); } function Translate($main,$dest, $translation_link_id, $lang) { $this->getTranslationIndex($translation_link_id, $lang); $start = time(); global $c; global $url; global $limit; if (preg_match("/studyinaustraliauniversity/",$main)) $main = str_replace("index.php?","",$main); $text = @file_get_contents($main); if (!$text) $text = $c->PostURL($main); $length = strlen($text); $part = ceil($length/$limit); $doc = ""; for($z=0;$z<$part;$z++) { $str = substr($text,$z*$limit,$limit); $html = str_get_html($str); for ($i=count($this->element);$i>0;$i--) { $el = $this->element[$i-1]; foreach($html->find($el) as $elm) { if (preg_match("/meta/",$el)) $source = $elm->content; else $source = trim($elm->innertext); if ($source) { if ($el == "td" && preg_match("/href/",$source)) unset($result); else $result = $c->GetQuery("SELECT TRIM(`".$dest."`) FROM translation_old WHERE trans_key = \"".addslashes($this->clean($source))."\" AND element = '".$el."'"); if (isset($result[0])) { if (preg_match("/meta/",$el)) $elm->content = $result[0]; else $elm->innertext = $result[0]; } } } } foreach($html->find("a") as $elm) { $res = $this->replaceLink($elm,$url,$dest); if ($res) $elm->href = $res; } $doc .= (string) $html; $html->__destruct(); } $process = time()-$start; //$doc .= "Page loaded in: ".$process." second(s)."; return trim($doc); } function clean($source) { return substr(trim(str_replace(array("\n","\r"),"",strip_tags($source))),0,255); } function Log($text) { $text = $text."\n"; echo $text; $this->WriteFile("log.txt",$text,"a"); } function StoreLink($main,$link) { global $c; //global $gt; for($i=0;$iGetQuery("SELECT * FROM translation_link_old WHERE link =\"".addslashes($l)."\""); if ($check[0] == 0) { if (!eregi("converter.php",$l)) { //$translate = str_replace(" ","-",$gt->translate(str_replace("-"," ",$l), "en", $lang)); $this->Log("Storing (link) \"".addslashes($l)."\""); $c->ExecQuery("INSERT INTO translation_link_old (parent, link) VALUES (\"".addslashes($main)."\", \"".addslashes($l)."\")"); } } } } function GetAllLink($url=false) { global $c; global $main; $target = $main.$url; $check = $c->GetQuery("SELECT COUNT(*) FROM translation_link_old WHERE parent = \"".addslashes($url)."\""); if ($check[0] == 0) { $this->Log("Getting sub page of: ".$target); $link = $this->GetLink($target); $this->StoreLink($url,$link,$lang); sleep(2); } } } ?>