1 <?php
3 class componentGeneric extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary= "Manage component base objects";
7 var $cli_description= "Some longer text\nfor help";
8 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* Generic terminal attributes */
11 var $interfaces= array();
12 var $ignore_account= TRUE;
14 /* Needed values and lists */
15 var $base= "";
16 var $cn= "";
17 var $description= "";
18 var $orig_dn= "";
20 /* attribute list for save action */
21 var $attributes= array("cn", "description");
22 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
23 var $netConfigDNS;
25 function componentgeneric ($config, $dn= NULL, $parent= NULL)
26 {
27 plugin::plugin ($config, $dn, $parent);
29 /* Set base */
30 if ($this->dn == "new"){
31 $ui= get_userinfo();
32 $this->base= dn2base($ui->dn);
33 $this->cn= "";
34 } else {
35 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
36 }
37 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
38 /* Save dn for later references */
39 $this->orig_dn= $this->dn;
41 if($this->is_account){
42 @log::log("view","component/".get_class($this),$this->dn);
43 }
44 }
46 function execute()
47 {
48 /* Call parent execute */
49 plugin::execute();
51 /* Do we represent a valid phone? */
52 if (!$this->is_account && $this->parent == NULL){
53 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
54 _("This 'dn' has no network features.")."</b>";
55 return($display);
56 }
58 /* Base select dialog */
59 $once = true;
60 foreach($_POST as $name => $value){
61 if(preg_match("/^chooseBase/",$name) && $once){
62 $once = false;
63 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
64 $this->dialog->setCurrentBase($this->base);
65 }
66 }
68 /* Dialog handling */
69 if(is_object($this->dialog)){
70 /* Must be called before save_object */
71 $this->dialog->save_object();
73 if($this->dialog->isClosed()){
74 $this->dialog = false;
75 }elseif($this->dialog->isSelected()){
77 /* A new base was selected, check if it is a valid one */
78 $tmp = $this->get_allowed_bases();
79 if(isset($tmp[$this->dialog->isSelected()])){
80 $this->base = $this->dialog->isSelected();
81 }
83 $this->dialog= false;
84 }else{
85 return($this->dialog->execute());
86 }
87 }
89 /* Fill templating stuff */
90 $smarty= get_smarty();
91 $smarty->assign("bases", $this->config->idepartments);
93 /* Set acls */
94 $tmp = $this->plInfo();
95 foreach($tmp['plProvidedAcls'] as $name => $translation){
96 $smarty->assign($name."ACL",$this->getacl($name));
97 }
99 $smarty->assign("bases", $this->get_allowed_bases());
101 /* Assign attributes */
102 foreach ($this->attributes as $attr){
103 $smarty->assign("$attr", $this->$attr);
104 }
105 $smarty->assign("base_select", $this->base);
107 /* Show main page */
108 $smarty->assign("netconfig", $this->netConfigDNS->execute());
109 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
110 }
112 function set_acl_base($base)
113 {
114 plugin::set_acl_base($base);
115 $this->netConfigDNS->set_acl_base($base);
116 }
118 function set_acl_category($cat)
119 {
120 plugin::set_acl_category($cat);
121 $this->netConfigDNS->set_acl_category($cat);
122 }
124 function remove_from_parent()
125 {
126 $ldap= $this->config->get_ldap_link();
127 $this->netConfigDNS->remove_from_parent();
128 $ldap->rmdir($this->dn);
130 @log::log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
132 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
133 $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
135 /* Delete references to object groups */
136 $ldap->cd ($this->config->current['BASE']);
137 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
138 while ($ldap->fetch()){
139 $og= new ogroup($this->config, $ldap->getDN());
140 unset($og->member[$this->dn]);
141 $og->save ();
142 }
143 }
146 /* Save data to object */
147 function save_object()
148 {
150 /* Create a base backup and reset the
151 base directly after calling plugin::save_object();
152 Base will be set seperatly a few lines below */
153 $base_tmp = $this->base;
154 plugin::save_object();
155 $this->base = $base_tmp;
157 $this->netConfigDNS->save_object();
159 /* Set new base if allowed */
160 $tmp = $this->get_allowed_bases();
161 if(isset($_POST['base'])){
162 if(isset($tmp[$_POST['base']])){
163 $this->base= $_POST['base'];
164 }
165 }
166 }
169 /* Check supplied data */
170 function check()
171 {
172 /* Call common method to give check the hook */
173 $message= plugin::check();
174 $message= array_merge($message,$this->netConfigDNS->check());
176 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
178 if ($this->cn == "" ){
179 $message[]= _("The required field 'Component name' is not set.");
180 }
182 /* To check for valid ip*/
183 if($this->netConfigDNS->ipHostNumber == ""){
184 $message[]= _("The required field IP address is empty.");
185 } else {
186 if (!is_ip($this->netConfigDNS->ipHostNumber)){
187 $message[]= _("The field IP address contains an invalid address.");
188 }
189 }
191 if ($this->orig_dn != $this->dn){
192 $ldap= $this->config->get_ldap_link();
193 $ldap->cd ($this->base);
194 $ldap->search ("(cn=".$this->cn.")", array("cn"));
195 if ($ldap->count() != 0){
196 while ($attrs= $ldap->fetch()){
197 if ($attrs['dn'] != $this->orig_dn){
198 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
199 break;
200 }
201 }
202 }
203 }
205 return ($message);
206 }
209 /* Save to LDAP */
210 function save()
211 {
212 plugin::save();
214 /* Remove all empty values */
215 if ($this->orig_dn == 'new'){
216 $attrs= array();
217 foreach ($this->attrs as $key => $val){
218 if (is_array($val) && count($val) == 0){
219 continue;
220 }
221 $attrs[$key]= $val;
222 }
223 $this->attrs= $attrs;
224 }
226 /* If this is a new Object IP & Mac aren't set.
227 IP & Mac are msut attributes, so we set this values by here. */
228 if($this->orig_dn == 'new'){
229 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
230 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
231 }
233 /* Write back to ldap */
234 $ldap= $this->config->get_ldap_link();
235 if ($this->orig_dn == 'new'){
236 $ldap->cd($this->config->current['BASE']);
237 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
238 $ldap->cd($this->dn);
239 $ldap->add($this->attrs);
240 @log::log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
241 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
243 } else {
244 if ($this->orig_dn != $this->dn){
245 $this->move($this->orig_dn, $this->dn);
246 }
248 $ldap->cd($this->dn);
249 $this->cleanup();
250 $ldap->modify ($this->attrs);
251 @log::log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
252 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
253 }
255 $this->netConfigDNS->cn = $this->cn;
256 $this->netConfigDNS->save($this->dn);
258 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
259 }
261 /* Return plugin informations for acl handling */
262 function plInfo()
263 {
264 return (array(
265 "plShortName" => _("Generic"),
266 "plDescription" => _("Component generic"),
267 "plSelfModify" => FALSE,
268 "plDepends" => array(),
269 "plPriority" => 1,
270 "plSection" => array("administration"),
271 "plCategory" => array("component" => array("description" => _("Network device"),
272 "objectClass" => array("device", "ipHost", "ieee802Device"))),
273 "plProvidedAcls"=> array(
274 "cn" => _("Name"),
275 "base" => _("Base"),
276 "description" => _("Description"))
277 ));
278 }
281 }
283 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
284 ?>