1 <?php
3 class componentGeneric extends plugin
4 {
5 /* Generic terminal attributes */
6 var $interfaces= array();
7 var $ignore_account= TRUE;
9 /* Needed values and lists */
10 var $base= "";
11 var $cn= "";
12 var $description= "";
13 var $orig_dn= "";
15 /* attribute list for save action */
16 var $attributes= array("cn", "description");
17 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
18 var $netConfigDNS;
20 function componentgeneric ($config, $dn= NULL, $parent= NULL)
21 {
22 plugin::plugin ($config, $dn, $parent);
24 /* Set base */
25 if ($this->dn == "new"){
26 $ui= get_userinfo();
27 $this->base= dn2base($ui->dn);
28 $this->cn= "";
29 } else {
30 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
31 }
32 $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
33 $this->netConfigDNS->acl = $this->acl;
35 /* Save dn for later references */
36 $this->orig_dn= $this->dn;
37 }
39 function execute()
40 {
41 $this->netConfigDNS->acl = $this->acl;
43 /* Call parent execute */
44 plugin::execute();
46 /* Do we represent a valid phone? */
47 if (!$this->is_account && $this->parent == NULL){
48 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
49 _("This 'dn' has no network features.")."</b>";
50 return($display);
51 }
53 /* Base select dialog */
54 $once = true;
55 foreach($_POST as $name => $value){
56 if(preg_match("/^chooseBase/",$name) && $once){
57 $once = false;
58 $this->dialog = new baseSelectDialog($this->config);
59 $this->dialog->setCurrentBase($this->base);
60 }
61 }
63 /* Dialog handling */
64 if(is_object($this->dialog)){
65 /* Must be called before save_object */
66 $this->dialog->save_object();
68 if($this->dialog->isClosed()){
69 $this->dialog = false;
70 }elseif($this->dialog->isSelected()){
71 $this->base = $this->dialog->isSelected();
72 $this->dialog= false;
73 }else{
74 return($this->dialog->execute());
75 }
76 }
78 /* Fill templating stuff */
79 $smarty= get_smarty();
80 $smarty->assign("bases", $this->config->idepartments);
82 /* Assign attributes */
83 foreach ($this->attributes as $attr){
84 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
85 $smarty->assign("$attr", $this->$attr);
86 }
87 $smarty->assign("base_select", $this->base);
88 $smarty->assign("baseACL", chkacl($this->acl,"base"));
89 $this->netConfigDNS->cn= $this->cn;
90 $smarty->assign("netconfig", $this->netConfigDNS->execute());
92 /* Display sub dialog from network settings */
93 if($this->netConfigDNS->dialog){
94 $this->dialog = TRUE;
95 return($this->netConfigDNS->execute());
96 }else{
97 $this->dialog = FALSE;
98 }
100 /* Show main page */
101 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
102 }
104 function remove_from_parent()
105 {
106 $this->netConfigDNS->acl = $this->acl;
107 $ldap= $this->config->get_ldap_link();
108 $this->netConfigDNS->remove_from_parent();
109 $ldap->rmdir($this->dn);
110 show_ldap_error($ldap->get_error(), _("Removing generic component failed"));
111 $this->handle_post_events("remove",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
113 /* Delete references to object groups */
114 $ldap->cd ($this->config->current['BASE']);
115 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
116 while ($ldap->fetch()){
117 $og= new ogroup($this->config, $ldap->getDN());
118 unset($og->member[$this->dn]);
119 $og->save ();
120 }
121 }
124 /* Save data to object */
125 function save_object()
126 {
127 plugin::save_object();
129 $this->netConfigDNS->save_object();
131 /* Save base, since this is no LDAP attribute */
132 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
133 $this->base= $_POST['base'];
134 }
135 }
138 /* Check supplied data */
139 function check()
140 {
141 /* Call common method to give check the hook */
142 $message= plugin::check();
143 $message= array_merge($message,$this->netConfigDNS->check());
145 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
147 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
148 $message[]= _("The required field 'Component name' is not set.");
149 }
151 /* Check if given name is a valid host/dns name */
152 if(!is_dns_name($this->cn) && chkacl ($this->acl, "cn") == ""){
153 $message[] = _("Please specify a valid name for this object.");
154 }
156 /* To check for valid ip*/
157 if($this->netConfigDNS->ipHostNumber == ""){
158 $message[]= _("The required field IP address is empty.");
159 } else {
160 if (!is_ip($this->netConfigDNS->ipHostNumber)){
161 $message[]= _("The field IP address contains an invalid address.");
162 }
163 }
165 $ui= get_userinfo();
166 $acl= get_permissions ($this->dn, $ui->subtreeACL);
167 $acl= get_module_permission($acl, "component", $this->dn);
168 if (chkacl($acl, "create") != ""){
169 $message[]= _("You have no permissions to create a component on this 'Base'.");
170 }
172 if ($this->orig_dn != $this->dn){
173 $ldap= $this->config->get_ldap_link();
174 $ldap->cd ($this->base);
175 $ldap->search ("(cn=".$this->cn.")", array("cn"));
176 if ($ldap->count() != 0){
177 while ($attrs= $ldap->fetch()){
178 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
179 continue;
180 }
181 if ($attrs['dn'] != $this->orig_dn){
182 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
183 break;
184 }
185 }
186 }
187 }
189 return ($message);
190 }
193 /* Save to LDAP */
194 function save()
195 {
196 $this->netConfigDNS->acl = $this->acl;
197 plugin::save();
199 /* Remove all empty values */
200 if ($this->orig_dn == 'new'){
201 $attrs= array();
202 foreach ($this->attrs as $key => $val){
203 if (is_array($val) && count($val) == 0){
204 continue;
205 }
206 $attrs[$key]= $val;
207 }
208 $this->attrs= $attrs;
209 }
211 /* If this is a new Object IP & Mac aren't set.
212 IP & Mac are msut attributes, so we set this values by here. */
213 if($this->orig_dn == 'new'){
214 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
215 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
216 }
218 /* Write back to ldap */
219 $ldap= $this->config->get_ldap_link();
220 if ($this->orig_dn == 'new'){
221 $ldap->cd($this->config->current['BASE']);
222 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
223 $ldap->cd($this->dn);
224 $ldap->add($this->attrs);
225 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
226 } else {
227 if ($this->orig_dn != $this->dn){
228 $this->move($this->orig_dn, $this->dn);
229 }
231 $ldap->cd($this->dn);
232 $this->cleanup();
233 $ldap->modify ($this->attrs);
235 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
236 }
238 $this->netConfigDNS->cn = $this->cn;
239 $this->netConfigDNS->save($this->dn);
241 show_ldap_error($ldap->get_error(), _("Saving generic component failed"));
242 }
243 }
245 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
246 ?>