Code

Updated get status flag
[gosa.git] / plugins / admin / systems / class_goImapServer.inc
1 <?php
3 class goImapServer extends plugin{
4         
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");
11   
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       = "";
27   
28   var $goImapSieveServer    = "";
29   var $goImapSievePort      = "";
30   
31   var $goImapServerStatus   = "";
32   
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");
43  
44   function goImapServer($config,$dn)
45   {
46     plugin::plugin($config,$dn);
47   
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"));
54     
55   }
57   function execute()
58   { 
59     $smarty = get_smarty();
60   
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();
95     /* Check if this is a new entry ... add/modify */
96     $ldap = $this->config->get_ldap_link();
97     $ldap->cat($this->dn,array("objectClass"));
98     if($ldap->count()){
99       $ldap->cd($this->dn);
100       $ldap->modify($this->attrs);
101     }else{
102       $ldap->cd($this->dn);
103       $ldap->add($this->attrs);
104     }
105     show_ldap_error($ldap->get_error());
106     $this->handle_post_events("remove");
107   }
109   function save()
110   {
111     $this->goImapSieveServer = $this->cn;
112     plugin::save();
113     /* Check if this is a new entry ... add/modify */
114     $ldap = $this->config->get_ldap_link();
115     $ldap->cat($this->dn,array("objectClass"));
116     if($ldap->count()){
117       $ldap->cd($this->dn);
118       $ldap->modify($this->attrs);
119     }else{
120       $ldap->cd($this->dn);
121       $ldap->add($this->attrs);
122     }
123     show_ldap_error($ldap->get_error());
124     if($this->initially_was_account){
125       $this->handle_post_events("modify");
126     }else{
127       $this->handle_post_events("add");
128     }
130   }
133    /* Directly save new status flag */
134   function setStatus($value)
135   {
136     if($value == "none") return;
137     if(!$this->initially_was_account) return;
138     $ldap = $this->config->get_ldap_link();
139     $ldap->cd($this->dn);
140     $ldap->cat($this->dn,array("objectClass"));
141     if($ldap->count()){
143       $tmp = $ldap->fetch();
144       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
145         $attrs['objectClass'][] = $tmp['objectClass'][$i];
146       }
147       $flag = $this->StatusFlag;
148       $attrs[$flag] = $value;
149       $this->$flag = $value;
150       $ldap->modify($attrs);
151       show_ldap_error($ldap->get_error());
152       $this->action_hook();
153     }
154   }
157   function check()
158   { 
159     $message = plugin::check();
160     if(empty($this->goImapName)){
161       $message[] =_("Please specify a server identifier.");
162     }  
163     if(empty($this->goImapConnect)){
164       $message[] =_("Please specify a connect url.");
165     }  
166     if(empty($this->goImapAdmin)){
167       $message[] =_("Please specify an admin user.");
168     }  
169     if(empty($this->goImapPassword)){
170       $message[] =_("Please specify a password for the admin user.");
171     } 
173     /* Check connect string */
174     if (!preg_match('/^\{[^:]+:[0-9]+.*\}$/', $this->goImapConnect)){
175       $message[]= sprintf(_("The imap connect string needs to be in the form '%s'."),
176           '{server-name:port/options}');
177     }
178     if (!preg_match('/^[0-9]+$/', $this->goImapSievePort)){
179       $message[]= _("The sieve port needs to be numeric.");
180     }
181  
182     return ($message);
183   }
186   function save_object()
187   {
188     if(isset($_POST['goImapServerPosted'])){
189       plugin::save_object(); 
191       foreach(array("cyrusImap","cyrusImapSSL","cyrusPop3","cyrusPop3SSL") as $checkbox) { 
192         if(!isset($_POST[$checkbox])){
193           $this->$checkbox = false;
194         }else{
195           $this->$checkbox = true;
196         }
197       }
198     }
199   }
201    function action_hook($add_attrs= array())
202   {
203     /* Find postcreate entries for this class */
204     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
205     if ($command == "" && isset($this->config->data['TABS'])){
206       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
207     }
208     if ($command != ""){
209       /* Walk through attribute list */
210       foreach ($this->attributes as $attr){
211         if (!is_array($this->$attr)){
212           $command= preg_replace("/%$attr/", $this->$attr, $command);
213         }
214       }
215       $command= preg_replace("/%dn/", $this->dn, $command);
216       /* Additional attributes */
217       foreach ($add_attrs as $name => $value){
218         $command= preg_replace("/%$name/", $value, $command);
219       }
221       /* If there are still some %.. in our command, try to fill these with some other class vars */
222       if(preg_match("/%/",$command)){
223         $attrs = get_object_vars($this);
224         foreach($attrs as $name => $value){
225           if(!is_string($value)) continue;
226           $command= preg_replace("/%$name/", $value, $command);
227         }
228       }
230       if (check_command($command)){
231         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
232             $command, "Execute");
234         exec($command);
235       } else {
236         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
237         print_red ($message);
238       }
239     }
240   }
242   
243   /* Get updates for status flag */
244   function updateStatusState()
245   {
246     if(empty($this->StatusFlag)) return;
248     $attrs = array();
249     $flag = $this->StatusFlag;
250     $ldap = $this->config->get_ldap_link();
251     $ldap->cd($this->cn);
252     $ldap->cat($this->dn,array($flag));
253     if($ldap->count()){
254       $attrs = $ldap->fetch();
255     }
256     if(isset($attrs[$flag][0])){
257       $this->$flag = $attrs[$flag][0];
258     }
259   }
261 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
262 ?>