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 /* Do we represent a valid phone? */
46 if (!$this->is_account && $this->parent == NULL){
47 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
48 _("This 'dn' has no network features.")."</b>";
49 return($display);
50 }
52 /* Fill templating stuff */
53 $smarty= get_smarty();
54 $smarty->assign("bases", $this->config->idepartments);
56 /* Assign attributes */
57 foreach ($this->attributes as $attr){
58 $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
59 $smarty->assign("$attr", $this->$attr);
60 }
61 $smarty->assign("base_select", $this->base);
63 /* Show Asterisk for required attribute ipHostNumber and macAddress */
64 $smarty->assign("staticAddress", "<font class=\"must\">*</font>");
66 /* Show main page */
67 $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
68 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
69 }
71 function remove_from_parent()
72 {
73 $ldap= $this->config->get_ldap_link();
74 $ldap->rmdir($this->dn);
75 show_ldap_error($ldap->get_error());
76 $this->handle_post_events("remove");
78 /* Delete references to object groups */
79 $ldap->cd ($this->config->current['BASE']);
80 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
81 while ($ldap->fetch()){
82 $og= new ogroup($this->config, $ldap->getDN());
83 unset($og->member[$this->dn]);
84 $og->save ();
85 }
87 }
90 /* Save data to object */
91 function save_object()
92 {
93 plugin::save_object();
95 /* Save base, since this is no LDAP attribute */
96 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
97 $this->base= $_POST['base'];
98 }
99 }
102 /* Check supplied data */
103 function check()
104 {
105 $message= array();
106 $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
108 /* must: cn, macAddress, ipHostNumber */
109 if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
110 $message[]= _("The required field 'Component name' is not set.");
111 }
112 if ($this->macAddress == "" && chkacl ($this->acl, "macAddress") == ""){
113 $message[]= _("The required field 'MAC-address' is not set.");
114 }
115 if ($this->ipHostNumber == "" && chkacl ($this->acl, "ipHostNumber") == ""){
116 $message[]= _("The required field 'IP-address' is not set.");
117 }
119 $ui= get_userinfo();
120 $acl= get_permissions ($this->dn, $ui->subtreeACL);
121 $acl= get_module_permission($acl, "component", $this->dn);
122 if (chkacl($acl, "create") != ""){
123 $message[]= _("You have no permissions to create a component on this 'Base'.");
124 }
126 if ($this->orig_dn != $this->dn){
127 $ldap= $this->config->get_ldap_link();
128 $ldap->cd ($this->base);
129 $ldap->search ("(cn=".$this->cn.")", array("cn"));
130 if ($ldap->count() != 0){
131 while ($attrs= $ldap->fetch()){
132 if ($attrs['dn'] != $this->orig_dn){
133 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
134 break;
135 }
136 }
137 }
138 }
140 return ($message);
141 }
144 /* Save to LDAP */
145 function save()
146 {
147 plugin::save();
149 /* Remove all empty values */
150 if ($this->orig_dn == 'new'){
151 $attrs= array();
152 foreach ($this->attrs as $key => $val){
153 if (is_array($val) && count($val) == 0){
154 continue;
155 }
156 $attrs[$key]= $val;
157 }
158 $this->attrs= $attrs;
159 }
161 /* Write back to ldap */
162 $ldap= $this->config->get_ldap_link();
163 if ($this->orig_dn == 'new'){
164 $ldap->cd($this->config->current['BASE']);
165 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
166 $ldap->cd($this->dn);
167 $ldap->add($this->attrs);
168 $this->handle_post_events("add");
169 } else {
170 if ($this->orig_dn != $this->dn){
171 $this->move($this->orig_dn, $this->dn);
172 }
174 $ldap->cd($this->dn);
175 $ldap->modify($this->attrs);
176 $this->handle_post_events("modify");
177 }
178 show_ldap_error($ldap->get_error());
180 /* Optionally execute a command after we're done */
181 $this->postcreate();
182 }
184 }
186 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
187 ?>