1 <?php
3 class goGlpiServer extends plugin{
5 var $cli_summary = "This pluign is used within the ServerService Pluign \nand indicates that this server supports asterisk management.";
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("goGlpiServer");
11 var $attributes = array("goGlpiAdmin","goGlpiDatabase","goGlpiPassword");
12 var $StatusFlag = "goGlpiServerStatus";
14 /* This class can't be assigned twice so it conflicts with itsself */
15 var $conflicts = array("goGlpiServer");
17 var $DisplayName = "";
18 var $dn = NULL;
19 var $cn = "";
20 var $acl;
22 var $goGlpiServerStatus ="";
23 var $goGlpiAdmin ="";
24 var $goGlpiDatabase ="";
25 var $goGlpiPassword ="";
28 function goGlpiServer($config,$dn)
29 {
30 plugin::plugin($config,$dn);
31 $this->DisplayName = _("Inventory database");
32 }
35 function execute()
36 {
37 $smarty = get_smarty();
38 foreach($this->attributes as $attr){
39 $smarty->assign($attr,$this->$attr);
40 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
41 }
42 return($smarty->fetch(get_template_path("goGlpiServer.tpl",TRUE,dirname(__FILE__))));
43 }
46 function getListEntry()
47 {
48 $this->updateStatusState();
49 $flag = $this->StatusFlag;
50 $fields['Status'] = $this->$flag;
51 $fields['Message'] = _("Glpi management");
52 $fields['AllowStart'] = true;
53 $fields['AllowStop'] = true;
54 $fields['AllowRestart'] = true;
55 $fields['AllowRemove']= true;
56 $fields['AllowEdit'] = true;
57 return($fields);
58 }
61 function remove_from_parent()
62 {
63 plugin::remove_from_parent();
65 /* Remove status flag, it is not a memeber of
66 this->attributes, so ensure that it is deleted too */
67 if(!empty($this->StatusFlag)){
68 $this->attrs[$this->StatusFlag] = array();
69 }
71 /* Check if this is a new entry ... add/modify */
72 $ldap = $this->config->get_ldap_link();
73 $ldap->cat($this->dn,array("objectClass"));
74 if($ldap->count()){
75 $ldap->cd($this->dn);
76 $ldap->modify($this->attrs);
77 }else{
78 $ldap->cd($this->dn);
79 $ldap->add($this->attrs);
80 }
81 show_ldap_error($ldap->get_error(), sprintf(_("Removing server services/glpi with dn '%s' failed."),$this->dn));
82 $this->handle_post_events("remove");
83 }
86 function save()
87 {
88 plugin::save();
89 /* Check if this is a new entry ... add/modify */
90 $ldap = $this->config->get_ldap_link();
91 $ldap->cat($this->dn,array("objectClass"));
92 if($ldap->count()){
93 $ldap->cd($this->dn);
94 $ldap->modify($this->attrs);
95 }else{
96 $ldap->cd($this->dn);
97 $ldap->add($this->attrs);
98 }
99 show_ldap_error($ldap->get_error(), sprintf(_("Saving server services/glpi with dn '%s' failed."),$this->dn));
100 if($this->initially_was_account){
101 $this->handle_post_events("modify");
102 }else{
103 $this->handle_post_events("add");
104 }
105 }
108 /* Directly save new status flag */
109 function setStatus($value)
110 {
111 if($value == "none") return;
112 if(!$this->initially_was_account) return;
113 $ldap = $this->config->get_ldap_link();
114 $ldap->cd($this->dn);
115 $ldap->cat($this->dn,array("objectClass"));
116 if($ldap->count()){
118 $tmp = $ldap->fetch();
119 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
120 $attrs['objectClass'][] = $tmp['objectClass'][$i];
121 }
122 $flag = $this->StatusFlag;
123 $attrs[$flag] = $value;
124 $this->$flag = $value;
125 $ldap->modify($attrs);
126 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/glpi with dn '%s' failed."),$this->dn));
127 $this->action_hook();
128 }
129 }
132 function check()
133 {
134 $message = plugin::check();
135 if(empty($this->goGlpiAdmin)){
136 $message[]= _("The attribute user is empty or contains invalid characters.");
137 }
138 if(empty($this->goGlpiDatabase)){
139 $message[]= _("The attribute database is empty or contains invalid characters.");
140 }
141 return($message);
142 }
145 function save_object()
146 {
147 if(isset($_POST['goLogDBServerPosted'])){
148 plugin::save_object();
149 }
150 }
152 function action_hook($add_attrs= array())
153 {
154 /* Find postcreate entries for this class */
155 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
156 if ($command == "" && isset($this->config->data['TABS'])){
157 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
158 }
159 if ($command != ""){
160 /* Walk through attribute list */
161 foreach ($this->attributes as $attr){
162 if (!is_array($this->$attr)){
163 $command= preg_replace("/%$attr/", $this->$attr, $command);
164 }
165 }
166 $command= preg_replace("/%dn/", $this->dn, $command);
167 /* Additional attributes */
168 foreach ($add_attrs as $name => $value){
169 $command= preg_replace("/%$name/", $value, $command);
170 }
172 /* If there are still some %.. in our command, try to fill these with some other class vars */
173 if(preg_match("/%/",$command)){
174 $attrs = get_object_vars($this);
175 foreach($attrs as $name => $value){
176 if(!is_string($value)) continue;
177 $command= preg_replace("/%$name/", $value, $command);
178 }
179 }
181 if (check_command($command)){
182 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
183 $command, "Execute");
185 exec($command);
186 } else {
187 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
188 print_red ($message);
189 }
190 }
191 }
194 /* Get updates for status flag */
195 function updateStatusState()
196 {
197 if(empty($this->StatusFlag)) return;
199 $attrs = array();
200 $flag = $this->StatusFlag;
201 $ldap = $this->config->get_ldap_link();
202 $ldap->cd($this->cn);
203 $ldap->cat($this->dn,array($flag));
204 if($ldap->count()){
205 $attrs = $ldap->fetch();
206 }
207 if(isset($attrs[$flag][0])){
208 $this->$flag = $attrs[$flag][0];
209 }
210 }
213 /* Return plugin informations for acl handling */
214 function plInfo()
215 {
216 return (array(
217 "plShortName" => _("Glpi server"),
218 "plDescription" => _("Glpi server service"),
219 "plSelfModify" => FALSE,
220 "plDepends" => array(),
221 "plPriority" => 0,
222 "plSection" => array("administration"),
223 "plCategory" => array("server"),
225 "plProvidedAcls"=> array(
226 "goGlpiAdmin" => _("Admin"),
227 "goGlpiDatabase" => _("Database"),
228 "goGlpiPassword" => _("Password"))
229 ));
230 }
233 }
234 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
235 ?>