1 | <?php |
---|
2 | |
---|
3 | /*************************************************************************** |
---|
4 | * Expresso Livre * |
---|
5 | * http://www.expressolivre.org * |
---|
6 | * -------------------------------------------- * |
---|
7 | * This program is free software; you can redistribute it and/or modify it * |
---|
8 | * under the terms of the GNU General Public License as published by the * |
---|
9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
10 | * option) any later version. * |
---|
11 | \**************************************************************************/ |
---|
12 | /* |
---|
13 | ####################################################################### |
---|
14 | #### Author : Harish Chauhan #### |
---|
15 | #### Start Date : 14 Oct,2004 #### |
---|
16 | #### End Date : -- Oct,2004 #### |
---|
17 | #### Updated : 18 Feb,2010 #### |
---|
18 | #### Modified by : jakjr, niltonneto #### |
---|
19 | #### Description : Additional Imap Class for not-implemented #### |
---|
20 | #### functions into PHP-IMAP extension. #### |
---|
21 | ####################################################################### |
---|
22 | */ |
---|
23 | |
---|
24 | class imapfp |
---|
25 | { |
---|
26 | var $host; // host like 127.0.0.1 or mail.yoursite.com |
---|
27 | var $port; // port default is 110 or 143 |
---|
28 | var $user; // user for logon |
---|
29 | var $imap; // imap object |
---|
30 | var $password; // user paswword |
---|
31 | var $state; // variable define different state of connection |
---|
32 | var $connection; // handle to a open connection |
---|
33 | var $error; // error string |
---|
34 | var $must_update; |
---|
35 | var $tag; |
---|
36 | var $mail_box; |
---|
37 | |
---|
38 | function imapfp() |
---|
39 | { |
---|
40 | $this->imap = new imap_functions(); |
---|
41 | $this->user = $this->imap->username; |
---|
42 | $this->password = $this->imap->password; |
---|
43 | $this->host = $this->imap->imap_server; |
---|
44 | $this->port = $this->imap->imap_port; |
---|
45 | $this->state="DISCONNECTED"; |
---|
46 | $this->connection=null; |
---|
47 | $this->error=""; |
---|
48 | $this->must_update=false; |
---|
49 | $this->tag=uniqid("HKC"); |
---|
50 | $this->imap_delimiter = $_SESSION['phpgw_info']['expressomail']['email_server']['imapDelimiter']; |
---|
51 | } |
---|
52 | |
---|
53 | function get_error() |
---|
54 | { |
---|
55 | if($this->error) |
---|
56 | return $this->error; |
---|
57 | } |
---|
58 | |
---|
59 | function get_state() |
---|
60 | { |
---|
61 | return $this->state; |
---|
62 | } |
---|
63 | |
---|
64 | function open($host="",$port="") |
---|
65 | { |
---|
66 | if(!empty($host)) |
---|
67 | { |
---|
68 | if ($port == 993) |
---|
69 | $this->host="ssl://$host"; |
---|
70 | else |
---|
71 | $this->host=$host; |
---|
72 | } |
---|
73 | if(!empty($port)) |
---|
74 | $this->port=$port; |
---|
75 | return $this->open_connection(); |
---|
76 | } |
---|
77 | |
---|
78 | function close() |
---|
79 | { |
---|
80 | if($this->must_update) |
---|
81 | $this->close_mailbox(); |
---|
82 | $this->logout(); |
---|
83 | @fclose($this->connection); |
---|
84 | $this->connection=null; |
---|
85 | $this->state="DISCONNECTED"; |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | function get_mailboxes_size() |
---|
90 | { |
---|
91 | // INBOX |
---|
92 | if($this->put_line($this->tag . " GETANNOTATION \"user".$this->imap_delimiter.$this->user ."\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
---|
93 | { |
---|
94 | $response_inbox=$this->get_server_responce(); |
---|
95 | |
---|
96 | if(substr($response_inbox,strpos($response_inbox,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
97 | { |
---|
98 | $this->error= "Error : $response !<br>"; |
---|
99 | return false; |
---|
100 | } |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | $this->error= "Error : Could not send User request. <br>"; |
---|
105 | return false; |
---|
106 | } |
---|
107 | $response_inbox_array = split("\r\n", $response_inbox); |
---|
108 | array_pop($response_inbox_array); |
---|
109 | array_shift($response_inbox_array); |
---|
110 | |
---|
111 | // SUB_FOLDERS |
---|
112 | if($this->put_line($this->tag . " GETANNOTATION \"user".$this->imap_delimiter.$this->user .$this->imap_delimiter."*\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
---|
113 | { |
---|
114 | $response_sub=$this->get_server_responce(); |
---|
115 | |
---|
116 | if(substr($response_sub,strpos($response_sub,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
117 | { |
---|
118 | $this->error= "Error : $response !<br>"; |
---|
119 | return false; |
---|
120 | } |
---|
121 | } |
---|
122 | else |
---|
123 | { |
---|
124 | $this->error= "Error : Could not send User request. <br>"; |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | $response_sub_array = split("\r\n", $response_sub); |
---|
129 | array_pop($response_sub_array); |
---|
130 | array_shift($response_sub_array); |
---|
131 | |
---|
132 | return array_merge($response_inbox_array, $response_sub_array); |
---|
133 | } |
---|
134 | |
---|
135 | //This function is used to get response line from server |
---|
136 | function get_line() |
---|
137 | { |
---|
138 | while(!feof($this->connection)) |
---|
139 | { |
---|
140 | $line.=fgets($this->connection); |
---|
141 | if(strlen($line)>=2 && substr($line,-2)=="\r\n") |
---|
142 | return(substr($line,0,-2)); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | //This function is to retrive the full response message from server |
---|
147 | function get_server_responce() |
---|
148 | { |
---|
149 | $i=0; |
---|
150 | while(1) |
---|
151 | { |
---|
152 | $i++; |
---|
153 | $response.="\r\n".$this->get_line(); |
---|
154 | if(substr($response,strpos($response,$this->tag),strlen($this->tag))==$this->tag) |
---|
155 | break; |
---|
156 | |
---|
157 | //jakjr |
---|
158 | if ($i>300) |
---|
159 | { |
---|
160 | if ($response) |
---|
161 | return $response; |
---|
162 | else |
---|
163 | return false; |
---|
164 | } |
---|
165 | } |
---|
166 | return $response; |
---|
167 | } |
---|
168 | // This function is to send the command to server |
---|
169 | function put_line($msg="") |
---|
170 | { |
---|
171 | return @fputs($this->connection,"$msg\r\n"); |
---|
172 | } |
---|
173 | |
---|
174 | //This function is to open the connection to the server |
---|
175 | function open_connection() |
---|
176 | { |
---|
177 | if($this->state!="DISCONNECTED") |
---|
178 | { |
---|
179 | $this->error= "Error : Already Connected!<br>"; |
---|
180 | return false; |
---|
181 | } |
---|
182 | if(empty($this->host) || empty($this->port)) |
---|
183 | { |
---|
184 | $this->error= "Error : Either HOST or PORT is undifined!<br>"; |
---|
185 | return false; |
---|
186 | } |
---|
187 | $this->connection= fsockopen($this->host, $this->port, $errno, $errstr, 5); |
---|
188 | if(!$this->connection) |
---|
189 | { |
---|
190 | $this->error= "Could not make a connection to server , Error : $errstr ($errno)<br>"; |
---|
191 | return false; |
---|
192 | } |
---|
193 | $respone=$this->get_line(); |
---|
194 | $this->state="AUTHORIZATION"; |
---|
195 | return true; |
---|
196 | } |
---|
197 | |
---|
198 | //The logout function informs the server that the client is done with the connection. |
---|
199 | function logout() |
---|
200 | { |
---|
201 | //jakjr |
---|
202 | if(($this->state!="AUTHORIZATION") && ($this->state!="AUTHENTICATED")) |
---|
203 | { |
---|
204 | $this->error= "Error : No Connection Found!<br>"; |
---|
205 | return false; |
---|
206 | } |
---|
207 | if($this->put_line($this->tag." LOGOUT")) |
---|
208 | { |
---|
209 | $response=$this->get_server_responce(); |
---|
210 | if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
211 | { |
---|
212 | $this->error= "Error : $response !<br>"; |
---|
213 | return false; |
---|
214 | } |
---|
215 | } |
---|
216 | else |
---|
217 | { |
---|
218 | $this->error= "Error : Could not send User request. <br>"; |
---|
219 | return false; |
---|
220 | } |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | //this function is used to login into server $user is a valid username and $pwd is a valid password. |
---|
225 | function login($user,$pwd) |
---|
226 | { |
---|
227 | $this->user = $user; |
---|
228 | |
---|
229 | if($this->state=="DISCONNECTED") |
---|
230 | { |
---|
231 | $this->error= "Error : No Connection Found!<br>"; |
---|
232 | return false; |
---|
233 | } |
---|
234 | if($this->state=="AUTHENTICATED") |
---|
235 | { |
---|
236 | $this->error= "Error : Already Authenticated!<br>"; |
---|
237 | return false; |
---|
238 | } |
---|
239 | if($this->put_line($this->tag." LOGIN $user $pwd")) |
---|
240 | { |
---|
241 | $response=$this->get_server_responce(); |
---|
242 | |
---|
243 | if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
244 | { |
---|
245 | $this->error= "Error : $response !<br>"; |
---|
246 | return false; |
---|
247 | } |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | $this->error= "Error : Could not send User request. <br>"; |
---|
252 | return false; |
---|
253 | } |
---|
254 | $this->state="AUTHENTICATED"; |
---|
255 | return true; |
---|
256 | } |
---|
257 | } |
---|
258 | ?> |
---|