4a717147641fbcbf9801fe4cef9df81c7fff04c4
[old-projects.git] / philter / philter / include / philter.inc.php
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 function bd() { global $philter; return ($philter->is_advanced() ? 'tmp_' : ''); }
15
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('spool'),
32 'match_plugins' => array(),
33 'action_plugins' => array(),
34 'global_plugins' => array(),
35 'i18n' => array()
36 );
37 $this->err = "";
38 }
39
40 function is_advanced() { return isset($_SESSION['philter_adv']); }
41 function set_advanced() { $_SESSION['philter_adv'] = true; }
42 function del_advanced() { unset($_SESSION['philter_adv']); }
43
44 /** returns the i18n string
45 * @param $_index the index of the string
46 * @returns the string or false
47 */
48 function i18n($_index) {
49 if(isset($this->config['i18n'][$_index]))
50 return $this->config['i18n'][$_index];
51 return false;
52 }
53
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 * @param &$procmail the string containing the procmail filter
87 * @param $filename the base filename of the procmailrc filter
88 * @return true if all is ok
89 */
90 function write_procmailrc(&$procmail, $filename) {
91 $file = $this->config['path']['spool']."/".$filename;
92 $f = fopen($file.".tmp", "w");
93 fwrite($f, $procmail, strlen($procmail));
94 fclose($f);
95 rename($file.".tmp", $file);
96 echo "<pre>\n";
97 echo $procmail;
98 echo "</pre>\n";
99 return true;
100 }
101
102 /** function that register a new Plugin
103 * @param $_plugin an instance of the plugin
104 * @return true if all is ok, read error() else
105 */
106 function register_plugin($_plugin) {
107 if($rtti = $_plugin->rtti()) {
108 if(is_subclass_of($_plugin, 'matchplugin')) {
109 if($_plugin->is_global()) {
110 $this->err = "Philter::register_plugin : this plugin is global";
111 return false;
112 }
113 $index = 'match_plugins';
114 } elseif(is_subclass_of($_plugin, 'actionplugin')) {
115 $index = 'action_plugins';
116 } else {
117 $this->err = "Philter::register_plugin : bad object";
118 return false;
119 }
120
121 if(isset($this->config[$index][$rtti])) {
122 $this->err = "Philter::register_plugin : a plugin is already registerd";
123 return false;
124 }
125
126 $this->config[$index][$rtti] = $_plugin;
127 return true;
128 }
129
130 $this->err = "Philter::register_plugin : rtti is 0, this is an abstract class";
131 return false;
132 }
133
134 /** function that register plugin as a global rule
135 * @param $_plugin an instance of the plugin
136 * @return true if all is ok, read error() else
137 */
138 function register_global_plugin($_plugin) {
139 if($rtti = $_plugin->rtti()) {
140 if(!is_subclass_of($_plugin, 'matchplugin')) {
141 $this->err = "Philter::register_global_plugin : bad object";
142 return false;
143 }
144
145 if(!$_plugin->is_global()) {
146 $this->err = "Philter::register_global_plugin : this plugin is not global";
147 return false;
148 }
149
150 if(isset($this->config['global_plugins'][$rtti])) {
151 $this->err = "Philter::register_global_plugin : a plugin is already registerd";
152 return false;
153 }
154
155 $this->config['global_plugins'][$rtti] = $_plugin;
156 return true;
157 }
158
159 $this->err = "Philter::register_global_plugin : rtti is 0, this is an abstract class";
160 return false;
161 }
162 }
163
164 /********************************************************************************
165 * $Id$
166 * vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=100:
167 ********************************************************************************/
168 ?>