From 9b22223f642b6ab65a71694ff34f709f06dfce9d Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Fri, 17 Jul 2009 13:19:44 -0600 Subject: [PATCH] Horde 4/autoloading conventions --- ingo/lib/Driver.php | 2 +- ingo/lib/Driver/{ldap.php => Ldap.php} | 4 +- ingo/lib/Driver/{null.php => Null.php} | 4 +- ingo/lib/Driver/{sivtest.php => Sivtest.php} | 4 +- ingo/lib/Driver/{timsieved.php => Timsieved.php} | 4 +- ingo/lib/Driver/{vfs.php => Vfs.php} | 4 +- ingo/lib/Script.php | 2 +- ingo/lib/Script/{imap.php => Imap.php} | 97 +--- ingo/lib/Script/Imap/Api.php | 100 ++++ ingo/lib/Script/{imap/live.php => Imap/Live.php} | 4 +- ingo/lib/Script/{imap/mock.php => Imap/Mock.php} | 2 +- ingo/lib/Script/{maildrop.php => Maildrop.php} | 8 +- ingo/lib/Script/{procmail.php => Procmail.php} | 8 +- ingo/lib/Script/{sieve.php => Sieve.php} | 51 +- ingo/lib/Storage.php | 677 +---------------------- ingo/lib/Storage/Blacklist.php | 57 ++ ingo/lib/Storage/Filters.php | 264 +++++++++ ingo/lib/Storage/Filters/Sql.php | 344 ++++++++++++ ingo/lib/Storage/Forward.php | 45 ++ ingo/lib/Storage/{mock.php => Mock.php} | 16 +- ingo/lib/Storage/{prefs.php => Prefs.php} | 20 +- ingo/lib/Storage/Rule.php | 84 +++ ingo/lib/Storage/Spam.php | 44 ++ ingo/lib/Storage/{sql.php => Sql.php} | 379 +------------ ingo/lib/Storage/Vacation.php | 142 +++++ ingo/lib/Storage/Whitelist.php | 47 ++ 26 files changed, 1223 insertions(+), 1190 deletions(-) rename ingo/lib/Driver/{ldap.php => Ldap.php} (98%) rename ingo/lib/Driver/{null.php => Null.php} (80%) rename ingo/lib/Driver/{sivtest.php => Sivtest.php} (98%) rename ingo/lib/Driver/{timsieved.php => Timsieved.php} (96%) rename ingo/lib/Driver/{vfs.php => Vfs.php} (97%) rename ingo/lib/Script/{imap.php => Imap.php} (90%) create mode 100644 ingo/lib/Script/Imap/Api.php rename ingo/lib/Script/{imap/live.php => Imap/Live.php} (96%) rename ingo/lib/Script/{imap/mock.php => Imap/Mock.php} (98%) rename ingo/lib/Script/{maildrop.php => Maildrop.php} (99%) rename ingo/lib/Script/{procmail.php => Procmail.php} (99%) rename ingo/lib/Script/{sieve.php => Sieve.php} (98%) create mode 100644 ingo/lib/Storage/Blacklist.php create mode 100644 ingo/lib/Storage/Filters.php create mode 100644 ingo/lib/Storage/Filters/Sql.php create mode 100644 ingo/lib/Storage/Forward.php rename ingo/lib/Storage/{mock.php => Mock.php} (71%) rename ingo/lib/Storage/{prefs.php => Prefs.php} (92%) create mode 100644 ingo/lib/Storage/Rule.php create mode 100644 ingo/lib/Storage/Spam.php rename ingo/lib/Storage/{sql.php => Sql.php} (53%) create mode 100644 ingo/lib/Storage/Vacation.php create mode 100644 ingo/lib/Storage/Whitelist.php diff --git a/ingo/lib/Driver.php b/ingo/lib/Driver.php index a972abb82..b85d5e2d5 100644 --- a/ingo/lib/Driver.php +++ b/ingo/lib/Driver.php @@ -41,7 +41,7 @@ class Ingo_Driver static public function factory($driver, $params = array()) { $driver = basename($driver); - $class = 'Ingo_Driver_' . $driver; + $class = 'Ingo_Driver_' . ucfirst($driver); return class_exists($class) ? new $class($params) diff --git a/ingo/lib/Driver/ldap.php b/ingo/lib/Driver/Ldap.php similarity index 98% rename from ingo/lib/Driver/ldap.php rename to ingo/lib/Driver/Ldap.php index 7d1541011..d6a5ddf31 100644 --- a/ingo/lib/Driver/ldap.php +++ b/ingo/lib/Driver/Ldap.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Driver_ldap extends Ingo_Driver +class Ingo_Driver_Ldap extends Ingo_Driver { /** * Constructor. diff --git a/ingo/lib/Driver/null.php b/ingo/lib/Driver/Null.php similarity index 80% rename from ingo/lib/Driver/null.php rename to ingo/lib/Driver/Null.php index 54cadee06..231b6d8d0 100644 --- a/ingo/lib/Driver/null.php +++ b/ingo/lib/Driver/Null.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Driver_sivtest extends Ingo_Driver +class Ingo_Driver_Sivtest extends Ingo_Driver { /** * The Net_Sieve object. diff --git a/ingo/lib/Driver/timsieved.php b/ingo/lib/Driver/Timsieved.php similarity index 96% rename from ingo/lib/Driver/timsieved.php rename to ingo/lib/Driver/Timsieved.php index afc9ffa24..d600b8d3b 100644 --- a/ingo/lib/Driver/timsieved.php +++ b/ingo/lib/Driver/Timsieved.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Driver_timsieved extends Ingo_Driver +class Ingo_Driver_Timsieved extends Ingo_Driver { /** * The Net_Sieve object. diff --git a/ingo/lib/Driver/vfs.php b/ingo/lib/Driver/Vfs.php similarity index 97% rename from ingo/lib/Driver/vfs.php rename to ingo/lib/Driver/Vfs.php index 257a97c60..5e33423f3 100644 --- a/ingo/lib/Driver/vfs.php +++ b/ingo/lib/Driver/Vfs.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Driver_vfs extends Ingo_Driver +class Ingo_Driver_Vfs extends Ingo_Driver { /** * Constructs a new VFS-based storage driver. diff --git a/ingo/lib/Script.php b/ingo/lib/Script.php index b61a28cc3..eda050fdc 100644 --- a/ingo/lib/Script.php +++ b/ingo/lib/Script.php @@ -115,7 +115,7 @@ class Ingo_Script static public function factory($script, $params = array()) { $script = basename($script); - $class = 'Ingo_Script_' . $script; + $class = 'Ingo_Script_' . ucfirst($script); return class_exists($class) ? new $class($params) diff --git a/ingo/lib/Script/imap.php b/ingo/lib/Script/Imap.php similarity index 90% rename from ingo/lib/Script/imap.php rename to ingo/lib/Script/Imap.php index aff92dedf..a3f0a8742 100644 --- a/ingo/lib/Script/imap.php +++ b/ingo/lib/Script/Imap.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Script_imap extends Ingo_Script +class Ingo_Script_Imap extends Ingo_Script { /** * The list of actions allowed (implemented) for this driver. @@ -82,7 +82,7 @@ class Ingo_Script_imap extends Ingo_Script /** * The API to use for IMAP functions. * - * @var Ingo_Script_imap_api + * @var Ingo_Script_Imap_Api */ protected $_api; @@ -99,7 +99,7 @@ class Ingo_Script_imap extends Ingo_Script public function perform($params) { if (empty($params['api'])) { - $this->_api = Ingo_Script_imap_api::factory('live', $params); + $this->_api = Ingo_Script_Imap_Api::factory('live', $params); } else { $this->_api = &$params['api']; } @@ -368,92 +368,3 @@ class Ingo_Script_imap extends Ingo_Script } } - -class Ingo_Script_imap_api -{ - /** - * TODO - */ - protected $_params; - - /** - * TODO - */ - static public function factory($type, $params) - { - $class = 'Ingo_Script_imap_' . $type; - return new $class($params); - } - - /** - * TODO - */ - public function __construct($params = array()) - { - $this->_params = $params; - } - - /** - * TODO - */ - public function deleteMessages($indices) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function moveMessages($indices, $folder) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function copyMessages($indices, $folder) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function setMessageFlags($indices, $flags) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function fetchEnvelope($indices) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function search($query) - { - return PEAR::raiseError('Not implemented.'); - } - - /** - * TODO - */ - public function getCache() - { - return false; - } - - /** - * TODO - */ - public function storeCache($timestamp) - { - } - -} diff --git a/ingo/lib/Script/Imap/Api.php b/ingo/lib/Script/Imap/Api.php new file mode 100644 index 000000000..a2d2db8da --- /dev/null +++ b/ingo/lib/Script/Imap/Api.php @@ -0,0 +1,100 @@ + + * @package Ingo + */ +class Ingo_Script_Imap_Api +{ + /** + * TODO + */ + protected $_params; + + /** + * TODO + */ + static public function factory($type, $params) + { + $class = 'Ingo_Script_Imap_' . ucfirst($type); + return new $class($params); + } + + /** + * TODO + */ + public function __construct($params = array()) + { + $this->_params = $params; + } + + /** + * TODO + */ + public function deleteMessages($indices) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function moveMessages($indices, $folder) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function copyMessages($indices, $folder) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function setMessageFlags($indices, $flags) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function fetchEnvelope($indices) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function search($query) + { + return PEAR::raiseError('Not implemented.'); + } + + /** + * TODO + */ + public function getCache() + { + return false; + } + + /** + * TODO + */ + public function storeCache($timestamp) + { + } + +} diff --git a/ingo/lib/Script/imap/live.php b/ingo/lib/Script/Imap/Live.php similarity index 96% rename from ingo/lib/Script/imap/live.php rename to ingo/lib/Script/Imap/Live.php index a2ad225ad..dee3fac41 100644 --- a/ingo/lib/Script/imap/live.php +++ b/ingo/lib/Script/Imap/Live.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Script_imap_live extends Ingo_Script_imap_api +class Ingo_Script_Imap_Live extends Ingo_Script_Imap_Api { /** */ diff --git a/ingo/lib/Script/imap/mock.php b/ingo/lib/Script/Imap/Mock.php similarity index 98% rename from ingo/lib/Script/imap/mock.php rename to ingo/lib/Script/Imap/Mock.php index ecd4784a1..a88afe950 100644 --- a/ingo/lib/Script/imap/mock.php +++ b/ingo/lib/Script/Imap/Mock.php @@ -2,7 +2,7 @@ /** * TODO */ -class Ingo_Script_imap_mock extends Ingo_Script_imap_api +class Ingo_Script_Imap_Mock extends Ingo_Script_Imap_Api { /** * TODO diff --git a/ingo/lib/Script/maildrop.php b/ingo/lib/Script/Maildrop.php similarity index 99% rename from ingo/lib/Script/maildrop.php rename to ingo/lib/Script/Maildrop.php index 2c3b4483b..676f9454f 100644 --- a/ingo/lib/Script/maildrop.php +++ b/ingo/lib/Script/Maildrop.php @@ -1,6 +1,6 @@ * @@ -19,7 +19,7 @@ define('MAILDROP_STORAGE_ACTION_STOREANDFORWARD', 100); /** */ -class Ingo_Script_maildrop extends Ingo_Script { +class Ingo_Script_Maildrop extends Ingo_Script { /** * The list of actions allowed (implemented) for this driver. @@ -329,7 +329,7 @@ class Ingo_Script_maildrop extends Ingo_Script { /** * The Maildrop_Comment:: class represents a maildrop comment. This is - * a pretty simple class, but it makes the code in Ingo_Script_maildrop:: + * a pretty simple class, but it makes the code in Ingo_Script_Maildrop:: * cleaner as it provides a generate() function and can be added to the * recipe list the same way as a recipe can be. * @@ -414,7 +414,7 @@ class Maildrop_Recipe { * 'action-value' (only used if the * 'action' requires it) * @param array $scriptparams Array of parameters passed to - * Ingo_Script_maildrop::. + * Ingo_Script_Maildrop::. */ function Maildrop_Recipe($params = array(), $scriptparams = array()) { diff --git a/ingo/lib/Script/procmail.php b/ingo/lib/Script/Procmail.php similarity index 99% rename from ingo/lib/Script/procmail.php rename to ingo/lib/Script/Procmail.php index 494cfd260..f0228432d 100644 --- a/ingo/lib/Script/procmail.php +++ b/ingo/lib/Script/Procmail.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Script_procmail extends Ingo_Script { +class Ingo_Script_Procmail extends Ingo_Script { /** * The list of actions allowed (implemented) for this driver. @@ -320,7 +320,7 @@ class Ingo_Script_procmail extends Ingo_Script { /** * The Procmail_Comment:: class represents a Procmail comment. This is - * a pretty simple class, but it makes the code in Ingo_Script_procmail:: + * a pretty simple class, but it makes the code in Ingo_Script_Procmail:: * cleaner as it provides a generate() function and can be added to the * recipe list the same way as a recipe can be. * @@ -399,7 +399,7 @@ class Procmail_Recipe { * 'action-value' (only used if the * 'action' requires it) * @param array $scriptparams Array of parameters passed to - * Ingo_Script_procmail::. + * Ingo_Script_Procmail::. */ function Procmail_Recipe($params = array(), $scriptparams = array()) { diff --git a/ingo/lib/Script/sieve.php b/ingo/lib/Script/Sieve.php similarity index 98% rename from ingo/lib/Script/sieve.php rename to ingo/lib/Script/Sieve.php index 75395424d..6dd2fd90a 100644 --- a/ingo/lib/Script/sieve.php +++ b/ingo/lib/Script/Sieve.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Script_sieve extends Ingo_Script { +class Ingo_Script_Sieve extends Ingo_Script { /** * The list of actions allowed (implemented) for this driver. @@ -1666,12 +1666,12 @@ class Sieve_Test_Relational extends Sieve_Test { foreach ($headers as $val) { $headerstr .= (empty($headerstr) ? '"' : ', "') . - Ingo_Script_sieve::escapeString($val) . '"'; + Ingo_Script_Sieve::escapeString($val) . '"'; } $code .= $headerstr . "] "; } elseif ($header_count == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString($headers[0]) . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString($headers[0]) . '" '; } return $code . '["' . $this->_vars['value'] . '"]'; @@ -1855,11 +1855,11 @@ class Sieve_Test_Exists extends Sieve_Test { $headerstr = ''; foreach ($headers as $header) { $headerstr .= (empty($headerstr) ? '"' : ', "') . - Ingo_Script_sieve::escapeString($header) . '"'; + Ingo_Script_Sieve::escapeString($header) . '"'; } $code .= $headerstr . "] "; } elseif (count($headers) == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString($headers[0]) . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString($headers[0]) . '" '; } else { return "**error** No Headers Specified"; } @@ -1935,12 +1935,12 @@ class Sieve_Test_Address extends Sieve_Test { $header = trim($header); if (!empty($header)) { $headerstr .= empty($headerstr) ? '"' : ', "'; - $headerstr .= Ingo_Script_sieve::escapeString($header, $this->_vars['match-type'] == ':regex') . '"'; + $headerstr .= Ingo_Script_Sieve::escapeString($header, $this->_vars['match-type'] == ':regex') . '"'; } } $code .= $headerstr . "] "; } elseif (count($headers) == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString($headers[0], $this->_vars['match-type'] == ':regex') . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString($headers[0], $this->_vars['match-type'] == ':regex') . '" '; } else { return "No Headers Specified"; } @@ -1954,12 +1954,12 @@ class Sieve_Test_Address extends Sieve_Test { $addr = trim($addr); if (!empty($addr)) { $addressstr .= empty($addressstr) ? '"' : ', "'; - $addressstr .= Ingo_Script_sieve::escapeString($addr, $this->_vars['match-type'] == ':regex') . '"'; + $addressstr .= Ingo_Script_Sieve::escapeString($addr, $this->_vars['match-type'] == ':regex') . '"'; } } $code .= $addressstr . "] "; } elseif (count($addresses) == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString($addresses[0], $this->_vars['match-type'] == ':regex') . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString($addresses[0], $this->_vars['match-type'] == ':regex') . '" '; } else { return "No Addresses Specified"; } @@ -2053,7 +2053,7 @@ class Sieve_Test_Header extends Sieve_Test { $headerstr = ''; foreach ($headers as $header) { $headerstr .= empty($headerstr) ? '"' : ', "'; - $headerstr .= Ingo_Script_sieve::escapeString($header, $this->_vars['match-type'] == ':regex') . '"'; + $headerstr .= Ingo_Script_Sieve::escapeString($header, $this->_vars['match-type'] == ':regex') . '"'; } $code .= $headerstr . "] "; } elseif (count($headers) == 1) { @@ -2069,11 +2069,11 @@ class Sieve_Test_Header extends Sieve_Test { $stringlist = ''; foreach ($strings as $str) { $stringlist .= empty($stringlist) ? '"' : ', "'; - $stringlist .= Ingo_Script_sieve::escapeString($str, $this->_vars['match-type'] == ':regex') . '"'; + $stringlist .= Ingo_Script_Sieve::escapeString($str, $this->_vars['match-type'] == ':regex') . '"'; } $code .= $stringlist . "] "; } elseif (count($strings) == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString(reset($strings), $this->_vars['match-type'] == ':regex') . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString(reset($strings), $this->_vars['match-type'] == ':regex') . '" '; } else { return _("No strings specified"); } @@ -2153,11 +2153,11 @@ class Sieve_Test_Body extends Sieve_Test { $stringlist = ''; foreach ($strings as $str) { $stringlist .= empty($stringlist) ? '"' : ', "'; - $stringlist .= Ingo_Script_sieve::escapeString($str, $this->_vars['match-type'] == ':regex') . '"'; + $stringlist .= Ingo_Script_Sieve::escapeString($str, $this->_vars['match-type'] == ':regex') . '"'; } $code .= $stringlist . "] "; } elseif (count($strings) == 1) { - $code .= '"' . Ingo_Script_sieve::escapeString($strings[0], $this->_vars['match-type'] == ':regex') . '" '; + $code .= '"' . Ingo_Script_Sieve::escapeString($strings[0], $this->_vars['match-type'] == ':regex') . '" '; } else { return _("No strings specified"); } @@ -2325,7 +2325,7 @@ class Sieve_Action_Redirect extends Sieve_Action { function toCode($depth = 0) { return str_repeat(' ', $depth * 4) . 'redirect ' . - '"' . Ingo_Script_sieve::escapeString($this->_vars['address']) . '";'; + '"' . Ingo_Script_Sieve::escapeString($this->_vars['address']) . '";'; } /** @@ -2371,7 +2371,7 @@ class Sieve_Action_Reject extends Sieve_Action { */ function toCode() { - return 'reject "' . Ingo_Script_sieve::escapeString($this->_vars['reason']) . '";'; + return 'reject "' . Ingo_Script_Sieve::escapeString($this->_vars['reason']) . '";'; } /** @@ -2524,7 +2524,7 @@ class Sieve_Action_Fileinto extends Sieve_Action { */ function toCode() { - return 'fileinto "' . Ingo_Script_sieve::escapeString($this->_vars['folder']) . '";'; + return 'fileinto "' . Ingo_Script_Sieve::escapeString($this->_vars['folder']) . '";'; } /** @@ -2715,12 +2715,12 @@ class Sieve_Action_Vacation extends Sieve_Action { $address = trim($address); if (!empty($address)) { $stringlist .= empty($stringlist) ? '"' : ', "'; - $stringlist .= Ingo_Script_sieve::escapeString($address) . '"'; + $stringlist .= Ingo_Script_Sieve::escapeString($address) . '"'; } } $stringlist = "[" . $stringlist . "] "; } elseif (count($addresses) == 1) { - $stringlist = '"' . Ingo_Script_sieve::escapeString($addresses[0]) . '" '; + $stringlist = '"' . Ingo_Script_Sieve::escapeString($addresses[0]) . '" '; } if (!empty($stringlist)) { @@ -2728,11 +2728,10 @@ class Sieve_Action_Vacation extends Sieve_Action { } if (!empty($this->_vars['subject'])) { - include_once 'Horde/MIME.php'; - $code .= ':subject "' . MIME::encode(Ingo_Script_sieve::escapeString($this->_vars['subject']), 'UTF-8') . '" '; + $code .= ':subject "' . Horde_Mime::encode(Ingo_Script_Sieve::escapeString($this->_vars['subject']), 'UTF-8') . '" '; } return $code - . '"' . Ingo_Script_sieve::escapeString($this->_vars['reason']) + . '"' . Ingo_Script_Sieve::escapeString($this->_vars['reason']) . '";'; } @@ -2833,13 +2832,13 @@ class Sieve_Action_Flag extends Sieve_Action { $flag = trim($flag); if (!empty($flag)) { $stringlist .= empty($stringlist) ? '"' : ', "'; - $stringlist .= Ingo_Script_sieve::escapeString($flag) . '"'; + $stringlist .= Ingo_Script_Sieve::escapeString($flag) . '"'; } } $stringlist = '[' . $stringlist . ']'; $code .= $stringlist . ';'; } else { - $code .= '"' . Ingo_Script_sieve::escapeString($this->_vars['flags'][0]) . '";'; + $code .= '"' . Ingo_Script_Sieve::escapeString($this->_vars['flags'][0]) . '";'; } } return $code; @@ -2939,7 +2938,7 @@ class Sieve_Action_Notify extends Sieve_Action { function toCode() { return 'notify :method "mailto" :options "' . - Ingo_Script_sieve::escapeString($this->_vars['address']) . + Ingo_Script_Sieve::escapeString($this->_vars['address']) . '" :message "' . _("You have received a new message") . "\n" . _("From:") . " \$from\$ \n" . diff --git a/ingo/lib/Storage.php b/ingo/lib/Storage.php index c6274827f..8e908a454 100644 --- a/ingo/lib/Storage.php +++ b/ingo/lib/Storage.php @@ -89,7 +89,7 @@ class Ingo_Storage $params = Horde::getDriverConfig('storage', $driver); } - $class = 'Ingo_Storage_' . $driver; + $class = 'Ingo_Storage_' . ucfirst($driver); return class_exists($class) ? new $class($params) : false; @@ -100,7 +100,7 @@ class Ingo_Storage */ public function __destruct() { - $cache = &Horde_SessionObjects::singleton(); + $cache = Horde_SessionObjects::singleton(); /* Store the current objects. */ foreach ($this->_cache as $key => $val) { @@ -122,7 +122,7 @@ class Ingo_Storage * @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. + * @return Ingo_Storage_Rule|Ingo_Storage_Filters The specified object. */ public function retrieve($field, $cache = true, $readonly = false) { @@ -131,7 +131,7 @@ class Ingo_Storage if (!isset($this->_cache[$field])) { $this->_cache[$field] = array('mod' => false); if (isset($_SESSION['ingo']['storage'][$field])) { - $cacheSess = &Horde_SessionObjects::singleton(); + $cacheSess = Horde_SessionObjects::singleton(); $this->_cache[$field]['ob'] = $cacheSess->query($_SESSION['ingo']['storage'][$field]); } else { $this->_cache[$field]['ob'] = &$this->_retrieve($field, $readonly); @@ -154,7 +154,7 @@ class Ingo_Storage * 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. + * @return Ingo_Storage_Rule|Ingo_Storage_Filters The specified data. */ protected function _retrieve($field, $readonly = false) { @@ -164,7 +164,7 @@ class Ingo_Storage /** * Stores the specified data. * - * @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store. + * @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob The object to store. * @param boolean $cache Cache the object? * * @return boolean True on success. @@ -221,7 +221,7 @@ class Ingo_Storage * * @abstract * - * @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store. + * @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob The object to store. * * @return boolean True on success. */ @@ -365,666 +365,3 @@ class Ingo_Storage } } - -/** - * Ingo_Storage_rule:: is the base class for the various action objects - * used by Ingo_Storage. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_rule -{ - /** - * The object type. - * - * @var integer - */ - protected $_obtype; - - /** - * Whether the rule has been saved (if being saved separately). - * - * @var boolean - */ - protected $_saved = false; - - /** - * Returns the object rule type. - * - * @return integer The object rule type. - */ - public function obType() - { - return $this->_obtype; - } - - /** - * Marks the rule as saved or unsaved. - * - * @param boolean $data Whether the rule has been saved. - */ - public function setSaved($data) - { - $this->_saved = $data; - } - - /** - * Returns whether the rule has been saved. - * - * @return boolean True if the rule has been saved. - */ - public function isSaved() - { - return $this->_saved; - } - - /** - * Function to manage an internal address list. - * - * @param mixed $data The incoming data (array or string). - * @param boolean $sort Sort the list? - * - * @return array The address list. - */ - protected 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) { - $output = Horde_Array::prepareAddressList($output); - } - - return $output; - } - -} - -/** - * Ingo_Storage_blacklist is the object used to hold blacklist rule - * information. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_blacklist extends Ingo_Storage_rule -{ - protected $_addr = array(); - protected $_folder = ''; - protected $_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. - */ - public 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; - } - - public function setBlacklistFolder($data) - { - $this->_folder = $data; - } - - public function getBlacklist() - { - return empty($this->_addr) - ? array() - : array_filter($this->_addr, array('Ingo', 'filterEmptyAddress')); - } - - public function getBlacklistFolder() - { - return $this->_folder; - } - -} - -/** - * Ingo_Storage_whitelist is the object used to hold whitelist rule - * information. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_whitelist extends Ingo_Storage_rule -{ - protected $_addr = array(); - protected $_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. - */ - public 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; - } - - public function getWhitelist() - { - return empty($this->_addr) - ? array() - : array_filter($this->_addr, array('Ingo', 'filterEmptyAddress')); - } - -} - -/** - * Ingo_Storage_forward is the object used to hold mail forwarding rule - * information. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_forward extends Ingo_Storage_rule -{ - protected $_addr = array(); - protected $_keep = true; - protected $_obtype = Ingo_Storage::ACTION_FORWARD; - - public function setForwardAddresses($data, $sort = true) - { - $this->_addr = &$this->_addressList($data, $sort); - } - - public function setForwardKeep($data) - { - $this->_keep = $data; - } - - public function getForwardAddresses() - { - if (is_array($this->_addr)) { - foreach ($this->_addr as $key => $val) { - if (empty($val)) { - unset($this->_addr[$key]); - } - } - } - return $this->_addr; - } - - public function getForwardKeep() - { - return $this->_keep; - } - -} - -/** - * Ingo_Storage_vacation is the object used to hold vacation rule - * information. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_vacation extends Ingo_Storage_rule -{ - protected $_addr = array(); - protected $_days = 7; - protected $_excludes = array(); - protected $_ignorelist = true; - protected $_reason = ''; - protected $_subject = ''; - protected $_start; - protected $_end; - protected $_obtype = Ingo_Storage::ACTION_VACATION; - - public function setVacationAddresses($data, $sort = true) - { - $this->_addr = &$this->_addressList($data, $sort); - } - - public function setVacationDays($data) - { - $this->_days = $data; - } - - public function setVacationExcludes($data, $sort = true) - { - $this->_excludes = &$this->_addressList($data, $sort); - } - - public function setVacationIgnorelist($data) - { - $this->_ignorelist = $data; - } - - public function setVacationReason($data) - { - $this->_reason = $data; - } - - public function setVacationSubject($data) - { - $this->_subject = $data; - } - - public function setVacationStart($data) - { - $this->_start = $data; - } - - public function setVacationEnd($data) - { - $this->_end = $data; - } - - public function getVacationAddresses() - { - if (empty($GLOBALS['conf']['hooks']['vacation_addresses'])) { - return $this->_addr; - } - - try { - return Horde::callHook('_ingo_hook_vacation_addresses', array(Ingo::getUser()), 'ingo'); - } catch (Horde_Exception $e) { - return array(); - } - } - - public function getVacationDays() - { - return $this->_days; - } - - public function getVacationExcludes() - { - return $this->_excludes; - } - - public function getVacationIgnorelist() - { - return $this->_ignorelist; - } - - public function getVacationReason() - { - return $this->_reason; - } - - public function getVacationSubject() - { - return $this->_subject; - } - - public function getVacationStart() - { - return $this->_start; - } - - public function getVacationStartYear() - { - return date('Y', $this->_start); - } - - public function getVacationStartMonth() - { - return date('n', $this->_start); - } - - public function getVacationStartDay() - { - return date('j', $this->_start); - } - - public function getVacationEnd() - { - return $this->_end; - } - - public function getVacationEndYear() - { - return date('Y', $this->_end); - } - - public function getVacationEndMonth() - { - return date('n', $this->_end); - } - - public 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 - * @package Ingo - */ -class Ingo_Storage_spam extends Ingo_Storage_rule -{ - - /** - * The object type. - * - * @var integer - */ - protected $_obtype = Ingo_Storage::ACTION_SPAM; - - protected $_folder = null; - protected $_level = 5; - - public function setSpamFolder($folder) - { - $this->_folder = $folder; - } - - public function setSpamLevel($level) - { - $this->_level = $level; - } - - public function getSpamFolder() - { - return $this->_folder; - } - - public function getSpamLevel() - { - return $this->_level; - } - -} - -/** - * Ingo_Storage_filters is the object used to hold user-defined filtering rule - * information. - * - * @author Michael Slusarz - * @package Ingo - */ -class Ingo_Storage_filters -{ - /** - * The filter list. - * - * @var array - */ - protected $_filters = array(); - - /** - * The object type. - * - * @var integer - */ - protected $_obtype = Ingo_Storage::ACTION_FILTERS; - - /** - * Returns the object rule type. - * - * @return integer The object rule type. - */ - public function obType() - { - return $this->_obtype; - } - - /** - * Propagates the filter list with data. - * - * @param array $data A list of rule hashes. - */ - public function setFilterlist($data) - { - $this->_filters = $data; - } - - /** - * Returns the filter list. - * - * @return array The list of rule hashes. - */ - public function getFilterList() - { - return $this->_filters; - } - - /** - * Return the filter entry for a given ID. - * - * @return mixed The rule hash entry, or false if not defined. - */ - public function getFilter($id) - { - return isset($this->_filters[$id]) - ? $this->_filters[$id] - : false; - } - - /** - * Returns a single rule hash. - * - * @param integer $id A rule number. - * - * @return array The requested rule hash. - */ - public function getRule($id) - { - return $this->_filters[$id]; - } - - /** - * Returns a rule hash with default value used when creating new rules. - * - * @return array A rule hash. - */ - public 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 - * (ACTION_* constants). - * - * @return integer The number of the first matching rule or null. - */ - public 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 - * (ACTION_* constants). - * - * @return array The first matching rule hash or null. - */ - public 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. - */ - public 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 - */ - public 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. - */ - public 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. - */ - public 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. - */ - public 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. - */ - public 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. - */ - public function ruleDisable($id) - { - $this->_filters[$id]['disable'] = true; - } - - /** - * Enables a rule. - * - * @param integer $id Number of the rule to enable. - */ - public function ruleEnable($id) - { - $this->_filters[$id]['disable'] = false; - } - -} diff --git a/ingo/lib/Storage/Blacklist.php b/ingo/lib/Storage/Blacklist.php new file mode 100644 index 000000000..2733db5ef --- /dev/null +++ b/ingo/lib/Storage/Blacklist.php @@ -0,0 +1,57 @@ + + * @package Ingo + */ +class Ingo_Storage_Blacklist extends Ingo_Storage_Rule +{ + protected $_addr = array(); + protected $_folder = ''; + protected $_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. + */ + public 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; + } + + public function setBlacklistFolder($data) + { + $this->_folder = $data; + } + + public function getBlacklist() + { + return empty($this->_addr) + ? array() + : array_filter($this->_addr, array('Ingo', 'filterEmptyAddress')); + } + + public function getBlacklistFolder() + { + return $this->_folder; + } + +} diff --git a/ingo/lib/Storage/Filters.php b/ingo/lib/Storage/Filters.php new file mode 100644 index 000000000..4fdea7c83 --- /dev/null +++ b/ingo/lib/Storage/Filters.php @@ -0,0 +1,264 @@ + + * @package Ingo + */ +class Ingo_Storage_Filters +{ + /** + * The filter list. + * + * @var array + */ + protected $_filters = array(); + + /** + * The object type. + * + * @var integer + */ + protected $_obtype = Ingo_Storage::ACTION_FILTERS; + + /** + * Returns the object rule type. + * + * @return integer The object rule type. + */ + public function obType() + { + return $this->_obtype; + } + + /** + * Propagates the filter list with data. + * + * @param array $data A list of rule hashes. + */ + public function setFilterlist($data) + { + $this->_filters = $data; + } + + /** + * Returns the filter list. + * + * @return array The list of rule hashes. + */ + public function getFilterList() + { + return $this->_filters; + } + + /** + * Return the filter entry for a given ID. + * + * @return mixed The rule hash entry, or false if not defined. + */ + public function getFilter($id) + { + return isset($this->_filters[$id]) + ? $this->_filters[$id] + : false; + } + + /** + * Returns a single rule hash. + * + * @param integer $id A rule number. + * + * @return array The requested rule hash. + */ + public function getRule($id) + { + return $this->_filters[$id]; + } + + /** + * Returns a rule hash with default value used when creating new rules. + * + * @return array A rule hash. + */ + public 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 + * (ACTION_* constants). + * + * @return integer The number of the first matching rule or null. + */ + public 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 + * (ACTION_* constants). + * + * @return array The first matching rule hash or null. + */ + public 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. + */ + public 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 + */ + public 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. + */ + public 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. + */ + public 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. + */ + public 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. + */ + public 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. + */ + public function ruleDisable($id) + { + $this->_filters[$id]['disable'] = true; + } + + /** + * Enables a rule. + * + * @param integer $id Number of the rule to enable. + */ + public function ruleEnable($id) + { + $this->_filters[$id]['disable'] = false; + } + +} diff --git a/ingo/lib/Storage/Filters/Sql.php b/ingo/lib/Storage/Filters/Sql.php new file mode 100644 index 000000000..2202d72c1 --- /dev/null +++ b/ingo/lib/Storage/Filters/Sql.php @@ -0,0 +1,344 @@ + + * @package Ingo + */ +class Ingo_Storage_Filters_Sql extends Ingo_Storage_Filters { + + /** + * Handle for the current database connection. + * + * @var DB + */ + protected $_db; + + /** + * Handle for the current database connection, used for writing. + * + * Defaults to the same handle as $_db if a separate write database is not + * required. + * + * @var DB + */ + protected $_write_db; + + /** + * Driver specific parameters. + * + * @var array + */ + protected $_params; + + /** + * Constructor. + * + * @param DB $db Handle for the database connection. + * @param DB $write_db Handle for the database connection, used for + * writing. + * @param array $params Driver specific parameters. + */ + public function __construct($db, $write_db, $params) + { + $this->_db = $db; + $this->_write_db = $write_db; + $this->_params = $params; + } + + /** + * Loads all rules from the DB backend. + * + * @param boolean $readonly Whether to disable any write operations. + */ + public function init($readonly = false) + { + $query = sprintf('SELECT * FROM %s WHERE rule_owner = ? ORDER BY rule_order', + $this->_params['table_rules']); + $values = array(Ingo::getUser()); + Horde::logMessage('Ingo_Storage_Filters_Sql(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + return $result; + } + $data = array(); + while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { + $data[$row['rule_order']] = array( + 'id' => (int)$row['rule_id'], + 'name' => Horde_String::convertCharset($row['rule_name'], $this->_params['charset']), + 'action' => (int)$row['rule_action'], + 'action-value' => Horde_String::convertCharset($row['rule_value'], $this->_params['charset']), + 'flags' => (int)$row['rule_flags'], + 'conditions' => empty($row['rule_conditions']) ? null : Horde_String::convertCharset(unserialize($row['rule_conditions']), $this->_params['charset']), + 'combine' => (int)$row['rule_combine'], + 'stop' => (bool)$row['rule_stop'], + 'disable' => !(bool)$row['rule_active']); + } + $this->setFilterlist($data); + + if (empty($data) && !$readonly) { + $data = @unserialize($GLOBALS['prefs']->getDefault('rules')); + if ($data) { + foreach ($data as $val) { + $this->addRule($val, false); + } + } else { + $this->addRule( + array('name' => 'Whitelist', + 'action' => Ingo_Storage::ACTION_WHITELIST), + false); + $this->addRule( + array('name' => 'Vacation', + 'action' => Ingo_Storage::ACTION_VACATION, + 'disable' => true), + false); + $this->addRule( + array('name' => 'Blacklist', + 'action' => Ingo_Storage::ACTION_BLACKLIST), + false); + $this->addRule( + array('name' => 'Spam Filter', + 'action' => Ingo_Storage::ACTION_SPAM, + 'disable' => true), + false); + $this->addRule( + array('name' => 'Forward', + 'action' => Ingo_Storage::ACTION_FORWARD), + false); + } + } + } + + /** + * Converts a rule hash from Ingo's internal format to the database + * format. + * + * @param array $rule Rule hash in Ingo's format. + * + * @return array Rule hash in DB's format. + */ + protected function _ruleToBackend($rule) + { + return array(Horde_String::convertCharset($rule['name'], Horde_Nls::getCharset(), $this->_params['charset']), + (int)$rule['action'], + isset($rule['action-value']) ? Horde_String::convertCharset($rule['action-value'], Horde_Nls::getCharset(), $this->_params['charset']) : null, + isset($rule['flags']) ? (int)$rule['flags'] : null, + isset($rule['conditions']) ? serialize(Horde_String::convertCharset($rule['conditions'], Horde_Nls::getCharset(), $this->_params['charset'])) : null, + isset($rule['combine']) ? (int)$rule['combine'] : null, + isset($rule['stop']) ? (int)$rule['stop'] : null, + isset($rule['disable']) ? (int)(!$rule['disable']) : 1); + } + + /** + * 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. + */ + public function addRule($rule, $default = true) + { + if ($default) { + $rule = array_merge($this->getDefaultRule(), $rule); + } + + $query = sprintf('INSERT INTO %s (rule_id, rule_owner, rule_name, rule_action, rule_value, rule_flags, rule_conditions, rule_combine, rule_stop, rule_active, rule_order) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + $this->_params['table_rules']); + $id = $this->_write_db->nextId($this->_params['table_rules']); + if (is_a($id, 'PEAR_Error')) { + return $id; + } + $order = key(array_reverse($this->_filters, true)) + 1; + $values = array_merge(array($id, Ingo::getUser()), + $this->_ruleToBackend($rule), + array($order)); + Horde::logMessage('Ingo_Storage_Filters_Sql::addRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + + $rule['id'] = $id; + $this->_filters[$order] = $rule; + } + + /** + * Updates an existing rule with a rule hash. + * + * @param array $rule A rule hash + * @param integer $id A rule number + */ + public function updateRule($rule, $id) + { + $query = sprintf('UPDATE %s SET rule_name = ?, rule_action = ?, rule_value = ?, rule_flags = ?, rule_conditions = ?, rule_combine = ?, rule_stop = ?, rule_active = ?, rule_order = ? WHERE rule_id = ? AND rule_owner = ?', + $this->_params['table_rules']); + $values = array_merge($this->_ruleToBackend($rule), + array($id, $rule['id'], Ingo::getUser())); + Horde::logMessage('Ingo_Storage_Filters_Sql::updateRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + + $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. + */ + public function deleteRule($id) + { + if (!isset($this->_filters[$id])) { + return false; + } + + $query = sprintf('DELETE FROM %s WHERE rule_id = ? AND rule_owner = ?', + $this->_params['table_rules']); + $values = array($this->_filters[$id]['id'], Ingo::getUser()); + Horde::logMessage('Ingo_Storage_Filters_Sql::deleteRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + unset($this->_filters[$id]); + + $query = sprintf('UPDATE %s SET rule_order = rule_order - 1 WHERE rule_owner = ? AND rule_order > ?', + $this->_params['table_rules']); + $values = array(Ingo::getUser(), $id); + Horde::logMessage('Ingo_Storage_Filters_Sql::deleteRule(): ' . $query, + __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + + return true; + } + + /** + * 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. + */ + public function copyRule($id) + { + if (isset($this->_filters[$id])) { + $newrule = $this->_filters[$id]; + $newrule['name'] = sprintf(_("Copy of %s"), $this->_filters[$id]['name']); + $this->addRule($newrule, false); + $this->ruleUp(count($this->_filters) - 1, count($this->_filters) - $id - 2); + 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. + */ + public function ruleUp($id, $steps = 1) + { + return $this->_ruleMove($id, -$steps); + } + + /** + * 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. + */ + public function ruleDown($id, $steps = 1) + { + return $this->_ruleMove($id, $steps); + } + + /** + * Moves a rule in the filters list. + * + * @param integer $id Number of the rule to move. + * @param integer $steps Number of positions and direction to move the + * rule. + */ + protected function _ruleMove($id, $steps) + { + $query = sprintf('UPDATE %s SET rule_order = rule_order %s 1 WHERE rule_owner = ? AND rule_order %s ? AND rule_order %s ?', + $this->_params['table_rules'], + $steps > 0 ? '-' : '+', + $steps > 0 ? '>' : '>=', + $steps > 0 ? '<=' : '<'); + $values = array(Ingo::getUser()); + if ($steps < 0) { + $values[] = (int)($id + $steps); + $values[] = (int)$id; + } else { + $values[] = (int)$id; + $values[] = (int)($id + $steps); + } + Horde::logMessage('Ingo_Storage_Filters_Sql::ruleUp(): ' . $query, + __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + $query = sprintf('UPDATE %s SET rule_order = ? WHERE rule_owner = ? AND rule_id = ?', + $this->_params['table_rules']); + $values = array((int)($id + $steps), + Ingo::getUser(), + $this->_filters[$id]['id']); + Horde::logMessage('Ingo_Storage_Filters_Sql::ruleUp(): ' . $query, + __FILE__, __LINE__, PEAR_LOG_DEBUG); + $result = $this->_write_db->query($query, $values); + if (is_a($result, 'PEAR_Error')) { + Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); + return $result; + } + + $this->init(); + } + + /** + * Disables a rule. + * + * @param integer $id Number of the rule to disable. + */ + public function ruleDisable($id) + { + $rule = $this->_filters[$id]; + $rule['disable'] = true; + $this->updateRule($rule, $id); + } + + /** + * Enables a rule. + * + * @param integer $id Number of the rule to enable. + */ + public function ruleEnable($id) + { + $rule = $this->_filters[$id]; + $rule['disable'] = false; + $this->updateRule($rule, $id); + } + +} diff --git a/ingo/lib/Storage/Forward.php b/ingo/lib/Storage/Forward.php new file mode 100644 index 000000000..051dd1f0b --- /dev/null +++ b/ingo/lib/Storage/Forward.php @@ -0,0 +1,45 @@ + + * @package Ingo + */ +class Ingo_Storage_Forward extends Ingo_Storage_Rule +{ + protected $_addr = array(); + protected $_keep = true; + protected $_obtype = Ingo_Storage::ACTION_FORWARD; + + public function setForwardAddresses($data, $sort = true) + { + $this->_addr = &$this->_addressList($data, $sort); + } + + public function setForwardKeep($data) + { + $this->_keep = $data; + } + + public function getForwardAddresses() + { + if (is_array($this->_addr)) { + foreach ($this->_addr as $key => $val) { + if (empty($val)) { + unset($this->_addr[$key]); + } + } + } + return $this->_addr; + } + + public function getForwardKeep() + { + return $this->_keep; + } + +} diff --git a/ingo/lib/Storage/mock.php b/ingo/lib/Storage/Mock.php similarity index 71% rename from ingo/lib/Storage/mock.php rename to ingo/lib/Storage/Mock.php index aeafa9986..38825f484 100644 --- a/ingo/lib/Storage/mock.php +++ b/ingo/lib/Storage/Mock.php @@ -1,6 +1,6 @@ _data[$field])) { switch ($field) { case self::ACTION_BLACKLIST: - return new Ingo_Storage_blacklist(); + return new Ingo_Storage_Blacklist(); case self::ACTION_FILTERS: - $ob = new Ingo_Storage_filters(); + $ob = new Ingo_Storage_Filters(); include INGO_BASE . '/config/prefs.php.dist'; $ob->setFilterList(unserialize($_prefs['rules']['value'])); return $ob; case self::ACTION_FORWARD: - return new Ingo_Storage_forward(); + return new Ingo_Storage_Forward(); case self::ACTION_VACATION: - return new Ingo_Storage_vacation(); + return new Ingo_Storage_Vacation(); case self::ACTION_WHITELIST: - return new Ingo_Storage_whitelist(); + return new Ingo_Storage_Whitelist(); case self::ACTION_SPAM: - return new Ingo_Storage_spam(); + return new Ingo_Storage_Spam(); default: return false; diff --git a/ingo/lib/Storage/prefs.php b/ingo/lib/Storage/Prefs.php similarity index 92% rename from ingo/lib/Storage/prefs.php rename to ingo/lib/Storage/Prefs.php index 3c61bce8b..be3547edf 100644 --- a/ingo/lib/Storage/prefs.php +++ b/ingo/lib/Storage/Prefs.php @@ -1,6 +1,6 @@ * @package Ingo */ -class Ingo_Storage_prefs extends Ingo_Storage +class Ingo_Storage_Prefs extends Ingo_Storage { /** * Constructor. @@ -29,7 +29,7 @@ class Ingo_Storage_prefs extends Ingo_Storage * 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. + * @return Ingo_Storage_Rule|Ingo_Storage_Filters The specified data. */ protected function _retrieve($field, $readonly = false) { @@ -40,7 +40,7 @@ class Ingo_Storage_prefs extends Ingo_Storage switch ($field) { case self::ACTION_BLACKLIST: - $ob = new Ingo_Storage_blacklist(); + $ob = new Ingo_Storage_Blacklist(); $data = @unserialize($prefs->getValue('blacklist')); if ($data) { $ob->setBlacklist($data['a'], false); @@ -49,7 +49,7 @@ class Ingo_Storage_prefs extends Ingo_Storage break; case self::ACTION_WHITELIST: - $ob = new Ingo_Storage_whitelist(); + $ob = new Ingo_Storage_Whitelist(); $data = @unserialize($prefs->getValue('whitelist')); if ($data) { $ob->setWhitelist($data, false); @@ -57,7 +57,7 @@ class Ingo_Storage_prefs extends Ingo_Storage break; case self::ACTION_FILTERS: - $ob = new Ingo_Storage_filters(); + $ob = new Ingo_Storage_Filters(); $data = @unserialize($prefs->getValue('rules', false)); if ($data === false) { /* Convert rules from the old format. */ @@ -71,7 +71,7 @@ class Ingo_Storage_prefs extends Ingo_Storage break; case self::ACTION_FORWARD: - $ob = new Ingo_Storage_forward(); + $ob = new Ingo_Storage_Forward(); $data = @unserialize($prefs->getValue('forward')); if ($data) { $ob->setForwardAddresses($data['a'], false); @@ -80,7 +80,7 @@ class Ingo_Storage_prefs extends Ingo_Storage break; case self::ACTION_VACATION: - $ob = new Ingo_Storage_vacation(); + $ob = new Ingo_Storage_Vacation(); $data = @unserialize($prefs->getValue('vacation', false)); if ($data === false) { /* Convert vacation from the old format. */ @@ -105,7 +105,7 @@ class Ingo_Storage_prefs extends Ingo_Storage break; case self::ACTION_SPAM: - $ob = new Ingo_Storage_spam(); + $ob = new Ingo_Storage_Spam(); $data = @unserialize($prefs->getValue('spam')); if ($data) { $ob->setSpamFolder($data['folder']); @@ -124,7 +124,7 @@ class Ingo_Storage_prefs extends Ingo_Storage /** * Stores the specified data in the storage backend. * - * @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store. + * @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob The object to store. * * @return boolean True on success. */ diff --git a/ingo/lib/Storage/Rule.php b/ingo/lib/Storage/Rule.php new file mode 100644 index 000000000..9dfb0e7f2 --- /dev/null +++ b/ingo/lib/Storage/Rule.php @@ -0,0 +1,84 @@ + + * @package Ingo + */ +class Ingo_Storage_Rule +{ + /** + * The object type. + * + * @var integer + */ + protected $_obtype; + + /** + * Whether the rule has been saved (if being saved separately). + * + * @var boolean + */ + protected $_saved = false; + + /** + * Returns the object rule type. + * + * @return integer The object rule type. + */ + public function obType() + { + return $this->_obtype; + } + + /** + * Marks the rule as saved or unsaved. + * + * @param boolean $data Whether the rule has been saved. + */ + public function setSaved($data) + { + $this->_saved = $data; + } + + /** + * Returns whether the rule has been saved. + * + * @return boolean True if the rule has been saved. + */ + public function isSaved() + { + return $this->_saved; + } + + /** + * Function to manage an internal address list. + * + * @param mixed $data The incoming data (array or string). + * @param boolean $sort Sort the list? + * + * @return array The address list. + */ + protected 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) { + $output = Horde_Array::prepareAddressList($output); + } + + return $output; + } + +} diff --git a/ingo/lib/Storage/Spam.php b/ingo/lib/Storage/Spam.php new file mode 100644 index 000000000..9f3f2da2b --- /dev/null +++ b/ingo/lib/Storage/Spam.php @@ -0,0 +1,44 @@ + + * @package Ingo + */ +class Ingo_Storage_Spam extends Ingo_Storage_Rule +{ + /** + * The object type. + * + * @var integer + */ + protected $_obtype = Ingo_Storage::ACTION_SPAM; + + protected $_folder = null; + protected $_level = 5; + + public function setSpamFolder($folder) + { + $this->_folder = $folder; + } + + public function setSpamLevel($level) + { + $this->_level = $level; + } + + public function getSpamFolder() + { + return $this->_folder; + } + + public function getSpamLevel() + { + return $this->_level; + } + +} diff --git a/ingo/lib/Storage/sql.php b/ingo/lib/Storage/Sql.php similarity index 53% rename from ingo/lib/Storage/sql.php rename to ingo/lib/Storage/Sql.php index ceb515713..6b3c45bdf 100644 --- a/ingo/lib/Storage/sql.php +++ b/ingo/lib/Storage/Sql.php @@ -1,6 +1,6 @@ @@ -26,7 +26,7 @@ * @author Jan Schneider * @package Ingo */ -class Ingo_Storage_sql extends Ingo_Storage +class Ingo_Storage_Sql extends Ingo_Storage { /** * Handle for the current database connection. @@ -126,7 +126,7 @@ class Ingo_Storage_sql extends Ingo_Storage * 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. + * @return Ingo_Storage_Rule|Ingo_Storage_Filters The specified data. */ protected function _retrieve($field, $readonly = false) { @@ -134,7 +134,7 @@ class Ingo_Storage_sql extends Ingo_Storage case self::ACTION_BLACKLIST: case self::ACTION_WHITELIST: if ($field == self::ACTION_BLACKLIST) { - $ob = new Ingo_Storage_blacklist(); + $ob = new Ingo_Storage_Blacklist(); $filters = &$this->retrieve(self::ACTION_FILTERS); if (is_a($filters, 'PEAR_Error')) { return $filters; @@ -144,13 +144,13 @@ class Ingo_Storage_sql extends Ingo_Storage $ob->setBlacklistFolder($rule['action-value']); } } else { - $ob = new Ingo_Storage_whitelist(); + $ob = new Ingo_Storage_Whitelist(); } $query = sprintf('SELECT list_address FROM %s WHERE list_owner = ? AND list_blacklist = ?', $this->_params['table_lists']); $values = array(Ingo::getUser(), (int)($field == self::ACTION_BLACKLIST)); - Horde::logMessage('Ingo_Storage_sql::_retrieve(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_retrieve(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $addresses = $this->_db->getCol($query, 0, $values); if (is_a($addresses, 'PEAR_Error')) { @@ -165,7 +165,7 @@ class Ingo_Storage_sql extends Ingo_Storage break; case self::ACTION_FILTERS: - $ob = new Ingo_Storage_filters_sql($this->_db, $this->_write_db, $this->_params); + $ob = new Ingo_Storage_Filters_Sql($this->_db, $this->_write_db, $this->_params); if (is_a($result = $ob->init($readonly), 'PEAR_Error')) { return $result; } @@ -174,12 +174,12 @@ class Ingo_Storage_sql extends Ingo_Storage case self::ACTION_FORWARD: $query = sprintf('SELECT * FROM %s WHERE forward_owner = ?', $this->_params['table_forwards']); - Horde::logMessage('Ingo_Storage_sql::_retrieve(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_retrieve(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->query($query, Ingo::getUser()); $data = $result->fetchRow(DB_FETCHMODE_ASSOC); - $ob = new Ingo_Storage_forward(); + $ob = new Ingo_Storage_Forward(); if ($data && !is_a($data, 'PEAR_Error')) { $ob->setForwardAddresses(explode("\n", $data['forward_addresses']), false); $ob->setForwardKeep((bool)$data['forward_keep']); @@ -193,12 +193,12 @@ class Ingo_Storage_sql extends Ingo_Storage case self::ACTION_VACATION: $query = sprintf('SELECT * FROM %s WHERE vacation_owner = ?', $this->_params['table_vacations']); - Horde::logMessage('Ingo_Storage_sql::_retrieve(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_retrieve(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->query($query, Ingo::getUser()); $data = $result->fetchRow(DB_FETCHMODE_ASSOC); - $ob = new Ingo_Storage_vacation(); + $ob = new Ingo_Storage_Vacation(); if ($data && !is_a($data, 'PEAR_Error')) { $ob->setVacationAddresses(explode("\n", $data['vacation_addresses']), false); $ob->setVacationDays((int)$data['vacation_days']); @@ -228,12 +228,12 @@ class Ingo_Storage_sql extends Ingo_Storage case self::ACTION_SPAM: $query = sprintf('SELECT * FROM %s WHERE spam_owner = ?', $this->_params['table_spam']); - Horde::logMessage('Ingo_Storage_sql::_retrieve(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_retrieve(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_db->query($query, Ingo::getUser()); $data = $result->fetchRow(DB_FETCHMODE_ASSOC); - $ob = new Ingo_Storage_spam(); + $ob = new Ingo_Storage_Spam(); if ($data && !is_a($data, 'PEAR_Error')) { $ob->setSpamFolder($data['spam_folder']); $ob->setSpamLevel((int)$data['spam_level']); @@ -256,7 +256,7 @@ class Ingo_Storage_sql extends Ingo_Storage * * @access private * - * @param Ingo_Storage_rule|Ingo_Storage_filters $ob The object to store. + * @param Ingo_Storage_Rule|Ingo_Storage_Filters $ob The object to store. * * @return boolean True on success. */ @@ -284,7 +284,7 @@ class Ingo_Storage_sql extends Ingo_Storage $query = sprintf('DELETE FROM %s WHERE list_owner = ? AND list_blacklist = ?', $this->_params['table_lists']); $values = array(Ingo::getUser(), $is_blacklist); - Horde::logMessage('Ingo_Storage_sql::_store(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_store(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $result = $this->_write_db->query($query, $values); if (is_a($result, 'PEAR_Error')) { @@ -293,7 +293,7 @@ class Ingo_Storage_sql extends Ingo_Storage } $query = sprintf('INSERT INTO %s (list_owner, list_blacklist, list_address) VALUES (?, ?, ?)', $this->_params['table_lists']); - Horde::logMessage('Ingo_Storage_sql::_store(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_store(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $addresses = $is_blacklist ? $ob->getBlacklist() : $ob->getWhitelist(); foreach ($addresses as $address) { @@ -325,7 +325,7 @@ class Ingo_Storage_sql extends Ingo_Storage implode("\n", $ob->getForwardAddresses()), (int)(bool)$ob->getForwardKeep(), Ingo::getUser()); - Horde::logMessage('Ingo_Storage_sql::_store(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_store(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $ret = $this->_write_db->query($query, $values); if (!is_a($ret, 'PEAR_Error')) { @@ -354,7 +354,7 @@ class Ingo_Storage_sql extends Ingo_Storage implode("\n", $ob->getVacationExcludes()), (int)(bool)$ob->getVacationIgnorelist(), Ingo::getUser()); - Horde::logMessage('Ingo_Storage_sql::_store(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_store(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $ret = $this->_write_db->query($query, $values); if (!is_a($ret, 'PEAR_Error')) { @@ -373,7 +373,7 @@ class Ingo_Storage_sql extends Ingo_Storage (int)$ob->getSpamLevel(), $ob->getSpamFolder(), Ingo::getUser()); - Horde::logMessage('Ingo_Storage_sql::_store(): ' . $query, + Horde::logMessage('Ingo_Storage_Sql::_store(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); $ret = $this->_write_db->query($query, $values); if (!is_a($ret, 'PEAR_Error')) { @@ -394,344 +394,3 @@ class Ingo_Storage_sql extends Ingo_Storage } } - -/** - * Ingo_Storage_filters_sql is the object used to hold user-defined filtering - * rule information. - * - * @author Jan Schneider - * @package Ingo - */ -class Ingo_Storage_filters_sql extends Ingo_Storage_filters { - - /** - * Handle for the current database connection. - * - * @var DB - */ - protected $_db; - - /** - * Handle for the current database connection, used for writing. - * - * Defaults to the same handle as $_db if a separate write database is not - * required. - * - * @var DB - */ - protected $_write_db; - - /** - * Driver specific parameters. - * - * @var array - */ - protected $_params; - - /** - * Constructor. - * - * @param DB $db Handle for the database connection. - * @param DB $write_db Handle for the database connection, used for - * writing. - * @param array $params Driver specific parameters. - */ - public function __construct($db, $write_db, $params) - { - $this->_db = $db; - $this->_write_db = $write_db; - $this->_params = $params; - } - - /** - * Loads all rules from the DB backend. - * - * @param boolean $readonly Whether to disable any write operations. - */ - public function init($readonly = false) - { - $query = sprintf('SELECT * FROM %s WHERE rule_owner = ? ORDER BY rule_order', - $this->_params['table_rules']); - $values = array(Ingo::getUser()); - Horde::logMessage('Ingo_Storage_filters_sql(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - return $result; - } - $data = array(); - while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) { - $data[$row['rule_order']] = array( - 'id' => (int)$row['rule_id'], - 'name' => Horde_String::convertCharset($row['rule_name'], $this->_params['charset']), - 'action' => (int)$row['rule_action'], - 'action-value' => Horde_String::convertCharset($row['rule_value'], $this->_params['charset']), - 'flags' => (int)$row['rule_flags'], - 'conditions' => empty($row['rule_conditions']) ? null : Horde_String::convertCharset(unserialize($row['rule_conditions']), $this->_params['charset']), - 'combine' => (int)$row['rule_combine'], - 'stop' => (bool)$row['rule_stop'], - 'disable' => !(bool)$row['rule_active']); - } - $this->setFilterlist($data); - - if (empty($data) && !$readonly) { - $data = @unserialize($GLOBALS['prefs']->getDefault('rules')); - if ($data) { - foreach ($data as $val) { - $this->addRule($val, false); - } - } else { - $this->addRule( - array('name' => 'Whitelist', - 'action' => Ingo_Storage::ACTION_WHITELIST), - false); - $this->addRule( - array('name' => 'Vacation', - 'action' => Ingo_Storage::ACTION_VACATION, - 'disable' => true), - false); - $this->addRule( - array('name' => 'Blacklist', - 'action' => Ingo_Storage::ACTION_BLACKLIST), - false); - $this->addRule( - array('name' => 'Spam Filter', - 'action' => Ingo_Storage::ACTION_SPAM, - 'disable' => true), - false); - $this->addRule( - array('name' => 'Forward', - 'action' => Ingo_Storage::ACTION_FORWARD), - false); - } - } - } - - /** - * Converts a rule hash from Ingo's internal format to the database - * format. - * - * @param array $rule Rule hash in Ingo's format. - * - * @return array Rule hash in DB's format. - */ - protected function _ruleToBackend($rule) - { - return array(Horde_String::convertCharset($rule['name'], Horde_Nls::getCharset(), $this->_params['charset']), - (int)$rule['action'], - isset($rule['action-value']) ? Horde_String::convertCharset($rule['action-value'], Horde_Nls::getCharset(), $this->_params['charset']) : null, - isset($rule['flags']) ? (int)$rule['flags'] : null, - isset($rule['conditions']) ? serialize(Horde_String::convertCharset($rule['conditions'], Horde_Nls::getCharset(), $this->_params['charset'])) : null, - isset($rule['combine']) ? (int)$rule['combine'] : null, - isset($rule['stop']) ? (int)$rule['stop'] : null, - isset($rule['disable']) ? (int)(!$rule['disable']) : 1); - } - - /** - * 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. - */ - public function addRule($rule, $default = true) - { - if ($default) { - $rule = array_merge($this->getDefaultRule(), $rule); - } - - $query = sprintf('INSERT INTO %s (rule_id, rule_owner, rule_name, rule_action, rule_value, rule_flags, rule_conditions, rule_combine, rule_stop, rule_active, rule_order) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', - $this->_params['table_rules']); - $id = $this->_write_db->nextId($this->_params['table_rules']); - if (is_a($id, 'PEAR_Error')) { - return $id; - } - $order = key(array_reverse($this->_filters, true)) + 1; - $values = array_merge(array($id, Ingo::getUser()), - $this->_ruleToBackend($rule), - array($order)); - Horde::logMessage('Ingo_Storage_filters_sql::addRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - - $rule['id'] = $id; - $this->_filters[$order] = $rule; - } - - /** - * Updates an existing rule with a rule hash. - * - * @param array $rule A rule hash - * @param integer $id A rule number - */ - public function updateRule($rule, $id) - { - $query = sprintf('UPDATE %s SET rule_name = ?, rule_action = ?, rule_value = ?, rule_flags = ?, rule_conditions = ?, rule_combine = ?, rule_stop = ?, rule_active = ?, rule_order = ? WHERE rule_id = ? AND rule_owner = ?', - $this->_params['table_rules']); - $values = array_merge($this->_ruleToBackend($rule), - array($id, $rule['id'], Ingo::getUser())); - Horde::logMessage('Ingo_Storage_filters_sql::updateRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - - $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. - */ - public function deleteRule($id) - { - if (!isset($this->_filters[$id])) { - return false; - } - - $query = sprintf('DELETE FROM %s WHERE rule_id = ? AND rule_owner = ?', - $this->_params['table_rules']); - $values = array($this->_filters[$id]['id'], Ingo::getUser()); - Horde::logMessage('Ingo_Storage_filters_sql::deleteRule(): ' . $query, __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - unset($this->_filters[$id]); - - $query = sprintf('UPDATE %s SET rule_order = rule_order - 1 WHERE rule_owner = ? AND rule_order > ?', - $this->_params['table_rules']); - $values = array(Ingo::getUser(), $id); - Horde::logMessage('Ingo_Storage_filters_sql::deleteRule(): ' . $query, - __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - - return true; - } - - /** - * 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. - */ - public function copyRule($id) - { - if (isset($this->_filters[$id])) { - $newrule = $this->_filters[$id]; - $newrule['name'] = sprintf(_("Copy of %s"), $this->_filters[$id]['name']); - $this->addRule($newrule, false); - $this->ruleUp(count($this->_filters) - 1, count($this->_filters) - $id - 2); - 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. - */ - public function ruleUp($id, $steps = 1) - { - return $this->_ruleMove($id, -$steps); - } - - /** - * 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. - */ - public function ruleDown($id, $steps = 1) - { - return $this->_ruleMove($id, $steps); - } - - /** - * Moves a rule in the filters list. - * - * @param integer $id Number of the rule to move. - * @param integer $steps Number of positions and direction to move the - * rule. - */ - protected function _ruleMove($id, $steps) - { - $query = sprintf('UPDATE %s SET rule_order = rule_order %s 1 WHERE rule_owner = ? AND rule_order %s ? AND rule_order %s ?', - $this->_params['table_rules'], - $steps > 0 ? '-' : '+', - $steps > 0 ? '>' : '>=', - $steps > 0 ? '<=' : '<'); - $values = array(Ingo::getUser()); - if ($steps < 0) { - $values[] = (int)($id + $steps); - $values[] = (int)$id; - } else { - $values[] = (int)$id; - $values[] = (int)($id + $steps); - } - Horde::logMessage('Ingo_Storage_filters_sql::ruleUp(): ' . $query, - __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - $query = sprintf('UPDATE %s SET rule_order = ? WHERE rule_owner = ? AND rule_id = ?', - $this->_params['table_rules']); - $values = array((int)($id + $steps), - Ingo::getUser(), - $this->_filters[$id]['id']); - Horde::logMessage('Ingo_Storage_filters_sql::ruleUp(): ' . $query, - __FILE__, __LINE__, PEAR_LOG_DEBUG); - $result = $this->_write_db->query($query, $values); - if (is_a($result, 'PEAR_Error')) { - Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR); - return $result; - } - - $this->init(); - } - - /** - * Disables a rule. - * - * @param integer $id Number of the rule to disable. - */ - public function ruleDisable($id) - { - $rule = $this->_filters[$id]; - $rule['disable'] = true; - $this->updateRule($rule, $id); - } - - /** - * Enables a rule. - * - * @param integer $id Number of the rule to enable. - */ - public function ruleEnable($id) - { - $rule = $this->_filters[$id]; - $rule['disable'] = false; - $this->updateRule($rule, $id); - } - -} diff --git a/ingo/lib/Storage/Vacation.php b/ingo/lib/Storage/Vacation.php new file mode 100644 index 000000000..1b22b39cf --- /dev/null +++ b/ingo/lib/Storage/Vacation.php @@ -0,0 +1,142 @@ + + * @package Ingo + */ +class Ingo_Storage_Vacation extends Ingo_Storage_Rule +{ + protected $_addr = array(); + protected $_days = 7; + protected $_excludes = array(); + protected $_ignorelist = true; + protected $_reason = ''; + protected $_subject = ''; + protected $_start; + protected $_end; + protected $_obtype = Ingo_Storage::ACTION_VACATION; + + public function setVacationAddresses($data, $sort = true) + { + $this->_addr = &$this->_addressList($data, $sort); + } + + public function setVacationDays($data) + { + $this->_days = $data; + } + + public function setVacationExcludes($data, $sort = true) + { + $this->_excludes = &$this->_addressList($data, $sort); + } + + public function setVacationIgnorelist($data) + { + $this->_ignorelist = $data; + } + + public function setVacationReason($data) + { + $this->_reason = $data; + } + + public function setVacationSubject($data) + { + $this->_subject = $data; + } + + public function setVacationStart($data) + { + $this->_start = $data; + } + + public function setVacationEnd($data) + { + $this->_end = $data; + } + + public function getVacationAddresses() + { + if (empty($GLOBALS['conf']['hooks']['vacation_addresses'])) { + return $this->_addr; + } + + try { + return Horde::callHook('_ingo_hook_vacation_addresses', array(Ingo::getUser()), 'ingo'); + } catch (Horde_Exception $e) { + return array(); + } + } + + public function getVacationDays() + { + return $this->_days; + } + + public function getVacationExcludes() + { + return $this->_excludes; + } + + public function getVacationIgnorelist() + { + return $this->_ignorelist; + } + + public function getVacationReason() + { + return $this->_reason; + } + + public function getVacationSubject() + { + return $this->_subject; + } + + public function getVacationStart() + { + return $this->_start; + } + + public function getVacationStartYear() + { + return date('Y', $this->_start); + } + + public function getVacationStartMonth() + { + return date('n', $this->_start); + } + + public function getVacationStartDay() + { + return date('j', $this->_start); + } + + public function getVacationEnd() + { + return $this->_end; + } + + public function getVacationEndYear() + { + return date('Y', $this->_end); + } + + public function getVacationEndMonth() + { + return date('n', $this->_end); + } + + public function getVacationEndDay() + { + return date('j', $this->_end); + } + +} diff --git a/ingo/lib/Storage/Whitelist.php b/ingo/lib/Storage/Whitelist.php new file mode 100644 index 000000000..19f5295a6 --- /dev/null +++ b/ingo/lib/Storage/Whitelist.php @@ -0,0 +1,47 @@ + + * @package Ingo + */ +class Ingo_Storage_Whitelist extends Ingo_Storage_Rule +{ + protected $_addr = array(); + protected $_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. + */ + public 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; + } + + public function getWhitelist() + { + return empty($this->_addr) + ? array() + : array_filter($this->_addr, array('Ingo', 'filterEmptyAddress')); + } + +} -- 2.11.0