-
- /******************************************************************************
- ** This function simply deletes the given folder
- ******************************************************************************/
- function sqimap_mailbox_delete ($imap_stream, $mailbox) {
- fputs ($imap_stream, sqimap_session_id() . " DELETE \"$mailbox\"\r\n");
- $read_ary = sqimap_read_data($imap_stream, sqimap_session_id(), true, $response, $message);
- sqimap_unsubscribe ($imap_stream, $mailbox);
- }
-
- /***********************************************************************
- ** Determines if the user is subscribed to the folder or not
- **********************************************************************/
- function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
- $boxes = sqimap_mailbox_list ($imap_stream);
- foreach ($boxes as $ref) {
- if ($ref['unformatted'] == $folder)
- return true;
- }
- return false;
- }
-
-
-
- /******************************************************************************
- ** Formats a mailbox into 4 parts for the $boxes array
- **
- ** The four parts are:
- **
- ** raw - Raw LIST/LSUB response from the IMAP server
- ** formatted - nicely formatted folder name
- ** unformatted - unformatted, but with delimiter at end removed
- ** unformatted-dm - folder name as it appears in raw response
- ** unformatted-disp - unformatted without $folder_prefix
- **
- ******************************************************************************/
- function sqimap_mailbox_parse ($line, $line_lsub, $dm) {
- global $folder_prefix;
-
- // Process each folder line
- for ($g=0; $g < count($line); $g++) {
-
- // Store the raw IMAP reply
- if (isset($line[$g]))
- $boxes[$g]["raw"] = $line[$g];
- else
- $boxes[$g]["raw"] = "";
-
-
- // Count number of delimiters ($dm) in folder name
- $mailbox = trim($line_lsub[$g]);
- $dm_count = countCharInString($mailbox, $dm);
- if (substr($mailbox, -1) == $dm)
- $dm_count--; // If name ends in delimiter - decrement count by one
-
- // Format folder name, but only if it's a INBOX.* or have
- // a parent.
- $boxesbyname[$mailbox] = $g;
- $parentfolder = readMailboxParent($mailbox, $dm);
- if((strtolower(substr($mailbox, 0, 5)) == "inbox") ||
- (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
- (isset($boxesbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
- $indent = $dm_count - (countCharInString($folder_prefix, $dm));
- if ($indent > 0)
- $boxes[$g]["formatted"] = str_repeat(" ", $indent);
- else
- $boxes[$g]["formatted"] = '';
- $boxes[$g]["formatted"] .= readShortMailboxName($mailbox, $dm);
- } else {
- $boxes[$g]["formatted"] = $mailbox;
- }
-
- $boxes[$g]['unformatted-dm'] = $mailbox;
- if (substr($mailbox, -1) == $dm)
+/**
+ * Expunges a mailbox
+ *
+ * WARNING: Select mailbox before calling this function.
+ *
+ * permanently removes all messages that have the \Deleted flag
+ * set from the selected mailbox. See EXPUNGE command chapter in
+ * IMAP RFC.
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name (unused since 1.1.3).
+ * @param boolean $handle_errors error handling control (displays error_box on error).
+ * @param mixed $id (since 1.3.0) integer message id or array with integer ids
+ * @return integer number of expunged messages
+ * @since 1.0 or older
+ */
+function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
+ if ($id) {
+ if (is_array($id)) {
+ $id = sqimap_message_list_squisher($id);
+ }
+ $id = ' '.$id;
+ $uid = TRUE;
+ } else {
+ $uid = false;
+ }
+ $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
+ $response, $message, $uid);
+ $cnt = 0;
+
+ if (is_array($read)) {
+ foreach ($read as $r) {
+ if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
+ $cnt++;
+ }
+ }
+ }
+ return $cnt;
+}
+
+/**
+ * Checks whether or not the specified mailbox exists
+ *
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @param array $mailboxlist (since 1.5.1) optional array of mailboxes from
+ * sqimap_get_mailboxes() (to avoid having to talk to imap server)
+ * @return boolean
+ * @since 1.0 or older
+ */
+function sqimap_mailbox_exists ($imap_stream, $mailbox, $mailboxlist=null) {
+ if (!isset($mailbox) || empty($mailbox)) {
+ return false;
+ }
+
+ if (is_array($mailboxlist)) {
+ // use previously retrieved mailbox list
+ foreach ($mailboxlist as $mbox) {
+ if ($mbox['unformatted-dm'] == $mailbox) { return true; }
+ }
+ return false;
+ } else {
+ // go to imap server
+ $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
+ true, $response, $message);
+ return isset($mbx[0]);
+ }
+}
+
+/**
+ * Selects a mailbox
+ * Before 1.3.0 used more arguments and returned data depended on those arguments.
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @return array results of select command (on success - permanentflags, flags and rights)
+ * @since 1.0 or older
+ */
+function sqimap_mailbox_select ($imap_stream, $mailbox) {
+ if (empty($mailbox)) {
+ return;
+ }
+
+ // cleanup $mailbox in order to prevent IMAP injection attacks
+ $mailbox = str_replace(array("\r","\n"), array("",""),$mailbox);
+
+ /**
+ * Default UW IMAP server configuration allows to access other files
+ * on server. $imap_server_type is not checked because interface can
+ * be used with 'other' or any other server type setting. $mailbox
+ * variable can be modified in any script that uses variable from GET
+ * or POST. This code blocks all standard SquirrelMail IMAP API requests
+ * that use mailbox with full path (/etc/passwd) or with ../ characters
+ * in path (../../etc/passwd)
+ */
+ if (strstr($mailbox, '../') || substr($mailbox, 0, 1) == '/') {
+ global $oTemplate;
+ error_box(sprintf(_("Invalid mailbox name: %s"),htmlspecialchars($mailbox)));
+ sqimap_logout($imap_stream);
+ $oTemplate->display('footer.tpl');
+ die();
+ }
+
+ $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
+ true, $response, $message);
+ $result = array();
+ for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
+ if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
+ $result[strtoupper($regs[1])] = $regs[2];
+ } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
+ $result[strtoupper($regs[2])] = $regs[1];
+ } else {
+ if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
+ $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
+ $result['PERMANENTFLAGS'] = explode(' ',strtolower($regs[1]));
+ } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
+ $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
+ $result['FLAGS'] = explode(' ',strtolower($regs[1]));
+ }
+ }
+ }
+ if (!isset($result['PERMANENTFLAGS'])) {
+ $result['PERMANENTFLAGS'] = $result['FLAGS'];
+ }
+ if (preg_match('/^\[(.+)\]/',$message, $regs)) {
+ $result['RIGHTS']=strtoupper($regs[1]);
+ }
+
+ return $result;
+}
+
+/**
+ * Creates a folder.
+ *
+ * Mailbox is automatically subscribed.
+ *
+ * Set $type to string that does not match 'noselect' (case insensitive),
+ * if you don't want to prepend delimiter to mailbox name. Please note
+ * that 'noinferiors' might be used someday as keyword for folders
+ * that store only messages.
+ * @param stream $imap_steam imap connection resource
+ * @param string $mailbox mailbox name
+ * @param string $type folder type.
+ * @since 1.0 or older
+ */
+function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
+ global $delimiter;
+ if (strtolower($type) == 'noselect') {
+ $create_mailbox = $mailbox . $delimiter;
+ } else {
+ $create_mailbox = $mailbox;
+ }
+
+ $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
+ sqimap_encode_mailbox_name($create_mailbox),
+ true, $response, $message);
+ sqimap_subscribe ($imap_stream, $mailbox);
+}
+
+/**
+ * Subscribes to an existing folder.
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @param boolean $debug (since 1.5.1)
+ * @since 1.0 or older
+ */
+function sqimap_subscribe ($imap_stream, $mailbox,$debug=true) {
+ $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
+ sqimap_encode_mailbox_name($mailbox),
+ $debug, $response, $message);
+}
+
+/**
+ * Unsubscribes from an existing folder
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @since 1.0 or older
+ */
+function sqimap_unsubscribe ($imap_stream, $mailbox) {
+ $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
+ sqimap_encode_mailbox_name($mailbox),
+ false, $response, $message);
+}
+
+/**
+ * Deletes the given folder
+ * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @since 1.0 or older
+ */
+function sqimap_mailbox_delete ($imap_stream, $mailbox) {
+ global $data_dir, $username;
+ sqimap_unsubscribe ($imap_stream, $mailbox);
+
+ if (sqimap_mailbox_exists($imap_stream, $mailbox)) {
+
+ $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
+ sqimap_encode_mailbox_name($mailbox),
+ true, $response, $message);
+ if ($response !== 'OK') {
+ // subscribe again
+ sqimap_subscribe ($imap_stream, $mailbox);
+ } else {
+ $temp = array(&$mailbox, 'delete', '');
+ do_hook('rename_or_delete_folder', $temp);
+ removePref($data_dir, $username, "thread_$mailbox");
+ removePref($data_dir, $username, "collapse_folder_$mailbox");
+ }
+ }
+}
+
+/**
+ * Determines if the user is subscribed to the folder or not
+ * @param stream $imap_stream imap connection resource
+ * @param string $mailbox mailbox name
+ * @return boolean
+ * @since 1.2.0
+ */
+function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
+ $boxesall = sqimap_mailbox_list ($imap_stream);
+ foreach ($boxesall as $ref) {
+ if ($ref['unformatted'] == $folder) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/**
+ * Renames a mailbox.
+ * Since 1.2.6 and 1.3.0 contains rename_or_delete_folder hook
+ * @param stream $imap_stream imap connection resource
+ * @param string $old_name mailbox name
+ * @param string $new_name new mailbox name
+ * @since 1.2.3
+ */
+function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
+ if ( $old_name != $new_name ) {
+ global $delimiter, $imap_server_type, $data_dir, $username;
+ if ( substr( $old_name, -1 ) == $delimiter ) {
+ $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
+ $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
+ $postfix = $delimiter;
+ } else {
+ $postfix = '';
+ }
+
+ $boxesall = sqimap_mailbox_list_all($imap_stream);
+ $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
+ ' ' . sqimap_encode_mailbox_name($new_name);
+ $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
+ sqimap_unsubscribe($imap_stream, $old_name.$postfix);
+ $oldpref_thread = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
+ $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
+ removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
+ removePref($data_dir, $username, 'collapse_folder_'.$old_name.$postfix);
+ sqimap_subscribe($imap_stream, $new_name.$postfix);
+ setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref_thread);
+ setPref($data_dir, $username, 'collapse_folder_'.$new_name.$postfix, $oldpref_collapse);
+ $temp = array(&$old_name, 'rename', &$new_name);
+ do_hook('rename_or_delete_folder', $temp);
+ $l = strlen( $old_name ) + 1;
+ $p = 'unformatted';
+
+ foreach ($boxesall as $box) {
+ if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
+ $new_sub = $new_name . $delimiter . substr($box[$p], $l);
+ /* With Cyrus IMAPd >= 2.0 rename is recursive, so don't check for errors here */
+ if ($imap_server_type == 'cyrus') {
+ $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
+ $data = sqimap_run_command($imap_stream, $cmd, false,
+ $response, $message);
+ }
+ $was_subscribed = sqimap_mailbox_is_subscribed($imap_stream, $box[$p]);
+ if ( $was_subscribed ) {
+ sqimap_unsubscribe($imap_stream, $box[$p]);
+ }
+ $oldpref_thread = getPref($data_dir, $username, 'thread_'.$box[$p]);
+ $oldpref_collapse = getPref($data_dir, $username, 'collapse_folder_'.$box[$p]);
+ removePref($data_dir, $username, 'thread_'.$box[$p]);
+ removePref($data_dir, $username, 'collapse_folder_'.$box[$p]);
+ if ( $was_subscribed ) {
+ sqimap_subscribe($imap_stream, $new_sub);
+ }
+ setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref_thread);
+ setPref($data_dir, $username, 'collapse_folder_'.$new_sub, $oldpref_collapse);
+ $temp = array(&$box[$p], 'rename', &$new_sub);
+ do_hook('rename_or_delete_folder', $temp);
+ }
+ }
+ }
+}
+
+/**
+ * Formats a mailbox into parts for the $boxesall array
+ *
+ * The parts are:
+ * <ul>
+ * <li>raw - Raw LIST/LSUB response from the IMAP server
+ * <li>formatted - nicely formatted folder name
+ * <li>unformatted - unformatted, but with delimiter at end removed
+ * <li>unformatted-dm - folder name as it appears in raw response
+ * <li>unformatted-disp - unformatted without $folder_prefix
+ * <li>id - TODO: document me
+ * <li>flags - TODO: document me
+ * </ul>
+ * Before 1.2.0 used third argument for delimiter.
+ *
+ * Before 1.5.1 used second argument for lsub line. Argument was removed in order to use
+ * find_mailbox_name() on the raw input. Since 1.5.1 includes RFC3501 names in flags
+ * array (for example, "\NoSelect" in addition to "noselect")
+ * @param array $line
+ * @return array
+ * @since 1.0 or older
+ * @todo document id and flags keys in boxes array and function arguments.
+ */
+function sqimap_mailbox_parse ($line) {
+ global $folder_prefix, $delimiter;
+
+ /* Process each folder line */
+ for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
+ /* Store the raw IMAP reply */
+ if (isset($line[$g])) {
+ $boxesall[$g]['raw'] = $line[$g];
+ } else {
+ $boxesall[$g]['raw'] = '';
+ }
+
+ /* Count number of delimiters ($delimiter) in folder name */
+ $mailbox = find_mailbox_name($line[$g]);
+ $dm_count = substr_count($mailbox, $delimiter);
+ if (substr($mailbox, -1) == $delimiter) {
+ /* If name ends in delimiter, decrement count by one */
+ $dm_count--;
+ }
+
+ /* Format folder name, but only if it's a INBOX.* or has a parent. */
+ $boxesallbyname[$mailbox] = $g;
+ $parentfolder = readMailboxParent($mailbox, $delimiter);
+ if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
+ (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
+ (isset($boxesallbyname[$parentfolder]) &&
+ (strlen($parentfolder) > 0) ) ) {
+ $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
+ if ($indent > 0) {
+ $boxesall[$g]['formatted'] = str_repeat(' ', $indent);
+ } else {
+ $boxesall[$g]['formatted'] = '';
+ }
+ $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
+ } else {
+ $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
+ }
+
+ $boxesall[$g]['unformatted-dm'] = $mailbox;
+ if (substr($mailbox, -1) == $delimiter) {