summaryrefslogtreecommitdiff
path: root/lib/KolabAdmin/Sieve.php
blob: 0233da45b6d46b3f0c2a9ba5d9083c7970a00112 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
/**
 * Manages Kolab user sieve scripts.
 *
 * PHP version 5
 *
 * @category Kolab
 * @package  KolabAdmin
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */

/**
 * Manages Kolab user sieve scripts.
 *
 * Copyright 2010 Klarälvdalens Datakonsult AB
 *
 * See the enclosed file COPYING for license information (LGPL). If you did not
 * receive this file, see
 * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 * @category Kolab
 * @package  KolabAdmin
 * @author   Gunnar Wrobel <wrobel@pardus.de>
 * @license  http://www.fsf.org/copyleft/lgpl.html LGPL
 * @link     http://www.kolab.org
 */
class KolabAdmin_Sieve
{

    const SCRIPT = 'kolab.siv';

    const OLD_SCRIPT_DELIVERY = 'kolab-deliver.siv';

    const OLD_SCRIPT_FORWARD  = 'kolab-forward.siv';

    const OLD_SCRIPT_VACATION = 'kolab-vacation.siv';


    /**
     * The sieve connection.
     */
    private $_sieve;

    /**
     * The sieve script segments.
     */
    private $_segments;

    /**
     * Constructor.
     *
     * @param Net_Sieve $sieve The sieve connection.
     */
    public function __construct(Net_Sieve $sieve)
    {
        $this->_sieve = $sieve;
    }

    private function _init()
    {
        if ($this->_segments === null) {
            $scripts = $this->_sieve->listScripts();

            if (!empty($scripts)) {
                if (in_array(self::SCRIPT, $scripts)) {
                    $this->_segments = $this->_splitSegments(
                        $this->_sieve->getScript(self::SCRIPT)
                    );
                } else {
                    if (in_array(self::OLD_SCRIPT_DELIVERY, $scripts)) {
                        $this->_segments['delivery'] = new KolabAdmin_Sieve_Segment_Delivery(
                            $this->_sieve->getScript(self::OLD_SCRIPT_DELIVERY)
                        );
                        if ($this->_sieve->getActive() == self::OLD_SCRIPT_DELIVERY) {
                            $this->_segments['delivery']->setActive();
                        }
                    }
                    if (in_array(self::OLD_SCRIPT_FORWARD, $scripts)) {
                        $this->_segments['forward'] = new KolabAdmin_Sieve_Segment_Forward(
                            $this->_sieve->getScript(self::OLD_SCRIPT_FORWARD)
                        );
                        if ($this->_sieve->getActive() == self::OLD_SCRIPT_FORWARD) {
                            $this->_segments['forward']->setActive();
                        }
                    }
                    if (in_array(self::OLD_SCRIPT_VACATION, $scripts)) {
                        $this->_segments['vacation'] = new KolabAdmin_Sieve_Segment_Vacation(
                            $this->_sieve->getScript(self::OLD_SCRIPT_VACATION)
                        );
                        if ($this->_sieve->getActive() == self::OLD_SCRIPT_VACATION) {
                            $this->_segments['vacation']->setActive();
                        }
                    }
                }
            }
            if (!isset($this->_segments['delivery'])) {
                $this->_segments['delivery'] = new KolabAdmin_Sieve_Segment_Delivery();
            }
            if (!isset($this->_segments['forward'])) {
                $this->_segments['forward'] = new KolabAdmin_Sieve_Segment_Forward();
            }
            if (!isset($this->_segments['vacation'])) {
                $this->_segments['vacation'] = new KolabAdmin_Sieve_Segment_Vacation();
            }
        }
    }

    private function _splitSegments($script)
    {
        $segments = array();
        preg_match_all('/### SEGMENT START [^#]*.*?### SEGMENT END [^#]*/s', $script, $matches);
        foreach ($matches[0] as $match) {
            preg_match('/### SEGMENT START ([^# ]*) ?(ENABLED)?/s', $match, $id);
            if (!empty($id[1])) {
                $type = strtolower($id[1]);
                $class = 'KolabAdmin_Sieve_Segment_' . ucfirst($type);
                $segments[$type] = new $class($match);
                if (isset($id[2]) && $id[2] == 'ENABLED') {
                    $segments[$type]->setActive();
                }
            }
        }
        return $segments;
    }

    public function fetchDeliverySegment()
    {
        $this->_init();
        return $this->_segments['delivery'];
    }

    public function fetchForwardSegment()
    {
        $this->_init();
        return $this->_segments['forward'];
    }

    public function fetchVacationSegment()
    {
        $this->_init();
        return $this->_segments['vacation'];
    }

    public function store()
    {
        $result = $this->_sieve->installScript(self::SCRIPT, $this->getScript(), true);
        if ($result instanceOf PEAR_Error) {
            throw new Exception($result->getMessage());
        }
    }

    public function getScript()
    {
        $script = 'require "fileinto";' . "\r\n" . 'require "vacation";' . "\r\n\r\n\r\n";
        foreach ($this->_segments as $segment) {
            $script .= $segment->generate() . "\r\n\r\n";
        }
        return $script;
    }
}