Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezmailfilterrule.php
Go to the documentation of this file.
1 <?php
3 //
4 // $Id: ezmailfilterrule.php 6657 2001-08-27 10:54:12Z jhe $
5 //
6 // eZMailFilterRule class
7 //
8 // Created on: <29-Mar-2001 14:20:04 fh>
9 //
10 // This source file is part of Exponential Basic, publishing software.
11 //
12 // Copyright (C) 1999-2001 eZ Systems. All rights reserved.
13 //
14 // This program is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU General Public License
16 // as published by the Free Software Foundation; either version 2
17 // of the License, or (at your option) any later version.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, US
27 //
28
29
32
41 // include_once( "ezmail/classes/ezmailfolder.php" );
42 // include_once( "ezmail/classes/ezmail.php" );
43
44 /* DEFINES */
45 define( "FILTER_MESSAGE", 0 );
46 define( "FILTER_BODY", 1 );
47 define( "FILTER_ANY", 2 );
48 define( "FILTER_TOCC", 3 );
49 define( "FILTER_SUBJECT", 4 );
50 define( "FILTER_FROM", 5 );
51 define( "FILTER_TO", 6 );
52 define( "FILTER_CC", 7 );
53
54 define( "FILTER_EQUALS", 0 );
55 define( "FILTER_NEQUALS", 1 );
56 define( "FILTER_CONTAINS", 2 );
57 define( "FILTER_NCONTAINS", 3 );
58 define( "FILTER_REGEXP", 4 );
59
60 class eZMailFilterRule
61 {
68 function __construct( $id="" )
69 {
70 if ( $id != "" )
71 {
72 $this->ID = $id;
73 $this->get( $this->ID );
74 }
75 }
76
80 function delete( $id = -1 )
81 {
82 $db =& eZDB::globalDatabase();
83 if( $id == -1 )
84 $id = $this->ID;
85
86 $db->query( "DELETE FROM eZMail_FilterRule WHERE ID='$id'" );
87 return true;
88 }
89
93 function store()
94 {
95 $db =& eZDB::globalDatabase();
96 $match = $db->escapeString( $this->Match );
97
98 if ( !isset( $this->ID ) )
99 {
100 $db->lock( "eZMail_FilterRule" );
101 $nextID = $db->nextID( "eZMail_FilterRule", "ID" );
102 $result = $db->query( "INSERT INTO eZMail_FilterRule ( ID, UserID, HeaderType, CheckType,
103 MatchValue, FolderID, IsActive )
104 VALUES (
105 '$nextID',
106 '$this->UserID',
107 '$this->HeaderType',
108 '$this->CheckType',
109 '$match',
110 '$this->FolderID',
111 '$this->IsActive' )
112 " );
113 $db->unlock();
114 $this->ID = $nextID;
115 }
116 else
117 {
118 $result = $db->query( "UPDATE eZMail_FilterRule SET
119 UserID='$this->UserID',
120 HeaderType='$this->HeaderType',
121 CheckType='$this->CheckType',
122 MatchValue='$match',
123 FolderID='$this->FolderID',
124 IsActive='$this->IsActive'
125 WHERE ID='$this->ID'
126 " );
127
128 }
129 if ( $result == false )
130 $db->rollback( );
131 else
132 $db->commit();
133
134 return true;
135 }
136
140 function get( $id="" )
141 {
142 $ret = false;
143 if ( $id != "" )
144 {
145 $db =& eZDB::globalDatabase();
146 $db->array_query( $mail_array, "SELECT * FROM eZMail_FilterRule WHERE ID='$id'" );
147 if ( count( $mail_array ) > 1 )
148 {
149 die( "Error: Mail filters with the same ID where found in the database. This should not happen." );
150 }
151 else if( count( $mail_array ) == 1 )
152 {
153 $this->ID =& $mail_array[0][ $db->fieldName("ID") ];
154 $this->UserID =& $mail_array[0][ $db->fieldName("UserID") ];
155 $this->HeaderType =& $mail_array[0][ $db->fieldName("HeaderType") ];
156 $this->CheckType =& $mail_array[0][ $db->fieldName("CheckType") ];
157 $this->Match =& $mail_array[0][ $db->fieldName("MatchValue") ];
158 $this->FolderID =& $mail_array[0][ $db->fieldName("FolderID") ];
159 $this->IsActive =& $mail_array[0][ $db->fieldName("IsActive") ];
160
161 $ret = true;
162 }
163 }
164 return $ret;
165 }
166
170 function id()
171 {
172 return $this->ID;
173 }
174
178 function owner()
179 {
180 return $this->UserID;;
181 }
182
186 function setOwner( $newOwner )
187 {
188 if ( is_a( $newOwner, "eZUser" ) )
189 $this->UserID = $newOwner->id();
190 else
191 $this->UserID = $newOwner;
192 }
193
197 function headerType()
198 {
199 return $this->HeaderType;
200 }
201
205 function setHeaderType( $value )
206 {
207 $this->HeaderType = $value;
208 }
209
213 function checkType()
214 {
215 return $this->CheckType;
216 }
217
221 function setCheckType( $value )
222 {
223 $this->CheckType = $value;
224 }
225
229 function match()
230 {
231 return $this->Match;
232 }
233
237 function setMatch( $value )
238 {
239 $this->Match = $value;
240 }
241
245 function isActive()
246 {
247
248 return $this->IsActive;
249 }
250
254 function setIsActive( $value )
255 {
256 $this->IsActive = $value;
257 }
258
262 function folderID()
263 {
264 return $this->FolderID;
265 }
266
270 function setFolderID( $value )
271 {
272 $this->FolderID = $value;
273 }
274
280 static public function getByUser( $user )
281 {
282 if( !is_string( $user ) && get_class( $user ) == "ezuser" )
283 $user = $user->id();
284
285 $database =& eZDB::globalDatabase();
286
287 $return_array = array();
288 $account_array = array();
289
290 $database->array_query( $account_array, "SELECT ID FROM eZMail_FilterRule WHERE UserID='$user'" );
291
292 for ( $i=0; $i < count($account_array); $i++ )
293 {
294 $return_array[$i] = new eZMailFilterRule( $account_array[$i][$database->fieldName("ID")] );
295 }
296
297 return $return_array;
298 }
299
309 function applyFilter( &$mail )
310 {
311 // Get the array of elements to search in
312 $searchArray =& $this->buildSearchArray( $mail );
313 // Loop through the elements and run the required tests. Exit at once when we find a match.
314 // This is not good coding practice, but it's the best way to do it here
315 switch( $this->CheckType )
316 {
317 case FILTER_EQUALS :
318 {
319 foreach( $searchArray as $searchItem )
320 {
321 if( $searchItem == $this->Match )
322 {
323 $this->doFilter( $mail );
324 return true;
325 }
326 }
327 }
328 break;
329 case FILTER_NEQALS :
330 {
331 foreach( $searchArray as $searchItem )
332 {
333 if( $searchItem == $this->Match )
334 return false;
335 }
336 $this->doFilter( $mail );
337 return true;
338 }
339 break;
340 case FILTER_CONTAINS :
341 {
342 foreach( $searchArray as $searchItem )
343 {
344 $pos = strpos( $searchItem, $this->Match );
345 if( $pos !== false )
346 {
347 $this->doFilter( $mail );
348 return true;
349 }
350 }
351 }
352 break;
353 case FILTER_NCONTAINS :
354 {
355 foreach( $searchArray as $searchItem )
356 {
357 $pos = strpos( $searchItem, $this->Match );
358 if( $pos !== false )
359 return false;
360 }
361 $this->doFilter( $mail );
362 return true;
363 }
364 break;
365 case FILTER_REGEXP :
366 {
367 foreach( $searchArray as $searchItem )
368 {
369 if( ereg( $this->Match, $searchItem ) )
370 {
371 $this->doFilter( $mail );
372 return true;
373 }
374 }
375 }
376 break;
377 }
378 return false;
379 }
380
387 function &buildSearchArray( &$mail )
388 {
389 $searchArray = array();
390 switch( $this->HeaderType )
391 {
392 case FILTER_MESSAGE :
393 {
394 $searchArray[] =& $mail->body();
395 $searchArray[] =& $mail->to();
396 $searchArray[] =& $mail->cc();
397 $searchArray[] =& $mail->bcc();
398 $searchArray[] =& $mail->from();
399 $searchArray[] =& $mail->fromName();
400 $searchArray[] =& $mail->subject();
401 }
402 break;
403 case FILTER_BODY :
404 {
405 $searchArray[] =& $mail->body();
406 }
407 break;
408 case FILTER_ANY :
409 {
410 $searchArray[] =& $mail->to();
411 $searchArray[] =& $mail->cc();
412 $searchArray[] =& $mail->bcc();
413 $searchArray[] =& $mail->from();
414 $searchArray[] =& $mail->fromName();
415 $searchArray[] =& $mail->subject();
416 }
417 break;
418 case FILTER_TOCC :
419 {
420 $searchArray[] =& $mail->to();
421 $searchArray[] =& $mail->cc();
422 }
423 break;
424 case FILTER_SUBJECT :
425 {
426 $searchArray[] =& $mail->subject();
427 }
428 break;
429 case FILTER_FROM :
430 {
431 $searchArray[] =& $mail->from();
432 $searchArray[] =& $mail->fromName();
433 }
434 break;
435 case FILTER_TO :
436 {
437 $searchArray[] =& $mail->to();
438 }
439 break;
440 case FILTER_CC :
441 {
442 $searchArray[] =& $mail->cc();
443 }
444 break;
445 }
446 return $searchArray;
447 }
448
452 function doFilter( &$mail )
453 {
454 $folder = new eZMailFolder( $this->FolderID );
455
456 if( is_a( $folder, "eZMailFolder" ) )
457 {
458 $folder->addMail( $mail );
459 }
460 else
461 {
462 $inbox = eZMailFolder::getSpecialFolder( INBOX );
463 $inbox->addMail( $mail );
464 }
465 }
466
467 var $ID;
468 var $UserID;
469
470 // the filter
471 var $HeaderType;
472 var $CheckType;
473 var $Match;
474
475 var $FolderID;
476 var $IsActive;
477 }
478
481
491 class eZMailFilter
492 {
493 function eZMailFilter( $userID = false )
494 {
495 if ( $userID == false )
496 {
497 $user =& eZUser::currentUser();
498 $userID = $user->id();
499 }
500 $this->Filters = eZMailFilterRule::getByUser( $userID );
501 $this->Inbox = eZMailFolder::getSpecialFolder( INBOX );
502 $this->numFilters = count( $this->Filters );
503 }
504
505 function runFilters( &$mail )
506 {
507 $i = 0;
508 $res = false;
509 do
510 {
511 // echo "Running filter\n";
512 if ( count( $this->Filters ) > 0 )
513 $res = $this->Filters[$i]->applyFilter( $mail );
514 $i++;
515 }
516 while ( $i < $this->$NumFilters && $res == false );
517
518 if ( $res == false )
519 {
520 // echo "Didn't match\n";
521 $this->Inbox->addMail( $mail );
522 }
523 }
524
525 var $NumFilters;
526 var $Filters;
527 var $Inbox;
528 }
529
530 ?>
$i
$i
Definition: admin/adlist.php:91
$userID
else $userID
Definition: appointmentedit.php:121
eZDB\globalDatabase
static & globalDatabase()
Definition: ezdb.php:134
eZMailFilterRule
Definition: ezmailfilterrule.php:61
eZMailFilterRule\setOwner
setOwner( $newOwner)
Definition: ezmailfilterrule.php:186
eZMailFilterRule\applyFilter
applyFilter(&$mail)
Definition: ezmailfilterrule.php:309
eZMailFilterRule\doFilter
doFilter(&$mail)
Definition: ezmailfilterrule.php:452
eZMailFilterRule\headerType
headerType()
Definition: ezmailfilterrule.php:197
eZMailFilterRule\getByUser
static getByUser( $user)
Definition: ezmailfilterrule.php:280
eZMailFilterRule\__construct
__construct( $id="")
Definition: ezmailfilterrule.php:68
eZMailFilterRule\setIsActive
setIsActive( $value)
Definition: ezmailfilterrule.php:254
eZMailFilterRule\setMatch
setMatch( $value)
Definition: ezmailfilterrule.php:237
eZMailFilterRule\folderID
folderID()
Definition: ezmailfilterrule.php:262
eZMailFilterRule\$FolderID
$FolderID
Definition: ezmailfilterrule.php:475
eZMailFilterRule\$ID
$ID
Definition: ezmailfilterrule.php:467
eZMailFilterRule\setHeaderType
setHeaderType( $value)
Definition: ezmailfilterrule.php:205
eZMailFilterRule\setFolderID
setFolderID( $value)
Definition: ezmailfilterrule.php:270
eZMailFilterRule\checkType
checkType()
Definition: ezmailfilterrule.php:213
eZMailFilterRule\owner
owner()
Definition: ezmailfilterrule.php:178
eZMailFilterRule\$UserID
$UserID
Definition: ezmailfilterrule.php:468
eZMailFilterRule\$CheckType
$CheckType
Definition: ezmailfilterrule.php:472
eZMailFilterRule\id
id()
Definition: ezmailfilterrule.php:170
eZMailFilterRule\match
match()
Definition: ezmailfilterrule.php:229
eZMailFilterRule\buildSearchArray
& buildSearchArray(&$mail)
Definition: ezmailfilterrule.php:387
eZMailFilterRule\$IsActive
$IsActive
Definition: ezmailfilterrule.php:476
eZMailFilterRule\isActive
isActive()
Definition: ezmailfilterrule.php:245
eZMailFilterRule\setCheckType
setCheckType( $value)
Definition: ezmailfilterrule.php:221
eZMailFilterRule\store
store()
Definition: ezmailfilterrule.php:93
eZMailFilterRule\$HeaderType
$HeaderType
Definition: ezmailfilterrule.php:471
eZMailFilterRule\$Match
$Match
Definition: ezmailfilterrule.php:473
eZMailFilter
Definition: ezmailfilterrule.php:492
eZMailFilter\$NumFilters
$NumFilters
Definition: ezmailfilterrule.php:525
eZMailFilter\$Inbox
$Inbox
Definition: ezmailfilterrule.php:527
eZMailFilter\$Filters
$Filters
Definition: ezmailfilterrule.php:526
eZMailFilter\eZMailFilter
eZMailFilter( $userID=false)
Definition: ezmailfilterrule.php:493
eZMailFilter\runFilters
runFilters(&$mail)
Definition: ezmailfilterrule.php:505
eZMailFolder
Definition: ezmailfolder.php:49
eZMailFolder\getSpecialFolder
static getSpecialFolder( $specialType, $user=false)
Definition: ezmailfolder.php:477
eZUser\currentUser
static & currentUser()
Definition: ezuser.php:896
$result
$result
Definition: companysearch.php:94
$id
$id
Definition: companytypelist.php:193
$db
$db
Definition: ezarticle/user/authorlist.php:53
$mail_array
$mail_array
Definition: ezbug/admin/cron.php:42
$mail
$mail
Definition: ezbulkmail/admin/maillist.php:61
$folder
if(!isset( $FolderID)) $folder
Definition: ezfilemanager/admin/browse.php:74
FILTER_TO
const FILTER_TO
Definition: ezmailfilterrule.php:51
FILTER_CC
const FILTER_CC
Definition: ezmailfilterrule.php:52
FILTER_TOCC
const FILTER_TOCC
Definition: ezmailfilterrule.php:48
FILTER_REGEXP
const FILTER_REGEXP
Definition: ezmailfilterrule.php:58
FILTER_ANY
const FILTER_ANY
Definition: ezmailfilterrule.php:47
FILTER_CONTAINS
const FILTER_CONTAINS
Definition: ezmailfilterrule.php:56
FILTER_BODY
const FILTER_BODY
Definition: ezmailfilterrule.php:46
FILTER_NCONTAINS
const FILTER_NCONTAINS
Definition: ezmailfilterrule.php:57
FILTER_FROM
const FILTER_FROM
Definition: ezmailfilterrule.php:50
FILTER_SUBJECT
const FILTER_SUBJECT
Definition: ezmailfilterrule.php:49
FILTER_EQUALS
const FILTER_EQUALS
Definition: ezmailfilterrule.php:54
FILTER_MESSAGE
const FILTER_MESSAGE
Definition: ezmailfilterrule.php:45
INBOX
const INBOX
Definition: ezmailfolder.php:43
$ret
$ret
Definition: ezsitemanager/admin/filelist.php:88
$res
$res
Definition: ezstats/admin/cron.php:45
$user
$user
Definition: user/adlist.php:39
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.