[2] | 1 | <?php |
---|
[1036] | 2 | |
---|
[2] | 3 | //Conecta com o Servidor e o serviço Sieve; |
---|
| 4 | class SieveS{ |
---|
| 5 | |
---|
| 6 | var $host; |
---|
| 7 | var $port; |
---|
| 8 | var $user; |
---|
| 9 | var $pass; |
---|
| 10 | var $proxy; |
---|
| 11 | |
---|
| 12 | var $implementation; |
---|
| 13 | var $saslmethods; |
---|
| 14 | var $extensions; |
---|
| 15 | var $starttls_avail; |
---|
| 16 | var $socket; |
---|
| 17 | var $socket_timeout; |
---|
| 18 | |
---|
| 19 | var $scriptlist; |
---|
| 20 | var $activescript; |
---|
| 21 | var $errstr; |
---|
| 22 | var $errnum; |
---|
| 23 | var $ScriptS; |
---|
| 24 | |
---|
| 25 | function SieveS(){ |
---|
| 26 | |
---|
| 27 | $this->host = $_SESSION['phpgw_info']['expressomail']['email_server']['imapSieveServer']; |
---|
| 28 | $this->port = $_SESSION['phpgw_info']['expressomail']['email_server']['imapSievePort']; |
---|
| 29 | $this->user = $_SESSION['phpgw_info']['expressomail']['user']['account_lid']; |
---|
| 30 | $this->pass = $_SESSION['phpgw_info']['expressomail']['user']['passwd']; |
---|
| 31 | $this->proxy = ''; |
---|
| 32 | |
---|
| 33 | $this->socket_timeout = 5; |
---|
| 34 | $this->implementation = array('unknown'); |
---|
| 35 | $this->saslmethods = array('unknown'); |
---|
| 36 | $this->extensions = array('unknown'); |
---|
| 37 | $this->starttls_avail = false; |
---|
| 38 | $this->scriptlist = array(); |
---|
| 39 | $this->activescript = ''; |
---|
| 40 | $this->errstr = ''; |
---|
| 41 | $this->errnum = ''; |
---|
| 42 | |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | function start(){ |
---|
| 46 | // Cria a conexao; |
---|
| 47 | if(!isset($this->socket)){ |
---|
| 48 | $this->socket = fsockopen($this->host, $this->port, $this->errnum, $this->errstr, "60"); |
---|
| 49 | |
---|
| 50 | } |
---|
| 51 | // Verifica a conexao; |
---|
| 52 | if(!$this->socket){ |
---|
| 53 | return "não conectado"; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | $said = $this->read(); |
---|
| 57 | if (!preg_match("/timsieved/i",$said)) { |
---|
| 58 | $this->close(); |
---|
| 59 | $this->errstr = "start: bad response from $this->host: $said"; |
---|
| 60 | return false; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | if (preg_match("/IMPLEMENTATION/",$said)){ |
---|
| 64 | while (!preg_match("/^OK/",$said)) { |
---|
| 65 | if (preg_match("/^\"IMPLEMENTATION\" +\"(.*)\"/",$said,$bits)){ |
---|
| 66 | $this->implementation = $bits[1]; |
---|
| 67 | } |
---|
| 68 | elseif (preg_match("/^\"SASL\" +\"(.*)\"/",$said,$bits)) { |
---|
| 69 | $auth_types = $bits[1]; |
---|
| 70 | $this->saslmethods = split(" ", $auth_types); |
---|
| 71 | } |
---|
| 72 | elseif (preg_match("/^\"SIEVE\" +\"(.*)\"/",$said,$bits)) { |
---|
| 73 | $extensions = $bits[1]; |
---|
| 74 | $this->extensions = split(" ", $extensions); |
---|
| 75 | } |
---|
| 76 | elseif (preg_match("/^\"STARTTLS\"/",$said)){ |
---|
| 77 | $this->starttls_avail = true; |
---|
| 78 | } |
---|
| 79 | $said = $this->read(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | else |
---|
| 83 | { |
---|
| 84 | // assume cyrus v1. |
---|
| 85 | if (preg_match("/\"(.+)\" +\"(.+)\"/",$said,$bits)) { |
---|
| 86 | $this->implementation = $bits[1]; |
---|
| 87 | $sasl_str = $bits[2]; // should look like: SASL={PLAIN,...} |
---|
| 88 | } |
---|
| 89 | if (preg_match("/SASL=\{(.+)\}/",$sasl_str,$morebits)) { |
---|
| 90 | $auth_types = $morebits[1]; |
---|
| 91 | $this->saslmethods = split(", ", $auth_types); |
---|
| 92 | }else { |
---|
| 93 | // a bit desperate if we get here. |
---|
| 94 | $this->implementation = $said; |
---|
| 95 | $this->saslmethods = $said; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | $authstr = $this->proxy . "\x00" . $this->user . "\x00" . $this->pass; |
---|
| 100 | $encoded = base64_encode($authstr); |
---|
| 101 | $len = strlen($encoded); |
---|
| 102 | |
---|
| 103 | //fputs($this->socket,"AUTHENTICATE \"PLAIN\" \{$len+}\r\n"); |
---|
| 104 | //fputs($this->socket,"$encoded\r\n"); |
---|
| 105 | |
---|
| 106 | fwrite($this->socket, 'AUTHENTICATE "PLAIN" {' . $len . '+}' . "\r\n"); |
---|
| 107 | fwrite($this->socket,"$encoded\r\n"); |
---|
| 108 | |
---|
| 109 | $said = $this->read(); |
---|
| 110 | |
---|
| 111 | if (preg_match("/NO/",$said)) { |
---|
| 112 | $this->close(); |
---|
| 113 | $this->errstr = "start: authentication failure connecting to $this->host"; |
---|
| 114 | return false; |
---|
| 115 | } |
---|
| 116 | elseif (!preg_match("/OK/",$said)) { |
---|
| 117 | $this->close(); |
---|
| 118 | $this->errstr = "start: bad authentication response from $this->host: $said"; |
---|
| 119 | return false; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return true; |
---|
| 123 | |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | function close(){ |
---|
| 127 | |
---|
| 128 | if(!$this->socket){ |
---|
| 129 | return true; |
---|
| 130 | } |
---|
| 131 | fputs($this->socket,"LOGOUT\r\n"); |
---|
| 132 | $rc = fclose($this->socket); |
---|
| 133 | if($rc != 1){ |
---|
| 134 | $this->errstr = "close: failed closing socket to $this->server"; |
---|
| 135 | return false; |
---|
| 136 | } |
---|
| 137 | return true; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | function read(){ |
---|
| 141 | |
---|
| 142 | $buffer = ''; |
---|
| 143 | |
---|
| 144 | // Verifca a conexao; |
---|
| 145 | if(!$this->socket){ |
---|
| 146 | return $buffer; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | //Funções do php |
---|
| 150 | socket_set_timeout($this->socket,$this->socket_timeout); |
---|
| 151 | socket_set_blocking($this->socket,true); |
---|
| 152 | |
---|
| 153 | //Lê um caracter de cada vez e o adiciona na variavel buffer; |
---|
[1149] | 154 | while ( is_resource( $this -> socket ) && ( ! feof( $this -> socket ) ) ) |
---|
| 155 | { |
---|
[2] | 156 | $char = fread($this->socket,1); |
---|
| 157 | |
---|
| 158 | $status = socket_get_status($this->socket); |
---|
| 159 | if($status['timed_out']) |
---|
| 160 | return $buffer; |
---|
| 161 | |
---|
| 162 | if(($char == "\n") || ($char == "\r")){ |
---|
| 163 | if($char == "\r") |
---|
| 164 | fread($this->socket,1); |
---|
| 165 | return $buffer; |
---|
| 166 | } |
---|
| 167 | $buffer .= $char; |
---|
| 168 | } |
---|
| 169 | return $buffer; |
---|
| 170 | } |
---|
| 171 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 172 | // Manipulação dos scripts |
---|
| 173 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 174 | |
---|
| 175 | function listscripts(){ |
---|
| 176 | |
---|
| 177 | $bits = ''; |
---|
| 178 | |
---|
| 179 | //Verifica a conexao |
---|
| 180 | if(!$this->socket){ |
---|
| 181 | $this->errstr = "listscripts: sem conexão para o servidor $this->host"; |
---|
| 182 | return false; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | $scripts = array(); |
---|
| 186 | fputs($this->socket,"LISTSCRIPTS\r\n"); |
---|
| 187 | |
---|
| 188 | $said = $this->read(); |
---|
| 189 | while (!preg_match("/^OK/",$said) && !preg_match("/^NO/",$said)) { |
---|
| 190 | |
---|
| 191 | // Cyrus v1 script lines look like '"script*"' with the |
---|
| 192 | // asterisk denoting the active script. Cyrus v2 script |
---|
| 193 | // lines will look like '"script" ACTIVE' if active. |
---|
| 194 | |
---|
| 195 | if (preg_match("/^\"(.+)\"\s*(.+)*$/m",$said,$bits)) { |
---|
| 196 | if (preg_match("/\*$/",$bits[1])){ |
---|
| 197 | $bits[1] = preg_replace("/\*$/","",$bits[1]); |
---|
| 198 | $this->activescript = $bits[1]; |
---|
| 199 | } |
---|
| 200 | if (isset($bits[2]) && $bits[2] == 'ACTIVE') |
---|
| 201 | $this->activescript = $bits[1]; |
---|
| 202 | array_push($scripts,$bits[1]); |
---|
| 203 | } |
---|
| 204 | $said = $this->read(); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | if (preg_match("/^OK/",$said)) { |
---|
| 208 | $this->scriptlist = $scripts; |
---|
| 209 | return $this->scriptlist; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | // Pega o conteudo do script no servidor |
---|
| 215 | function getscript(){ |
---|
| 216 | |
---|
| 217 | $scriptfile = $this->listscripts(); |
---|
| 218 | |
---|
| 219 | // verifica se existe o script; |
---|
| 220 | if($scriptfile == ""){ |
---|
| 221 | return "Falta o script"; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | if(!$this->socket){ |
---|
| 225 | return "Falha na conexao"; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | $script = ''; |
---|
| 229 | |
---|
| 230 | fputs($this->socket,"GETSCRIPT \"$scriptfile[0]\"\r\n"); |
---|
| 231 | $said = $this->read(); |
---|
| 232 | while ((!preg_match("/^OK/",$said)) && (!preg_match("/^NO/",$said))) { |
---|
| 233 | // replace newlines which read() removed |
---|
| 234 | if (!preg_match("/\n$/",$said)) $said .= "\n"; |
---|
| 235 | $script .= $said; |
---|
| 236 | $said = $this->read(); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | if($said == "OK"){ |
---|
| 240 | return $script; |
---|
| 241 | }else{ |
---|
| 242 | return false; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | //envia para o servidor o nome do script($scriptfile) e seu conteudo($script) |
---|
| 247 | function putscript ($scriptfile,$script) { |
---|
| 248 | if (!isset($scriptfile)) { |
---|
| 249 | $this->errstr = "Não foi possível enviar o script para o servidor"; |
---|
| 250 | return false; |
---|
| 251 | } |
---|
| 252 | if (!isset($script)) { |
---|
| 253 | $this->errstr = "Não foi possível enviar o script para o servidor"; |
---|
| 254 | return false; |
---|
| 255 | } |
---|
| 256 | if (!$this->socket) { |
---|
| 257 | $this->errstr = "Sem conexão com o servidor $this->server"; |
---|
| 258 | return false; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | $len = strlen($script); |
---|
| 262 | |
---|
| 263 | //fputs($this->socket,"PUTSCRIPT \"$scriptfile\" \{$len+}\r\n"); |
---|
| 264 | //fputs($this->socket,"$script\r\n"); |
---|
| 265 | |
---|
| 266 | fwrite($this->socket, 'PUTSCRIPT "'.$scriptfile.'" {' . $len . '+}' . "\r\n"); |
---|
| 267 | fwrite($this->socket,"$script\r\n"); |
---|
| 268 | |
---|
| 269 | $said = ''; |
---|
| 270 | while ($said == '') { |
---|
| 271 | $said = $this->read(); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | if (preg_match("/^OK/",$said)) { |
---|
| 275 | return true; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | $this->errstr = "Não foi possível enviar o $scriptfile: $said"; |
---|
| 279 | return false; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | // Ativa o script para o servico sieve; |
---|
| 283 | function activatescript ($scriptfile) { |
---|
| 284 | if (!isset($scriptfile)) { |
---|
| 285 | $this->errstr = "activatescript: no script file specified"; |
---|
| 286 | return false; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if (!$this->socket) { |
---|
| 290 | $this->errstr = "activatescript: no connection open to $this->server"; |
---|
| 291 | return false; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | fputs($this->socket,"SETACTIVE \"$scriptfile\"\r\n"); |
---|
| 295 | |
---|
| 296 | $said = $this->read(); |
---|
| 297 | |
---|
| 298 | if (preg_match("/^OK/",$said)) { |
---|
| 299 | return true; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | $this->errstr = "activatescript: could not activate script $scriptfile: $said"; |
---|
| 303 | return false; |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | // Deleta o script do serviço sieve; |
---|
| 307 | function deletescript ($scriptName) { |
---|
| 308 | if(!isset($scriptName)){ |
---|
| 309 | $this->errstr = "deletescript: no script file specified"; |
---|
| 310 | return false; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | // Verifica a conexão; |
---|
| 314 | if(!$this->socket){ |
---|
| 315 | $this->errstr = "deletescript : no connection open to $this->server"; |
---|
| 316 | return false; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | fputs($this->socket,"DELETESCRIPT \"$scriptName\"\r\n"); |
---|
| 320 | |
---|
| 321 | $said = $this->read(); |
---|
| 322 | |
---|
| 323 | if(preg_match("/^OK/",$said)) { |
---|
| 324 | return true; |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | $this->errstr = "deletescript: could not delete script $scriptName: $said"; |
---|
| 328 | return false; |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | ?> |
---|