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;
40 }
42 function execute()
43 {
44 /* Call parent execute */
45 plugin::execute();
47 /* Do we represent a valid phone? */
48 if (!$this->is_account && $this->parent == NULL){
49 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
50 _("This 'dn' has no network features.")."</b>";
51 return($display);
52 }
54 /* Base select dialog */
55 $once = true;
56 foreach($_POST as $name => $value){
57 if(preg_match("/^chooseBase/",$name) && $once){
58 $once = false;
59 $this->dialog = new baseSelectDialog($this->config);
60 $this->dialog->setCurrentBase($this->base);
61 }
62 }
64 /* Dialog handling */
65 if(is_object($this->dialog)){
66 /* Must be called before save_object */
67 $this->dialog->save_object();
69 if($this->dialog->isClosed()){
70 $this->dialog = false;
71 }elseif($this->dialog->isSelected()){
72 $this->base = $this->dialog->isSelected();
73 $this->dialog= false;
74 }else{
75 return($this->dialog->execute());
76 }
77 }
79 /* Fill templating stuff */
80 $smarty= get_smarty();
81 $smarty->assign("bases", $this->config->idepartments);
83 /* Assign attributes */
84 foreach ($this->attributes as $attr){
85 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
86 $smarty->assign("$attr", $this->$attr);
87 }
88 $smarty->assign("base_select", $this->base);
89 $smarty->assign("baseACL", chkacl($this->acl,"base"));
91 /* Show main page */
92 $this->netConfigDNS->cn= $this->cn;
93 $smarty->assign("netconfig", $this->netConfigDNS->execute());
94 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
95 }
97 function remove_from_parent()
98 {
99 $ldap= $this->config->get_ldap_link();
100 $this->netConfigDNS->remove_from_parent();
101 $ldap->rmdir($this->dn);
102 show_ldap_error($ldap->get_error(), _("Removing generic component failed"));
103 $this->handle_post_events("remove");
105 /* Delete references to object groups */
106 $ldap->cd ($this->config->current['BASE']);
107 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
108 while ($ldap->fetch()){
109 $og= new ogroup($this->config, $ldap->getDN());
110 unset($og->member[$this->dn]);
111 $og->save ();
112 }
113 }
116 /* Save data to object */
117 function save_object()
118 {
119 plugin::save_object();
121 $this->netConfigDNS->save_object();
123 /* Save base, since this is no LDAP attribute */
124 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
125 $this->base= $_POST['base'];
126 }
127 }
130 /* Check supplied data */
131 function check()
132 {
133 /* Call common method to give check the hook */
134 $message= plugin::check();
135 $message= array_merge($message,$this->netConfigDNS->check());
137 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
139 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
140 $message[]= _("The required field 'Component name' is not set.");
141 }
143 /* To check for valid ip*/
144 if($this->netConfigDNS->ipHostNumber == ""){
145 $message[]= _("The required field IP address is empty.");
146 } else {
147 if (!is_ip($this->netConfigDNS->ipHostNumber)){
148 $message[]= _("The field IP address contains an invalid address.");
149 }
150 }
152 $ui= get_userinfo();
153 $acl= get_permissions ($this->dn, $ui->subtreeACL);
154 $acl= get_module_permission($acl, "component", $this->dn);
155 if (chkacl($acl, "create") != ""){
156 $message[]= _("You have no permissions to create a component on this 'Base'.");
157 }
159 if ($this->orig_dn != $this->dn){
160 $ldap= $this->config->get_ldap_link();
161 $ldap->cd ($this->base);
162 $ldap->search ("(cn=".$this->cn.")", array("cn"));
163 if ($ldap->count() != 0){
164 while ($attrs= $ldap->fetch()){
165 if ($attrs['dn'] != $this->orig_dn){
166 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
167 break;
168 }
169 }
170 }
171 }
173 return ($message);
174 }
177 /* Save to LDAP */
178 function save()
179 {
180 plugin::save();
182 /* Remove all empty values */
183 if ($this->orig_dn == 'new'){
184 $attrs= array();
185 foreach ($this->attrs as $key => $val){
186 if (is_array($val) && count($val) == 0){
187 continue;
188 }
189 $attrs[$key]= $val;
190 }
191 $this->attrs= $attrs;
192 }
194 /* If this is a new Object IP & Mac aren't set.
195 IP & Mac are msut attributes, so we set this values by here. */
196 if($this->orig_dn == 'new'){
197 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
198 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
199 }
201 /* Write back to ldap */
202 $ldap= $this->config->get_ldap_link();
203 if ($this->orig_dn == 'new'){
204 $ldap->cd($this->config->current['BASE']);
205 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
206 $ldap->cd($this->dn);
207 $ldap->add($this->attrs);
208 $this->handle_post_events("add");
209 } else {
210 if ($this->orig_dn != $this->dn){
211 $this->move($this->orig_dn, $this->dn);
212 }
214 $ldap->cd($this->dn);
215 $this->cleanup();
216 $ldap->modify ($this->attrs);
218 $this->handle_post_events("modify");
219 }
221 $this->netConfigDNS->cn = $this->cn;
222 $this->netConfigDNS->save($this->dn);
224 show_ldap_error($ldap->get_error(), _("Saving generic component failed"));
226 /* Optionally execute a command after we're done */
227 $this->postcreate();
228 }
230 }
232 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
233 ?>