| 1 | <?php |
| 2 | /******************************************************************************** |
| 3 | * include/philter.inc.php : Philter base class |
| 4 | * ----------------------- |
| 5 | * |
| 6 | * This file is part of the philter distribution |
| 7 | * Copyright: See COPYING files that comes with this distribution |
| 8 | ********************************************************************************/ |
| 9 | |
| 10 | require_once("include/core.inc.php"); |
| 11 | |
| 12 | function i18n($_index) { global $philter; echo $philter->i18n($_index); } |
| 13 | function _i18n($_index) { global $philter; return $philter->i18n($_index); } |
| 14 | |
| 15 | /** Philter base configuration class |
| 16 | * This is the base class of Philter. |
| 17 | */ |
| 18 | class Philter { |
| 19 | /** philter configuration. |
| 20 | * @see config.inc.php |
| 21 | */ |
| 22 | var $config; |
| 23 | /** error string */ |
| 24 | var $err; |
| 25 | |
| 26 | /** constructor */ |
| 27 | function Philter() { |
| 28 | $this->config = array( |
| 29 | 'db' => array('host', 'name', 'user', 'pwd', 'link'), |
| 30 | 'path' => array('procmail', 'spool'), |
| 31 | 'match_plugins' => array(), |
| 32 | 'action_plugins' => array(), |
| 33 | 'global_plugins' => array(), |
| 34 | 'i18n' => array() |
| 35 | ); |
| 36 | $this->err = ""; |
| 37 | } |
| 38 | |
| 39 | function set_advanced() { $_SESSION['philter_adv'] = true; } |
| 40 | function del_advanced() { unset($_SESSION['philter_adv']); } |
| 41 | function is_advanced() { return isset($_SESSION['philter_adv']); } |
| 42 | |
| 43 | /** returns the i18n string |
| 44 | * @param $_index the index of the string |
| 45 | * @returns the string or false |
| 46 | */ |
| 47 | function i18n($_index) { |
| 48 | if(isset($this->config['i18n'][$_index])) |
| 49 | return $this->config['i18n'][$_index]; |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | /** returns the error. |
| 54 | * @return the error string |
| 55 | */ |
| 56 | function error() { |
| 57 | return $this->err; |
| 58 | } |
| 59 | |
| 60 | /** sets the error string. |
| 61 | * @return nothing |
| 62 | */ |
| 63 | function set_error($_err) { |
| 64 | $this->err = $_err; |
| 65 | } |
| 66 | |
| 67 | /** returns the database persistent connection link. |
| 68 | * it's a shortname for $this->config['db']['link'] |
| 69 | * @returns a mysql resource |
| 70 | */ |
| 71 | function link() { return $this->config['db']['link']; } |
| 72 | |
| 73 | /** init the link to the database */ |
| 74 | function pconnect() { |
| 75 | $this->config['db']['link'] = |
| 76 | mysql_pconnect( |
| 77 | $this->config['db']['host'], |
| 78 | $this->config['db']['user'], |
| 79 | $this->config['db']['pass'] |
| 80 | ); |
| 81 | mysql_select_db($this->config['db']['name'], $this->link()); |
| 82 | } |
| 83 | |
| 84 | /** function that writes the procmailrc. |
| 85 | * @return true if all is ok |
| 86 | */ |
| 87 | function write_procmailrc() { |
| 88 | // TODO |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | /** function that register a new Plugin |
| 93 | * @param $_plugin an instance of the plugin |
| 94 | * @return true if all is ok, read error() else |
| 95 | */ |
| 96 | function register_plugin($_plugin) { |
| 97 | if($rtti = $_plugin->rtti()) { |
| 98 | if(is_subclass_of($_plugin, 'matchplugin')) { |
| 99 | if($_plugin->is_global()) { |
| 100 | $this->err = "Philter::register_plugin : this plugin is global"; |
| 101 | return false; |
| 102 | } |
| 103 | $index = 'match_plugins'; |
| 104 | } elseif(is_subclass_of($_plugin, 'actionplugin')) { |
| 105 | $index = 'action_plugins'; |
| 106 | } else { |
| 107 | $this->err = "Philter::register_plugin : bad object"; |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if(isset($this->config[$index][$rtti])) { |
| 112 | $this->err = "Philter::register_plugin : a plugin is already registerd"; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | $this->config[$index][$rtti] = $_plugin; |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | $this->err = "Philter::register_plugin : rtti is 0, this is an abstract class"; |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | /** function that register plugin as a global rule |
| 125 | * @param $_plugin an instance of the plugin |
| 126 | * @return true if all is ok, read error() else |
| 127 | */ |
| 128 | function register_global_plugin($_plugin) { |
| 129 | if($rtti = $_plugin->rtti()) { |
| 130 | if(!is_subclass_of($_plugin, 'matchplugin')) { |
| 131 | $this->err = "Philter::register_global_plugin : bad object"; |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if(!$_plugin->is_global()) { |
| 136 | $this->err = "Philter::register_global_plugin : this plugin is not global"; |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | if(isset($this->config['global_plugins'][$rtti])) { |
| 141 | $this->err = "Philter::register_global_plugin : a plugin is already registerd"; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | $this->config['global_plugins'][$rtti] = $_plugin; |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | $this->err = "Philter::register_global_plugin : rtti is 0, this is an abstract class"; |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /******************************************************************************** |
| 155 | * $Id$ |
| 156 | * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100: |
| 157 | ********************************************************************************/ |
| 158 | ?> |