1<?php
2/*
3 * User-friendly interface to SIEVE server-side mail filtering.
4 * Plugin for Squirrelmail 1.4+
5 *
6 * Licensed under the GNU GPL. For full terms see the file COPYING that came
7 * with the Squirrelmail distribution.
8 *
9 * @version DO_Sieve_LdapSieve.class.php,v 1.0 2007-01-22 10:10:10
10 * @authors Boris Maroutaeff <boris.maroutaeff@uclouvain.be>,
11 *          Laurent Buset <laurent.buset@uclouvain.be>
12 *          Pascal Maes <pascal.maes@uclouvain.be>
13 * @copyright 2006-2008
14 * @package plugins
15 * @subpackage avelsieve
16 */
17
18/**
19 * Backend for Sieve script management interface for Sun JES Messaging Server
20 * The rules are stored in the attribute mailsieverulesource of the LDAP server
21 */
22class DO_Sieve_LdapSieve extends DO_Sieve {
23    var $loggedin = false;
24    var $sieveServerAddress;
25    var $sieveUsername;
26
27    function DO_Sieve_LdapSieve() {
28        global $username, $ldap_server,$avelsieve_hard_capabilities;
29
30        $this->DO_Sieve();
31
32        /* Get Cached Capabilities if they exist. */
33
34        sqgetGlobalVar('sieve_capabilities', $sieve_capabilities, SQ_SESSION);
35        if(isset($sieve_capabilities)) {
36            $this->capabilities = $sieve_capabilities;
37        }
38
39        sqgetGlobalVar('rules', $rules, SQ_SESSION);
40        if(isset($rules)) {
41            $this->rules = $rules;
42        }
43
44        $this->sieveUsername = $username;
45        $this->sieveLdapServer = $ldap_server;
46    $this->sieveHardcodedCapabilities = $avelsieve_hard_capabilities;
47    }
48
49    /**
50    * This function does nothing
51    *
52    */
53    function init() {
54
55    }
56
57    /**
58     * Login to LDAP server. Also saves the capabilities in Session.
59     *
60     * @return boolean
61     */
62    function login() {
63
64        if($this->loggedin) {
65        return true;
66        }
67
68        $this->sieveLdapHost = $this->sieveLdapServer[0]['host'];
69    $this->sieveLdapBase = $this->sieveLdapServer[0]['base'];
70
71    /*  Anonymous connexion to retrieve dn */
72
73    $this->sieveLdapConn = ldap_connect($this->sieveLdapHost);
74    $this->ldapbind = ldap_bind($this->sieveLdapConn)
75        or die("Unable to connect to LDAP server, contact your administrator");
76    $this->sr = ldap_search($this->sieveLdapConn, $this->sieveLdapBase, "uid=$this->sieveUsername");
77    $this->info = ldap_get_entries($this->sieveLdapConn, $this->sr);
78    $this->dn = $this->info[0]["dn"];
79    ldap_close($this->sieveLdapConn);
80
81    /* Authenticated connexion to LDAP server */
82
83    sqgetGlobalVar('key', $key, SQ_COOKIE);
84    sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
85
86    /* Need the cleartext password to connect to the LDAP server */
87    $acctpass = OneTimePadDecrypt($key, $onetimepad);
88
89    if(!$this->dn || !$acctpass)
90        die("Error: unable to find DN or invalid password.");
91    $this->sieveLdapConn = ldap_connect($this->sieveLdapHost);
92    $this->ldapbind = ldap_bind($this->sieveLdapConn, $this->dn, $acctpass)
93        or die("Unable to bind to LDAP server, contact your administrator");
94
95    if(!isset($this->sieve_capabilities)) {
96        $this->capabilities = $sieve_capabilities = $this->sieveHardcodedCapabilities;
97        $_SESSION['sieve_capabilities'] = $sieve_capabilities;
98    }
99
100        $this->loggedin = true;
101        return true;
102    }
103
104    /**
105     * Get rules from attribute mailsieverulesource of LDAP server
106     *
107     * @param string $scriptname (NULL in this case : not used)
108     * @param array $rules
109     * @param array $scriptinfo
110     * @return boolean
111     */
112    function load($scriptname = NULL, &$rules, &$scriptinfo) {
113        $rules = array();
114        $scriptinfo = array();
115
116        if(!$this->loggedin) {
117            $this->login();
118        }
119
120        /* Get rules from LDAP server. */
121
122        $this->sr = ldap_search($this->sieveLdapConn, $this->sieveLdapBase, "uid=$this->sieveUsername", array("mailsieverulesource"))
123            or die("Unable to receive results from LDAP server, contact your administrator");
124        $this->infos = ldap_get_entries($this->sieveLdapConn, $this->sr)
125            or die("Unable to get results from LDAP server, contact your administrator");
126
127    /* All the rules are store in only one value of the attribute mailsieverulesource */
128
129        $sievescript = $this->infos[0]["mailsieverulesource"][0];
130        //ldap_close($this->sieveLdapConn);
131        //$this->loggedin = false;
132
133        /* Extract rules from $sievescript. */
134        $rules = avelsieve_extract_rules($sievescript, $scriptinfo);
135        return true;
136    }
137
138    /**
139    * Upload rules
140    *
141    * @param string $newscript The SIEVE script to be uploaded
142    * @param string $scriptname (NULL in this case : not used)
143    * @return true on success, false upon failure
144    */
145    function save($newscript, $scriptname = NULL) {
146
147        if(!$this->loggedin) {
148            $this->login();
149        }
150        $attrs["mailsieverulesource"] = stripslashes($newscript);
151    if (!ldap_mod_replace($this->sieveLdapConn, $this->dn, $attrs)) {
152        /* Just to be safe. */
153            $errormsg = _("Could not set active script on your IMAP server ");
154            $errormsg .= $this->sieveLdapServer . ".<br />";
155            $errormsg .= _("Please contact your administrator.");
156            print_errormsg($errormsg);
157            return false;
158    }
159    return true;
160        //ldap_close($this->sieveLdapConn);
161        //$this->loggedin = false;
162    }
163
164    /**
165     * Delete rules stored in LDAP server.
166     *
167     * @param string $script (NULL in this case : not used)
168     * @return true on success, false upon failure
169     */
170    function delete($script = NULL) {
171        if(!$this->loggedin) {
172            $this->login();
173        }
174        $attrs["mailsieverulesource"] = array();
175    if (!ldap_mod_del($this->sieveLdapConn, $this->dn, $attrs)) {
176            $errormsg = _("Could not delete script from server ");
177            $errormsg .= $this->sieveLdapServer . ".<br />";
178            $errormsg .= _("Please contact your administrator.");
179            print_errormsg($errormsg);
180            return false;
181    }
182    return true;
183        //ldap_close($this->sieveLdapConn);
184        //$this->loggedin = false;
185    }
186
187    /**
188     * Log Out from ManageSieve Server.
189     */
190    function logout() {
191        $ldap_close($this->sieveLdapConn);
192    }
193}
194
195