Code

Updated icons
[gosa.git] / gosa-plugins / systems / admin / systems / services / class_goService.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class goService extends plugin{
24         
25   /* This plugin only writes its objectClass */
26   var $objectclasses    = array();
27   var $attributes       = array();
28   var $StatusFlag       = "";
29  
30   /* This class can't be assigned twice so it conflicts with itsself */
31   var $conflicts            = array();
32   var $dn                   = NULL;
33   var $cn                   = "";
34   var $DisplayName          = "";
35   var $view_logged          = FALSE;
37   var $krb_service_prefix   = "";
38   var $krb_host_key         = NULL; 
39    
40   /* Construcktion */ 
41   function goService(&$config,$dn,$parent = NULL)
42   {
43     plugin::plugin($config,$dn);
44     $this->DisplayName = _("Empty service");
45   }
47   
48   /* Create content */
49   function execute()
50   {
51     if($this->is_account && !$this->view_logged){
52       $this->view_logged = TRUE;
53       new log("view","server/".get_class($this),$this->dn);
54     }
56     $str ="<div style='width:100%; text-align:right;'>".
57           "  <input type='submit' name='SaveService' value='".msgPool::saveButton()."'>&nbsp;".
58           "  <input type='submit' name='CancelService' value='".msgPool::cancelButton()."'>".
59           "</div>";
60     return($str);
61   }
64   /* Get service information for serverService plugin */
65   function getListEntry()
66   {
67     
68     $this->updateStatusState();
70     /* Assign status flag */
71     if(!empty($this->StatusFlag)){
72       $flag                   = $this->StatusFlag;
73       $fields['Status']       = $this->$flag;
74     }else{
75       $fields['Status']       = "";
76     }
78     /* Name displayed in service overview */
79     $fields['Message']      = _("Empty service");
81     /* Allow/disallow some functions */
82     $sf = !empty($this->StatusFlag);
83     $fields['AllowStart']   = $sf && $this->acl_is_writeable("start");
84     $fields['AllowStop']    = $sf && $this->acl_is_writeable("stop");
85     $fields['AllowRestart'] = $sf && $this->acl_is_writeable("restart");
86     $fields['AllowRemove']  = $this->acl_is_removeable();
87     $fields['AllowEdit']    = $this->acl_is_readable("");
88     return($fields);
89   }
92   /* Remove service */
93   function remove_from_parent()
94   {
95     if(!$this->initially_was_account || !$this->acl_is_removeable()){
96       return;
97     }
98     
99     plugin::remove_from_parent();
101     /* Remove status flag, it is not a memeber of 
102         this->attributes, so ensure that it is deleted too */
103     if(!empty($this->StatusFlag)){
104       $this->attrs[$this->StatusFlag] = array();
105     }
107     /* Check if this is a new entry ... add/modify */
108     $ldap = $this->config->get_ldap_link();
109     $ldap->cat($this->dn,array("objectClass"));
110     if($ldap->count()){
111       $ldap->cd($this->dn);
112       $ldap->modify($this->attrs);
113     }else{
114       $ldap->cd($this->dn);
115       $ldap->add($this->attrs);
116       
117     }
119     new log("remove","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
121     if (!$ldap->success()){
122       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
123     }
124     $this->handle_post_events("remove");
125   }
128   /* Save service */
129   function save()
130   {
131     plugin::save();
132     /* Check if this is a new entry ... add/modify */
133     $ldap = $this->config->get_ldap_link();
134     $ldap->cat($this->dn,array("objectClass"));
135     if($ldap->count()){
136       $ldap->cd($this->dn);
137       $ldap->modify($this->attrs);
138     }else{
139       $ldap->cd($this->dn);
140       $ldap->add($this->attrs);
141     }
142     if($this->initially_was_account){
143       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
144       $this->handle_post_events("modify");
145     }else{
146       $this->handle_post_events("add");
147       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
148     }
149     if (!$ldap->success()){
150       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
151     }
152   }
155   /* Directly save new status flag */
156   function setStatus($value)
157   {
158     if($value == "none") return;
160     /* Can't set status flag for new services (Object doesn't exists in ldap tree) */
161     if(!$this->initially_was_account) return;
163     /* Can't set status flag, if no flag is specified  */
164     if(empty($this->StatusFlag)){
165       return;
166     }
168     /* Get object (server), update status flag and save changes */
169     $ldap = $this->config->get_ldap_link();
170     $ldap->cd($this->dn);
171     $ldap->cat($this->dn,array("objectClass"));
172     if($ldap->count()){
174       $tmp = $ldap->fetch();
175       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
176         $attrs['objectClass'][] = $tmp['objectClass'][$i];
177       }
178       $flag = $this->StatusFlag;
179       $attrs[$flag] = $value;
180       $this->$flag = $value;
181       $ldap->modify($attrs);
182       if (!$ldap->success()){
183         msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
184       }
185       $this->action_hook();
186     }
187   }
190   function check()
191   { 
192     $message = plugin::check();
193     return($message);
194   }
195   
197   function save_object()
198   {
199     plugin::save_object();
200   }  
202   
203   function action_hook($add_attrs= array())
204   {
205     /* Find postcreate entries for this class */
206     $command= $this->config->search(get_class($this), "ACTION_HOOK",array('menu','tabs'));
207     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);
217       /* Additional attributes */
218       foreach ($add_attrs as $name => $value){
219         $command= preg_replace("/%$name/", $value, $command);
220       }
222       /* If there are still some %.. in our command, try to fill these with some other class vars */
223       if(preg_match("/%/",$command)){
224         $attrs = get_object_vars($this);
225         foreach($attrs as $name => $value){
226           if(!is_string($value)) continue;
227           $command= preg_replace("/%$name/", $value, $command);
228         }
229       }
231       if (check_command($command)){
232         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
233             $command, "Execute");
235         exec($command);
236       } else {
237         msg_dialog::display(_("Configuration error"), msgPool::cmdnotfound("ACTION_HOOK", get_class($this)), ERROR_DIALOG);
238       }
239     }
240   }
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 ?>