19,29c19,29
< 	case 'admin/modules#gtrans':
< 	  $output = 'gtrans';
< 	  break;
< 	case 'admin/modules#description':
< 	case 'admin/help#gtrans':
< 	  $output = t('Translates site using Google Translation.');
< 	  break;
< 	case 'user/help#gtrans':
< 	  $output = t('Translates site using Google Translation service.');
< 	  break;
< 	}
---
>   case 'admin/modules#gtrans':
>     $output = 'gtrans';
>     break;
>   case 'admin/modules#description':
>   case 'admin/help#gtrans':
>     $output = t('Translates site using Google Translation.');
>     break;
>   case 'user/help#gtrans':
>     $output = t('Translates site using Google Translation service.');
>     break;
>   }
40c40
< 	'description' => t('Control Google Translate service.'),
---
>   'description' => t('Control Google Translate service.'),
52,54c52,54
< 	'#title'=>t('Enabled languages'),
< 	'#options' => $languages,
< 	'#default_value' => variable_get('gtrans_enabled_languages', array('en', 'fr', 'de')),
---
>   '#title'=>t('Enabled languages'),
>   '#options' => $languages,
>   '#default_value' => variable_get('gtrans_enabled_languages', array('en', 'fr', 'de')),
57,59c57,59
< 	'#title'=>t('Site language'),
< 	'#options' => $languages,
< 	'#default_value' => variable_get('gtrans_your_language', 'en'),
---
>   '#title'=>t('Site language'),
>   '#options' => $languages,
>   '#default_value' => variable_get('gtrans_your_language', 'en'),
69,70c69,70
< 	 $blocks[0]['info'] = t('Change Language');
< 	 return $blocks;
---
>    $blocks[0]['info'] = t('Change Language');
>    return $blocks;
73,79c73,79
< 		$block = array();
< 		switch ($delta) {
< 			case 0:
< 				$block['subject'] = t('Change Language');
< 				$block['content'] = drupal_get_form('gtrans_change_language_form');
< 				return $block;
< 		}
---
>     $block = array();
>     switch ($delta) {
>       case 0:
>         $block['subject'] = t('Change Language');
>         $block['content'] = drupal_get_form('gtrans_change_language_form');
>         return $block;
>     }
84,96c84,96
< 	$languages = gtrans_get_avail_langs();
< 	// get enabled languages, assume french and spanish by default
< 	$lang = variable_get('gtrans_enabled_languages', array('fr' => 'fr', 'es' => 'es'));
< 	$hls = array();
< 	// show only enabled language
< 	foreach($lang as $key => $val){
< 		if(!is_null($languages[$val])){
< 			$hls[$val] = $languages[$val];
< 		}
< 	}
< 	$form['hl']  = array('#type' => 'select',
< 		'#default_value' => ($_SESSION['hl']? $_SESSION['hl'] : 'fr'),
< 		'#options' =>$hls,
---
>   $languages = gtrans_get_avail_langs();
>   // get enabled languages, assume french and spanish by default
>   $lang = variable_get('gtrans_enabled_languages', array('fr' => 'fr', 'es' => 'es'));
>   $hls = array();
>   // show only enabled language
>   foreach($lang as $key => $val){
>     if(!is_null($languages[$val])){
>       $hls[$val] = $languages[$val];
>     }
>   }
>   $form['hl']  = array('#type' => 'select',
>     '#default_value' => ($_SESSION['hl']? $_SESSION['hl'] : 'fr'),
>     '#options' =>$hls,
98,100c98,100
< 		);
< 	$form['submit'] = array('#type' => 'submit',
< 		'#value' => t('Change'),
---
>     );
>   $form['submit'] = array('#type' => 'submit',
>     '#value' => t('Change'),
102c102
< 		);
---
>     );
104c104
< 	return $form;
---
>   return $form;
116c116,117
< function gtrans_init(){
---
> function gtrans_init()
> {
118,121c119,122
< 	avoid calling translation for POSTed pages
< 	and for pages called by Google Translate service,
< 	otherwise we'll end up going in a chain of calls
< 	of the current drupal page
---
>   avoid calling translation for POSTed pages
>   and for pages called by Google Translate service,
>   otherwise we'll end up going in a chain of calls
>   of the current drupal page
123,126c124,150
<   if(!$_GET['gtrans'] && !$_POST && _garg(0)!='admin'){
< 		if($_SESSION['drupal_hl']){
< 			gtrans_translate_page($_SESSION['drupal_hl']);
< 		}
---
>   if(!$_GET['gtrans']){
>     if($_SESSION['drupal_hl']){
>       ob_start();  
>     }
>   }
>   if (intval($_GET['gtrans']))
>   {
>     $transid=intval($_GET['gtrans']);
>     if(file_exists($fileid="/tmp/$transid.gtrans_auxdata.gz"))
>     {
>       // ¡¡¡¡¡ CHECK FIRST FOR THE MD5 !!!!!
>       // Hackers can try to read this if you don't check it.
>       if ($_GET['md5']==md5_file($fileid))
>       {
>       $f1=fopen($fileid,"r");
>       $decompressed_file = '';
>       while (!feof($f1))  $decompressed_file .= fread($f1, 4096);
>       fclose($f1);
>       unlink($fileid);
>       die($decompressed_file /* . "<p>number $transid</p>" */);
>       }
>       else
>       {
>       
>       }
>     }
>     else die("this session not exists");
127a152,164
> 
> 
> }
> 
> function gtrans_exit()
> {
>   if(!$_GET['gtrans']){
>     if($_SESSION['drupal_hl']){
>       $html=ob_get_clean();  
>       gtrans_translate_page($_SESSION['drupal_hl'],$html);
>     }
>   }
> 
133c170,171
< function gtrans_translate_page($hl='fr'){
---
> function gtrans_translate_page($hl,$html)
> {
137,194c175,255
<   if($hl != variable_get('gtrans_your_language', 'en')){
< 		global $base_url;
< 		// Google Translate IP
< 		$url = '64.233.179.104';
< 		// prepare the request, add a parameter to prevent 
< 		// recursively attempting to translate
< 		$req_uri = str_replace('destination=', '', drupal_get_destination());
< 		$uri = 'translate_c?hl=en&ie=UTF-8&oe=UTF-8&langpair='.variable_get('gtrans_your_language', 'en').'%7C'.$hl.'&u='.$base_url.'/'.$req_uri.'%3Fgtrans%3Dtrue';
< 		// connecting...
< 		$fp=fsockopen($url, 80, $errno, $errstr, 60);
< 		if(!$fp){
< 			watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Couldn\'t contact Google Translate server');
< 			break;
< 		}
< 		else{
< 			// connected!
< 			// prepare headers to send
< 			$out = "GET /".$uri." HTTP/1.1\r\n";
< 			$out .= "Host: ".$url."\r\n";
< 			$out .= "Connection: Close\r\n\r\n";
< 			// send header
< 			fwrite($fp, $out);
< 			// receive data
< 			$body = false;
< 			while (!feof($fp)) {
< 			$s = fgets($fp, 1024);
< 			if ( $body )
< 				$content .= $s;
< 			// ignore HTTP headers
< 			if ( $s == "\r\n" )
< 						 $body = true;
< 			}
< 			// close the connection
< 			fclose($fp);
< 			// check the data we got:
< 			if(!strlen($content)){
< 				watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': No content from Google Translate server');
< 			}
< 			else{
< 				// now prepare content
< 				$content_array = explode("\r\n", $content);
< 				$content = $content_array[1];
< 				// Google Translate modifes all links to point to it,
< 				// we will reverse this action and take the links back
< 				$content = preg_replace('/<a href="(.*?)u=(.*?)">/', '<a href="$2">', $content);
< 				// now translate HTML entites
< 				$content = str_replace(str_replace('%7C', '&#037;7C', htmlentities($uri)), '', $content);
< 				// set language in session
< 				$_SESSION['drupal_hl'] = $hl;
< 				// shoot the translated page and end script run
< 				if(strlen($content)>30){
< 					die($content);
< 				}
< 				else{
< 					watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Content I got was: '.$content);
< 				}
< 			}
< 		}
---
>   
>   if($hl != variable_get('gtrans_your_language', 'en') && strlen($html)>1024)
>   {
>     $transid=1;//rand();
>     $n=1;
>     while(file_exists($fileid="/tmp/$transid.gtrans_auxdata.gz")) $transid=$n++;
>     
>     if ($transid>10) // Thisi is the Max translation we can handle at same time.
>     {  // Useful for preventing recursive loads.
>     $n=1;
>       while(file_exists($fileid="/tmp/$transid.gtrans_auxdata.gz")) 
>       {
>         unlink("/tmp/$transid.gtrans_auxdata.gz");
>         $transid=$n++;
>       }
>       //die("Exceeded max limit of translations");
>       return;
>     }
>     $f1=fopen($fileid,"w1"); 
>     fwrite($f1,$html);
>     fclose($f1);
>     $md5=md5_file($fileid); // I will put the MD5 here to prevent hackers to read the files.
>     global $base_url;
>     // Google Translate IP
>     $url = '64.233.179.104';
>     // prepare the request, add a parameter to prevent 
>     // recursively attempting to translate
> //    $req_uri = str_replace('destination=', '', drupal_get_destination());
>     $uri = 'translate_c?hl=en&ie=UTF-8&oe=UTF-8&langpair='.variable_get('gtrans_your_language', 'en').'%7C'.$hl.'&u='.$base_url.'%3Fgtrans%3D'.$transid . '%26md5%3D'.$md5;
>     // connecting...
>     $fp=fsockopen($url, 80, $errno, $errstr, 60);
>     if(!$fp)
>     {
>       watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Couldn\'t contact Google Translate server');
>       break;
>     }
>     else
>     {
>       // connected!
>       // prepare headers to send
>       $out = "GET /".$uri." HTTP/1.1\r\n";
>       $out .= "Host: ".$url."\r\n";
>       $out .= "Connection: Close\r\n\r\n";
>       // send header
>       fwrite($fp, $out);
>       // receive data
>       $body = false;
>       while (!feof($fp)) {
>       $s = fgets($fp, 1024);
>       if ( $body )
>         $content .= $s;
>       // ignore HTTP headers
>       if ( $s == "\r\n" )
>              $body = true;
>       }
>       // close the connection
>       fclose($fp);
>       // check the data we got:
>       if(!strlen($content)){
>         watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': No content from Google Translate server');
>       }
>       else{
>         // now prepare content
>         $content_array = explode("\r\n", $content);
>         $content = $content_array[1];
>         // Google Translate modifes all links to point to it,
>         // we will reverse this action and take the links back
>         $content = preg_replace('/<a href="(.*?)u=(.*?)">/', '<a href="$2">', $content);
>         // now translate HTML entites
>         $content = str_replace(str_replace('%7C', '&#037;7C', htmlentities($uri)), '', $content);
>         // set language in session
>         $_SESSION['drupal_hl'] = $hl;
>         // shoot the translated page and end script run
>         if(strlen($content)>30){
>           die($content);
>         }
>         else{
>           watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Content I got was: '.$content);
>         }
>       }
>     }
195a257,258
>   else echo $html;
> 
217,227c280,290
< 	'ar'=> 'Arabic', 
< 	'en'=> 'English', 
< 	'fr'=> 'French', 
< 	'de'=> 'Deutsch', 
< 	'it'=> 'Italian', 
< 	'es'=> 'Spanish', 
< 	'pt'=> 'Portuguese', 
< 	'ja'=> 'Japanese', 
< 	'zh-CN'=> 'Chinese', 
< 	'ko'=> 'Korean', 
< 	);
---
>   'ar'=> 'Arabic', 
>   'en'=> 'English', 
>   'fr'=> 'French', 
>   'de'=> 'Deutsch', 
>   'it'=> 'Italian', 
>   'es'=> 'Spanish', 
>   'pt'=> 'Portuguese', 
>   'ja'=> 'Japanese', 
>   'zh-CN'=> 'Chinese', 
>   'ko'=> 'Korean', 
>   );
