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,$this);
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()){
73 /* A new base was selected, check if it is a valid one */
74 $tmp = $this->get_allowed_bases();
75 if(isset($tmp[$this->dialog->isSelected()])){
76 $this->base = $this->dialog->isSelected();
77 }
79 $this->dialog= false;
80 }else{
81 return($this->dialog->execute());
82 }
83 }
85 /* Fill templating stuff */
86 $smarty= get_smarty();
87 $smarty->assign("bases", $this->config->idepartments);
89 /* Set acls */
90 $tmp = $this->plInfo();
91 foreach($tmp['plProvidedAcls'] as $name => $translation){
92 $smarty->assign($name."ACL",$this->getacl($name));
93 }
95 $smarty->assign("bases", $this->get_allowed_bases());
97 /* Assign attributes */
98 foreach ($this->attributes as $attr){
99 $smarty->assign("$attr", $this->$attr);
100 }
101 $smarty->assign("base_select", $this->base);
103 /* Show main page */
104 $smarty->assign("netconfig", $this->netConfigDNS->execute());
105 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
106 }
108 function set_acl_base($base)
109 {
110 plugin::set_acl_base($base);
111 $this->netConfigDNS->set_acl_base($base);
112 }
114 function set_acl_category($cat)
115 {
116 plugin::set_acl_category($cat);
117 $this->netConfigDNS->set_acl_category($cat);
118 }
120 function remove_from_parent()
121 {
122 $ldap= $this->config->get_ldap_link();
123 $this->netConfigDNS->remove_from_parent();
124 $ldap->rmdir($this->dn);
125 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
126 $this->handle_post_events("remove");
128 /* Delete references to object groups */
129 $ldap->cd ($this->config->current['BASE']);
130 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
131 while ($ldap->fetch()){
132 $og= new ogroup($this->config, $ldap->getDN());
133 unset($og->member[$this->dn]);
134 $og->save ();
135 }
136 }
139 /* Save data to object */
140 function save_object()
141 {
143 /* Create a base backup and reset the
144 base directly after calling plugin::save_object();
145 Base will be set seperatly a few lines below */
146 $base_tmp = $this->base;
147 plugin::save_object();
148 $this->base = $base_tmp;
150 $this->netConfigDNS->save_object();
152 /* Set new base if allowed */
153 $tmp = $this->get_allowed_bases();
154 if(isset($_POST['base'])){
155 if(isset($tmp[$_POST['base']])){
156 $this->base= $_POST['base'];
157 }
158 }
159 }
162 /* Check supplied data */
163 function check()
164 {
165 /* Call common method to give check the hook */
166 $message= plugin::check();
167 $message= array_merge($message,$this->netConfigDNS->check());
169 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
171 if ($this->cn == "" ){
172 $message[]= _("The required field 'Component name' is not set.");
173 }
175 /* To check for valid ip*/
176 if($this->netConfigDNS->ipHostNumber == ""){
177 $message[]= _("The required field IP address is empty.");
178 } else {
179 if (!is_ip($this->netConfigDNS->ipHostNumber)){
180 $message[]= _("The field IP address contains an invalid address.");
181 }
182 }
184 /* Set new acl base */
185 if($this->dn == "new") {
186 $this->set_acl_base($this->base);
187 }
189 $ui= get_userinfo();
190 if ($this->dn == "new" && !$this->acl_is_createable()){
191 $message[]= _("You have no permissions to create a component on this 'Base'.");
192 }
194 if ($this->orig_dn != $this->dn){
195 $ldap= $this->config->get_ldap_link();
196 $ldap->cd ($this->base);
197 $ldap->search ("(cn=".$this->cn.")", array("cn"));
198 if ($ldap->count() != 0){
199 while ($attrs= $ldap->fetch()){
200 if ($attrs['dn'] != $this->orig_dn){
201 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
202 break;
203 }
204 }
205 }
206 }
208 return ($message);
209 }
212 /* Save to LDAP */
213 function save()
214 {
215 plugin::save();
217 /* Remove all empty values */
218 if ($this->orig_dn == 'new'){
219 $attrs= array();
220 foreach ($this->attrs as $key => $val){
221 if (is_array($val) && count($val) == 0){
222 continue;
223 }
224 $attrs[$key]= $val;
225 }
226 $this->attrs= $attrs;
227 }
229 /* If this is a new Object IP & Mac aren't set.
230 IP & Mac are msut attributes, so we set this values by here. */
231 if($this->orig_dn == 'new'){
232 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
233 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
234 }
236 /* Write back to ldap */
237 $ldap= $this->config->get_ldap_link();
238 if ($this->orig_dn == 'new'){
239 $ldap->cd($this->config->current['BASE']);
240 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
241 $ldap->cd($this->dn);
242 $ldap->add($this->attrs);
243 $this->handle_post_events("add");
244 } else {
245 if ($this->orig_dn != $this->dn){
246 $this->move($this->orig_dn, $this->dn);
247 }
249 $ldap->cd($this->dn);
250 $this->cleanup();
251 $ldap->modify ($this->attrs);
253 $this->handle_post_events("modify");
254 }
256 $this->netConfigDNS->cn = $this->cn;
257 $this->netConfigDNS->save($this->dn);
259 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
261 /* Optionally execute a command after we're done */
262 $this->postcreate();
263 }
265 /* Return plugin informations for acl handling */
266 function plInfo()
267 {
268 return (array(
269 "plShortName" => _("Generic"),
270 "plDescription" => _("Component generic"),
271 "plSelfModify" => FALSE,
272 "plDepends" => array(),
273 "plPriority" => 1,
274 "plSection" => array("administration"),
275 "plCategory" => array("component" => array("description" => _("Network device"),
276 "objectClass" => array("device", "ipHost", "ieee802Device"))),
277 "plProvidedAcls"=> array(
278 "cn" => _("Name"),
279 "base" => _("Base"),
280 "description" => _("Description"))
281 ));
282 }
285 }
287 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
288 ?>