Index: gtrans.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/gtrans/gtrans.module,v
retrieving revision 1.13
diff -u -p -r1.13 gtrans.module
--- gtrans.module	8 May 2007 23:19:03 -0000	1.13
+++ gtrans.module	2 Mar 2008 15:02:13 -0000
@@ -128,6 +128,73 @@ function gtrans_init(){
 }
 
 /**
+ * Connect to google translate and send headers.  Collect result and return
+ */
+function gtrans_google_connection($url,$uri,$redirect_max,$redirect_count=0) {
+	// initialize return content to ""
+	$content = "";
+	
+	// open socket
+	$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 ((stristr($s,"HTTP/1.1 301")!="") || (stristr($s,"HTTP/1.1 302")!="")) {
+				// if a 301 or 302 is provided by google translate, parse the next line for the redirection URL
+				$s = fgets($fp,1024);
+				$redirect=preg_replace("/location:/i","",$s);  
+                        	$url_bits = parse_url(trim($redirect));
+				
+				if($redirect_count < $redirect_max) {
+					// we are under the redirect limit, call self on new url..  
+					fclose($fp);
+					return gtrans_google_connection($url_bits['host'],$uri,$redirect_max,$redirect_count+1);
+				}
+				else {	
+					// over the redirect limit, return what we've got
+					fclose($fp);
+					return $content;
+				}
+			}
+			
+			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');
+		}
+	}
+		
+		
+	return $content;
+
+}
+
+/**
  * The real stuf: translate the whole drupal page and send it to the browser.
  */
 function gtrans_translate_page($hl='fr'){
@@ -137,64 +204,43 @@ function gtrans_translate_page($hl='fr')
   if($hl != variable_get('gtrans_your_language', 'en')){
 		global $base_url;
 		// Google Translate IP
-		$url = '64.233.179.104';
+		$url = 'translate.google.com';
 		// 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;
+		
+		// send url and uri to google - follow up to 3 redirects
+		$content = gtrans_google_connection($url,$uri,3);
+		
+		// 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{
-			// 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');
+			// 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{
-				// 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);
-				}
+				watchdog('user', t('Attempt failed to translate page').' '.$_SERVER['REQUEST_URI'].' '.t('to').' '.$languages[$hl].': Content I got was: '.$content);
 			}
 		}
-  }
+   }
 }
 
+
 /**
  * A duplicate of arg()
  *
