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