+/**
+ * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail
+ *
+ * @param resource $imap_stream IMAP socket connection
+ * @param string $sSortField Field to sort on
+ * @param bool $reverse Reverse order search
+ * @param array $aUid limit the search to the provided array with uid's default sqimap_get_small_headers uses 1:*
+ * @return array $aUid sorted uid list
+ */
+function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid = NULL) {
+ if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') {
+ $msgs = sqimap_get_small_header_list($imap_stream, $aUid,
+ array($sSortField), array());
+ } else {
+ $msgs = sqimap_get_small_header_list($imap_stream, $aUid,
+ array(), array($sSortField));
+ }
+
+ // sqimap_get_small_header (see above) returns fields in lower case,
+ // but the code below uses all upper case
+ foreach ($msgs as $k => $v)
+ if (isset($msgs[$k][strtolower($sSortField)]))
+ $msgs[$k][strtoupper($sSortField)] = $msgs[$k][strtolower($sSortField)];
+
+ $aUid = array();
+ $walk = false;
+ switch ($sSortField) {
+ // natcasesort section
+ case 'FROM':
+ case 'TO':
+ case 'CC':
+ if(!$walk) {
+ array_walk($msgs, create_function('&$v,&$k,$f',
+ '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
+ $addr = reset(parseRFC822Address($v[$f],1));
+ $sPersonal = (isset($addr[SQM_ADDR_PERSONAL]) && $addr[SQM_ADDR_PERSONAL]) ?
+ $addr[SQM_ADDR_PERSONAL] : "";
+ $sEmail = ($addr[SQM_ADDR_HOST]) ?
+ $addr[SQM_ADDR_MAILBOX] . "@".$addr[SQM_ADDR_HOST] :
+ $addr[SQM_ADDR_HOST];
+ $v[$f] = ($sPersonal) ? decodeHeader($sPersonal, true, false):$sEmail;'),$sSortField);
+ $walk = true;
+ }
+ // nobreak
+ case 'SUBJECT':
+ if(!$walk) {
+ array_walk($msgs, create_function('&$v,&$k,$f',
+ '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
+ $v[$f] = strtolower(decodeHeader(trim($v[$f]), true, false));
+ $v[$f] = (preg_match("/^(?:(?:vedr|sv|re|aw|fw|fwd|\[\w\]):\s*)*\s*(.*)$/si", $v[$f], $matches)) ?
+ $matches[1] : $v[$f];'),$sSortField);
+ $walk = true;
+ }
+ foreach ($msgs as $item) {
+ $aUid[$item['UID']] = $item[$sSortField];
+ }
+ natcasesort($aUid);
+ $aUid = array_keys($aUid);
+ if ($reverse) {
+ $aUid = array_reverse($aUid);
+ }
+ break;
+ // \natcasesort section
+ // sort_numeric section
+ case 'DATE':
+ case 'INTERNALDATE':
+ if(!$walk) {
+ array_walk($msgs, create_function('&$v,$k,$f',
+ '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
+ $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField);
+ $walk = true;
+ }
+ // nobreak;
+ case 'RFC822.SIZE':
+ if(!$walk) {
+ // redefine $sSortField to maintain the same namespace between
+ // server-side sorting and SquirrelMail sorting
+ $sSortField = 'SIZE';
+ }
+ foreach ($msgs as $item) {
+ $aUid[$item['UID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0;
+ }
+ if ($reverse) {
+ arsort($aUid,SORT_NUMERIC);
+ } else {
+ asort($aUid, SORT_NUMERIC);
+ }
+ $aUid = array_keys($aUid);
+ break;
+ // \sort_numeric section
+ case 'UID':
+ $aUid = array_reverse($msgs);
+ break;
+ }
+ return $aUid;
+}