Location: PHPKode > projects > Myphpim > MyPhPim-01.05/main.php3
<?php

//****************************************************************************************
//                Copyright (C) 2000 Koen de Boeve
//
//        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.
//
//        Version : MyPhPim-01.05
//        Author : Koen de Boeve
//        Contact: hide@address.com
//**************************************************************************************** 

include "conf/global_conf.inc";

session_start ();

$db = new DB;
$imap = new ImapCl;
$html = new HtmlCl;


//########################################
// Menu switch

Function GetFilename ( $file ) {
		$tmp = str_replace ( "\\\\", "/", $file );
		$filename = basename ( $tmp );
		return $filename;
}

function is_upload_file($filename) {
	if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
		$tmp_file = dirname(tempnam('', ''));
	}
	$tmp_file .= '/' . basename($filename);
	/* User might have trailing slash in php.ini... */
	return (ereg_replace('/+', '/', $tmp_file) == $filename);
}

if ( $step1 ) {
	$user_id = $db->UserId ( $user );
	$pdb = new PilotCl;
	$categories = $pdb->GetCategoriesPDB ( $filename );
	$list = $pdb->GetAddressPDB ( $filename );
	$numlist = count ( $list );
	for ( $i = 0; $i < $numlist; $i++ ) {
        	$entry = $list[$i];
                $numcat = count ( $selected_category ) ;
		$cat_name = $pdb->GetCatNamePDB ( $categories, $entry->cat1 );
		$ok = 0;
		for ( $j = 0; $j < $numcat; $j++ ) {
			if ( $cat_name == $selected_category[$j] ) {
				$ok = 1; 
			}
		}
		if ( ${$entry->id} == 'Y' ) {
			$ok = 1; 
		}
		if ( $ok == 1 ) {
			$query = "SELECT * FROM addresses_pilot WHERE pilot_id=$entry->id AND owner=$user_id";
                        $result = $db->query ( $query );
                        $numrows = $db->numrows ( $result );
                        if ( $numrows > 0 ) {
                                $db_id = $db->result ( $result, 0, "pilot_id" );
                        } else {
                                $db_id = FALSE;
                        }
			if ( !$db_id ) {
        			$query =  "INSERT INTO addresses ( address_id, owner, name, firstname, address, city, zip, state,
					country, telhome, telwork, telother, main, pager, email, faxwork, mobile, custom1, custom2, custom3,
					custom4, company, note, title )
					VALUES ( NULL, '$user_id', '$entry->name', '$entry->firstname', '$entry->address', '$entry->city',
					'$entry->zip', '$entry->state', '$entry->country', '$entry->telhome', '$entry->telwork',
					'$entry->telother', '$entry->main', '$entry->pager', '$entry->email', '$entry->faxwork',
					'$entry->mobile', '$entry->custom1', '$entry->custom2', '$entry->custom3', '$entry->custom4',
					'$entry->company', '$entry->note', '$entry->title')"; 
				$result = $db->query ( $query );
				$last_id = $db->LastId ( );
				$query = "INSERT INTO addresses_pilot VALUES ( NULL, '$last_id', '$entry->id', '$cat_name', '$user_id')";
				$result = $db->query ( $query );
				header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=list" );
				
			} else {
				$address_id = $db->result ( $result, 0, "address_id" );
				$query =  "UPDATE addresses SET name='$entry->name', firstname='$entry->firstname', address='$entry->address', 
					city='$entry->city', zip='$entry->zip', state='$entry->state', country='$entry->country', 
					telhome='$entry->telhome', telwork='$entry->telwork', telother='$entry->telother', main='$entry->main', 
					pager='$entry->pager', email='$entry->email', faxwork='$entry->faxwork', mobile='$entry->mobile', 
					custom1='$entry->custom1', custom2='$entry->custom2', custom3='$entry->custom3', custom4='$entry->custom4', 
					company='$entry->company', note='$entry->note', title='$entry->title' WHERE address_id=$address_id";
				$result = $db->query ( $query );
			}
		} else {		
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=list" );
		}
	}
	unlink ( $filename );
} 

if ( $addatt == "ADD" ) { $menu = "compose"; $act = $act; }

if ( $mr ) { 
	switch ( $eventtype ) {
		case "event" : 	
			switch ( $mr ) {
				case "A" : 
					switch ( $mract ) {
						case "ACCEPT" :
							$query = "UPDATE calendar SET owner_id=$user_id WHERE cal_id=$eventid";
							$result = $db->query ( $query );
							$query = "UPDATE mr SET flag='A', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Added To Your Calendar</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "REJECT" :
							$query = "UPDATE mr SET flag='R', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Rejected</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "RESEND" :
							$query = "UPDATE mr SET flag='P', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Resend</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "CLOSE" :
							$query = "UPDATE mr SET flag='C', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							$query = "DELETE FROM calendar WHERE cal_id=$eventid";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Transaction closed</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
					}
					break;;
				case "C" : 
					$query = "UPDATE mr SET flag='C' WHERE mr_id=$mr_id";
					$result = $db->query ( $query );
					echo "<html><head>";
                			echo "</head>\n";
               	 			echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
					echo "<center><h2>Transaction Closed </h2>";
					echo "<form><input type=button value=OK onClick=window.close()></form></center>";
					echo "</body></html>";
					break;;
			}
			break;;
		case "todo" : 	
			switch ( $mr ) {
				case "A" : 
					switch ( $mract ) {
						case "ACCEPT" :
							$query = "UPDATE todo SET owner=$user_id WHERE todo_id=$eventid";
							$result = $db->query ( $query );
							$query = "UPDATE mr SET flag='A', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Added To Your Todo List</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "REJECT" :
							$query = "UPDATE mr SET flag='R', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Rejected</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "RESEND" :
							$query = "UPDATE mr SET flag='P', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Resend</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "CLOSE" :
							$query = "UPDATE mr SET flag='C', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							$query = "DELETE FROM todo WHERE todo_id=$eventid";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Transaction closed</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
					}
					break;;
				case "C" : 
					$query = "UPDATE mr SET flag='C' WHERE mr_id=$mr_id";
					$result = $db->query ( $query );
					echo "<html><head>";
                			echo "</head>\n";
               	 			echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
					echo "<center><h2>Transaction Closed </h2>";
					echo "<form><input type=button value=OK onClick=window.close()></form></center>";
					echo "</body></html>";
					break;;
			}
			break;;
		case "note" : 	
			switch ( $mr ) {
				case "A" : 
					switch ( $mract ) {
						case "ACCEPT" :
							$query = "UPDATE notes SET owner=$user_id WHERE notes_id=$eventid";
							$result = $db->query ( $query );
							$query = "UPDATE mr SET flag='A', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Added To Your Notes</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "REJECT" :
							$query = "UPDATE mr SET flag='R', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Rejected</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "RESEND" :
							$query = "UPDATE mr SET flag='P', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Resend</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "CLOSE" :
							$query = "UPDATE mr SET flag='C', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							$query = "DELETE FROM notes WHERE notes_id=$eventid";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Transaction closed</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
					}
					break;;
				case "C" : 
					$query = "UPDATE mr SET flag='C' WHERE mr_id=$mr_id";
					$result = $db->query ( $query );
					echo "<html><head>";
                			echo "</head>\n";
               	 			echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
					echo "<center><h2>Transaction Closed </h2>";
					echo "<form><input type=button value=OK onClick=window.close()></form></center>";
					echo "</body></html>";
					break;;
			}
			break;;
		case "address" : 	
			switch ( $mr ) {
				case "A" : 
					switch ( $mract ) {
						case "ACCEPT" :
							$query = "UPDATE addresses SET owner=$user_id WHERE address_id=$eventid";
							$result = $db->query ( $query );
							$query = "UPDATE mr SET flag='A', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Added To Your Addressbook</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "REJECT" :
							$query = "UPDATE mr SET flag='R', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Rejected</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "RESEND" :
							$query = "UPDATE mr SET flag='P', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Record Resend</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
						case "CLOSE" :
							$query = "UPDATE mr SET flag='C', comments='$comments' WHERE mr_id=$mr_id";
							$result = $db->query ( $query );
							$query = "DELETE FROM notes WHERE notes_id=$eventid";
							$result = $db->query ( $query );
							echo "<html><head>";
                					echo "</head>\n";
               	 					echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
							echo "<center><h2>Transaction closed</h2>";
							echo "<form><input type=button value=OK onClick=window.close()></form></center>";
							echo "</body></html>";
							break;;
					}
					break;;
				case "C" : 
					$query = "UPDATE mr SET flag='C' WHERE mr_id=$mr_id";
					$result = $db->query ( $query );
					echo "<html><head>";
                			echo "</head>\n";
               	 			echo "<body bgcolor=$layout[bgcolor] link=$layout[link] vlink=$layout[vlink] alink=$layout[alink]>\n";
					echo "<center><h2>Transaction Closed </h2>";
					echo "<form><input type=button value=OK onClick=window.close()></form></center>";
					echo "</body></html>";
					break;;
			}
			break;;
	}
				 
}

if ( $menu ) {
	switch ( $menu ) {
		case "top":
                                $date = getdate() ;
                                echo "<html>\n";
                                echo "<head>\n";
                                echo "<title>htmlheader</title>\n";
				echo "<style type=\"text/css\">\n";
				echo "<!--\n";
				echo "body { margin: 0 }\n";
				echo "A{text-decoration:none}\n";
				echo "-->\n";
				echo "</style>\n";
                                echo "</head>\n";
                                echo "<body bgcolor=seagreen topmargin=0 leftmargin=0 rightmargin=0 marginwidth=10 marginheight=0>";
				echo "<table border=0 width=50%><tr><td><img src=pics/myphpim.gif border=0></td>";
				echo "<td align=center><font size=-1 color=darkseagreen><b>Version $version</b></font><br>
					<font size=-1 color=white>$date[weekday] $date[mday] $date[month] $date[year]</font><br>
					<font size=-1 color=white>Welcome <b>$user</b>	
					</font></td></tr></table>";
				echo "</body>\n";
                                echo "</html>\n";
                                break;;
                case "navigate":
				$nummodules = count ( $modules );
				$now = date ( 'Y-m-d' );
                                echo "<html>\n";
                                echo "<head>\n";
				echo "<style type=\"text/css\">\n";
                                echo "<!--\n";
                                echo "body { margin: 0 }\n";
				echo "A{text-decoration:none}\n";
                                echo "-->\n";
                                echo "</style>\n";
                                echo "<title>navigate</title>\n";
                                echo "</head>\n";
                                echo "<body bgcolor=black text=white vlink=white alink=white link=white marginheight=0>\n";
				echo "<table border=0 width=100%><tr>\n";
                                echo "<td align=left valign=bottom><font size=-1>&nbsp;</font></td>\n";
                                echo "<td align=right valign=bottom><font size=-1>&nbsp;</font></td>\n";
                                echo "<td align=right valign=bottom><font size=-1>";
				if ( $nomail == 0 ) {
                                	echo "<a href=\"javascript:onClick=parent.frames[2].location='main.php3?menu=mail&msgdetail=0';
                                	onClick=parent.frames[1].location='main.php3?menu=navigate';
                                	onClick=parent.frames[3].location='main.php3?menu=list&folder=INBOX'\" >| Mail </a>";
				} 
				echo "<a href=\"Javascript:onClick=parent.frames[2].location='todo.php3?menu=main';
                                       onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail';
                                       onClick=parent.frames[3].location='todo.php3?menu=list'\">| To Do </a>
					<a href=\"Javascript:onClick=parent.frames[2].location='notes.php3?menu=main';
                                        onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail';
                                        onClick=parent.frames[3].location='notes.php3?menu=list'\">| Notes </a>
					<a href=\"Javascript:onClick=parent.frames[2].location='calendar.php3?menu=main';
                                        onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail';
                                        onClick=parent.frames[3].location='calendar.php3?menu=list&date=$now'\">| Calendar </a>
					<a href=\"Javascript:onClick=parent.frames[2].location='addresses.php3?menu=main';
                                        onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail';
                                        onClick=parent.frames[3].location='addresses.php3?menu=list'\">| Addressbook </a>";
				// Seek modules 
				if ( $nummodules > 0 ) {
					for ( $j = 0; $j < $nummodules; $j++ ) {
						echo "<a href=\"Javascript:onClick=parent.frames[2].location='modules/$modules[$j].php3?menu=main';
                                       		onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail';
                                       		onClick=parent.frames[3].location='modules/$modules[$j].php3?menu=list'\">| $modules[$j] </a>"; 
						
					}
				}
				echo "<a href=\"Javascript:onClick=parent.frames[2].location='main.php3?menu=prefs'; 
				onClick=parent.frames[1].location='main.php3?menu=navigate&nomail=$nomail'; 
				onClick=parent.frames[3].location='main.php3?prefs=startup'\">| Prefs </a>
				<a href=logout.php3? target=_top> | Logout |</a></font></td>\n";
                                echo "</tr></table>\n";
                                echo "</body>\n";
                                echo "</html>\n";
                                break;;
		case "noaccount" : 
				echo "<html>\n";
                                echo "<head>\n";
                                echo "<title>depend</title>\n";
                                echo "</head>\n";
				if ( $error == 1 ) {
					echo "<body bgcolor=white>\n";
					echo "<center><font size=+1>No Mail Account</font></center><p>\n";
					echo "<center><font size=-1><b>Untill you add a mail account to your profile, mail functionality will be disabled<p>\n";
					echo "Please go to \"prefs\" in the main menu bar and choose \"Mail Setup\" to add a mail account</font></center><p>\n";
						
				} else { 
                                	echo "<body bgcolor=darkseagreen text=blue vlink=blue alink=blue link=blue marginheight=5>\n";
					
				}
				echo "</body></html>\n";
				break;;
		case "mail": 
                                echo "<html>\n";
                                echo "<head>\n";
				echo "<style type=\"text/css\">\n";
                                echo "<!--\n";
                                echo "body { margin: 0 }\n";
				echo "A.blue:link{text-decoration:none; color: blue}\n";
                		echo "A.blue:visited{text-decoration:none; color: blue}\n";
                		echo "A.blue:active{text-decoration:none; color: blue}\n";
                		echo "A.white:link{text-decoration:none; color: white}\n";
                		echo "A.white:visited{text-decoration:none; color: white}\n";
               	 		echo "A.white:active{text-decoration:none; color: white}\n";
                		echo "A.red:link{text-decoration:none; color: red}\n";
                		echo "A.red:visited{text-decoration:none; color: red}\n";
                		echo "A.red:active{text-decoration:none; color: red}\n";
                                echo "-->\n";
                                echo "</style>\n";
                                echo "<title>depend</title>\n";
                                echo "</head>\n";
                                echo "<body bgcolor=darkseagreen marginheight=0>\n";
                                echo "<table border=0 width=100%>\n";
                                echo "<tr>\n";
                                if ( $msgdetail == 0 ) {
                                        echo "<td align=left><font size=-1><a CLASS=blue href=\"javascript: onClick=parent.frames[2].location='main.php3?menu=mail&msgdetail=0'; parent.frames[3].location='main.php3?menu=compose'\"> | Compose | </a></font></td>\n";
                                        echo "<td align=right><font size=-1>\n";
					$userdata = $db->Fetch_login ( $user, $choosehost );
					$login = $userdata[0];
					$pass = $userdata[1]; 
					$proto = $userdata[2]; 
					$host = $userdata[3]; 
					$port = $userdata[4]; 
                                        if ( $proto == "IMAP" ) {
                                                $mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
                                                $boxes = $imap->FetchFolders ( $host, $mbox );
                                                $numboxes = count ( $boxes );
                                                for ( $j = 0; $j < $numboxes; $j++ ) {
                                                        echo "<a CLASS=blue href=\"javascript:onClick=parent.frames[2].location='main.php3?menu=mail&msgdetail=0'; parent.frames[3].location='main.php3?menu=list&folder=$boxes[$j]'\"> | $boxes[$j]  </a>\n";
                                                }
                                                echo "<a CLASS=blue href=main.php3?menu=folders onClick=\"window.self.location='main.php3?menu=mail&msgdetail=0'\" target=bodyframe> | Folders Administration | </a>\n";
                                        } else {
						$mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
					}
					
                                }
                                if ( $msgdetail == 1 ) {
					$userdata = $db->Fetch_login ( $user, $choosehost );
					$login = $userdata[0];
					$pass = $userdata[1];
					$proto = $userdata[2];  
					$host = $userdata[3];
					$port = $userdata[4]; 
                                        echo "<td align=left><font size=-1>
						<a CLASS=blue href=main.php3?menu=compose target=bodyframe> | Compose | </a>
						<a CLASS=blue href=main.php3?menu=compose&act=reply&folder=$folder&msgno=$msgno target=bodyframe> | Reply | </a>
						<a CLASS=blue href=main.php3?menu=compose&act=forward&folder=$folder&msgno=$msgno& target=bodyframe> | Forward | </a>";
					if ( !$m || $m == "msg") {
						echo "<a CLASS=blue href=\"javascript:onClick=parent.frames[3].location='popup.php3?type=mailsource&folder=$folder&msgno=$msgno&m=source'; 
							parent.frames[2].location='main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$msgno&m=source'\"> | View Source | </a>";
					} else if ( $m == "source" ) {
						echo "<a CLASS=blue href=\"javascript:onClick=parent.frames[3].location='imap.php3?folder=$folder&msgno=$msgno&m=msg';
							parent.frames[2].location='main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$msgno&m=msg'\"> | View Message | </a>";
					}	
					echo 	"</font></td>\n";
                                        echo "<td align=right><font size=-1>\n";
                                        if ( $msgno > 1 ) {
                                                $prev = $msgno - 1;
                                                echo "<a CLASS=blue href=imap.php3?folder=$folder&msgno=$prev 
							onClick=\"window.self.location='main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$prev'\" 
							target=bodyframe> | Previous Message | </a>\n";
                                        }
                                        if ( $proto == "IMAP" ) {
                                                $mbox = $imap->InitFolder ( $host, $port, $proto, $login, $pass, $folder );
                                        } else {
                                                $mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
                                        }
                                        $top = $imap->NumMesg ( $mbox );
                                        if ( $top > $msgno ) {
                                                $next = $msgno + 1;
                                                echo "<a CLASS=blue href=imap.php3?folder=$folder&msgno=$next 
							onClick=\"window.self.location='main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$next'\" 
							target=bodyframe> | Next Message | </a>\n";
                                        }
                                        echo "<a CLASS=blue href=main.php3?folder=INBOX&menu=list 
						onClick=\"window.self.location='main.php3?menu=mail&msgdetail=0'\" target=bodyframe>| Inbox | </a>\n";
                                } 
                                echo "</td></tr>\n";
                                echo "</table></body>\n";
                                echo "</html>\n";
                                break;;
		case "list" :	
				if ( !$sorttype ) {
					$sorttype = "date";
				}
				if ( $reverse == 0) {
					$reverse = 1;
				} else {
					$reverse = 0;
				}	
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );	
				$form = new FormCl;
				$userdata = $db->Fetch_login ( $user, $choosehost );
                                $login = $userdata[0];
                                $pass = $userdata[1];
                                $proto = $userdata[2];
                                $host = $userdata[3];
                                $port = $userdata[4];
				if ( $proto == "IMAP" ) {
                                	$mbox = $imap->InitFolder ( $host, $port, $proto, $login, $pass, $folder );
                                } else {
                                	$mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
                                }
				//$numsg = $imap->NumMesg ( $mbox );
				$msgnos = $imap->sort( $mbox, stripslashes ( $sorttype ), $reverse );
				$numsg = count ( $msgnos );
				$html->HtmlHeader ( $folder , "" );
				$form->Init ( "main.php3", "POST", "mail" );
				echo "<table border=0 width=100%>\n";
				echo "<tr bgcolor=dimgray><td align=center><font size=-1 color=white>New</font></td>\n";
				echo "<td align=center><font size=-1>&nbsp</font></td>\n";
				echo "<td align=center><font size=-1 color=white>Nr</font></td>\n";
				echo "<td align=center><font size=-1><a CLASS=white href=main.php3?menu=list&folder=$folder&sorttype=subject&reverse=$reverse>Subject</a></font></td>\n";
				echo "<td align=center><font size=-1><a CLASS=white href=main.php3?menu=list&folder=$folder&sorttype=from&reverse=$reverse>From</a></font></td>\n";
				echo "<td align=center><font size=-1><a CLASS=white href=main.php3?menu=list&folder=$folder&sorttype=date&reverse=$reverse>Date</a></font></td>\n";
				echo "</tr>\n";
				for ( $i = 0; $i < $numsg; $i++ ) {
					$count = $i + 1;
					$msgno = $msgnos[$i];
					$header = $imap->FetchHeader ( $mbox, $msgno);
					echo "<tr bgcolor=lightgrey>\n";
					$flag = $imap->FetchHeaderData ( $header, "FLAGS" );
					switch ( $flag ) {
						case "N" : echo "<td align=center><font size=-1 color=red>New</font></td>\n"; break;;
						case "R" : echo "<td align=center><font size=-1 color=red>New</font></td>\n"; break;;
						case "U" : echo "<td align=center><font size=-1 color=red>New</font></td>\n"; break;;
						default  : echo "<td align=center><font size=-1>&nbsp</font></td>\n"; break;; 
					}
					echo "<td align=center><font size=-1>";
					$form->checkbox ( "index[$msgno]", 2, $msgno );
					echo "</font></td>\n";
					echo "<td align=right><font size=-1>
						<a CLASS=blue href=main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$msgno 
						onClick=\"window.self.location='imap.php3?folder=$folder&msgno=$msgno'\" 
						target=dependframe>$count</a></font></td>\n";
					$subject = $imap->FetchHeaderData ( $header, "SUBJECT" );
					if ( $subject == "" ) { // If we don't have a subject make 'No Subject' as subject
						$subject = "No Subject"; 
					}
					$tmp = $subject;
					$subject = $imap->ConvertEnc ( $tmp );
					echo "<td align=left><font size=-1>
						<a CLASS=blue href=main.php3?menu=mail&msgdetail=1&folder=$folder&msgno=$msgno 
						onClick=\"window.self.location='imap.php3?folder=$folder&msgno=$msgno'\" 
						target=dependframe>$subject</a></font></td>\n";
					$tmp = $imap->FetchHeaderData ( $header, "FROM" );
					$from = $tmp[0];
					$query =  "SELECT email FROM addresses WHERE email='$from->address' AND owner='$user_id'";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					if ( $numrows != 0 ) { 
						echo "<td align=left><font size=-1>$from->name&lt;
							<a CLASS=blue href=main.php3?menu=compose&to=$from->address>$from->address</a>&gt;</font></td>\n";
					} else {
						echo "<td align=left><font size=-1>$from->name&lt;
							<a CLASS=blue href=\"javascript: DoWhatWithemail ( '$from->address' )\">$from->address</a>&gt;</font>
							</td>\n";
					}
					$date = $imap->FetchHeaderData ( $header, "DATE" );
					echo "<td align=left><font size=-1>$date</a></font></td>\n";	
					$count++;
					echo "</tr>\n";
				}

				echo "</table>\n";
				if ( $proto == "IMAP" ) {
					$form->submit ( "transfer", "DELETE" );
        				echo " Selected Messages, OR \n";
					$form->submit ( "transfer", "MOVE" );
        				echo " OR \n";
					$form->submit ( "transfer", "COPY" );
        				echo " Selected Messages TO \n";
					$boxes = $imap->FetchFolders ( $host, $mbox );
					$form->simpleselect ( "destination", $boxes, $boxes, 0 );
				} else {
					$form->submit ( "transfer", "DELETE" );
        				echo " Selected Messages \n";
				}	
        			$form->hidden ( "folder", "$folder" );
				$form->close ( );
				$html->HtmlFooter ( );
				break;;
		case "compose"  :
				$title = "Compose New Message";
				$query = "SELECT email FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$from = $db->result ( $result, 0, "email" );
				if ( $act == "forward" ) {
					$title = "Forward Message";
					$userdata = $db->Fetch_login ( $user, $choosehost );
                                	$login = $userdata[0];
                                	$pass = $userdata[1];
                                	$proto = $userdata[2];
                                	$host = $userdata[3];
                                	$port = $userdata[4];
                                	if ( $proto == "IMAP" ) {
                                        	$mbox = $imap->InitFolder ( $host, $port, $proto, $login, $pass, $folder );
                                	} else {
                                        	$mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
                                	}
					$this_part = imap_fetchstructure ( $mbox, $msgno );
					$struct = $imap->FetchpParts ( $mbox, $folder, $msgno, $this_part, 1 );
					$structparts = explode ( ", ", $struct );
					$numstructparts = count ( $structparts );
					for ( $i = 1; $i < $numstructparts; $i = $i + 3 ) {
						$j = $i + 1;
						$k = $i + 2;
						if ( $k == 3 ) {
							$names = $structparts[$k];
						} else {
							$names = $names . ", " . $structparts[$k];
						}
						if ( $structparts[$i] == 1 ) {
                        				$part = $structparts[$i];
                				} else {
                        				if ( strpos ( $structparts[$i], "." ) ) {
                                				$tmp = explode ( ".", $structparts[$i] );
								$counttmp = count ( $tmp ) ;
								for ( $count = 1; $count < $counttmp; $count++ ) {
									if ( $count == 1 ) {
                                						$part = $tmp[1];
									} else {
										$part = $part . "." . $tmp[$count];
									}
								}
                        				}
                				}
						$text = imap_fetchbody ( $mbox, $msgno, $part );
						$decoded_part = imap_base64 ( $text );
						$fp = fopen ( $uploadpath .$structparts[$k], "w" );
						fwrite ( $fp, $decoded_part );
						fclose ( $fp );
						
					}
					$headers = $imap->FetchHeader ( $mbox, $msgno );
					$subject = "[Fwd:] " . $imap->FetchHeaderData ( $headers, "subject" );
					$tmp = $imap->FetchHeaderData ( $headers, "date" );
					$dateparts = split( " ", $tmp );
					$olddate = $dateparts[0] . " " . $dateparts[1] . " " . $dateparts[2] . " " . $dateparts[3]; 
					$tmp = $imap->FetchHeaderData ( $headers, "from" );
					if ( is_array ( $tmp ) ) {
						$numto = count ( $tmp );
						for ( $i = 0; $i < $numto; $i++ ) {
							if ( $i == 0 ) {
								$fromorig = $tmp[$i]->address;
							} else {
								$fromorig = $fromorig . ", " . $tmp[$i]->address;
							}
						}
					}
					$txt = $imap->get_part ( $mbox, $msgno, "TEXT/PLAIN" );
					$lines = split("[\n]", $txt);
					$numlines = count ( $lines );
					$body = "---Forwarded Message---\r\n";
					$body = $body . "From: " . $fromorig . "\r\n";
					$body = $body . "Subject: " . $imap->FetchHeaderData ( $headers, "subject" ) . "\r\n";
					$body = $body . "Date: $olddate\r\n";
					$body = $body . "\r\n";
					
					for ($j = 0; $j < $numlines; $j++) {
						$body = $body . ">" . $lines[$j];	
					}
				}
					
				if ( $act == "reply" ) {
					$title = "Reply to message";
					$userdata = $db->Fetch_login ( $user, $choosehost );
                                	$login = $userdata[0];
                                	$pass = $userdata[1];
                                	$proto = $userdata[2];
                                	$host = $userdata[3];
                                	$port = $userdata[4];
                                	if ( $proto == "IMAP" ) {
                                        	$mbox = $imap->InitFolder ( $host, $port, $proto, $login, $pass, $folder );
                                	} else {
                                        	$mbox = $imap->Init ( $host, $port, $proto, $login, $pass );
                                	}
					$headers = $imap->FetchHeader ( $mbox, $msgno );
					$subject = "[Re:] " . $imap->FetchHeaderData ( $headers, "subject" );
					$tmp = $imap->FetchHeaderData ( $headers, "date" );
					$dateparts = split( " ", $tmp );
					$olddate = $dateparts[0] . " " . $dateparts[1] . " " . $dateparts[2] . " " . $dateparts[3]; 
					$tmp = $imap->FetchHeaderData ( $headers, "from" );
					if ( is_array ( $tmp ) ) {
						$numto = count ( $tmp );
						for ( $i = 0; $i < $numto; $i++ ) {
							if ( $i == 0 ) {
								$to = $tmp[$i]->address;
							} else {
								$to = $to . ", " . $tmp[$i]->address;
							}
						}
					}
					$tmp = $imap->FetchHeaderData ( $headers, "to" );
					if ( is_array ( $tmp ) ) {
                                                $numcc = count ( $tmp );
						
                                                for ( $i = 0; $i < $numcc; $i++ ) {
							if ( $tmp[$i]->address != $from ) {
                                                        	if ( $i == 0 ) {
                                                                	$cc = $tmp[$i]->address;
                                                        	} else {
                                                                	$cc = $cc . ", " . $tmp[$i]->address;
                                                        	}
                                                	}
						}
                                        }
					$text = imap_fetchbody ( $mbox, $msgno, 1 );
					$lines = split("[\n]", $text);
					$numlines = count ( $lines );
					$body = "On $olddate You wrote:\r\n";
					for ($j = 0; $j < $numlines; $j++) {
						$body = $body . ">" . $lines[$j];	
					}
				}
				if ( $newatt ) {
					if (is_upload_file($newatt)) {
						copy($newatt,$uploadpath . $newatt_name);
					} else {
						echo "Could not load file...<p>\n";
					}
					$file[0]->name = $newatt_name;
					$file[0]->tmp = $newatt;
					$gr = $file[0];
					if ( $j == 0 ) {
						$tmpfiles = $gr->tmp;
						$names = $gr->name;
					} else {
						$tmpfiles = $tmpfiles . ", " . $gr->tmp;
						$names = $names . ", " . $gr->name;
					}
					$j++;
				} else {
					$j = 0;
				}
				$form =new FormCl;
				$html->htmlheader ( $title, 0 );
				$listto = str_replace (",", ":", $to );
				$listcc = str_replace (",", ":", $cc );
				$listbcc = str_replace (",", ":", $bcc );
				echo "<table border=0>\n";
				echo "<form enctype=multipart/form-data action=main.php3 method=post name=composeform>\n";
				echo "<tr><td>From</td><td> : </td><td>\n";
				$form->text ( "from", $from, 120, 25, 0 );
				echo "</td></tr>\n";
				echo "<tr><td>To</td><td : </td><td>\n";
				$form->text ( "to", $to, 120, 25, 0 );
				echo "<a href='javascript: GetAddressList (\"$listto\", \"$listcc\", \"$listbcc\", \"to\", \"$names\", \"$folder\", \"$msgno\", \"$act\")'>Add</a>\n";
				echo "</td></tr>\n";
				echo "<tr><td>Cc</td><td : </td><td>\n";
				$form->text ( "cc", $cc , 120, 25, 0 );
				echo "<a href='javascript: GetAddressList (\"$listto\", \"$listcc\", \"$listbcc\", \"cc\", \"$names\", \"$folder\", \"$msgno\", \"$act\")'>Add</a>\n";
				echo "</td></tr>\n";
				echo "<tr><td>Bcc</td><td : </td><td>\n";
				$form->text ( "bcc", $bcc, 120, 25, 0 );
				echo "<a href='javascript: GetAddressList (\"$listto\", \"$listcc\", \"$listbcc\", \"bcc\", \"$names\", \"$folder\", \"$msgno\", \"$act\")'>Add</a>\n";
				echo "</td></tr>\n";
				echo "<tr><td>Subject</td><td : </td><td>\n";
				$form->text ( "subject", "$subject", 120, 70, 0 );
				echo "</td></tr>\n";
				$form->hidden ( "MAX_FILE_SIZE", "5000000" );
				$form->hidden ( "j", $j );
				$form->hidden ( "names",$names );
				$form->hidden ( "act",$act );
				$form->hidden ( "folder",$folder );
				$form->hidden ( "msgno",$msgno );
				$form->hidden ( "tmpfiles", $tmpfiles );
				echo "<tr><td>Attachments</td><td> : </td><td>$names</td><td>\n";
				echo "</td></tr><tr><td>&nbsp</td><td>&nbsp</td><td>\n";
				$form->file ( "newatt", 40 );
				$form->submit ( "addatt", "ADD" );
				echo "</td></tr>\n";
				echo "<tr><td colspan=3>\n";
				$form->textarea ( "body", $body, 80, 10 );
				echo "</td></tr>\n";
				echo "<tr><td colspan=3 align=center>\n";
				$form->submit ( "send", "SEND" );
				echo "</td></tr>\n";
				$form->close();
				echo "</table>\n";	
				$html->htmlfooter ();
				break;;
		case "folders" :
				$form = new FormCl;
				$userdata = $db->Fetch_login ( $user, $choosehost );
                                $login = $userdata[0];
                                $pass = $userdata[1];
                                $proto = $userdata[2];
                                $host = $userdata[3];
                                $port = $userdata[4];
				$mbox = $imap->init ( $host, $port, $proto, $login, $pass );
                        	echo "<html><head><title>Folders of $user</title></head><body bgcolor=white link=blue alink=blue vlink=blue>\n";
                        	echo "<center><font=+2><b><u>Create/Delete Folders</u></b></font></center><br>\n";
				$form->init ( "main.php3", "POST", "mail" );
				$form->text ( "newbox", "", 16, 16, 0 );
				$form->submit ( "menu" , "Create Box" );
				$form->close();
				$form->init ( "main.php3", "POST", "mail" );
				$list = $imap->FetchFolders ( $host, $mbox );
				$form->SimpleSelect ( "selmbox", $list, $list, 0 );
				$form->submit ( "menu", "Delete Box" );
				$form->close ();
                        	echo "</body></html>\n";
                        	break;;
		case "Create Box" :
				$form = new FormCl;
                                $userdata = $db->Fetch_login ( $user, $choosehost );
                                $login = $userdata[0];
                                $pass = $userdata[1];
                                $proto = $userdata[2];
                                $host = $userdata[3];
                                $port = $userdata[4];
                                $mbox = $imap->init ( $host, $port, $proto, $login, $pass );
				$success = $imap->CreateNewFolder ( $host, $mbox, $newbox );
                                echo "<html><head><title>Folders of $user</title></head><body bgcolor=white link=blue alink=blue vlink=blue>\n";
                                echo "<center><font=+2><b><u>Create/Delete Folders</u></b></font></center><br>\n";
				if ( $success == 1 ) {
                                	echo "Creation of folder \"$newbox\" Succeeded<p>\n";
                        	} else {
                                	echo "Could Not Create Folder \"$newbox\"<p>\n";
                        	}
                                $form->init ( "main.php3", "POST", "mail" );
                                $form->text ( "newbox", "", 16, 16, 0 );
                                $form->submit ( "menu" , "Create Box" );
                                $form->close();
                                $form->init ( "main.php3", "POST", "mail" );
                                $list = $imap->FetchFolders ( $host, $mbox );
                                $form->SimpleSelect ( "selmbox", $list, $list, 0 );
                                $form->submit ( "menu", "Delete Box" );
                                $form->close ();
                                echo "</body></html>\n";
                                break;;
		case "Delete Box" :
                                $form = new FormCl;
                                $userdata = $db->Fetch_login ( $user, $choosehost );
                                $login = $userdata[0];
                                $pass = $userdata[1];
                                $proto = $userdata[2];
                                $host = $userdata[3];
                                $port = $userdata[4];
                                $mbox = $imap->init ( $host, $port, $proto, $login, $pass );
                                $success = $imap->DeleteFolder ( $host, $mbox, $selmbox );
                                echo "<html><head><title>Folders of $user</title></head><body bgcolor=white link=blue alink=blue vlink=blue>\n";
                                echo "<center><font=+2><b><u>Create/Delete Folders</u></b></font></center><br>\n";
                                if ( $success == 1 ) {
                                        echo "Deletion of folder \"$selmbox\" Succeeded<p>\n";
                                } else {
                                        echo "Could Not Delete Folder \"$selmbox\"<p>\n";
                                }
                                $form->init ( "main.php3", "POST", "mail" );
                                $form->text ( "newbox", "", 16, 16, 0 );
                                $form->submit ( "menu" , "Create Box" );
                                $form->close();
                                $form->init ( "main.php3", "POST", "mail" );
                                $list = $imap->FetchFolders ( $host, $mbox );
                                $form->SimpleSelect ( "selmbox", $list, $list, 0 );
                                $form->submit ( "menu", "Delete Box" );
                                $form->close ();
                                echo "</body></html>\n";
                                break;;
		case "prefs" :
				echo "<html><head>\n";
				echo "<style type=\"text/css\">\n";
                                echo "<!--\n";
                                echo "body { margin: 0 }\n";
				echo "A{text-decoration:none}\n";
                                echo "-->\n";
                                echo "</style>\n";
				echo "<title>prefs</title></head><body bgcolor=darkseagreen text=blue vlink=blue alink=blue link=blue marginheight=0>\n";
				echo "<table border=0 width=100%><tr><td align=right><font size=-1>
					<a href=\"javascript:onClick=parent.frames[2].location='main.php3?menu=prefs'; parent.frames[3].location='groupadmin.php3?menu=setup'\">
					| Groups Setup |</a> 
					<a href=\"javascript:onClick=parent.frames[2].location='main.php3?menu=prefs'; parent.frames[3].location='main.php3?prefs=mailsetup'\">
					| Mail Setup |</a> </font></td></tr></table>\n";
				echo "</body></html>\n";
				break;;

		}
	}
if ( $transfer ) {
	switch ( $transfer ) {
		case "DELETE": 
			$userdata = $db->Fetch_login ( $user, $choosehost );
                        $loginname = $userdata[0];
                        $pass = $userdata[1];
                        $proto = $userdata[2];
                        $host = $userdata[3];
                        $port = $userdata[4];
                        if ( $proto == "IMAP" ) {
                        	$mbox = $imap->InitFolder ( $host, $port, $proto, $loginname, $pass, $folder );
                        } else {
                        	$mbox = $imap->Init ( $host, $port, $proto, $loginname, $pass );
                        }
                        $numsg = $imap->NumMesg ( $mbox );
			$j = 0;
			if ( $numsg > 1 ) {
				for ( $i = 0; $i <= $numsg; $i++ ) {
					if ( $index[$i] ) {
                                        	$value[$j] = $index[$i];
                                        	$j++;
                               	 	}	
				} 
				$msgstr = implode ( ",", $value );	
			} else {
				$msgstr = 1;	
			}
			$imap->TransferMsg ( $host, $mbox, $msgstr, "DELETE", $destination );
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?menu=list&folder=$folder" );
			break;;
		case "MOVE": 
			$userdata = $db->Fetch_login ( $user, $choosehost );
                        $loginname = $userdata[0];
                        $pass = $userdata[1];
                        $proto = $userdata[2];
                        $host = $userdata[3];
                        $port = $userdata[4];
                        if ( $proto == "IMAP" ) {
                        	$mbox = $imap->InitFolder ( $host, $port, $proto, $loginname, $pass, $folder );
                        } else {
                        	$mbox = $imap->Init ( $host, $port, $proto, $loginname, $pass );
                        }
                        $numsg = $imap->NumMesg ( $mbox );
			$j = 0;
			if ( $numsg > 1 ) {
				for ( $i = 0; $i <= $numsg; $i++ ) {
					if ( $index[$i] ) {
                                        	$value[$j] = $index[$i];
                                        	$j++;
                                	}	
				}
				$msgstr = implode ( ",", $value );
			} else {
				$msgstr = 1;	
			}
			$imap->TransferMsg ( $host, $mbox, $msgstr, "MOVE", $destination );
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?menu=list&folder=$folder" );
			break;;
		case "COPY": 
			$userdata = $db->Fetch_login ( $user, $choosehost );
                        $loginname = $userdata[0];
                        $pass = $userdata[1];
                        $proto = $userdata[2];
                        $host = $userdata[3];
                        $port = $userdata[4];
                        if ( $proto == "IMAP" ) {
                        	$mbox = $imap->InitFolder ( $host, $port, $proto, $loginname, $pass, $folder );
                        } else {
                        	$mbox = $imap->Init ( $host, $port, $proto, $loginname, $pass );
                        }
                        $numsg = $imap->NumMesg ( $mbox );
			$j = 0;
			if ( $numsg > 1 ) {
				for ( $i = 0; $i <= $numsg; $i++ ) {
					if ( $index[$i] ) {
                                        	$value[$j] = $index[$i];
                                        	$j++;
                                	}	
				}
				$msgstr = implode ( ",", $value );
			} else {
				$msgstr = 1;
			}	
			$imap->TransferMsg ( $host, $mbox, $msgstr, "COPY", $destination );
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?menu=list&folder=$folder" );
			break;;
	}
}

if ( $prefs ) {
	switch ( $prefs ) {
		case "Go" :
				$choosehost = $mailhost;
				//setcookie ("choosehost", $mailhost );
				$html->HtmlHeader ( "User Preferences", 0 );
                                echo "Choose a different mail account for this session: \n";
                                $form = new FormCl;
                                $accounts = $db->FetchAccountnames ( $user );
                                $numacc = count ( $accounts );
                                for ( $i = 0; $i <  $numacc; $i++ ) {
                                        $acc = $accounts[$i];
                                        $account[$i] = $acc->server;
                                }
                                $form->init ( "main.php3", "POST","prefs" );
                                $form->simpleselect ( "mailhost", $account, $account, $mailhost);
                                $form->submit ( "prefs", "Go" );
                                $form->close();
				echo "<hr noshade>\n";
				echo "Change Password<p>";
                                $form->init ( "main.php3", "POST","prefs" );
				echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  Enter Password : \n";
				$form->text ( "newpass", '', 16, 16, 1 ); 
				echo "<p>Enter Password Again:\n";
				$form->text ( "check_newpass", '', 16, 16, 1 ); 
				$form->submit ( "prefs", "Change Password" );
				$form->close();
				echo "<hr noshade>\n";
				echo "Change Your Email Address<p>\n";
				$query = "SELECT email FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$emailaddr = $db->result ( $result, 0, "email" );
				$form->init ( "main.php3", "POST","prefs" );
				$form->text ( "new_emailaddr", $emailaddr, 40, 40, 0 );
				$form->submit ( "prefs", "Change Email Address" );
				$form->close ();
				echo "<hr noshade>\n";
                                $html->HtmlFooter ();
                                break;;
		case "startup":
				$html->HtmlHeader ( "User Preferences", 0 );
				echo "Choose a different mail account for this session: \n";
				$form = new FormCl;
				$accounts = $db->FetchAccountnames ( $user );
				$numacc = count ( $accounts );
				for ( $i = 0; $i <  $numacc; $i++ ) {
					$acc = $accounts[$i];
					$account[$i] = $acc->server;
				}
				$form->init ( "main.php3", "POST","prefs" );
				$form->simpleselect ( "mailhost", $account, $account, $choosehost);
				$form->submit ( "prefs", "Go" );
				$form->close();
				echo "<hr noshade>\n";
				echo "Change Password<p>";
                                $form->init ( "main.php3", "POST","prefs" );
				echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enter Password : \n";
				$form->text ( "newpass", '', 16, 16, 1 ); 
				echo "<p>Enter Password Again:\n";
				$form->text ( "check_newpass", '', 16, 16, 1 ); 
				$form->submit ( "prefs", "Change Password" );
				$form->close();
				echo "<hr noshade>\n";
				echo "Change Your Email Address<p>\n";
				$query = "SELECT email FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$emailaddr = $db->result ( $result, 0, "email" );
				$form->init ( "main.php3", "POST","prefs" );
				$form->text ( "new_emailaddr", $emailaddr, 40, 40, 0 );
				$form->submit ( "prefs", "Change Email Address" );
				$form->close ();
				echo "<hr noshade>\n";
				$html->HtmlFooter ();
				break;;
		case "Change Email Address":
				$query = "UPDATE users SET email='$new_emailaddr' WHERE user='$user'";
				$result = $db->query ( $query );
				$html->HtmlHeader ( "User Preferences", 0 );
				echo "Choose a different mail account for this session: \n";
				$form = new FormCl;
				$accounts = $db->FetchAccountnames ( $user );
				$numacc = count ( $accounts );
				for ( $i = 0; $i <  $numacc; $i++ ) {
					$acc = $accounts[$i];
					$account[$i] = $acc->server;
				}
				$form->init ( "main.php3", "POST","prefs" );
				$form->simpleselect ( "mailhost", $account, $account, $choosehost);
				$form->submit ( "prefs", "Go" );
				$form->close();
				echo "<hr noshade>\n";
				echo "Change Password<p>";
                                $form->init ( "main.php3", "POST","prefs" );
				echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enter Password : \n";
				$form->text ( "newpass", '', 16, 16, 1 ); 
				echo "<p>Enter Password Again:\n";
				$form->text ( "check_newpass", '', 16, 16, 1 ); 
				$form->submit ( "prefs", "Change Password" );
				$form->close();
				echo "<hr noshade>\n";
				echo "Change Your Email Address<p>\n";
				$query = "SELECT email FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$emailaddr = $db->result ( $result, 0, "email" );
				$form->init ( "main.php3", "POST","prefs" );
				$form->text ( "new_emailaddr", $emailaddr, 40, 40, 0 );
				$form->submit ( "prefs", "Change Email Address" );
				$form->close ();
				echo "<hr noshade>\n";
				$html->HtmlFooter ();
				break;;
		case "Change Password":
				if ( strcmp ( $newpass, $check_newpass ) == 0 ) {
					$query = "UPDATE users set passwd=PASSWORD('$newpass') WHERE user='$user'";
					$result = $db->query ( $query );
					$html->HtmlHeader ( "User Preferences", 0 );
					echo "Choose a different mail account for this session: \n";
					$form = new FormCl;
					$accounts = $db->FetchAccountnames ( $user );
					$numacc = count ( $accounts );
					for ( $i = 0; $i <  $numacc; $i++ ) {
						$acc = $accounts[$i];
						$account[$i] = $acc->server;
					}
					$form->init ( "main.php3", "POST","prefs" );
					$form->simpleselect ( "mailhost", $account, $account, $choosehost);
					$form->submit ( "prefs", "Go" );
					$form->close();
					echo "<hr noshade>\n";
					echo "Change Password<p>";
                                	$form->init ( "main.php3", "POST","prefs" );
					echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enter Password : \n";
					$form->text ( "newpass", '', 16, 16, 1 ); 
					echo "<p>Enter Password Again:\n";
					$form->text ( "check_newpass", '', 16, 16, 1 ); 
					$form->submit ( "prefs", "Change Password" );
					$form->close();
					echo "<hr noshade>\n";
					echo "Change Your Email Address<p>\n";
					$query = "SELECT email FROM users WHERE user='$user'";
					$result = $db->query ( $query );
					$emailaddr = $db->result ( $result, 0, "email" );
					$form->init ( "main.php3", "POST","prefs" );
					$form->text ( "new_emailaddr", $emailaddr, 40, 40, 0 );
					$form->submit ( "prefs", "Change Email Address" );
					$form->close ();
					echo "<hr noshade>\n";
				} else {
					$html->HtmlHeader ( "User Preferences", 0 );
					echo "Choose a different mail account for this session: \n";
					$form = new FormCl;
					$accounts = $db->FetchAccountnames ( $user );
					$numacc = count ( $accounts );
					for ( $i = 0; $i <  $numacc; $i++ ) {
						$acc = $accounts[$i];
						$account[$i] = $acc->server;
					}
					$form->init ( "main.php3", "POST","prefs" );
					$form->simpleselect ( "mailhost", $account, $account, $choosehost);
					$form->submit ( "prefs", "Go" );
					$form->close();
					echo "<hr noshade>\n";
					echo "Change Password<p>";
					echo "<font color=red size=-1>Entries Do Not Match ! Please enter password again</font><p>\n";
                                	$form->init ( "main.php3", "POST","prefs" );
					echo "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Enter Password : \n";
					$form->text ( "newpass", '', 16, 16, 1 ); 
					echo "<p>Enter Password Again:\n";
					$form->text ( "check_newpass", '', 16, 16, 1 ); 
					$form->submit ( "prefs", "Change Password" );
					$form->close();
					echo "<hr noshade>\n";
					echo "Change Your Email Address<p>\n";
					$query = "SELECT email FROM users WHERE user='$user'";
					$result = $db->query ( $query );
					$emailaddr = $db->result ( $result, 0, "email" );
					$form->init ( "main.php3", "POST","prefs" );
					$form->text ( "new_emailaddr", $emailaddr, 40, 40, 0 );
					$form->submit ( "prefs", "Change Email Address" );
					$form->close ();
					echo "<hr noshade>\n";
				}
				$html->HtmlFooter ();
				break;;
		case "Delete Account" :
				$vars->account_id = $account_id;
                                $vars->user = $accuser;
                                $vars->server = $accserver;
                                $vars->port = $accport;
                                $vars->proto = $accproto;
                                $vars->loginname = $accloginname;
				$db->DeleteEmailAccount ( $vars );
				header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?prefs=mailsetup" );
				break;;	
		case "mailsetup":
				$form = new FormCl;
				$html->HtmlHeader ( "Mail Account Setup For $user", "0" );
				echo "<hr noshade>\n";
				echo "<center><font size=+1>Accounts Overview</font></center><br>\n";
				$accounts = $db->FetchAllAccounts ( $user );
				if ( $accounts[0] == 0 ) {
					echo "<center><font size=-1>No Mail Accounts set in this profile</font></center><p>\n";
				} else {
					echo "<center><table border=0 cellpading=0 cellspacing=0>\n";
					echo "<tr bgcolor=dimgray><td align=center><font color=white size=-1>Mail Host</font></td>\n";
					echo "<td align=center><font color=white size=-1>Port</font></td>\n";
					echo "<td align=center><font color=white size=-1>Server Type</font></td>\n";
					echo "<td align=center><font color=white size=-1>Login</font></td>\n";
					echo "<td align=center><font color=white size=-1>Password</font></td>\n";
					echo "<td align=center><font color=white size=-1>Default Server</font></td>\n";
					echo "<td>&nbsp;</td>\n";
					echo "<td>&nbsp;</td>\n";
					
					$numacc = count ( $accounts );
					for ( $i = 0; $i <  $numacc; $i++ ) {
						$acc = $accounts[$i];
						$form->init ( "main.php3", "POST", "prefs" );
						echo "<tr bgcolor=lightgrey><td align=center><font size=+1 face=clean><small>\n";
						$form->text ( "accserver", $acc->server, 80, 16, 0 );
						echo "</small></font></td><td align=center><font size=+1 face=clean><small>\n";
						$form->text ( "accport", $acc->port, 5, 5, 0 );
						echo "</small></font></td><td align=center><font size=-1>\n";
						$protoarr = array ( "IMAP", "POP3" );
						$form->SimpleSelect ( "accproto", $protoarr, $protoarr, "$acc->proto" );
						echo "</font></td><td align=center><font size=+1 face=clean><small>\n";
						$form->text ( "accloginname", $acc->loginname, 16, 16, 0 );
						echo "</small></font></td><td align=center><font size=+1 face=clean><small>\n";
						$form->text ( "accpassword", $acc->password, 16, 16, 1 );
						echo "</small></font></td><td align=center><font size=-1>\n";
						if ( $acc->defacc == "Y" ) {
							echo "<input type=checkbox name=\"defacc\" value=\"Y\" checked>\n";
						} else {
							echo "<input type=checkbox name=\"defacc\" value=\"Y\">\n";
						}
						echo "</font></td><td align=center><font size=-1>\n";
						$form->hidden ( "account_id", $acc->account_id );
						$form->hidden ( "accuser", $acc->user_id );
						$form->submit ( "prefs", "Delete Account" );
						echo "</font></td><td align=center><font size=-1>\n";
						$form->submit ( "prefs", "Update Account" );
						echo "</font></td></tr>\n";
						$form->close ();
						echo "<tr bgcolor=dimgray><td colspan=8><font size=-4>&nbsp;</font></td></tr>\n";
					}
					echo "</table>\n";
				}
				echo "<hr noshade>\n";
				echo "<center><font size=+1>Create New Account</font></center><br>\n";
				$form->init ( "main.php3", "POST", "prefs" );
				echo "<table border=0>\n";
				echo "<tr><td><font size=-1>Server</font></td><td><font size=-1> : </font></td><td><font size=+1 face=clean><small>
\n";
				$form->text( "accserver", "", 80, 16, 0 );
				echo "</small></font></td>\n"; 
				echo "<td><font size=-1>Port</font></td><td><font size=0> : </font></td><td><font size=+1 face=clean><small>\n";
				$form->text ( "accport", "", 5, 5, 0 );
				echo "</small></font></td>\n";
				echo "<td><font size=-1>Server Type</font></td><td><font size=-1> : </font></td><td><font size=-1>\n";
				$proto = array ( "IMAP", "POP3" );
				$form->SimpleSelect ( "accproto", $proto, $proto, "IMAP" );
				echo "</font></td></tr><tr>\n";
				echo "<td><font size=-1>Login</font></td><td><font size=-1> : </font></td><td><font size=+1 face=clean><small>\n";
				$form->text ( "accloginname", "", 80, 16, 0 );
				echo "</small></font></td>\n";
				echo "<td><font size=-1>Password</font></td><td><fontsize=-1> : </font></td><td><font size=+1 face=clean><small>\n";
				$form->text ( "accpassword", "", 16, 16, 1 );
				echo "</small></font></td></tr><tr><td colspan=9 align=center><font size=-1>\n";
				$form->submit ( "prefs", "Create Account" );
				echo "</font></td></tr>\n";
				echo "</table>\n";
				$form->close ( );	
				echo "<hr noshade>\n";
				$html->HtmlFooter();
				break;;
		case "Create Account" :
				$crypt = new MCryptCl;
				$encrpasswd = $crypt->Encrypt ( $accpassword );
				$vars->user = $accuser;
				$vars->server = $accserver;
				$vars->port = $accport;
				$vars->proto = $accproto;
				$vars->loginname = $accloginname;
				$vars->encrpass = $encrpasswd;
				$success = $db->CreateAccount ( $vars, $user );	
				if ( $success == 1 ) {
					header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?prefs=mailsetup" );
				} else {
					header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?prefs=mailsetup" );
					echo "Account could not be created\n";
				}
				break;;
		case "Update Account" :
				$crypt = new MCryptCl;
				$encrpasswd = $crypt->Encrypt ( $accpassword );
				$vars->account_id = $account_id;
				$vars->user = $accuser;
				$vars->server = $accserver;
				$vars->port = $accport;
				$vars->proto = $accproto;
				$vars->loginname = $accloginname;
				$vars->encrpass = $encrpasswd;
				if ( $defacc != "Y" ) { 
					$vars->defacc = "N";
				} else {
					$vars->defacc = "Y";
				}
				$db->UpdateAccount ( $vars );
				header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/main.php3?prefs=mailsetup" );
				break;;
	}
}


Function getmime ( $file ) {
		$name = GetFileName ( $file );
		$list = split ( "\.", $name );
		$numlist = count ( $list );
		$end = $numlist - 1;
		$ext = strtoupper ( $list[$end] );
		switch ( $ext ) {
			case "TXT" : $mime = "text/plain"; break;;
			case "ASC" : $mime = "text/plain"; break;;
			case "HTML" : $mime = "text/html"; break;;
			case "HTM" : $mime = "text/html"; break;;
			case "CSS" : $mime = "text/css"; break;;
			case "RTF" : $mime = "text/rtf"; break;;
			case "RTX" : $mime = "text/rtx"; break;;
			case "SGML" : $mime = "text/sgml"; break;;
			case "SGM" : $mime = "text/sgml"; break;;
			case "XML" : $mime = "text/xml"; break;;
			case "HQX" : $mime = "application/mac-binhex40"; break;;
			case "CPT" : $mime = "application/mac-compactpro"; break;;
			case "DOC" : $mime = "application/msword"; break;;
			case "BIN" : $mime = "application/octet-stream"; break;;
			case "DMS" : $mime = "application/octet-stream"; break;;
			case "LHA" : $mime = "application/octet-stream"; break;;
			case "LZH" : $mime = "application/octet-stream"; break;;
			case "EXE" : $mime = "application/octet-stream"; break;;
			case "CLASS" : $mime = "application/octet-stream"; break;;
			case "PDF" : $mime = "application/pdf"; break;;
			case "AI" : $mime = "application/postscript"; break;;
			case "EPS" : $mime = "application/postscript"; break;;
			case "PS" : $mime = "application/postscript"; break;;
			case "PPT" : $mime = "application/ms-powerpoint"; break;;
			case "XLS" : $mime = "application/ms-excel"; break;;
			case "BCPIO" : $mime = "application/x-cpio"; break;;
			case "BZ2" : $mime = "application/x-bzip2"; break;;
			case "VCD" : $mime = "application/x-cdlink"; break;;
			case "CPIO" : $mime = "application/x-cpio"; break;;
			case "CSH" : $mime = "application/x-csh"; break;;
			case "DVI" : $mime = "application/x-dvi"; break;;
			case "SPL" : $mime = "application/x-futuresplash"; break;;
			case "GTAR" : $mime = "application/x-gtar"; break;;
			case "GZ" : $mime = "application/x-gzip"; break;;
			case "TGZ" : $mime = "application/x-gzip"; break;;
			case "JS" : $mime = "application/x-javascript"; break;;
			case "LATEX" : $mime = "application/x-latex"; break;;
			case "SH" : $mime = "application/x-sh";break;;
			case "SWF" : $mime = "application/x-shockwave-flash"; break;;
			case "SIT" : $mime = "application/x-stuffit"; break;;
			case "TAR" : $mime = "application/x-tar"; break;;
			case "TCL" : $mime = "application/x-tcl"; break;;
			case "TEX" : $mime = "application/x-tex"; break;;
			case "TEXINFO" : $mime = "application/x-texinfo"; break;;
			case "TEXI" : $mime = "application/x-texinfo"; break;;
			case "T" : $mime = "application/x-troff"; break;; 			
			case "TR" : $mime = "application/x-troff"; break;; 			
			case "TROFF" : $mime = "application/x-troff"; break;; 			
			case "MAN" : $mime = "application/x-troff-man"; break;;
			case "ZIP" : $mime = "application/zip"; break;;
			case "AU" : $mime = "audio/basic";
			case "SND" : $mime = "audio/basic";
			case "MID" : $mime = "audio/midi"; break;;
			case "MIDI" : $mime = "audio/midi"; break;;
			case "KAR" : $mime = "audio/midi"; break;;
			case "MP3" : $mime = "audio/mpeg"; break;;
			case "MP2" : $mime = "audio/mpeg"; break;;
			case "MPGA" : $mime = "audio/mpeg"; break;;
			case "AIF" : $mime = "audio/x-aiff"; break;;
			case "AIFC" : $mime = "audio/x-aiff"; break;;
			case "AIFF" : $mime = "audio/x-aiff"; break;;
			case "RAM": $mime = "audio/x-pn-realaudio"; break;;
			case "RM": $mime = "audio/x-pn-realaudio"; break;;
			case "RA" : $mime = "audio/x-realaudio"; break;;
			case "WAV" : $mime = "audio/x-wav"; break;;
			case "GIF" : $mime = "image/gif"; break;;
			case "JPEG" : $mime = "image/jpeg"; break;;
			case "JPE" : $mime = "image/jpeg"; break;;
			case "JPG" : $mime = "image/jpeg"; break;;
			case "PNG" : $mime = "image/png"; break;;
			case "TIFF" : $mime = "image/tiff"; break;;
			case "TIF" : $mime = "image/tiff"; break;;
			case "XPM" : $mime = "image/x-xpixmap"; break;;
			case "XBM" : $mime = "image/x-xbitmap"; break;;
			case "WRL" : $mime = "model/vrml"; break;;
			case "VRML" : $mime = "model/vrml"; break;;
			case "MPEG" : $mime = "video/mpeg"; break;;
			case "MPG" : $mime = "video/mpeg"; break;;
			case "MPE" : $mime = "video/mpeg"; break;;
			case "QT" : $mime = "video/quicktime"; break;;
			case "MOV" : $mime = "video/quicktime"; break;;
			case "AVI" : $mime = "video/x-msvideo"; break;;
			case "MOVIE" : $mime = "video/x-sgi-movie"; break;;
			default : $mime = "application/octet-stream"; break;;
		}
		return $mime;
			
}


if ( $send ) {
	switch ( $send ) {
		case "SEND":
			$name_array = explode ( ", ", $names );
			$tmp_array = explode ( ", ", $tmpfiles );
			if ( $names && $names != "" ) {
				$att = count ( $name_array );
			} else {
				$att = 0;
			}
			$m = new CMIMEMail($to, $cc, $bcc, $from, $subject, $att);
			$m->mailbody( $body, "" );
			if ( $name_array[0] ) {
				for ( $i =0; $i < $att; $i++ ) {
					$mimetype = getmime ( $name_array[$i] );
					$filename = $uploadpath . $name_array[$i];
					$m->attachFile($filename ,$mimetype);
					system ( "rm $filename" );
				}
			}
			$email = $m->send( );
			$html->htmlheader( "",0);
			echo "<center><h1>mail send</h1></center>\n";
			$html->htmlfooter ();
			break;;	
	}
}	


?>

Return current item: Myphpim