Author Topic: Googiespell / SSL  (Read 7814 times)

Offline rosali

  • Hero Member
  • *****
  • Posts: 2,533
Googiespell / SSL
« on: February 06, 2008, 03:29:00 PM »
I was fighting with my ISP webspace. Unfortunately it is poor without ssl support in their apache/php build.

Here is an approach to bypass it:

config/main.inc.php

Code: [Select]
// googiespell url
// TODO: when database config is implemented, modify "program/js/tiny_mce/plugins/spellchecker/config.php" to pick up this value from database !!!
$rcmail_config['googliespell_url'] = '[url]https://www.google.com/tbproxy/spell?hl=en&lang=';[/url]

// googiespell tunnel
$rcmail_config['googiespell_tunnel_host'] = 'liebl.ath.cx';
$rcmail_config['googiespell_tunnel_request'] = '/googiespell/';

googiespell_tunnel_host -> liebl.ath.cx that's my dev box (stuff will be there for limited time)

http://liebl.ath.cx/googiespell/index.php

Code: [Select]
////////////////////////////////////////
// (c) [url]www.roland-liebl.de[/url]
// NO WARRANTY - USE AS AT YOUR OWN RISK
////////////////////////////////////////

// default settings
$host = "ssl://www.google.com";
$port = 443;
$path = "/tbproxy/spell?lang=en";
$data = "";

// serve $_GET

foreach($_GET as $key => $val){
$_POST[$key] = $val;
}

// use $_POST

if(
isset($_POST['host']) &&
isset($_POST['port']) &&
isset($_POST['path'])
 ){

$host = $_POST['host'];
$port = $_POST['port'];
$path = $_POST['path'];
}

// now proceed

if(isset($_POST['data'])){
$data = str_replace("\\","",$_POST['data']);
}

if ($fp = fsockopen($host, $port, $errno, $errstr, 30)){
$out = "POST $path HTTP/1.0\r\n";
  $out .= "Host: $host\r\n";
  $out .= "Content-Length: " . strlen($data) . "\r\n";
  $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  $out .= "Connection: Close\r\n\r\n";
  $out .= $data;
  fwrite($fp, $out);
  while (!feof($fp))
  $store .= fgets($fp, 128);
  fclose($fp);
}

print $store;

?>


/program/steps/mail/spell.inc

Code: [Select]


/*
 +-----------------------------------------------------------------------+
 | program/steps/mail/spell.inc                     |
 |                                   |
 | This file is part of the RoundCube Webmail client          |
 | Copyright (C) 2005-2007, RoundCube Dev. - Switzerland        |
 | Licensed under the GNU GPL                      |
 |                                   |
 | PURPOSE:                               |
 | Submit request to Google's spell checking engine          |
 |                                   |
 | CREDITS:                               |
 | Script from GoogieSpell by amix.dk                 |
 |                                   |
 +-----------------------------------------------------------------------+
 | Author: Thomas Bruederli            |
 +-----------------------------------------------------------------------+

 $Id: spell.inc 850 2007-10-03 00:13:32Z ihug $

*/

$REMOTE_REQUEST = TRUE;

// default settings
$host = "ssl://www.google.com";
$port = 443;
$lang = get_input_value('lang', RCUBE_INPUT_GET);
$path = "/tbproxy/spell?lang=$lang";

// spell check uri is configured
if (!empty($CONFIG['spellcheck_uri']))
 {
 $a_uri = parse_url($CONFIG['spellcheck_uri']);
 $ssl = ($a_uri['scheme']=='https' || $a_uri['scheme']=='ssl');
 $port = $a_uri['port'] ? $a_uri['port'] : ($ssl ? 443 : 80);
 $host = ($ssl ? 'ssl://' : '') . $a_uri['host'];
 $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $lang;
 }

$data = file_get_contents('php://input');

$store = "";

// find me: html tunnel to googiespell

if(isset($CONFIG['googiespell_tunnel_host']) && $CONFIG['googiespell_tunnel_host'] != ""){

include_once("program/include/http_request.class.inc");
$request = new http_request('POST',$CONFIG['googiespell_tunnel_host'],$CONFIG['googiespell_tunnel_request']);
$request->add_data('host',$host);
$request->add_data('port',$port);
$request->add_data('path',$path);
$request->add_data('data',$data);
$request->send();
$temparr = explode("\r\n",$request->response);
$store = "";
for($i=1;$i if(isset($temparr[$i])){
$store .= $temparr[$i];
}
}
unset($request);
}
else{

if ($fp = fsockopen($host, $port, $errno, $errstr, 30))
  {
  $out = "POST $path HTTP/1.0\r\n";
  $out .= "Host: $host\r\n";
  $out .= "Content-Length: " . strlen($data) . "\r\n";
  $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  $out .= "Connection: Close\r\n\r\n";
  $out .= $data;
  fwrite($fp, $out);
 
  while (!feof($fp))
  $store .= fgets($fp, 128);
  fclose($fp);
  }
}

print $store;
exit;

?>

/program/include/http_request.class.inc

Code: [Select]

//Usage example:
//$request = new http_request('POST','macosbrain','/nvtracker/test.php');///method, host, file, port(optional), http_v(optional)
//$request->proxy('192.168.0.1',8080); //use a proxy server for request -> optional
//$request->add_header('Accept','text/plain');///add some header informations
//$request->add_header('User-Agent','Firefox/1.5.0.4');///add some header informations
//$request->add_header('Referer','[url]http://localhost'[/url]);///add some header informations

//$cookies = array(
// 'uid' => 3,
// 'pass' => 'testpass',
// 'PHPSESSID' => '123456789');
//$request->add_cookies($cookies); /// add the cookies to the request
//$request->add_data('disk','on'); // add "disk" with value "on"
//$request->add_data('test','123');// add "test" with value "123"
//$request->add_file('file_1',"test.jpg",$request->get_bin_file_content('C:\\small.JPG'));//add a real file, with fake name(like an upload)
//$request->add_file('file_2',"nice_dummy.txt",'content in the file');//add a custom "file" (like an upload)

//$request->send();///send the request
//echo('
---------packet send--------
');
//print_r($request->packet);
//echo('---------send_header---------
');
//print_r($request->send_header);
//echo('---------send_data---------
');
//print_r($request->send_data);
//echo('---------response from host---------
');
//print_r($request->response);echo('
');


class http_request
{
function __construct($type,$host,$file,$port = 80,$http_v = 'HTTP/1.1')
{
$this->host = $host;
$this->port = $port;
if(!$this->set_type($type))
die('sorry: please set a request type');
$this->file = $file;
$this->http_v = $http_v;
$this->add_header('Host',$host);
$this->add_header('Connection','Close');
}

function proxy($host,$port)
{
$this->con = @$this->connect($host,$port);
if(!$this->con)
die('sorry: the proxy on host "'.$host.'" did not response on port '.$port);
$this->proxy_con = true;
}

function connect($host,$port)
{
if(!is_numeric($port))
die('sorry: but this is not a port');
preg_match('@^(?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(?=\.?\d)\.)){4}$@i', $host, $ip);
if($ip[0]!='')
return fsockopen($host,$port);
else
return fsockopen(gethostbyname($host),$port);
}

function get_ascii_file_content($file)
{
$content = @file_get_contents($file);
if($content=='')
die('sorry: can not access file or file is empty');
return $content;
}

function get_bin_file_content($file)
{
$handle = @fopen($file, "rb");
if(!$handle)
die('sorry: can not access file');
$content = fread ($handle, filesize($file));
fclose ($handle);
return $content;
}

function send($set_ct = true)
{
if($set_ct==true)
$this->set_content_type();
if(!isset($this->proxy_con)){
$this->proxy_con = false;
}
if($this->proxy_con != true)
{
$this->con = @$this->connect($this->host,$this->port);
if(!$this->con)
die('sorry: "'.$this->host.'" did not response on port '.$this->port);
}

$this->send_data = '';
$contentlength = 0;

if($this->type=='GET')
{
$get_str='';
if(!isset($this->data)){
$this->data = false;
}
if($this->data)
{
foreach($this->data as $name => $value)
{
$get_str .= $name.'='.$value.'&';
}
}
$this->file.='?'.substr($get_str,0,-1);

}
else //// all other request types (POST, CONNECT ...)
{
if(!isset($this->boundary)){
$this->boundary = false;
}
if($this->boundary)
{
if($this->data)
{
foreach($this->data as $name => $value)
{
$contentlength += strlen("--".$this->boundary."\nContent-Disposition: form-data; name=\"".$name."\"\n\n".$value."\n");
$this->send_data.=("--".$this->boundary."\nContent-Disposition: form-data; name=\"".$name."\"\n\n".$value."\n");
}
}
$contentlength += strlen($this->boundary)+3;
$this->add_header('Content-length',$contentlength);
$this->send_data.= "--".$this->boundary."--\n";
}
else
{
if(is_array($this->data)){
foreach($this->data as $name => $value)
{
$this->send_data.= $name.'='.$value.'&';
}
$this->send_data = substr($this->send_data,0,-1);
$contentlength = strlen($this->send_data);
$this->add_header('Content-length',$contentlength);
}
}
}
if($this->proxy_con==true)
$this->send_header = $this->type.' '.'http://'.$this->host.$this->file.' '.$this->http_v."\r\n";
else
$this->send_header = $this->type.' '.$this->file.' '.$this->http_v."\r\n";
foreach($this->header as $head_name => $head_value)
$this->send_header .= $head_name.': '.$head_value."\r\n";
$this->send_header .= "\n";
$this->packet = $this->send_header.$this->send_data;
$this->send_to_host($this->packet);
}

function send_to_host($packet)
{
if(get_resource_type($this->con)!='stream')
die('sorry: no connection');
fputs($this->con,$packet);
if($this->proxy_con==true)
{
 $this->response='';
 while (!feof($this->con)) {
  $this->response.=fgets($this->con);
 }
}
else
{
 $this->response='';
 while ((!feof($this->con)) or (!eregi(chr(0x0d).chr(0x0a).chr(0x0d).chr(0x0a),$this->response))) {
  $this->response.=fread($this->con,1);
 }
}
fclose($this->con);
}

function set_content_type()
{
if(!isset($this->files)){
$this->files = 0;
}
if($this->files > 0)
{
$this->boundary = '---------------------------'.rand(1,9999999999);
$this->add_header('Content-Type','multipart/form-data; boundary='.$this->boundary);
}
elseif($this->type!='GET')
$this->add_header('Content-Type','application/x-www-form-urlencoded');
}

function add_cookies($cookies)
{
if(!is_array($cookies))
die('sorry: we need an array for the cookies');
$cookie_str = "";
foreach($cookies as $cookie_name => $cookie_value){
$cookie_str .= $cookie_name.'='.$cookie_value.'; ';
}
$cookie_str = substr($cookie_str,0,-2);
$this->add_header('Cookie',$cookie_str);
}

function set_type($name)
{
$name = strtoupper($name);
$types = array('GET','HEAD','POST','PUT','DELETE','TRACE','CONNECT');
if( in_array($name,$types) )
{
$this->type = $name;
return true;
}
else
{
return false;
}
}

function add_data($name,$value)
{
$this->data[$name] = $value;
}

function add_file($name,$filename,$content)
{
if($this->type=='GET')
die('sorry: please do not use file uploads in get requests');
$this->files ++;
$this->add_data($name.'"; filename="'.$filename,$content);
}

function add_header($name,$value)
{
$this->header[$name]=$value;
}
}

?>

I'll implement it into TinyMCE SpellChecker soon ...

-Roland
Regards,
Rosali
__________________
MyRoundcube Project (commercial)