summaryrefslogtreecommitdiff
path: root/program/include
diff options
context:
space:
mode:
Diffstat (limited to 'program/include')
-rw-r--r--program/include/rcube_imap_generic.php57
-rw-r--r--program/include/rcube_session.php23
2 files changed, 42 insertions, 38 deletions
diff --git a/program/include/rcube_imap_generic.php b/program/include/rcube_imap_generic.php
index f3855892a..29159c734 100644
--- a/program/include/rcube_imap_generic.php
+++ b/program/include/rcube_imap_generic.php
@@ -213,31 +213,26 @@ class rcube_imap_generic
{
$line = '';
- if (!$this->fp) {
- return NULL;
- }
-
if (!$size) {
$size = 1024;
}
do {
- if (feof($this->fp)) {
+ if ($this->eof()) {
return $line ? $line : NULL;
}
$buffer = fgets($this->fp, $size);
if ($buffer === false) {
- @fclose($this->fp);
- $this->fp = null;
+ $this->closeSocket();
break;
}
if ($this->_debug) {
$this->debug('S: '. rtrim($buffer));
}
$line .= $buffer;
- } while ($buffer[strlen($buffer)-1] != "\n");
+ } while (substr($buffer, -1) != "\n");
return $line;
}
@@ -267,7 +262,7 @@ class rcube_imap_generic
{
$data = '';
$len = 0;
- while ($len < $bytes && !feof($this->fp))
+ while ($len < $bytes && !$this->eof())
{
$d = fread($this->fp, $bytes-$len);
if ($this->_debug) {
@@ -312,8 +307,7 @@ class rcube_imap_generic
} else if ($res == 'BAD') {
$this->errornum = self::ERROR_BAD;
} else if ($res == 'BYE') {
- @fclose($this->fp);
- $this->fp = null;
+ $this->closeSocket();
$this->errornum = self::ERROR_BYE;
}
@@ -339,6 +333,32 @@ class rcube_imap_generic
return self::ERROR_UNKNOWN;
}
+ private function eof()
+ {
+ if (!is_resource($this->fp)) {
+ return true;
+ }
+
+ // If a connection opened by fsockopen() wasn't closed
+ // by the server, feof() will hang.
+ $start = microtime(true);
+
+ if (feof($this->fp) ||
+ ($this->prefs['timeout'] && (microtime(true) - $start > $this->prefs['timeout']))
+ ) {
+ $this->closeSocket();
+ return true;
+ }
+
+ return false;
+ }
+
+ private function closeSocket()
+ {
+ @fclose($this->fp);
+ $this->fp = null;
+ }
+
function setError($code, $msg='')
{
$this->errornum = $code;
@@ -360,8 +380,7 @@ class rcube_imap_generic
}
if ($error && preg_match('/^\* (BYE|BAD) /i', $string, $m)) {
if (strtoupper($m[1]) == 'BYE') {
- @fclose($this->fp);
- $this->fp = null;
+ $this->closeSocket();
}
return true;
}
@@ -701,11 +720,12 @@ class rcube_imap_generic
$host = $this->prefs['ssl_mode'] . '://' . $host;
}
+ if ($this->prefs['timeout'] <= 0) {
+ $this->prefs['timeout'] = ini_get('default_socket_timeout');
+ }
+
// Connect
- if ($this->prefs['timeout'] > 0)
- $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
- else
- $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr);
+ $this->fp = @fsockopen($host, $this->prefs['port'], $errno, $errstr, $this->prefs['timeout']);
if (!$this->fp) {
$this->setError(self::ERROR_BAD, sprintf("Could not connect to %s:%d: %s", $host, $this->prefs['port'], $errstr));
@@ -855,8 +875,7 @@ class rcube_imap_generic
$this->readReply();
}
- @fclose($this->fp);
- $this->fp = false;
+ $this->closeSocket();
}
/**
diff --git a/program/include/rcube_session.php b/program/include/rcube_session.php
index 59ce42379..0bae4a711 100644
--- a/program/include/rcube_session.php
+++ b/program/include/rcube_session.php
@@ -183,27 +183,12 @@ class rcube_session
}
- public function regenerate_id()
+ public function regenerate_id($destroy=true)
{
- $randval = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-
- for ($random = '', $i=1; $i <= 32; $i++) {
- $random .= substr($randval, mt_rand(0,(strlen($randval) - 1)), 1);
- }
-
- // use md5 value for id or remove capitals from string $randval
- $random = md5($random);
-
- // delete old session record
- $this->destroy(session_id());
-
- session_id($random);
-
- $cookie = session_get_cookie_params();
- $lifetime = $cookie['lifetime'] ? time() + $cookie['lifetime'] : 0;
-
- rcmail::setcookie(session_name(), $random, $lifetime);
+ session_regenerate_id($destroy);
+ $this->vars = false;
+ $this->key = session_id();
return true;
}