summaryrefslogtreecommitdiff
path: root/plugins/password/drivers/poppassd.php
blob: fc105de47da3bfc60b488dbc0916060086412796 (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
<?php

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

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 password_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 format_error_result(PASSWORD_CONNECT_ERROR, $result->getMessage());
    }
    else {
        $result = $poppassd->readLine();
        if(!preg_match('/^2\d\d/', $result)) {
            $poppassd->disconnect();
            return 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 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 format_error_result(PASSWORD_ERROR, $result);
                }
                else {
                    $poppassd->writeLine("newpass ". $passwd);
                    $result = $poppassd->readLine();
                    $poppassd->disconnect();
                    if (!preg_match('/^2\d\d/', $result))
                        return format_error_result(PASSWORD_ERROR, $result);
                    else
                        return PASSWORD_SUCCESS;
                }
            }
        }
    }
}