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 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
<?php
/**
* The Ingo_Script_imap:: class represents an IMAP client-side
* script generator.
*
* $Horde: ingo/lib/Script/imap.php,v 1.49.10.1 2005/01/03 12:25:38 jan Exp $
*
* Copyright 2003-2005 Michael Slusarz <slusarz@bigworm.curecanti.org>
*
* See the enclosed file LICENSE for license information. If you
* did not receive this file, see http://www.horde.org/licenses.
*
* @author Michael Slusarz <slusarz@bigworm.curecanti.org>
* @version $Revision: 1.49.10.1 $
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Script_imap extends Ingo_Script {
/**
* The list of actions allowed (implemented) for this driver.
*
* @var array $_actions
*/
var $_actions = array(
INGO_STORAGE_ACTION_KEEP,
INGO_STORAGE_ACTION_MOVE,
INGO_STORAGE_ACTION_DISCARD,
INGO_STORAGE_ACTION_MOVEKEEP
);
/**
* The categories of filtering allowed.
*
* @var array $_categories
*/
var $_categories = array(
INGO_STORAGE_ACTION_BLACKLIST,
INGO_STORAGE_ACTION_WHITELIST
);
/**
* The list of tests allowed (implemented) for this driver.
*
* @var array $_tests
*/
var $_tests = array(
'contains', 'not contain'
);
/**
* The types of tests allowed (implemented) for this driver.
*
* @var array $_types
*/
var $_types = array(
INGO_STORAGE_TYPE_HEADER,
INGO_STORAGE_TYPE_SIZE,
INGO_STORAGE_TYPE_BODY
);
/**
* Does the driver support setting IMAP flags?
*
* @var boolean $_supportIMAPFlags
*/
var $_supportIMAPFlags = true;
/**
* Does the driver support the stop-script option?
*
* @var boolean $_supportStopScript
*/
var $_supportStopScript = true;
/**
* This driver can perform on demand filtering (in fact, that is all
* it can do).
*
* @var boolean $_ondemand
*/
var $_ondemand = true;
/**
* Perform the filtering specified in the rules.
*
* @access public
*
* @param array $params The parameter array. It MUST contain:
* <pre>
* 'imap' -- An open IMAP stream.
* 'mailbox' -- The name of the mailbox to filter.
* </pre>
*
* @return boolean True if filtering performed, false if not.
*/
function perform($params)
{
global $ingo_storage, $notification, $prefs;
$whitelist_ids = array();
/* Get the IMAP_Cache object. */
require_once 'Horde/IMAP/Cache.php';
$imap_cache = &IMAP_Cache::singleton();
/* Only do filtering if:
1. We have not done filtering before -or-
2. The mailbox has changed -or-
3. The rules have changed. */
$cache = $imap_cache->getCache($params['imap'], $params['mailbox'], 'ingochange');
if (($cache !== false) && ($cache == $_SESSION['ingo']['change'])) {
return true;
}
/* Are we using a POP mailbox? */
$ob = @imap_check($params['imap']);
$pop3 = (stristr($ob->Driver, 'pop3') !== false);
require_once 'Horde/IMAP/Search.php';
require_once 'Horde/MIME.php';
$imap_search = &IMAP_Search::singleton(array('pop3' => $pop3));
/* Grab the rules list. */
$filters = $ingo_storage->retrieve(INGO_STORAGE_ACTION_FILTERS);
/* Should we filter only [un]seen messages? */
$seen_flag = $prefs->getValue('filter_seen');
/* Should we use detailed notification messages? */
$detailmsg = $prefs->getValue('show_filter_msg');
/* Parse through the rules, one-by-one. */
foreach ($filters->getFilterlist() as $rule) {
/* Check to make sure this is a valid rule and that the rule is
not disabled. */
if (!$this->_validRule($rule['action']) ||
!empty($rule['disable'])) {
continue;
}
$search_array = array();
$stop_flag = false;
switch ($rule['action']) {
case INGO_STORAGE_ACTION_BLACKLIST:
case INGO_STORAGE_ACTION_WHITELIST:
$bl_folder = null;
if ($rule['action'] == INGO_STORAGE_ACTION_BLACKLIST) {
$blacklist = $ingo_storage->retrieve(INGO_STORAGE_ACTION_BLACKLIST);
$addr = $blacklist->getBlacklist();
$bl_folder = $blacklist->getBlacklistFolder();
} else {
$whitelist = $ingo_storage->retrieve(INGO_STORAGE_ACTION_WHITELIST);
$addr = $whitelist->getWhitelist();
}
/* If list is empty, move on. */
if (empty($addr)) {
continue;
}
$query = &new IMAP_Search_Query();
foreach ($addr as $val) {
$ob = &new IMAP_Search_Query();
$ob->deleted(false);
if ($seen_flag == INGO_SCRIPT_FILTER_UNSEEN) {
$ob->seen(false);
} elseif ($seen_flag == INGO_SCRIPT_FILTER_SEEN) {
$ob->seen(true);
}
$ob->header('from', $val);
$search_array[] = &$ob;
}
$query->imapOr($search_array);
$indices = $imap_search->searchMailbox($query, $params['imap'], $params['mailbox']);
if ($rule['action'] == INGO_STORAGE_ACTION_BLACKLIST) {
$indices = array_diff($indices, $whitelist_ids);
if (!empty($indices)) {
$sequence = implode(',', $indices);
if (!empty($bl_folder)) {
@imap_mail_move($params['imap'], $sequence, $bl_folder, CP_UID);
} else {
@imap_delete($params['imap'], $sequence, FT_UID);
}
@imap_expunge($params['imap']);
$notification->push(sprintf(_("Filter activity: %s message(s) that matched the blacklist were deleted."), count($indices)), 'horde.message');
}
} else {
$whitelist_ids = $indices;
}
break;
case INGO_STORAGE_ACTION_KEEP:
case INGO_STORAGE_ACTION_MOVE:
case INGO_STORAGE_ACTION_DISCARD:
$query = &new IMAP_Search_Query();
foreach ($rule['conditions'] as $val) {
$ob = &new IMAP_Search_Query();
$ob->deleted(false);
if ($seen_flag == INGO_SCRIPT_FILTER_UNSEEN) {
$ob->seen(false);
} elseif ($seen_flag == INGO_SCRIPT_FILTER_SEEN) {
$ob->seen(true);
}
if (!empty($val['type']) &&
($val['type'] == INGO_STORAGE_TYPE_SIZE)) {
if ($val['match'] == 'greater than') {
$operator = '>';
} elseif ($val['match'] == 'less than') {
$operator = '<';
}
$ob->size($val['value'], $operator);
} elseif (!empty($val['type']) &&
($val['type'] == INGO_STORAGE_TYPE_BODY)) {
if ($val['match'] == 'contains') {
$ob->body($val['value'], false);
} elseif ($val['match'] == 'not contain') {
$ob->body($val['value'], true);
}
} else {
if ($val['match'] == 'contains') {
$ob->header($val['field'], $val['value'], false);
} elseif ($val['match'] == 'not contain') {
$ob->header($val['field'], $val['value'], true);
}
}
$search_array[] = &$ob;
}
if ($rule['combine'] == INGO_STORAGE_COMBINE_ALL) {
$query->imapAnd($search_array);
} else {
$query->imapOr($search_array);
}
$indices = $imap_search->searchMailbox($query, $params['imap'], $params['mailbox']);
if (($indices = array_diff($indices, $whitelist_ids))) {
$sequence = implode(',', $indices);
$stop_flag = true;
/* Set the flags. */
if (!empty($rule['flags']) &&
($rule['action'] != INGO_STORAGE_ACTION_DISCARD)) {
$flags = array();
if ($rule['flags'] & INGO_STORAGE_FLAG_ANSWERED) {
$flags[] = '\\Answered';
}
if ($rule['flags'] & INGO_STORAGE_FLAG_DELETED) {
$flags[] = '\\Deleted';
}
if ($rule['flags'] & INGO_STORAGE_FLAG_FLAGGED) {
$flags[] = '\\Flagged';
}
if ($rule['flags'] & INGO_STORAGE_FLAG_SEEN) {
$flags[] = '\\Seen';
}
@imap_setflag_full($params['imap'], $sequence, implode(' ', $flags), ST_UID);
}
if ($rule['action'] == INGO_STORAGE_ACTION_KEEP) {
/* Add these indices to the current whitelist. */
$whitelist_ids = array_unique($indices + $whitelist_ids);
} elseif ($rule['action'] == INGO_STORAGE_ACTION_MOVE) {
/* We need to grab the overview first. */
if ($detailmsg) {
$overview = @imap_fetch_overview($params['imap'], $sequence, FT_UID);
}
/* Move the messages to the requested mailbox. */
@imap_mail_move($params['imap'], $sequence, $rule['action-value'], CP_UID);
@imap_expunge($params['imap']);
/* Display notification message(s). */
if ($detailmsg) {
foreach ($overview as $msg) {
$notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been moved to the folder \"%s\"."),
isset($msg->subject) ? MIME::decode($msg->subject, NLS::getCharset()) : _("[No Subject]"),
MIME::decode($msg->from, NLS::getCharset()),
$rule['action-value']), 'horde.message');
}
} else {
$notification->push(sprintf(_("Filter activity: %s message(s) have been moved to the folder \"%s\"."),
count($indices),
$rule['action-value']), 'horde.message');
}
} elseif ($rule['action'] == INGO_STORAGE_ACTION_DISCARD) {
/* We need to grab the overview first. */
if ($detailmsg) {
$overview = @imap_fetch_overview($params['imap'], $sequence, FT_UID);
}
/* Delete the messages now. */
@imap_delete($params['imap'], $sequence, FT_UID);
@imap_expunge($params['imap']);
/* Display notification message(s). */
if ($detailmsg) {
foreach ($overview as $msg) {
$notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been deleted."),
isset($msg->subject) ? MIME::decode($msg->subject, NLS::getCharset()) : _("[No Subject]"),
MIME::decode($msg->from, NLS::getCharset())), 'horde.message');
}
} else {
$notification->push(sprintf(_("Filter activity: %s message(s) have been deleted."), count($indices)), 'horde.message');
}
} elseif ($rule['action'] == INGO_STORAGE_ACTION_MOVEKEEP) {
/* Copy the messages to the requested mailbox. */
@imap_mail_copy($params['imap'], $sequence, $rule['action-value'], CP_UID);
/* Display notification message(s). */
if ($detailmsg) {
$overview = @imap_fetch_overview($params['imap'], $sequence, FT_UID);
foreach ($overview as $msg) {
$notification->push(sprintf(_("Filter activity: The message \"%s\" from \"%s\" has been copied to the folder \"%s\"."), isset($msg->subject) ? MIME::decode($msg->subject, NLS::getCharset()) : _("[No Subject]"), MIME::decode($msg->from, NLS::getCharset()), $rule['action-value']), 'horde.message');
}
} else {
$notification->push(sprintf(_("Filter activity: %s message(s) have been copied to the folder \"%s\"."), count($indices), $rule['action-value']), 'horde.message');
}
}
}
break;
}
/* Handle stop flag. */
if ($stop_flag && $rule['stop']) {
break;
}
}
/* Set cache flag. */
$imap_cache->storeCache($params['imap'], $params['mailbox'], array('ingochange' => $_SESSION['ingo']['change']));
return true;
}
/**
* Is the apply() function available?
* The 'mail/getStream' API function must be available.
*
* @access public
*
* @return boolean True if apply() is available, false if not.
*/
function canApply()
{
global $registry;
return ($this->performAvailable() && $registry->hasMethod('mail/getStream'));
}
/**
* Apply the filters now.
*
* @access public
*
* @return boolean See perform().
*/
function apply()
{
global $registry;
if ($this->canApply()) {
$res = $registry->call('mail/getStream', array('INBOX'));
if ($res !== false) {
$ob = @imap_check($res);
return $this->perform(array('imap' => $res, 'mailbox' => $ob->Mailbox));
}
}
return false;
}
}
|