+/** Check capabilities */
+fputs($stream, "A001 CAPABILITY\r\n");
+$capline = '';
+while ($line=fgets($stream, 1024)){
+ if (preg_match("/A001.*/",$line)) {
+ break;
+ } else {
+ $capline.=$line;
+ }
+}
+
+/* don't display capabilities before STARTTLS */
+if ($use_imap_tls===2 && stristr($capline, 'STARTTLS') === false) {
+ do_err('Your server doesn\'t support STARTTLS.');
+} elseif($use_imap_tls===2) {
+ /* try starting starttls */
+ fwrite($stream,"A002 STARTTLS\r\n");
+ $starttls_line=fgets($stream, 1024);
+ if (! preg_match("/^A002 OK.*/i",$starttls_line)) {
+ $imap_starttls_err = 'IMAP STARTTLS failed. Server replied: '
+ .htmlspecialchars($starttls_line);
+ do_err($imap_starttls_err);
+ } elseif (! stream_socket_enable_crypto($stream,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
+ do_err('Failed to enable encryption on IMAP connection.');
+ } else {
+ echo $IND . "IMAP STARTTLS extension looks OK.<br />\n";
+ }
+
+ // get new capability line
+ fwrite($stream,"A003 CAPABILITY\r\n");
+ $capline='';
+ while ($line=fgets($stream, 1024)){
+ if (preg_match("/A003.*/",$line)) {
+ break;
+ } else {
+ $capline.=$line;
+ }
+ }
+}
+
+echo $IND . 'Capabilities: <tt>'.htmlspecialchars($capline)."</tt><br />\n";
+
+if($imap_auth_mech == 'login' && stristr($capline, 'LOGINDISABLED') !== FALSE) {
+ do_err('Your server doesn\'t allow plaintext logins. '.
+ 'Try enabling another authentication mechanism like CRAM-MD5, DIGEST-MD5 or TLS-encryption '.
+ 'in the SquirrelMail configuration.', FALSE);
+}
+
+if (stristr($capline, 'XMAGICTRASH') !== false) {
+ $magic_trash = 'It looks like IMAP_MOVE_EXPUNGE_TO_TRASH option is turned on '
+ .'in your Courier IMAP configuration. Courier does not provide tools that '
+ .'allow to detect folder used for Trash or commands are not documented. '
+ .'SquirrelMail can\'t detect special trash folder. SquirrelMail manages '
+ .'all message deletion or move operations internally and '
+ .'IMAP_MOVE_EXPUNGE_TO_TRASH option can cause errors in message and '
+ .'folder management operations. Please turn off IMAP_MOVE_EXPUNGE_TO_TRASH '
+ .'option in Courier imapd configuration.';
+ do_err($magic_trash,false);
+}
+
+/* add warning about IMAP delivery */
+if (stristr($capline, 'XCOURIEROUTBOX') !== false) {
+ $courier_outbox = 'OUTBOX setting is enabled in your Courier imapd '
+ .'configuration. SquirrelMail uses standard SMTP protocol or sendmail '
+ .'binary to send emails. Courier IMAP delivery method is not supported'
+ .' and can create duplicate email messages.';
+ do_err($courier_outbox,false);
+}
+
+/** OK, close connection */
+fputs($stream, "A004 LOGOUT\r\n");
+fclose($stream);
+