summaryrefslogtreecommitdiff
blob: fab46b3d1c8fa16854ecf59063bcf63224b75908 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
 * Horde Hooks configuration file.
 *
 * THE HOOKS PROVIDED IN THIS FILE ARE EXAMPLES ONLY.  DO NOT ENABLE THEM
 * BLINDLY IF YOU DO NOT KNOW WHAT YOU ARE DOING.  YOU HAVE TO CUSTOMIZE THEM
 * TO MATCH YOUR SPECIFIC NEEDS AND SYSTEM ENVIRONMENT.
 *
 * This file is where you define any hooks, for preferences or general Horde
 * use, that your installation uses. The functions in this file can vastly
 * change how your installation behaves, so make sure to test out any changes
 * here before doing them in a production environment.
 *
 * Hook function names are automatically determined. The format of the name
 * is:
 *
 * _<type of hook>_hook_<name of hook>.
 *
 * Types of hooks that are defined in this file are 'prefs' (hooks to set the
 * value of preferences), 'horde' (hooks for the Horde Framework scripts) and
 * 'app' (where app is any Horde application name, like 'imp') hooks that are
 * application specific.
 *
 * So, a hook to set the preference 'theme' would be named
 * "_prefs_hook_theme".
 *
 * NOTE 1: Having a hook function in this file does NOT mean that the hook
 * will automatically be used. YOU MUST enable the hook. For preferences, set
 * 'hook' => true in that preferences attributes; for other hooks, there will
 * be a configuration option in each application's conf.php file such as
 * $conf['hooks']['hookname'] which must be set to true.
 *
 * NOTE 2: Preferences hooks are ONLY executed on login. Preferences are
 * cached during a users session and, to avoid unnecessary overhead every time
 * a preference is accessed, the results of hooks are cached as well. This
 * leads to ...
 *
 * NOTE 3: Any preference that is NOT LOCKED, that is set by a hook, WILL BE
 * SAVED WITH THAT VALUE. This means several things:
 * 1) Users will get the results of the hook set for them in their
 *    preferences.
 * 2) By virtue of this, the next time they log in and load their
 *    preferences, the hook will NOT be called, because in their last session,
 *    we saved the results of the hook for them. However, if the preference is
 *    locked, the result of the hook will never be saved.
 *
 * $Horde: horde/config/hooks.php.dist,v 1.109 2007/01/03 06:26:08 slusarz Exp $
 */

// Default Kolab hooks:
if (!empty($GLOBALS['conf']['kolab']['enabled'])) {
    require_once 'Horde/Kolab.php';
    if (!function_exists('_username_hook_frombackend')) {
        function _username_hook_frombackend($userID)
        {
            // Connect to the LDAP server.
            $ds = ldap_connect(
                $GLOBALS['conf']['kolab']['ldap']['server'],
                $GLOBALS['conf']['kolab']['ldap']['port']
            );
            if (!$ds) {
                return $userID;
            }
            ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
            // Bind anonymously.
            $result = @ldap_bind($ds);
            if (!$result) {
                return $userID;
            }
            // Find the user's DN.
            $result = ldap_search(
                $ds,
                $GLOBALS['conf']['kolab']['ldap']['basedn'],
                'uid=' . $userID
            );
            $entry = ldap_first_entry($ds, $result);
            if ($entry === false) {
                // The user already authenticated with his email address.
                return $userID;
            }
            $email = ldap_get_values($ds, $entry, 'mail');
            return $email[0];
        }
        function _imp_hook_mbox_redirect($mailbox)
        {
            switch (Kolab::getMailboxType($mailbox)) {
            case 'event':
                return $GLOBALS['registry']->get('webroot', 'kronolith');

            case 'task':
                return $GLOBALS['registry']->get('webroot', 'nag');

            case 'note':
                return $GLOBALS['registry']->get('webroot', 'mnemo');

            case 'contact':
                return $GLOBALS['registry']->get('webroot', 'turba');

            default:
                return '';
            }
        }
        function _imp_hook_mbox_icons()
        {
            static $icons;

            if (!empty($icons)) {
                return $icons;
            }

            $folders = Kolab::listFolders();

            $icons = array();

            foreach ($folders as $folder) {
                $name = preg_replace('/^{[^}]+}/', '', $folder[0]);

                switch ($folder[1]) {
                case 'event':
		    $icons[$name] = array(
                        'icon' => 'kronolith.png',
                        'icondir' => $GLOBALS['registry']->getImageDir('kronolith'),
                        'alt' => _("Calendar")
                    );
                    break;

                case 'task':
		    $icons[$name] = array(
                        'icon' => 'nag.png',
                        'icondir' => $GLOBALS['registry']->getImageDir('nag'),
                        'alt' => _("Tasks")
                    );
                    break;

                case 'note':
                    $icons[$name] = array(
                        'icon' => 'mnemo.png',
                        'icondir' => $GLOBALS['registry']->getImageDir('mnemo'),
                        'alt' => _("Notes")
                    );
                    break;

                case 'contact':
                    $icons[$name] = array(
                        'icon' => 'turba.png',
                        'icondir' => $GLOBALS['registry']->getImageDir('turba'),
                        'alt' => _("Contacts")
                    );
                    break;
                }
            }
            return $icons;
        }
    }

    if (!function_exists('_horde_hook_preauthenticate')) {
        function _horde_hook_preauthenticate($userID, $credential, $realm)
        {
            /**
             * In multidomain mode we authenticate against the home server
             * of the user, so first we have to query the global ldap server
             * for the users 'kolabHomeserver' entry and then we modify the
             * PHP session variables to use the home server.
             */

            $ds = ldap_connect(
               $GLOBALS['conf']['kolab']['ldap']['server'],
               $GLOBALS['conf']['kolab']['ldap']['port']
            );
            if (!$ds) {
                return false;
            }

            // Bind anonymously.
            $result = @ldap_bind($ds,
               $GLOBALS['conf']['kolab']['ldap']['binddn'],
               $GLOBALS['conf']['kolab']['ldap']['bindpw']
            );
            if (!$result) {
                return false;
            }

            // Find the user's DN.
            $result = ldap_search(
               $ds,
               $GLOBALS['conf']['kolab']['ldap']['basedn'],
               "uid=$userID"
            );

            $entry = ldap_first_entry($ds, $result);
            if ($entry === false) {
                return false;
            }

            $result = ldap_get_values($ds, $entry, 'kolabHomeserver');
            if (!$result) {
                // 'manager' has no 'kolabHomserver' set, so don't treat it as error
                $homeserver = $GLOBALS['conf']['kolab']['imap']['server'];
            } else {
                // We found a valid homeserver
                $homeserver = $result[0];
            }

            $_SESSION['kolabHomeserver'] = $homeserver;
            return true;
       }
   }
}