+function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
+ global $color, $squirrelmail_language;
+ $read = '';
+ $pre_a = explode(' ',trim($pre));
+ $pre = $pre_a[0];
+ $resultlist = array();
+ $data = array();
+ $read = sqimap_fgets($imap_stream);
+ $i = 0;
+ 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;
+ }
+ }
+ if (!empty($data)) {
+ $resultlist[] = $data;
+ }
+ elseif (empty($resultlist)) {
+ $resultlist[] = array();
+ }