File: 1.13.3b/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 -1; // failed to connect/open
 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: if (isset($NATS))
 72: {
 73: class FreeNATS_IMAP_Test extends FreeNATS_Local_Test
 74: 	{
 75: 	function DoTest($testname,$param,$hostname,$timeout,$params)
 76: 		{ // 0: host, 1: user, 2: pass, 3: protocol, 4: port, 5: ssl (1/0)
 77: 		if ($params[5]==1) $ssl=true;
 78: 		else $ssl=false;
 79: 		
 80: 		$ip=ip_lookup($params[0]);
 81: 		if ($ip=="0") return -1;
 82: 		
 83: 		return imap_test_time($ip,$params[1],$params[2],$timeout,$params[3],$params[4],$ssl);	
 84: 		
 85: 		}
 86: 	function Evaluate($result) 
 87: 		{
 88: 		if ($result<=0) return 2; // failed
 89: 		return 0; // passed
 90: 		}
 91: 	
 92: 	function ProtectOutput(&$test)
 93: 		{
 94: 		$test['testparam2']="";
 95: 		}
 96: 		
 97: 	function DisplayForm(&$row)
 98: 		{
 99: 		echo "<table border=0>";
100: 		echo "<tr><td align=left>";
101: 		echo "Hostname :";
102: 		echo "</td><td align=left>";
103: 		echo "<input type=text name=testparam size=30 maxlength=128 value=\"".$row['testparam']."\">";
104: 		echo "</td></tr>";
105: 		echo "<tr><td align=left>";
106: 		echo "Username :";
107: 		echo "</td><td align=left>";
108: 		echo "<input type=text name=testparam1 size=30 maxlength=128 value=\"".$row['testparam1']."\">";
109: 		echo "</td></tr>";
110: 		echo "<tr><td align=left>";
111: 		echo "Password :";
112: 		echo "</td><td align=left>";
113: 		//echo "<input type=password name=testparam2 size=30 maxlength=128 value=\"".$row['testparam2']."\">"; // debug
114: 		echo "<input type=text name=testparam2 size=30 maxlength=128 value=\"\">";
115: 		echo "<input type=hidden name=keepparam2 value=1>";
116: 		echo "</td></tr>";
117: 		echo "<tr><td colspan=2><i>Leave blank to not change or <input type=checkbox name=clearparam2 value=1> click to clear</i></td></tr>";
118: 		echo "<tr><td align=left>";
119: 		echo "Protocol :";
120: 		echo "</td><td align=left>";
121: 		if ($row['testparam3']=="") $protocol="imap";
122: 		else $protocol=$row['testparam3'];
123: 		echo "<select name=testparam3>";
124: 		echo "<option value=".$protocol.">".$protocol."</option>";
125: 		echo "<option value=imap>imap</option>";
126: 		echo "<option value=pop3>pop3</option>";
127: 		echo "</select>";
128: 		echo "</td></tr>";
129: 		echo "<tr><td align=left>";
130: 		echo "Port :";
131: 		echo "</td><td align=left>";
132: 		echo "<input type=text name=testparam4 size=10 maxlength=128 value=\"".$row['testparam4']."\">";
133: 		echo "</td></tr>";
134: 		echo "<tr><td colspan=2><i>Leave blank use protocol default port (110, 143 etc)</i></td></tr>";
135: 		echo "<tr><td align=left>";
136: 		echo "SSL :";
137: 		echo "</td><td align=left>";
138: 		if ($row['testparam5']==1) $s=" checked";
139: 		else $s="";
140: 		echo "<input type=checkbox name=testparam5 value=1".$s.">";
141: 		echo "</td></tr>";
142: 		echo "</table>";
143: 		}
144: 		
145: 	}
146: $params=array();
147: $NATS->Tests->Register("imap","FreeNATS_IMAP_Test",$params,"IMAP Connect",2,"FreeNATS IMAP/POP Tester");
148: $NATS->Tests->SetUnits("imap","Seconds","s");
149: }
150: 
151: 
152: ?>