summaryrefslogtreecommitdiff
path: root/plugins/password/drivers/poppassd.php
blob: e18ec26607677ab0138541865c89c6b08e1ff0a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

/**
 * Poppassd Password Driver
 *
 * Driver to change passwords via Poppassd/Courierpassd
 *
 * @version 2.0
 * @author Philip Weir
 *
 */

class rcube_poppassd_password
{
    function format_error_result($code, $line)
    {
        if (preg_match('/^\d\d\d\s+(\S.*)\s*$/', $line, $matches)) {
            return array('code' => $code, 'message' => $matches[1]);
        } else {
            return $code;
        }
    }

    function save($curpass, $passwd)
    {
        $rcmail = rcmail::get_instance();
//    include('Net/Socket.php');
        $poppassd = new Net_Socket();

        $result = $poppassd->connect($rcmail->config->get('password_pop_host'), $rcmail->config->get('password_pop_port'), null);
        if (PEAR::isError($result)) {
            return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage());
        }
        else {
            $result = $poppassd->readLine();
            if(!preg_match('/^2\d\d/', $result)) {
                $poppassd->disconnect();
                return $this->format_error_result(PASSWORD_ERROR, $result);
            }
            else {
                $poppassd->writeLine("user ". $_SESSION['username']);
                $result = $poppassd->readLine();
                if(!preg_match('/^[23]\d\d/', $result) ) {
                    $poppassd->disconnect();
                    return $this->format_error_result(PASSWORD_CONNECT_ERROR, $result);
                }
                else {
                    $poppassd->writeLine("pass ". $curpass);
                    $result = $poppassd->readLine();
                    if(!preg_match('/^[23]\d\d/', $result) ) {
                        $poppassd->disconnect();
                        return $this->format_error_result(PASSWORD_ERROR, $result);
                    }
                    else {
                        $poppassd->writeLine("newpass ". $passwd);
                        $result = $poppassd->readLine();
                        $poppassd->disconnect();
                        if (!preg_match('/^2\d\d/', $result))
                            return $this->format_error_result(PASSWORD_ERROR, $result);
                        else
                            return PASSWORD_SUCCESS;
                    }
                }
            }
        }
    }
}