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= "";
34 var $orig_cn= "";
35 var $orig_base= "";
37 /* attribute list for save action */
38 var $attributes= array("cn", "description");
39 var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
40 var $netConfigDNS;
41 var $view_logged = FALSE;
42 var $baseSelector;
44 function componentGeneric (&$config, $dn= NULL, $parent= NULL)
45 {
46 plugin::plugin ($config, $dn, $parent);
48 /* Set base */
49 if ($this->dn == "new"){
50 $ui= get_userinfo();
51 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
52 $this->cn= "";
53 } else {
54 $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("componentRDN"), '/')."/i", "", $this->dn);
55 }
56 $this->netConfigDNS = new termDNS($this->config,$this,$this->objectclasses, true);
57 /* Save dn for later references */
58 $this->orig_dn= $this->dn;
60 $this->orig_base = $this->base;
61 $this->orig_cn = $this->cn;
63 /* Instanciate base selector */
64 $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
65 $this->baseSelector->setSubmitButton(false);
66 $this->baseSelector->setHeight(300);
67 $this->baseSelector->update(true);
68 }
71 function execute()
72 {
73 /* Call parent execute */
74 plugin::execute();
76 if($this->is_account && !$this->view_logged){
77 $this->view_logged = TRUE;
78 new log("view","component/".get_class($this),$this->dn);
79 }
81 /* Do we represent a valid phone? */
82 if (!$this->is_account && $this->parent === NULL){
83 $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle> <b>".
84 msgPool::noValidExtension(_("component"))."</b>";
85 return($display);
86 }
88 /* Fill templating stuff */
89 $smarty= get_smarty();
90 $smarty->assign("usePrototype", "true");
92 /* Set acls */
93 $tmp = $this->plInfo();
94 foreach($tmp['plProvidedAcls'] as $name => $translation){
95 $smarty->assign($name."ACL",$this->getacl($name));
96 }
98 /* Assign attributes */
99 foreach ($this->attributes as $attr){
100 $smarty->assign("$attr", $this->$attr);
101 }
102 $smarty->assign("base", $this->baseSelector->render());
104 /* Show main page */
105 $str = $this->netConfigDNS->execute();
106 if(is_object($this->netConfigDNS->dialog)){
107 return($str);
108 }
109 $smarty->assign("netconfig", $str);
110 return($smarty->fetch (get_template_path('component.tpl', TRUE)));
111 }
113 function set_acl_base($base)
114 {
115 plugin::set_acl_base($base);
116 $this->netConfigDNS->set_acl_base($base);
117 }
119 function set_acl_category($cat)
120 {
121 plugin::set_acl_category($cat);
122 $this->netConfigDNS->set_acl_category($cat);
123 }
125 function remove_from_parent()
126 {
127 $ldap= $this->config->get_ldap_link();
128 $this->netConfigDNS->remove_from_parent();
129 $ldap->rmdir($this->dn);
131 new log("remove","component/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
133 if (!$ldap->success()){
134 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
135 }
137 $this->handle_post_events(array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
139 /* Delete references to object groups */
140 $ldap->cd ($this->config->current['BASE']);
141 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
142 while ($ldap->fetch()){
143 $og= new ogroup($this->config, $ldap->getDN());
144 unset($og->member[$this->dn]);
145 $og->save ();
146 }
147 }
150 /* Save data to object */
151 function save_object()
152 {
154 /* Create a base backup and reset the
155 base directly after calling plugin::save_object();
156 Base will be set seperatly a few lines below */
157 $base_tmp = $this->base;
158 plugin::save_object();
159 $this->base = $base_tmp;
161 $this->netConfigDNS->save_object();
163 /* Refresh base */
164 if ($this->acl_is_moveable($this->base)){
165 if (!$this->baseSelector->update()) {
166 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
167 }
168 if ($this->base != $this->baseSelector->getBase()) {
169 $this->base= $this->baseSelector->getBase();
170 $this->is_modified= TRUE;
171 }
172 }
174 }
177 /* Check supplied data */
178 function check()
179 {
180 /* Call common method to give check the hook */
181 $message= plugin::check();
182 $message= array_merge($message,$this->netConfigDNS->check());
184 $this->dn= "cn=".$this->cn.",".get_ou('componentRDN').$this->base;
186 if ($this->cn == "" ){
187 $message[]= msgPool::required(_("Component name"));
188 }
190 /* Check if given name is a valid host/dns name */
191 if(!tests::is_dns_name($this->cn)){
192 $message[]= msgPool::invalid(_("Component name"), $this->cn, "/[a-z0-9\.\-]/i");
193 }
195 /* To check for valid ip*/
196 if($this->netConfigDNS->ipHostNumber == ""){
197 $message[]= msgPool::required(_("IP address"));
198 } else {
199 if (!tests::is_ip($this->netConfigDNS->ipHostNumber)){
200 $message[]= msgPool::invalid(_("IP address"), "", "", "192.168.1.2");
201 }
202 }
204 if ($this->orig_dn != $this->dn){
205 $ldap= $this->config->get_ldap_link();
206 $ldap->cd ($this->base);
207 $ldap->search ("(cn=".$this->cn.")", array("cn"));
208 if ($ldap->count() != 0){
209 while ($attrs= $ldap->fetch()){
210 if(preg_match("/cn=dhcp,/",$attrs['dn'])){
211 continue;
212 }
213 if ($attrs['dn'] != $this->orig_dn && preg_match("/".preg_quote(get_ou("componentRDN"), '/')."/i",$attrs['dn'])){
214 $message[]= msgPool::duplicated(_("Component name"));
215 break;
216 }
217 }
218 }
219 }
221 /* Check if we are allowed to create or move this object
222 */
223 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
224 $message[] = msgPool::permCreate();
225 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
226 $message[] = msgPool::permMove();
227 }
229 // Check if a wrong base was supplied
230 if(!$this->baseSelector->checkLastBaseUpdate()){
231 $message[]= msgPool::check_base();;
232 }
234 return ($message);
235 }
238 /* Save to LDAP */
239 function save()
240 {
241 plugin::save();
243 /* Remove all empty values */
244 if ($this->orig_dn == 'new'){
245 $attrs= array();
246 foreach ($this->attrs as $key => $val){
247 if (is_array($val) && count($val) == 0){
248 continue;
249 }
250 $attrs[$key]= $val;
251 }
252 $this->attrs= $attrs;
253 }
255 /* If this is a new Object IP & Mac aren't set.
256 IP & Mac are msut attributes, so we set this values by here. */
257 if($this->orig_dn == 'new'){
258 $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
259 $this->attrs['macAddress'] = $this->netConfigDNS->macAddress;
260 }
262 /* Write back to ldap */
263 $ldap= $this->config->get_ldap_link();
264 if ($this->orig_dn == 'new'){
265 $ldap->cd($this->config->current['BASE']);
266 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
267 $ldap->cd($this->dn);
268 $ldap->add($this->attrs);
269 new log("create","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
270 $this->handle_post_events("add",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
272 } else {
273 $ldap->cd($this->dn);
274 $this->cleanup();
275 $ldap->modify ($this->attrs);
276 new log("modify","component/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
277 $this->handle_post_events("modify",array("macAddress" => $this->netConfigDNS->macAddress,"ipHostNumber" => $this->netConfigDNS->ipHostNumber));
278 }
280 $this->netConfigDNS->cn = $this->cn;
281 $this->netConfigDNS->save();
283 if (!$ldap->success()){
284 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
285 }
286 }
288 /* Return plugin informations for acl handling */
289 static function plInfo()
290 {
291 return (array(
292 "plShortName" => _("Generic"),
293 "plDescription" => _("Component generic"),
294 "plSelfModify" => FALSE,
295 "plDepends" => array(),
296 "plPriority" => 1,
297 "plSection" => array("administration"),
298 "plCategory" => array("component" => array("description" => _("Network device"),
299 "objectClass" => array("device", "ipHost", "ieee802Device"))),
300 "plProvidedAcls"=> array(
301 "cn" => _("Name"),
302 "base" => _("Base"),
303 "description" => _("Description"),
304 "userPassword" => _("Root password"))
305 ));
306 }
308 /* Display generic part for server copy & paste */
309 function getCopyDialog()
310 {
311 $vars = array("cn");
312 $smarty = get_smarty();
313 $smarty->assign("cn" ,$this->cn);
314 $smarty->assign("object","component");
315 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE));
316 $ret = array();
317 $ret['string'] = $str;
318 $ret['status'] = "";
319 return($ret);
320 }
323 function saveCopyDialog()
324 {
325 if(isset($_POST['cn'])){
326 $this->cn = $_POST['cn'];
327 }
328 }
331 function PrepareForCopyPaste($source)
332 {
333 plugin::PrepareForCopyPaste($source);
334 if(isset($source['macAddress'][0])){
335 $this->netConfigDNS->macAddress = $source['macAddress'][0];
336 }
337 if(isset($source['ipHostNumber'][0])){
338 $this->netConfigDNS->ipHostNumber = $source['ipHostNumber'][0];
339 }
340 }
343 function is_modal_dialog()
344 {
345 return((isset($this->dialog) && $this->dialog) || (isset($this->netConfigDNS->dialog) && $this->netConfigDNS->dialog));
346 }
348 }
350 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
351 ?>