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