+ 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) {
+ // FIX ME: WHAAAA DO NOT USE "None" for something that does not exist. Use false or NULL instead
+ if ($mailbox == 'None') {
+ 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;