File: api.php

package info (click to toggle)
ingo1 1.2.4%2Bdebian0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,196 kB
  • ctags: 1,955
  • sloc: php: 8,375; xml: 2,846; sql: 168; makefile: 74; sh: 26
file content (363 lines) | stat: -rw-r--r-- 10,563 bytes parent folder | download
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
/**
 * Ingo external API interface.
 *
 * This file defines Ingo's external API interface. Other applications
 * can interact with Ingo through this API.
 *
 * $Horde: ingo/lib/api.php,v 1.16.12.9 2009/12/30 17:40:03 jan Exp $
 *
 * See the enclosed file LICENSE for license information (ASL).  If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 */

$_services['perms'] = array(
    'args' => array(),
    'type' => '{urn:horde}stringArray');

$_services['removeUserData'] = array(
    'args' => array('user' => 'string'),
    'type' => 'boolean'
);

$_services['blacklistFrom'] = array(
    'args' => array('addresses' => '{urn:horde}stringArray'),
    'type' => 'boolean',
);

$_services['showBlacklist'] = array(
    'link' => '%application%/blacklist.php',
);

$_services['whitelistFrom'] = array(
    'args' => array('addresses' => '{urn:horde}stringArray'),
    'type' => 'boolean',
);

$_services['showWhitelist'] = array(
    'link' => '%application%/whitelist.php',
);

$_services['canApplyFilters'] = array(
    'args' => array(),
    'type' => 'boolean',
);

$_services['applyFilters'] = array(
    'args' => array('params' => '{urn:horde}stringArray'),
    'type' => 'boolean',
);

$_services['showFilters'] = array(
    'link' => '%application%/filters.php',
);

$_services['showVacation'] = array(
    'link' => '%application%/vacation.php',
);

$_services['setVacation'] = array(
    'args' => array('info' => '{urn:horde}stringArray'),
    'type' => 'boolean',
);

$_services['disableVacation'] = array(
    'args' => array(),
    'type' => 'boolean',
);

/**
 * Returns a list of available permissions.
 *
 * @return array  An array describing all available permissions.
 */
function _ingo_perms()
{
    $perms = array();
    $perms['tree']['ingo']['allow_rules'] = false;
    $perms['title']['ingo:allow_rules'] = _("Allow Rules");
    $perms['type']['ingo:allow_rules'] = 'boolean';
    $perms['tree']['ingo']['max_rules'] = false;
    $perms['title']['ingo:max_rules'] = _("Maximum Number of Rules");
    $perms['type']['ingo:max_rules'] = 'int';

    return $perms;
}

/**
 * Removes user data.
 *
 * @param string $user  Name of user to remove data for.
 *
 * @return mixed  true on success | PEAR_Error on failure
 */
function _ingo_removeUserData($user)
{
    if (!Auth::isAdmin() && $user != Auth::getAuth()) {
        return PEAR::raiseError(_("You are not allowed to remove user data."));
    }

    require_once dirname(__FILE__) . '/../lib/base.php';

    /* Remove all filters/rules owned by the user. */
    $result = $GLOBALS['ingo_storage']->removeUserData($user);
    if (is_a($result, 'PEAR_Error')) {
        Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
        return $result;
    }

    /* Now remove all shares owned by the user. */
    if (!empty($GLOBALS['ingo_shares'])) {
        /* Get the user's default share */
        $share = $GLOBALS['ingo_shares']->getShare($user);
        if (is_a($share, 'PEAR_Error')) {
            Horde::logMessage($share, __FILE__, __LINE__, PEAR_LOG_ERR);
            return $share;
        } else {
            $result = $GLOBALS['ingo_shares']->removeShare($share);
            if (is_a($result, 'PEAR_Error')) {
                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
                return $result;
            }
        }
        
        /* Get a list of all shares this user has perms to and remove the
         * perms */
        $shares = $GLOBALS['ingo_shares']->listShares($user);
        if (is_a($shares, 'PEAR_Error')) {
            Horde::logMessage($shares, __FILE__, __LINE__, PEAR_LOG_ERR);
        }
        foreach ($shares as $share) {
            $share->removeUser($user);
        }

        /* Get a list of all shares this user owns and has perms to delete and
         * remove them */
        $shares = $GLOBALS['ingo_shares']->listShares($user, PERMS_DELETE, $user);
        if (is_a($shares, 'PEAR_Error')) {
            Horde::logMessage($shares, __FILE__, __LINE__, PEAR_LOG_ERR);
            return $shares;
        }
        foreach ($shares as $share) {
            $GLOBALS['ingo_shares']->removeShare($share);
        }
    }

    return true;
}

/**
 * Add addresses to the blacklist
 *
 * @param string $addresses    The addresses to add
 */
function _ingo_blacklistFrom($addresses)
{
    require_once dirname(__FILE__) . '/../lib/base.php';
    if (!empty($GLOBALS['ingo_shares'])) {
        $_SESSION['ingo']['current_share'] = $signature;
    }

    global $ingo_storage;

    /* Check for '@' entries in $addresses - this would call all mail to
     * be blacklisted which is most likely not what is desired. */
    $addresses = array_unique($addresses);
    $key = array_search('@', $addresses);
    if ($key !== false) {
        unset($addresses[$key]);
    }

    if (!empty($addresses)) {
        $blacklist = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_BLACKLIST);
        $ret = $blacklist->setBlacklist(array_merge($blacklist->getBlacklist(), $addresses));
        if (is_a($ret, 'PEAR_Error')) {
            $GLOBALS['notification']->push($ret, $ret->getCode());
        } else {
            $ingo_storage->store($blacklist);
            Ingo::updateScript();
            foreach ($addresses as $from) {
                $GLOBALS['notification']->push(sprintf(_("The address \"%s\" has been added to your blacklist."), $from));
            }
        }
    }
}

/**
 * Add addresses to the white list
 *
 * @param string $addresses    The addresses to add
 */
function _ingo_whitelistFrom($addresses)
{
    require_once dirname(__FILE__) . '/../lib/base.php';
    if (!empty($GLOBALS['ingo_shares'])) {
        $_SESSION['ingo']['current_share'] = $signature;
    }

    global $ingo_storage;

    $whitelist = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_WHITELIST);
    $ret = $whitelist->setWhitelist(array_merge($whitelist->getWhitelist(), $addresses));
    if (is_a($ret, 'PEAR_Error')) {
        $GLOBALS['notification']->push($ret, $ret->getCode());
    } else {
        $ingo_storage->store($whitelist);
        Ingo::updateScript();
        foreach ($addresses as $from) {
            $GLOBALS['notification']->push(sprintf(_("The address \"%s\" has been added to your whitelist."), $from));
        }
    }
}

/**
 * Can this driver perform on-demand filtering?
 *
 * @return boolean  True if perform() is available, false if not.
 */
function _ingo_canApplyFilters()
{
    require_once dirname(__FILE__) . '/../lib/base.php';

    $ingo_script = Ingo::loadIngoScript();
    if ($ingo_script) {
        return $ingo_script->performAvailable();
    } else {
        return false;
    }
}

/**
 * Perform the filtering specified in the rules.
 *
 * @param array $params  The parameter array.
 *
 * @return boolean  True if filtering was performed, false if not.
 */
function _ingo_applyFilters($params = array())
{
    require_once dirname(__FILE__) . '/../lib/base.php';
    if (!empty($GLOBALS['ingo_shares'])) {
        $_SESSION['ingo']['current_share'] = $signature;
    }

    $ingo_script = Ingo::loadIngoScript();
    if ($ingo_script) {
        if (!isset($params['filter_seen'])) {
            $params['filter_seen'] = $GLOBALS['prefs']->getValue('filter_seen');
        }
        if (!isset($params['show_filter_msg'])) {
            $params['show_filter_msg'] = $GLOBALS['prefs']->getValue('show_filter_msg');
        }
        return $ingo_script->perform($params);
    }
}

/**
 * Set vacation
 *
 * @param array $info  Vacation details
 *
 * @return boolean  True on success.
 */
function _ingo_setVacation($info)
{
    require_once dirname(__FILE__) . '/../lib/base.php';
    if (!empty($GLOBALS['ingo_shares'])) {
        $_SESSION['ingo']['current_share'] = $signature;
    }

    global $ingo_storage;

    /* Get vacation filter. */
    $filters = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_FILTERS);
    $vacation_rule_id = $filters->findRuleId(INGO_STORAGE_ACTION_VACATION);

    if (empty($info)) {
        return true;
    }

    /* Set vacation object and rules. */
    $vacation = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_VACATION);

    /* Make sure we have at least one address. */
    if (empty($info['addresses'])) {
        require_once 'Horde/Identity.php';
        $identity = &Identity::singleton('none');
        $info['addresses'] = implode("\n", $identity->getAll('from_addr'));
        /* Remove empty lines. */
        $info['addresses'] = preg_replace('/\n+/', "\n", $info['addresses']);
        if (empty($addresses)) {
            $info['addresses'] = Auth::getAuth();
        }
    }

    $vacation->setVacationAddresses($addresses);

    if (isset($info['days'])) {
        $vacation->setVacationDays($info['days']);
    }
    if (isset($info['excludes'])) {
        $vacation->setVacationExcludes($info['excludes']);
    }
    if (isset($info['ignorelist'])) {
        $vacation->setVacationIgnorelist(($info['ignorelist'] == 'on'));
    }
    if (isset($info['reason'])) {
        $vacation->setVacationReason($info['reason']);
    }
    if (isset($info['subject'])) {
        $vacation->setVacationSubject($info['subject']);
    }
    if (isset($info['start'])) {
        $vacation->setVacationStart($info['start']);
    }
    if (isset($info['end'])) {
        $vacation->setVacationEnd($info['end']);
    }

    $filters->ruleEnable($vacation_rule_id);
    $result = $ingo_storage->store($filters);
    if (!is_a($result, 'PEAR_Error')) {
        if ($GLOBALS['prefs']->getValue('auto_update')) {
            Ingo::updateScript();
        }

        /* Update the timestamp for the rules. */
        $_SESSION['ingo']['change'] = time();
    }

    return $result;
}

/**
 * Disable vacation
 *
 * @return boolean  True on success.
 */
function _ingo_disableVacation()
{
    require_once dirname(__FILE__) . '/../lib/base.php';
    if (!empty($GLOBALS['ingo_shares'])) {
        $_SESSION['ingo']['current_share'] = $signature;
    }

    global $ingo_storage;

    /* Get vacation filter. */
    $filters = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_FILTERS);
    $vacation_rule_id = $filters->findRuleId(INGO_STORAGE_ACTION_VACATION);

    $filters->ruleDisable($vacation_rule_id);
    $result = $ingo_storage->store($filters);
    if (!is_a($result, 'PEAR_Error')) {
        if ($GLOBALS['prefs']->getValue('auto_update')) {
            Ingo::updateScript();
        }

        /* Update the timestamp for the rules. */
        $_SESSION['ingo']['change'] = time();
    }

    return $result;
}