Location: PHPKode > projects > XMail Log Archiver > xmlogarch.d/reports.d/0000-server.php
<?php
/*
	 License
    XMail Log Archiver
	 http://xmlogarch.sourceforge.net
    Copyright (C) 2006 Bryn Mosher

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License Version 2 as
	 published by the Free Software Foundation.

    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 contact , write to 
	 hide@address.com
	 - or -
	 Free Software Foundation, Inc.
	 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#
# (ie myreport.php  is named myreport )
# name of php file  without the '.php' extension
$modTitle = "XMail Server Status";

#
# this is used to detect loaded modules
# key must be the same as name
#val is array of required modules
$reqRepMod['server'] = array();

#
# this is the function to perform the report
# must be called ($modulenName)Do() and recieves no parameters
function serverDo () {
	if ( ( strlen( $GLOBALS['xmailPass'] ) > 0 ) && ( strlen( $GLOBALS['xmailLogin'] ) > 0 ) ) {
		$xSock = openXmail();
		$usersComp = fetchXmailUsers( $xSock );
		closeXmail( $xSock );
		$retVal = TRUE;
	} else {
		logIt( "No login information for the server was found" );
		$retVal = FALSE;
	}
	return $retVal;
}

#
# the reports code belongs below
function highServerScores ( $allSmtp ) {
	global $highScoreCount;
}

function closeXmail ( $xSock ) {
	$retVal = socket_close( $xSock );
	return $retVal;
}

function fetchXmailUsers( $xSock ) {
	# returns an array with user emails or aliases as keys.
	# if the address is an alias, it's forward is the key's value
	# if no alias, the value is TRUE
	memUse( "Starting to fetch users from Xmail Server" );
	$req = "userlist\n";
	socket_write( $xSock, $req, strlen( $req ) );
	$list = "";
	while( @socket_recv( $xSock, $recv, 32, MSG_WAITALL ) ) {
		$list .= $recv;
	}
	$grep = "/\\\"([\\S|^\\\"]*)\\\"\t\\\"([\\S|^\\\"]*)\\\".*/i";
	if ( !preg_match_all( $grep, $list, $users ) )	{
		mex( 35, $xmailBanner);
	}
	$userComp = array();
	foreach ( $users[1] as $key => $val ) {
		$usersComp[$users[2][$key] . "@" . $val] = TRUE;
	}
	$domains = array_unique( $users[1] );
	memUse( "Processed user list and domain list from XMail server" );
	logIt( "Total " . count( $usersComp ) . " users in " . count( $domains ) . " domains." );
	unset( $domains, $users );
	$req = "aliaslist\n";
	socket_write( $xSock, $req, strlen( $req ) );
	$list = "";
	while( @socket_recv( $xSock, $recv, 32, MSG_WAITALL ) ) {
		$list .= $recv;
	}
	$grep = "/\\\"([\\S|^\\\"]*)\\\"\t\\\"([\\S|^\\\"]*)\\\"\t\\\"([\\S|^\\\"]*)\\\".*/i";
	if ( !preg_match_all( $grep, $list, $users ) )	{
		mex( 36, $xmailBanner);
	}
	$dem = array_unique( $users[3] );
	foreach ( $users[1] as $key => $val ) {
		if ( $users[3][$key] ) {
			$usersComp[$users[2][$key] . "@" . $val] = $users[3][$key];
		}
	}
	logIt( "Total " . count( $users[3] ) . " aliases added to " . count( array_unique( $users[3] ) ) . " users." );
	unset ( $users, $xmailBanner, $req, $grep, $list );
	$retVal = $usersComp;
}

function openXmail () {
	global $xmailServer, $xmailPort, $fqp, $xmailLogin, $uP, $domains, $xmailPass;
	$retVal = FALSE;
	if ( $GLOBALS['truePass'] == TRUE ) {
		$xmailPassword = $xmailPass;
	} else {
		$xmailPassword = genPassUcr( $xmailPass );
	}
	logIt( "Attempting connection to '" . $fqp . "'.", _me );
	if ( $xSock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP ) ) {
	   if ( @socket_set_option( $xSock, SOL_SOCKET, SO_RCVTIMEO, array( "sec" => 2, "usec" => 0 ) ) ) {	
			if ( @socket_connect( $xSock, $xmailServer, intval( $xmailPort ) ) ) {
				socket_getsockname( $xSock, $connIp, $connPort );
				logIt( "Connected " . $connIp . ":" . $connPort . " to " . gethostbyname( $xmailServer ) . ":" . $xmailPort . ".", _me );
				if ( !socket_recv( $xSock, $recv, 2048, MSG_WAITALL ) ) {
					mex( 12, $fqp );
				}
				if ( preg_match( "/(XMail (\S*) CTRL.*)\;(.*)/", $recv, $subs ) ) {
					$xmailVer = trim( $subs[2] ); // future quirks
					$xmailBanner = trim( $subs[1] );
					$xmailConnectTime = trim( $subs[3] );
					logIt("Connected to '" . $xmailBanner . "' at " . $xmailConnectTime . ".", _me);
					$req = $xmailLogin . "\t" . $xmailPassword . "\n";
					socket_write( $xSock, $req, strlen( $req ) );
					if ( socket_recv( $xSock, $recv, 2048, MSG_WAITALL ) ) {
						if ( strrpos( $recv, "+00000 OK" ) === false ) {
							mex( 34, $fqp . " Response: '" . trim( $recv ) . "'" );
						}
					} else {
						mex( 34, $fqp );
					}
					logIt( "User '" . $xmailLogin . "' logged in to " . $xmailServer . ".", $fqp );
				} else {
					mex( 32, $fqp . " Response: '" . trim( $recv ) . "'");
				}
				$retVal = $xSock;
			} else {
				mex( 11, trim($fqp) );
			}
		}
	}
	return $retVal;
}
?>
Return current item: XMail Log Archiver