Location: PHPKode > scripts > PHPwebmail > phpwebmail/mail_index.php
<?php
/*          
PHPwebmail is a webmail client written in php.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License   
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/ 

   session_start();
   require("not_registered.php");

   // get user prefs account from DB
      if ($db_link = mysql_connect("localhost",$sql_user,$sql_pw)) {
	@mysql_select_db("phpwebmail");
	$sql_query = "SELECT textmode, nummsgs FROM prefs WHERE user='$name'";
	$result = mysql_query($sql_query);
        // catch SQL errors
        if (mysql_errno()) echo ("ERROR ".mysql_error());
	$row = mysql_fetch_row($result);
	$textmode = $row[0];
	$nummsgs = $row[1];
      } // if db_link ends
?>

<html>
<head> 
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>PHPwebmail</title></head>

<body bgcolor="<?php echo $bg_color_frame; ?>" text="<?php echo $fg_color; ?>" link="<?php echo $link_color; ?>" vlink="<?php echo $link_color; ?>" alink="<?php echo $alink_color; ?>">
<font face="arial" size=2>

<?php

function ask_delete () {

    global $folder;
    global $name;
    global $passwd;
    global $imap_server;
    global $textmode;
?>
	<font face="arial" size=2><b>Really and finally delete all messages in 
	<?php if (!$textmode) echo "<img src=images/folder.gif> ";  
	echo $folder; ?>?<br><br>
	<a href="mail_index.php?<?php SID ?>&action=really">
	<?php if (!$textmode) echo "<img src=images/_yes.gif border=0 alt=\"yes\">"; else echo "yes"; ?></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<a href="javascript:history.back()">
	<?php if (!$textmode) echo "<img src=images/_no.gif border=0 alt=\"no\">"; else echo "no"; ?></a></b>
<?php
}

function full_delete () {

    global $folder;
    global $name;
    global $passwd;
    global $imap_server;

    $mbox = imap_open("{".$imap_server.":143}".$folder,$name,$passwd);
    if ($mbox == 0) {
       print("<B>Error</B> opening mailbox for ".$name);
    } else {
	$messages = imap_num_msg($mbox);
	for($i=1;$i<=$messages;$i++) imap_setflag_full($mbox,imap_uid($mbox,$i),'\\DELETED',SE_UID);
       $dummy = imap_expunge($mbox);
       $dummy = imap_close($mbox);	
    }	

}


function delete_mail () {

    global $folder;
    global $name;
    global $passwd;
    global $message;
    global $imap_server;
    

    $mbox = imap_open("{".$imap_server.":143}".$folder,$name,$passwd);
    if ($mbox == 0) {
       print("<B>Error</B> opening mailbox for ".$name);
    } else {
       $head = imap_headerinfo($mbox,$message,40,40,""); 
       $deleted = $head->Deleted;
       // just toggle the messages "Deleted" flag. The message will be removed with expunge_mails.
       if ($deleted == "D") imap_clearflag_full($mbox,imap_uid($mbox,$message),'\\DELETED',SE_UID);
       else imap_setflag_full($mbox,imap_uid($mbox,$message),'\\DELETED',SE_UID);

       $dummy = imap_close($mbox);
    }
}


function expunge_mails () {

    global $folder;
    global $name;
    global $passwd;
    global $imap_server;

    $mbox = imap_open("{".$imap_server.":143}".$folder,$name,$passwd);
    if ($mbox == 0) {
       print("<B>Error</B> opening mailbox for ".$name);
    } else {
       $dummy = imap_expunge($mbox);
       $dummy = imap_close($mbox);
    }
}


function show_mailbox () {

	// remember the passed login information
        global $folder;
	global $name;
	global $passwd;
	global $imap_server;
	global $textmode;
	global $nummsgs;
        global $topmsgnum;
        global $startat;

    // open an IMAP connection
    $mbox = imap_open("{".$imap_server.":143}".$folder,$name,$passwd);

    if ($mbox == 0) {
        header("Location:index.php");
    } else {

      $messages = imap_num_msg($mbox);

      if ($messages == 0) {
          echo "<font face=\"arial\" size=2><B>Mail folder ";
          if (!$textmode) echo "<img src=images/folder.gif> ";
          echo $folder." is empty.</B><BR>";
      } else {
              if ($startat == $messages) unset($startat); // back at the beginning
	      if (!$startat) {
	      	$lastmsg = $messages - $nummsgs;
		$mess_start = $messages;
	      } else {
	      	$lastmsg = $startat - $nummsgs;
		$mess_start = $startat;
	      }
 
          echo ("<TABLE BORDER=1><TR VALIGN=TOP>");
          echo "<TD align=left width=50><font face=\"arial\" size=2>Flags</TD>
	 	<TD><font face=\"arial\" size=2><b>";
	   	if (ucwords($folder) == "INBOX") echo "From "; else echo "From/To ";
	   	echo "</b></TD>
		<TD><font face=\"arial\" size=2><b>Subject </b><i>(click to read)</TD>
		<TD><font face=\"arial\" size=2>Date/Time</TD>
		<TD><font face=\"arial\" size=2>$messages Messages <a href=\"mail_index.php?".SID."&action=all\">";
	  // if ($textmode) echo "delete all"; else echo "<img src=\"images/_del_all.gif\" border=0 alt=\"delete all\">";
	  echo "delete all"; 
	  echo "</a><br>";
	  if (isset($startat) && ($mess_start < $messages)) {  
                echo ("<A HREF=mail_index.php?SID&startat=".($startat + $nummsgs)." target=message_index>&lt; Previous ($nummsgs)</a>");
          }	  
          if (($startat && $startat > $nummsgs) || (!$startat && $messages > $nummsgs)) {
	  	if ($startat && $startat > $nummsgs) $the_num = $startat - $nummsgs;
			else $the_num = $messages - $nummsgs;
		if ($the_num > $nummsgs) $the_num = $nummsgs;
                echo (" <A HREF=mail_index.php?SID&startat=$lastmsg target=message_index>Next (".$the_num.") &gt;</a>");
 	  }	  
	  echo "</TD></TR>";

         //This for loop goes highest to lowest so newer messages are on top.
         for ($i=$mess_start;$i>$lastmsg;$i--) {
                if ($head = imap_header($mbox,$i,255,80,"")) {
                        $datestring = $head->date;
        		  // beautify date string: remove seconds and time zone)
        		  $date = substr ($datestring, 0, strlen($datestring) - strlen(strrchr($datestring,":")));

        		  // Was it a sent or received message?
        		  $from = $head->fromaddress;
        		  $to = $head->toaddress;
        		  if (eregi($name."@",$from)) $recipient = $to; else $recipient = $from;

        		  // In "INBOX" only show "From"
        	  	  if (ucwords($folder) == "INBOX") $recipient = $from;

                        // be special chars safe
                        $subject = decode_mime_string($head->subject);
        		  if (empty($subject)) $subject = "(no subject)";

        		  // get the flags
        		  $recent = $head->Recent;
                        $new = $head->New;
                        $unseen = $head->Unseen;
                        $flagged = $head->Flagged;
                        $answered = $head->Answered;
                        $deleted = $head->Deleted;

        		// draw nice icons for the flags
        		if ($answered == 'A') $flag_1 = "<img src=\"images/reply.gif\" alt=\"Answered\">"; else unset($flag_1);
        		if ($deleted == 'D') $flag_2 = "<img src=\"images/expunge.gif\" alt=\"Deleted\">"; else unset($flag_2);
        		if ($new == 'N' || $unseen == 'U' || $recent == 'R') $flag_3 = "<img src=\"images/new.gif\" alt=\"New, Unseen or Recent\">"; else unset($flag_3);
        		if ($flagged == 'F') $flag_4 = "<b>F</b>"; else unset($flag_4);

                        echo ("<TR VALIGN=TOP>");
                        if ($textmode) {
                        	if ($flag_1) $flag_1 = "A ";
                        	if ($flag_2) $flag_2 = "D ";
                        	if ($flag_3) $flag_3 = "N";
                        }
                        echo ("<TD><font face=\"arial\" size=2>".$flag_1.$flag_2.$flag_3.$flag_4."&nbsp;</TD>");
                        echo ("<TD><font face=\"arial\" size=2>".$recipient."</TD>");
                        echo ("<TD><font face=\"arial\" size=2><A HREF=\"read_mail.php?").SID;
                        echo ("&message=".$i."\" target=\"message_text\">".$subject."</A></TD>");
                        echo ("<TD ALIGN=CENTER><font face=\"arial\" size=2>".$date."</TD>");
        		echo ("<TD ALIGN=CENTER><font face=\"arial\" size=2>");
        		if ($textmode) {
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=reply\" target=\"message_text\">re</A> | ";
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=replyall\" target=\"message_text\">all</A> | ";
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=forward\" target=\"message_text\">fw</A> | ";
        		  echo "<a href=mail_index.php?SID&message=$i&action=delete";
			  if ($startat) echo "&startat=$startat";
			  echo ">del</A>";
        		} else {
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=reply\" target=\"message_text\"><img src=\"images/_re.gif\" border=0 ALT=\"reply\" align=\"center\"></A><img src=\"images/_div.gif\" align=\"center\">";
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=replyall\" target=\"message_text\"><img src=\"images/_all.gif\" border=0 ALT=\"reply all\" align=\"center\"></A><img src=\"images/_div.gif\" align=\"center\">";
        		  echo "<a href=\"send_mail.php?".SID."&message=".$i."&action=forward\" target=\"message_text\"><img src=\"images/_fw.gif\" border=0 ALT=\"forward\" align=\"center\"></A><img src=\"images/_div.gif\" align=\"center\">";
        		  echo "<a href=mail_index.php?SID&message=$i&action=delete";
			  if ($startat) echo "&startat=$startat";
			  echo "><img src=\"images/_del.gif\" border=0 ALT=\"delete\" align=\"center\"></A>";
        		}
        		echo ("</TD></TR>\n");
		} // if head
         } // for

         echo ("</table>");
      }
      // close the stream
      $dummy = imap_close($mbox);
    }
}

// this is the main entry point

//if ($select_folder != "") $folder = $select_folder;
if (!isset($folder)) $folder="INBOX";
if (!isset($folder_new)) $folder_new="";
if ($folder_new != "") $folder = $folder_new;

// use mime decoding-encoding functions
require("mime.php");

if (!isset($action)) $action="";
if ($action == "delete") delete_mail();
if ($action == "expunge") expunge_mails();
if ($action == "really") full_delete();
if ($action == "all") ask_delete();
	else show_mailbox();

?>

</BODY></HTML>
Return current item: PHPwebmail