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