- while (1) {
- $char = $read{0};
- switch ($char) {
- case $pre{0}:
- /* get the command */
- $arg = '';
- $i = strlen($pre)+1;
- $s = substr($read,$i);
- if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) {
- $arg = substr($s,0,$j);
- }
- $tag = substr($read,0,$i-1);
- if ($arg && $tag==$pre) {
- switch ($arg) {
- case 'OK':
- case 'BAD':
- case 'NO':
- case 'BYE':
- case 'PREAUTH':
- $response = $arg;
- $message = trim(substr($read,$i+strlen($arg)));
- break 3;
- default:
- /* this shouldn't happen */
- $response = $arg;
- $message = trim(substr($read,$i+strlen($arg)));
- break 3;
- }
- } elseif($tag !== $pre) {
- /* reset data array because we do not need this reponse */
- $data = array();
- $read = sqimap_fgets($imap_stream);
- break;
- }
- case '*':
- if (preg_match('/^\*\s\d+\sFETCH/',$read)) {
- /* check for literal */
- $s = substr($read,-3);
- $fetch_data = array();
- do { /* outer loop, continue until next untagged fetch
- or tagged reponse */
- do { /* innerloop for fetching literals. with this loop
- we prohibid that literal responses appear in the
- outer loop so we can trust the untagged and
- tagged info provided by $read */
- if ($s === "}\r\n") {
- $j = strrpos($read,'{');
- $iLit = substr($read,$j+1,-3);
- $fetch_data[] = $read;
- $sLiteral = fread($imap_stream,$iLit);
- /* backwards compattibility */
- $aLiteral = explode("\n", $sLiteral);
- /* release not neaded data */
- unset($sLiteral);
- foreach ($aLiteral as $line) {
- $fetch_data[] = $line ."\n";
- }
- /* release not neaded data */
- unset($aLiteral);
- /* next fgets belongs to this fetch because
- we just got teh exact literalsize and there
- must follow data to complete the response */
- $fetch_data[] = sqimap_fgets($imap_stream);
- } else {
- $fetch_data[] = $read;
- }
- /* retrieve next line and check in the while
- statements if it belongs to this fetch response */
- $read = sqimap_fgets($imap_stream);
- /* check for next untagged reponse and break */
- if ($read{0} == '*') break 2;
- $s = substr($read,-3);
- } while ($s === "}\r\n");
- $s = substr($read,-3);
- } while ($read{0} !== '*' &&
- substr($read,0,strlen($pre)) !== $pre);
- $resultlist[] = $fetch_data;
- /* release not neaded data */
- unset ($fetch_data);
- } else {
- $s = substr($read,-3);
- do {
- if ($s === "}\r\n") {
- $j = strrpos($read,'{');
- $iLit = substr($read,$j+1,-3);
- $data[] = $read;
- $data[] = fread($imap_stream,$iLit);
- $fetch_data[] = sqimap_fgets($imap_stream);
- } else {
- $data[] = $read;
- }
- $read = sqimap_fgets($imap_stream);
- if ($read{0} == '*') break;
- $s = substr($read,-3);
- } while ($s === "}\r\n");
- break 1;
- }
- break;
- case '+':
- $read = sqimap_fgets($imap_stream);
- break;
- default:
- $read = sqimap_fgets($imap_stream);
- break;
- }
+ while ($read) {
+ $char = $read{0};
+ switch ($char)
+ {
+ case '+':
+ default:
+ $read = sqimap_fgets($imap_stream);
+ break;
+
+ case $tag{0}:
+ {
+ /* get the command */
+ $arg = '';
+ $i = strlen($tag)+1;
+ $s = substr($read,$i);
+ if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) {
+ $arg = substr($s,0,$j);
+ }
+ $found_tag = substr($read,0,$i-1);
+ if ($found_tag) {
+ switch ($arg)
+ {
+ case 'OK':
+ case 'BAD':
+ case 'NO':
+ case 'BYE':
+ case 'PREAUTH':
+ $response[$found_tag] = $arg;
+ $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
+ if (!empty($data)) {
+ $resultlist[] = $data;
+ }
+ $aResponse[$found_tag] = $resultlist;
+ $data = $resultlist = array();
+ if ($found_tag == $tag) {
+ break 3; /* switch switch while */
+ }
+ break;
+ default:
+ /* this shouldn't happen */
+ $response[$found_tag] = $arg;
+ $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
+ if (!empty($data)) {
+ $resultlist[] = $data;
+ }
+ $aResponse[$found_tag] = $resultlist;
+ $data = $resultlist = array();
+ if ($found_tag == $tag) {
+ break 3; /* switch switch while */
+ }
+ }
+ }
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) { /* error */
+ break 2; /* switch while */
+ }
+ break;
+ } // end case $tag{0}
+
+ case '*':
+ {
+ if (($sCommand == "FETCH" || $sCommand == "STORE") && preg_match('/^\*\s\d+\sFETCH/',$read)) {
+ /* check for literal */
+ $s = substr($read,-3);
+ $fetch_data = array();
+ do { /* outer loop, continue until next untagged fetch
+ or tagged reponse */
+ do { /* innerloop for fetching literals. with this loop
+ we prohibid that literal responses appear in the
+ outer loop so we can trust the untagged and
+ tagged info provided by $read */
+ if ($s === "}\r\n") {
+ $j = strrpos($read,'{');
+ $iLit = substr($read,$j+1,-3);
+ $fetch_data[] = $read;
+ $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return);
+ if ($sLiteral === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ /* backwards compattibility */
+ $aLiteral = explode("\n", $sLiteral);
+ /* release not neaded data */
+ unset($sLiteral);
+ foreach ($aLiteral as $line) {
+ $fetch_data[] = $line ."\n";
+ }
+ /* release not neaded data */
+ unset($aLiteral);
+ /* next fgets belongs to this fetch because
+ we just got the exact literalsize and there
+ must follow data to complete the response */
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ $fetch_data[] = $read;
+ } else {
+ $fetch_data[] = $read;
+ }
+ /* retrieve next line and check in the while
+ statements if it belongs to this fetch response */
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) { /* error */
+ break 4; /* while while switch while */
+ }
+ /* check for next untagged reponse and break */
+ if ($read{0} == '*') break 2;
+ $s = substr($read,-3);
+ } while ($s === "}\r\n");
+ $s = substr($read,-3);
+ } while ($read{0} !== '*' &&
+ substr($read,0,strlen($tag)) !== $tag);
+ $resultlist[] = $fetch_data;
+ /* release not neaded data */
+ unset ($fetch_data);
+ } else {
+ $s = substr($read,-3);
+ do {
+ if ($s === "}\r\n") {
+ $j = strrpos($read,'{');
+ $iLit = substr($read,$j+1,-3);
+ // check for numeric value to avoid that untagged responses like:
+ // * OK [PARSE] Unexpected characters at end of address: {SET:debug=51}
+ // will trigger literal fetching ({SET:debug=51} !== int )
+ if (is_numeric($iLit)) {
+ $data[] = $read;
+ $sLiteral = fread($imap_stream,$iLit);
+ if ($sLiteral === false) { /* error */
+ $read = false;
+ break 3; /* while switch while */
+ }
+ $data[] = $sLiteral;
+ $data[] = sqimap_fgets($imap_stream);
+ } else {
+ $data[] = $read;
+ }
+ } else {
+ $data[] = $read;
+ }
+ $read = sqimap_fgets($imap_stream);
+ if ($read === false) {
+ break 3; /* while switch while */
+ } else if ($read{0} == '*') {
+ break;
+ }
+ $s = substr($read,-3);
+ } while ($s === "}\r\n");
+ break 1;
+ }
+ break;
+ } // end case '*'
+ } // end switch
+ } // end while
+
+ /* error processing in case $read is false */
+ if ($read === false) {
+ // try to retrieve an untagged bye respons from the results
+ $sResponse = array_pop($data);
+ if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) {
+ if (!$handle_errors) {
+ $query = '';
+ }
+ sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:"),$sResponse);
+ echo '</body></html>';
+ exit;
+ } else if ($handle_errors) {
+ unset($data);
+ sqimap_error_box(_("ERROR: Connection dropped by IMAP server."), $query);
+ exit;
+ }