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;
61 function department ($config, $dn)
62 {
64 plugin::plugin($config, $dn);
65 $this->is_account= TRUE;
66 $this->ui= get_userinfo();
67 $this->dn= $dn;
68 $this->orig_dn= $dn;
69 $this->config= $config;
71 /* Set base */
72 if ($this->dn == "new"){
73 $ui= get_userinfo();
74 if(isset($_SESSION['CurrentMainBase'])){
75 $this->base= $_SESSION['CurrentMainBase'];
76 }else{
77 $this->base= dn2base($ui->dn);
78 }
79 } else {
80 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
81 }
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 /* Reload departments */
96 $this->config->get_departments($this->dn);
97 $this->config->make_idepartments();
98 $smarty= get_smarty();
100 $tmp = $this->plInfo();
101 foreach($tmp['plProvidedAcls'] as $name => $translation){
102 $smarty->assign($name."ACL",$this->getacl($name));
103 }
105 /* Base select dialog */
106 $once = true;
107 foreach($_POST as $name => $value){
108 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
109 $once = false;
110 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
111 $this->dialog->setCurrentBase($this->base);
112 }
113 }
115 /* Dialog handling */
116 if(is_object($this->dialog)){
117 /* Must be called before save_object */
118 $this->dialog->save_object();
120 if($this->dialog->isClosed()){
121 $this->dialog = false;
122 }elseif($this->dialog->isSelected()){
124 /* A new base was selected, check if it is a valid one */
125 $tmp = $this->get_allowed_bases();
126 if(isset($tmp[$this->dialog->isSelected()])){
127 $this->base = $this->dialog->isSelected();
128 }
130 $this->dialog= false;
131 }else{
132 return($this->dialog->execute());
133 }
134 }
136 /* Hide all departments, that are subtrees of this department */
137 $bases = $this->get_allowed_bases();
138 if(($this->dn == "new")||($this->dn == "")){
139 $tmp = $bases;
140 }else{
141 $tmp = array();
142 foreach($bases as $dn=>$base){
143 $fixed = str_replace("/","\\",$this->dn);
144 /* Only attach departments which are not a subtree of this one */
145 if(!preg_match("/".$fixed."/",$dn)){
146 $tmp[$dn]=$base;
147 }
148 }
149 }
150 $smarty->assign("bases", $tmp);
152 foreach ($this->attributes as $val){
153 $smarty->assign("$val", $this->$val);
154 }
155 $smarty->assign("base_select", $this->base);
157 /* Set admin unit flag */
158 $smarty->assign("UnitTagDiabled",$this->UnitTagDiabled);
159 if(!$this->UnitTagDiabled){
160 if ($this->is_administrational_unit) {
161 $smarty->assign("unitTag", "checked");
162 } else {
163 $smarty->assign("unitTag", "");
164 }
165 }
167 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
168 }
170 function clear_fields()
171 {
172 $this->dn = "";
173 $this->base = "";
175 foreach ($this->attributes as $val){
176 $this->$val= "";
177 }
178 }
180 function remove_from_parent()
181 {
182 $ldap= $this->config->get_ldap_link();
183 $ldap->cd ($this->dn);
184 $ldap->recursive_remove();
185 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
187 /* Optionally execute a command after we're done */
188 $this->handle_post_events('remove');
189 }
191 function must_be_tagged()
192 {
193 return $this->must_be_tagged;
194 }
196 function am_i_moved()
197 {
198 return $this->rec_cpy;
199 }
202 /* Save data to object */
203 function save_object()
204 {
205 if (isset($_POST['dep_generic_posted'])){
207 /* Create a base backup and reset the
208 base directly after calling plugin::save_object();
209 Base will be set seperatly a few lines below */
210 $base_tmp = $this->base;
211 plugin::save_object();
212 $this->base = $base_tmp;
214 /* Set new base if allowed */
215 $tmp = $this->get_allowed_bases();
216 if(isset($_POST['base'])){
217 if(isset($tmp[$_POST['base']])){
218 $this->base= $_POST['base'];
219 }
220 }
222 /* Save tagging flag */
223 if(!$this->UnitTagDiabled){
224 if ($this->acl_is_writeable("unitTag")){
225 if (isset($_POST['unitTag'])){
226 $this->is_administrational_unit= true;
227 } else {
228 $this->is_administrational_unit= false;
229 }
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(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 (!is_phone_nr($this->telephoneNumber)){
267 $message[]= _("The field 'Phone' contains an invalid phone number.");
268 }
269 if (!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 if(!$this->UnitTagDiabled){
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 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
315 return;
316 }
317 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
318 }
319 }
320 }
321 plugin::save();
323 if(!$this->UnitTagDiabled){
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;
341 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
342 $this->attrs['gosaUnitTag']= array();
343 }
344 $this->gosaUnitTag = "";
345 }
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);
358 $this->handle_post_events('modify');
359 } else {
360 $ldap->add($this->attrs);
361 $this->handle_post_events('add');
362 }
363 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
366 if(!$this->UnitTagDiabled){
367 /* The parameter forces only to set must_be_tagged, and don't touch any objects
368 This will be done later */
369 $this->tag_objects(true);
371 /* Fix tagging if needed */
372 if (!$this->is_administrational_unit){
373 $this->handle_object_tagging();
374 }
375 }
377 /* Optionally execute a command after we're done */
378 $this->postcreate();
379 return(false);
380 }
383 function ShowMoveFrame()
384 {
385 $smarty = get_smarty();
386 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
387 $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.");
388 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
389 return($display);
390 }
392 function ShowTagFrame()
393 {
394 $smarty = get_smarty();
395 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
396 $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.");
397 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
398 return($display);
399 }
401 /* Tag objects to have the gosaAdministrativeUnitTag */
402 function tag_objects($OnlySetTagFlag = false)
403 {
404 if(!$OnlySetTagFlag){
405 $smarty= get_smarty();
406 /* Print out html introduction */
407 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
408 <html>
409 <head>
410 <title></title>
411 <style type="text/css">@import url("themes/default/style.css");</style>
412 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
413 </head>
414 <body style="background: none; margin:4px;" id="body" >
415 ';
416 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
417 }
419 $add= $this->is_administrational_unit;
420 $len= strlen($this->dn);
421 $ldap= $this->config->get_ldap_link();
422 $ldap->cd($this->dn);
423 if ($add){
424 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
425 $this->gosaUnitTag.')))', array('dn'));
426 } else {
427 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
428 }
430 while ($attrs= $ldap->fetch()){
432 /* Skip self */
433 if ($attrs['dn'] == $this->dn){
434 continue;
435 }
437 /* Check for confilicting administrative units */
438 $fix= true;
439 foreach ($this->config->adepartments as $key => $tag){
440 /* This one is shorter than our dn, its not relevant... */
441 if ($len >= strlen($key)){
442 continue;
443 }
445 /* This one matches with the latter part. Break and don't fix this entry */
446 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
447 $fix= false;
448 break;
449 }
450 }
452 /* Fix entry if needed */
453 if ($fix){
454 if($OnlySetTagFlag){
455 $this->must_be_tagged =true;
456 return;
457 }
458 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
459 }
460 }
461 if(!$OnlySetTagFlag){
462 echo '<p class="seperator"> </p>';
463 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
464 <br><input type='submit' name='back' value='"._("Continue")."'>
465 </form></div>";
466 }
467 }
470 /* Move/Rename complete trees */
471 function recursive_move($src_dn, $dst_dn,$force = false)
472 {
473 /* If force == false prepare to recursive move this object from src to dst
474 on the next call. */
475 if(!$force){
476 $this->rec_cpy = true;
477 $this->rec_src = $src_dn;
478 $this->rec_dst = $dst_dn;
479 }else{
481 /* If this is called, but not wanted, abort */
482 if(!$this->rec_cpy){
483 return;
484 }
486 $src_dn = $this->rec_src;
487 $dst_dn = $this->rec_dst;
489 /* Print header to have styles included */
490 $smarty= get_smarty();
492 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
493 <html>
494 <head>
495 <title></title>
496 <style type="text/css">@import url("themes/default/style.css");</style>
497 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
498 </head>
499 <body style="background: none; margin:4px;" id="body" >
500 ';
501 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
504 /* Check if the destination entry exists */
505 $ldap= $this->config->get_ldap_link();
507 /* Check if destination exists - abort */
508 $ldap->cat($dst_dn, array('dn'));
509 if ($ldap->fetch()){
510 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
511 E_USER_WARNING);
512 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
513 return (FALSE);
514 }
516 /* Perform a search for all objects to be moved */
517 $objects= array();
518 $ldap->cd($src_dn);
519 $ldap->search("(objectClass=*)", array("dn"));
520 while($attrs= $ldap->fetch()){
521 $dn= $attrs['dn'];
522 $objects[$dn]= strlen($dn);
523 }
525 /* Sort objects by indent level */
526 asort($objects);
527 reset($objects);
529 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
530 foreach ($objects as $object => $len){
533 $src= str_replace("\\","\\\\",$object);
534 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
535 $dst= str_replace($src_dn,$dst_dn,$object);
537 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
539 if (!$this->copy($src, $dst)){
540 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
541 return (FALSE);
542 }
543 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
544 flush();
545 }
547 /* Remove src_dn */
548 $ldap->cd($src_dn);
549 $ldap->recursive_remove();
550 $this->rec_src = $this->rec_dst = "";
551 $this->rec_cpy =false;
553 echo '<p class="seperator"> </p>';
555 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
556 <br><input type='submit' name='back' value='"._("Continue")."'>
557 </form></div>";
559 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
560 echo "</body></html>";
562 return (TRUE);
563 }
564 }
567 /* Return plugin informations for acl handling */
568 function plInfo()
569 {
570 return (array("plShortName" => _("Generic"),
571 "plDescription" => _("Departments"),
572 "plSelfModify" => FALSE,
573 "plPriority" => 0,
574 "plDepends" => array(),
575 "plSection" => array("admin"),
576 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
578 "plProvidedAcls" => array(
579 "description" => _("Description"),
580 "c" => _("Country"),
581 "base" => _("Base"),
582 "l" => _("Location"),
583 "telephoneNumber" => _("Telephone"),
584 "ou" => _("Department name"),
585 "businessCategory" => _("Category"),
586 "st" => _("State"),
587 "postalAddress" => _("Address"),
588 "gosaUnitTag" => _("Administrative settings"),
589 "facsimileTelephoneNumber" => _("Fax"))
590 ));
591 }
592 }
593 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
594 ?>