1: <?php
2:
3: /**
4: * Jyxo PHP Library
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file license.txt.
10: * It is also available through the world-wide-web at this URL:
11: * https://github.com/jyxo/php/blob/master/license.txt
12: */
13:
14: namespace Jyxo\Beholder\TestCase;
15:
16: /**
17: * Tests IMAP server availability.
18: *
19: * @category Jyxo
20: * @package Jyxo\Beholder
21: * @copyright Copyright (c) 2005-2011 Jyxo, s.r.o.
22: * @license https://github.com/jyxo/php/blob/master/license.txt
23: * @author Jaroslav HanslĂk
24: */
25: class Imap extends \Jyxo\Beholder\TestCase
26: {
27: /**
28: * Host.
29: *
30: * @var string
31: */
32: private $host;
33:
34: /**
35: * Username.
36: *
37: * @var string
38: */
39: private $user;
40:
41: /**
42: * Password.
43: *
44: * @var string
45: */
46: private $password;
47:
48: /**
49: * Port.
50: *
51: * @var integer
52: */
53: private $port;
54:
55: /**
56: * Validate certificates.
57: *
58: * @var boolean
59: */
60: private $validateCert;
61:
62: /**
63: * Constructor.
64: *
65: * @param string $description Test description
66: * @param string $host Server hostname
67: * @param string $user Username
68: * @param string $password Password
69: * @param integer $port Port
70: * @param boolean $validateCert Validate certificates
71: */
72: public function __construct($description, $host = 'localhost', $user = '', $password = '', $port = 143, $validateCert = true)
73: {
74: parent::__construct($description);
75:
76: $this->host = (string) $host;
77: $this->user = (string) $user;
78: $this->password = (string) $password;
79: $this->port = (int) $port;
80: $this->validateCert = (bool) $validateCert;
81: }
82:
83: /**
84: * Performs the test.
85: *
86: * @return \Jyxo\Beholder\Result
87: */
88: public function run()
89: {
90: // The imap extension is required
91: if (!extension_loaded('imap')) {
92: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::NOT_APPLICABLE, 'Extension imap missing');
93: }
94:
95: // Label for status
96: $description = sprintf('%s@%s:%s', $this->user, $this->host, $this->port);
97:
98: $imap = imap_open('{' . $this->host . ':' . $this->port . '/' . (!$this->validateCert ? 'no' : '') . 'validate-cert}', $this->user, $this->password, OP_HALFOPEN, 1);
99: if (false === $imap) {
100: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::FAILURE, sprintf('Connection error %s', $description));
101: }
102: imap_close($imap);
103:
104: return new \Jyxo\Beholder\Result(\Jyxo\Beholder\Result::SUCCESS, $description);
105: }
106: }
107: