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