1<?php
2/**
3* AmavisdEngine class
4* @author Samuel Tran
5* @author Jeremy Fowler
6* @version 03-22-07
7* @package AmavisdEngine
8*
9* Copyright (C) 2005 - 2007 MailZu
10* License: GPL, see LICENSE
11*/
12/**
13* Base directory of application
14*/
15@define('BASE_DIR', dirname(__FILE__) . '/..');
16
17/**
18* CmnFns class
19*/
20include_once('CmnFns.class.php');
21
22/**
23*	PEAR::Net_Socket Library
24*/
25if ($GLOBALS['conf']['app']['safeMode']) {
26	ini_set('include_path', ( dirname(__FILE__) . '/pear/' . PATH_SEPARATOR . ini_get('include_path') ));
27	include_once('pear/PEAR.php');
28	include_once('pear/Net/Socket.php');
29}
30else {
31	include_once 'PEAR.php';
32	include_once 'Net/Socket.php';
33}
34
35/**
36* Provide all access/communication to Amavisd AM.PDP
37*/
38
39class AmavisdEngine {
40
41	var $socket;				// Reference to socket
42	var $port;					// Amavisd spam release port
43	var $connected; 		// Connection status
44	var $last_error;		// Last error message
45
46	/**
47	* AmavisdEngine object constructor
48	* $param none
49	* $return object Amavisd object
50	*/
51	function AmavisdEngine($host) {
52
53		$this->socket = new Net_Socket();
54		$this->port = $GLOBALS['conf']['amavisd']['spam_release_port'];
55		$this->connected = false;
56		$this->last_error = '';
57
58		// Connect to the Amavisd Port or wait 5 seconds and timeout
59		$result = $this->socket->connect($host, $this->port, true, 5);
60
61		if (PEAR::isError($result)) {
62			$this->last_error = "Error connecting to $host:$this->port, " . $result->getMessage();
63		} else {
64			$this->connected = true;
65		}
66	}
67
68	/**
69	* Shutdown and close socket
70	* @param none
71	*/
72	function disconnect() {
73		$this->socket->disconnect();
74	}
75
76	/**
77	* Release message from quarantine
78	* @param $mail_id
79	* @param $secret_id
80	* @param $recipient
81	* @result response
82  */
83
84	function release_message($mail_id, $secret_id, $recipient, $quar_type, $quar_loc) {
85
86		if (! $this->connected) {
87			return $this->last_error;
88		}
89
90		$in = "request=release\r\n";
91		$in .= "mail_id=$mail_id\r\n";
92		$in .= "secret_id=$secret_id\r\n";
93		$in .= "quar_type=$quar_type\r\n";
94
95		# If it is file-based quarantine, lets provide the filename on the host
96		if ( $quar_type == 'F' ) {
97		  $in .= "mail_file=$quar_loc\r\n";
98		}
99
100		$in .= "recipient=<$recipient>\r\n";
101		$in .= "\r\n";
102
103		// Sending request ...
104		$out = $this->socket->write($in);
105
106		if (PEAR::isError($out)) {
107			$this->last_error = 'Error writing to socket: ' . $out->getMessage();
108			return $this->last_error;
109		}
110
111		// Set timeout of 5 seconds
112		$this->socket->setTimeout(5);
113
114		// Reading response
115		$out = $this->socket->read(512);
116
117		if (PEAR::isError($out)) {
118			$this->last_error = 'Error reading from socket: ' . $out->getMessage();
119			return $this->last_error;
120		}
121
122		return $out;
123
124	}
125}
126
127?>
128