**Need Fast Quick Help for PHP Page

Discussion in 'PHP' started by dman779, Dec 3, 2008.

  1. #1
    Okay so this page is the Support Ticket coding for the admin section of my site. As you can see when a user is sent a new message they receive email notification that they have a New Message Waiting For Them. But I can't seem to add the same function for when users get replies to existing messages. Take a look at the code below and hopefully someone can tell me where to insert the other mail function code to make email notification available for new replies.

    The mail function is this part and is in the script already and working for when people are sent new messages. This is the mail function:

    $subject="You Have a New Private Message at Innovus Net";
    $message='Dear User,<br>
    You have a new Private Message waiting at <a href="http://www.InnovusNet.com">InnovusNet.com</a> from the Support Team.  To read or respond to this message, please login to your "Dashboard" page.<br><br>
    ************************<br>You will not receive E-Mail notification for replies to messages so check your Support Ticket mailbox frequently.<br>
    <br>Regards, <br>Innovus Net<br><br><a href="http://www.InnovusNet.com">www.InnovusNet.com</a><br><a href="mailto:admin@InnovusNet.com">Admin@InnovusNet.com</a><br>AIM: <a href="aim:goim?screenname=InnovusNet&amp;message=Hello">Innovus Net</a>';	
    mailing(($_REQUEST['user_id']),$message,$subject);	
    PHP:


    Main Script for page missing some of the code because it was too big.

    <?
    require('checkAdmin.php');
    //ADMIN SUPPORT
    include("../includes/config.php");
    function escape2($var)
    {
    	return $var;
    }
    require_once( "../Table.php" );
    
    require_once("../mysql/Mysql.php");
    $mysql = new Mysql();
    //print_r($_POST);
    /*$from_morder=NULL;
    $tic_rm=escape2($_REQUEST['tic_rm']);
    if(!$tic_from_order)
    {*/
    	$ticket=escape2($_REQUEST['ticket_id']);
    /*
    }
    else{ $from_morder="&do_orders=viewOrder&state_id=$id&tic_rm=1"; }*/
    
    if(!$ticket && access_rights(108))//access_rights($func_num) is used check accessble
    {
    
    //------------------------------------------------------------------------------------------------------------------>
    $period=escape2($_POST['period']);
    //----------------------------------------------------------->
    if(($_POST['start_m'] &&  $_POST['start_d'] && $_POST['start_y'] ) || ($_POST['end_m'] &&  $_POST['end_d'] && $_POST['end_y'] ))
    $period='';
    function mailing($id,$message,$subject)
    {
    $DomainName=mysql_query("select * from users,sites where users.id='$id' and sites.siteID=users.siteID") or die(mysql_error());
    $DomainName=mysql_fetch_array($DomainName);
    $emailFrom="Innovus Net <info@innovusnet.com>"; 
    $to=$DomainName[email];
    $headers = "From: Info@InnovusNet.com \r\n";
    
    	$headers .= "Reply-To: Info@InnovusNet.com \r\n";
    	$headers .= "MIME-Version: 1.0\r\n";
    
    	$headers .="Date: ".date('r', time())."\r\n";
    	
    	$headers .="Content-Type: text/html; charset=ISO-8859-1\n";
     
       $headers .= "Content-Transfer-Encoding: 7bit\n";
    	
    	# -=-=-=- TEXT EMAIL PART
    	 $headers.="Content-Type: text/html; charset=ISO-8859-1\r\n";
    	$message=nl2br($message);
    	$newmessage='<table width="100%" border="0">
      <tr>
        <td width="31%"><img src="http://innovusnet.com/images/logo.PNG" width="309" height="127"></td>
        <td width="69%">&nbsp;</td>
      </tr>
      <tr>
        <td height="32" colspan="2" background="http://innovusnet.com/images/tabr.jpg">&nbsp;</td>
      </tr>
      <tr>
        <td height="395" colspan="2" align="left" valign="top"><table width="1160" height="342" border="0">
          <tr>
            <td width="160" height="141">&nbsp;</td>
            <td width="990" align="left" valign="top">'.$message.'</td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td colspan="2">
          <p align="center"><strong>&copy; 2008, Innovus Net, Inc. All rights reserved.</strong></p></td>
      </tr>
    </table>';
    
    	if(mail($to, $subject, $newmessage,$headers))
    	echo "Mail Sent to :$to<br>";
    	else echo "Mail Failed : $to<br>";
    }
    //----------------------------------------------------------->
    $ticket=escape2($_REQUEST['ticket']);
    $sel_status=escape2($_POST['sel_status']);
    $answered=escape2($_POST['answered']);
    ?>
    <a href="?start_message=new#newMess">Start New Message</a> 
    <a href="?start_message=readonly#newMess">Send Read Only Message</a>
    <form method="post" action="">
    <table width="75%" border="0" cellspacing="2">
      <tbody>
        <tr>
          <td width="32%">&nbsp;</td>
          <td width="68%" colspan="2">&nbsp;</td>
        </tr>
      
    
    
        <tr>
          <td >Time : </td>
          <td colspan="2">&nbsp;</td>
        </tr>
    	<tr>
    	  <td>&nbsp;&nbsp;Select Period: </td>
    	  <td colspan="2"><select name="period" id="period">
    		<option <? if($period=='') echo ' selected '; ?>>Any</option>
    		<option value="1" <? if($period=='1') echo ' selected '; ?>>Today</option>
    		<option value="-1" <? if($period=='-1') echo ' selected '; ?>>Yesterday</option>
    		<option value="30" <? if($period=='30') echo ' selected '; ?>>Month up to the Date</option>
    		<option value="-30" <? if($period=='-30') echo ' selected '; ?>>Last Month</option>
    		<option value="365" <? if($period=='365') echo ' selected '; ?>>Year up to Date</option>
    		<option value="-365" <? if($period=='-365') echo ' selected '; ?>>Last Year</option>
    	  </select> 
    		If Custom Time is used this period will be &quot;Any&quot; </td>
    	</tr>
        <tr>
          <td >&nbsp;&nbsp;Start Time: </td>
          <td colspan="2"><select name="start_m" >
            <option value="0" selected="selected">---</option>
            <option value="1">Jan</option>
            <option value="2">Feb</option><option value="3">Mar</option><option value="4">Apr</option><option value="5">May</option><option value="6">Jun</option><option value="7">Jul</option><option value="8">Aug</option><option value="9">Sept</option><option value="10">Oct</option><option value="11">Nov</option><option value="12">Dec</option>
          </select>&nbsp;
            <select name="start_d" >
    		<option value="0" selected="selected">--</option>
    		<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option>
    		<option value="13">13</option>
    		<option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option>
            </select>&nbsp;
            <select name="start_y" >
    		<option value="0" selected="selected">----</option>
    
    		<? for($i=2007; $i<=date('Y');$i++)
    		{
    			echo '<option value="'.$i.'">'.$i.'</option>';
    		}
    		?>
            </select>&nbsp;
    		<? if($_POST['start_m'] &&  $_POST['start_d'] && $_POST['start_y'] )
    		echo $_POST['start_m'].'-'.$_POST['start_d'].'-'.$_POST['start_y'] ;
    		?>        </td>
        </tr>
        <tr>
          <td >&nbsp;&nbsp;End Time: </td>
          <td colspan="2"><select name="end_m" >
    	 <option value="0" selected="selected">---</option>
            <option value="1">Jan</option>
            <option value="2">Feb</option><option value="3">Mar</option><option value="4">Apr</option><option value="5">May</option><option value="6">Jun</option><option value="7">Jul</option><option value="8">Aug</option><option value="9">Sept</option><option value="10">Oct</option><option value="11">Nov</option><option value="12">Dec</option>
                </select>&nbsp;
            <select name="end_d" >
    		<option value="0" selected="selected">--</option>
    		<option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option><option value="13">13</option>
    		<option value="13">13</option>
    		<option value="15">15</option><option value="16">16</option><option value="17">17</option><option value="18">18</option><option value="19">19</option><option value="20">20</option><option value="21">21</option><option value="22">22</option><option value="23">23</option><option value="24">24</option><option value="25">25</option><option value="26">26</option><option value="27">27</option><option value="28">28</option><option value="29">29</option><option value="30">30</option><option value="31">31</option>
              </select>&nbsp;
            <select name="end_y" >
    		<option value="0" selected="selected">----</option>
    		<? for($i=2007; $i<=date('Y');$i++)
    		{
    			echo '<option value="'.$i.'">'.$i.'</option>';
    		}
    		?>
              </select>&nbsp;
    				<? if($_POST['end_m'] &&  $_POST['end_d'] && $_POST['end_y'] )
    		echo $_POST['end_m'].'-'.$_POST['end_d'].'-'.$_POST['end_y'] ;
    		?>				</td>
        </tr>
        <tr>
          <td valign="top"><label for="new_status_id">Order Status:</label></td>
          <td><label>
            <input name="sel_status" type="radio" value="0" <? if($sel_status=='0' || $sel_status==NULL) echo ' checked '; ?>>
    All </label><br>
    		<label><input name="sel_status" type="radio" value="1" <? if($sel_status=='1') echo ' checked '; ?>>
            Open</label><br>
            <label><input name="sel_status" type="radio" value="4" <? if($sel_status=='4') echo ' checked '; ?>>
            Close
            </label>
            <br>
    		 <label><input name="sel_status" type="radio" value="9" <? if($sel_status=='9') echo ' checked '; ?>>
            Admin Message
            </label>
            <br>
            </td>
          <td><label>
            <input name="answered" type="radio" value="0" <? if($answered=='0' || $answered==NULL) echo ' checked '; ?> /> 
            All
    </label>
            <br />
            <label>
            <input name="answered" type="radio" value="1" <? if($answered=='1') echo ' checked '; ?> />
            Last Entry by Admin</label>
            <br />
            <label>
            <input name="answered" type="radio" value="2" <? if($answered=='2') echo ' checked '; ?> />
            Last Entry by User</label></td>
        </tr>
        <tr>
          <td align="right"><input type="submit" name="s_submit" value="Search"></td>
          <td colspan="2">&nbsp;</td>
        </tr>
    	</tbody>
      </table>
    </form>
    <?
    	
    
    	
    	$search='1';
    	if($sel_status=='1') $search .=" &&  support_tickets.status = 1 ";
    	elseif($sel_status=='4') $search .=" &&  support_tickets.status % 4 = 0";
    	elseif($sel_status=='9') $search .=" &&  support_tickets.status = 9";
    	else;
    	
    	if($answered=='1') $search .=" &&  support_tickets.answered='1' ";
    	elseif($answered=='2') $search .=" &&  support_tickets.answered='2' ";
    	else;
    	
    	$deact=0;
    	 if($_POST['start_m'] &&  $_POST['start_d'] && $_POST['start_y'] )
    	 {
    		$startnew_time=mktime(0, 0, 0, $_POST['start_m'], $_POST['start_d'], $_POST['start_y']);
    		$search.=' &&  support_tickets.time_posted>='.$startnew_time.' ';
    		$deact=1;
    	 }
    	 if($_POST['end_m'] &&  $_POST['end_d'] && $_POST['end_y'] )
    	 {
    		$endnew_time=mktime(23, 59, 59, $_POST['end_m'], $_POST['end_d'], $_POST['end_y']);
    		$search.=' &&  support_tickets.time_posted<='.$endnew_time.' ';
    		$deact=1;
    	 }
    	
    	
    	if($period && $deact==0)
    	switch($period)
    	{
    		case '1':	$req_time=mktime(0, 0, 0, date('n'), date('j'), date('Y'));	
    					$search.=' &&  support_tickets.time_posted>='.$req_time.' '; 
    					break;
    		case '-1':	$req_time=mktime(0, 0, 0, date('n'), date('j')-1, date('Y'));
    					$end_time=mktime(23, 59, 59, date('n'), date('j')-2, date('Y'));
    					$search.=' &&  support_tickets.time_posted>='.$end_time.' &&  support_tickets.time_posted<='.$req_time.' ';
    					break;
    		case '30':	$req_time=mktime(0, 0, 0,date('n'), 1, date('Y'));			
    					$search.=' &&  support_tickets.time_posted>='.$req_time.' ';
    					break;
    		case '-30':	$req_time=mktime(0, 0, 0, date('n')-1, 1, date('Y'));	
    					$end_time=mktime(23, 59, 59, date('n')-2, 1, date('Y'));
    					$search.=' &&  support_tickets.time_posted>='.$end_time.' &&  support_tickets.time_posted<='.$req_time.' ';	
    					break;
    		case '365':	$req_time=mktime(0, 0, 0, 1, 1, date('Y'));					
    					$search.=' &&  support_tickets.time_posted>='.$req_time.' ';
    					break;
    		case '-365':$req_time=mktime(0, 0, 0, 1, 1, date('Y')-1);	
    					$end_time=mktime(23, 59, 59, 1, 1, date('Y')-2);
    					$search.=' &&  support_tickets.time_posted>='.$end_time.' &&  support_tickets.time_posted<='.$req_time.' ';			
    					break;
    		default: break;
    	}
    	
    		/*if($_POST['user_sh_id'])
    		$search = $search . "&&  support_tickets.user_id='" . $_POST['user_sh_id'] . "'";
    		if($_POST['user_sh_email'])
    		$search = $search . "&& users.email='" . $_POST['user_sh_email'] . "'";*/
    //------------------------------------------------------------------------------------------------------------------>
    
    $sort_by=$_GET['s'];
    $order=$_GET['o'];
    if(!$sort_by)
    {
    	$sort_by = 'time_posted';
    	$order = 'desc';
    }
    
    echo "<br>Pending Support Tickets<br>";
    	$camp=new Table();
    	$colNames=array();
    	$format=array();
    	$colNames[]="Ticket ID";		$format[]=$camp->createFormat("./support_reply.php?ticket_id=", "id", "id",null);
    	$colNames[]="Title";		$format[]=$camp->createFormat("./support_reply.php?ticket_id=", "id","title", null);
    	$colNames[]="Date Posted";	$format[]=$camp->createFormat(null, null,"time_posted", 1);
    	$colNames[]="Affiliate ID";		$format[]=$camp->createFormat(null, null,"user_id", null);
    	$colNames[]="Name";	$format[]=$camp->createFormat(null, null,"first_name", null);
    	$colNames[]="E-mail";	$format[]=$camp->createFormat(null, null,"email", null);
    	$colNames[]="Answered";	$format[]=$camp->createFormat(null, null,"answered", 2);
    	$colNames[]="View";	$format[]=$camp->createFormat(null, null,"id", 3);
    	
    	//if(!$sel_status)
    		//$search .=" &&  support_tickets.status % 4 != 0 && support_tickets.status != 9 ";
    	
    	$search .=" && support_tickets.backlink='0' && support_tickets.user_id=users.id ";
    	$camp->setInitial( $colNames,"support_tickets.*,users.first_name, users.email", "support_tickets,users", $search ,$sort_by,$order,1,NULL);
    	$camp->setRow( $format );
    	$camp->display();
    echo "<br><Br>";
    
    }
    
    /*if(escape2($_POST['rel_order']))
    {
    	$user_id_tic=escape2($_POST['user_id_tic']);
    	add_ticket_1($user_id_tic, $ticket);
    	
    }
    
    if($_POST['rel_order_reverse'])
    {
    	$user_id_tic=$_POST['user_id_tic'];
    	add_ticket_1_reverse($user_id_tic, $ticket);
    	if($tic_rm==1) $ticket=NULL;  //ticket is changed to NULL when it is used from manage_orders.php when unmarking as order related
    	
    }*/
     
     
     if(escape2($_POST['newText']) && access_rights(108))//access_rights($func_num) is used check accessble
    {	
    	$close=$_POST['close_ticket'];
    	//if($_POST['close_ticket']) $close=4;
    	if(!($_POST['title']))
    		$errors=$errors . "Your title is invalid.<br>";
    	if(!$_POST['newText'])
    		$errors=$errors . "Your message is invalid.<br>";
    	if(!$_POST['user_id'])
    		$errors=$errors . "Please enter a User ID.<br>";
    	if(!$errors)
    	{
    		$answer_status=1; //answered=2 => not answered //here 1 because admin
    		$is_insert=$mysql->doInsert("support_tickets", "(user_id, time_posted, text, title, admin, answered, status)",
    		 "('" . escape2($_POST['user_id']) . "','" . time() . "','" . escape2($_POST['newText']) . "', '". $_POST['title'] ."','". $_SESSION['adminID'] ."', '$answer_status', '$close')"); 
    		 
    		 if($is_insert) echo '<br>New Message is successfully posted<br>';	
    		 
    $subject="You Have a New Private Message at Innovus Net";
    $message='Dear User,<br>
    You have a new Private Message waiting at <a href="http://www.InnovusNet.com">InnovusNet.com</a> from the Support Team.  To read or respond to this message, please login to your "Dashboard" page.<br><br>
    ************************<br>You will not receive E-Mail notification for replies to messages so check your Support Ticket mailbox frequently.<br>
    <br>Regards, <br>Innovus Net<br><br><a href="http://www.InnovusNet.com">www.InnovusNet.com</a><br><a href="mailto:admin@InnovusNet.com">Admin@InnovusNet.com</a><br>AIM: <a href="aim:goim?screenname=InnovusNet&amp;message=Hello">Innovus Net</a>';	
    mailing(($_REQUEST['user_id']),$message,$subject);	
    
    	}
    	else echo $errors;
    }
     
     
    if(escape2($_POST['text']) && access_rights(108))//access_rights($func_num) is used check accessble
    {	
    	$close=0;
    	if($_POST['close_ticket']) $close=4;
    	/*if(!($_POST['title']))
    		$errors=$errors . "Your title is invalid.<br>";*/
    	if(!$_POST['text'])
    		$errors=$errors . "Your message is invalid.<br>";
    	if(!$errors)
    	{
    		$is_insert=$mysql->doInsert("support_tickets", "(backlink, user_id, time_posted, text, admin)",
    		 "('" . escape2($_POST['backlink']) . "','0','" . time() . "','" . escape2($_POST['text']) . "', '". $_SESSION['adminID'] ."')");		 
    //user_id=0 for admin
    		 if($is_insert)
    		 {
    		  	$answer_status=1; //answered=2 => not answered //here 1 because admin
    		 	$mysql->doUpdate("support_tickets", "answered='$answer_status', status='".$close."'" ,"id='".$ticket."'");			
    			echo "<br>Replyed Succesfully";
    			
    		 }	 
    
    		 
    		 else echo ' insert Error';
    	}
    	else echo $errors;
    	
    }
    
    if(escape2($_POST['close_ticket_wor']))
    {
    	$close=0;
    	if(escape2($_POST['close_ticket_wor'])) $close=4;
    	
    	$close_wor=$mysql->doUpdate("support_tickets", "answered=1, status='".$close."'" ,"id='".$ticket."'");
    	if($close_wor) echo '<br>Ticket Closed.';
    	
    }
    if(escape2($_REQUEST['start_message'])=='new' || escape2($_REQUEST['start_message'])=='readonly')
    {
    ?>
    <a name="newMess"></a>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="post">
    	  <TABLE borderColor="#3b5998" rules="all" frame="border">
    		<TBODY>
          <TR>
            <TD colSpan="2">New Ticket</TD>
          </TR>
          <TR>
            <TD colSpan="2">Title:
              <INPUT maxLength="80" name="title"  size="40"></TD>
          </TR>
          <TR>
            <TD colSpan="2"><TEXTAREA name="newText" cols="50" rows="5" height="140px" width="90%"></TEXTAREA></TD>
          </TR>
          <TR>
            <TD colSpan="2">
    		<? if(is_numeric($_REQUEST['user_id'])) { ?>
    		<input name="user_id" type="hidden" value="<?=$_REQUEST['user_id']?>" />
    		<? }
    		else {  ?>
    		Enter user ID: <input name="user_id" type="text"  size="10" />&nbsp;&nbsp;
    		<? } 
    		
    		if(escape2($_REQUEST['start_message'])=='new')
    			$close_status = 1; //not closed
    		 elseif(escape2($_REQUEST['start_message'])=='readonly')
    			$close_status = 9; //closed
    		
    		?>
    <input name="close_ticket" type="hidden" value="<?=$close_status?>" />
    		<INPUT name="submit" type="submit" value="submit"></TD>
          </TR>
        </TBODY>
      </TABLE>
    </form>
    
    ?>
    PHP:

    Thanks Guys!
     
    dman779, Dec 3, 2008 IP