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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
<?php
/**
* The Ingo_Script_imap:: class represents an IMAP client-side script
* generator.
*
* $Horde: ingo/lib/Script/imap.php,v 1.49.10.21 2009/12/30 17:58:52 jan Exp $
*
* Copyright 2003-2009 The Horde Project (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/asl.php.
*
* @author Michael Slusarz <slusarz@horde.org>
* @package Ingo
*/
class Ingo_Script_imap extends Ingo_Script {
/**
* The list of actions allowed (implemented) for this driver.
*
* @var array
*/
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
*/
var $_categories = array(
INGO_STORAGE_ACTION_BLACKLIST,
INGO_STORAGE_ACTION_WHITELIST
);
/**
* The list of tests allowed (implemented) for this driver.
*
* @var array
*/
var $_tests = array(
'contains', 'not contain'
);
/**
* The types of tests allowed (implemented) for this driver.
*
* @var array
*/
var $_types = array(
INGO_STORAGE_TYPE_HEADER,
INGO_STORAGE_TYPE_SIZE,
INGO_STORAGE_TYPE_BODY
);
/**
* Does the driver support setting IMAP flags?
*
* @var boolean
*/
var $_supportIMAPFlags = true;
/**
* Does the driver support the stop-script option?
*
* @var boolean
*/
var $_supportStopScript = true;
/**
* This driver can perform on demand filtering (in fact, that is all
* it can do).
*
* @var boolean
*/
var $_ondemand = true;
/**
* The API to use for IMAP functions.
*
* @var Ingo_Script_imap_api
*/
var $_api;
/**
* Perform the filtering specified in the rules.
*
* @param array $params The parameter array. It MUST contain:
* - 'imap': An open IMAP stream.
* - 'mailbox': The name of the mailbox to filter.
* - 'show_filter_msg': Show detailed filter status
* messages?
* - 'filter_seen': Only filter seen messages?
*
* @return boolean True if filtering performed, false if not.
*/
function perform($params)
{
global $ingo_storage, $notification, $prefs;
if (empty($params['api'])) {
$this->_api = Ingo_Script_imap_api::factory('live', $params);
} else {
$this->_api = &$params['api'];
}
/* Indices that will be ignored by subsequent rules. */
$ignore_ids = array();
/* Only do filtering if:
1. We have not done filtering before -or-
2. The mailbox has changed -or-
3. The rules have changed. */
$cache = $this->_api->getCache();
if (($cache !== false) && ($cache == $_SESSION['ingo']['change'])) {
return true;
}
require_once 'Horde/MIME.php';
/* Grab the rules list. */
$filters = &$ingo_storage->retrieve(INGO_STORAGE_ACTION_FILTERS);
/* 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();
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;
}
require_once INGO_BASE . '/lib/IMAP/Search.php';
$query = new Ingo_IMAP_Search_Query();
foreach ($addr as $val) {
$ob = $this->_getQuery($params);
$ob->header('from', $val);
$search_array[] = $ob;
}
$query->imapOr($search_array);
$indices = $this->_api->search($query);
/* Remove any indices that got in there by way of partial
* address match. */
$sequence = implode(',', $indices);
$msgs = $this->_api->fetchMessageOverviews($sequence);
foreach ($msgs as $msg) {
$from_addr = MIME::bareAddress($msg->from);
$found = false;
foreach ($addr as $val) {
if (strtolower($from_addr) == strtolower($val)) {
$found = true;
}
}
if (!$found) {
$indices = array_diff($indices, array($msg->uid));
}
}
if ($rule['action'] == INGO_STORAGE_ACTION_BLACKLIST) {
$indices = array_diff($indices, $ignore_ids);
if (!empty($indices)) {
$sequence = implode(',', $indices);
if (!empty($bl_folder)) {
$this->_api->moveMessages($sequence, $bl_folder);
} else {
$this->_api->deleteMessages($sequence);
}
$this->_api->expunge($indices);
$notification->push(sprintf(_("Filter activity: %s message(s) that matched the blacklist were deleted."), count($indices)), 'horde.message');
}
} else {
$ignore_ids = $indices;
}
break;
case INGO_STORAGE_ACTION_KEEP:
case INGO_STORAGE_ACTION_MOVE:
case INGO_STORAGE_ACTION_DISCARD:
require_once INGO_BASE . '/lib/IMAP/Search.php';
$query = new Ingo_IMAP_Search_Query();
foreach ($rule['conditions'] as $val) {
$ob = $this->_getQuery($params);
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 (strpos($val['field'], ',') == false) {
if ($val['match'] == 'contains') {
$ob->header($val['field'], $val['value'], false);
} elseif ($val['match'] == 'not contain') {
$ob->header($val['field'], $val['value'], true);
}
} else {
$headers = array();
foreach (explode(',', $val['field']) as $header) {
$headerOb = $this->_getQuery($params);
if ($val['match'] == 'contains') {
$headerOb->header($header, $val['value'], false);
} elseif ($val['match'] == 'not contain') {
$headerOb->header($header, $val['value'], true);
}
$headers[] = $headerOb;
}
if ($val['match'] == 'contains') {
$ob->imapOr($headers);
} elseif ($val['match'] == 'not contain') {
$ob->imapAnd($headers);
}
}
}
$search_array[] = $ob;
}
if ($rule['combine'] == INGO_STORAGE_COMBINE_ALL) {
$query->imapAnd($search_array);
} else {
$query->imapOr($search_array);
}
$indices = $this->_api->search($query);
if (($indices = array_diff($indices, $ignore_ids))) {
if ($rule['stop']) {
/* If the stop action is set, add these
* indices to the list of ids that will be
* ignored by subsequent rules. */
$ignore_ids = array_unique($indices + $ignore_ids);
}
$sequence = implode(',', $indices);
/* 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';
}
$this->_api->setMessageFlags($sequence,
implode(' ', $flags));
}
if ($rule['action'] == INGO_STORAGE_ACTION_KEEP) {
/* Add these indices to the ignore list. */
$ignore_ids = array_unique($indices + $ignore_ids);
} elseif ($rule['action'] == INGO_STORAGE_ACTION_MOVE) {
/* We need to grab the overview first. */
if ($params['show_filter_msg']) {
$overview = $this->_api->fetchMessageOverviews($sequence);
}
/* Move the messages to the requested mailbox. */
$this->_api->moveMessages($sequence,
$rule['action-value']);
$this->_api->expunge($indices);
/* Display notification message(s). */
if ($params['show_filter_msg']) {
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]"),
isset($msg->from) ? MIME::decode($msg->from, NLS::getCharset()) : _("[No Sender]"),
String::convertCharset($rule['action-value'], 'UTF7-IMAP', NLS::getCharset())),
'horde.message');
}
} else {
$notification->push(sprintf(_("Filter activity: %s message(s) have been moved to the folder \"%s\"."),
count($indices),
String::convertCharset($rule['action-value'], 'UTF7-IMAP', NLS::getCharset())), 'horde.message');
}
} elseif ($rule['action'] == INGO_STORAGE_ACTION_DISCARD) {
/* We need to grab the overview first. */
if ($params['show_filter_msg']) {
$overview = $this->_api->fetchMessageOverviews($sequence);
}
/* Delete the messages now. */
$this->_api->deleteMessages($sequence);
$this->_api->expunge($indices);
/* Display notification message(s). */
if ($params['show_filter_msg']) {
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]"),
isset($msg->from) ? MIME::decode($msg->from, NLS::getCharset()) : _("[No Sender]")),
'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. */
$this->_api->copyMessages($sequence,
$rule['action-value']);
/* Display notification message(s). */
if ($params['show_filter_msg']) {
$overview = $this->_api->fetchMessageOverviews($sequence);
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]"),
isset($msg->from) ? MIME::decode($msg->from, NLS::getCharset()) : _("[No Sender]"),
String::convertCharset($rule['action-value'], 'UTF7-IMAP', NLS::getCharset())),
'horde.message');
}
} else {
$notification->push(sprintf(_("Filter activity: %s message(s) have been copied to the folder \"%s\"."), count($indices), String::convertCharset($rule['action-value'], 'UTF7-IMAP', NLS::getCharset())), 'horde.message');
}
}
}
break;
}
}
/* Set cache flag. */
$this->_api->storeCache($_SESSION['ingo']['change']);
return true;
}
/**
* Is the apply() function available?
* The 'mail/getStream' API function must be available.
*
* @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.
*
* @return boolean See perform().
*/
function apply()
{
global $registry, $prefs;
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, 'filter_seen' => $prefs->getValue('filter_seen'), 'show_filter_msg' => $prefs->getValue('show_filter_msg')));
}
}
return false;
}
/**
* Returns a query object prepared for adding further criteria.
*
* @param $params
*
* @return Ingo_IMAP_Search_Query A query object.
*/
function _getQuery($params)
{
$ob = new Ingo_IMAP_Search_Query();
$ob->deleted(false);
$ob->seen($params['filter_seen'] == INGO_SCRIPT_FILTER_SEEN);
return $ob;
}
}
class Ingo_Script_imap_api {
var $_params;
function Ingo_Script_imap_api($params = array())
{
$this->_params = $params;
}
function factory($type, $params)
{
$class = 'Ingo_Script_imap_' . $type;
if (!class_exists($class)) {
require dirname(__FILE__) . '/imap/' . $type . '.php';
}
return new $class($params);
}
function deleteMessages($sequence)
{
return PEAR::raiseError('Not implemented.');
}
function expunge($indices)
{
return PEAR::raiseError('Not implemented.');
}
function moveMessages($sequence, $folder)
{
return PEAR::raiseError('Not implemented.');
}
function copyMessages($sequence, $folder)
{
return PEAR::raiseError('Not implemented.');
}
function setMessageFlags($sequence, $flags)
{
return PEAR::raiseError('Not implemented.');
}
function fetchMessageOverviews($sequence)
{
return PEAR::raiseError('Not implemented.');
}
function search($query)
{
return PEAR::raiseError('Not implemented.');
}
function getCache()
{
return false;
}
function storeCache($timestamp)
{
}
}
|