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 $macAddress= "";
18 var $ipHostNumber= "";
19 var $description= "";
20 var $orig_dn= "";
22 /* attribute list for save action */
23 var $attributes= array("cn", "description", "macAddress", "ipHostNumber");
24 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
26 function componentgeneric ($config, $dn= NULL)
27 {
28 plugin::plugin ($config, $dn);
30 /* Set base */
31 if ($this->dn == "new"){
32 $ui= get_userinfo();
33 $this->base= dn2base($ui->dn);
34 $this->cn= "";
35 } else {
36 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
37 }
39 /* Save dn for later references */
40 $this->orig_dn= $this->dn;
41 }
43 function execute()
44 {
45 /* Call parent execute */
46 plugin::execute();
48 /* Do we represent a valid phone? */
49 if (!$this->is_account && $this->parent == NULL){
50 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
51 _("This 'dn' has no network features.")."</b>";
52 return($display);
53 }
55 /* Fill templating stuff */
56 $smarty= get_smarty();
57 $smarty->assign("bases", $this->config->idepartments);
59 /* Assign attributes */
60 foreach ($this->attributes as $attr){
61 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
62 $smarty->assign("$attr", $this->$attr);
63 }
64 $smarty->assign("base_select", $this->base);
66 /* Show Asterisk for required attribute ipHostNumber and macAddress */
67 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
69 /* Show main page */
70 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
71 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
72 }
74 function remove_from_parent()
75 {
76 $ldap= $this->config->get_ldap_link();
77 $ldap->rmdir($this->dn);
78 show_ldap_error($ldap->get_error());
79 $this->handle_post_events("remove");
81 /* Delete references to object groups */
82 $ldap->cd ($this->config->current['BASE']);
83 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
84 while ($ldap->fetch()){
85 $og= new ogroup($this->config, $ldap->getDN());
86 unset($og->member[$this->dn]);
87 $og->save ();
88 }
90 }
93 /* Save data to object */
94 function save_object()
95 {
96 plugin::save_object();
98 /* Save base, since this is no LDAP attribute */
99 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
100 $this->base= $_POST['base'];
101 }
102 }
105 /* Check supplied data */
106 function check()
107 {
108 $message= array();
109 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
111 /* must: cn, macAddress, ipHostNumber */
112 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
113 $message[]= _("The required field 'Component name' is not set.");
114 }
115 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
116 $message[]= _("The required field 'MAC-address' is not set.");
117 }
118 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
119 $message[]= _("The required field 'IP-address' is not set.");
120 }
122 $ui= get_userinfo();
123 $acl= get_permissions ($this->dn, $ui->subtreeACL);
124 $acl= get_module_permission($acl, "component", $this->dn);
125 if (chkacl($acl, "create") != ""){
126 $message[]= _("You have no permissions to create a component on this 'Base'.");
127 }
129 if ($this->orig_dn != $this->dn){
130 $ldap= $this->config->get_ldap_link();
131 $ldap->cd ($this->base);
132 $ldap->search ("(cn=".$this->cn.")", array("cn"));
133 if ($ldap->count() != 0){
134 while ($attrs= $ldap->fetch()){
135 if ($attrs['dn'] != $this->orig_dn){
136 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
137 break;
138 }
139 }
140 }
141 }
143 return ($message);
144 }
147 /* Save to LDAP */
148 function save()
149 {
150 plugin::save();
152 /* Remove all empty values */
153 if ($this->orig_dn == 'new'){
154 $attrs= array();
155 foreach ($this->attrs as $key => $val){
156 if (is_array($val) && count($val) == 0){
157 continue;
158 }
159 $attrs[$key]= $val;
160 }
161 $this->attrs= $attrs;
162 }
164 /* Write back to ldap */
165 $ldap= $this->config->get_ldap_link();
166 if ($this->orig_dn == 'new'){
167 $ldap->cd($this->config->current['BASE']);
168 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
169 $ldap->cd($this->dn);
170 $ldap->add($this->attrs);
171 $this->handle_post_events("add");
172 } else {
173 if ($this->orig_dn != $this->dn){
174 $this->move($this->orig_dn, $this->dn);
175 }
177 $ldap->cd($this->dn);
178 $ldap->modify($this->attrs);
179 $this->handle_post_events("modify");
180 }
181 show_ldap_error($ldap->get_error());
183 /* Optionally execute a command after we're done */
184 $this->postcreate();
185 }
187 }
189 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
190 ?>