PageRenderTime 248ms CodeModel.GetById 111ms app.highlight 11ms RepoModel.GetById 120ms app.codeStats 0ms

/library/Zend/Mail/Protocol/Pop3.php

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 461 lines | 206 code | 64 blank | 191 comment | 33 complexity | 3a2ee85eda44ac359489d59757a17ce9 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 * 
 15 * @category   Zend
 16 * @package    Zend_Mail
 17 * @subpackage Protocol
 18 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id: Pop3.php 9098 2008-03-30 19:29:10Z thomas $
 21 */
 22
 23
 24/**
 25 * @category   Zend
 26 * @package    Zend_Mail
 27 * @subpackage Protocol
 28 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 29 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 30 */
 31class Zend_Mail_Protocol_Pop3
 32{
 33    /**
 34     * saves if server supports top
 35     * @var null|bool
 36     */
 37    public $hasTop = null;
 38
 39    /**
 40     * socket to pop3
 41     * @var null|resource
 42     */
 43    protected $_socket;
 44
 45    /**
 46     * greeting timestamp for apop
 47     * @var null|string
 48     */
 49    protected $_timestamp;
 50
 51
 52    /**
 53     * Public constructor
 54     *
 55     * @param  string      $host  hostname of IP address of POP3 server, if given connect() is called
 56     * @param  int|null    $port  port of POP3 server, null for default (110 or 995 for ssl)
 57     * @param  bool|string $ssl   use ssl? 'SSL', 'TLS' or false
 58     * @throws Zend_Mail_Protocol_Exception
 59     */
 60    public function __construct($host = '', $port = null, $ssl = false)
 61    {
 62        if ($host) {
 63            $this->connect($host, $port, $ssl);
 64        }
 65    }
 66
 67
 68    /**
 69     * Public destructor
 70     */
 71    public function __destruct()
 72    {
 73        $this->logout();
 74    }
 75
 76
 77    /**
 78     * Open connection to POP3 server
 79     *
 80     * @param  string      $host  hostname of IP address of POP3 server
 81     * @param  int|null    $port  of POP3 server, default is 110 (995 for ssl)
 82     * @param  string|bool $ssl   use 'SSL', 'TLS' or false
 83     * @return string welcome message
 84     * @throws Zend_Mail_Protocol_Exception
 85     */
 86    public function connect($host, $port = null, $ssl = false)
 87    {
 88        if ($ssl == 'SSL') {
 89            $host = 'ssl://' . $host;
 90        }
 91
 92        if ($port === null) {
 93            $port = $ssl == 'SSL' ? 995 : 110;
 94        }
 95
 96        $this->_socket = @fsockopen($host, $port);
 97        if (!$this->_socket) {
 98            /**
 99             * @see Zend_Mail_Protocol_Exception
100             */
101            require_once 'Zend/Mail/Protocol/Exception.php';
102            throw new Zend_Mail_Protocol_Exception('cannot connect to host');
103        }
104
105        $welcome = $this->readResponse();
106
107        strtok($welcome, '<');
108        $this->_timestamp = strtok('>');
109        if (!strpos($this->_timestamp, '@')) {
110            $this->_timestamp = null;
111        } else {
112            $this->_timestamp = '<' . $this->_timestamp . '>';
113        }
114
115        if ($ssl === 'TLS') {
116            $this->request('STLS');
117            $result = stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
118            if (!$result) {
119                /**
120                 * @see Zend_Mail_Protocol_Exception
121                 */
122                require_once 'Zend/Mail/Protocol/Exception.php';
123                throw new Zend_Mail_Protocol_Exception('cannot enable TLS');
124            }
125        }
126
127        return $welcome;
128    }
129
130
131    /**
132     * Send a request
133     *
134     * @param string $request your request without newline
135     * @return null
136     * @throws Zend_Mail_Protocol_Exception
137     */
138    public function sendRequest($request)
139    {
140        $result = @fputs($this->_socket, $request . "\r\n");
141        if (!$result) {
142            /**
143             * @see Zend_Mail_Protocol_Exception
144             */
145            require_once 'Zend/Mail/Protocol/Exception.php';
146            throw new Zend_Mail_Protocol_Exception('send failed - connection closed?');
147        }
148    }
149
150
151    /**
152     * read a response
153     *
154     * @param  boolean $multiline response has multiple lines and should be read until "<nl>.<nl>"
155     * @return string response
156     * @throws Zend_Mail_Protocol_Exception
157     */
158    public function readResponse($multiline = false)
159    {
160        $result = @fgets($this->_socket);
161        if (!is_string($result)) {
162            /**
163             * @see Zend_Mail_Protocol_Exception
164             */
165            require_once 'Zend/Mail/Protocol/Exception.php';
166            throw new Zend_Mail_Protocol_Exception('read failed - connection closed?');
167        }
168
169        $result = trim($result);
170        if (strpos($result, ' ')) {
171            list($status, $message) = explode(' ', $result, 2);
172        } else {
173            $status = $result;
174            $message = '';
175        }
176
177        if ($status != '+OK') {
178            /**
179             * @see Zend_Mail_Protocol_Exception
180             */
181            require_once 'Zend/Mail/Protocol/Exception.php';
182            throw new Zend_Mail_Protocol_Exception('last request failed');
183        }
184
185        if ($multiline) {
186            $message = '';
187            $line = fgets($this->_socket);
188            while ($line && trim($line) != '.') {
189                $message .= $line;
190                $line = fgets($this->_socket);
191            };
192        }
193
194        return $message;
195    }
196
197
198    /**
199     * Send request and get resposne
200     *
201     * @see sendRequest(), readResponse()
202     *
203     * @param  string $request    request
204     * @param  bool   $multiline  multiline response?
205     * @return string             result from readResponse()
206     * @throws Zend_Mail_Protocol_Exception
207     */
208    public function request($request, $multiline = false)
209    {
210        $this->sendRequest($request);
211        return $this->readResponse($multiline);
212    }
213
214
215    /**
216     * End communication with POP3 server (also closes socket)
217     *
218     * @return null
219     */
220    public function logout()
221    {
222        if (!$this->_socket) {
223            return;
224        }
225
226        try {
227            $this->request('QUIT');
228        } catch (Zend_Mail_Protocol_Exception $e) {
229            // ignore error - we're closing the socket anyway
230        }
231
232        fclose($this->_socket);
233        $this->_socket = null;
234    }
235
236
237    /**
238     * Get capabilities from POP3 server
239     *
240     * @return array list of capabilities
241     * @throws Zend_Mail_Protocol_Exception
242     */
243    public function capa()
244    {
245        $result = $this->request('CAPA', true);
246        return explode("\n", $result);
247    }
248
249
250    /**
251     * Login to POP3 server. Can use APOP
252     *
253     * @param  string $user      username
254     * @param  string $password  password
255     * @param  bool   $try_apop  should APOP be tried?
256     * @return void
257     * @throws Zend_Mail_Protocol_Exception
258     */
259    public function login($user, $password, $tryApop = true)
260    {
261        if ($tryApop && $this->_timestamp) {
262            try {
263                $this->request("APOP $user " . md5($this->_timestamp . $password));
264                return;
265            } catch (Zend_Mail_Protocol_Exception $e) {
266                // ignore
267            }
268        }
269
270        $result = $this->request("USER $user");
271        $result = $this->request("PASS $password");
272    }
273
274
275    /**
276     * Make STAT call for message count and size sum
277     *
278     * @param  int $messages  out parameter with count of messages
279     * @param  int $octets    out parameter with size in octects of messages
280     * @return void
281     * @throws Zend_Mail_Protocol_Exception
282     */
283    public function status(&$messages, &$octets)
284    {
285        $messages = 0;
286        $octets = 0;
287        $result = $this->request('STAT');
288
289        list($messages, $octets) = explode(' ', $result);
290    }
291
292
293    /**
294     * Make LIST call for size of message(s)
295     *
296     * @param  int|null $msgno number of message, null for all
297     * @return int|array size of given message or list with array(num => size)
298     * @throws Zend_Mail_Protocol_Exception
299     */
300    public function getList($msgno = null)
301    {
302        if ($msgno !== null) {
303            $result = $this->request("LIST $msgno");
304
305            list(, $result) = explode(' ', $result);
306            return (int)$result;
307        }
308
309        $result = $this->request('LIST', true);
310        $messages = array();
311        $line = strtok($result, "\n");
312        while ($line) {
313            list($no, $size) = explode(' ', trim($line));
314            $messages[(int)$no] = (int)$size;
315            $line = strtok("\n");
316        }
317
318        return $messages;
319    }
320
321
322    /**
323     * Make UIDL call for getting a uniqueid
324     *
325     * @param  int|null $msgno number of message, null for all
326     * @return string|array uniqueid of message or list with array(num => uniqueid)
327     * @throws Zend_Mail_Protocol_Exception
328     */
329    public function uniqueid($msgno = null)
330    {
331        if ($msgno !== null) {
332            $result = $this->request("UIDL $msgno");
333
334            list(, $result) = explode(' ', $result);
335            return $result;
336        }
337
338        $result = $this->request('UIDL', true);
339
340        $result = explode("\n", $result);
341        $messages = array();
342        foreach ($result as $line) {
343            if (!$line) {
344                continue;
345            }
346            list($no, $id) = explode(' ', trim($line), 2);
347            $messages[(int)$no] = $id;
348        }
349
350        return $messages;
351
352    }
353
354
355    /**
356     * Make TOP call for getting headers and maybe some body lines
357     * This method also sets hasTop - before it it's not known if top is supported
358     *
359     * The fallback makes normale RETR call, which retrieves the whole message. Additional
360     * lines are not removed.
361     *
362     * @param  int  $msgno    number of message
363     * @param  int  $lines    number of wanted body lines (empty line is inserted after header lines)
364     * @param  bool $fallback fallback with full retrieve if top is not supported
365     * @return string message headers with wanted body lines
366     * @throws Zend_Mail_Protocol_Exception
367     */
368    public function top($msgno, $lines = 0, $fallback = false)
369    {
370        if ($this->hasTop === false) {
371            if ($fallback) {
372                return $this->retrieve($msgno);
373            } else {
374                /**
375                 * @see Zend_Mail_Protocol_Exception
376                 */
377                require_once 'Zend/Mail/Protocol/Exception.php';
378                throw new Zend_Mail_Protocol_Exception('top not supported and no fallback wanted');
379            }
380        }
381        $this->hasTop = true;
382
383        $lines = (!$lines || $lines < 1) ? 0 : (int)$lines;
384
385        try {
386            $result = $this->request("TOP $msgno $lines", true);
387        } catch (Zend_Mail_Protocol_Exception $e) {
388            $this->hasTop = false;
389            if ($fallback) {
390                $result = $this->retrieve($msgno);
391            } else {
392                throw $e;
393            }
394        }
395
396        return $result;
397    }
398
399
400    /**
401     * Make a RETR call for retrieving a full message with headers and body
402     *
403     * @deprecated since 1.1.0; this method has a typo - please use retrieve()
404     * @param  int $msgno  message number
405     * @return string message
406     * @throws Zend_Mail_Protocol_Exception
407     */
408    public function retrive($msgno)
409    {
410        return $this->retrieve($msgno);
411    }
412
413
414    /**
415     * Make a RETR call for retrieving a full message with headers and body
416     *
417     * @param  int $msgno  message number
418     * @return string message
419     * @throws Zend_Mail_Protocol_Exception
420     */
421    public function retrieve($msgno)
422    {
423        $result = $this->request("RETR $msgno", true);
424        return $result;
425    }
426
427    /**
428     * Make a NOOP call, maybe needed for keeping the server happy
429     *
430     * @return null
431     * @throws Zend_Mail_Protocol_Exception
432     */
433    public function noop()
434    {
435        $this->request('NOOP');
436    }
437
438
439    /**
440     * Make a DELE count to remove a message
441     *
442     * @return null
443     * @throws Zend_Mail_Protocol_Exception
444     */
445    public function delete($msgno)
446    {
447        $this->request("DELE $msgno");
448    }
449
450
451    /**
452     * Make RSET call, which rollbacks delete requests
453     *
454     * @return null
455     * @throws Zend_Mail_Protocol_Exception
456     */
457    public function undelete()
458    {
459        $this->request('RSET');
460    }
461}