File: 0.04.30a/server/base/tests/imap.inc.php (View as HTML)

  1: <?php // imap.inc.php -- IMAP and POP3 test
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeNATS is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: function imap_test_connect($host,$user,$pass,$timeout=-1,$protocol="imap",$port=-1,$ssl=false,$debug=false)
 24: {
 25: global $NATS;
 26: if ($timeout>0) $timeout=$timeout; // use specific for test if set
 27: else
 28: 	{
 29: 	// otherwise use system if available
 30: 	if (isset($NATS)) $timeout=$NATS->Cfg->Get("test.imap.timeout",0);
 31: 	if ($timeout<=0) $timeout=0; // unset specifically or in environment
 32: 	}
 33: 	
 34: if ($timeout>0) imap_timeout(IMAP_OPENTIMEOUT,$timeout);
 35: 
 36: if ($port<=0)
 37: 	{
 38: 	$port=143; // default
 39: 	if ( ($protocol=="imap") && ($ssl) ) $port=993;
 40: 	else if ($protocol=="pop3")
 41: 		{
 42: 		if ($ssl) $port=995;
 43: 		else $port=110;
 44: 		}
 45: 	}
 46: 
 47: $mailbox="{".$host.":".$port."/service=".$protocol;
 48: if ($ssl) $mailbox.="/ssl";
 49: $mailbox.="/novalidate-cert";
 50: $mailbox.="}INBOX";
 51: if ($debug) echo $user.":".$pass."@".$mailbox."\n";
 52: $imap=@imap_open($mailbox,$user,$pass);
 53: if ($imap===false) return 0;
 54: 
 55: @imap_close($imap);
 56: return 1;
 57: }
 58: 
 59: function imap_test_time($host,$user,$pass,$timeout=-1,$protocol="imap",$port=-1,$ssl=false,$debug=false)
 60: {
 61: $timer=new TFNTimer();
 62: $timer->Start();
 63: $res=imap_test_connect($host,$user,$pass,$timeout,$protocol,$port,$ssl,$debug);
 64: $time=$timer->Stop();
 65: if ($res<=0) return $res; // test failed to connect
 66: $time=round($time,4);
 67: if ($time==0) $time=0.0001;
 68: return $time;
 69: }
 70: 
 71: ?>