[3157] | 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 | } |
---|
| 51 | |
---|
| 52 | function get_error() |
---|
| 53 | { |
---|
| 54 | if($this->error) |
---|
| 55 | return $this->error; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | function get_state() |
---|
| 59 | { |
---|
| 60 | return $this->state; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | function open($host="",$port="") |
---|
| 64 | { |
---|
| 65 | if(!empty($host)) |
---|
| 66 | { |
---|
| 67 | if ($port == 993) |
---|
| 68 | $this->host="ssl://$host"; |
---|
| 69 | else |
---|
| 70 | $this->host=$host; |
---|
| 71 | } |
---|
| 72 | if(!empty($port)) |
---|
| 73 | $this->port=$port; |
---|
| 74 | return $this->open_connection(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | function close() |
---|
| 78 | { |
---|
| 79 | if($this->must_update) |
---|
| 80 | $this->close_mailbox(); |
---|
| 81 | $this->logout(); |
---|
| 82 | @fclose($this->connection); |
---|
| 83 | $this->connection=null; |
---|
| 84 | $this->state="DISCONNECTED"; |
---|
| 85 | return true; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | function get_mailboxes_size() |
---|
| 89 | { |
---|
| 90 | // INBOX |
---|
| 91 | if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
---|
| 92 | { |
---|
| 93 | $response_inbox=$this->get_server_responce(); |
---|
| 94 | |
---|
| 95 | if(substr($response_inbox,strpos($response_inbox,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
| 96 | { |
---|
| 97 | $this->error= "Error : $response !<br>"; |
---|
| 98 | return false; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | $this->error= "Error : Could not send User request. <br>"; |
---|
| 104 | return false; |
---|
| 105 | } |
---|
| 106 | $response_inbox_array = split("\r\n", $response_inbox); |
---|
| 107 | array_pop($response_inbox_array); |
---|
| 108 | array_shift($response_inbox_array); |
---|
| 109 | |
---|
| 110 | // SUB_FOLDERS |
---|
| 111 | if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."/*\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
---|
| 112 | { |
---|
| 113 | $response_sub=$this->get_server_responce(); |
---|
| 114 | |
---|
| 115 | if(substr($response_sub,strpos($response_sub,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
| 116 | { |
---|
| 117 | $this->error= "Error : $response !<br>"; |
---|
| 118 | return false; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
| 123 | $this->error= "Error : Could not send User request. <br>"; |
---|
| 124 | return false; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | $response_sub_array = split("\r\n", $response_sub); |
---|
| 128 | array_pop($response_sub_array); |
---|
| 129 | array_shift($response_sub_array); |
---|
| 130 | |
---|
| 131 | return array_merge($response_inbox_array, $response_sub_array); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | //This function is used to get response line from server |
---|
| 135 | function get_line() |
---|
| 136 | { |
---|
| 137 | while(!feof($this->connection)) |
---|
| 138 | { |
---|
| 139 | $line.=fgets($this->connection); |
---|
| 140 | if(strlen($line)>=2 && substr($line,-2)=="\r\n") |
---|
| 141 | return(substr($line,0,-2)); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | //This function is to retrive the full response message from server |
---|
| 146 | function get_server_responce() |
---|
| 147 | { |
---|
| 148 | $i=0; |
---|
| 149 | while(1) |
---|
| 150 | { |
---|
| 151 | $i++; |
---|
| 152 | $response.="\r\n".$this->get_line(); |
---|
| 153 | if(substr($response,strpos($response,$this->tag),strlen($this->tag))==$this->tag) |
---|
| 154 | break; |
---|
| 155 | |
---|
| 156 | //jakjr |
---|
| 157 | if ($i>300) |
---|
| 158 | { |
---|
| 159 | if ($response) |
---|
| 160 | return $response; |
---|
| 161 | else |
---|
| 162 | return false; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | return $response; |
---|
| 166 | } |
---|
| 167 | // This function is to send the command to server |
---|
| 168 | function put_line($msg="") |
---|
| 169 | { |
---|
| 170 | return @fputs($this->connection,"$msg\r\n"); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | //This function is to open the connection to the server |
---|
| 174 | function open_connection() |
---|
| 175 | { |
---|
| 176 | if($this->state!="DISCONNECTED") |
---|
| 177 | { |
---|
| 178 | $this->error= "Error : Already Connected!<br>"; |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | if(empty($this->host) || empty($this->port)) |
---|
| 182 | { |
---|
| 183 | $this->error= "Error : Either HOST or PORT is undifined!<br>"; |
---|
| 184 | return false; |
---|
| 185 | } |
---|
| 186 | $this->connection= fsockopen($this->host, $this->port, $errno, $errstr, 5); |
---|
| 187 | if(!$this->connection) |
---|
| 188 | { |
---|
| 189 | $this->error= "Could not make a connection to server , Error : $errstr ($errno)<br>"; |
---|
| 190 | return false; |
---|
| 191 | } |
---|
| 192 | $respone=$this->get_line(); |
---|
| 193 | $this->state="AUTHORIZATION"; |
---|
| 194 | return true; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | //The logout function informs the server that the client is done with the connection. |
---|
| 198 | function logout() |
---|
| 199 | { |
---|
| 200 | //jakjr |
---|
| 201 | if(($this->state!="AUTHORIZATION") && ($this->state!="AUTHENTICATED")) |
---|
| 202 | { |
---|
| 203 | $this->error= "Error : No Connection Found!<br>"; |
---|
| 204 | return false; |
---|
| 205 | } |
---|
| 206 | if($this->put_line($this->tag." LOGOUT")) |
---|
| 207 | { |
---|
| 208 | $response=$this->get_server_responce(); |
---|
| 209 | if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
| 210 | { |
---|
| 211 | $this->error= "Error : $response !<br>"; |
---|
| 212 | return false; |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | else |
---|
| 216 | { |
---|
| 217 | $this->error= "Error : Could not send User request. <br>"; |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | return true; |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | //this function is used to login into server $user is a valid username and $pwd is a valid password. |
---|
| 224 | function login($user,$pwd) |
---|
| 225 | { |
---|
| 226 | $this->user = $user; |
---|
| 227 | |
---|
| 228 | if($this->state=="DISCONNECTED") |
---|
| 229 | { |
---|
| 230 | $this->error= "Error : No Connection Found!<br>"; |
---|
| 231 | return false; |
---|
| 232 | } |
---|
| 233 | if($this->state=="AUTHENTICATED") |
---|
| 234 | { |
---|
| 235 | $this->error= "Error : Already Authenticated!<br>"; |
---|
| 236 | return false; |
---|
| 237 | } |
---|
| 238 | if($this->put_line($this->tag." LOGIN $user $pwd")) |
---|
| 239 | { |
---|
| 240 | $response=$this->get_server_responce(); |
---|
| 241 | |
---|
| 242 | if(substr($response,strpos($response,"$this->tag ")+strlen($this->tag)+1,2)!="OK") |
---|
| 243 | { |
---|
| 244 | $this->error= "Error : $response !<br>"; |
---|
| 245 | return false; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
| 250 | $this->error= "Error : Could not send User request. <br>"; |
---|
| 251 | return false; |
---|
| 252 | } |
---|
| 253 | $this->state="AUTHENTICATED"; |
---|
| 254 | return true; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | ?> |
---|