1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class department extends plugin
22 {
23 /* department attributes */
24 var $ou= "";
25 var $description= "";
26 var $base= "";
27 var $st= "";
28 var $l= "";
29 var $postalAddress= "";
30 var $businessCategory= "";
31 var $telephoneNumber= "";
32 var $facsimileTelephoneNumber= "";
33 var $orig_dn= "";
34 var $is_administrational_unit= false;
35 var $gosaUnitTag= "";
36 var $view_logged = FALSE;
37 var $rec_dst=false; // Destination for recursive move
38 var $rec_src=false; // Source for recursive move
39 var $rec_cpy=false; // Is recursive move requested ?
41 /* Headpage attributes */
42 var $last_dep_sorting= "invalid";
43 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;
64 $this->orig_ou= $this->ou;
65 $this->config= $config;
67 /* Set base */
68 if ($this->dn == "new"){
69 $ui= get_userinfo();
70 if(session::is_set('CurrentMainBase')){
71 $this->base = session::get('CurrentMainBase');
72 }else{
73 $this->base= dn2base($ui->dn);
74 }
75 } else {
76 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
77 }
79 $this->orig_base = $this->base;
81 /* Is administrational Unit? */
82 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
83 $this->is_administrational_unit= true;
84 $this->initially_was_tagged = true;
85 }
86 }
88 function execute()
89 {
90 /* Call parent execute */
91 plugin::execute();
93 /* Log view */
94 if($this->is_account && !$this->view_logged){
95 $this->view_logged = TRUE;
96 new log("view","department/".get_class($this),$this->dn);
97 }
99 /* Reload departments */
100 $this->config->get_departments($this->dn);
101 $this->config->make_idepartments();
102 $smarty= get_smarty();
104 $tmp = $this->plInfo();
105 foreach($tmp['plProvidedAcls'] as $name => $translation){
106 $smarty->assign($name."ACL",$this->getacl($name));
107 }
109 /* Base select dialog */
110 $once = true;
111 foreach($_POST as $name => $value){
112 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
113 $once = false;
114 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
115 $this->dialog->setCurrentBase($this->base);
116 }
117 }
119 /* Dialog handling */
120 if(is_object($this->dialog)){
121 /* Must be called before save_object */
122 $this->dialog->save_object();
124 if($this->dialog->isClosed()){
125 $this->dialog = false;
126 }elseif($this->dialog->isSelected()){
128 /* A new base was selected, check if it is a valid one */
129 $tmp = $this->get_allowed_bases();
130 if(isset($tmp[$this->dialog->isSelected()])){
131 $this->base = $this->dialog->isSelected();
132 }
134 $this->dialog= false;
135 }else{
136 return($this->dialog->execute());
137 }
138 }
140 /* Hide all departments, that are subtrees of this department */
141 $bases = $this->get_allowed_bases();
142 if(($this->dn == "new")||($this->dn == "")){
143 $tmp = $bases;
144 }else{
145 $tmp = array();
146 foreach($bases as $dn=>$base){
147 $fixed = str_replace("/","\\",$this->dn);
148 /* Only attach departments which are not a subtree of this one */
149 if(!preg_match("/".$fixed."/",$dn)){
150 $tmp[$dn]=$base;
151 }
152 }
153 }
154 $smarty->assign("bases", $tmp);
156 foreach ($this->attributes as $val){
157 $smarty->assign("$val", $this->$val);
158 }
159 $smarty->assign("base_select", $this->base);
161 /* Set admin unit flag */
162 if ($this->is_administrational_unit) {
163 $smarty->assign("unitTag", "checked");
164 } else {
165 $smarty->assign("unitTag", "");
166 }
168 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
169 }
171 function clear_fields()
172 {
173 $this->dn = "";
174 $this->base = "";
176 foreach ($this->attributes as $val){
177 $this->$val= "";
178 }
179 }
181 function remove_from_parent()
182 {
183 $ldap= $this->config->get_ldap_link();
184 $ldap->cd ($this->dn);
185 $ldap->recursive_remove();
186 new log("remove","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
187 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
189 /* Optionally execute a command after we're done */
190 $this->handle_post_events('remove');
191 }
193 function must_be_tagged()
194 {
195 return $this->must_be_tagged;
196 }
198 function am_i_moved()
199 {
200 return $this->rec_cpy;
201 }
204 /* Save data to object */
205 function save_object()
206 {
207 if (isset($_POST['dep_generic_posted'])){
209 /* Create a base backup and reset the
210 base directly after calling plugin::save_object();
211 Base will be set seperatly a few lines below */
212 $base_tmp = $this->base;
213 plugin::save_object();
214 $this->base = $base_tmp;
216 /* Set new base if allowed */
217 $tmp = $this->get_allowed_bases();
218 if(isset($_POST['base'])){
219 if(isset($tmp[$_POST['base']])){
220 $this->base= $_POST['base'];
221 }
222 }
224 /* Save tagging flag */
225 if ($this->acl_is_writeable("unitTag")){
226 if (isset($_POST['unitTag'])){
227 $this->is_administrational_unit= true;
228 } else {
229 $this->is_administrational_unit= false;
230 }
231 }
232 }
233 }
236 /* Check values */
237 function check()
238 {
239 /* Call common method to give check the hook */
240 $message= plugin::check();
242 /* Check for presence of this department */
243 $ldap= $this->config->get_ldap_link();
244 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
245 if ($this->orig_dn == "new" && $ldap->count()){
246 $message[]= _("Department with that 'Name' already exists.");
247 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
248 $message[]= _("Department with that 'Name' already exists.");
249 }
251 /* All required fields are set? */
252 if ($this->ou == ""){
253 $message[]= _("Required field 'Name' is not set.");
254 }
255 if ($this->description == ""){
256 $message[]= _("Required field 'Description' is not set.");
257 }
259 if(tests::is_department_name_reserved($this->ou,$this->base)){
260 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
261 }
263 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
264 $message[]= _("The field 'Name' contains invalid characters.");
265 }
266 if (!tests::is_phone_nr($this->telephoneNumber)){
267 $message[]= _("The field 'Phone' contains an invalid phone number.");
268 }
269 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
270 $message[]= _("The field 'Fax' contains an invalid phone number.");
271 }
273 return $message;
274 }
277 /* Save to LDAP */
278 function save()
279 {
280 $ldap= $this->config->get_ldap_link();
282 /* Add tag objects if needed */
283 if ($this->is_administrational_unit){
285 /* If this wasn't tagged before add oc an reset unit tag */
286 if(!$this->initially_was_tagged){
287 $this->objectclasses[]= "gosaAdministrativeUnit";
288 $this->gosaUnitTag= "";
290 /* It seams that this method is called twice,
291 set this to true. to avoid adding this oc twice */
292 $this->initially_was_tagged = true;
293 }
295 if ($this->gosaUnitTag == ""){
297 /* It's unlikely, but check if already used... */
298 $try= 5;
299 $ldap->cd($this->config->current['BASE']);
300 while ($try--){
302 /* Generate microtime stamp as tag */
303 list($usec, $sec)= explode(" ", microtime());
304 $time_stamp= preg_replace("/\./", "", $sec.$usec);
306 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
307 if ($ldap->count() == 0){
308 break;
309 }
310 }
311 if($try == 0) {
312 msg_dialog::display(_("Fatal error"), _("Cannot find an unused tag for this administrative unit!"), WARNING_DIALOG);
313 return;
314 }
315 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
316 }
317 }
318 plugin::save();
320 /* Remove tag information if needed */
321 if (!$this->is_administrational_unit){
322 $tmp= array();
324 /* Remove gosaAdministrativeUnit from this plugin */
325 $has_unit_tag= false;
326 foreach($this->attrs['objectClass'] as $oc){
327 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
328 $tmp[]= $oc;
329 }
330 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
331 $has_unit_tag= true;
332 }
333 }
334 $this->attrs['objectClass']= $tmp;
336 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
337 $this->attrs['gosaUnitTag']= array();
338 }
339 $this->gosaUnitTag = "";
340 }
342 /* Write back to ldap */
343 $ldap= $this->config->get_ldap_link();
344 $ldap->cat($this->dn, array('dn'));
345 $a= $ldap->fetch();
346 $ldap->cd($this->dn);
348 if (count($a)){
349 $this->cleanup();
350 $ldap->modify ($this->attrs);
351 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
352 $this->handle_post_events('modify');
353 } else {
354 $ldap->add($this->attrs);
355 $this->handle_post_events('add');
356 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
357 }
358 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
360 /* The parameter forces only to set must_be_tagged, and don't touch any objects
361 This will be done later */
362 $this->tag_objects(true);
364 /* Optionally execute a command after we're done */
365 $this->postcreate();
366 return(false);
367 }
370 function ShowMoveFrame()
371 {
372 $smarty = get_smarty();
373 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
374 $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.");
375 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
376 return($display);
377 }
379 function ShowTagFrame()
380 {
381 $smarty = get_smarty();
382 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
383 $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.");
384 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
385 return($display);
386 }
388 /* Tag objects to have the gosaAdministrativeUnitTag */
389 function tag_objects($OnlySetTagFlag = false)
390 {
391 if(!$OnlySetTagFlag){
392 $smarty= get_smarty();
393 /* Print out html introduction */
394 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
395 <html>
396 <head>
397 <title></title>
398 <style type="text/css">@import url("themes/default/style.css");</style>
399 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
400 </head>
401 <body style="background: none; margin:4px;" id="body" >
402 ';
403 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
404 }
406 $add= $this->is_administrational_unit;
407 $len= strlen($this->dn);
408 $ldap= $this->config->get_ldap_link();
409 $ldap->cd($this->dn);
410 if ($add){
411 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
412 $this->gosaUnitTag.')))', array('dn'));
413 } else {
414 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
415 }
417 while ($attrs= $ldap->fetch()){
419 /* Skip self */
420 if ($attrs['dn'] == $this->dn){
421 continue;
422 }
424 /* Check for confilicting administrative units */
425 $fix= true;
426 foreach ($this->config->adepartments as $key => $tag){
427 /* This one is shorter than our dn, its not relevant... */
428 if ($len >= strlen($key)){
429 continue;
430 }
432 /* This one matches with the latter part. Break and don't fix this entry */
433 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
434 $fix= false;
435 break;
436 }
437 }
439 /* Fix entry if needed */
440 if ($fix){
441 if($OnlySetTagFlag){
442 $this->must_be_tagged =true;
443 return;
444 }
445 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
446 }
447 }
448 if(!$OnlySetTagFlag){
449 echo '<p class="seperator"> </p>';
450 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
451 <br><input type='submit' name='back' value='"._("Continue")."'>
452 </form></div>";
453 }
454 }
457 /* Move/Rename complete trees */
458 function recursive_move($src_dn, $dst_dn,$force = false)
459 {
460 /* If force == false prepare to recursive move this object from src to dst
461 on the next call. */
462 if(!$force){
463 $this->rec_cpy = true;
464 $this->rec_src = $src_dn;
465 $this->rec_dst = $dst_dn;
466 }else{
468 /* If this is called, but not wanted, abort */
469 if(!$this->rec_cpy){
470 return;
471 }
473 $src_dn = $this->rec_src;
474 $dst_dn = $this->rec_dst;
476 /* Print header to have styles included */
477 $smarty= get_smarty();
479 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
480 <html>
481 <head>
482 <title></title>
483 <style type="text/css">@import url("themes/default/style.css");</style>
484 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
485 </head>
486 <body style="background: none; margin:4px;" id="body" >
487 ';
488 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
491 /* Check if the destination entry exists */
492 $ldap= $this->config->get_ldap_link();
494 /* Check if destination exists - abort */
495 $ldap->cat($dst_dn, array('dn'));
496 if ($ldap->fetch()){
497 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
498 E_USER_WARNING);
499 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
500 return (FALSE);
501 }
503 /* Perform a search for all objects to be moved */
504 $objects= array();
505 $ldap->cd($src_dn);
506 $ldap->search("(objectClass=*)", array("dn"));
507 while($attrs= $ldap->fetch()){
508 $dn= $attrs['dn'];
509 $objects[$dn]= strlen($dn);
510 }
512 /* Sort objects by indent level */
513 asort($objects);
514 reset($objects);
516 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
517 foreach ($objects as $object => $len){
520 $src= str_replace("\\","\\\\",$object);
521 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
522 $dst= str_replace($src_dn,$dst_dn,$object);
524 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
526 $this->update_acls($object, $dst,TRUE);
528 if (!$this->copy($src, $dst)){
529 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
530 return (FALSE);
531 }
532 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
533 flush();
534 }
536 /* Remove src_dn */
537 $ldap->cd($src_dn);
538 $ldap->recursive_remove();
539 $this->dn = $this->rec_dst;
540 $this->rec_src = $this->rec_dst = "";
541 $this->rec_cpy =false;
543 echo '<p class="seperator"> </p>';
545 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
546 <br><input type='submit' name='back' value='"._("Continue")."'>
547 </form></div>";
549 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
550 echo "</body></html>";
552 return (TRUE);
553 }
554 }
557 /* Return plugin informations for acl handling */
558 static function plInfo()
559 {
560 return (array("plShortName" => _("Generic"),
561 "plDescription" => _("Departments"),
562 "plSelfModify" => FALSE,
563 "plPriority" => 0,
564 "plDepends" => array(),
565 "plSection" => array("admin"),
566 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
568 "plProvidedAcls" => array(
569 "description" => _("Description"),
570 "c" => _("Country"),
571 "base" => _("Base"),
572 "l" => _("Location"),
573 "telephoneNumber" => _("Telephone"),
574 "ou" => _("Department name"),
575 "businessCategory" => _("Category"),
576 "st" => _("State"),
577 "postalAddress" => _("Address"),
578 "gosaUnitTag" => _("Administrative settings"),
579 "facsimileTelephoneNumber" => _("Fax"))
580 ));
581 }
583 function handle_object_tagging($dn= "", $tag= "", $show= false)
584 {
585 /* No dn? Self-operation... */
586 if ($dn == ""){
587 $dn= $this->dn;
589 /* No tag? Find it yourself... */
590 if ($tag == ""){
591 $len= strlen($dn);
593 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
594 $relevant= array();
595 foreach ($this->config->adepartments as $key => $ntag){
597 /* This one is bigger than our dn, its not relevant... */
598 if ($len <= strlen($key)){
599 continue;
600 }
602 /* This one matches with the latter part. Break and don't fix this entry */
603 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
604 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
605 $relevant[strlen($key)]= $ntag;
606 continue;
607 }
609 }
611 /* If we've some relevant tags to set, just get the longest one */
612 if (count($relevant)){
613 ksort($relevant);
614 $tmp= array_keys($relevant);
615 $idx= end($tmp);
616 $tag= $relevant[$idx];
617 $this->gosaUnitTag= $tag;
618 }
619 }
620 }
622 /* Set tag? */
623 if ($tag != ""){
624 /* Set objectclass and attribute */
625 $ldap= $this->config->get_ldap_link();
626 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
627 $attrs= $ldap->fetch();
628 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
629 if ($show) {
630 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
631 flush();
632 }
633 return;
634 }
635 if (count($attrs)){
636 if ($show){
637 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
638 flush();
639 }
640 $nattrs= array("gosaUnitTag" => $tag);
641 $nattrs['objectClass']= array();
642 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
643 $oc= $attrs['objectClass'][$i];
644 if ($oc != "gosaAdministrativeUnitTag"){
645 $nattrs['objectClass'][]= $oc;
646 }
647 }
648 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
649 $ldap->cd($dn);
650 $ldap->modify($nattrs);
651 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
652 } else {
653 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
654 }
656 } else {
657 /* Remove objectclass and attribute */
658 $ldap= $this->config->get_ldap_link();
659 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
660 $attrs= $ldap->fetch();
661 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
662 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
663 return;
664 }
665 if (count($attrs)){
666 if ($show){
667 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
668 flush();
669 }
670 $nattrs= array("gosaUnitTag" => array());
671 $nattrs['objectClass']= array();
672 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
673 $oc= $attrs['objectClass'][$i];
674 if ($oc != "gosaAdministrativeUnitTag"){
675 $nattrs['objectClass'][]= $oc;
676 }
677 }
678 $ldap->cd($dn);
679 $ldap->modify($nattrs);
680 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
681 } else {
682 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
683 }
684 }
686 }
688 }
690 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
691 ?>