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[]= msgPool::duplicated(_("Name"));
247 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
248 $message[]= msgPool::duplicated(_("Name"));
249 }
251 /* All required fields are set? */
252 if ($this->ou == ""){
253 $message[]= _("Required field 'Name' is not set.");
254 $message[]= msgPool::required(_("Name"));
255 }
256 if ($this->description == ""){
257 $message[]= _("Required field 'Description' is not set.");
258 $message[]= msgPool::required(_("Description"));
259 }
261 if(tests::is_department_name_reserved($this->ou,$this->base)){
262 $message[]= msgPool::reserved(_("Name"));
263 }
265 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
266 $message[]= msgPool::invalid(_("Name"), $this->ou, "/[^#+:=>\\\\\/]/");
267 }
268 if (!tests::is_phone_nr($this->telephoneNumber)){
269 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
270 }
271 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
272 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
273 }
275 return $message;
276 }
279 /* Save to LDAP */
280 function save()
281 {
282 $ldap= $this->config->get_ldap_link();
284 /* Add tag objects if needed */
285 if ($this->is_administrational_unit){
287 /* If this wasn't tagged before add oc an reset unit tag */
288 if(!$this->initially_was_tagged){
289 $this->objectclasses[]= "gosaAdministrativeUnit";
290 $this->gosaUnitTag= "";
292 /* It seams that this method is called twice,
293 set this to true. to avoid adding this oc twice */
294 $this->initially_was_tagged = true;
295 }
297 if ($this->gosaUnitTag == ""){
299 /* It's unlikely, but check if already used... */
300 $try= 5;
301 $ldap->cd($this->config->current['BASE']);
302 while ($try--){
304 /* Generate microtime stamp as tag */
305 list($usec, $sec)= explode(" ", microtime());
306 $time_stamp= preg_replace("/\./", "", $sec.$usec);
308 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
309 if ($ldap->count() == 0){
310 break;
311 }
312 }
313 if($try == 0) {
314 msg_dialog::display(_("Fatal error"), _("Cannot find an unused tag for this administrative unit!"), WARNING_DIALOG);
315 return;
316 }
317 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
318 }
319 }
320 $this->skipTagging = TRUE;
321 plugin::save();
323 $this->attrs['gosaUnitTag'] = $this->gosaUnitTag;
325 /* Remove tag information if needed */
326 if (!$this->is_administrational_unit){
327 $tmp= array();
329 /* Remove gosaAdministrativeUnit from this plugin */
330 $has_unit_tag= false;
331 foreach($this->attrs['objectClass'] as $oc){
332 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
333 $tmp[]= $oc;
334 }
335 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
336 $has_unit_tag= true;
337 }
338 }
339 $this->attrs['objectClass']= $tmp;
340 $this->attrs['gosaUnitTag']= array();
341 $this->gosaUnitTag = "";
342 }
344 /* Write back to ldap */
345 $ldap= $this->config->get_ldap_link();
346 $ldap->cat($this->dn, array('dn'));
347 $a= $ldap->fetch();
348 $ldap->cd($this->dn);
350 if (count($a)){
351 $this->cleanup();
352 $ldap->modify ($this->attrs);
353 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
354 $this->handle_post_events('modify');
355 } else {
356 $ldap->add($this->attrs);
357 $this->handle_post_events('add');
358 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
359 }
360 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
362 /* The parameter forces only to set must_be_tagged, and don't touch any objects
363 This will be done later */
364 $this->tag_objects(true);
366 /* Optionally execute a command after we're done */
367 $this->postcreate();
368 return(false);
369 }
372 function ShowMoveFrame()
373 {
374 $smarty = get_smarty();
375 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
376 $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.");
377 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
378 return($display);
379 }
381 function ShowTagFrame()
382 {
383 $smarty = get_smarty();
384 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
385 $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.");
386 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
387 return($display);
388 }
390 /* Tag objects to have the gosaAdministrativeUnitTag */
391 function tag_objects($OnlySetTagFlag = false)
392 {
393 if(!$OnlySetTagFlag){
394 $smarty= get_smarty();
395 /* Print out html introduction */
396 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
397 <html>
398 <head>
399 <title></title>
400 <style type="text/css">@import url("themes/default/style.css");</style>
401 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
402 </head>
403 <body style="background: none; margin:4px;" id="body" >
404 ';
405 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
406 }
408 $add= $this->is_administrational_unit;
409 $len= strlen($this->dn);
410 $ldap= $this->config->get_ldap_link();
411 $ldap->cd($this->dn);
412 if ($add){
413 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
414 $this->gosaUnitTag.')))', array('dn'));
415 } else {
416 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
417 }
419 while ($attrs= $ldap->fetch()){
421 /* Skip self */
422 if ($attrs['dn'] == $this->dn){
423 continue;
424 }
426 /* Check for confilicting administrative units */
427 $fix= true;
428 foreach ($this->config->adepartments as $key => $tag){
429 /* This one is shorter than our dn, its not relevant... */
430 if ($len >= strlen($key)){
431 continue;
432 }
434 /* This one matches with the latter part. Break and don't fix this entry */
435 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
436 $fix= false;
437 break;
438 }
439 }
441 /* Fix entry if needed */
442 if ($fix){
443 if($OnlySetTagFlag){
444 $this->must_be_tagged =true;
445 return;
446 }
447 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
448 }
449 }
450 if(!$OnlySetTagFlag){
451 echo '<p class="seperator"> </p>';
452 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
453 <br><input type='submit' name='back' value='"._("Continue")."'>
454 </form></div>";
455 }
456 }
459 /* Move/Rename complete trees */
460 function recursive_move($src_dn, $dst_dn,$force = false)
461 {
462 /* If force == false prepare to recursive move this object from src to dst
463 on the next call. */
464 if(!$force){
465 $this->rec_cpy = true;
466 $this->rec_src = $src_dn;
467 $this->rec_dst = $dst_dn;
468 }else{
470 /* If this is called, but not wanted, abort */
471 if(!$this->rec_cpy){
472 return;
473 }
475 $src_dn = $this->rec_src;
476 $dst_dn = $this->rec_dst;
478 /* Print header to have styles included */
479 $smarty= get_smarty();
481 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
482 <html>
483 <head>
484 <title></title>
485 <style type="text/css">@import url("themes/default/style.css");</style>
486 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
487 </head>
488 <body style="background: none; margin:4px;" id="body" >
489 ';
490 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
493 /* Check if the destination entry exists */
494 $ldap= $this->config->get_ldap_link();
496 /* Check if destination exists - abort */
497 $ldap->cat($dst_dn, array('dn'));
498 if ($ldap->fetch()){
499 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
500 E_USER_WARNING);
501 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
502 return (FALSE);
503 }
505 /* Perform a search for all objects to be moved */
506 $objects= array();
507 $ldap->cd($src_dn);
508 $ldap->search("(objectClass=*)", array("dn"));
509 while($attrs= $ldap->fetch()){
510 $dn= $attrs['dn'];
511 $objects[$dn]= strlen($dn);
512 }
514 /* Sort objects by indent level */
515 asort($objects);
516 reset($objects);
518 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
519 foreach ($objects as $object => $len){
522 $src= str_replace("\\","\\\\",$object);
523 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
524 $dst= str_replace($src_dn,$dst_dn,$object);
526 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
528 $this->update_acls($object, $dst,TRUE);
530 if (!$this->copy($src, $dst)){
531 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
532 return (FALSE);
533 }
534 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
535 flush();
536 }
538 /* Remove src_dn */
539 $ldap->cd($src_dn);
540 $ldap->recursive_remove();
541 $this->dn = $this->rec_dst;
542 $this->rec_src = $this->rec_dst = "";
543 $this->rec_cpy =false;
545 echo '<p class="seperator"> </p>';
547 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
548 <br><input type='submit' name='back' value='"._("Continue")."'>
549 </form></div>";
551 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
552 echo "</body></html>";
554 return (TRUE);
555 }
556 }
559 /* Return plugin informations for acl handling */
560 static function plInfo()
561 {
562 return (array("plShortName" => _("Generic"),
563 "plDescription" => _("Departments"),
564 "plSelfModify" => FALSE,
565 "plPriority" => 0,
566 "plDepends" => array(),
567 "plSection" => array("admin"),
568 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
570 "plProvidedAcls" => array(
571 "description" => _("Description"),
572 "c" => _("Country"),
573 "base" => _("Base"),
574 "l" => _("Location"),
575 "telephoneNumber" => _("Telephone"),
576 "ou" => _("Department name"),
577 "businessCategory" => _("Category"),
578 "st" => _("State"),
579 "postalAddress" => _("Address"),
580 "gosaUnitTag" => _("Administrative settings"),
581 "facsimileTelephoneNumber" => _("Fax"))
582 ));
583 }
585 function handle_object_tagging($dn= "", $tag= "", $show= false)
586 {
587 /* No dn? Self-operation... */
588 if ($dn == ""){
589 $dn= $this->dn;
591 /* No tag? Find it yourself... */
592 if ($tag == ""){
593 $len= strlen($dn);
595 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
596 $relevant= array();
597 foreach ($this->config->adepartments as $key => $ntag){
599 /* This one is bigger than our dn, its not relevant... */
600 if ($len <= strlen($key)){
601 continue;
602 }
604 /* This one matches with the latter part. Break and don't fix this entry */
605 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
606 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
607 $relevant[strlen($key)]= $ntag;
608 continue;
609 }
611 }
613 /* If we've some relevant tags to set, just get the longest one */
614 if (count($relevant)){
615 ksort($relevant);
616 $tmp= array_keys($relevant);
617 $idx= end($tmp);
618 $tag= $relevant[$idx];
619 $this->gosaUnitTag= $tag;
620 }
621 }
622 }
624 /* Set tag? */
625 if ($tag != ""){
626 /* Set objectclass and attribute */
627 $ldap= $this->config->get_ldap_link();
628 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
629 $attrs= $ldap->fetch();
630 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
631 if ($show) {
632 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
633 flush();
634 }
635 return;
636 }
637 if (count($attrs)){
638 if ($show){
639 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
640 flush();
641 }
642 $nattrs= array("gosaUnitTag" => $tag);
643 $nattrs['objectClass']= array();
644 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
645 $oc= $attrs['objectClass'][$i];
646 if ($oc != "gosaAdministrativeUnitTag"){
647 $nattrs['objectClass'][]= $oc;
648 }
649 }
650 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
651 $ldap->cd($dn);
652 $ldap->modify($nattrs);
653 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
654 } else {
655 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
656 }
658 } else {
659 /* Remove objectclass and attribute */
660 $ldap= $this->config->get_ldap_link();
661 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
662 $attrs= $ldap->fetch();
663 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
664 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
665 return;
666 }
667 if (count($attrs)){
668 if ($show){
669 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
670 flush();
671 }
672 $nattrs= array("gosaUnitTag" => array());
673 $nattrs['objectClass']= array();
674 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
675 $oc= $attrs['objectClass'][$i];
676 if ($oc != "gosaAdministrativeUnitTag"){
677 $nattrs['objectClass'][]= $oc;
678 }
679 }
680 $ldap->cd($dn);
681 $ldap->modify($nattrs);
682 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
683 } else {
684 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
685 }
686 }
688 }
690 }
692 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
693 ?>