[go: up one dir, main page]

File: edit.php

package info (click to toggle)
avelsieve 1.9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,560 kB
  • ctags: 1,733
  • sloc: php: 7,397; sh: 100; makefile: 22
file content (333 lines) | stat: -rw-r--r-- 11,215 bytes parent folder | download | duplicates (2)
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
<?php
/**
 * User-friendly interface to SIEVE server-side mail filtering.
 * Plugin for Squirrelmail 1.4+
 *
 * This page is the main interface to editing and adding new rules.
 *
 * Licensed under the GNU GPL. For full terms see the file COPYING that came
 * with the Squirrelmail distribution.
 *
 * @version $Id: edit.php 1028 2009-05-22 10:01:15Z avel $
 * @author Alexandros Vellis <avel@users.sourceforge.net>
 * @copyright 2002-2009 Alexandros Vellis
 * @package plugins
 * @subpackage avelsieve
 */

/** Includes */
if (file_exists('../../include/init.php')) {
    include_once('../../include/init.php');
} else if (file_exists('../../include/validate.php')) {
    define('SM_PATH','../../');
    include_once(SM_PATH . 'include/validate.php');
    include_once(SM_PATH . 'include/load_prefs.php');
    include_once(SM_PATH . 'functions/page_header.php');
    include_once(SM_PATH . 'functions/date.php');
}
    
include_once(SM_PATH . 'functions/imap.php');
require(SM_PATH . 'plugins/avelsieve/config/config.php');

$prev = bindtextdomain ('avelsieve', SM_PATH . 'plugins/avelsieve/locale');
textdomain ('avelsieve');

include_once(SM_PATH . 'functions/identity.php');
require_once(SM_PATH . 'plugins/avelsieve/include/constants.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/html_rulestable.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/html_ruleedit.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/sieve_conditions.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/sieve_actions.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/sieve.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/support.inc.php');
include_once(SM_PATH . 'plugins/avelsieve/include/styles.inc.php');

sqsession_is_active();

$errmsg = array();

/* Session / Server vars */
sqgetGlobalVar('sieve_capabilities', $sieve_capabilities, SQ_SESSION);
sqgetGlobalVar('key', $key, SQ_COOKIE);
sqgetGlobalVar('rules', $rules, SQ_SESSION);
/* Mode of operation */
sqgetGlobalVar('dup', $dup, SQ_GET & SQ_POST);
sqgetGlobalVar('addnew', $addnew, SQ_GET);
sqgetGlobalVar('position', $position, SQ_GET & SQ_POST);
/* New folder Creation */
sqgetGlobalVar('newfoldername', $newfoldername, SQ_POST);
if(isset($newfoldername)) $newfoldername = trim($newfoldername);
sqgetGlobalVar('newfolderparent', $newfolderparent, SQ_POST);

// intermediate_action is an action during an editing / creation of a rule,
// that does _not_ imply submitting the new rule data.
sqgetGlobalVar('intermediate_action', $intermediate_action, SQ_POST);

/* Essentials */
sqgetGlobalVar('popup', $popup, SQ_GET);
sqgetGlobalVar('edit', $edit, SQ_FORM);

sqgetGlobalVar('previoustype', $previoustype, SQ_POST);
sqgetGlobalVar('cond', $new_cond, SQ_POST);
sqgetGlobalVar('previous_cond', $previous_cond, SQ_POST);

sqgetGlobalVar('serialized_rule', $serialized_rule, SQ_GET);

sqgetGlobalVar('type', $type_get, SQ_GET);

$referrerUrl = $referrerArgs = '';
sqgetGlobalVar('referrerUrl', $referrerUrl, SQ_FORM);
sqgetGlobalVar('referrerArgs', $referrerArgs, SQ_FORM);
if(!empty($referrerArgs)) {
    $referrerArgs = unserialize($referrerArgs);
}

$base_uri = sqm_baseuri();

isset($popup) ? $popup = '?popup=1' : $popup = '';

$backend_class_name = 'DO_Sieve_'.$avelsieve_backend;
$s = new $backend_class_name;
$s->init();

/* If this page is called before table.php is ever shown, then we have to make
 * the current filtering rules available in the Session. This will happen when
 * a user clicks either:
 * i) creation of a rule from the message commands (while viewing a message)
 * ii) creation of a rule from some search criteria.
 */
if (!isset($rules)) {
    $s->login();
    /* Actually get the script 'phpscript' (hardcoded ATM). */
    if($s->load('phpscript', $rules, $scriptinfo)) {
        $_SESSION['rules'] = $rules;
        $_SESSION['scriptinfo'] = $scriptinfo;
    }
    $s->logout();
}

/* Create new mailbox, if required by the user. */
if(!empty($newfoldername)) {
    global $created_mailbox_name;
    $created_mailbox_name = '';
    avelsieve_create_folder($newfoldername, $newfolderparent, $created_mailbox_name, $errmsg);
}

/* Mode of operation */
if(isset($dup)) {
    $mode = 'duplicate';
} elseif(isset($addnew)) {
    $mode = 'addnew';
} else {
    $mode = 'edit';
}

if(is_numeric($type_get) && $type_get > 1 &&
    file_exists(SM_PATH . 'plugins/avelsieve/include/html_ruleedit.'.$type_get.'.inc.php')) {
    /**
     * Include the appropriate class for rule editing.
     */
    include_once(SM_PATH . 'plugins/avelsieve/include/html_ruleedit.'.$type_get.'.inc.php');
    $edit_class_name = 'avelsieve_html_edit_'. $type_get;

    /**
     * Some rules are unique only. Example: There is only one junk mail rule (#11) and
     * only one whitelist rule (#12). Here, we replace the 'addnew' mode with 'edit',
     * if the rule already exists.
     */
    if($avelsieve_maintypes[$type_get]['unique']) {
        foreach($rules as $no=>$rule) {
            if($rule['type'] == $type_get) {
                $mode = 'edit';
                $edit = $no;
            }
        }
    }

} else {
    $edit_class_name = 'avelsieve_html_edit';
}

$additional_options = array();
if(isset($position) && isset($rules[$position])) {
    $additional_options['position'] = $position;
}

$ruleobj = new $edit_class_name($s, $mode, $popup);
$ruleobj->set_errmsg($errmsg);

if(isset($edit)) {
    /* Editing an existing rule */
    $ruleobj->set_rule_type( (isset($rules[$edit]['type']) ? $rules[$edit]['type'] : 1));
    $ruleobj->set_rule_data($rules[$edit]);

} elseif(isset($serialized_rule)) {
    /* Adding a new rule through $_GET, e.g. from search integration feature. */
    $ruleobj->set_rule_type($type_get);
    $ruleobj->set_rule_data(unserialize(urldecode($serialized_rule)));

} elseif(!isset($edit) && isset($type_get) && empty($_POST)) {
    /* Adding a new rule through $_GET */
    $ruleobj->set_rule_type($type_get);
    if(isset($avelsieve_rules_settings[$type_get]['default_rule'])) {
        $ruleobj->set_rule_data($avelsieve_rules_settings[$type_get]['default_rule']);
    }
    $ruleobj->process_input($_GET, true);

} elseif(!isset($edit) && isset($type_get) && !empty($_POST)) {
    /* Continuing to add a new rule */
    $ruleobj->set_rule_type($type_get);
} else {
    /* Adding a new rule from scratch */
    $ruleobj->set_rule_type($type_get);
}
$type = $ruleobj->type;

if(!isset($type) || (isset($type) && !is_numeric($type)) ) $type = 1;

/* TODO - use a snippet like this to change type from the edit UI */
/*
if(isset($previoustype) && (
    $previoustype == 0 ||
    (isset($type) && $previoustype != $type)
  )) {
        $changetype = true;
} else {
        $changetype = false;
}
*/

/* This is for determining if the test of a specific condition has changed */
$changetype = false;

if(isset($previous_cond) && isset($new_cond)) {
    foreach($previous_cond as $n=>$t) {
        if(isset($new_cond[$n]['type']) && $t['type'] != $new_cond[$n]['type']) {
            $changetype = true;
        }
    }
}

/* Calculate referrer URL, if it exists. */

if(!empty($referrerUrl)) {
    $ruleobj->set_referrer($referrerUrl, $referrerArgs);
    $redirectUrl = $referrerUrl;
    if(!empty($referrerArgs)) {
        $xtra = array();
        foreach($referrerArgs as $k=>$v) {
            $xtra[] = $k.'='.$v;
        }
        if(strstr($redirectUrl, '?')) {
            $redirectUrl .= '&' . implode('&', $xtra);
        } else {
            $redirectUrl .= '?' . implode('&', $xtra);
        }
    }
} else {
    $redirectUrl = "table.php$popup";
}

/* Available Actions that occur if submitting the form in a number of ways */

if(isset($_POST['cancel'])) {
    /* Cancel Editing */
    header("Location: $redirectUrl");
    exit;

} elseif(isset($_POST['apply']) && !$changetype) {
    /* Apply change in existing rule */
    //print "/* Apply change in existing rule */";
    $ruleobj->process_input($_POST, true);
    if(empty($ruleobj->errmsg)) {
        $_SESSION['rules'][$edit] = $ruleobj->rule;
        if($rawSuccessMsg = $ruleobj->getSuccessMessage()) {
            $_SESSION['comm']['raw'] = $rawSuccessMsg;
        } else {
            $_SESSION['comm']['edited'] = $edit;
        }
        $_SESSION['haschanged'] = true;
        header("Location: $redirectUrl");
        exit;
    }

} elseif(isset($_POST['addnew']) && !$changetype) {
    /* Add new rule */
    $ruleobj->process_input($_POST, true);

    if(empty($ruleobj->errmsg)) {
        if(isset($dup)) {
            // insert moving rule in place
            array_splice($_SESSION['rules'], $edit+1, 0, array($ruleobj->rule));
            // Reindex
            $_SESSION['rules'] = array_values($_SESSION['rules']);
        } elseif(isset($position)) {
            array_splice($_SESSION['rules'], $position+1, 0, array($ruleobj->rule));
            $_SESSION['rules'] = array_values($_SESSION['rules']);
        } else {
            // Append the new rule at the end of rules table
            $_SESSION['rules'][] = $ruleobj->rule;
        }
        /* Communication: */
        if($rawSuccessMsg = $ruleobj->getSuccessMessage()) {
            $_SESSION['comm']['raw'] = $rawSuccessMsg;
        } else {
            $_SESSION['comm']['edited'] = $edit;
            $_SESSION['comm']['new'] = true;
        }
        $_SESSION['haschanged'] = true;
        header("Location: $redirectUrl");
        exit;
    }
} elseif($changetype || isset($_POST['append']) || isset($_POST['less']) || isset($intermediate_action)) {
    /* still in editing; apply any changes. */
    $ruleobj->process_input($_POST, false);
}

/* Grab the list of my IMAP folders. This is only needed for the GUI, and is
 * done as the last step. */
sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
if(!isset($delimiter)) {
    $delimiter = sqimap_get_delimiter($imapConnection);
}
// $folder_prefix = "INBOX";
$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); 
if($SQM_INTERNAL_VERSION[0] == 1 && $SQM_INTERNAL_VERSION[1] == 5) {
    /* In Squirrelmail 1.5.x, use sqimap_mailbox_list() with
     * $show_only_subscribed_folders flag off. Thanks to Simon Matter */
    global $show_only_subscribed_folders;
    $old_show_only_subscribed_folders = $show_only_subscribed_folders;
    $show_only_subscribed_folders = false;
    $boxes = sqimap_mailbox_list($imapConnection,true);
    /* Restore correct folder cache */
    $show_only_subscribed_folders = $old_show_only_subscribed_folders;
    $dummy = sqimap_mailbox_list($imapConnection,true);

} else {
    /* In Squirrelmail 1.4.x, use sqimap_mailbox_list_all() */
    $boxes = sqimap_mailbox_list_all($imapConnection);
}
sqimap_logout($imapConnection); 


/* -------------- Presentation Logic ------------- */

$prev = bindtextdomain ('squirrelmail', SM_PATH . 'locale');
textdomain ('squirrelmail');
if($popup) {
    displayHtmlHeader('');
} else {
    displayPageHeader($color, 'None');
}

$prev = bindtextdomain ('avelsieve', SM_PATH . 'plugins/avelsieve/locale');
textdomain ('avelsieve');

if(isset($edit)) {
    echo $ruleobj->edit_rule($edit);
} else {
    echo $ruleobj->edit_rule();
}
    
?>
</body></html>