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 $this->netConfigDNS->acl = $this->acl;
40 /* Save dn for later references */
41 $this->orig_dn= $this->dn;
42 }
44 function execute()
45 {
46 $this->netConfigDNS->acl = $this->acl;
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);
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()){
76 $this->base = $this->dialog->isSelected();
77 $this->dialog= false;
78 }else{
79 return($this->dialog->execute());
80 }
81 }
83 /* Fill templating stuff */
84 $smarty= get_smarty();
85 $smarty->assign("bases", $this->config->idepartments);
87 /* Assign attributes */
88 foreach ($this->attributes as $attr){
89 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
90 $smarty->assign("$attr", $this->$attr);
91 }
92 $smarty->assign("base_select", $this->base);
93 $smarty->assign("baseACL", chkacl($this->acl,"base"));
95 /* Show main page */
96 $this->netConfigDNS->cn= $this->cn;
97 $smarty->assign("netconfig", $this->netConfigDNS->execute());
98 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
99 }
101 function remove_from_parent()
102 {
103 $this->netConfigDNS->acl = $this->acl;
104 $ldap= $this->config->get_ldap_link();
105 $this->netConfigDNS->remove_from_parent();
106 $ldap->rmdir($this->dn);
107 show_ldap_error($ldap->get_error(), _("Removing generic component failed"));
108 $this->handle_post_events("remove");
110 /* Delete references to object groups */
111 $ldap->cd ($this->config->current['BASE']);
112 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
113 while ($ldap->fetch()){
114 $og= new ogroup($this->config, $ldap->getDN());
115 unset($og->member[$this->dn]);
116 $og->save ();
117 }
118 }
121 /* Save data to object */
122 function save_object()
123 {
124 plugin::save_object();
126 $this->netConfigDNS->save_object();
128 /* Save base, since this is no LDAP attribute */
129 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
130 $this->base= $_POST['base'];
131 }
132 }
135 /* Check supplied data */
136 function check()
137 {
138 /* Call common method to give check the hook */
139 $message= plugin::check();
140 $message= array_merge($message,$this->netConfigDNS->check());
142 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
144 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
145 $message[]= _("The required field 'Component name' is not set.");
146 }
148 /* To check for valid ip*/
149 if($this->netConfigDNS->ipHostNumber == ""){
150 $message[]= _("The required field IP address is empty.");
151 } else {
152 if (!is_ip($this->netConfigDNS->ipHostNumber)){
153 $message[]= _("The field IP address contains an invalid address.");
154 }
155 }
157 $ui= get_userinfo();
158 $acl= get_permissions ($this->dn, $ui->subtreeACL);
159 $acl= get_module_permission($acl, "component", $this->dn);
160 if (chkacl($acl, "create") != ""){
161 $message[]= _("You have no permissions to create a component on this 'Base'.");
162 }
164 if ($this->orig_dn != $this->dn){
165 $ldap= $this->config->get_ldap_link();
166 $ldap->cd ($this->base);
167 $ldap->search ("(cn=".$this->cn.")", array("cn"));
168 if ($ldap->count() != 0){
169 while ($attrs= $ldap->fetch()){
170 if ($attrs['dn'] != $this->orig_dn){
171 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
172 break;
173 }
174 }
175 }
176 }
178 return ($message);
179 }
182 /* Save to LDAP */
183 function save()
184 {
185 $this->netConfigDNS->acl = $this->acl;
186 plugin::save();
188 /* Remove all empty values */
189 if ($this->orig_dn == 'new'){
190 $attrs= array();
191 foreach ($this->attrs as $key => $val){
192 if (is_array($val) && count($val) == 0){
193 continue;
194 }
195 $attrs[$key]= $val;
196 }
197 $this->attrs= $attrs;
198 }
200 /* If this is a new Object IP & Mac aren't set.
201 IP & Mac are msut attributes, so we set this values by here. */
202 if($this->orig_dn == 'new'){
203 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
204 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
205 }
207 /* Write back to ldap */
208 $ldap= $this->config->get_ldap_link();
209 if ($this->orig_dn == 'new'){
210 $ldap->cd($this->config->current['BASE']);
211 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
212 $ldap->cd($this->dn);
213 $ldap->add($this->attrs);
214 $this->handle_post_events("add");
215 } else {
216 if ($this->orig_dn != $this->dn){
217 $this->move($this->orig_dn, $this->dn);
218 }
220 $ldap->cd($this->dn);
221 $this->cleanup();
222 $ldap->modify ($this->attrs);
224 $this->handle_post_events("modify");
225 }
227 $this->netConfigDNS->cn = $this->cn;
228 $this->netConfigDNS->save($this->dn);
230 show_ldap_error($ldap->get_error(), _("Saving generic component failed"));
232 /* Optionally execute a command after we're done */
233 $this->postcreate();
234 }
236 }
238 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
239 ?>