I tried use this code: http://blog.philippheckel.com/2008/05/16/roundcube-login-via-php-script/
But is outdated (not work in roundcube 1.0.1).
I tried
recreate code, in localhost work fine, but in my SERVER not work, returns this error:
> Your session is invalid or expired.
See my code (is a problem with cookies roundcube):
<?php
define('EOL', chr(10));
define('ROUNDCUBE_HOST', 'www.website.com.br');
define('ROUNDCUBE_FOLDER', 'folder-roundcube-in-server');
define('ROUNDCUBE_LOGIN', '[email protected]');
define('ROUNDCUBE_PASS', 'password');
header('Cache-Control: private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
function getToken($d, $i) {
$d = substr($d, $i);
$posToken = stripos($d, 'value="');
$d = trim(substr($d, $posToken + 7));
$posToken = stripos($d, '"');
$d = trim(substr($d, 0, $posToken));
return $d;
}
function getConn($method, $host, $path, $headers, $port) {
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if ($fp === false) {
return array(
'status' => string($errno),
'err' => $errstr . '(' . $errno .')'
);
} else {
$out = $method . ' ' . $path . ' HTTP/1.1' . EOL;
$out .= $headers;
fwrite($fp, $out);
$cookies = array();
$first = true;
$isContent = false;
$content = '';
while(feof($fp) === false) {
$data = fgets($fp, 128);
if($first === true){
$first = false;
$data = trim(preg_replace('#HTTP[/]1[.]\d #', '', $data));
$status = trim(substr($data, 0, strpos($data, ' ')));
} else if(stripos($data, 'set-cookie:') === 0) {
$cookies[] = trim(preg_replace('#^set[-]cookie[:]#i', '', substr($data, 0, strpos($data, ';'))));
} else if($isContent === false) {
$isContent = trim($data) === '';
} else {
$content .= $data;
}
}
fclose($fp);
return array(
'status' => $status,
'cookies' => $cookies,
'content' => $content
);
}
}
$header = 'Host: ' . ROUNDCUBE_HOST . EOL;
$header .= 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . EOL;
$header .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . EOL;
$header .= 'Accept-Language: pt-br,en-us;q=0.7,en;q=0.3' . EOL;
$header .= 'Connection: close' . EOL . EOL;
$data = getConn('GET', ROUNDCUBE_HOST, '/' . ROUNDCUBE_FOLDER . '/?_task=login', $header, 80);
echo str_replace(EOL, '<br>', $header);
var_dump($data);
echo '<hr>';
$posToken = strpos($data['content'] ,'="_token"');
if($posToken === false) {
echo 'token not found';
} else if ($data['status'] === '200') {
$data['content'] = getToken($data['content'], $posToken + 9);
$postdata = http_build_query(
array(
'_token' => $data['content'],
'_url' => '',
'_task' => 'login',
'_action' => 'login',
'_timezone' => '__default__',
'_user' => ROUNDCUBE_LOGIN,
'_pass' => ROUNDCUBE_PASS
)
);
$header = 'Host: ' . ROUNDCUBE_HOST . EOL;
$header .= 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'] . EOL;
$header .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/'.'*;q=0.8' . EOL;
$header .= 'Accept-Language: pt-br,en-us;q=0.7,en;q=0.3' . EOL;
$header .= 'Referer: http://' . ROUNDCUBE_HOST . '/' . ROUNDCUBE_FOLDER . '/?_task=login' . EOL;
if(count($data['cookies']) > 0) {
$header .= 'Cookie: ' . implode('; ', $data['cookies']) . EOL;
}
$header .= 'Connection: close' . EOL;
$header .= 'Content-Type: application/x-www-form-urlencoded' . EOL;
$header .= 'Content-Length: ' . strlen($postdata) . EOL . EOL;
$header .= $postdata;
$data = getConn('POST', ROUNDCUBE_HOST, '/' . ROUNDCUBE_FOLDER . '/?_task=login', $header, 80);
echo str_replace(EOL, '<br>', $header);
var_dump($data);
if($data['status'] === '0') { //0 = Not connected
echo 'error_send_post(socket): ', $data['err'];
} else if($data['status'] !== '302') { //If HTTP <> 302 has an issue
echo 'error_send_post_http_' . $data['status'];
} else {// If 302 (Moved page) login ok
$j = count($data['cookies']);
for($i = 0; $i < $j; $i++) {
header('Set-Cookie: ' . $data['cookies'][$i] . '; path=/; httpOnly', false);
}
header('X-DNS-Prefetch-Control: off');
header('Vary: Accept-Encoding');
//header('Location: http://' . ROUNDCUBE_HOST . '/' . ROUNDCUBE_FOLDER . '/?_task=mail');
echo '<a href="http://' . ROUNDCUBE_HOST . '/' . ROUNDCUBE_FOLDER . '/?_task=mail">Access email</a>';
}
} else if($data['status'] === '0') {
echo 'error_get_token(socket): ', $data['err'];
} else {
echo 'error_get_token_http_' . $data['status'];
}