35 if ($response ==
'OK') {
92 if( !is_array( $messages_array ) ) {
93 return $messages_array;
96 sort($messages_array, SORT_NUMERIC);
98 while ($messages_array) {
99 $start = array_shift($messages_array);
101 while (isset($messages_array[0]) && $messages_array[0] ==
$end + 1) {
102 $end = array_shift($messages_array);
104 if ($msgs_str !=
'') {
108 if ($start !=
$end) {
109 $msgs_str .=
':' .
$end;
121 $responses = array ();
125 if (!preg_match(
"/^\* ([0-9]+) FETCH/i", $responses[0][0], $regs)) {
126 $responses = array ();
147 $server_sort_array = array();
148 $sort_test = array();
155 if (isset($mbxresponse[
'UIDNEXT']) && $mbxresponse[
'UIDNEXT']) {
156 $uidnext = $mbxresponse[
'UIDNEXT']-1;
160 $query =
"SEARCH UID 1:$uidnext";
162 if (isset($uids[0])) {
163 for ($i=0,$iCnt=count($uids);$i<$iCnt;++$i) {
164 for (
$j = 0, $jCnt=count($uids[$i]);
$j<$jCnt;++
$j) {
165 if (preg_match(
"/^\* SEARCH (.+)$/", $uids[$i][
$j], $regs)) {
166 $server_sort_array += preg_split(
"/ /", trim($regs[1]));
171 if (!preg_match(
"/OK/", $response)) {
172 $server_sort_array =
'no';
175 $qty = $mbxresponse[
'EXISTS'];
176 $server_sort_array = range(1, $qty);
178 $server_sort_array = array_reverse($server_sort_array);
180 return $server_sort_array;
183 $sort_on = array (0=>
'DATE',
192 $sort_on[0] =
'ARRIVAL';
193 $sort_on[1] =
'ARRIVAL';
199 if (!empty($sort_on[
$sort])) {
203 if (isset($sort_test[0])) {
204 for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) {
205 for (
$j = 0, $jCnt=count($sort_test[$i]);
$j<$jCnt;++
$j) {
206 if (preg_match(
"/^\* SORT (.+)$/", $sort_test[$i][
$j], $regs)) {
207 $server_sort_array += preg_split(
"/ /", trim($regs[1]));
214 $server_sort_array = array_reverse($server_sort_array);
216 if (!preg_match(
"/OK/", $response)) {
217 $server_sort_array =
'no';
220 return $server_sort_array;
239 $php_sort_array = array();
242 if (isset($mbxresponse[
'UIDNEXT']) && $mbxresponse[
'UIDNEXT']) {
243 $uidnext = $mbxresponse[
'UIDNEXT']-1;
247 $query =
"SEARCH UID 1:$uidnext";
249 if (isset($uids[0])) {
250 $php_sort_array = array();
252 foreach($uids as $line) {
253 if (preg_match(
"/^\* SEARCH (.+)$/", $line, $regs)) {
254 $php_sort_array += preg_split(
"/ /", trim($regs[1]));
258 if (!preg_match(
"/OK/", $response)) {
259 $php_sort_array =
'no';
262 $qty = $mbxresponse[
'EXISTS'];
263 $php_sort_array = range(1, $qty);
266 return $php_sort_array;
284 $indent_array = array();
286 $thread_new = array();
292 $thread_count = count($thread_new);
293 for ($i = 0; $i < $thread_count; $i++) {
294 $thread_new[$i] = preg_replace(
"/\s\(/",
"(", $thread_new[$i]);
295 $thread_new[$i] = preg_replace(
"/(\d+)/",
"$1|", $thread_new[$i]);
296 $thread_new[$i] = preg_split(
"/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY);
300 for ($i = 0; $i < $thread_count; $i++) {
303 if (isset($thread_new[$i][0])) {
304 if (preg_match(
"/(\d+)/", $thread_new[$i][0], $regs)) {
320 $thread_length = count($thread_new[$i]);
321 for ($k=1;$k<$thread_length-1;$k++) {
322 $chars = count_chars($thread_new[$i][$k], 1);
323 if (isset($chars[
'40'])) {
324 $level += $chars[
'40'];
326 if (isset($chars[
'41'])) {
327 $level -= $chars[
'41'];
332 if ($level == $cutoff) {
336 if (isset($chars[
'32'])) {
337 if (!isset($spaces[$level])) {
340 $spaces[$level] += $chars[
'32'];
342 for ($x=0;$x<=$level;$x++) {
343 if (isset($spaces[$x])) {
344 $spaces_total += $spaces[$x];
347 $indent = $level + $spaces_total;
352 $indent = $indent +1;
354 if (preg_match(
"/(\d+)/", $thread_new[$i][$k], $regs)) {
368 $indent_array[$child] = abs($indent);
372 return $indent_array;
388 $thread_temp = array ();
390 $sort_type =
'REFERENCES';
392 $sort_type =
'ORDEREDSUBJECT';
397 if (isset($thread_test[0])) {
398 for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) {
399 if (preg_match(
"/^\* THREAD (.+)$/", $thread_test[$i], $regs)) {
400 $thread_list = trim($regs[1]);
407 if (!preg_match(
"/OK/", $response)) {
408 $server_sort_array =
'no';
409 return $server_sort_array;
411 if (isset($thread_list)) {
412 $thread_temp = preg_split(
"//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
415 $char_count = count($thread_temp);
417 $thread_new = array();
420 for ($i=0;$i<$char_count;$i++) {
421 if ($thread_temp[$i] !=
')' && $thread_temp[$i] !=
'(') {
422 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
423 }
elseif ($thread_temp[$i] ==
'(') {
424 $thread_new[$k] .= $thread_temp[$i];
426 }
elseif ($thread_temp[$i] ==
')') {
428 $thread_new[$k] .= $thread_temp[$i];
429 $counter = $counter - 1;
431 $thread_new[$k] .= $thread_temp[$i];
433 $thread_new[$k] =
"";
434 $counter = $counter - 1;
440 $thread_new = array_reverse($thread_new);
441 $thread_list = implode(
" ", $thread_new);
442 $thread_list = str_replace(
"(",
" ", $thread_list);
443 $thread_list = str_replace(
")",
" ", $thread_list);
444 $thread_list = preg_split(
"/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
445 $server_sort_array = $thread_list;
453 $stop = gettimeofday();
454 $timepassed = 1000000 * ($stop[
'sec'] - $start[
'sec']) + $stop[
'usec'] - $start[
'usec'];
473 $iPos = strpos(
$read,
'"',$iPos);
475 if ($iPos &&
$read{$iPos -1} !=
'\\') {
476 $s = substr(
$read,$i,($iPos-$i));
481 if ($iPos > strlen(
$read)) {
485 }
else if ($char ==
'{') {
488 $iPos = strpos(
$read,
'}',$i);
490 $lit_cnt = substr(
$read, $i, $iPos - $i);
491 $i += strlen($lit_cnt) + 3;
493 $s = ($lit_cnt ? substr(
$read,$i,$lit_cnt):
'');
518 $i = strpos(
$read,
'(',$i);
519 $i_pos = strpos(
$read,
')',$i);
520 $s = substr(
$read,$i+1,$i_pos - $i -1);
521 $a = explode(
' ',$s);
540 for(
$j=0, $iCnt = count($aResponse);
$j<$iCnt; ++
$j) {
543 $read = implode(
'', $aResponse[
$j]);
545 unset($aResponse[
$j]);
551 $i_space = strpos(
$read,
' ', 2);
552 $id = substr(
$read, 2, $i_space - 2);
554 $fetch = substr(
$read, $i_space+1,5);
556 if (!is_numeric(
$id) && $fetch !==
'FETCH') {
557 $aMsg[
'ERROR'] =
$read;
561 $i = strpos(
$read,
'(', $i_space+5);
563 $i_len = strlen(
$read);
566 while($i < $i_len && $i !==
false) {
568 $i_len = strlen(
$read);
569 $i = strpos(
$read,
' ');
570 $arg = substr(
$read,0,$i);
575 $i_pos = strpos(
$read,
' ',$i);
577 $i_pos = strpos(
$read,
')', $i);
580 $unique_id = substr(
$read, $i, $i_pos-$i);
588 if (!$flags)
break 3;
590 foreach($flags as $flag) {
591 $flag = strtolower($flag);
592 $aFlags[$flag] =
true;
594 $aMsg[
'FLAGS'] = $aFlags;
597 $i_pos = strpos(
$read,
' ', $i);
599 $i_pos = strpos(
$read,
')', $i);
602 $aMsg[
'SIZE'] = substr(
$read,$i,$i_pos-$i);
611 case 'BODYSTRUCTURE':
616 case 'BODY.PEEK[HEADER.FIELDS':
617 case 'BODY[HEADER.FIELDS':
618 $i = strpos(
$read,
'{',$i);
620 if (
$header ===
false)
break 2;
622 $hdr = trim(str_replace(array(
"\r\n",
"\n\t",
"\n "),array(
"\n",
' ',
' '),
$header));
625 $aHdr = explode(
"\n" , $hdr);
626 $aReceived = array();
627 foreach ($aHdr as $line) {
628 $pos = strpos($line,
':');
630 $field = strtolower(substr($line, 0, $pos));
631 if (!strstr($field,
' ')) {
632 $value = trim(substr($line, $pos+1));
635 $aMsg[
'date'] = trim(str_replace(
' ',
' ', $value));
637 case 'x-priority': $aMsg[
'x-priority'] = ($value) ? (
int) $value{0} : 3;
break;
641 if (!isset($aMsg[
'x-priority'])) {
642 $aPrio = preg_split(
'/\s/',trim($value));
643 $sPrio = strtolower(array_shift($aPrio));
644 if (is_numeric($sPrio)) {
645 $iPrio = (int) $sPrio;
646 }
elseif ( $sPrio ==
'non-urgent' || $sPrio ==
'low' ) {
648 }
elseif ( $sPrio ==
'urgent' || $sPrio ==
'high' ) {
654 $aMsg[
'x-priority'] = $iPrio;
659 if ($pos = strpos($type,
";")) {
660 $type = substr($type, 0, $pos);
662 $type = explode(
"/", $type);
663 if(!is_array($type) || count($type) < 2) {
664 $aMsg[
'content-type'] = array(
'text',
'plain');
666 $aMsg[
'content-type'] = array(strtolower($type[0]),strtolower($type[1]));
670 $aMsg[
'received'][] = $value;
673 $aMsg[$field] = $value;
685 if (!empty($unique_id)) {
686 $msgi =
"$unique_id";
687 $aMsg[
'UID'] = $unique_id;
691 $aMessageList[$msgi] = $aMsg;
694 return $aMessageList;
709 $aValue=preg_split(
'/\s/',trim($sValue));
710 $value = strtolower(array_shift($aValue));
711 if ( is_numeric($value) ) {
714 if ( $value ==
'urgent' || $value ==
'high' ) {
716 }
elseif ( $value ==
'non-urgent' || $value ==
'low' ) {
730 $maxmsg =
sizeof($msg_list);
737 $read_list = array();
744 for ($i = 0; $i <
sizeof($msg_list); $i++) {
745 $messages[
"$msg_list[$i]"] = array();
750 $query =
"FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Importance Priority Content-Type)])";
752 $query =
"FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Importance Priority Content-Type)])";
757 foreach ($read_list as
$r) {
760 $from =
_(
"Unknown Sender");
763 $type = array(
'',
'');
764 $cc =
$to = $inrepto =
'';
769 unset($internal_date);
771 $flag_seen = $flag_answered = $flag_deleted = $flag_flagged =
false;
780 $i_space = strpos(
$read,
' ',2);
782 $fetch = substr(
$read,$i_space+1,5);
783 if (!is_numeric(
$id) && $fetch !==
'FETCH') {
785 echo
'<br /><b><font color="'.$color[2].
'">' .
786 _(
"ERROR: Could not complete request.") .
788 _(
"Unknown response from IMAP server:") .
' 1.' .
789 htmlspecialchars(
$read) .
"</font><br />\n";
792 $i = strpos(
$read,
'(',$i_space+5);
794 $i_len = strlen(
$read);
796 while ($i < $i_len && $i !==
false) {
799 $i_len = strlen(
$read);
800 $i = strpos(
$read,
' ');
801 $arg = substr(
$read,0,$i);
806 $i_pos = strpos(
$read,
' ',$i);
808 $i_pos = strpos(
$read,
')',$i);
811 $unique_id = substr(
$read,$i,$i_pos-$i);
819 if (!$flags)
break 3;
820 foreach ($flags as $flag) {
821 $flag = strtolower($flag);
824 case '\\seen': $flag_seen =
true;
break;
825 case '\\answered': $flag_answered =
true;
break;
826 case '\\deleted': $flag_deleted =
true;
break;
827 case '\\flagged': $flag_flagged =
true;
break;
833 $i_pos = strpos(
$read,
' ',$i);
835 $i_pos = strpos(
$read,
')',$i);
838 $size = substr(
$read,$i,$i_pos-$i);
853 case 'BODY.PEEK[HEADER.FIELDS':
854 case 'BODY[HEADER.FIELDS':
855 $i = strpos(
$read,
'{',$i);
857 if (
$header ===
false)
break 2;
859 $hdr = trim(str_replace(array(
"\r\n",
"\n\t",
"\n "),array(
"\n",
' ',
' '),
$header));
862 $hdr = explode(
"\n" , $hdr);
863 foreach ($hdr as $line) {
864 $pos = strpos($line,
':');
866 $field = strtolower(substr($line, 0, $pos));
867 if (!strstr($field,
' ')) {
868 $value = trim(substr($line, $pos+1));
871 case 'to':
$to = $value;
break;
872 case 'cc':
$cc = $value;
break;
873 case 'from':
$from = $value;
break;
874 case 'date':
$date = $value;
break;
886 $type = strtolower($value);
887 if ($pos = strpos($type,
";")) {
888 $type = substr($type, 0, $pos);
890 $type = explode(
"/", $type);
891 if ( empty($type[0]) ) {
896 if ( empty($type[1]) ) {
911 if (isset(
$date) || isset($internal_date)) {
912 if (isset($internal_date)) {
913 $internal_date = str_replace(
' ',
' ', $internal_date);
914 $tmpinternal_date = explode(
' ', trim($internal_date));
916 $date = $internal_date;
917 $tmpdate = $tmpinternal_date;
922 $tmpdate = explode(
' ', trim(
$date));
923 if (!isset($internal_date)) {
924 $internal_date =
$date;
925 $tmpinternal_date = $tmpdate;
929 $internal_date = $tmpinternal_date = $tmpdate =
$date = array();
933 $messages[$msgi][
'ID'] = $unique_id;
936 $messages[$msgi][
'ID'] =
$id;
938 $messages[$msgi][
'RECEIVED_TIME_STAMP'] =
getTimeStamp($tmpinternal_date);
939 $messages[$msgi][
'RECEIVED_DATE_STRING'] =
getDateString($messages[$msgi][
'RECEIVED_TIME_STAMP']);
940 $messages[$msgi][
'TIME_STAMP'] =
getTimeStamp($tmpdate);
941 $messages[$msgi][
'DATE_STRING'] =
getDateString($messages[$msgi][
'TIME_STAMP']);
942 $messages[$msgi][
'FROM'] =
$from;
943 $messages[$msgi][
'SUBJECT'] =
$subject;
945 $messages[$msgi][
'TO'] =
$to;
947 $messages[$msgi][
'PRIORITY'] = $priority;
948 $messages[$msgi][
'CC'] =
$cc;
949 $messages[$msgi][
'SIZE'] = $size;
950 $messages[$msgi][
'TYPE0'] = $type[0];
951 $messages[$msgi][
'TYPE1'] = $type[1];
952 $messages[$msgi][
'FLAG_DELETED'] = $flag_deleted;
953 $messages[$msgi][
'FLAG_ANSWERED'] = $flag_answered;
954 $messages[$msgi][
'FLAG_SEEN'] = $flag_seen;
955 $messages[$msgi][
'FLAG_FLAGGED'] = $flag_flagged;
965 $messages[$msgi][
'FROM-SORT'] =
$from;
967 if (preg_match(
"/^(?:(?:vedr|sv|re|aw|fw|fwd|\[\w\]):\s*)*\s*(.*)$/si", $subject_sort, $matches)){
968 $messages[$msgi][
'SUBJECT-SORT'] = $matches[1];
970 $messages[$msgi][
'SUBJECT-SORT'] = $subject_sort;
979 $messages[$msgi][
'TO-SORT'] =
$to;
983 array_reverse($messages);
984 $new_messages = array();
985 foreach ($messages as $i =>
$message) {
988 return $new_messages;
1000 $read_list = array();
1002 $query =
"FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])";
1006 foreach ($readin_list as
$r) {
1007 $r = implode(
'',
$r);
1009 $r = str_replace(array(
"\r\n",
"\n\t",
"\n\s"),array(
"\n",
'',
''),
$r);
1014 $r = explode(
"\n" ,
$r);
1016 if (!preg_match(
"/^\\*\s+([0-9]+)\s+FETCH/iAU",
$r[0], $regs)) {
1018 echo
'<br /><b><font color="'.$color[2].
'">' .
1019 _(
"ERROR: Could not complete request.") .
1021 _(
"Unknown response from IMAP server:") .
' 1.' .
1022 $r[0] .
"</font><br />\n";
1027 if (!preg_match(
"/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",
$r[0], $regs)) {
1029 echo
'<br /><b><font color="'.$color[2].
'">' .
1030 _(
"ERROR: Could not complete request.") .
1032 _(
"Unknown response from IMAP server:") .
' 1.' .
1033 $r[0] .
"</font><br />\n";
1039 $field = substr($field,strlen($field)+2);
1040 $result[] = array(
$id,$field);
1058 if (preg_match(
'/.+FLAGS\s\((.*)\)\s/AUi',
$read[0],$regs)) {
1059 if (trim($regs[1])) {
1060 $flags = preg_split(
'/ /', $regs[1],-1,PREG_SPLIT_NO_EMPTY);
1067 $errmessage =
_(
"The server couldn't find the message you requested.") .
1068 '<p>'._(
"Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox).");
1073 $bodystructure = implode(
'',
$read);
1104 if (!empty($msg->entities))
foreach ($msg->entities as $i => $entity) {
1105 if (is_object($entity) && strtolower(get_class($entity)) ==
'message') {
1106 if (!empty($entity->rfc822_header)) {
1135 return ($response ==
'OK');
1176 $set_string = ($set ?
'+' :
'-');