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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
|
<?php
/**
* $Horde: ingo/lib/Storage.php,v 1.43.8.24 2009/12/22 01:53:26 jan Exp $
*
* See the enclosed file LICENSE for license information (ASL). If you
* did not receive this file, see http://www.horde.org/licenses/asl.php.
*
* @package Ingo
*/
/**
* Ingo_Storage:: 'combine' constants
*/
define('INGO_STORAGE_COMBINE_ALL', 1);
define('INGO_STORAGE_COMBINE_ANY', 2);
/**
* Ingo_Storage:: 'action' constants
*/
define('INGO_STORAGE_ACTION_FILTERS', 0);
define('INGO_STORAGE_ACTION_KEEP', 1);
define('INGO_STORAGE_ACTION_MOVE', 2);
define('INGO_STORAGE_ACTION_DISCARD', 3);
define('INGO_STORAGE_ACTION_REDIRECT', 4);
define('INGO_STORAGE_ACTION_REDIRECTKEEP', 5);
define('INGO_STORAGE_ACTION_REJECT', 6);
define('INGO_STORAGE_ACTION_BLACKLIST', 7);
define('INGO_STORAGE_ACTION_VACATION', 8);
define('INGO_STORAGE_ACTION_WHITELIST', 9);
define('INGO_STORAGE_ACTION_FORWARD', 10);
define('INGO_STORAGE_ACTION_MOVEKEEP', 11);
define('INGO_STORAGE_ACTION_FLAGONLY', 12);
define('INGO_STORAGE_ACTION_NOTIFY', 13);
define('INGO_STORAGE_ACTION_SPAM', 14);
/**
* Ingo_Storage:: 'flags' constants
*/
define('INGO_STORAGE_FLAG_ANSWERED', 1);
define('INGO_STORAGE_FLAG_DELETED', 2);
define('INGO_STORAGE_FLAG_FLAGGED', 4);
define('INGO_STORAGE_FLAG_SEEN', 8);
/**
* Ingo_Storage:: 'type' constants.
*/
define('INGO_STORAGE_TYPE_HEADER', 1);
define('INGO_STORAGE_TYPE_SIZE', 2);
define('INGO_STORAGE_TYPE_BODY', 3);
/**
* Ingo_Storage:: defines an API to store the various filter rules.
*
* @author Michael Slusarz <slusarz@horde.org>
* @author Jan Schneider <jan@horde.org>
* @package Ingo
*/
class Ingo_Storage {
/**
* Driver specific parameters.
*
* @var array
*/
var $_params = array();
/**
* Cached rule objects.
*
* @var array
*/
var $_cache = array();
/**
* Has _addShutdownCache() been called yet?
*
* @var boolean
*/
var $_shutdownCache = false;
/**
* Attempts to return a concrete Ingo_Storage instance based on $driver.
*
* @param string $driver The type of concrete Ingo_Storage subclass to
* return. This is based on the storage driver
* ($driver). The code is dynamically included.
* @param array $params A hash containing any additional configuration or
* connection parameters a subclass might need.
*
* @return mixed The newly created concrete Ingo_Storage instance, or
* false on an error.
*/
function factory($driver = null, $params = null)
{
if (is_null($driver)) {
$driver = $GLOBALS['conf']['storage']['driver'];
}
$driver = basename($driver);
if (is_null($params)) {
$params = Horde::getDriverConfig('storage', $driver);
}
require_once dirname(__FILE__) . '/Storage/' . $driver . '.php';
$class = 'Ingo_Storage_' . $driver;
if (class_exists($class)) {
return new $class($params);
} else {
return false;
}
}
/**
* Retrieves the specified data.
*
* @param integer $field The field name of the desired data
* (INGO_STORAGE_ACTION_* constants).
* @param boolean $cache Use the cached object?
* @param boolean $readonly Whether to disable any write operations.
*
* @return Ingo_Storage_rule|Ingo_Storage_filters The specified object.
*/
function &retrieve($field, $cache = true, $readonly = false)
{
/* Don't cache if using shares. */
if ($cache && empty($GLOBALS['ingo_shares'])) {
if (!isset($this->_cache[$field])) {
$this->_cache[$field] = array('mod' => false);
if (isset($_SESSION['ingo']['storage'][$field])) {
require_once 'Horde/SessionObjects.php';
$cacheSess = &Horde_SessionObjects::singleton();
$this->_cache[$field]['ob'] = $cacheSess->query($_SESSION['ingo']['storage'][$field]);
} else {
$this->_cache[$field]['ob'] = $this->_retrieve($field, $readonly);
}
if (!$this->_shutdownCache) {
register_shutdown_function(array(&$this, '_addCacheShutdown'));
$this->_shutdownCache = true;
}
}
$ob = $this->_cache[$field]['ob'];
} else {
$ob = $this->_retrieve($field, $readonly);
}
return $ob;
}
/**
* Retrieves the specified data from the storage backend.
*
* @abstract
*
* @param integer $field The field name of the desired data.
* See lib/Storage.php for the available fields.
* @param boolean $readonly Whether to disable any write operations.
*
* @return Ingo_Storage_rule|Ingo_Storage_filters The specified data.
*/
function _retrieve($field, $readonly = false)
{
return false;
}
/**
* Stores the specified data.
*
* @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store.
* @param boolean $cache Cache the object?
*
* @return boolean True on success.
*/
function store(&$ob, $cache = true)
{
$type = $ob->obType();
if (in_array($type, array(INGO_STORAGE_ACTION_BLACKLIST,
INGO_STORAGE_ACTION_VACATION,
INGO_STORAGE_ACTION_WHITELIST,
INGO_STORAGE_ACTION_FORWARD,
INGO_STORAGE_ACTION_SPAM))) {
$filters = $this->retrieve(INGO_STORAGE_ACTION_FILTERS);
if ($filters->findRuleId($type) === null) {
switch ($type) {
case INGO_STORAGE_ACTION_BLACKLIST:
$name = 'Blacklist';
break;
case INGO_STORAGE_ACTION_VACATION:
$name = 'Vacation';
break;
case INGO_STORAGE_ACTION_WHITELIST:
$name = 'Whitelist';
break;
case INGO_STORAGE_ACTION_FORWARD:
$name = 'Forward';
break;
case INGO_STORAGE_ACTION_SPAM:
$name = 'Spam Filter';
break;
}
$filters->addRule(array('action' => $type, 'name' => $name));
$result = $this->store($filters, $cache);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
}
}
$result = $this->_store($ob);
if ($cache) {
$this->_cache[$ob->obType()] = array('ob' => $ob, 'mod' => true);
if (!$this->_shutdownCache) {
register_shutdown_function(array(&$this, '_addCacheShutdown'));
$this->_shutdownCache = true;
}
}
return $result;
}
/**
* Stores the specified data in the storage backend.
*
* @abstract
* @access private
*
* @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store.
*
* @return boolean True on success.
*/
function _store(&$ob)
{
return false;
}
/**
* Saves a copy of objects at the end of a request.
*
* @access private
*/
function _addCacheShutdown()
{
require_once 'Horde/SessionObjects.php';
$cache = &Horde_SessionObjects::singleton();
/* Store the current objects. */
foreach ($this->_cache as $key => $val) {
if (!$val['mod'] && isset($_SESSION['ingo']['storage'][$key])) {
continue;
}
if (isset($_SESSION['ingo']['storage'][$key])) {
$cache->setPruneFlag($_SESSION['ingo']['storage'][$key], true);
}
$_SESSION['ingo']['storage'][$key] = $cache->storeOid($val['ob'], false);
}
}
/**
* Returns information on a given action constant.
*
* @param integer $action The INGO_STORAGE_ACTION_* value.
*
* @return stdClass Object with the following values:
* <pre>
* 'flags' => (boolean) Does this action allow flags to be set?
* 'label' => (string) The label for this action.
* 'type' => (string) Either 'folder', 'text', or empty.
* </pre>
*/
function getActionInfo($action)
{
$ob = &new stdClass;
$ob->flags = false;
$ob->type = 'text';
switch ($action) {
case INGO_STORAGE_ACTION_KEEP:
$ob->label = _("Deliver into my Inbox");
$ob->type = false;
$ob->flags = true;
break;
case INGO_STORAGE_ACTION_MOVE:
$ob->label = _("Deliver to folder...");
$ob->type = 'folder';
$ob->flags = true;
break;
case INGO_STORAGE_ACTION_DISCARD:
$ob->label = _("Delete message completely");
$ob->type = false;
break;
case INGO_STORAGE_ACTION_REDIRECT:
$ob->label = _("Redirect to...");
break;
case INGO_STORAGE_ACTION_REDIRECTKEEP:
$ob->label = _("Deliver into my Inbox and redirect to...");
$ob->flags = true;
break;
case INGO_STORAGE_ACTION_MOVEKEEP:
$ob->label = _("Deliver into my Inbox and copy to...");
$ob->type = 'folder';
$ob->flags = true;
break;
case INGO_STORAGE_ACTION_REJECT:
$ob->label = _("Reject with reason...");
break;
case INGO_STORAGE_ACTION_FLAGONLY:
$ob->label = _("Only flag the message");
$ob->type = false;
$ob->flags = true;
break;
case INGO_STORAGE_ACTION_NOTIFY:
$ob->label = _("Notify email address...");
break;
}
return $ob;
}
/**
* Returns information on a given test string.
*
* @param string $action The test string.
*
* @return stdClass Object with the following values:
* <pre>
* 'label' => (string) The label for this action.
* 'type' => (string) Either 'int', 'none', or 'text'.
* </pre>
*/
function getTestInfo($test)
{
/* Mapping of gettext strings -> labels. */
$labels = array(
'contains' => _("Contains"),
'not contain' => _("Doesn't contain"),
'is' => _("Is"),
'not is' => _("Isn't"),
'begins with' => _("Begins with"),
'not begins with' => _("Doesn't begin with"),
'ends with' => _("Ends with"),
'not ends with' => _("Doesn't end with"),
'exists' => _("Exists"),
'not exist' => _("Doesn't exist"),
'regex' => _("Regular expression"),
'matches' => _("Matches (with placeholders)"),
'not matches' => _("Doesn't match (with placeholders)"),
'less than' => _("Less than"),
'less than or equal to' => _("Less than or equal to"),
'greater than' => _("Greater than"),
'greater than or equal to' => _("Greater than or equal to"),
'equal' => _("Equal to"),
'not equal' => _("Not equal to")
);
/* The type of tests available. */
$types = array(
'int' => array(
'less than', 'less than or equal to', 'greater than',
'greater than or equal to', 'equal', 'not equal'
),
'none' => array(
'exists', 'not exist'
),
'text' => array(
'contains', 'not contain', 'is', 'not is', 'begins with',
'not begins with', 'ends with', 'not ends with', 'regex',
'matches', 'not matches'
)
);
/* Create the information object. */
$ob = &new stdClass;
$ob->label = $labels[$test];
foreach ($types as $key => $val) {
if (in_array($test, $val)) {
$ob->type = $key;
break;
}
}
return $ob;
}
/**
* Removes the user data from the storage backend.
* Stub for child class to override if it can implement.
*
* @param string $user The user name to delete filters for.
*
* @return mixed True | PEAR_Error
*/
function removeUserData($user)
{
return PEAR::raiseError(_("Removing user data is not supported with the current filter storage backend."));
}
}
/**
* Ingo_Storage_rule:: is the base class for the various action objects
* used by Ingo_Storage.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_rule {
/**
* The object type.
*
* @var integer
*/
var $_obtype;
/**
* Whether the rule has been saved (if being saved separately).
*
* @var boolean
*/
var $_saved = false;
/**
* Returns the object rule type.
*
* @return integer The object rule type.
*/
function obType()
{
return $this->_obtype;
}
/**
* Marks the rule as saved or unsaved.
*
* @param boolean $data Whether the rule has been saved.
*/
function setSaved($data)
{
$this->_saved = $data;
}
/**
* Returns whether the rule has been saved.
*
* @return boolean True if the rule has been saved.
*/
function isSaved()
{
return $this->_saved;
}
/**
* Function to manage an internal address list.
*
* @access private
*
* @param mixed $data The incoming data (array or string).
* @param boolean $sort Sort the list?
*
* @return array The address list.
*/
function &_addressList($data, $sort)
{
$output = array();
if (is_array($data)) {
$output = $data;
} else {
$data = trim($data);
$output = (empty($data)) ? array() : preg_split("/\s+/", $data);
}
if ($sort) {
require_once 'Horde/Array.php';
$output = Horde_Array::prepareAddressList($output);
}
return $output;
}
}
/**
* Ingo_Storage_blacklist is the object used to hold blacklist rule
* information.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_blacklist extends Ingo_Storage_rule {
var $_addr = array();
var $_folder = '';
var $_obtype = INGO_STORAGE_ACTION_BLACKLIST;
/**
* Sets the list of blacklisted addresses.
*
* @param mixed $data The list of addresses (array or string).
* @param boolean $sort Sort the list?
*
* @return mixed PEAR_Error on error, true on success.
*/
function setBlacklist($data, $sort = true)
{
$addr = &$this->_addressList($data, $sort);
if (!empty($GLOBALS['conf']['storage']['maxblacklist'])) {
$addr_count = count($addr);
if ($addr_count > $GLOBALS['conf']['storage']['maxblacklist']) {
return PEAR::raiseError(sprintf(_("Maximum number of blacklisted addresses exceeded (Total addresses: %s, Maximum addresses: %s). Could not add new addresses to blacklist."), $addr_count, $GLOBALS['conf']['storage']['maxblacklist']), 'horde.error');
}
}
$this->_addr = $addr;
return true;
}
function setBlacklistFolder($data)
{
$this->_folder = $data;
}
function getBlacklist()
{
return array_filter($this->_addr, array('Ingo', '_filterEmptyAddress'));
}
function getBlacklistFolder()
{
return $this->_folder;
}
}
/**
* Ingo_Storage_whitelist is the object used to hold whitelist rule
* information.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_whitelist extends Ingo_Storage_rule {
var $_addr = array();
var $_obtype = INGO_STORAGE_ACTION_WHITELIST;
/**
* Sets the list of whitelisted addresses.
*
* @param mixed $data The list of addresses (array or string).
* @param boolean $sort Sort the list?
*
* @return mixed PEAR_Error on error, true on success.
*/
function setWhitelist($data, $sort = true)
{
$addr = &$this->_addressList($data, $sort);
$addr = array_filter($addr, array('Ingo', '_filterEmptyAddress'));
if (!empty($GLOBALS['conf']['storage']['maxwhitelist'])) {
$addr_count = count($addr);
if ($addr_count > $GLOBALS['conf']['storage']['maxwhitelist']) {
return PEAR::raiseError(sprintf(_("Maximum number of whitelisted addresses exceeded (Total addresses: %s, Maximum addresses: %s). Could not add new addresses to whitelist."), $addr_count, $GLOBALS['conf']['storage']['maxwhitelist']), 'horde.error');
}
}
$this->_addr = $addr;
return true;
}
function getWhitelist()
{
return array_filter($this->_addr, array('Ingo', '_filterEmptyAddress'));
}
}
/**
* Ingo_Storage_forward is the object used to hold mail forwarding rule
* information.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_forward extends Ingo_Storage_rule {
var $_addr = array();
var $_keep = true;
var $_obtype = INGO_STORAGE_ACTION_FORWARD;
function setForwardAddresses($data, $sort = true)
{
$this->_addr = &$this->_addressList($data, $sort);
}
function setForwardKeep($data)
{
$this->_keep = $data;
}
function getForwardAddresses()
{
if (is_array($this->_addr)) {
foreach ($this->_addr as $key => $val) {
if (empty($val)) {
unset($this->_addr[$key]);
}
}
}
return $this->_addr;
}
function getForwardKeep()
{
return $this->_keep;
}
}
/**
* Ingo_Storage_vacation is the object used to hold vacation rule
* information.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_vacation extends Ingo_Storage_rule {
var $_addr = array();
var $_days = 7;
var $_excludes = array();
var $_ignorelist = true;
var $_reason = '';
var $_subject = '';
var $_start;
var $_end;
var $_obtype = INGO_STORAGE_ACTION_VACATION;
function setVacationAddresses($data, $sort = true)
{
$this->_addr = &$this->_addressList($data, $sort);
}
function setVacationDays($data)
{
$this->_days = $data;
}
function setVacationExcludes($data, $sort = true)
{
$this->_excludes = &$this->_addressList($data, $sort);
}
function setVacationIgnorelist($data)
{
$this->_ignorelist = $data;
}
function setVacationReason($data)
{
$this->_reason = $data;
}
function setVacationSubject($data)
{
$this->_subject = $data;
}
function setVacationStart($data)
{
$this->_start = $data;
}
function setVacationEnd($data)
{
$this->_end = $data;
}
function getVacationAddresses()
{
if (empty($GLOBALS['conf']['hooks']['vacation_addresses'])) {
return $this->_addr;
}
$addresses = Horde::callHook('_ingo_hook_vacation_addresses', array(Ingo::getUser()), 'ingo');
if (is_a($addresses, 'PEAR_Error')) {
$addresses = array();
}
return $addresses;
}
function getVacationDays()
{
return $this->_days;
}
function getVacationExcludes()
{
return $this->_excludes;
}
function getVacationIgnorelist()
{
return $this->_ignorelist;
}
function getVacationReason()
{
return $this->_reason;
}
function getVacationSubject()
{
return $this->_subject;
}
function getVacationStart()
{
return $this->_start;
}
function getVacationStartYear()
{
return date('Y', $this->_start);
}
function getVacationStartMonth()
{
return date('n', $this->_start);
}
function getVacationStartDay()
{
return date('j', $this->_start);
}
function getVacationEnd()
{
return $this->_end;
}
function getVacationEndYear()
{
return date('Y', $this->_end);
}
function getVacationEndMonth()
{
return date('n', $this->_end);
}
function getVacationEndDay()
{
return date('j', $this->_end);
}
}
/**
* Ingo_Storage_spam is an object used to hold default spam-rule filtering
* information.
*
* @author Jason M. Felice <jason.m.felice@gmail.com>
* @since Ingo 1.2
* @package Ingo
*/
class Ingo_Storage_spam extends Ingo_Storage_rule {
/**
* The object type.
*
* @var integer
*/
var $_obtype = INGO_STORAGE_ACTION_SPAM;
var $_folder = null;
var $_level = 5;
function setSpamFolder($folder)
{
$this->_folder = $folder;
}
function setSpamLevel($level)
{
$this->_level = $level;
}
function getSpamFolder()
{
return $this->_folder;
}
function getSpamLevel()
{
return $this->_level;
}
}
/**
* Ingo_Storage_filters is the object used to hold user-defined filtering rule
* information.
*
* @author Michael Slusarz <slusarz@horde.org>
* @since Ingo 1.0
* @package Ingo
*/
class Ingo_Storage_filters {
/**
* The filter list.
*
* @var array
*/
var $_filters = array();
/**
* The object type.
*
* @var integer
*/
var $_obtype = INGO_STORAGE_ACTION_FILTERS;
/**
* Returns the object rule type.
*
* @return integer The object rule type.
*/
function obType()
{
return $this->_obtype;
}
/**
* Propagates the filter list with data.
*
* @param array $data A list of rule hashes.
*/
function setFilterlist($data)
{
$this->_filters = $data;
}
/**
* Returns the filter list.
*
* @return array The list of rule hashes.
*/
function getFilterlist()
{
return $this->_filters;
}
/**
* Returns a single rule hash.
*
* @param integer $id A rule number.
*
* @return array The requested rule hash.
*/
function getRule($id)
{
return $this->_filters[$id];
}
/**
* Returns a rule hash with default value used when creating new rules.
*
* @return array A rule hash.
*/
function getDefaultRule()
{
return array(
'name' => _("New Rule"),
'combine' => INGO_STORAGE_COMBINE_ALL,
'conditions' => array(),
'action' => INGO_STORAGE_ACTION_KEEP,
'action-value' => '',
'stop' => true,
'flags' => 0,
'disable' => false
);
}
/**
* Searches for the first rule of a certain action type and returns its
* number.
*
* @param integer $action The field type of the searched rule
* (INGO_STORAGE_ACTION_* constants).
*
* @return integer The number of the first matching rule or null.
*/
function findRuleId($action)
{
foreach ($this->_filters as $id => $rule) {
if ($rule['action'] == $action) {
return $id;
}
}
}
/**
* Searches for and returns the first rule of a certain action type.
*
* @param integer $action The field type of the searched rule
* (INGO_STORAGE_ACTION_* constants).
*
* @return array The first matching rule hash or null.
*/
function findRule($action)
{
$id = $this->findRuleId($action);
if ($id !== null) {
return $this->getRule($id);
}
}
/**
* Adds a rule hash to the filters list.
*
* @param array $rule A rule hash.
* @param boolean $default If true merge the rule hash with default rule
* values.
*/
function addRule($rule, $default = true)
{
if ($default) {
$this->_filters[] = array_merge($this->getDefaultRule(), $rule);
} else {
$this->_filters[] = $rule;
}
}
/**
* Updates an existing rule with a rule hash.
*
* @param array $rule A rule hash
* @param integer $id A rule number
*/
function updateRule($rule, $id)
{
$this->_filters[$id] = $rule;
}
/**
* Deletes a rule from the filters list.
*
* @param integer $id Number of the rule to delete.
*
* @return boolean True if the rule has been found and deleted.
*/
function deleteRule($id)
{
if (isset($this->_filters[$id])) {
unset($this->_filters[$id]);
$this->_filters = array_values($this->_filters);
return true;
}
return false;
}
/**
* Creates a copy of an existing rule.
*
* The created copy is added to the filters list right after the original
* rule.
*
* @param integer $id Number of the rule to copy.
*
* @return boolean True if the rule has been found and copied.
*/
function copyRule($id)
{
if (isset($this->_filters[$id])) {
$newrule = $this->_filters[$id];
$newrule['name'] = sprintf(_("Copy of %s"), $this->_filters[$id]['name']);
$this->_filters = array_merge(array_slice($this->_filters, 0, $id + 1), array($newrule), array_slice($this->_filters, $id + 1));
return true;
}
return false;
}
/**
* Moves a rule up in the filters list.
*
* @param integer $id Number of the rule to move.
* @param integer $steps Number of positions to move the rule up.
*/
function ruleUp($id, $steps = 1)
{
for ($i = 0; $i < $steps && $id > 0;) {
$temp = $this->_filters[$id - 1];
$this->_filters[$id - 1] = $this->_filters[$id];
$this->_filters[$id] = $temp;
/* Continue to move up until we swap with a viewable category. */
if (in_array($temp['action'], $_SESSION['ingo']['script_categories'])) {
$i++;
}
$id--;
}
}
/**
* Moves a rule down in the filters list.
*
* @param integer $id Number of the rule to move.
* @param integer $steps Number of positions to move the rule down.
*/
function ruleDown($id, $steps = 1)
{
$rulecount = count($this->_filters) - 1;
for ($i = 0; $i < $steps && $id < $rulecount;) {
$temp = $this->_filters[$id + 1];
$this->_filters[$id + 1] = $this->_filters[$id];
$this->_filters[$id] = $temp;
/* Continue to move down until we swap with a viewable
category. */
if (in_array($temp['action'], $_SESSION['ingo']['script_categories'])) {
$i++;
}
$id++;
}
}
/**
* Disables a rule.
*
* @param integer $id Number of the rule to disable.
*/
function ruleDisable($id)
{
$this->_filters[$id]['disable'] = true;
}
/**
* Enables a rule.
*
* @param integer $id Number of the rule to enable.
*/
function ruleEnable($id)
{
$this->_filters[$id]['disable'] = false;
}
}
|