| 1 | <?php |
| 2 | /* |
| 3 | ##################################################### |
| 4 | #### #### |
| 5 | #### Author : Harish Chauhan #### |
| 6 | #### Start Date : 14 Oct,2004 #### |
| 7 | #### End Date : -- Oct,2004 #### |
| 8 | #### Updated : #### |
| 9 | #### #### |
| 10 | ##################################################### |
| 11 | modified by : jakjr |
| 12 | */ |
| 13 | include_once("class.imap_functions.inc.php"); |
| 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 | |
| 81 | // todas as subfolder |
| 82 | //if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."/*\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
| 83 | |
| 84 | //Somente a inbox |
| 85 | //if($this->put_line($this->tag . " GETANNOTATION \"user/".$this->user ."\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
| 86 | |
| 87 | //Tudo |
| 88 | //if($this->put_line($this->tag . " GETANNOTATION \"user/" . $this->user . "*\" \"/vendor/cmu/cyrus-imapd/size\" \"value.shared\"" )) |
| 89 | |
| 90 | |
| 91 | // INBOX |
| 92 | if($this->put_line($this->tag . " GETANNOTATION \"user/".$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->user ."/*\" \"/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 | /* |
| 259 | * TODO: Futuramente esse m?todo deve ser embutido na classe imap_functions, |
| 260 | * conforme m?todo 'get_quota_folders' comentado. |
| 261 | */ |
| 262 | function get_mailboxes_quota(){ |
| 263 | |
| 264 | if(!$this->open($this->host,$this->port)) |
| 265 | return $this->get_error(); |
| 266 | |
| 267 | if (!$this->login( $this->user,$this->password )) |
| 268 | return $this->get_error(); |
| 269 | |
| 270 | $response_array = $this->get_mailboxes_size(); |
| 271 | |
| 272 | if ($this->error) |
| 273 | return $this->get_error(); |
| 274 | |
| 275 | $data = array(); |
| 276 | $quota_root = $this->imap->get_quota(array('folder_id' => "INBOX")); |
| 277 | $data["quota_root"] = $quota_root; |
| 278 | |
| 279 | foreach ($response_array as $idx=>$line) { |
| 280 | $line2 = str_replace('"', "", $line); |
| 281 | $line2 = str_replace(" /vendor/cmu/cyrus-imapd/size (value.shared ",";",str_replace("* ANNOTATION ","",$line2)); |
| 282 | list($folder,$size) = explode(";",$line2); |
| 283 | $size = str_replace(")","",$size); |
| 284 | $quotaPercent = (($size / 1048576) / $data["quota_root"]["quota_limit"])*100; |
| 285 | if ($size < 1048576 && $size > 1024) |
| 286 | $quota_used = round($size / 1024, 0).' Kb'; |
| 287 | else if($size > 1024) |
| 288 | $quota_used = round($size / (1024*1024), 1).' Mb'; |
| 289 | else |
| 290 | $quota_used = $size." b"; |
| 291 | $folder = mb_convert_encoding($folder, "ISO_8859-1", "UTF7-IMAP"); |
| 292 | if(!preg_match('/user\\'.$this->imap->imap_delimiter.$this->user.'\\'.$this->imap->imap_delimiter.'/i',$folder)){ |
| 293 | $folder = $this->imap->functions->getLang("Inbox"); |
| 294 | } |
| 295 | else |
| 296 | $folder = preg_replace('/user\\'.$this->imap->imap_delimiter.$this->user.'\\'.$this->imap->imap_delimiter.'/i','', $folder); |
| 297 | |
| 298 | $data[$folder] = array("quota_percent" => sprintf("%.1f",round($quotaPercent,1)), "quota_used" => $quota_used); |
| 299 | } |
| 300 | return $data; |
| 301 | } |
| 302 | |
| 303 | } |
| 304 | |
| 305 | /* function get_quota_folders(){ |
| 306 | include_once("class.imapfp.inc.php"); |
| 307 | $imapfp = new imapfp(); |
| 308 | |
| 309 | if(!$imapfp->open($this->imap_server,$this->imap_port)) |
| 310 | return $imapfp->get_error(); |
| 311 | if (!$imapfp->login( $this->username,$this->password )) |
| 312 | return $imapfp->get_error(); |
| 313 | |
| 314 | $response_array = $imapfp->get_mailboxes_size(); |
| 315 | if ($imapfp->error) |
| 316 | return $imapfp->get_error(); |
| 317 | |
| 318 | $data = array(); |
| 319 | $quota_root = $this->get_quota(array('folder_id' => "INBOX")); |
| 320 | $data["quota_limit"] = $quota_root['quota_limit']; |
| 321 | |
| 322 | foreach ($response_array as $idx=>$line) { |
| 323 | $line2 = str_replace('"', "", $line); |
| 324 | sscanf(rtrim(ltrim($line2)),"* ANNOTATION %s /vendor/cmu/cyrus-imapd/size (value.shared %d)",$folder, $size); |
| 325 | $quotaPercent = (($size / 1048576) / $data["quota_limit"])*100; |
| 326 | if ($size < 1048576 && $size > 1024) |
| 327 | $quota_used = round($size / 1024, 0).' Kb'; |
| 328 | else if($size > 1024) |
| 329 | $quota_used = round($size / (1024*1024), 1).' Mb'; |
| 330 | else |
| 331 | $quota_used = $size." b"; |
| 332 | |
| 333 | if(!preg_match('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i',$folder)){ |
| 334 | $folder = $this->functions->getLang("Inbox"); |
| 335 | } |
| 336 | else |
| 337 | $folder = preg_replace('/user\\'.$this->imap_delimiter.$this->username.'\\'.$this->imap_delimiter.'/i','', $folder); |
| 338 | |
| 339 | $data[$folder] = array("quota_percent" => $quotaPercent, "quota_used" => $quota_used); |
| 340 | } |
| 341 | return $data; |
| 342 | }*/ |
| 343 | ?> |