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= "";
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;
54 /* Temporary disable the Unit Tag functionalityi.
55 This is not used anymore, cause of the new acl implementation.
56 Setting this to TRUE will disable
57 all POST / GET activity, "Department tagging" and the checkbox within the generic template.
58 */
59 var $UnitTagDiabled = TRUE;
60 var $orig_base = "";
61 var $orig_ou = "";
63 function department ($config, $dn)
64 {
66 plugin::plugin($config, $dn);
67 $this->is_account= TRUE;
68 $this->ui= get_userinfo();
69 $this->dn= $dn;
70 $this->orig_dn= $dn;
71 $this->orig_ou= $this->ou;
72 $this->config= $config;
74 /* Set base */
75 if ($this->dn == "new"){
76 $ui= get_userinfo();
77 if(isset($_SESSION['CurrentMainBase'])){
78 $this->base= $_SESSION['CurrentMainBase'];
79 }else{
80 $this->base= dn2base($ui->dn);
81 }
82 } else {
83 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
84 }
86 $this->orig_base = $this->base;
88 /* Is administrational Unit? */
89 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
90 $this->is_administrational_unit= true;
91 $this->initially_was_tagged = true;
92 }
93 }
95 function execute()
96 {
97 /* Call parent execute */
98 plugin::execute();
100 /* Reload departments */
101 $this->config->get_departments($this->dn);
102 $this->config->make_idepartments();
103 $smarty= get_smarty();
105 $tmp = $this->plInfo();
106 foreach($tmp['plProvidedAcls'] as $name => $translation){
107 $smarty->assign($name."ACL",$this->getacl($name));
108 }
110 /* Base select dialog */
111 $once = true;
112 foreach($_POST as $name => $value){
113 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
114 $once = false;
115 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
116 $this->dialog->setCurrentBase($this->base);
117 }
118 }
120 /* Dialog handling */
121 if(is_object($this->dialog)){
122 /* Must be called before save_object */
123 $this->dialog->save_object();
125 if($this->dialog->isClosed()){
126 $this->dialog = false;
127 }elseif($this->dialog->isSelected()){
129 /* A new base was selected, check if it is a valid one */
130 $tmp = $this->get_allowed_bases();
131 if(isset($tmp[$this->dialog->isSelected()])){
132 $this->base = $this->dialog->isSelected();
133 }
135 $this->dialog= false;
136 }else{
137 return($this->dialog->execute());
138 }
139 }
141 /* Hide all departments, that are subtrees of this department */
142 $bases = $this->get_allowed_bases();
143 if(($this->dn == "new")||($this->dn == "")){
144 $tmp = $bases;
145 }else{
146 $tmp = array();
147 foreach($bases as $dn=>$base){
148 $fixed = str_replace("/","\\",$this->dn);
149 /* Only attach departments which are not a subtree of this one */
150 if(!preg_match("/".$fixed."/",$dn)){
151 $tmp[$dn]=$base;
152 }
153 }
154 }
155 $smarty->assign("bases", $tmp);
157 foreach ($this->attributes as $val){
158 $smarty->assign("$val", $this->$val);
159 }
160 $smarty->assign("base_select", $this->base);
162 /* Set admin unit flag */
163 $smarty->assign("UnitTagDiabled",$this->UnitTagDiabled);
164 if(!$this->UnitTagDiabled){
165 if ($this->is_administrational_unit) {
166 $smarty->assign("unitTag", "checked");
167 } else {
168 $smarty->assign("unitTag", "");
169 }
170 }
172 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
173 }
175 function clear_fields()
176 {
177 $this->dn = "";
178 $this->base = "";
180 foreach ($this->attributes as $val){
181 $this->$val= "";
182 }
183 }
185 function remove_from_parent()
186 {
187 $ldap= $this->config->get_ldap_link();
188 $ldap->cd ($this->dn);
189 $ldap->recursive_remove();
190 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
192 /* Optionally execute a command after we're done */
193 $this->handle_post_events('remove');
194 }
196 function must_be_tagged()
197 {
198 return $this->must_be_tagged;
199 }
201 function am_i_moved()
202 {
203 return $this->rec_cpy;
204 }
207 /* Save data to object */
208 function save_object()
209 {
210 if (isset($_POST['dep_generic_posted'])){
212 /* Create a base backup and reset the
213 base directly after calling plugin::save_object();
214 Base will be set seperatly a few lines below */
215 $base_tmp = $this->base;
216 plugin::save_object();
217 $this->base = $base_tmp;
219 /* Set new base if allowed */
220 $tmp = $this->get_allowed_bases();
221 if(isset($_POST['base'])){
222 if(isset($tmp[$_POST['base']])){
223 $this->base= $_POST['base'];
224 }
225 }
227 /* Save tagging flag */
228 if(!$this->UnitTagDiabled){
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 }
238 }
241 /* Check values */
242 function check()
243 {
244 /* Call common method to give check the hook */
245 $message= plugin::check();
247 /* Check for presence of this department */
248 $ldap= $this->config->get_ldap_link();
249 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
250 if ($this->orig_dn == "new" && $ldap->count()){
251 $message[]= _("Department with that 'Name' already exists.");
252 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
253 $message[]= _("Department with that 'Name' already exists.");
254 }
256 /* All required fields are set? */
257 if ($this->ou == ""){
258 $message[]= _("Required field 'Name' is not set.");
259 }
260 if ($this->description == ""){
261 $message[]= _("Required field 'Description' is not set.");
262 }
264 if(is_department_name_reserved($this->ou,$this->base)){
265 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
266 }
268 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
269 $message[]= _("The field 'Name' contains invalid characters.");
270 }
271 if (!is_phone_nr($this->telephoneNumber)){
272 $message[]= _("The field 'Phone' contains an invalid phone number.");
273 }
274 if (!is_phone_nr($this->facsimileTelephoneNumber)){
275 $message[]= _("The field 'Fax' contains an invalid phone number.");
276 }
278 return $message;
279 }
282 /* Save to LDAP */
283 function save()
284 {
285 $ldap= $this->config->get_ldap_link();
287 if(!$this->UnitTagDiabled){
289 /* Add tag objects if needed */
290 if ($this->is_administrational_unit){
292 /* If this wasn't tagged before add oc an reset unit tag */
293 if(!$this->initially_was_tagged){
294 $this->objectclasses[]= "gosaAdministrativeUnit";
295 $this->gosaUnitTag= "";
297 /* It seams that this method is called twice,
298 set this to true. to avoid adding this oc twice */
299 $this->initially_was_tagged = true;
300 }
302 if ($this->gosaUnitTag == ""){
304 /* It's unlikely, but check if already used... */
305 $try= 5;
306 $ldap->cd($this->config->current['BASE']);
307 while ($try--){
309 /* Generate microtime stamp as tag */
310 list($usec, $sec)= explode(" ", microtime());
311 $time_stamp= preg_replace("/\./", "", $sec.$usec);
313 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
314 if ($ldap->count() == 0){
315 break;
316 }
317 }
318 if($try == 0) {
319 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
320 return;
321 }
322 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
323 }
324 }
325 }
326 plugin::save();
328 if(!$this->UnitTagDiabled){
330 /* Remove tag information if needed */
331 if (!$this->is_administrational_unit){
332 $tmp= array();
334 /* Remove gosaAdministrativeUnit from this plugin */
335 $has_unit_tag= false;
336 foreach($this->attrs['objectClass'] as $oc){
337 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
338 $tmp[]= $oc;
339 }
340 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
341 $has_unit_tag= true;
342 }
343 }
344 $this->attrs['objectClass']= $tmp;
346 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
347 $this->attrs['gosaUnitTag']= array();
348 }
349 $this->gosaUnitTag = "";
350 }
351 }
353 /* Write back to ldap */
354 $ldap= $this->config->get_ldap_link();
355 $ldap->cat($this->dn, array('dn'));
356 $a= $ldap->fetch();
357 $ldap->cd($this->dn);
359 if (count($a)){
360 $this->cleanup();
361 $ldap->modify ($this->attrs);
363 $this->handle_post_events('modify');
364 } else {
365 $ldap->add($this->attrs);
366 $this->handle_post_events('add');
367 }
368 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
371 if(!$this->UnitTagDiabled){
372 /* The parameter forces only to set must_be_tagged, and don't touch any objects
373 This will be done later */
374 $this->tag_objects(true);
376 /* Fix tagging if needed */
377 if (!$this->is_administrational_unit){
378 $this->handle_object_tagging();
379 }
380 }
382 /* Optionally execute a command after we're done */
383 $this->postcreate();
384 return(false);
385 }
388 function ShowMoveFrame()
389 {
390 $smarty = get_smarty();
391 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
392 $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.");
393 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
394 return($display);
395 }
397 function ShowTagFrame()
398 {
399 $smarty = get_smarty();
400 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
401 $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.");
402 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
403 return($display);
404 }
406 /* Tag objects to have the gosaAdministrativeUnitTag */
407 function tag_objects($OnlySetTagFlag = false)
408 {
409 if(!$OnlySetTagFlag){
410 $smarty= get_smarty();
411 /* Print out html introduction */
412 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
413 <html>
414 <head>
415 <title></title>
416 <style type="text/css">@import url("themes/default/style.css");</style>
417 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
418 </head>
419 <body style="background: none; margin:4px;" id="body" >
420 ';
421 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
422 }
424 $add= $this->is_administrational_unit;
425 $len= strlen($this->dn);
426 $ldap= $this->config->get_ldap_link();
427 $ldap->cd($this->dn);
428 if ($add){
429 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
430 $this->gosaUnitTag.')))', array('dn'));
431 } else {
432 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
433 }
435 while ($attrs= $ldap->fetch()){
437 /* Skip self */
438 if ($attrs['dn'] == $this->dn){
439 continue;
440 }
442 /* Check for confilicting administrative units */
443 $fix= true;
444 foreach ($this->config->adepartments as $key => $tag){
445 /* This one is shorter than our dn, its not relevant... */
446 if ($len >= strlen($key)){
447 continue;
448 }
450 /* This one matches with the latter part. Break and don't fix this entry */
451 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
452 $fix= false;
453 break;
454 }
455 }
457 /* Fix entry if needed */
458 if ($fix){
459 if($OnlySetTagFlag){
460 $this->must_be_tagged =true;
461 return;
462 }
463 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
464 }
465 }
466 if(!$OnlySetTagFlag){
467 echo '<p class="seperator"> </p>';
468 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
469 <br><input type='submit' name='back' value='"._("Continue")."'>
470 </form></div>";
471 }
472 }
475 /* Move/Rename complete trees */
476 function recursive_move($src_dn, $dst_dn,$force = false)
477 {
478 /* If force == false prepare to recursive move this object from src to dst
479 on the next call. */
480 if(!$force){
481 $this->rec_cpy = true;
482 $this->rec_src = $src_dn;
483 $this->rec_dst = $dst_dn;
484 }else{
486 /* If this is called, but not wanted, abort */
487 if(!$this->rec_cpy){
488 return;
489 }
491 $src_dn = $this->rec_src;
492 $dst_dn = $this->rec_dst;
494 /* Print header to have styles included */
495 $smarty= get_smarty();
497 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
498 <html>
499 <head>
500 <title></title>
501 <style type="text/css">@import url("themes/default/style.css");</style>
502 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
503 </head>
504 <body style="background: none; margin:4px;" id="body" >
505 ';
506 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
509 /* Check if the destination entry exists */
510 $ldap= $this->config->get_ldap_link();
512 /* Check if destination exists - abort */
513 $ldap->cat($dst_dn, array('dn'));
514 if ($ldap->fetch()){
515 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
516 E_USER_WARNING);
517 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
518 return (FALSE);
519 }
521 /* Perform a search for all objects to be moved */
522 $objects= array();
523 $ldap->cd($src_dn);
524 $ldap->search("(objectClass=*)", array("dn"));
525 while($attrs= $ldap->fetch()){
526 $dn= $attrs['dn'];
527 $objects[$dn]= strlen($dn);
528 }
530 /* Sort objects by indent level */
531 asort($objects);
532 reset($objects);
534 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
535 foreach ($objects as $object => $len){
538 $src= str_replace("\\","\\\\",$object);
539 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
540 $dst= str_replace($src_dn,$dst_dn,$object);
542 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
544 $this->update_acls($object, $dst,TRUE);
546 if (!$this->copy($src, $dst)){
547 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
548 return (FALSE);
549 }
550 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
551 flush();
552 }
554 /* Remove src_dn */
555 $ldap->cd($src_dn);
556 $ldap->recursive_remove();
557 $this->dn = $this->rec_dst;
558 $this->rec_src = $this->rec_dst = "";
559 $this->rec_cpy =false;
561 echo '<p class="seperator"> </p>';
563 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
564 <br><input type='submit' name='back' value='"._("Continue")."'>
565 </form></div>";
567 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
568 echo "</body></html>";
570 return (TRUE);
571 }
572 }
575 /* Return plugin informations for acl handling */
576 function plInfo()
577 {
578 return (array("plShortName" => _("Generic"),
579 "plDescription" => _("Departments"),
580 "plSelfModify" => FALSE,
581 "plPriority" => 0,
582 "plDepends" => array(),
583 "plSection" => array("admin"),
584 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
586 "plProvidedAcls" => array(
587 "description" => _("Description"),
588 "c" => _("Country"),
589 "base" => _("Base"),
590 "l" => _("Location"),
591 "telephoneNumber" => _("Telephone"),
592 "ou" => _("Department name"),
593 "businessCategory" => _("Category"),
594 "st" => _("State"),
595 "postalAddress" => _("Address"),
596 "gosaUnitTag" => _("Administrative settings"),
597 "facsimileTelephoneNumber" => _("Fax"))
598 ));
599 }
600 }
601 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
602 ?>