~ubuntu-branches/debian/lenny/ingo1/lenny

« back to all changes in this revision

Viewing changes to scripts/convert_imp_filters.php

  • Committer: Bazaar Package Importer
  • Author(s): Gregory Colpart (evolix)
  • Date: 2008-07-20 03:31:29 UTC
  • mfrom: (1.4.2 upstream) (3.1.2 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080720033129-ocgs19lwfa516qix
Tags: 1.2-1
* New upstream release.
* The main changes compared to the previous version: WCAG 1.0 Priority
  2/Section 508 accessibility guidelines compliance; support for timed
  vacation messages (I will be away from X to Y); Procmail improvements
  including negative matches, body tests, vacation mail loop prevention, and
  full charset support; Maildrop improvements including vacation support and
  full character set support; Sieve improvements including a sivtest driver,
  numeric spam score tests, and quota support; the ability to store filter
  rules in a SQL database; Administrators can edit other user's filter
  rules; and many more bug fixes and improvements.
* This new release fix bugs with blacklists and whitelists. (Closes:
  #470878)
* No more need to copy CREDITS file in debian/rules. 
* Update to standards version 3.8.0, no further required changes.
* Link prototype.js file with libjs-scriptaculous package.
* Improvements in debian/copyright file. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
 * Converts a user's preferences from the "old" IMP 3.x or IMP HEAD
4
 
 * (pre 4.x) filter data structure to the "new" Ingo 2.x structure.
5
 
 *
6
 
 * This script is untested so use at your own risk.
7
 
 *
8
 
 * Usage: php convert_filters.php < filename
9
 
 * Filename is a file that contains a list of users, one username per line.
10
 
 * The username should be the same as how the preferences are stored in
11
 
 * the preferences backend (e.g. usernames may have to be in the form
12
 
 * user@example.com).
13
 
 *
14
 
 * This script is written to convert SQL databases ONLY.
15
 
 * There is no guarantee it will work on other preference backends (e.g. LDAP).
16
 
 *
17
 
 * $Horde: ingo/scripts/convert_imp_filters.php,v 1.18.10.7 2006/01/31 20:00:36 jan Exp $
18
 
 *
19
 
 * Copyright 2003-2006 Michael Slusarz <slusarz@bigworm.colorado.edu>
20
 
 *
21
 
 * See the enclosed file LICENSE for license information (ASL).  If you
22
 
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
23
 
 */
24
 
 
25
 
@define('AUTH_HANDLER', true);
26
 
@define('HORDE_BASE', dirname(__FILE__) . '/../..');
27
 
@define('INGO_BASE', dirname(__FILE__) . '/..');
28
 
 
29
 
// Do CLI checks and environment setup first.
30
 
require_once HORDE_BASE . '/lib/core.php';
31
 
require_once 'Horde/CLI.php';
32
 
 
33
 
// Make sure no one runs this from the web.
34
 
if (!Horde_CLI::runningFromCLI()) {
35
 
    exit("Must be run from the command line\n");
36
 
}
37
 
 
38
 
// Load the CLI environment - make sure there's no time limit, init
39
 
// some variables, etc.
40
 
Horde_CLI::init();
41
 
 
42
 
/* Initialize the needed libraries. */
43
 
require_once INGO_BASE . '/lib/base.php';
44
 
 
45
 
/* Update each user. */
46
 
while (!feof(STDIN)) {
47
 
    $user = fgets(STDIN);
48
 
    $count = 0;
49
 
    $user = trim($user);
50
 
 
51
 
    echo "Converting filters for user: $user\n";
52
 
 
53
 
    $userprefs = &Prefs::singleton($conf['prefs']['driver'],
54
 
                                   'imp', $user, '', null, false);
55
 
    $userprefs->retrieve();
56
 
    $oldfilters = @unserialize($userprefs->getValue('filters'));
57
 
 
58
 
    if (!is_array($oldfilters)) {
59
 
        echo "    Nothing to convert\n";
60
 
        continue;
61
 
    }
62
 
 
63
 
    /* Load the user's preferences. */
64
 
    $prefs = &Prefs::factory($conf['prefs']['driver'], 'ingo', $user, null, null, false);
65
 
    $prefs->retrieve();
66
 
 
67
 
    /* Merge with existing ingo filters. */
68
 
    $filters = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_FILTERS, false);
69
 
 
70
 
    /* Make sure special rules exist */
71
 
    $rule_count = 0;
72
 
    $located = array();
73
 
    $filterlist = $filters->getFilterList();
74
 
    if (!empty($filterlist)) {
75
 
        foreach ($filters->getFilterList() as $rule) {
76
 
            $located[$rule['action']] = true;
77
 
        }
78
 
    }
79
 
 
80
 
    if (!isset($located[INGO_STORAGE_ACTION_BLACKLIST])) {
81
 
        $filters->addRule(array('name' => "Blacklist", 'action' => INGO_STORAGE_ACTION_BLACKLIST), false);
82
 
        echo "    Added Blacklist Rule\n";
83
 
        $rule_count++;
84
 
    }
85
 
 
86
 
    if (!isset($located[INGO_STORAGE_ACTION_WHITELIST])) {
87
 
        $filters->addRule(array('name' => "Whitelist", 'action' => INGO_STORAGE_ACTION_WHITELIST), false);
88
 
        echo "    Added Whitelist Rule\n";
89
 
        $rule_count++;
90
 
    }
91
 
 
92
 
    if (!isset($located[INGO_STORAGE_ACTION_VACATION])) {
93
 
        $filters->addRule(array('name' => "Vacation", 'action' => INGO_STORAGE_ACTION_VACATION), false);
94
 
        echo "    Added Vacation Rule\n";
95
 
        $rule_count++;
96
 
    }
97
 
 
98
 
    if (!isset($located[INGO_STORAGE_ACTION_FORWARD])) {
99
 
        $filters->addRule(array('name' => "Forward", 'action' => INGO_STORAGE_ACTION_FORWARD), false);
100
 
        echo "    Added Forward Rule\n";
101
 
        $rule_count++;
102
 
    }
103
 
 
104
 
    if ($rule_count) {
105
 
        echo "    Importing " . $rule_count . " existing ingo filters\n";
106
 
    }
107
 
 
108
 
    /* IMP HEAD filter style
109
 
     * Array
110
 
     * (
111
 
     *   [bl] => Array
112
 
     *   (
113
 
     *     [BLACKLISTED ADDRESSES]
114
 
     *   ),
115
 
     *
116
 
     *   [rule] => Array
117
 
     *   (
118
 
     *     [*Filter number*] => Array
119
 
     *     (
120
 
     *       [flt] => Array
121
 
     *       (
122
 
     *         [] => Array
123
 
     *           (
124
 
     *             [fld] => Array(*Field name(s)*)
125
 
     *             [txt] => Array(*Text to match*)
126
 
     *           )
127
 
     *       )
128
 
     *       [act] => *Action code*
129
 
     *       [fol] => *Folder name to move to*
130
 
     *     )
131
 
     *   )
132
 
     * )
133
 
     */
134
 
    if (isset($oldfilters['bl'])) {
135
 
        if (!empty($oldfilters['bl'])) {
136
 
            $ob = &new Ingo_Storage_blacklist();
137
 
            $ob->setBlacklist($oldfilters['bl']);
138
 
            $ingo_storage->store($ob);
139
 
            echo "    Converted Blacklist\n";
140
 
        }
141
 
 
142
 
        if (!empty($oldfilters['rule'])) {
143
 
            foreach ($oldfilters['rule'] as $val) {
144
 
                $curr = array(
145
 
                    'action' => 0,
146
 
                    'name' => 'Converted IMP Filter',
147
 
                    'combine' => INGO_STORAGE_COMBINE_ALL,
148
 
                    'stop' => false,
149
 
                    'flags' => 0,
150
 
                    'conditions' => array(),
151
 
                    'action-value' => null
152
 
                );
153
 
 
154
 
                /* IMP_FILTER_DELETE = 1, IMP_FILTER_MOVE = 2,
155
 
                   IMP_FILTER_NUKE = 3 */
156
 
                if ($val['act'] == 1) {
157
 
                    $curr['action'] = INGO_STORAGE_ACTION_MOVE;
158
 
                    $curr['combine'] .= ' - DELETE';
159
 
                } elseif ($val['act'] == 2) {
160
 
                    $curr['action'] = INGO_STORAGE_ACTION_MOVE;
161
 
                    $curr['combine'] .= ' - MOVE';
162
 
                } elseif ($val['act'] == 3) {
163
 
                    $curr['action'] = INGO_STORAGE_ACTION_DISCARD;
164
 
                    $curr['combine'] .= ' - NUKE';
165
 
                }
166
 
 
167
 
                foreach ($val['flt'] as $val2) {
168
 
                    foreach ($val2['fld'] as $key => $field) {
169
 
                        $curr['conditions'][] = Array(
170
 
                            'field' => ucfirst($field),
171
 
                            'match' => 'contains',
172
 
                            'value' => $val2['txt'][$key],
173
 
                            'case' => false,
174
 
                            'type' => INGO_STORAGE_TYPE_HEADER
175
 
                        );
176
 
                    }
177
 
                }
178
 
 
179
 
                if (isset($val['fol'])) {
180
 
                    $curr['action-value'] = $val['fol'];
181
 
                }
182
 
 
183
 
                $count++;
184
 
                $filters->addRule($curr);
185
 
            }
186
 
        }
187
 
 
188
 
        echo "    Converted $count Filters\n";
189
 
        $ingo_storage->store($filters);
190
 
    }
191
 
 
192
 
    /* IMP 3.x filter style
193
 
     * Array
194
 
     * (
195
 
     *   [#] => Array
196
 
     *   (
197
 
     *     [action] => 'move' -or- 'delete'
198
 
     *     [folder] => folder name (may not exist)
199
 
     *     [fields] => Array
200
 
     *     (
201
 
     *     )
202
 
     *     [text] => Array
203
 
     *     (
204
 
     *     )
205
 
     *   )
206
 
     * )
207
 
     */
208
 
    else {
209
 
        $bl_array = array();
210
 
 
211
 
        if (!empty($oldfilters)) {
212
 
            foreach ($oldfilters as $rule) {
213
 
                if (($rule['action'] == 'delete') &&
214
 
                    (count($rule['fields']) == 1) &&
215
 
                    ($rule['fields'][0] == 'from')) {
216
 
                    if (!in_array($rule['text'], $bl_array)) {
217
 
                        $bl_array[] = $rule['text'];
218
 
                    }
219
 
                } else {
220
 
                    $curr = array(
221
 
                        'action' => 0,
222
 
                        'name' => 'Converted IMP Filter',
223
 
                        'combine' => INGO_STORAGE_COMBINE_ANY,
224
 
                        'stop' => false,
225
 
                        'flags' => 0,
226
 
                        'conditions' => array(),
227
 
                        'action-value' => null
228
 
                    );
229
 
 
230
 
                    if ($rule['action'] == 'move') {
231
 
                        $curr['action'] = INGO_STORAGE_ACTION_MOVE;
232
 
                        $curr['name'] .= ' - MOVE';
233
 
                    } elseif ($rule['action'] == 'delete') {
234
 
                        $curr['action'] = INGO_STORAGE_ACTION_DISCARD;
235
 
                        $curr['name'] .= ' - DISCARD';
236
 
                    }
237
 
 
238
 
                    if (isset($rule['folder'])) {
239
 
                        $curr['action-value'] = $rule['folder'];
240
 
                    }
241
 
 
242
 
                    foreach ($rule['fields'] as $key => $val) {
243
 
                        $curr['conditions'][] = Array(
244
 
                            'field' => ucfirst($val),
245
 
                            'match' => 'contains',
246
 
                            'value' => $rule['text'],
247
 
                            'case' => false,
248
 
                            'type' => INGO_STORAGE_TYPE_HEADER
249
 
                        );
250
 
                    }
251
 
 
252
 
                    $count++;
253
 
                    $filters->addRule($curr);
254
 
                }
255
 
            }
256
 
        }
257
 
 
258
 
        $ob = &new Ingo_Storage_blacklist();
259
 
        $ob->setBlacklist($bl_array);
260
 
        $ingo_storage->store($ob);
261
 
        echo "    Converted " . count($bl_array) . " Blacklist Entries\n";
262
 
 
263
 
        $ingo_storage->store($filters);
264
 
        echo "    Converted $count Filters\n";
265
 
 
266
 
        $prefs->store();
267
 
    }
268
 
}