Location: PHPKode > scripts > txtSQL MailBox > txtsql-mailbox/contactbook.php
<?
/*************************************************************************/
 #  phpMailbox 1.1   by Sivaprasad R.L (http://netlogger.net/phpmailbox)  #
 #                                                                        #
 #          --  A php pop3 client --              			  #
 #                                                                        #
 # 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., 675 Mass Ave, Cambridge, MA 02139, USA.              #
 #                                                                        #
 #             Copyright (C) by Sivaprasad R.L                            #
 #                                                                        #
/*************************************************************************/

   include "mailheader.php";
   if($op != "view") {
        echo "<script language=javascript>
            function open_w(file) {
                newwind = window.open(file,'View','width=500, height=500, scrollbars=yes, toolbars=no');
            }
            </script>";
    }

    session_start();	

    global $uid,$domain, $footermsgtxt, $attachmentdir,$imgpath, $hrcolor;

    if($op != "view") echo "<br><table width=40% border=1 bordercolor=$hrcolor align=center><tr><td align=center><a class=\"ahref\" href=contactbook.php?op=listall>list all</a></td><td align=center><a  class=\"ahref\" href=contactbook.php?op=addnew>Add new</a></td><td align=center><a  class=\"ahref\" href=contactbook.php?op=search>Search</a></td></tr></table><br><br>";

    if($op=="addnew")
        addnew();
    else if($op == "search")
        search();
    else if($op == "view")
        view();
    else if($op == "delete")
        del();
    else if($op == "edit")
        edit();
    else
       listall();

    if($op != "view") {
        echo "<br><table width=40% border=1 bordercolor=$hrcolor align=center><tr><td align=center><a  class=\"ahref\" href=contactbook.php?op=listall>list all</a></td><td align=center><a  class=\"ahref\" href=contactbook.php?op=addnew>Add new</a></td><td align=center><a  class=\"ahref\" href=contactbook.php?op=search>Search</a></td></tr></table><br><br>";
        include "mailfooter.php";
    }
?>

<?
function listall() {

   global $uid, $index, $imgpath, $hrcolor;

   $countlimit = 20;

   $query = "Select * from contactbook where uid = '$uid' order by firstname";
   $res = mysql_query($query);
   echo "<form name=listform method=post action=contactbook.php>
   <input type=hidden name=op value=delete>

   <table width=100% align=center border=0><tr class=\"bgrowcolor1\"><td width=3%>&nbsp;</td><td width=3%>&nbsp;</td><td width=3%>&nbsp;</td><td width=28%><b class=\"font1\">Name</td><td width=30%><b class=\"font1\">Email</td><td width=15%><b class=\"font1\">Phone (Res)</td><td width=15%><B class=\"font1\">Phone (Work)</td></tr>";
   $numrows = mysql_num_rows($res);
   if($numrows == 0) {
       echo "<tr><td colspan=7 align=center>No records found</td></tr>";
   }

   $color = "bgrowcolor1";
   $count = 0;
   if(isset($index)) {
        $skipcount = $index * $countlimit;
        mysql_data_seek($res,$skipcount);
   }

   while($count < $countlimit && $row = mysql_fetch_array($res)) {
       $contactid = $row[contactid];
       $firstname = $row[firstname];
       $lastname = $row[lastname];
       $email = $row[email];
       $homephone = $row[homephone];
       $workphone = $row[workphone];

       echo "<tr class=\"$color\"><td><a class=\"ahref\" href=\"Javascript:open_w('contactbook.php?op=view&cid=$contactid');\"><img src=\"$imgpath/view.gif\" alt='View Profile' border=0 width=16 height=12></a></td><td><a  class=\"ahref\" href='contactbook.php?op=edit&cid=$contactid' ><img src=\"$imgpath/edit.gif\" border=0 alt='Edit Contact' width=16 height=16></a></td><td><input type=checkbox name=del[] value=$contactid></td><td><a class=\"ahref\" href=\"Javascript:open_w('contactbook.php?op=view&cid=$contactid');\">$lastname, $firstname </a></td><td><a  class=\"ahref\" href=compose.php?to=$email>$email</a></td><td>$homephone</td><td>$workphone</td></tr>";
       if($color== "bgrowcolor1") $color = "bgrowcolor2"; else $color = "bgrowcolor1";
       $count++;
   }
   echo "</table>&nbsp;&nbsp;&nbsp;&nbsp;
   <input type=submit name=deleteall value='Delete Selected'></form>";

  echo "<center>";

  if($index > 0) {
       $ind = $index-1;
       echo "<a  class=\"ahref\" href='contactbook.php?op=listall&index=$ind'>« prev</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  }

  $limit = $numrows/$countlimit;
  if($limit > 1) {
      for($i=0; $i < $limit; $i++) {
          $ind = $i+1;
          if($index == $i) echo "$ind ";
          else echo "<a  class=\"ahref\" href='contactbook.php?op=listall&index=$i'>$ind</a>&nbsp";
      }
  }

  echo "&nbsp;&nbsp;&nbsp;&nbsp;";
  if( ($skipcount + $count) < $numrows) {
       $ind = $index + 1;
       echo "<a  class=\"ahref\" href='contactbook.php?op=listall&index=$ind'>next »</a></center>";
  }

}

function addnew() {
   global $uid,$save,$uid,$firstname,$lastname,$email,$company,$homeaddress,$homepage,$city,
       $homephone,$workphone,$mobilephone,$other,$email2,$events,$reminders,$notes,$imgpath,$hrcolor;

   if(isset($save)) {
       $query = "insert into contactbook (uid,firstname,lastname,email,company,homeaddress,city,homepage,
       homephone,workphone,mobilephone,other,email2,events,reminders,notes) values('$uid','$firstname','$lastname','$email','$company','$homeaddress','$city','$homepage',
       '$homephone','$workphone','$mobilephone','$other','$email2','$events','$reminders','$notes');";
       $res = mysql_query($query);

       listall();
   } else {

   echo "<form name=addnew method=post action=contactbook.php>
   <b class=\"font3\">Add a new contact :</b><hr align=left color=$hrcolor width=40%><br><br>
   <table align=center width=90%>
   <tr><td width=25%>First Name </td><td><input type=text name=firstname></td></tr>
   <tr><td>Last Name </td><td><input type=text name=lastname></td></tr>
   <tr><td>Email </td><td><input type=text name=email></td></tr>
   <tr><td>Altername Email #2 </td><td><input type=text name=email2></td></tr>
   <tr><td>Phone (Work #1)</td><td><input type=text name=homephone></td></tr>
   <tr><td>Phone (Home #2)</td><td><input type=text name=workphone></td></tr>
   <tr><td>Phone (Mobile #3)</td><td><input type=text name=mobilephone></td></tr>
   <tr><td>Phone (Other #4)</td><td><input type=text name=other></td></tr>
   <tr><td>Address </td><td><textarea name=address rows=4 cols=25></textarea></td></tr>
   <tr><td>City </td><td><input type=text name=city></td></tr>
   <tr><td>Company </td><td><input type=text name=company size=40></td></tr>
   <tr><td>Homepage </td><td><input type=text name=homepage size=40 value='http://'></td></tr>
   <tr><td colspan=2><hr align=left width=100% color=$hrcolor></td></tr>
   <tr><td colspan=2><hr align=left width=100%  color=$hrcolor></td></tr>
   <tr><td valign=top>Related Events </td><td>List the events related to this person such as Birthday, Annivesary etc.. or
   any special dates that you want to be reminded by us. Add as many events as you like.<br>
   To add an event, add as 'Event name: date' (eg: Birthday : 03/21)(note: The date format is mm/dd)<br>
   <textarea name=events rows=4 cols=40></textarea></td></tr>
   <tr><td>Remind me </td><td><input type=text name=reminders size=3 value=1> day(s) before the event</td></tr>
   <tr><td colspan=2><hr  align=left width=100% color=$hrcolor></td></tr>

   <tr><td>Notes </td><td><textarea name=notes rows=4 cols=40></textarea></td></tr></table>
   <input type=hidden name=save value='true'>
   <input type=hidden name=op value='addnew'>
   <center><input type=submit name=add value=submit></center></form>";
   }

}

function search() {

global $uid, $q, $searchdb, $searchfield, $index;
global $hrcolor, $imgpath;

echo "<form method=post action=contactbook.php name=searchform>
      <input type=hidden name=op value=search>
      <table align=center><tr><Td>Search : </td><td><input type=text name=q value='$q'></td>
      <td> in </td><td>
      <select name=searchfield>
      <option value='all'>All</option>
      <option value='firstname'>First name</option>
      <option value='lastname'>Last name</option>
      <option value='email'>Email</option>
      <option value='homeaddress'>Address</option>
      <option value='city'>city</option>
      <option value='company'>Events</option>
      <option value='notes'>Notes</option>
      </select>
      </tr>
      </table>
      <center><input type=submit name=searchdb value='Search'>
      </form>";

 if($searchdb == "Search") {
    $query = "Select * from contactbook where uid = '$uid' and ( ";
    if($searchfield != "all") {
         $words = explode(" ",$q);
         foreach($words as $w) {
             $condition = " ($searchfield like '%$w%') ||";
         }
         $condition = substr($condition,0,-2) . ")";
    } else {
         $searchfield = array ("firstname","lastname","email","homeaddress","city","company","notes");
         foreach($searchfield as $sf) {
             $words = explode(" ",$q);
             foreach($words as $w) {
                 $condition .= " ($sf like '%$w%') ||";
             }
         }
         $condition = substr($condition,0,-2) . ")";
    }

    $query .= $condition;
    $res = mysql_query($query);
    $numrows = mysql_num_rows($res);

    echo "<Br><center>Your search returned $numrows rows</center>
    <table width=100% align=center border=0><tr class=\"bgrowcolor1\">
    <td width=3%>&nbsp;</td>
    <td width=28%><b class=\"font1\">Name</td>
    <td width=30%><b class=\"font1\">Email</td>
    <td width=15%><b class=\"font1\">Phone (Res)</td>
    <td width=15%><b class=\"font1\">Phone (Work)</td></tr>";

   $skipcount = 0; $count = 0; $countlimit = 20;
   if(isset($index)) {
        $skipcount = $index * $countlimit;
        mysql_data_seek($res,$skipcount);
   }

    while($count < $countlimit && $row = mysql_fetch_array($res)) {
       $contactid = $row[contactid];
       $firstname = $row[firstname];
       $lastname = $row[lastname];
       $email = $row[email];
       $homephone = $row[homephone];
       $workphone = $row[workphone];
       echo "<tr class=\"$color\"><td><a class=\"ahref\" href=\"Javascript:open_w('contactbook.php?op=view&cid=$contactid');\"><img src=\"$imgpath/view.gif\" alt='View Profile' border=0 width=16 height=12></a></td><td>$lastname, $firstname</td><td><a  class=\"ahref\" href=compose.php?to=$email>$email</a></td><td>$homephone</td><td>$workphone</td></tr>";
       if($color== "bgrowcolor1") $color = "bgrowcolor2"; else $color = "bgrowcolor1";
       $count++;
   }
  echo "</table>&nbsp;&nbsp;&nbsp;&nbsp;";
  echo "<center>";
  if($index > 0) {
       $ind = $index-1;
       echo "<a  class=\"ahref\" href='contactbook.php?op=search&index=$ind'>« prev</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  }

  $limit = $numrows/$countlimit;
  if($limit > 1) {
      for($i=0; $i < $limit; $i++) {
          $ind = $i+1;
          if($index == $i) echo "$ind ";
          else echo "<a  class=\"ahref\" href='contactbook.php?op=search&index=$i'>$ind</a>&nbsp";
      }
  }

  echo "&nbsp;&nbsp;&nbsp;&nbsp;";
  if( ($skipcount + $count) < $numrows) {
       $ind = $index + 1;
       echo "<a  class=\"ahref\" href='contactbook.php?op=search&index=$ind'>next »</a></center>";
  }
 }
}

function view() {
   global $uid, $cid, $domain, $imgpath;
   global $hrcolor;

   $query = "Select * from contactbook where uid = '$uid' and contactid=$cid";
   $res = mysql_query($query);

   echo "<html><title>$domain - View profile</title><body text=#63627f>";
   if(mysql_num_rows($res) == 0) {
       echo "<tr><td colspan=2 align=center>No records found</td></tr>";
   }

   if($row = mysql_fetch_array($res)) {
       $contactid = $row[contactid];
       $userrowid = $row[uid];
	
       if($userrowid != $uid) {
           echo "<br><center><b>Error : Permission denied<br><br></b></center></table>";
           return;
       }

       $firstname = $row[firstname];
       $lastname = $row[lastname];
       $email = $row[email];
       $homephone = $row[homephone];
       $email2 = $row[email2];
       $workphone = $row[workphone];
       $mobilephone = $row[mobilephone];
       $other = $row[other];
       $homeaddress = $row[homeaddress];
       $city = $row[city];
       $company = $row[company];
       $homepage = $row[homepage];
       $events = $row[events];
       $reminders = $row[reminders];
       $notes = $row[notes];
   }


   echo "
   <table align=center  width=100%><tr><td class=\"bgrowcolor2\">
   <table align=center width=98%><tr><td class=\"bgrowcolor1\">
   <center><b class=\"font3\">V i e w  &nbsp; P r o f i l e</b></center><br>
   <Table width=90% align=center>
   <tr class=\"bgrowcolor1\"><td width=35%>First Name </td><td>$firstname</td></tr>
   <tr><td>Last Name </td><td>$lastname</td></tr>
   <tr class=\"bgrowcolor1\"><td>Email </td><td>$email</td></tr>
   <tr class=\"bgrowcolor1\"><td>Alternate Email </td><td>$email2</td></tr>
   <tr><td>Phone (Work)</td><td>$homephone</td></tr>
   <tr class=\"bgrowcolor1\"><td>Phone (Home)</td><td>$workphone</td></tr>
   <tr class=\"bgrowcolor1\"><td>Phone (mobile)</td><td>$mobilephone</td></tr>
   <tr class=\"bgrowcolor1\"><td>Phone (other)</td><td>$other</td></tr>

   <tr><td>Address </td><td>$homeaddress</td></tr>
   <tr class=\"bgrowcolor1\"><td>City </td><td>$city</td></tr>
   <tr><td>Company </td><td>$company</td></tr>
   <tr class=\"bgrowcolor1\"><td>Homepage </td><td>$homepage</td></tr>
   <tr><td colspan=2><hr align=left width=100% color=$hrcolor></td></tr>";

   echo "</table></td></tr>
   <tr><td colspan=2><hr align=left width=100%  color=$hrcolor></td></tr>

   <tr class=\"bgrowcolor1\"><td colspan=2 valign=top>Related Events : </td></tr>";

   echo "<tr><td colspan=2><table width=70% align=center>";
   $listevents = explode("\n",$events);
   foreach($listevents as $ev) {
           $array = explode(":",$ev);
	   if($array[1] != "")
              echo "<tr><td width=30%><b>$array[0]</b></td><td>: $array[1]</tr>";
   }

   echo "</table></td></tr><tr><td colspan=2><hr align=left width=100% color=$hrcolor></td></tr>";

   if($notes != "") 
      echo "<tr class=\"bgrowcolor1\"><td colspan=2>Notes </td></tr><tr><td colspan=2>$notes</td></tr>";

   echo "</table></td></tr></table><center><form><input type=button name=close value=\"close\" onClick=\"window.close();\"> </center></td></tr></table></body></html>";
}

function del() {

    global $uid, $del, $imgpath,$hrcolor;

    if(is_array($del)) {
        foreach ($del as $d) {
            $q = "select * from contactbook where uid = '$uid' and contactid = $d";
            $r = mysql_query($q);
            if(mysql_num_rows($r) > 0) {
                $query = "delete from contactbook where contactid = $d";
                $res = mysql_query($query);
            }
        }
    } else {
            $q = "select * from contactbook where uid = '$uid' and contactid = $del";
            $r = mysql_query($q);
            if(mysql_num_rows($r) > 0) {
                $query = "delete from contactbook where contactid = $del";
                $res = mysql_query($query);
            }
    }
    listall();
}

function edit() {
   global $uid,$cid, $save,$firstname,$lastname,$email,$company,$homeaddress,$homepage,$city,
       $homephone,$workphone,$events,$reminders,$notes, $imgpath,$hrcolor,$mobilephone,$other,$email2;

   if($save == "true") {
      $query = "update contactbook set  firstname= '$firstname',lastname='$lastname',
      email = '$email', homephone = '$homephone', workphone ='$workphone', mobilephone ='$mobilephone', other ='$other', email2 = '$email2', homeaddress= '$homeaddress',
      city = '$city', company = '$company', homepage= '$homepage', events = '$events',
      reminders = '$reminders',notes = '$notes' where contactid = $cid";
      $res = mysql_query($query);

      listall();
      return;
   }

   $query = "Select * from contactbook where uid = '$uid' and contactid = $cid";
   $res = mysql_query($query);

   if($row = mysql_fetch_array($res)) {
       $userrowid = $row[uid];

       if($userrowid != $uid) {
           echo "<br><center><b>Error : Permission denied<br><br></b></center></table>";
           return;
       }

       $firstname = $row[firstname];
       $lastname = $row[lastname];
       $email = $row[email];
       $homephone = $row[homephone];
       $workphone = $row[workphone];
       $mobilephone = $row[mobilephone];
       $other = $row[other];
       $email2 = $row[email2];
       $homeaddress = $row[homeaddress];
       $city = $row[city];
       $company = $row[company];
       $homepage = $row[homepage];
       $IM = $row[IM];
       $events = $row[events];
       $reminders = $row[reminders];
       $notes = $row[notes];
   }

   echo "<form name=editform method=post action=contactbook.php>
   <b class=\"font3\">Edit contact :</b><hr align=left color=$hrcolor width=40%><br><br>
   <table align=center width=90%>
   <tr><td width=25%>First Name </td><td><input type=text name=firstname value='$firstname'></td></tr>
   <tr><td>Last Name </td><td><input type=text name=lastname value='$lastname'></td></tr>
   <tr><td>Email </td><td><input type=text name=email value='$email'></td></tr>
   <tr><td>Alternate Email #2 </td><td><input type=text name=email2 value='$email2'></td></tr>
   <tr><td>Phone (Work #1)</td><td><input type=text name=homephone value='$homephone'></td></tr>
   <tr><td>Phone (Home #2)</td><td><input type=text name=workphone value='$workphone'></td></tr>
   <tr><td>Phone (Mobile #3)</td><td><input type=text name=mobilephone value='$mobilephone'></td></tr>
   <tr><td>Phone (Other #4)</td><td><input type=text name=other value='$other'></td></tr>
   <tr><td>Address </td><td><textarea name=homeaddress rows=4 cols=25>$homeaddress</textarea></td></tr>
   <tr><td>City </td><td><input type=text name=city value='$city'></td></tr>
   <tr><td>Company </td><td><input type=text name=company size=40 value='$company'></td></tr>
   <tr><td>Homepage </td><td><input type=text name=homepage size=40  value='$homepage'></td></tr>
   <tr><td colspan=2><hr align=left width=100% color=$hrcolor></td></tr>
   <tr><td valign=top>Related Events </td><td><textarea name=events rows=4 cols=40>$events</textarea></td></tr>
   <tr><td>Remind me </td><td><input type=text name=reminders  value='$reminders'size=3 value=1> day(s) before the event</td></tr>
   <tr><td colspan=2><hr  align=left width=100% color=$hrcolor></td></tr>
   <tr><td>Notes </td><td><textarea name=notes rows=4 cols=40>$notes</textarea></td></tr></table>
   <input type=hidden name=save value='true'>
   <input type=hidden name=op value='edit'>
   <input type=hidden name=cid value='$cid'>
   <center><input type=submit name=add value=submit></center></form>";
}
?>
Return current item: txtSQL MailBox