FeedbackMgr.php 2.7 KB
<?php
/**
 * @class FeedbackMgr
 * @version $Id: FeedbackMgr.php 1804 2013-09-23 16:28:52Z elena $
 *    
 */

  class FeedbackMgr
  {
  	protected $feedXml, $feedXmlName;
  	
  	protected $emails = 'amda@irap.omp.eu';
  	
  	function __construct()
  	{

  	}
  	
  	function addFeedback($user, $interface, $subject, $userText, $userAgent, $attach)
  	{
  		 if ($user == null)
  	    return 'err_user';
  		
  	  //send feedback to emails list
  	  $this->sendEmail($user, $interface, $userText, $attach);
  		
  	  //load feedback file
  	  
  	  if (!file_exists(FeedbackXml))
  	             return 'err_file';
      $this->feedXmlName = FeedbackXml;
      $this->feedXml = new DomDocument("1.0","UTF-8");
      $this->feedXml->preserveWhiteSpace = false;
      $this->feedXml->formatOutput = true;
      if (file_exists($this->feedXmlName)) 
      {
        $this->feedXml->load($this->feedXmlName);
        $rootElement = $this->feedXml->documentElement;
      }
      else
      {
      	$rootElement = $this->feedXml->createElement("root");
        $this->feedXml->appendChild($rootElement);
      }
      
      if (($this->feedXml == null) or ($rootElement == null))
        return 'err_file';
      
      //get feed id - ToDo - to improve
  	  $N = $this->feedXml->getElementsByTagName("issue")->length > 0 ? $this->feedXml->getElementsByTagName("issue")->length : 0;
      
  	  $issue = $this->feedXml->createElement("issue", htmlspecialchars($userText));
      $issue->setAttribute("user", $user);
      $issue->setAttribute("userAgent", $userAgent);
      $issue->setAttribute('interface',$interface);
      $issue->setAttribute('IP',getenv('REMOTE_ADDR'));
      $issue->setAttribute("date", date("Y-m-d"));
      $issue->setAttribute("xml:id", $N);
      $issue->setAttribute("subject", $subject);
      if (isset($attach) && ($attach != ''))
      	 $issue->setAttribute("attachement",$attach); 

      $rootElement->appendChild($issue);
      $this->feedXml->save($this->feedXmlName);
      
      return 'none';
  	}
  	
    protected function sendEmail($user, $interface, $userText, $attach)
  	{
  		  $amda = new AmdaClient();
   	  $result = $amda->getUserInfo($user);
   	  
   	  if ($result['success'])
   	  {
   	  	 $from = $result['email'];
   	  	 $to   = $this->emails.",".$result['email'];
   	  }
   	  else
   	  {
   	  	 $from = $user;
   	  	 $to   = $this->emails;
   	  }
  		
      $subject = 'AMDA FEEDBACK: '.$interface;
      $message = $userText;
      if (isset($attach) && ($attach != ''))
      		$message .= "\r\nAttachment : ".$attach;
      $headers = 'From: '.$from."\r\n".
                 'Content-type: text/plain; charset=UTF-8';
   	  
      mail($to, $subject, $message, $headers);
  	}
  	
  }

?>