eec9adf02ce43d5b9d99100d8578e65fd61ddef3
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 department extends plugin
24 {
25 /* department attributes */
26 var $ou= "";
27 var $description= "";
28 var $base= "";
29 var $st= "";
30 var $l= "";
31 var $postalAddress= "";
32 var $businessCategory= "";
33 var $telephoneNumber= "";
34 var $facsimileTelephoneNumber= "";
35 var $is_administrational_unit= false;
36 var $gosaUnitTag= "";
37 var $view_logged = FALSE;
39 var $type ="ou";
40 var $namingAttr = "ou";
42 /* Headpage attributes */
43 var $last_dep_sorting= "invalid";
44 var $departments= array();
45 var $must_be_tagged = false;
47 /* attribute list for save action */
48 var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
49 "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
50 var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
51 var $initially_was_tagged = false;
53 var $orig_base = "";
54 var $orig_ou = "";
56 function department (&$config, $dn)
57 {
59 plugin::plugin($config, $dn);
60 $this->is_account= TRUE;
61 $this->ui= get_userinfo();
62 $this->dn= $dn;
63 $this->orig_dn= $dn;
65 /* Save current naming attribuet
66 */
67 $nA = $this->namingAttr;
68 $orig_nA = "orig_".$nA;
69 $this->$orig_nA = $this->$nA;
71 $this->config= $config;
73 /* Set base */
74 if ($this->dn == "new"){
75 $ui= get_userinfo();
76 if(session::is_set('CurrentMainBase')){
77 $this->base = session::get('CurrentMainBase');
78 }else{
79 $this->base= dn2base($ui->dn);
80 }
81 } else {
82 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
83 }
85 $this->orig_base = $this->base;
87 /* Is administrational Unit? */
88 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
89 $this->is_administrational_unit= true;
90 $this->initially_was_tagged = true;
91 }
92 }
94 function execute()
95 {
96 /* Call parent execute */
97 plugin::execute();
99 /* Log view */
100 if($this->is_account && !$this->view_logged){
101 $this->view_logged = TRUE;
102 new log("view","department/".get_class($this),$this->dn);
103 }
105 /* Reload departments */
106 $this->config->get_departments($this->dn);
107 $this->config->make_idepartments();
108 $smarty= get_smarty();
110 /* Hide base selector, if this object represents the base itself
111 */
112 $smarty->assign("hide_base", FALSE);
113 if($this->dn == $this->config->current['BASE']){
114 $smarty->assign("hide_base", TRUE);
115 }
117 $tmp = $this->plInfo();
118 foreach($tmp['plProvidedAcls'] as $name => $translation){
119 $smarty->assign($name."ACL",$this->getacl($name));
120 }
122 /* Base select dialog */
123 $once = true;
124 foreach($_POST as $name => $value){
125 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
126 $once = false;
127 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
128 $this->dialog->setCurrentBase($this->base);
129 }
130 }
132 /* Dialog handling */
133 if(is_object($this->dialog)){
134 /* Must be called before save_object */
135 $this->dialog->save_object();
137 if($this->dialog->isClosed()){
138 $this->dialog = false;
139 }elseif($this->dialog->isSelected()){
141 /* A new base was selected, check if it is a valid one */
142 $tmp = $this->get_allowed_bases();
143 if(isset($tmp[$this->dialog->isSelected()])){
144 $this->base = $this->dialog->isSelected();
145 }
147 $this->dialog= false;
148 }else{
149 return($this->dialog->execute());
150 }
151 }
153 /* Hide all departments, that are subtrees of this department */
154 $bases = $this->get_allowed_bases();
155 if(($this->dn == "new")||($this->dn == "")){
156 $tmp = $bases;
157 }else{
158 $tmp = array();
159 foreach($bases as $dn=>$base){
160 $fixed = str_replace("/","\\",$this->dn);
161 /* Only attach departments which are not a subtree of this one */
162 if(!preg_match("/".$fixed."/",$dn)){
163 $tmp[$dn]=$base;
164 }
165 }
166 }
167 $smarty->assign("bases", $tmp);
169 foreach ($this->attributes as $val){
170 $smarty->assign("$val", $this->$val);
171 }
172 $smarty->assign("base_select", $this->base);
174 /* Set admin unit flag */
175 if ($this->is_administrational_unit) {
176 $smarty->assign("unitTag", "checked");
177 } else {
178 $smarty->assign("unitTag", "");
179 }
181 $smarty->assign("dep_type",$this->type);
184 $dep_types = departmentManagement::get_support_departments();
185 $tpl ="";
186 foreach($dep_types as $key => $data){
187 if($data['ATTR'] == $this->type){
188 $tpl = $data['TPL'];
189 break;
190 }
191 }
192 if($tpl == "") {
193 trigger_error("No template specified for container type '".$this->type."', please update epartmentManagement::get_support_departments().");
194 $tpl = "generic.tpl";
195 }
196 return($smarty->fetch (get_template_path($tpl, TRUE)));
197 }
199 function clear_fields()
200 {
201 $this->dn = "";
202 $this->base = "";
204 foreach ($this->attributes as $val){
205 $this->$val= "";
206 }
207 }
209 function remove_from_parent()
210 {
211 $ldap= $this->config->get_ldap_link();
212 $ldap->cd ($this->dn);
213 $ldap->rmdir_recursive($this->dn);
214 new log("remove","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
215 if (!$ldap->success()){
216 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
217 }
219 /* Optionally execute a command after we're done */
220 $this->handle_post_events('remove');
221 }
223 function must_be_tagged()
224 {
225 return $this->must_be_tagged;
226 }
228 /* Save data to object */
229 function save_object()
230 {
231 if (isset($_POST['dep_generic_posted'])){
233 /* Create a base backup and reset the
234 base directly after calling plugin::save_object();
235 Base will be set seperatly a few lines below */
236 $base_tmp = $this->base;
237 plugin::save_object();
238 $this->base = $base_tmp;
240 /* Set new base if allowed */
241 $tmp = $this->get_allowed_bases();
242 if(isset($_POST['base'])){
243 if(isset($tmp[$_POST['base']])){
244 $this->base= $_POST['base'];
245 }
246 }
248 /* Save tagging flag */
249 if ($this->acl_is_writeable("unitTag")){
250 if (isset($_POST['unitTag'])){
251 $this->is_administrational_unit= true;
252 } else {
253 $this->is_administrational_unit= false;
254 }
255 }
256 }
257 }
260 /* Check values */
261 function check()
262 {
263 /* Call common method to give check the hook */
264 $message= plugin::check();
266 /* Check for presence of this department */
267 $ldap= $this->config->get_ldap_link();
268 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
269 if ($this->orig_dn == "new" && $ldap->count()){
270 $message[]= msgPool::duplicated(_("Name"));
271 } elseif ($this->orig_dn != $this->dn && $ldap->count()){
272 $message[]= msgPool::duplicated(_("Name"));
273 }
275 /* All required fields are set? */
276 if ($this->ou == ""){
277 $message[]= msgPool::required(_("Name"));
278 }
279 if ($this->description == ""){
280 $message[]= msgPool::required(_("Description"));
281 }
283 if(tests::is_department_name_reserved($this->ou,$this->base)){
284 $message[]= msgPool::reserved(_("Name"));
285 }
287 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
288 $message[]= msgPool::invalid(_("Name"), $this->ou, "/[^#+:=>\\\\\/]/");
289 }
290 if (!tests::is_phone_nr($this->telephoneNumber)){
291 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
292 }
293 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
294 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
295 }
297 /* Check if we are allowed to create or move this object
298 */
299 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
300 $message[] = msgPool::permCreate();
301 }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
302 $message[] = msgPool::permMove();
303 }
305 return $message;
306 }
309 /* Save to LDAP */
310 function save()
311 {
312 $ldap= $this->config->get_ldap_link();
314 /* Ensure that ou is saved too, it is required by objectClass gosaDepartment
315 */
316 $nA = $this->namingAttr;
317 $this->ou = $this->$nA;
319 /* Add tag objects if needed */
320 if ($this->is_administrational_unit){
322 /* If this wasn't tagged before add oc an reset unit tag */
323 if(!$this->initially_was_tagged){
324 $this->objectclasses[]= "gosaAdministrativeUnit";
325 $this->gosaUnitTag= "";
327 /* It seams that this method is called twice,
328 set this to true. to avoid adding this oc twice */
329 $this->initially_was_tagged = true;
330 }
332 if ($this->gosaUnitTag == ""){
334 /* It's unlikely, but check if already used... */
335 $try= 5;
336 $ldap->cd($this->config->current['BASE']);
337 while ($try--){
339 /* Generate microtime stamp as tag */
340 list($usec, $sec)= explode(" ", microtime());
341 $time_stamp= preg_replace("/\./", "", $sec.$usec);
343 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
344 if ($ldap->count() == 0){
345 break;
346 }
347 }
348 if($try == 0) {
349 msg_dialog::display(_("Fatal error"), _("Cannot find an unused tag for this administrative unit!"), WARNING_DIALOG);
350 return;
351 }
352 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
353 }
354 }
355 $this->skipTagging = TRUE;
356 plugin::save();
359 /* Remove tag information if needed */
360 if (!$this->is_administrational_unit && $this->initially_was_tagged){
361 $tmp= array();
363 /* Remove gosaAdministrativeUnit from this plugin */
364 $has_unit_tag= false;
365 foreach($this->attrs['objectClass'] as $oc){
366 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
367 $tmp[]= $oc;
368 }
369 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
370 $has_unit_tag= true;
371 }
372 }
373 $this->attrs['objectClass']= $tmp;
374 $this->attrs['gosaUnitTag']= array();
375 $this->gosaUnitTag = "";
376 }
379 /* Write back to ldap */
380 $ldap->cat($this->dn, array('dn'));
381 $ldap->cd($this->dn);
383 if ($ldap->count()){
384 $this->cleanup();
385 $ldap->modify ($this->attrs);
386 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
387 $this->handle_post_events('modify');
388 } else {
389 $ldap->add($this->attrs);
390 $this->handle_post_events('add');
391 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
392 }
393 if (!$ldap->success()){
394 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
395 }
397 /* The parameter forces only to set must_be_tagged, and don't touch any objects
398 This will be done later */
399 $this->tag_objects(true);
401 /* Optionally execute a command after we're done */
402 $this->postcreate();
403 return(false);
404 }
407 /* Tag objects to have the gosaAdministrativeUnitTag */
408 function tag_objects($OnlySetTagFlag = false)
409 {
410 if(!$OnlySetTagFlag){
411 $smarty= get_smarty();
412 /* Print out html introduction */
413 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
414 <html>
415 <head>
416 <title></title>
417 <style type="text/css">@import url("themes/default/style.css");</style>
418 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
419 </head>
420 <body style="background: none; margin:4px;" id="body" >
421 ';
422 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
423 }
425 $add= $this->is_administrational_unit;
426 $len= strlen($this->dn);
427 $ldap= $this->config->get_ldap_link();
428 $ldap->cd($this->dn);
429 if ($add){
430 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
431 $this->gosaUnitTag.')))', array('dn'));
432 } else {
433 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
434 }
436 $objects = array();
437 while ($attrs= $ldap->fetch()){
438 $objects[] = $attrs;
439 }
440 foreach($objects as $attrs){
442 /* Skip self */
443 if ($attrs['dn'] == $this->dn){
444 continue;
445 }
447 /* Check for confilicting administrative units */
448 $fix= true;
449 foreach ($this->config->adepartments as $key => $tag){
450 /* This one is shorter than our dn, its not relevant... */
451 if ($len >= strlen($key)){
452 continue;
453 }
455 /* This one matches with the latter part. Break and don't fix this entry */
456 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
457 $fix= false;
458 break;
459 }
460 }
462 /* Fix entry if needed */
463 if ($fix){
464 if($OnlySetTagFlag){
465 $this->must_be_tagged =true;
466 return;
467 }
468 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
469 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
470 }
471 }
473 if(!$OnlySetTagFlag){
474 $this->must_be_tagged = FALSE;
475 echo '<p class="seperator"> </p>';
476 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
477 <br><input type='submit' name='back' value='"._("Continue")."'>
478 </form></div>";
479 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
480 }
481 }
484 /* Move/Rename complete trees */
485 function recursive_move($src_dn, $dst_dn,$force = false)
486 {
487 /* Print header to have styles included */
488 $smarty= get_smarty();
490 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
491 <html>
492 <head>
493 <title></title>
494 <style type="text/css">@import url("themes/default/style.css");</style>
495 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
496 </head>
497 <body style="background: none; margin:4px;" id="body" >
498 ';
499 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
502 /* Check if the destination entry exists */
503 $ldap= $this->config->get_ldap_link();
505 /* Check if destination exists - abort */
506 $ldap->cat($dst_dn, array('dn'));
507 if ($ldap->fetch()){
508 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
509 E_USER_WARNING);
510 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
511 return (FALSE);
512 }
514 /* Perform a search for all objects to be moved */
515 $objects= array();
516 $ldap->cd($src_dn);
517 $ldap->search("(objectClass=*)", array("dn"));
518 while($attrs= $ldap->fetch()){
519 $dn= $attrs['dn'];
520 $objects[$dn]= strlen($dn);
521 }
523 /* Sort objects by indent level */
524 asort($objects);
525 reset($objects);
527 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
528 foreach ($objects as $object => $len){
531 $src= str_replace("\\","\\\\",$object);
532 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
533 $dst= str_replace($src_dn,$dst_dn,$object);
535 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
537 $this->update_acls($object, $dst,TRUE);
539 if (!$this->copy($src, $dst)){
540 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
541 return (FALSE);
542 }
543 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
544 flush();
545 }
547 /* Remove src_dn */
548 $ldap->cd($src_dn);
549 $ldap->recursive_remove();
550 $this->orig_dn = $this->dn = $dst_dn;
551 $this->orig_base= $this->base;
552 $this->entryCSN = getEntryCSN($this->dn);
554 echo '<p class="seperator"> </p>';
556 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
557 <br><input type='submit' name='back' value='"._("Continue")."'>
558 </form></div>";
560 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
561 echo "</body></html>";
563 return (TRUE);
564 }
567 /* Return plugin informations for acl handling */
568 static function plInfo()
569 {
570 return (array("plShortName" => _("Generic"),
571 "plDescription" => _("Departments"),
572 "plSelfModify" => FALSE,
573 "plPriority" => 0,
574 "plDepends" => array(),
575 "plSection" => array("admin"),
576 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
578 "plProvidedAcls" => array(
579 "description" => _("Description"),
580 "c" => _("Country"),
581 "base" => _("Base"),
582 "l" => _("Location"),
583 "telephoneNumber" => _("Telephone"),
584 "ou" => _("Department name"),
585 "businessCategory" => _("Category"),
586 "st" => _("State"),
587 "postalAddress" => _("Address"),
588 "gosaUnitTag" => _("Administrative settings"),
589 "facsimileTelephoneNumber" => _("Fax"))
590 ));
591 }
593 function handle_object_tagging($dn= "", $tag= "", $show= false)
594 {
595 /* No dn? Self-operation... */
596 if ($dn == ""){
597 $dn= $this->dn;
599 /* No tag? Find it yourself... */
600 if ($tag == ""){
601 $len= strlen($dn);
603 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
604 $relevant= array();
605 foreach ($this->config->adepartments as $key => $ntag){
607 /* This one is bigger than our dn, its not relevant... */
608 if ($len <= strlen($key)){
609 continue;
610 }
612 /* This one matches with the latter part. Break and don't fix this entry */
613 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
614 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
615 $relevant[strlen($key)]= $ntag;
616 continue;
617 }
619 }
621 /* If we've some relevant tags to set, just get the longest one */
622 if (count($relevant)){
623 ksort($relevant);
624 $tmp= array_keys($relevant);
625 $idx= end($tmp);
626 $tag= $relevant[$idx];
627 $this->gosaUnitTag= $tag;
628 }
629 }
630 }
632 /* Set tag? */
633 if ($tag != ""){
634 /* Set objectclass and attribute */
635 $ldap= $this->config->get_ldap_link();
636 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
637 $attrs= $ldap->fetch();
638 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
639 if ($show) {
640 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
641 flush();
642 }
643 return;
644 }
645 if (count($attrs)){
646 if ($show){
647 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
648 flush();
649 }
650 $nattrs= array("gosaUnitTag" => $tag);
651 $nattrs['objectClass']= array();
652 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
653 $oc= $attrs['objectClass'][$i];
654 if ($oc != "gosaAdministrativeUnitTag"){
655 $nattrs['objectClass'][]= $oc;
656 }
657 }
658 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
659 $ldap->cd($dn);
660 $ldap->modify($nattrs);
661 if (!$ldap->success()){
662 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
663 }
664 } else {
665 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
666 }
668 } else {
669 /* Remove objectclass and attribute */
670 $ldap= $this->config->get_ldap_link();
671 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
672 $attrs= $ldap->fetch();
673 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
674 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
675 return;
676 }
677 if (count($attrs)){
678 if ($show){
679 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
680 flush();
681 }
682 $nattrs= array("gosaUnitTag" => array());
683 $nattrs['objectClass']= array();
684 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
685 $oc= $attrs['objectClass'][$i];
686 if ($oc != "gosaAdministrativeUnitTag"){
687 $nattrs['objectClass'][]= $oc;
688 }
689 }
690 $ldap->cd($dn);
691 $ldap->modify($nattrs);
692 if (!$ldap->success()){
693 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
694 }
695 } else {
696 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
697 }
698 }
699 }
701 }
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>