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 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
407 echo "<body style='background-image:none;margin:3px;color:black'>";
408 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
409 }
411 $add= $this->is_administrational_unit;
412 $len= strlen($this->dn);
413 $ldap= $this->config->get_ldap_link();
414 $ldap->cd($this->dn);
415 if ($add){
416 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
417 $this->gosaUnitTag.')))', array('dn'));
418 } else {
419 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
420 }
422 while ($attrs= $ldap->fetch()){
424 /* Skip self */
425 if ($attrs['dn'] == $this->dn){
426 continue;
427 }
429 /* Check for confilicting administrative units */
430 $fix= true;
431 foreach ($this->config->adepartments as $key => $tag){
432 /* This one is shorter than our dn, its not relevant... */
433 if ($len >= strlen($key)){
434 continue;
435 }
437 /* This one matches with the latter part. Break and don't fix this entry */
438 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
439 $fix= false;
440 break;
441 }
442 }
444 /* Fix entry if needed */
445 if ($fix){
446 if($OnlySetTagFlag){
447 $this->must_be_tagged =true;
448 return;
449 }
450 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
451 }
452 }
453 if(!$OnlySetTagFlag){
454 echo '<p class="seperator"> </p>';
455 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
456 <br><input type='submit' name='back' value='"._("Continue")."'>
457 </form></div>";
458 }
459 }
462 /* Move/Rename complete trees */
463 function recursive_move($src_dn, $dst_dn,$force = false)
464 {
465 /* If force == false prepare to recursive move this object from src to dst
466 on the next call. */
467 if(!$force){
468 $this->rec_cpy = true;
469 $this->rec_src = $src_dn;
470 $this->rec_dst = $dst_dn;
471 }else{
473 /* If this is called, but not wanted, abort */
474 if(!$this->rec_cpy){
475 return;
476 }
478 $src_dn = $this->rec_src;
479 $dst_dn = $this->rec_dst;
481 /* Print header to have styles included */
482 $smarty= get_smarty();
483 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
484 echo "<body style='background-image:none;margin:3px;color:black'>";
486 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
489 /* Check if the destination entry exists */
490 $ldap= $this->config->get_ldap_link();
492 /* Check if destination exists - abort */
493 $ldap->cat($dst_dn, array('dn'));
494 if ($ldap->fetch()){
495 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
496 E_USER_WARNING);
497 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
498 return (FALSE);
499 }
501 /* Perform a search for all objects to be moved */
502 $objects= array();
503 $ldap->cd($src_dn);
504 $ldap->search("(objectClass=*)", array("dn"));
505 while($attrs= $ldap->fetch()){
506 $dn= $attrs['dn'];
507 $objects[$dn]= strlen($dn);
508 }
510 /* Sort objects by indent level */
511 asort($objects);
512 reset($objects);
514 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
515 foreach ($objects as $object => $len){
518 $src= str_replace("\\","\\\\",$object);
519 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
520 $dst= str_replace($src_dn,$dst_dn,$object);
522 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
524 if (!$this->copy($src, $dst)){
525 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
526 return (FALSE);
527 }
529 flush();
530 }
532 /* Remove src_dn */
533 $ldap->cd($src_dn);
534 $ldap->recursive_remove();
535 $this->rec_src = $this->rec_dst = "";
536 $this->rec_cpy =false;
538 echo '<p class="seperator"> </p>';
540 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
541 <br><input type='submit' name='back' value='"._("Continue")."'>
542 </form></div>";
544 echo "</body></html>";
546 return (TRUE);
547 }
548 }
551 /* Return plugin informations for acl handling */
552 function plInfo()
553 {
554 return (array("plShortName" => _("Generic"),
555 "plDescription" => _("Departments"),
556 "plSelfModify" => FALSE,
557 "plPriority" => 0,
558 "plDepends" => array(),
559 "plSection" => array("admin"),
560 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
562 "plProvidedAcls" => array(
563 "description" => _("Description"),
564 "c" => _("Country"),
565 "base" => _("Base"),
566 "l" => _("Location"),
567 "telephoneNumber" => _("Telephone"),
568 "ou" => _("Department name"),
569 "businessCategory" => _("Category"),
570 "st" => _("State"),
571 "postalAddress" => _("Address"),
572 "gosaUnitTag" => _("Administrative settings"),
573 "facsimileTelephoneNumber" => _("Fax"))
574 ));
575 }
576 }
577 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
578 ?>