Happy New Year
[squirrelmail.git] / functions / imap_messages.php
index 0de22a6218ae5d5cdde377d591b95acbab40230a..48d2feebd8d84eeae9d200616620c9ad68042189 100755 (executable)
@@ -6,7 +6,7 @@
  * This implements functions that manipulate messages
  * NOTE: Quite a few functions in this file are obsolete
  *
- * @copyright 1999-2011 The SquirrelMail Project Team
+ * @copyright 1999-2022 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -82,7 +82,8 @@ function sqimap_msgs_list_delete($imap_stream, $mailbox, $id, $bypass_trash=fals
 /**
  * Set a flag on the provided uid list
  * @param  resource imap connection
- * @param  array $id list with uid's
+ * @param  mixed $id Normally an array which is a list with message UIDs to be flagged
+ *                   or a string range such as "1:*"
  * @param  string $flag Flags to set/unset flags can be i.e.'\Seen', '\Answered', '\Seen \Answered'
  * @param  bool   $set  add (true) or remove (false) the provided flag
  * @param  bool   $handle_errors Show error messages in case of a NO, BAD or BYE response
@@ -92,8 +93,12 @@ function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
     $msgs_id = sqimap_message_list_squisher($id);
     $set_string = ($set ? '+' : '-');
 
-    for ($i=0; $i<sizeof($id); $i++) {
-        $aMessageList["$id[$i]"] = array();
+    $aMessageList = array();
+    // TODO: There doesn't seem to be a reason to set up $aMessageList anyway because an empty array for each message doesn't add anything to the parseFetch() return value, so this code block could be simply deleted:
+    if (!is_string($id)) {
+        for ($i=0; $i<sizeof($id); $i++) {
+            $aMessageList["$id[$i]"] = array();
+        }
     }
 
     $aResponse = sqimap_run_command_list($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE);
@@ -103,8 +108,8 @@ function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
 
     // some broken IMAP servers do not return UID elements on UID STORE
     // if this is the case, then we need to do a UID FETCH
-    $testkey=$id[0];
-    if (!isset($parseFetchResults[$testkey]['UID'])) {
+    if (!empty($parseFetchResults)
+     && !isset(reset($parseFetchResults)['UID'])) {
         $aResponse = sqimap_run_command_list($imap_stream, "FETCH $msgs_id (FLAGS)", $handle_errors, $response, $message, TRUE);
         $parseFetchResults = parseFetch($aResponse,$aMessageList);
     }
@@ -241,25 +246,47 @@ function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid =
       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);
+            if (check_php_version(5, 3, 0))
+                $walk_function = 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;
+                };
+            else
+                $walk_function = 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;');
+            array_walk($msgs, $walk_function, $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);
+            if (check_php_version(5, 3, 0))
+                $walk_function = 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];
+                };
+            else
+                $walk_function = 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];');
+            array_walk($msgs, $walk_function, $sSortField);
             $walk = true;
         }
         foreach ($msgs as $item) {
@@ -276,9 +303,16 @@ function get_squirrel_sort($imap_stream, $sSortField, $reverse = false, $aUid =
       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);
+            if (check_php_version(5, 3, 0))
+                $walk_function = function(&$v,$k,$f) {
+                    $v[$f] = (isset($v[$f])) ? $v[$f] : "";
+                    $v[$f] = getTimeStamp(explode(" ",$v[$f]));
+                };
+            else
+                $walk_function = create_function('&$v,$k,$f',
+                    '$v[$f] = (isset($v[$f])) ? $v[$f] : "";
+                     $v[$f] = getTimeStamp(explode(" ",$v[$f]));');
+            array_walk($msgs, $walk_function, $sSortField);
             $walk = true;
         }
         // nobreak;
@@ -400,7 +434,7 @@ function get_thread_sort($imap_stream, $search='ALL') {
 
     if ($sThreadResponse) {
         for ($i=0,$iCnt = strlen($sThreadResponse);$i<$iCnt;++$i) {
-            $cChar = $sThreadResponse{$i};
+            $cChar = $sThreadResponse[$i];
             switch ($cChar) {
                 case '(': // new sub thread
                     // correction for a subthread of a thread with no parents in thread
@@ -468,14 +502,14 @@ function elapsedTime($start) {
  * @return string $s parsed string without the double quotes or literal count
  */
 function parseString($read,&$i) {
-    $char = $read{$i};
+    $char = $read[$i];
     $s = '';
     if ($char == '"') {
         $iPos = ++$i;
         while (true) {
             $iPos = strpos($read,'"',$iPos);
             if (!$iPos) break;
-            if ($iPos && $read{$iPos -1} != '\\') {
+            if ($iPos && $read[$iPos -1] != '\\') {
                 $s = substr($read,$i,($iPos-$i));
                 $i = $iPos;
                 break;
@@ -622,7 +656,7 @@ function parseFetch(&$aResponse,$aMessageList = array()) {
         $aMsg['ID'] = $id;
         $fetch = substr($read,$i_space+1,5);
         if (!is_numeric($id) && $fetch !== 'FETCH') {
-            $aMsg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code
+            $aMsg['ERROR'] = $read; // sm_encode_html_special_chars should be done just before display. this is backend code
             break;
         }
         $i = strpos($read,'(',$i_space+5);
@@ -681,7 +715,7 @@ function parseFetch(&$aResponse,$aMessageList = array()) {
             case 'BODYSTRUCTURE':
                 break; // to be implemented, moving imap code out of the Message class
             case 'INTERNALDATE':
-                $aMsg['INTERNALDATE'] = trim(str_replace('  ', ' ',parseString($read,$i)));
+                $aMsg['INTERNALDATE'] = trim(preg_replace('/\s+/', ' ',parseString($read,$i)));
                 break;
             case 'BODY.PEEK[HEADER.FIELDS':
             case 'BODY[HEADER.FIELDS':
@@ -702,9 +736,9 @@ function parseFetch(&$aResponse,$aMessageList = array()) {
                             $value = trim(substr($line, $pos+1));
                             switch($field) {
                                 case 'date':
-                                    $aMsg['date'] = trim(str_replace('  ', ' ', $value));
+                                    $aMsg['date'] = trim(preg_replace('/\s+/', ' ', $value));
                                     break;
-                                case 'x-priority': $aMsg['x-priority'] = ($value) ? (int) $value{0} : 3; break;
+                                case 'x-priority': $aMsg['x-priority'] = ($value) ? (int) $value[0] : 3; break;
                                 case 'priority':
                                 case 'importance':
                                     // duplicate code with Rfc822Header.cls:parsePriority()
@@ -773,8 +807,8 @@ function sqimap_parse_envelope($read, &$i, &$msg) {
     $arg_no = 0;
     $arg_a = array();
     ++$i;
-    for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) {
-        $char = strtoupper($read{$i});
+    for ($cnt = strlen($read); ($i < $cnt) && ($read[$i] != ')'); ++$i) {
+        $char = strtoupper($read[$i]);
         switch ($char) {
             case '{':
             case '"':
@@ -797,8 +831,8 @@ function sqimap_parse_envelope($read, &$i, &$msg) {
                 $addr_a = array();
                 $group = '';
                 $a=0;
-                for (; $i < $cnt && $read{$i} != ')'; ++$i) {
-                    if ($read{$i} == '(') {
+                for (; $i < $cnt && $read[$i] != ')'; ++$i) {
+                    if ($read[$i] == '(') {
                         $addr = sqimap_parse_address($read, $i);
                         if (($addr[3] == '') && ($addr[2] != '')) {
                             /* start of group */
@@ -852,8 +886,8 @@ function sqimap_parse_envelope($read, &$i, &$msg) {
  */
 function sqimap_parse_address($read, &$i) {
     $arg_a = array();
-    for (; $read{$i} != ')'; ++$i) {
-        $char = strtoupper($read{$i});
+    for (; $read[$i] != ')'; ++$i) {
+        $char = strtoupper($read[$i]);
         switch ($char) {
             case '{':
             case '"': $arg_a[] =  parseString($read,$i); break;