File: 1.01.4b/server/base/tests/smtp.inc.php (View as Code)

1: 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 smtp_test_connect($hostname,$port=25,$timeout=-1,$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.smtp.timeout",0); 31: if ($timeout<=0) $timeout=20; // unset specifically or in environment 32: } 33: 34: if ($port=="") $port=25; 35: $errno=0; 36: $errstr=0; 37: $fp=@fsockopen($hostname,$port,$errno,$errstr,$timeout); 38: if ($errno!=0) return 0; 39: if ($fp<=0) return 0; 40: stream_set_timeout($fp,$timeout); 41: $header=@fgets($fp,128); 42: if ($debug) echo $header."\n"; 43: if (substr($header,0,3)!="220") return 0; 44: if (@fputs($fp,"HELO freenats\n")===false) return 0; 45: $body=@fgets($fp,128); 46: if ($debug) echo $body."\n"; 47: if ($body=="") return 0; 48: if (@fputs($fp,"QUIT\n")===false) return 0; 49: @fclose($fp); 50: return 1; 51: } 52: 53: function smtp_test_time($hostname,$port=25,$timeout=-1,$debug=false) 54: { 55: $timer=new TFNTimer(); 56: $timer->Start(); 57: $res=smtp_test_connect($hostname,$port,$timeout,$debug); 58: $time=$timer->Stop(); 59: if ($res<=0) return $res; // connect failed 60: $time=round($time,4); 61: if ($time==0) $time=0.0001; 62: return $time; 63: } 64: 65: if (isset($NATS)) 66: { 67: class FreeNATS_SMTP_Test extends FreeNATS_Local_Test 68: { 69: function DoTest($testname,$param,$hostname,$timeout,$params) 70: { 71: // Pre-resolve DNS 72: $ip=ip_lookup($params[0]); 73: if ($ip=="0") return -1; 74: // Do the test 75: return smtp_test_time($ip,$params[1],$timeout); 76: } 77: function Evaluate($result) 78: { 79: if ($result<0) return 2; // failed 80: return 0; // passed 81: } 82: /* // -- this was put in as a test, code left to easily retest with if required - denies host to API 83: function ProtectOutput(&$test) 84: { 85: $test['testparam']=""; 86: } 87: */ 88: } 89: $params=array( "Hostname", "Port/Defaults to 25 if 0 or unset" ); 90: $NATS->Tests->Register("smtp","FreeNATS_SMTP_Test",$params,"SMTP Connect",1,"FreeNATS SMTP Tester"); 91: $NATS->Tests->SetUnits("smtp","Seconds","s"); 92: } 93: 94: 95: ?>