1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17: class Ingo_Transport_Sivtest extends Ingo_Transport_Timsieved
18: {
19: 20: 21:
22: public function __construct($params = array())
23: {
24: $default_params = array(
25: 'hostspec' => 'localhost',
26: 'logintype' => '',
27: 'port' => 4190,
28: 'scriptname' => 'ingo',
29: 'admin' => '',
30: 'usetls' => true,
31: 'command' => '',
32: 'socket' => '',
33: );
34:
35: parent::__construct(array_merge($default_params, $params));
36:
37: $this->_support_shares = false;
38: }
39:
40: 41: 42: 43: 44:
45: protected function _connect()
46: {
47: if (!empty($this->_sieve)) {
48: return;
49: }
50:
51: $this->sivtestSocket(
52: $this->_params['username'],
53: $this->_params['password'],
54: $this->_params['hostspec']);
55:
56: $this->_sieve = new Net_Sieve(
57: $this->_params['username'],
58: $this->_params['password'],
59: 'unix://' . $this->_params['socket'],
60: 0,
61: null,
62: null,
63: false,
64: true,
65: $this->_params['usetls']);
66:
67: $res = $this->_sieve->getError();
68: if ($res instanceof PEAR_Error) {
69: unset($this->_sieve);
70: throw new Ingo_Exception($res);
71: }
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public function sivtestSocket($username, $password, $hostspec)
86: {
87: $command = '';
88: $error_return = '';
89:
90: if (strtolower($this->_params['logintype']) == 'gssapi' &&
91: isset($_SERVER['KRB5CCNAME'])) {
92: $command .= 'KRB5CCNAME=' . $_SERVER['KRB5CCNAME'];
93: }
94:
95: $domain_socket = 'unix://' . $this->_params['socket'];
96:
97: $command .= ' ' . $this->_params['command']
98: . ' -m ' . $this->_params['logintype']
99: . ' -u ' . $username
100: . ' -a ' . $username
101: . ' -w ' . $password
102: . ' -p ' . $this->_params['port']
103: . ' -X ' . $this->_params['socket']
104: . ' ' . $hostspec;
105:
106: $conn_attempts = 0;
107: while ($conn_attempts++ < 4) {
108: $attempts = 0;
109: if (!file_exists($this->_params['socket'])) {
110: exec($command . ' > /dev/null 2>&1');
111: sleep(1);
112: while (!file_exists($this->_params['socket'])) {
113: usleep(200000);
114: if ($attempts++ > 5) {
115: $error_return = ': No socket after 10 seconds of trying!';
116: continue 2;
117: }
118: }
119: }
120: $socket = new Net_Socket();
121: $error = $socket->connect($domain_socket, 0, true, 30);
122: if (!($error instanceof PEAR_Error)) {
123: break;
124: }
125:
126:
127: unlink($this->_params['socket']);
128: }
129:
130: if (!empty($error_return)) {
131: throw new Ingo_Exception($error_return);
132: }
133:
134: $status = $socket->getStatus();
135: if ($status instanceof PEAR_Error || $status['eof']) {
136: throw new Ingo_Exception(_("Failed to write to socket: (connection lost!)"));
137: }
138:
139: $error = $socket->writeLine("CAPABILITY");
140: if ($error instanceof PEAR_Error) {
141: throw new Ingo_Exception(_("Failed to write to socket: " . $error->getMessage()));
142: }
143:
144: $result = $socket->readLine();
145: if ($result instanceof PEAR_Error) {
146: throw new Ingo_Exception(_("Failed to read from socket: " . $error->getMessage()));
147: }
148:
149: if (preg_match('|^bye \(referral "(sieve://)?([^"]+)|i',
150: $result, $matches)) {
151: $socket->disconnect();
152:
153: $this->sivtestSocket($username, $password, $matches[2]);
154: } else {
155: $socket->disconnect();
156: exec($command . ' > /dev/null 2>&1');
157: sleep(1);
158: }
159: }
160:
161: }
162: