1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
imap.php
См. документацию.
1<?php
2
4
6{
7 protected $imap_stream;
8 protected $counter;
9
10 public function __construct()
11 {
12 $this->counter = 0;
13 }
14
15 public function connect($host, $port, $timeout = 1, $skip_cert = false)
16 {
17 $imap_stream = @stream_socket_client(
18 sprintf('%s:%s', $host, $port), $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT,
19 stream_context_create(array('ssl' => array('verify_peer' => !$skip_cert, 'verify_peer_name' => !$skip_cert)))
20 );
21
22 if ($imap_stream === false)
23 throw new Exception(GetMessage('MAIL_IMAP_ERR_CONNECT'));
24
25 stream_set_timeout($imap_stream, $timeout);
26 $this->imap_stream = $imap_stream;
27
28 $prompt = $this->readLine();
29
30 if ($prompt === false)
31 throw new Exception(GetMessage('MAIL_IMAP_ERR_COMMUNICATE'));
32
33 if (mb_strpos($prompt, '* OK') !== 0)
34 {
35 $this->imap_stream = null;
36
37 throw new Exception(GetMessage('MAIL_IMAP_ERR_CONNECT').': '.GetMessage('MAIL_IMAP_ERR_BAD_SERVER'));
38 }
39/*
40 $tag = $this->sendCommand("STARTTLS\r\n");
41 $res = $this->readResponse($tag);
42
43 if (strpos($res, $tag.' OK') !== false)
44 {
45 $a = stream_socket_enable_crypto($this->imap_stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
46 }
47*/
48 }
49
50 public function authenticate($login, $password)
51 {
52 $tag = $this->sendCommand("AUTHENTICATE PLAIN\r\n");
53
54 $prompt = $this->readLine();
55 if (mb_strpos($prompt, '+') !== 0)
56 {
57 if (mb_strpos($prompt, $tag.' NO') === 0 || mb_strpos($prompt, $tag.' BAD') === 0)
58 throw new Exception(GetMessage('MAIL_IMAP_ERR_AUTH_MECH'));
59 else
60 throw new Exception(GetMessage('MAIL_IMAP_ERR_AUTH').': '.GetMessage('MAIL_IMAP_ERR_BAD_SERVER'));
61 }
62
63 fputs($this->imap_stream, base64_encode("\x00".$login."\x00".$password)."\r\n");
64
65 $response = $this->readResponse($tag);
66
67 if (mb_strpos($response, $tag.' OK') === false)
68 {
69 if (mb_strpos($response, $tag.' NO') === 0 || mb_strpos($response, $tag.' BAD') === 0)
70 throw new Exception(GetMessage('MAIL_IMAP_ERR_AUTH'));
71 else
72 throw new Exception(GetMessage('MAIL_IMAP_ERR_AUTH').': '.GetMessage('MAIL_IMAP_ERR_BAD_SERVER'));
73 }
74 }
75
76 public function getUnseen()
77 {
78 $unseen = 0;
79
80 $tag = $this->sendCommand("SELECT \"INBOX\"\r\n");
81 $response = $this->readResponse($tag);
82
83 if (mb_strpos($response, $tag.' OK') === false)
84 throw new Exception(GetMessage('MAIL_IMAP_ERR_BAD_SERVER'));
85
86 $tag = $this->sendCommand("SEARCH UNSEEN\r\n");
87 $response = $this->readResponse($tag);
88
89 if (mb_strpos($response, $tag.' OK') === false)
90 throw new Exception(GetMessage('MAIL_IMAP_ERR_BAD_SERVER'));
91
92 if (preg_match('/\* SEARCH( [0-9 ]*)?/i', $response, $matches))
93 {
94 if (isset($matches[1]))
95 $unseen = count(preg_split('/\s+/', $matches[1], null, PREG_SPLIT_NO_EMPTY));
96 }
97
98 return $unseen;
99 }
100
101 protected function sendCommand($command)
102 {
103 $this->counter++;
104
105 $tag = sprintf('A%03u', $this->counter);
106
107 $command = sprintf('%s %s', $tag, $command);
108 $bytes = fputs($this->imap_stream, $command);
109
110 if ($bytes < mb_strlen($command))
111 throw new Exception(GetMessage('MAIL_IMAP_ERR_COMMUNICATE'));
112
113 return $tag;
114 }
115
116 protected function readLine()
117 {
118 $line = '';
119
120 do
121 {
122 $buffer = fgets($this->imap_stream, 4096);
123
124 if ($buffer === false)
125 return false;
126
127 $line .= $buffer;
128 }
129 while (mb_strpos($buffer, "\r\n") === false);
130
131 return $line;
132 }
133
134 protected function readResponse($tag)
135 {
136 $response = '';
137
138 do
139 {
140 $line = $this->readLine();
141
142 if ($line === false)
143 return false;
144
145 $response .= $line;
146 }
147 while (mb_strpos($line, $tag) !== 0);
148
149 return $response;
150 }
151
152}
$login
Определения change_password.php:8
Определения imap.php:6
__construct()
Определения imap.php:10
authenticate($login, $password)
Определения imap.php:50
connect($host, $port, $timeout=1, $skip_cert=false)
Определения imap.php:15
readLine()
Определения imap.php:116
$imap_stream
Определения imap.php:7
sendCommand($command)
Определения imap.php:101
readResponse($tag)
Определения imap.php:134
$counter
Определения imap.php:8
getUnseen()
Определения imap.php:76
$bytes
Определения cron_html_pages.php:17
if(!defined("ADMIN_AJAX_MODE") &&(($_REQUEST["mode"] ?? '') !='excel')) $buffer
Определения epilog_admin_after.php:40
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$host
Определения .description.php:9
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$password
Определения result.php:7
$response
Определения result.php:21
$matches
Определения index.php:22