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 $orig_dn= "";
36 var $is_administrational_unit= false;
37 var $gosaUnitTag= "";
38 var $view_logged = FALSE;
39 var $rec_dst=false; // Destination for recursive move
40 var $rec_src=false; // Source for recursive move
41 var $rec_cpy=false; // Is recursive move requested ?
43 /* Headpage attributes */
44 var $last_dep_sorting= "invalid";
45 var $departments= array();
47 var $must_be_tagged = false;
49 /* attribute list for save action */
50 var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
51 "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
52 var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
53 var $initially_was_tagged = false;
55 var $orig_base = "";
56 var $orig_ou = "";
58 function department (&$config, $dn)
59 {
61 plugin::plugin($config, $dn);
62 $this->is_account= TRUE;
63 $this->ui= get_userinfo();
64 $this->dn= $dn;
65 $this->orig_dn= $dn;
66 $this->orig_ou= $this->ou;
67 $this->config= $config;
69 /* Set base */
70 if ($this->dn == "new"){
71 $ui= get_userinfo();
72 if(session::is_set('CurrentMainBase')){
73 $this->base = session::get('CurrentMainBase');
74 }else{
75 $this->base= dn2base($ui->dn);
76 }
77 } else {
78 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
79 }
81 $this->orig_base = $this->base;
83 /* Is administrational Unit? */
84 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
85 $this->is_administrational_unit= true;
86 $this->initially_was_tagged = true;
87 }
88 }
90 function execute()
91 {
92 /* Call parent execute */
93 plugin::execute();
95 /* Log view */
96 if($this->is_account && !$this->view_logged){
97 $this->view_logged = TRUE;
98 new log("view","department/".get_class($this),$this->dn);
99 }
101 /* Reload departments */
102 $this->config->get_departments($this->dn);
103 $this->config->make_idepartments();
104 $smarty= get_smarty();
106 $tmp = $this->plInfo();
107 foreach($tmp['plProvidedAcls'] as $name => $translation){
108 $smarty->assign($name."ACL",$this->getacl($name));
109 }
111 /* Base select dialog */
112 $once = true;
113 foreach($_POST as $name => $value){
114 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
115 $once = false;
116 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
117 $this->dialog->setCurrentBase($this->base);
118 }
119 }
121 /* Dialog handling */
122 if(is_object($this->dialog)){
123 /* Must be called before save_object */
124 $this->dialog->save_object();
126 if($this->dialog->isClosed()){
127 $this->dialog = false;
128 }elseif($this->dialog->isSelected()){
130 /* A new base was selected, check if it is a valid one */
131 $tmp = $this->get_allowed_bases();
132 if(isset($tmp[$this->dialog->isSelected()])){
133 $this->base = $this->dialog->isSelected();
134 }
136 $this->dialog= false;
137 }else{
138 return($this->dialog->execute());
139 }
140 }
142 /* Hide all departments, that are subtrees of this department */
143 $bases = $this->get_allowed_bases();
144 if(($this->dn == "new")||($this->dn == "")){
145 $tmp = $bases;
146 }else{
147 $tmp = array();
148 foreach($bases as $dn=>$base){
149 $fixed = str_replace("/","\\",$this->dn);
150 /* Only attach departments which are not a subtree of this one */
151 if(!preg_match("/".$fixed."/",$dn)){
152 $tmp[$dn]=$base;
153 }
154 }
155 }
156 $smarty->assign("bases", $tmp);
158 foreach ($this->attributes as $val){
159 $smarty->assign("$val", $this->$val);
160 }
161 $smarty->assign("base_select", $this->base);
163 /* Set admin unit flag */
164 if ($this->is_administrational_unit) {
165 $smarty->assign("unitTag", "checked");
166 } else {
167 $smarty->assign("unitTag", "");
168 }
170 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
171 }
173 function clear_fields()
174 {
175 $this->dn = "";
176 $this->base = "";
178 foreach ($this->attributes as $val){
179 $this->$val= "";
180 }
181 }
183 function remove_from_parent()
184 {
185 $ldap= $this->config->get_ldap_link();
186 $ldap->cd ($this->dn);
187 $ldap->recursive_remove();
188 new log("remove","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
189 if (!$ldap->success()){
190 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
191 }
193 /* Optionally execute a command after we're done */
194 $this->handle_post_events('remove');
195 }
197 function must_be_tagged()
198 {
199 return $this->must_be_tagged;
200 }
202 function am_i_moved()
203 {
204 return $this->rec_cpy;
205 }
208 /* Save data to object */
209 function save_object()
210 {
211 if (isset($_POST['dep_generic_posted'])){
213 /* Create a base backup and reset the
214 base directly after calling plugin::save_object();
215 Base will be set seperatly a few lines below */
216 $base_tmp = $this->base;
217 plugin::save_object();
218 $this->base = $base_tmp;
220 /* Set new base if allowed */
221 $tmp = $this->get_allowed_bases();
222 if(isset($_POST['base'])){
223 if(isset($tmp[$_POST['base']])){
224 $this->base= $_POST['base'];
225 }
226 }
228 /* Save tagging flag */
229 if ($this->acl_is_writeable("unitTag")){
230 if (isset($_POST['unitTag'])){
231 $this->is_administrational_unit= true;
232 } else {
233 $this->is_administrational_unit= false;
234 }
235 }
236 }
237 }
240 /* Check values */
241 function check()
242 {
243 /* Call common method to give check the hook */
244 $message= plugin::check();
246 /* Check for presence of this department */
247 $ldap= $this->config->get_ldap_link();
248 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
249 if ($this->orig_dn == "new" && $ldap->count()){
250 $message[]= msgPool::duplicated(_("Name"));
251 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
252 $message[]= msgPool::duplicated(_("Name"));
253 }
255 /* All required fields are set? */
256 if ($this->ou == ""){
257 $message[]= _("Required field 'Name' is not set.");
258 $message[]= msgPool::required(_("Name"));
259 }
260 if ($this->description == ""){
261 $message[]= _("Required field 'Description' is not set.");
262 $message[]= msgPool::required(_("Description"));
263 }
265 if(tests::is_department_name_reserved($this->ou,$this->base)){
266 $message[]= msgPool::reserved(_("Name"));
267 }
269 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
270 $message[]= msgPool::invalid(_("Name"), $this->ou, "/[^#+:=>\\\\\/]/");
271 }
272 if (!tests::is_phone_nr($this->telephoneNumber)){
273 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
274 }
275 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
276 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
277 }
279 return $message;
280 }
283 /* Save to LDAP */
284 function save()
285 {
286 $ldap= $this->config->get_ldap_link();
288 /* Add tag objects if needed */
289 if ($this->is_administrational_unit){
291 /* If this wasn't tagged before add oc an reset unit tag */
292 if(!$this->initially_was_tagged){
293 $this->objectclasses[]= "gosaAdministrativeUnit";
294 $this->gosaUnitTag= "";
296 /* It seams that this method is called twice,
297 set this to true. to avoid adding this oc twice */
298 $this->initially_was_tagged = true;
299 }
301 if ($this->gosaUnitTag == ""){
303 /* It's unlikely, but check if already used... */
304 $try= 5;
305 $ldap->cd($this->config->current['BASE']);
306 while ($try--){
308 /* Generate microtime stamp as tag */
309 list($usec, $sec)= explode(" ", microtime());
310 $time_stamp= preg_replace("/\./", "", $sec.$usec);
312 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
313 if ($ldap->count() == 0){
314 break;
315 }
316 }
317 if($try == 0) {
318 msg_dialog::display(_("Fatal error"), _("Cannot find an unused tag for this administrative unit!"), WARNING_DIALOG);
319 return;
320 }
321 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
322 }
323 }
324 $this->skipTagging = TRUE;
325 plugin::save();
327 /* Remove tag information if needed */
328 if (!$this->is_administrational_unit){
329 $tmp= array();
331 /* Remove gosaAdministrativeUnit from this plugin */
332 foreach($this->attrs['objectClass'] as $oc){
333 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
334 $tmp[]= $oc;
335 }
336 }
337 $this->attrs['objectClass']= $tmp;
338 }
340 /* Do we need to remove the tag itself? */
341 $has_unit_tag= false;
342 foreach($this->attrs['objectClass'] as $oc){
343 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
344 $has_unit_tag= true;
345 }
346 }
347 if ($has_unit_tag == false && $this->is_administrational_unit == false){
348 unset ($this->attrs['gosaUnitTag']);
349 } else {
350 $this->attrs['gosaUnitTag']= $this->gosaUnitTag;
351 }
353 /* Write back to ldap */
354 $ldap->cat($this->dn, array('dn'));
355 $ldap->cd($this->dn);
357 if ($ldap->count()){
358 $this->cleanup();
359 $ldap->modify ($this->attrs);
360 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
361 $this->handle_post_events('modify');
362 } else {
363 $ldap->add($this->attrs);
364 $this->handle_post_events('add');
365 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
366 }
367 if (!$ldap->success()){
368 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
369 }
371 /* The parameter forces only to set must_be_tagged, and don't touch any objects
372 This will be done later */
373 $this->tag_objects(true);
375 /* Optionally execute a command after we're done */
376 $this->postcreate();
377 return(false);
378 }
381 function ShowMoveFrame()
382 {
383 $smarty = get_smarty();
384 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
385 $smarty->assign("message","As soon as the move operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
386 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
387 return($display);
388 }
390 function ShowTagFrame()
391 {
392 $smarty = get_smarty();
393 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
394 $smarty->assign("message","As soon as the tag operation has finished, you can scroll down to end of the page and press the 'Continue' button to continue with the department management dialog.");
395 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
396 return($display);
397 }
399 /* Tag objects to have the gosaAdministrativeUnitTag */
400 function tag_objects($OnlySetTagFlag = false)
401 {
402 if(!$OnlySetTagFlag){
403 $smarty= get_smarty();
404 /* Print out html introduction */
405 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
406 <html>
407 <head>
408 <title></title>
409 <style type="text/css">@import url("themes/default/style.css");</style>
410 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
411 </head>
412 <body style="background: none; margin:4px;" id="body" >
413 ';
414 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
415 }
417 $add= $this->is_administrational_unit;
418 $len= strlen($this->dn);
419 $ldap= $this->config->get_ldap_link();
420 $ldap->cd($this->dn);
421 if ($add){
422 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
423 $this->gosaUnitTag.')))', array('dn'));
424 } else {
425 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
426 }
428 while ($attrs= $ldap->fetch()){
430 /* Skip self */
431 if ($attrs['dn'] == $this->dn){
432 continue;
433 }
435 /* Check for confilicting administrative units */
436 $fix= true;
437 foreach ($this->config->adepartments as $key => $tag){
438 /* This one is shorter than our dn, its not relevant... */
439 if ($len >= strlen($key)){
440 continue;
441 }
443 /* This one matches with the latter part. Break and don't fix this entry */
444 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
445 $fix= false;
446 break;
447 }
448 }
450 /* Fix entry if needed */
451 if ($fix){
452 if($OnlySetTagFlag){
453 $this->must_be_tagged =true;
454 return;
455 }
456 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
457 }
458 }
459 if(!$OnlySetTagFlag){
460 echo '<p class="seperator"> </p>';
461 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
462 <br><input type='submit' name='back' value='"._("Continue")."'>
463 </form></div>";
464 }
465 }
468 /* Move/Rename complete trees */
469 function recursive_move($src_dn, $dst_dn,$force = false)
470 {
471 /* If force == false prepare to recursive move this object from src to dst
472 on the next call. */
473 if(!$force){
474 $this->rec_cpy = true;
475 $this->rec_src = $src_dn;
476 $this->rec_dst = $dst_dn;
477 }else{
479 /* If this is called, but not wanted, abort */
480 if(!$this->rec_cpy){
481 return;
482 }
484 $src_dn = $this->rec_src;
485 $dst_dn = $this->rec_dst;
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->dn = $this->rec_dst;
551 $this->rec_src = $this->rec_dst = "";
552 $this->rec_cpy =false;
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 }
565 }
568 /* Return plugin informations for acl handling */
569 static function plInfo()
570 {
571 return (array("plShortName" => _("Generic"),
572 "plDescription" => _("Departments"),
573 "plSelfModify" => FALSE,
574 "plPriority" => 0,
575 "plDepends" => array(),
576 "plSection" => array("admin"),
577 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
579 "plProvidedAcls" => array(
580 "description" => _("Description"),
581 "c" => _("Country"),
582 "base" => _("Base"),
583 "l" => _("Location"),
584 "telephoneNumber" => _("Telephone"),
585 "ou" => _("Department name"),
586 "businessCategory" => _("Category"),
587 "st" => _("State"),
588 "postalAddress" => _("Address"),
589 "gosaUnitTag" => _("Administrative settings"),
590 "facsimileTelephoneNumber" => _("Fax"))
591 ));
592 }
594 function handle_object_tagging($dn= "", $tag= "", $show= false)
595 {
596 /* No dn? Self-operation... */
597 if ($dn == ""){
598 $dn= $this->dn;
600 /* No tag? Find it yourself... */
601 if ($tag == ""){
602 $len= strlen($dn);
604 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
605 $relevant= array();
606 foreach ($this->config->adepartments as $key => $ntag){
608 /* This one is bigger than our dn, its not relevant... */
609 if ($len <= strlen($key)){
610 continue;
611 }
613 /* This one matches with the latter part. Break and don't fix this entry */
614 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
615 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
616 $relevant[strlen($key)]= $ntag;
617 continue;
618 }
620 }
622 /* If we've some relevant tags to set, just get the longest one */
623 if (count($relevant)){
624 ksort($relevant);
625 $tmp= array_keys($relevant);
626 $idx= end($tmp);
627 $tag= $relevant[$idx];
628 $this->gosaUnitTag= $tag;
629 }
630 }
631 }
633 /* Set tag? */
634 if ($tag != ""){
635 /* Set objectclass and attribute */
636 $ldap= $this->config->get_ldap_link();
637 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
638 $attrs= $ldap->fetch();
639 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
640 if ($show) {
641 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
642 flush();
643 }
644 return;
645 }
646 if (count($attrs)){
647 if ($show){
648 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
649 flush();
650 }
651 $nattrs= array("gosaUnitTag" => $tag);
652 $nattrs['objectClass']= array();
653 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
654 $oc= $attrs['objectClass'][$i];
655 if ($oc != "gosaAdministrativeUnitTag"){
656 $nattrs['objectClass'][]= $oc;
657 }
658 }
659 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
660 $ldap->cd($dn);
661 $ldap->modify($nattrs);
662 if (!$ldap->success()){
663 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
664 }
665 } else {
666 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
667 }
669 } else {
670 /* Remove objectclass and attribute */
671 $ldap= $this->config->get_ldap_link();
672 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
673 $attrs= $ldap->fetch();
674 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
675 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
676 return;
677 }
678 if (count($attrs)){
679 if ($show){
680 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
681 flush();
682 }
683 $nattrs= array("gosaUnitTag" => array());
684 $nattrs['objectClass']= array();
685 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
686 $oc= $attrs['objectClass'][$i];
687 if ($oc != "gosaAdministrativeUnitTag"){
688 $nattrs['objectClass'][]= $oc;
689 }
690 }
691 $ldap->cd($dn);
692 $ldap->modify($nattrs);
693 if (!$ldap->success()){
694 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
695 }
696 } else {
697 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
698 }
699 }
701 }
703 }
705 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
706 ?>