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 $this->attrs['gosaUnitTag'] = $this->gosaUnitTag;
329 /* Remove tag information if needed */
330 if (!$this->is_administrational_unit){
331 $tmp= array();
333 /* Remove gosaAdministrativeUnit from this plugin */
334 $has_unit_tag= false;
335 foreach($this->attrs['objectClass'] as $oc){
336 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
337 $tmp[]= $oc;
338 }
339 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
340 $has_unit_tag= true;
341 }
342 }
343 $this->attrs['objectClass']= $tmp;
344 $this->attrs['gosaUnitTag']= array();
345 $this->gosaUnitTag = "";
346 }
348 /* Write back to ldap */
349 $ldap= $this->config->get_ldap_link();
350 $ldap->cat($this->dn, array('dn'));
351 $a= $ldap->fetch();
352 $ldap->cd($this->dn);
354 if (count($a)){
355 $this->cleanup();
356 $ldap->modify ($this->attrs);
357 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
358 $this->handle_post_events('modify');
359 } else {
360 $ldap->add($this->attrs);
361 $this->handle_post_events('add');
362 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
363 }
364 if (!$ldap->success()){
365 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
366 }
368 /* The parameter forces only to set must_be_tagged, and don't touch any objects
369 This will be done later */
370 $this->tag_objects(true);
372 /* Optionally execute a command after we're done */
373 $this->postcreate();
374 return(false);
375 }
378 function ShowMoveFrame()
379 {
380 $smarty = get_smarty();
381 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
382 $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.");
383 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
384 return($display);
385 }
387 function ShowTagFrame()
388 {
389 $smarty = get_smarty();
390 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
391 $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.");
392 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
393 return($display);
394 }
396 /* Tag objects to have the gosaAdministrativeUnitTag */
397 function tag_objects($OnlySetTagFlag = false)
398 {
399 if(!$OnlySetTagFlag){
400 $smarty= get_smarty();
401 /* Print out html introduction */
402 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
403 <html>
404 <head>
405 <title></title>
406 <style type="text/css">@import url("themes/default/style.css");</style>
407 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
408 </head>
409 <body style="background: none; margin:4px;" id="body" >
410 ';
411 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
412 }
414 $add= $this->is_administrational_unit;
415 $len= strlen($this->dn);
416 $ldap= $this->config->get_ldap_link();
417 $ldap->cd($this->dn);
418 if ($add){
419 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
420 $this->gosaUnitTag.')))', array('dn'));
421 } else {
422 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
423 }
425 while ($attrs= $ldap->fetch()){
427 /* Skip self */
428 if ($attrs['dn'] == $this->dn){
429 continue;
430 }
432 /* Check for confilicting administrative units */
433 $fix= true;
434 foreach ($this->config->adepartments as $key => $tag){
435 /* This one is shorter than our dn, its not relevant... */
436 if ($len >= strlen($key)){
437 continue;
438 }
440 /* This one matches with the latter part. Break and don't fix this entry */
441 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
442 $fix= false;
443 break;
444 }
445 }
447 /* Fix entry if needed */
448 if ($fix){
449 if($OnlySetTagFlag){
450 $this->must_be_tagged =true;
451 return;
452 }
453 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
454 }
455 }
456 if(!$OnlySetTagFlag){
457 echo '<p class="seperator"> </p>';
458 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
459 <br><input type='submit' name='back' value='"._("Continue")."'>
460 </form></div>";
461 }
462 }
465 /* Move/Rename complete trees */
466 function recursive_move($src_dn, $dst_dn,$force = false)
467 {
468 /* If force == false prepare to recursive move this object from src to dst
469 on the next call. */
470 if(!$force){
471 $this->rec_cpy = true;
472 $this->rec_src = $src_dn;
473 $this->rec_dst = $dst_dn;
474 }else{
476 /* If this is called, but not wanted, abort */
477 if(!$this->rec_cpy){
478 return;
479 }
481 $src_dn = $this->rec_src;
482 $dst_dn = $this->rec_dst;
484 /* Print header to have styles included */
485 $smarty= get_smarty();
487 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
488 <html>
489 <head>
490 <title></title>
491 <style type="text/css">@import url("themes/default/style.css");</style>
492 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
493 </head>
494 <body style="background: none; margin:4px;" id="body" >
495 ';
496 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
499 /* Check if the destination entry exists */
500 $ldap= $this->config->get_ldap_link();
502 /* Check if destination exists - abort */
503 $ldap->cat($dst_dn, array('dn'));
504 if ($ldap->fetch()){
505 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
506 E_USER_WARNING);
507 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
508 return (FALSE);
509 }
511 /* Perform a search for all objects to be moved */
512 $objects= array();
513 $ldap->cd($src_dn);
514 $ldap->search("(objectClass=*)", array("dn"));
515 while($attrs= $ldap->fetch()){
516 $dn= $attrs['dn'];
517 $objects[$dn]= strlen($dn);
518 }
520 /* Sort objects by indent level */
521 asort($objects);
522 reset($objects);
524 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
525 foreach ($objects as $object => $len){
528 $src= str_replace("\\","\\\\",$object);
529 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
530 $dst= str_replace($src_dn,$dst_dn,$object);
532 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
534 $this->update_acls($object, $dst,TRUE);
536 if (!$this->copy($src, $dst)){
537 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
538 return (FALSE);
539 }
540 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
541 flush();
542 }
544 /* Remove src_dn */
545 $ldap->cd($src_dn);
546 $ldap->recursive_remove();
547 $this->dn = $this->rec_dst;
548 $this->rec_src = $this->rec_dst = "";
549 $this->rec_cpy =false;
551 echo '<p class="seperator"> </p>';
553 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
554 <br><input type='submit' name='back' value='"._("Continue")."'>
555 </form></div>";
557 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
558 echo "</body></html>";
560 return (TRUE);
561 }
562 }
565 /* Return plugin informations for acl handling */
566 static function plInfo()
567 {
568 return (array("plShortName" => _("Generic"),
569 "plDescription" => _("Departments"),
570 "plSelfModify" => FALSE,
571 "plPriority" => 0,
572 "plDepends" => array(),
573 "plSection" => array("admin"),
574 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
576 "plProvidedAcls" => array(
577 "description" => _("Description"),
578 "c" => _("Country"),
579 "base" => _("Base"),
580 "l" => _("Location"),
581 "telephoneNumber" => _("Telephone"),
582 "ou" => _("Department name"),
583 "businessCategory" => _("Category"),
584 "st" => _("State"),
585 "postalAddress" => _("Address"),
586 "gosaUnitTag" => _("Administrative settings"),
587 "facsimileTelephoneNumber" => _("Fax"))
588 ));
589 }
591 function handle_object_tagging($dn= "", $tag= "", $show= false)
592 {
593 /* No dn? Self-operation... */
594 if ($dn == ""){
595 $dn= $this->dn;
597 /* No tag? Find it yourself... */
598 if ($tag == ""){
599 $len= strlen($dn);
601 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
602 $relevant= array();
603 foreach ($this->config->adepartments as $key => $ntag){
605 /* This one is bigger than our dn, its not relevant... */
606 if ($len <= strlen($key)){
607 continue;
608 }
610 /* This one matches with the latter part. Break and don't fix this entry */
611 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
612 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
613 $relevant[strlen($key)]= $ntag;
614 continue;
615 }
617 }
619 /* If we've some relevant tags to set, just get the longest one */
620 if (count($relevant)){
621 ksort($relevant);
622 $tmp= array_keys($relevant);
623 $idx= end($tmp);
624 $tag= $relevant[$idx];
625 $this->gosaUnitTag= $tag;
626 }
627 }
628 }
630 /* Set tag? */
631 if ($tag != ""){
632 /* Set objectclass and attribute */
633 $ldap= $this->config->get_ldap_link();
634 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
635 $attrs= $ldap->fetch();
636 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
637 if ($show) {
638 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
639 flush();
640 }
641 return;
642 }
643 if (count($attrs)){
644 if ($show){
645 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
646 flush();
647 }
648 $nattrs= array("gosaUnitTag" => $tag);
649 $nattrs['objectClass']= array();
650 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
651 $oc= $attrs['objectClass'][$i];
652 if ($oc != "gosaAdministrativeUnitTag"){
653 $nattrs['objectClass'][]= $oc;
654 }
655 }
656 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
657 $ldap->cd($dn);
658 $ldap->modify($nattrs);
659 if (!$ldap->success()){
660 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
661 }
662 } else {
663 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
664 }
666 } else {
667 /* Remove objectclass and attribute */
668 $ldap= $this->config->get_ldap_link();
669 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
670 $attrs= $ldap->fetch();
671 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
672 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
673 return;
674 }
675 if (count($attrs)){
676 if ($show){
677 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
678 flush();
679 }
680 $nattrs= array("gosaUnitTag" => array());
681 $nattrs['objectClass']= array();
682 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
683 $oc= $attrs['objectClass'][$i];
684 if ($oc != "gosaAdministrativeUnitTag"){
685 $nattrs['objectClass'][]= $oc;
686 }
687 }
688 $ldap->cd($dn);
689 $ldap->modify($nattrs);
690 if (!$ldap->success()){
691 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
692 }
693 } else {
694 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
695 }
696 }
698 }
700 }
702 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
703 ?>