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 /* Assign base ACL */
101 $smarty->assign("baseACL", $this->getacl("base"));
103 $tmp = $this->plInfo();
104 foreach($tmp['plProvidedAcls'] as $name => $translation){
105 $smarty->assign($name."ACL",$this->getacl($name));
106 }
108 /* Base select dialog */
109 $once = true;
110 foreach($_POST as $name => $value){
111 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
112 $once = false;
113 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
114 $this->dialog->setCurrentBase($this->base);
115 }
116 }
118 /* Dialog handling */
119 if(is_object($this->dialog)){
120 /* Must be called before save_object */
121 $this->dialog->save_object();
123 if($this->dialog->isClosed()){
124 $this->dialog = false;
125 }elseif($this->dialog->isSelected()){
127 /* A new base was selected, check if it is a valid one */
128 $tmp = $this->get_allowed_bases();
129 if(isset($tmp[$this->dialog->isSelected()])){
130 $this->base = $this->dialog->isSelected();
131 }
133 $this->dialog= false;
134 }else{
135 return($this->dialog->execute());
136 }
137 }
139 /* Hide all departments, that are subtrees of this department */
140 $bases = $this->get_allowed_bases();
141 if(($this->dn == "new")||($this->dn == "")){
142 $tmp = $bases;
143 }else{
144 $tmp = array();
145 foreach($bases as $dn=>$base){
146 $fixed = str_replace("/","\\",$this->dn);
147 /* Only attach departments which are not a subtree of this one */
148 if(!preg_match("/".$fixed."/",$dn)){
149 $tmp[$dn]=$base;
150 }
151 }
152 }
153 $smarty->assign("bases", $tmp);
155 foreach ($this->attributes as $val){
156 $smarty->assign("$val", $this->$val);
157 }
158 $smarty->assign("base_select", $this->base);
160 /* Set admin unit flag */
161 $smarty->assign("UnitTagDiabled",$this->UnitTagDiabled);
162 if(!$this->UnitTagDiabled){
163 if ($this->is_administrational_unit) {
164 $smarty->assign("unitTag", "checked");
165 } else {
166 $smarty->assign("unitTag", "");
167 }
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 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
190 /* Optionally execute a command after we're done */
191 $this->handle_post_events('remove');
192 }
194 function must_be_tagged()
195 {
196 return $this->must_be_tagged;
197 }
199 function am_i_moved()
200 {
201 return $this->rec_cpy;
202 }
205 /* Save data to object */
206 function save_object()
207 {
208 if (isset($_POST['dep_generic_posted'])){
210 /* Create a base backup and reset the
211 base directly after calling plugin::save_object();
212 Base will be set seperatly a few lines below */
213 $base_tmp = $this->base;
214 plugin::save_object();
215 $this->base = $base_tmp;
217 /* Set new base if allowed */
218 $tmp = $this->get_allowed_bases();
219 if(isset($_POST['base'])){
220 if(isset($tmp[$_POST['base']])){
221 $this->base= $_POST['base'];
222 }
223 }
225 /* Save tagging flag */
226 if(!$this->UnitTagDiabled){
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 }
236 }
239 /* Check values */
240 function check()
241 {
242 /* Call common method to give check the hook */
243 $message= plugin::check();
246 /* Set new acl base */
247 if($this->dn == "new") {
248 $this->set_acl_base($this->base);
249 }
251 /* Permissions for that base? */
252 // $this->dn= "ou=$this->ou,".$this->base;
253 if (!$this->acl_is_createable() && $this->dn == "new"){
254 $message[]= _("You have no permissions to create a department on this 'Base'.");
255 }
257 /* Check for presence of this department */
258 $ldap= $this->config->get_ldap_link();
259 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
260 if ($this->orig_dn == "new" && $ldap->count()){
261 $message[]= _("Department with that 'Name' already exists.");
262 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
263 $message[]= _("Department with that 'Name' already exists.");
264 }
266 /* All required fields are set? */
267 if ($this->ou == ""){
268 $message[]= _("Required field 'Name' is not set.");
269 }
270 if ($this->description == ""){
271 $message[]= _("Required field 'Description' is not set.");
272 }
274 if(is_department_name_reserved($this->ou,$this->base)){
275 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
276 }
278 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
279 $message[]= _("The field 'Name' contains invalid characters.");
280 }
281 if (!is_phone_nr($this->telephoneNumber)){
282 $message[]= _("The field 'Phone' contains an invalid phone number.");
283 }
284 if (!is_phone_nr($this->facsimileTelephoneNumber)){
285 $message[]= _("The field 'Fax' contains an invalid phone number.");
286 }
288 return $message;
289 }
292 /* Save to LDAP */
293 function save()
294 {
295 $ldap= $this->config->get_ldap_link();
297 if(!$this->UnitTagDiabled){
299 /* Add tag objects if needed */
300 if ($this->is_administrational_unit){
302 /* If this wasn't tagged before add oc an reset unit tag */
303 if(!$this->initially_was_tagged){
304 $this->objectclasses[]= "gosaAdministrativeUnit";
305 $this->gosaUnitTag= "";
307 /* It seams that this method is called twice,
308 set this to true. to avoid adding this oc twice */
309 $this->initially_was_tagged = true;
310 }
312 if ($this->gosaUnitTag == ""){
314 /* It's unlikely, but check if already used... */
315 $try= 5;
316 $ldap->cd($this->config->current['BASE']);
317 while ($try--){
319 /* Generate microtime stamp as tag */
320 list($usec, $sec)= explode(" ", microtime());
321 $time_stamp= preg_replace("/\./", "", $sec.$usec);
323 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
324 if ($ldap->count() == 0){
325 break;
326 }
327 }
328 if($try == 0) {
329 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
330 return;
331 }
332 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
333 }
334 }
335 }
336 plugin::save();
338 if(!$this->UnitTagDiabled){
340 /* Remove tag information if needed */
341 if (!$this->is_administrational_unit){
342 $tmp= array();
344 /* Remove gosaAdministrativeUnit from this plugin */
345 $has_unit_tag= false;
346 foreach($this->attrs['objectClass'] as $oc){
347 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
348 $tmp[]= $oc;
349 }
350 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
351 $has_unit_tag= true;
352 }
353 }
354 $this->attrs['objectClass']= $tmp;
356 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
357 $this->attrs['gosaUnitTag']= array();
358 }
359 $this->gosaUnitTag = "";
360 }
361 }
363 /* Write back to ldap */
364 $ldap= $this->config->get_ldap_link();
365 $ldap->cat($this->dn, array('dn'));
366 $a= $ldap->fetch();
367 $ldap->cd($this->dn);
369 if (count($a)){
370 $this->cleanup();
371 $ldap->modify ($this->attrs);
373 $this->handle_post_events('modify');
374 } else {
375 $ldap->add($this->attrs);
376 $this->handle_post_events('add');
377 }
378 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
381 if(!$this->UnitTagDiabled){
382 /* The parameter forces only to set must_be_tagged, and don't touch any objects
383 This will be done later */
384 $this->tag_objects(true);
386 /* Fix tagging if needed */
387 if (!$this->is_administrational_unit){
388 $this->handle_object_tagging();
389 }
390 }
392 /* Optionally execute a command after we're done */
393 $this->postcreate();
394 return(false);
395 }
398 function ShowMoveFrame()
399 {
400 $smarty = get_smarty();
401 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
402 $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.");
403 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
404 return($display);
405 }
407 function ShowTagFrame()
408 {
409 $smarty = get_smarty();
410 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
411 $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.");
412 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
413 return($display);
414 }
416 /* Tag objects to have the gosaAdministrativeUnitTag */
417 function tag_objects($OnlySetTagFlag = false)
418 {
419 if(!$OnlySetTagFlag){
420 $smarty= get_smarty();
421 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
422 echo "<body style='background-image:none;margin:3px;color:black'>";
423 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
424 }
426 $add= $this->is_administrational_unit;
427 $len= strlen($this->dn);
428 $ldap= $this->config->get_ldap_link();
429 $ldap->cd($this->dn);
430 if ($add){
431 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
432 $this->gosaUnitTag.')))', array('dn'));
433 } else {
434 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
435 }
437 while ($attrs= $ldap->fetch()){
439 /* Skip self */
440 if ($attrs['dn'] == $this->dn){
441 continue;
442 }
444 /* Check for confilicting administrative units */
445 $fix= true;
446 foreach ($this->config->adepartments as $key => $tag){
447 /* This one is shorter than our dn, its not relevant... */
448 if ($len >= strlen($key)){
449 continue;
450 }
452 /* This one matches with the latter part. Break and don't fix this entry */
453 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
454 $fix= false;
455 break;
456 }
457 }
459 /* Fix entry if needed */
460 if ($fix){
461 if($OnlySetTagFlag){
462 $this->must_be_tagged =true;
463 return;
464 }
465 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
466 }
467 }
468 if(!$OnlySetTagFlag){
469 echo '<p class="seperator"> </p>';
470 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
471 <br><input type='submit' name='back' value='"._("Continue")."'>
472 </form></div>";
473 }
474 }
477 /* Move/Rename complete trees */
478 function recursive_move($src_dn, $dst_dn,$force = false)
479 {
480 /* If force == false prepare to recursive move this object from src to dst
481 on the next call. */
482 if(!$force){
483 $this->rec_cpy = true;
484 $this->rec_src = $src_dn;
485 $this->rec_dst = $dst_dn;
486 }else{
488 /* If this is called, but not wanted, abort */
489 if(!$this->rec_cpy){
490 return;
491 }
493 $src_dn = $this->rec_src;
494 $dst_dn = $this->rec_dst;
496 /* Print header to have styles included */
497 $smarty= get_smarty();
498 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
499 echo "<body style='background-image:none;margin:3px;color:black'>";
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 }
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 "</body></html>";
561 return (TRUE);
562 }
563 }
566 /* Return plugin informations for acl handling */
567 function plInfo()
568 {
569 return (array("plShortName" => _("Generic"),
570 "plDescription" => _("Departments"),
571 "plSelfModify" => FALSE,
572 "plPriority" => 0,
573 "plDepends" => array(),
574 "plSection" => array("admin"),
575 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
577 "plProvidedAcls" => array(
578 "description" => _("Description"),
579 "c" => _("Country"),
580 "base" => _("Base"),
581 "l" => _("Location"),
582 "telephoneNumber" => _("Telephone"),
583 "ou" => _("Department name"),
584 "businessCategory" => _("Category"),
585 "st" => _("State"),
586 "postalAddress" => _("Address"),
587 "gosaUnitTag" => _("Administrative settings"),
588 "facsimileTelephoneNumber" => _("Fax"))
589 ));
590 }
591 }
592 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
593 ?>