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 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
191 /* Optionally execute a command after we're done */
192 $this->handle_post_events('remove');
193 }
195 function must_be_tagged()
196 {
197 return $this->must_be_tagged;
198 }
200 function am_i_moved()
201 {
202 return $this->rec_cpy;
203 }
206 /* Save data to object */
207 function save_object()
208 {
209 if (isset($_POST['dep_generic_posted'])){
211 /* Create a base backup and reset the
212 base directly after calling plugin::save_object();
213 Base will be set seperatly a few lines below */
214 $base_tmp = $this->base;
215 plugin::save_object();
216 $this->base = $base_tmp;
218 /* Set new base if allowed */
219 $tmp = $this->get_allowed_bases();
220 if(isset($_POST['base'])){
221 if(isset($tmp[$_POST['base']])){
222 $this->base= $_POST['base'];
223 }
224 }
226 /* Save tagging flag */
227 if ($this->acl_is_writeable("unitTag")){
228 if (isset($_POST['unitTag'])){
229 $this->is_administrational_unit= true;
230 } else {
231 $this->is_administrational_unit= false;
232 }
233 }
234 }
235 }
238 /* Check values */
239 function check()
240 {
241 /* Call common method to give check the hook */
242 $message= plugin::check();
244 /* Check for presence of this department */
245 $ldap= $this->config->get_ldap_link();
246 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
247 if ($this->orig_dn == "new" && $ldap->count()){
248 $message[]= msgPool::duplicated(_("Name"));
249 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
250 $message[]= msgPool::duplicated(_("Name"));
251 }
253 /* All required fields are set? */
254 if ($this->ou == ""){
255 $message[]= _("Required field 'Name' is not set.");
256 $message[]= msgPool::required(_("Name"));
257 }
258 if ($this->description == ""){
259 $message[]= _("Required field 'Description' is not set.");
260 $message[]= msgPool::required(_("Description"));
261 }
263 if(tests::is_department_name_reserved($this->ou,$this->base)){
264 $message[]= msgPool::reserved(_("Name"));
265 }
267 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
268 $message[]= msgPool::invalid(_("Name"), $this->ou, "/[^#+:=>\\\\\/]/");
269 }
270 if (!tests::is_phone_nr($this->telephoneNumber)){
271 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
272 }
273 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
274 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
275 }
277 return $message;
278 }
281 /* Save to LDAP */
282 function save()
283 {
284 $ldap= $this->config->get_ldap_link();
286 /* Add tag objects if needed */
287 if ($this->is_administrational_unit){
289 /* If this wasn't tagged before add oc an reset unit tag */
290 if(!$this->initially_was_tagged){
291 $this->objectclasses[]= "gosaAdministrativeUnit";
292 $this->gosaUnitTag= "";
294 /* It seams that this method is called twice,
295 set this to true. to avoid adding this oc twice */
296 $this->initially_was_tagged = true;
297 }
299 if ($this->gosaUnitTag == ""){
301 /* It's unlikely, but check if already used... */
302 $try= 5;
303 $ldap->cd($this->config->current['BASE']);
304 while ($try--){
306 /* Generate microtime stamp as tag */
307 list($usec, $sec)= explode(" ", microtime());
308 $time_stamp= preg_replace("/\./", "", $sec.$usec);
310 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
311 if ($ldap->count() == 0){
312 break;
313 }
314 }
315 if($try == 0) {
316 msg_dialog::display(_("Fatal error"), _("Cannot find an unused tag for this administrative unit!"), WARNING_DIALOG);
317 return;
318 }
319 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
320 }
321 }
322 $this->skipTagging = TRUE;
323 plugin::save();
325 $this->attrs['gosaUnitTag'] = $this->gosaUnitTag;
327 /* Remove tag information if needed */
328 if (!$this->is_administrational_unit){
329 $tmp= array();
331 /* Remove gosaAdministrativeUnit from this plugin */
332 $has_unit_tag= false;
333 foreach($this->attrs['objectClass'] as $oc){
334 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
335 $tmp[]= $oc;
336 }
337 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
338 $has_unit_tag= true;
339 }
340 }
341 $this->attrs['objectClass']= $tmp;
342 $this->attrs['gosaUnitTag']= array();
343 $this->gosaUnitTag = "";
344 }
346 /* Write back to ldap */
347 $ldap= $this->config->get_ldap_link();
348 $ldap->cat($this->dn, array('dn'));
349 $a= $ldap->fetch();
350 $ldap->cd($this->dn);
352 if (count($a)){
353 $this->cleanup();
354 $ldap->modify ($this->attrs);
355 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
356 $this->handle_post_events('modify');
357 } else {
358 $ldap->add($this->attrs);
359 $this->handle_post_events('add');
360 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
361 }
362 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
364 /* The parameter forces only to set must_be_tagged, and don't touch any objects
365 This will be done later */
366 $this->tag_objects(true);
368 /* Optionally execute a command after we're done */
369 $this->postcreate();
370 return(false);
371 }
374 function ShowMoveFrame()
375 {
376 $smarty = get_smarty();
377 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
378 $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.");
379 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
380 return($display);
381 }
383 function ShowTagFrame()
384 {
385 $smarty = get_smarty();
386 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
387 $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.");
388 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
389 return($display);
390 }
392 /* Tag objects to have the gosaAdministrativeUnitTag */
393 function tag_objects($OnlySetTagFlag = false)
394 {
395 if(!$OnlySetTagFlag){
396 $smarty= get_smarty();
397 /* Print out html introduction */
398 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
399 <html>
400 <head>
401 <title></title>
402 <style type="text/css">@import url("themes/default/style.css");</style>
403 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
404 </head>
405 <body style="background: none; margin:4px;" id="body" >
406 ';
407 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
408 }
410 $add= $this->is_administrational_unit;
411 $len= strlen($this->dn);
412 $ldap= $this->config->get_ldap_link();
413 $ldap->cd($this->dn);
414 if ($add){
415 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
416 $this->gosaUnitTag.')))', array('dn'));
417 } else {
418 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
419 }
421 while ($attrs= $ldap->fetch()){
423 /* Skip self */
424 if ($attrs['dn'] == $this->dn){
425 continue;
426 }
428 /* Check for confilicting administrative units */
429 $fix= true;
430 foreach ($this->config->adepartments as $key => $tag){
431 /* This one is shorter than our dn, its not relevant... */
432 if ($len >= strlen($key)){
433 continue;
434 }
436 /* This one matches with the latter part. Break and don't fix this entry */
437 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
438 $fix= false;
439 break;
440 }
441 }
443 /* Fix entry if needed */
444 if ($fix){
445 if($OnlySetTagFlag){
446 $this->must_be_tagged =true;
447 return;
448 }
449 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
450 }
451 }
452 if(!$OnlySetTagFlag){
453 echo '<p class="seperator"> </p>';
454 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
455 <br><input type='submit' name='back' value='"._("Continue")."'>
456 </form></div>";
457 }
458 }
461 /* Move/Rename complete trees */
462 function recursive_move($src_dn, $dst_dn,$force = false)
463 {
464 /* If force == false prepare to recursive move this object from src to dst
465 on the next call. */
466 if(!$force){
467 $this->rec_cpy = true;
468 $this->rec_src = $src_dn;
469 $this->rec_dst = $dst_dn;
470 }else{
472 /* If this is called, but not wanted, abort */
473 if(!$this->rec_cpy){
474 return;
475 }
477 $src_dn = $this->rec_src;
478 $dst_dn = $this->rec_dst;
480 /* Print header to have styles included */
481 $smarty= get_smarty();
483 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
484 <html>
485 <head>
486 <title></title>
487 <style type="text/css">@import url("themes/default/style.css");</style>
488 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
489 </head>
490 <body style="background: none; margin:4px;" id="body" >
491 ';
492 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
495 /* Check if the destination entry exists */
496 $ldap= $this->config->get_ldap_link();
498 /* Check if destination exists - abort */
499 $ldap->cat($dst_dn, array('dn'));
500 if ($ldap->fetch()){
501 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
502 E_USER_WARNING);
503 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
504 return (FALSE);
505 }
507 /* Perform a search for all objects to be moved */
508 $objects= array();
509 $ldap->cd($src_dn);
510 $ldap->search("(objectClass=*)", array("dn"));
511 while($attrs= $ldap->fetch()){
512 $dn= $attrs['dn'];
513 $objects[$dn]= strlen($dn);
514 }
516 /* Sort objects by indent level */
517 asort($objects);
518 reset($objects);
520 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
521 foreach ($objects as $object => $len){
524 $src= str_replace("\\","\\\\",$object);
525 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
526 $dst= str_replace($src_dn,$dst_dn,$object);
528 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
530 $this->update_acls($object, $dst,TRUE);
532 if (!$this->copy($src, $dst)){
533 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
534 return (FALSE);
535 }
536 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
537 flush();
538 }
540 /* Remove src_dn */
541 $ldap->cd($src_dn);
542 $ldap->recursive_remove();
543 $this->dn = $this->rec_dst;
544 $this->rec_src = $this->rec_dst = "";
545 $this->rec_cpy =false;
547 echo '<p class="seperator"> </p>';
549 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
550 <br><input type='submit' name='back' value='"._("Continue")."'>
551 </form></div>";
553 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
554 echo "</body></html>";
556 return (TRUE);
557 }
558 }
561 /* Return plugin informations for acl handling */
562 static function plInfo()
563 {
564 return (array("plShortName" => _("Generic"),
565 "plDescription" => _("Departments"),
566 "plSelfModify" => FALSE,
567 "plPriority" => 0,
568 "plDepends" => array(),
569 "plSection" => array("admin"),
570 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
572 "plProvidedAcls" => array(
573 "description" => _("Description"),
574 "c" => _("Country"),
575 "base" => _("Base"),
576 "l" => _("Location"),
577 "telephoneNumber" => _("Telephone"),
578 "ou" => _("Department name"),
579 "businessCategory" => _("Category"),
580 "st" => _("State"),
581 "postalAddress" => _("Address"),
582 "gosaUnitTag" => _("Administrative settings"),
583 "facsimileTelephoneNumber" => _("Fax"))
584 ));
585 }
587 function handle_object_tagging($dn= "", $tag= "", $show= false)
588 {
589 /* No dn? Self-operation... */
590 if ($dn == ""){
591 $dn= $this->dn;
593 /* No tag? Find it yourself... */
594 if ($tag == ""){
595 $len= strlen($dn);
597 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "No tag for $dn - looking for one...", "Tagging");
598 $relevant= array();
599 foreach ($this->config->adepartments as $key => $ntag){
601 /* This one is bigger than our dn, its not relevant... */
602 if ($len <= strlen($key)){
603 continue;
604 }
606 /* This one matches with the latter part. Break and don't fix this entry */
607 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $dn)){
608 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "DEBUG: Possibly relevant: $key", "Tagging");
609 $relevant[strlen($key)]= $ntag;
610 continue;
611 }
613 }
615 /* If we've some relevant tags to set, just get the longest one */
616 if (count($relevant)){
617 ksort($relevant);
618 $tmp= array_keys($relevant);
619 $idx= end($tmp);
620 $tag= $relevant[$idx];
621 $this->gosaUnitTag= $tag;
622 }
623 }
624 }
626 /* Set tag? */
627 if ($tag != ""){
628 /* Set objectclass and attribute */
629 $ldap= $this->config->get_ldap_link();
630 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
631 $attrs= $ldap->fetch();
632 if(isset($attrs['gosaUnitTag'][0]) && $attrs['gosaUnitTag'][0] == $tag){
633 if ($show) {
634 echo sprintf(_("Object '%s' is already tagged"), @LDAP::fix($dn))."<br>";
635 flush();
636 }
637 return;
638 }
639 if (count($attrs)){
640 if ($show){
641 echo sprintf(_("Adding tag (%s) to object '%s'"), $tag, @LDAP::fix($dn))."<br>";
642 flush();
643 }
644 $nattrs= array("gosaUnitTag" => $tag);
645 $nattrs['objectClass']= array();
646 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
647 $oc= $attrs['objectClass'][$i];
648 if ($oc != "gosaAdministrativeUnitTag"){
649 $nattrs['objectClass'][]= $oc;
650 }
651 }
652 $nattrs['objectClass'][]= "gosaAdministrativeUnitTag";
653 $ldap->cd($dn);
654 $ldap->modify($nattrs);
655 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
656 } else {
657 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not tagging ($tag) $dn - seems to have moved away", "Tagging");
658 }
660 } else {
661 /* Remove objectclass and attribute */
662 $ldap= $this->config->get_ldap_link();
663 $ldap->cat($dn, array('gosaUnitTag', 'objectClass'));
664 $attrs= $ldap->fetch();
665 if (isset($attrs['objectClass']) && !in_array_ics("gosaAdministrativeUnitTag", $attrs['objectClass'])){
666 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "$dn is not tagged", "Tagging");
667 return;
668 }
669 if (count($attrs)){
670 if ($show){
671 echo sprintf(_("Removing tag from object '%s'"), @LDAP::fix($dn))."<br>";
672 flush();
673 }
674 $nattrs= array("gosaUnitTag" => array());
675 $nattrs['objectClass']= array();
676 for ($i= 0; $i<$attrs['objectClass']['count']; $i++){
677 $oc= $attrs['objectClass'][$i];
678 if ($oc != "gosaAdministrativeUnitTag"){
679 $nattrs['objectClass'][]= $oc;
680 }
681 }
682 $ldap->cd($dn);
683 $ldap->modify($nattrs);
684 show_ldap_error($ldap->get_error(), sprintf(_("Handle object tagging with dn '%s' failed."),$dn));
685 } else {
686 @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, "Not removing tag ($tag) $dn - seems to have moved away", "Tagging");
687 }
688 }
690 }
692 }
694 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
695 ?>