| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * move_messages.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * Enables message moving between folders on the IMAP server. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | require_once('../src/validate.php'); |
| 15 | require_once('../functions/display_messages.php'); |
| 16 | require_once('../functions/imap.php'); |
| 17 | |
| 18 | global $compose_new_win; |
| 19 | |
| 20 | function putSelectedMessagesIntoString($msg) { |
| 21 | $j = 0; |
| 22 | $i = 0; |
| 23 | $firstLoop = true; |
| 24 | |
| 25 | // If they have selected nothing msg is size one still, but will |
| 26 | // be an infinite loop because we never increment j. so check to |
| 27 | // see if msg[0] is set or not to fix this. |
| 28 | while (($j < count($msg)) && ($msg[0])) { |
| 29 | if ($msg[$i]) { |
| 30 | if ($firstLoop != true) { |
| 31 | $selectedMessages .= "&"; |
| 32 | } else { |
| 33 | $firstLoop = false; |
| 34 | } |
| 35 | |
| 36 | $selectedMessages .= "selMsg[$j]=$msg[$i]"; |
| 37 | |
| 38 | $j++; |
| 39 | } |
| 40 | $i++; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | function attachSelectedMessages($msg, $imapConnection) { |
| 45 | |
| 46 | global $mailbox, $username, $attachment_dir, $attachments, $identity, $data_dir, $composesession; |
| 47 | |
| 48 | |
| 49 | if (!isset($attachments)) { |
| 50 | $attachments = array(); |
| 51 | session_register('attachments'); |
| 52 | } |
| 53 | |
| 54 | if (!isset($composesession)) { |
| 55 | $composesession = 1; |
| 56 | session_register('$composesession'); |
| 57 | } else { |
| 58 | $composesession++; |
| 59 | } |
| 60 | |
| 61 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir,$composesession); |
| 62 | |
| 63 | $rem_attachments = array(); |
| 64 | foreach ($attachments as $info) { |
| 65 | if ($info['session'] == $composesession) { |
| 66 | $attached_file = "$hashed_attachment_dir/$info[localfilename]"; |
| 67 | if (file_exists($attached_file)) { |
| 68 | unlink($attached_file); |
| 69 | } |
| 70 | } else { |
| 71 | $rem_attachments[] = $info; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | $attachments = $rem_attachments; |
| 76 | |
| 77 | |
| 78 | $i = 0; |
| 79 | $j = 0; |
| 80 | |
| 81 | while ($j < count($msg)) { |
| 82 | if (isset($msg[$i])) { |
| 83 | $id = $msg[$i]; |
| 84 | $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage); |
| 85 | if ($response = 'OK') { |
| 86 | // get subject so we can set the remotefilename |
| 87 | $read = sqimap_run_command ($imapConnection, "FETCH $id BODY.PEEK[HEADER.FIELDS (Subject)]", true, $response, $readmessage); |
| 88 | $subject = substr($read[1], strpos($read[1], ' ')); |
| 89 | $subject = trim($subject); |
| 90 | |
| 91 | if (isset($subject) && $subject != '') { |
| 92 | $subject = htmlentities($subject); |
| 93 | } else { |
| 94 | $subject = _("<No subject>"); |
| 95 | $subject = htmlentities($subject); |
| 96 | } |
| 97 | |
| 98 | array_shift($body_a); |
| 99 | $body = implode('', $body_a); |
| 100 | $body .= "\r\n"; |
| 101 | |
| 102 | $localfilename = GenerateRandomString(32, 'FILE', 7); |
| 103 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
| 104 | |
| 105 | $fp = fopen( $full_localfilename, 'w'); |
| 106 | fwrite ($fp, $body); |
| 107 | fclose($fp); |
| 108 | |
| 109 | $newAttachment = array(); |
| 110 | $newAttachment['localfilename'] = $localfilename; |
| 111 | $newAttachment['type'] = "message/rfc822"; |
| 112 | $newAttachment['remotefilename'] = "$subject".".eml"; |
| 113 | $newAttachment['session'] = $composesession; |
| 114 | $attachments[] = $newAttachment; |
| 115 | flush(); |
| 116 | } |
| 117 | $j++; |
| 118 | } |
| 119 | $i++; |
| 120 | |
| 121 | } |
| 122 | return $composesession; |
| 123 | } |
| 124 | |
| 125 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
| 126 | sqimap_mailbox_select($imapConnection, $mailbox); |
| 127 | |
| 128 | // expunge-on-demand if user isn't using move_to_trash or auto_expunge |
| 129 | if(isset($expungeButton)) { |
| 130 | sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
| 131 | $location = get_location(); |
| 132 | if (isset($where) && isset($what)) { |
| 133 | header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)); |
| 134 | } else { |
| 135 | header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)); |
| 136 | } |
| 137 | |
| 138 | } elseif(isset($undeleteButton)) { |
| 139 | // undelete messages if user isn't using move_to_trash or auto_expunge |
| 140 | |
| 141 | if (is_array($msg) == 1) { |
| 142 | // Removes \Deleted flag from selected messages |
| 143 | $j = 0; |
| 144 | $i = 0; |
| 145 | |
| 146 | // If they have selected nothing msg is size one still, but will be an infinite |
| 147 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
| 148 | while ($j < count($msg)) { |
| 149 | if ($msg[$i]) { |
| 150 | sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted"); |
| 151 | $j++; |
| 152 | } |
| 153 | $i++; |
| 154 | } |
| 155 | $location = get_location(); |
| 156 | |
| 157 | if ($where && $what) |
| 158 | header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)); |
| 159 | else |
| 160 | header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)); |
| 161 | } else { |
| 162 | displayPageHeader($color, $mailbox); |
| 163 | error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color); |
| 164 | } |
| 165 | } elseif (!isset($moveButton)) { |
| 166 | // If the delete button was pressed, the moveButton variable will not be set. |
| 167 | if (is_array($msg) == 1) { |
| 168 | // Marks the selected messages as 'Deleted' |
| 169 | $j = 0; |
| 170 | $i = 0; |
| 171 | |
| 172 | // If they have selected nothing msg is size one still, but will be an infinite |
| 173 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
| 174 | while ($j < count($msg)) { |
| 175 | if (isset($msg[$i])) { |
| 176 | if (isset($markRead)) { |
| 177 | sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen"); |
| 178 | } else if (isset($markUnread)) { |
| 179 | sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen"); |
| 180 | } else if (isset($attache)) { |
| 181 | break; |
| 182 | } else { |
| 183 | sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox); |
| 184 | } |
| 185 | $j++; |
| 186 | } |
| 187 | $i++; |
| 188 | } |
| 189 | if ($auto_expunge) { |
| 190 | sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
| 191 | } |
| 192 | $location = get_location(); |
| 193 | if (isset($where) && isset($what)) { |
| 194 | header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)); |
| 195 | } elseif(isset($attache)) { |
| 196 | $composesession = attachSelectedMessages($msg, $imapConnection); |
| 197 | if ($compose_new_win == '1') { |
| 198 | header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)."&composenew=1&session=$composesession&attachedmessages=true"); |
| 199 | } else { |
| 200 | header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true"); |
| 201 | } |
| 202 | } else { |
| 203 | header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)); |
| 204 | } |
| 205 | } else { |
| 206 | displayPageHeader($color, $mailbox); |
| 207 | error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color); |
| 208 | } |
| 209 | } else { // Move messages |
| 210 | // lets check to see if they selected any messages |
| 211 | if (is_array($msg) == 1) { |
| 212 | $j = 0; |
| 213 | $i = 0; |
| 214 | |
| 215 | // If they have selected nothing msg is size one still, but will be an infinite |
| 216 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
| 217 | while ($j < count($msg)) { |
| 218 | if (isset($msg[$i])) { |
| 219 | /** check if they would like to move it to the trash folder or not */ |
| 220 | sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox); |
| 221 | sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted"); |
| 222 | $j++; |
| 223 | } |
| 224 | $i++; |
| 225 | } |
| 226 | if ($auto_expunge == true) |
| 227 | sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
| 228 | |
| 229 | $location = get_location(); |
| 230 | if (isset($where) && isset($what)) |
| 231 | header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where)); |
| 232 | else |
| 233 | header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)); |
| 234 | } else { |
| 235 | displayPageHeader($color, $mailbox); |
| 236 | error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // Log out this session |
| 241 | sqimap_logout($imapConnection); |
| 242 | |
| 243 | ?> |
| 244 | </BODY></HTML> |