1 <?php
3 class goImapServer extends plugin{
5 var $cli_summary = "This pluign is used within the ServerService Pluign \nand indicates that this server supports mailqueue listings and so on.";
6 var $cli_description = "Some longer text\nfor help";
7 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9 /* This plugin only writes its objectClass */
10 var $objectclasses = array("goImapServer");
12 /* This class can't be assigned twice so it conflicts with itsself */
14 var $DisplayName = "";
15 var $dn = NULL;
16 var $StatusFlag = "goImapServerStatus";
17 var $attributes = array("goImapName","goImapConnect","goImapAdmin","goImapPassword",
18 "goImapSieveServer","goImapSievePort",
19 "cyrusImap","cyrusImapSSL","cyrusPop3","cyrusPop3SSL");
21 var $cn = "";
23 var $goImapName = "";
24 var $goImapConnect = "";
25 var $goImapAdmin = "";
26 var $goImapPassword = "";
28 var $goImapSieveServer = "";
29 var $goImapSievePort = "";
31 var $goImapServerStatus = "";
33 var $cyrusImap = false;
34 var $cyrusImapSSL = false;
35 var $cyrusPop3 = false;
36 var $cyrusPop3SSL = false;
37 var $is_account = false;
39 var $acl;
41 var $Actions = array();
42 var $conflicts = array("goImapServer","kolab");
44 function goImapServer($config,$dn)
45 {
46 plugin::plugin($config,$dn);
48 $this->DisplayName = _("IMAP/POP3 service");
50 $this->Actions = array( SERVICE_STOPPED=>SERVICE_STOPPED,
51 SERVICE_STARTED => SERVICE_STARTED,
52 SERVICE_RESTARTED=>SERVICE_RESTARTED,
53 "repair_database"=>_("Repair database"));
55 }
57 function execute()
58 {
59 $smarty = get_smarty();
61 /* set new status */
62 if(isset($_POST['ExecAction'])){
63 if(isset($this->Actions[$_POST['action']])){
64 $this->setStatus($_POST['action']);
65 }
66 }
68 foreach($this->attributes as $attr){
69 $smarty->assign($attr,$this->$attr);
70 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
71 }
72 $smarty->assign("Actions",$this->Actions);
73 $smarty->assign("is_new",$this->dn);
74 $smarty->assign("is_acc",$this->initially_was_account);
75 return($smarty->fetch(get_template_path("goImapServer.tpl",TRUE,dirname(__FILE__))));
76 }
78 function getListEntry()
79 {
80 $this->updateStatusState();
81 $flag = $this->StatusFlag;
82 $fields['Status'] = $this->$flag;
83 $fields['Message'] = _("Cyrus service");
84 $fields['AllowStart'] = true;
85 $fields['AllowStop'] = true;
86 $fields['AllowRestart'] = true;
87 $fields['AllowRemove']= true;
88 $fields['AllowEdit'] = true;
89 return($fields);
90 }
92 function remove_from_parent()
93 {
94 plugin::remove_from_parent();
96 /* Remove status flag, it is not a memeber of
97 this->attributes, so ensure that it is deleted too */
98 if(!empty($this->StatusFlag)){
99 $this->attrs[$this->StatusFlag] = array();
100 }
102 /* Check if this is a new entry ... add/modify */
103 $ldap = $this->config->get_ldap_link();
104 $ldap->cat($this->dn,array("objectClass"));
105 if($ldap->count()){
106 $ldap->cd($this->dn);
107 $ldap->modify($this->attrs);
108 }else{
109 $ldap->cd($this->dn);
110 $ldap->add($this->attrs);
111 }
112 show_ldap_error($ldap->get_error());
113 $this->handle_post_events("remove");
114 }
116 function save()
117 {
118 $this->goImapSieveServer = $this->cn;
119 plugin::save();
120 /* Check if this is a new entry ... add/modify */
121 $ldap = $this->config->get_ldap_link();
122 $ldap->cat($this->dn,array("objectClass"));
123 if($ldap->count()){
124 $ldap->cd($this->dn);
125 $ldap->modify($this->attrs);
126 }else{
127 $ldap->cd($this->dn);
128 $ldap->add($this->attrs);
129 }
130 show_ldap_error($ldap->get_error());
131 if($this->initially_was_account){
132 $this->handle_post_events("modify");
133 }else{
134 $this->handle_post_events("add");
135 }
137 }
140 /* Directly save new status flag */
141 function setStatus($value)
142 {
143 if($value == "none") return;
144 if(!$this->initially_was_account) return;
145 $ldap = $this->config->get_ldap_link();
146 $ldap->cd($this->dn);
147 $ldap->cat($this->dn,array("objectClass"));
148 if($ldap->count()){
150 $tmp = $ldap->fetch();
151 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
152 $attrs['objectClass'][] = $tmp['objectClass'][$i];
153 }
154 $flag = $this->StatusFlag;
155 $attrs[$flag] = $value;
156 $this->$flag = $value;
157 $ldap->modify($attrs);
158 show_ldap_error($ldap->get_error());
159 $this->action_hook();
160 }
161 }
164 function check()
165 {
166 $message = plugin::check();
167 if(empty($this->goImapName)){
168 $message[] =_("Please specify a server identifier.");
169 }
170 if(empty($this->goImapConnect)){
171 $message[] =_("Please specify a connect url.");
172 }
173 if(empty($this->goImapAdmin)){
174 $message[] =_("Please specify an admin user.");
175 }
176 if(empty($this->goImapPassword)){
177 $message[] =_("Please specify a password for the admin user.");
178 }
180 /* Check connect string */
181 if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
182 $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
183 '{server-name:port/options}');
184 }
185 if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
186 $message[]= _("The sieve port needs to be numeric.");
187 }
189 return ($message);
190 }
193 function save_object()
194 {
195 if(isset($_POST['goImapServerPosted'])){
196 plugin::save_object();
198 foreach(array("cyrusImap","cyrusImapSSL","cyrusPop3","cyrusPop3SSL") as $checkbox) {
199 if(!isset($_POST[$checkbox])){
200 $this->$checkbox = false;
201 }else{
202 $this->$checkbox = true;
203 }
204 }
205 }
206 }
208 function action_hook($add_attrs= array())
209 {
210 /* Find postcreate entries for this class */
211 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
212 if ($command == "" && isset($this->config->data['TABS'])){
213 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
214 }
215 if ($command != ""){
216 /* Walk through attribute list */
217 foreach ($this->attributes as $attr){
218 if (!is_array($this->$attr)){
219 $command= preg_replace("/%$attr/", $this->$attr, $command);
220 }
221 }
222 $command= preg_replace("/%dn/", $this->dn, $command);
223 /* Additional attributes */
224 foreach ($add_attrs as $name => $value){
225 $command= preg_replace("/%$name/", $value, $command);
226 }
228 /* If there are still some %.. in our command, try to fill these with some other class vars */
229 if(preg_match("/%/",$command)){
230 $attrs = get_object_vars($this);
231 foreach($attrs as $name => $value){
232 if(!is_string($value)) continue;
233 $command= preg_replace("/%$name/", $value, $command);
234 }
235 }
237 if (check_command($command)){
238 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
239 $command, "Execute");
241 exec($command);
242 } else {
243 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
244 print_red ($message);
245 }
246 }
247 }
250 /* Get updates for status flag */
251 function updateStatusState()
252 {
253 if(empty($this->StatusFlag)) return;
255 $attrs = array();
256 $flag = $this->StatusFlag;
257 $ldap = $this->config->get_ldap_link();
258 $ldap->cd($this->cn);
259 $ldap->cat($this->dn,array($flag));
260 if($ldap->count()){
261 $attrs = $ldap->fetch();
262 }
263 if(isset($attrs[$flag][0])){
264 $this->$flag = $attrs[$flag][0];
265 }
266 }
267 }
268 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
269 ?>