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;
24 var $view_logged = FALSE;
26 function componentGeneric (&$config, $dn= NULL, $parent= NULL)
27 {
28 plugin::plugin ($config, $dn, $parent);
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 }
38 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
39 /* Save dn for later references */
40 $this->orig_dn= $this->dn;
41 }
44 function execute()
45 {
46 /* Call parent execute */
47 plugin::execute();
49 if($this->is_account && !$this->view_logged){
50 $this->view_logged = TRUE;
51 new log("view","component/".get_class($this),$this->dn);
52 }
54 /* Do we represent a valid phone? */
55 if (!$this->is_account && $this->parent === NULL){
56 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
57 _("This 'dn' has no network features.")."</b>";
58 return($display);
59 }
61 /* Base select dialog */
62 $once = true;
63 foreach($_POST as $name => $value){
64 if(preg_match("/^chooseBase/",$name) && $once){
65 $once = false;
66 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
67 $this->dialog->setCurrentBase($this->base);
68 }
69 }
71 /* Dialog handling */
72 if(is_object($this->dialog)){
73 /* Must be called before save_object */
74 $this->dialog->save_object();
76 if($this->dialog->isClosed()){
77 $this->dialog = false;
78 }elseif($this->dialog->isSelected()){
80 /* A new base was selected, check if it is a valid one */
81 $tmp = $this->get_allowed_bases();
82 if(isset($tmp[$this->dialog->isSelected()])){
83 $this->base = $this->dialog->isSelected();
84 }
86 $this->dialog= false;
87 }else{
88 return($this->dialog->execute());
89 }
90 }
92 /* Fill templating stuff */
93 $smarty= get_smarty();
94 $smarty->assign("bases", $this->config->idepartments);
96 /* Set acls */
97 $tmp = $this->plInfo();
98 foreach($tmp['plProvidedAcls'] as $name => $translation){
99 $smarty->assign($name."ACL",$this->getacl($name));
100 }
102 $smarty->assign("bases", $this->get_allowed_bases());
104 /* Assign attributes */
105 foreach ($this->attributes as $attr){
106 $smarty->assign("$attr", $this->$attr);
107 }
108 $smarty->assign("base_select", $this->base);
110 /* Show main page */
111 $str = $this->netConfigDNS->execute();
112 if(is_object($this->netConfigDNS->dialog)){
113 return($str);
114 }
115 $smarty->assign("netconfig", $str);
116 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
117 }
119 function set_acl_base($base)
120 {
121 plugin::set_acl_base($base);
122 $this->netConfigDNS->set_acl_base($base);
123 }
125 function set_acl_category($cat)
126 {
127 plugin::set_acl_category($cat);
128 $this->netConfigDNS->set_acl_category($cat);
129 }
131 function remove_from_parent()
132 {
133 $ldap= $this->config->get_ldap_link();
134 $this->netConfigDNS->remove_from_parent();
135 $ldap->rmdir($this->dn);
137 new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
139 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
140 $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
142 /* Delete references to object groups */
143 $ldap->cd ($this->config->current['BASE']);
144 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
145 while ($ldap->fetch()){
146 $og= new ogroup($this->config, $ldap->getDN());
147 unset($og->member[$this->dn]);
148 $og->save ();
149 }
150 }
153 /* Save data to object */
154 function save_object()
155 {
157 /* Create a base backup and reset the
158 base directly after calling plugin::save_object();
159 Base will be set seperatly a few lines below */
160 $base_tmp = $this->base;
161 plugin::save_object();
162 $this->base = $base_tmp;
164 $this->netConfigDNS->save_object();
166 /* Set new base if allowed */
167 $tmp = $this->get_allowed_bases();
168 if(isset($_POST['base'])){
169 if(isset($tmp[$_POST['base']])){
170 $this->base= $_POST['base'];
171 }
172 }
173 }
176 /* Check supplied data */
177 function check()
178 {
179 /* Call common method to give check the hook */
180 $message= plugin::check();
181 $message= array_merge($message,$this->netConfigDNS->check());
183 $this->dn= "cn=".$this->cn.",".get_ou('componentou').$this->base;
185 if ($this->cn == "" ){
186 $message[]= _("The required field 'Component name' is not set.");
187 }
189 /* Check if given name is a valid host/dns name */
190 if(!tests::is_dns_name($this->cn)){
191 $message[] = _("Please specify a valid name for this object.");
192 }
194 /* To check for valid ip*/
195 if($this->netConfigDNS->ipHostNumber == ""){
196 $message[]= _("The required field IP address is empty.");
197 } else {
198 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
199 $message[]= _("The field IP address contains an invalid address.");
200 }
201 }
203 if ($this->orig_dn != $this->dn){
204 $ldap= $this->config->get_ldap_link();
205 $ldap->cd ($this->base);
206 $ldap->search ("(cn=".$this->cn.")", array("cn"));
207 if ($ldap->count() != 0){
208 while ($attrs= $ldap->fetch()){
209 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
210 continue;
211 }
212 if ($attrs['dn'] != $this->orig_dn){
213 $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
214 break;
215 }
216 }
217 }
218 }
220 return ($message);
221 }
224 /* Save to LDAP */
225 function save()
226 {
227 plugin::save();
229 /* Remove all empty values */
230 if ($this->orig_dn == 'new'){
231 $attrs= array();
232 foreach ($this->attrs as $key => $val){
233 if (is_array($val) && count($val) == 0){
234 continue;
235 }
236 $attrs[$key]= $val;
237 }
238 $this->attrs= $attrs;
239 }
241 /* If this is a new Object IP & Mac aren't set.
242 IP & Mac are msut attributes, so we set this values by here. */
243 if($this->orig_dn == 'new'){
244 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
245 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
246 }
248 /* Write back to ldap */
249 $ldap= $this->config->get_ldap_link();
250 if ($this->orig_dn == 'new'){
251 $ldap->cd($this->config->current['BASE']);
252 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
253 $ldap->cd($this->dn);
254 $ldap->add($this->attrs);
255 new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
256 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
258 } else {
259 if ($this->orig_dn != $this->dn){
260 $this->move($this->orig_dn, $this->dn);
261 }
263 $ldap->cd($this->dn);
264 $this->cleanup();
265 $ldap->modify ($this->attrs);
266 new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
267 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
268 }
270 $this->netConfigDNS->cn = $this->cn;
271 $this->netConfigDNS->save();
273 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
274 }
276 /* Return plugin informations for acl handling */
277 static function plInfo()
278 {
279 return (array(
280 "plShortName" => _("Generic"),
281 "plDescription" => _("Component generic"),
282 "plSelfModify" => FALSE,
283 "plDepends" => array(),
284 "plPriority" => 1,
285 "plSection" => array("administration"),
286 "plCategory" => array("component" => array("description" => _("Network device"),
287 "objectClass" => array("device", "ipHost", "ieee802Device"))),
288 "plProvidedAcls"=> array(
289 "cn" => _("Name"),
290 "base" => _("Base"),
291 "description" => _("Description"))
292 ));
293 }
295 /* Display generic part for server copy & paste */
296 function getCopyDialog()
297 {
298 $vars = array("cn");
299 $smarty = get_smarty();
300 $smarty->assign("cn" ,$this->cn);
301 $smarty->assign("object","component");
302 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
303 $ret = array();
304 $ret['string'] = $str;
305 $ret['status'] = "";
306 return($ret);
307 }
310 function saveCopyDialog()
311 {
312 if(isset($_POST['cn'])){
313 $this->cn = $_POST['cn'];
314 }
315 }
318 function PrepareForCopyPaste($source)
319 {
320 plugin::PrepareForCopyPaste($source);
321 if(isset($source['macAddress'][0])){
322 $this->netConfigDNS->macAddress = $source['macAddress'][0];
323 }
324 if(isset($source['ipHostNumber'][0])){
325 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
326 }
327 }
328 }
330 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
331 ?>