1 <?php
2 /*
3 * This code is part of GOsa (http://www.gosa-project.org)
4 * Copyright (C) 2003-2008 GONICUS GmbH
5 *
6 * ID: $$Id$$
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
23 class componentGeneric extends plugin
24 {
25 /* Generic terminal attributes */
26 var $interfaces= array();
27 var $ignore_account= TRUE;
29 /* Needed values and lists */
30 var $base= "";
31 var $cn= "";
32 var $description= "";
33 var $orig_dn= "";
35 /* attribute list for save action */
36 var $attributes= array("cn", "description");
37 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
38 var $netConfigDNS;
39 var $view_logged = FALSE;
41 function componentGeneric (&$config, $dn= NULL, $parent= NULL)
42 {
43 plugin::plugin ($config, $dn, $parent);
45 /* Set base */
46 if ($this->dn == "new"){
47 $ui= get_userinfo();
48 $this->base= dn2base($ui->dn);
49 $this->cn= "";
50 } else {
51 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
52 }
53 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
54 /* Save dn for later references */
55 $this->orig_dn= $this->dn;
56 }
59 function execute()
60 {
61 /* Call parent execute */
62 plugin::execute();
64 if($this->is_account && !$this->view_logged){
65 $this->view_logged = TRUE;
66 new log("view","component/".get_class($this),$this->dn);
67 }
69 /* Do we represent a valid phone? */
70 if (!$this->is_account && $this->parent === NULL){
71 $display= "<img alt=\"\" src=\"images/stop.png\" align=middle> <b>".
72 _("This 'dn' has no network features.")."</b>";
73 return($display);
74 }
76 /* Base select dialog */
77 $once = true;
78 foreach($_POST as $name => $value){
79 if(preg_match("/^chooseBase/",$name) && $once){
80 $once = false;
81 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
82 $this->dialog->setCurrentBase($this->base);
83 }
84 }
86 /* Dialog handling */
87 if(is_object($this->dialog)){
88 /* Must be called before save_object */
89 $this->dialog->save_object();
91 if($this->dialog->isClosed()){
92 $this->dialog = false;
93 }elseif($this->dialog->isSelected()){
95 /* A new base was selected, check if it is a valid one */
96 $tmp = $this->get_allowed_bases();
97 if(isset($tmp[$this->dialog->isSelected()])){
98 $this->base = $this->dialog->isSelected();
99 }
101 $this->dialog= false;
102 }else{
103 return($this->dialog->execute());
104 }
105 }
107 /* Fill templating stuff */
108 $smarty= get_smarty();
109 $smarty->assign("bases", $this->config->idepartments);
111 /* Set acls */
112 $tmp = $this->plInfo();
113 foreach($tmp['plProvidedAcls'] as $name => $translation){
114 $smarty->assign($name."ACL",$this->getacl($name));
115 }
117 $smarty->assign("bases", $this->get_allowed_bases());
119 /* Assign attributes */
120 foreach ($this->attributes as $attr){
121 $smarty->assign("$attr", $this->$attr);
122 }
123 $smarty->assign("base_select", $this->base);
125 /* Show main page */
126 $str = $this->netConfigDNS->execute();
127 if(is_object($this->netConfigDNS->dialog)){
128 return($str);
129 }
130 $smarty->assign("netconfig", $str);
131 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
132 }
134 function set_acl_base($base)
135 {
136 plugin::set_acl_base($base);
137 $this->netConfigDNS->set_acl_base($base);
138 }
140 function set_acl_category($cat)
141 {
142 plugin::set_acl_category($cat);
143 $this->netConfigDNS->set_acl_category($cat);
144 }
146 function remove_from_parent()
147 {
148 $ldap= $this->config->get_ldap_link();
149 $this->netConfigDNS->remove_from_parent();
150 $ldap->rmdir($this->dn);
152 new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
154 show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
155 $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
157 /* Delete references to object groups */
158 $ldap->cd ($this->config->current['BASE']);
159 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
160 while ($ldap->fetch()){
161 $og= new ogroup($this->config, $ldap->getDN());
162 unset($og->member[$this->dn]);
163 $og->save ();
164 }
165 }
168 /* Save data to object */
169 function save_object()
170 {
172 /* Create a base backup and reset the
173 base directly after calling plugin::save_object();
174 Base will be set seperatly a few lines below */
175 $base_tmp = $this->base;
176 plugin::save_object();
177 $this->base = $base_tmp;
179 $this->netConfigDNS->save_object();
181 /* Set new base if allowed */
182 $tmp = $this->get_allowed_bases();
183 if(isset($_POST['base'])){
184 if(isset($tmp[$_POST['base']])){
185 $this->base= $_POST['base'];
186 }
187 }
188 }
191 /* Check supplied data */
192 function check()
193 {
194 /* Call common method to give check the hook */
195 $message= plugin::check();
196 $message= array_merge($message,$this->netConfigDNS->check());
198 $this->dn= "cn=".$this->cn.",".get_ou('componentou').$this->base;
200 if ($this->cn == "" ){
201 $message[]= msgPool::required(_("Component name"));
202 }
204 /* Check if given name is a valid host/dns name */
205 if(!tests::is_dns_name($this->cn)){
206 $message[]= msgPool::invalid(_("Component name"), $this->cn, "/[a-z0-9\.\-]/i");
207 }
209 /* To check for valid ip*/
210 if($this->netConfigDNS->ipHostNumber == ""){
211 $message[]= msgPool::required(_("IP address"));
212 } else {
213 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
214 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.2");
215 }
216 }
218 if ($this->orig_dn != $this->dn){
219 $ldap= $this->config->get_ldap_link();
220 $ldap->cd ($this->base);
221 $ldap->search ("(cn=".$this->cn.")", array("cn"));
222 if ($ldap->count() != 0){
223 while ($attrs= $ldap->fetch()){
224 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
225 continue;
226 }
227 if ($attrs['dn'] != $this->orig_dn){
228 $message[]= msgPool::duplicated(_("Component name"));
229 break;
230 }
231 }
232 }
233 }
235 return ($message);
236 }
239 /* Save to LDAP */
240 function save()
241 {
242 plugin::save();
244 /* Remove all empty values */
245 if ($this->orig_dn == 'new'){
246 $attrs= array();
247 foreach ($this->attrs as $key => $val){
248 if (is_array($val) && count($val) == 0){
249 continue;
250 }
251 $attrs[$key]= $val;
252 }
253 $this->attrs= $attrs;
254 }
256 /* If this is a new Object IP & Mac aren't set.
257 IP & Mac are msut attributes, so we set this values by here. */
258 if($this->orig_dn == 'new'){
259 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
260 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
261 }
263 /* Write back to ldap */
264 $ldap= $this->config->get_ldap_link();
265 if ($this->orig_dn == 'new'){
266 $ldap->cd($this->config->current['BASE']);
267 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
268 $ldap->cd($this->dn);
269 $ldap->add($this->attrs);
270 new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
271 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
273 } else {
274 if ($this->orig_dn != $this->dn){
275 $this->move($this->orig_dn, $this->dn);
276 }
278 $ldap->cd($this->dn);
279 $this->cleanup();
280 $ldap->modify ($this->attrs);
281 new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
282 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
283 }
285 $this->netConfigDNS->cn = $this->cn;
286 $this->netConfigDNS->save();
288 show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
289 }
291 /* Return plugin informations for acl handling */
292 static function plInfo()
293 {
294 return (array(
295 "plShortName" => _("Generic"),
296 "plDescription" => _("Component generic"),
297 "plSelfModify" => FALSE,
298 "plDepends" => array(),
299 "plPriority" => 1,
300 "plSection" => array("administration"),
301 "plCategory" => array("component" => array("description" => _("Network device"),
302 "objectClass" => array("device", "ipHost", "ieee802Device"))),
303 "plProvidedAcls"=> array(
304 "cn" => _("Name"),
305 "base" => _("Base"),
306 "description" => _("Description"))
307 ));
308 }
310 /* Display generic part for server copy & paste */
311 function getCopyDialog()
312 {
313 $vars = array("cn");
314 $smarty = get_smarty();
315 $smarty->assign("cn" ,$this->cn);
316 $smarty->assign("object","component");
317 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
318 $ret = array();
319 $ret['string'] = $str;
320 $ret['status'] = "";
321 return($ret);
322 }
325 function saveCopyDialog()
326 {
327 if(isset($_POST['cn'])){
328 $this->cn = $_POST['cn'];
329 }
330 }
333 function PrepareForCopyPaste($source)
334 {
335 plugin::PrepareForCopyPaste($source);
336 if(isset($source['macAddress'][0])){
337 $this->netConfigDNS->macAddress = $source['macAddress'][0];
338 }
339 if(isset($source['ipHostNumber'][0])){
340 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
341 }
342 }
343 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>