Location: PHPKode > scripts > PHPwebmail > phpwebmail/address.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($sql_host,$sql_user,$sql_pw)) {
	@mysql_select_db("phpwebmail");
	$sql_query = "SELECT textmode 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];
      } // if db_link ends
?>
<html>
<head> 
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
</head>
<body bgcolor="<?php echo $bg_color_main; ?>" 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

// main entry point
if ($action == "really") {
// delete all
      if ($db_link = mysql_connect($sql_host,$sql_user,$sql_pw)) {
	@mysql_select_db("phpwebmail");
	$sql_query = "DELETE FROM addresses WHERE user='$name'";
	$result = mysql_query($sql_query);
        // catch SQL errors
        if (mysql_errno()) echo ("ERROR ".mysql_error());
      } // if db_link ends
}

if ($action == "all") {
// ask before deleting all
?>
	<h3>Really delete all addresses?</h3>
	<font face="arial" size=2><b><a href="address.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		  
} else if ($action == "edit") {
// show the address
      if ($db_link = mysql_connect($sql_host,$sql_user,$sql_pw)) {
	@mysql_select_db("phpwebmail");
	$sql_query = "SELECT name, email FROM addresses WHERE user='$name' AND nr=".$nr;
	$result = mysql_query($sql_query);
        // catch SQL errors
        if (mysql_errno()) echo ("ERROR ".mysql_error());
	$row = mysql_fetch_row($result);
	$the_name = $row[0];
	$the_email = $row[1];
?>
	<h3>Change this address:</h3>
	<form target="_self" method="post">
	<table><tr valign="center">
	<td><font face="arial" size=2><b>Name: <input type="text" size=25 name="new_name" value="<?php echo $the_name; ?>"></td>
	<td><font face="arial" size=2><b>Email: <input type="text" size=25 name="new_email" value="<?php echo $the_email; ?>"></td>
	<td><font face="arial" size=2><b>
	<?php if ($textmode) echo "enter"; else echo "<INPUT TYPE=image src=images/_enter.gif border=0 alt=\"enter\">"; ?>
	<input type="hidden" name="action" value="change"><input type="hidden" name="nr" value="<?php echo $nr; ?>"></td>
	</tr></table></form>
<?php		  
      } // if db_link ends
} else { 
//show the table

	echo "<h3>Address book:</h3>";
	if ($from_email) {
		if (strpos($from_email, "&lt;")) {
			$end_name = strpos($from_email, "$lt;");
			$end_mail = strpos($from_email, "&gt;");
			$the_name = substr($from_email, 0, $end_name-4);
			$the_email = substr($from_email, $end_name+1, $end_mail-$end_name-1);
		} else $the_email = $from_email;
	} // if from_email ends
	
	if ($action == "import") {
		$the_file = "/tmp/".$userfile_name;
		copy ($userfile,$the_file);
		if (eregi("text/",$userfile_type) || eregi(".csv",$userfile_name)) {
			$fp = fopen($the_file, "r") or die ("<b>Could not open file</b><br>");
			$linect = 0;
			// read in the textfile and split for name and address
			while (!feof($fp)) {
				unset($adr_email);
				$adr_line = fgets($fp, 1024);
				$adr_array = split(",|;|\t", $adr_line);
				$adr_name = $adr_array[0];
				foreach ($adr_array as $adr_part)
					if (eregi("@",$adr_part)) $adr_email = $adr_part;
				$linect++;
				// write to DB
				if (!empty($adr_part)) {
					if ($db_link = mysql_connect($sql_host,$sql_user,$sql_pw)) {
						@mysql_select_db("phpwebmail");
						$sql_query = "INSERT INTO addresses (user, name, email) VALUES('".$name."','".$adr_name."','".$adr_email."')";
						$result = mysql_query($sql_query);
	        				// catch SQL errors
 	        				if (mysql_errno()) echo ("ERROR ".mysql_error());	
       				        mysql_close($db_link);
       				    }
				}
			} // while ends
			echo "<b>$linect records imported.</b><br>";
			fclose($fp);
		} else echo "<b>not a text file</b><br>";
		unlink ($the_file);
	}
?>

	<form ENCTYPE="multipart/form-data" TARGET="_self" METHOD=POST>
	<table><tr valign="center">
	<td><font face="arial" size=2><INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="102400">
    	<b>Textfile (max. 100KB):<br><br></td><td colspan=3><INPUT NAME="userfile" TYPE="file"><br><br></td><td><font face="arial" size=2><b>
    	<?php if ($textmode) echo "<input type=submit value=import>"; else echo "<INPUT TYPE=image src=images/_import.gif border=0 alt=\"import\">"; ?>
    	<br><br></td><td><font face="arial" size=2><INPUT TYPE=HIDDEN NAME=action VALUE="import"></td>
	</tr></form>
	<form target="_self" method="post">
	<tr valign="center">
	<td><font face="arial" size=2><b>Name: </td><td><input type="text" size=25 name="new_name" value="<?php echo $the_name; ?>"></td>
	<td><font face="arial" size=2><b>Email: </td><td><input type="text" size=25 name="new_email" value="<?php echo $the_email; ?>"></td>
	<td><font face="arial" size=2><b>
	<?php if ($textmode) echo "<input type=submit value=enter>"; else echo "<INPUT TYPE=image src=images/_enter.gif border=0 alt=\"enter\">"; ?>
	<input type="hidden" name="from_email" value=""><input type="hidden" name="action" value="new"></td>
	</tr></table></form>

	<table border=1><TR ALIGN="TOP">
	<TD align="left"><font face="arial" size=2><b>Name</b> <i>(Click to edit)</TD>
	<TD align="left"><font face="arial" size=2><b>Email</b> <i>(Click for new mail)</TD>
	<TD align="left"><font face="arial" size=2><b><a href="address.php?<?php SID ?>&action=all">
	<?php if ($textmode) echo "delete all"; else echo "<img src=images/_del_all.gif border=0 alt=\"delete all\">"; ?>
	</a></TD>
	<TR>
<?php
      // get everything from the database
      if ($db_link = mysql_connect($sql_host,$sql_user,$sql_pw)) {
	@mysql_select_db("phpwebmail");
	if ($del == "yes") {
		$sql_query = "DELETE FROM addresses WHERE nr='$nr'";
		$result = mysql_query($sql_query);
	        // catch SQL errors
 	        if (mysql_errno()) echo ("ERROR ".mysql_error());
	} // if del ends

	if ($action == "change") {
		$sql_query = "UPDATE addresses SET name='".$new_name."', email='".$new_email."' WHERE nr='$nr'";
		if (eregi("@",$new_email) && $new_name) $result = mysql_query($sql_query);
	        // catch SQL errors
 	        if (mysql_errno()) echo ("ERROR ".mysql_error());
	} // if del ends

	if ($action == "new") {
		$sql_query = "INSERT INTO addresses (user, name, email) VALUES('".$name."','".$new_name."','".$new_email."')";
		if (eregi("@",$new_email) && $new_name) $result = mysql_query($sql_query);
	        // catch SQL errors
 	        if (mysql_errno()) echo ("<p class=\"standard_forms\">ERROR ".mysql_error());
	} // if new ends
	
	$sql_query = "SELECT nr, name, email FROM addresses WHERE user='$name' ORDER BY name";
	$result = mysql_query($sql_query);
        // catch SQL errors
        if (mysql_errno()) echo ("ERROR ".mysql_error());
	while ($row = mysql_fetch_row($result)) {
	  $the_nr = $row[0];
	  $the_name = $row[1];
	  $the_email = $row[2];

	  echo "<TR><TD><font face=\"arial\" size=2><a href=\"address.php?".SID."&nr=$the_nr&action=edit\">$the_name</a></TD>
		<TD><font face=\"arial\" size=2><a href=\"send_mail.php?".SID."&to=$the_email\">$the_email</a></TD>
	  	<TD align=\"center\"><font face=\"arial\" size=2><a href=\"address.php?".SID."&nr=$the_nr&del=yes\">";
	  if ($textmode) echo "delete"; else echo "<img src=\"images/_del_this.gif\" border=0 alt=\"delete\">";
	  	echo "</a></TD></TR>";

	} // while ends
        mysql_close($db_link);
      } // if db_link ends
      echo "</TABLE>";
} // else ends
?>

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