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= "";
36 var $view_logged = FALSE;
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(session::is_set('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 /* Log view */
101 if($this->is_account && !$this->view_logged){
102 $this->view_logged = TRUE;
103 new log("view","department/".get_class($this),$this->dn);
104 }
106 /* Reload departments */
107 $this->config->get_departments($this->dn);
108 $this->config->make_idepartments();
109 $smarty= get_smarty();
111 $tmp = $this->plInfo();
112 foreach($tmp['plProvidedAcls'] as $name => $translation){
113 $smarty->assign($name."ACL",$this->getacl($name));
114 }
116 /* Base select dialog */
117 $once = true;
118 foreach($_POST as $name => $value){
119 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
120 $once = false;
121 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
122 $this->dialog->setCurrentBase($this->base);
123 }
124 }
126 /* Dialog handling */
127 if(is_object($this->dialog)){
128 /* Must be called before save_object */
129 $this->dialog->save_object();
131 if($this->dialog->isClosed()){
132 $this->dialog = false;
133 }elseif($this->dialog->isSelected()){
135 /* A new base was selected, check if it is a valid one */
136 $tmp = $this->get_allowed_bases();
137 if(isset($tmp[$this->dialog->isSelected()])){
138 $this->base = $this->dialog->isSelected();
139 }
141 $this->dialog= false;
142 }else{
143 return($this->dialog->execute());
144 }
145 }
147 /* Hide all departments, that are subtrees of this department */
148 $bases = $this->get_allowed_bases();
149 if(($this->dn == "new")||($this->dn == "")){
150 $tmp = $bases;
151 }else{
152 $tmp = array();
153 foreach($bases as $dn=>$base){
154 $fixed = str_replace("/","\\",$this->dn);
155 /* Only attach departments which are not a subtree of this one */
156 if(!preg_match("/".$fixed."/",$dn)){
157 $tmp[$dn]=$base;
158 }
159 }
160 }
161 $smarty->assign("bases", $tmp);
163 foreach ($this->attributes as $val){
164 $smarty->assign("$val", $this->$val);
165 }
166 $smarty->assign("base_select", $this->base);
168 /* Set admin unit flag */
169 $smarty->assign("UnitTagDiabled",$this->UnitTagDiabled);
170 if(!$this->UnitTagDiabled){
171 if ($this->is_administrational_unit) {
172 $smarty->assign("unitTag", "checked");
173 } else {
174 $smarty->assign("unitTag", "");
175 }
176 }
178 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
179 }
181 function clear_fields()
182 {
183 $this->dn = "";
184 $this->base = "";
186 foreach ($this->attributes as $val){
187 $this->$val= "";
188 }
189 }
191 function remove_from_parent()
192 {
193 $ldap= $this->config->get_ldap_link();
194 $ldap->cd ($this->dn);
195 $ldap->recursive_remove();
196 new log("remove","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
197 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
199 /* Optionally execute a command after we're done */
200 $this->handle_post_events('remove');
201 }
203 function must_be_tagged()
204 {
205 return $this->must_be_tagged;
206 }
208 function am_i_moved()
209 {
210 return $this->rec_cpy;
211 }
214 /* Save data to object */
215 function save_object()
216 {
217 if (isset($_POST['dep_generic_posted'])){
219 /* Create a base backup and reset the
220 base directly after calling plugin::save_object();
221 Base will be set seperatly a few lines below */
222 $base_tmp = $this->base;
223 plugin::save_object();
224 $this->base = $base_tmp;
226 /* Set new base if allowed */
227 $tmp = $this->get_allowed_bases();
228 if(isset($_POST['base'])){
229 if(isset($tmp[$_POST['base']])){
230 $this->base= $_POST['base'];
231 }
232 }
234 /* Save tagging flag */
235 if(!$this->UnitTagDiabled){
236 if ($this->acl_is_writeable("unitTag")){
237 if (isset($_POST['unitTag'])){
238 $this->is_administrational_unit= true;
239 } else {
240 $this->is_administrational_unit= false;
241 }
242 }
243 }
244 }
245 }
248 /* Check values */
249 function check()
250 {
251 /* Call common method to give check the hook */
252 $message= plugin::check();
254 /* Check for presence of this department */
255 $ldap= $this->config->get_ldap_link();
256 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
257 if ($this->orig_dn == "new" && $ldap->count()){
258 $message[]= _("Department with that 'Name' already exists.");
259 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
260 $message[]= _("Department with that 'Name' already exists.");
261 }
263 /* All required fields are set? */
264 if ($this->ou == ""){
265 $message[]= _("Required field 'Name' is not set.");
266 }
267 if ($this->description == ""){
268 $message[]= _("Required field 'Description' is not set.");
269 }
271 if(is_department_name_reserved($this->ou,$this->base)){
272 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
273 }
275 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
276 $message[]= _("The field 'Name' contains invalid characters.");
277 }
278 if (!is_phone_nr($this->telephoneNumber)){
279 $message[]= _("The field 'Phone' contains an invalid phone number.");
280 }
281 if (!is_phone_nr($this->facsimileTelephoneNumber)){
282 $message[]= _("The field 'Fax' contains an invalid phone number.");
283 }
285 return $message;
286 }
289 /* Save to LDAP */
290 function save()
291 {
292 $ldap= $this->config->get_ldap_link();
294 if(!$this->UnitTagDiabled){
296 /* Add tag objects if needed */
297 if ($this->is_administrational_unit){
299 /* If this wasn't tagged before add oc an reset unit tag */
300 if(!$this->initially_was_tagged){
301 $this->objectclasses[]= "gosaAdministrativeUnit";
302 $this->gosaUnitTag= "";
304 /* It seams that this method is called twice,
305 set this to true. to avoid adding this oc twice */
306 $this->initially_was_tagged = true;
307 }
309 if ($this->gosaUnitTag == ""){
311 /* It's unlikely, but check if already used... */
312 $try= 5;
313 $ldap->cd($this->config->current['BASE']);
314 while ($try--){
316 /* Generate microtime stamp as tag */
317 list($usec, $sec)= explode(" ", microtime());
318 $time_stamp= preg_replace("/\./", "", $sec.$usec);
320 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
321 if ($ldap->count() == 0){
322 break;
323 }
324 }
325 if($try == 0) {
326 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
327 return;
328 }
329 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
330 }
331 }
332 }
333 plugin::save();
335 if(!$this->UnitTagDiabled){
337 /* Remove tag information if needed */
338 if (!$this->is_administrational_unit){
339 $tmp= array();
341 /* Remove gosaAdministrativeUnit from this plugin */
342 $has_unit_tag= false;
343 foreach($this->attrs['objectClass'] as $oc){
344 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
345 $tmp[]= $oc;
346 }
347 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
348 $has_unit_tag= true;
349 }
350 }
351 $this->attrs['objectClass']= $tmp;
353 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
354 $this->attrs['gosaUnitTag']= array();
355 }
356 $this->gosaUnitTag = "";
357 }
358 }
360 /* Write back to ldap */
361 $ldap= $this->config->get_ldap_link();
362 $ldap->cat($this->dn, array('dn'));
363 $a= $ldap->fetch();
364 $ldap->cd($this->dn);
366 if (count($a)){
367 $this->cleanup();
368 $ldap->modify ($this->attrs);
369 new log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
370 $this->handle_post_events('modify');
371 } else {
372 $ldap->add($this->attrs);
373 $this->handle_post_events('add');
374 new log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
375 }
376 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
379 if(!$this->UnitTagDiabled){
380 /* The parameter forces only to set must_be_tagged, and don't touch any objects
381 This will be done later */
382 $this->tag_objects(true);
384 /* Fix tagging if needed */
385 if (!$this->is_administrational_unit){
386 $this->handle_object_tagging();
387 }
388 }
390 /* Optionally execute a command after we're done */
391 $this->postcreate();
392 return(false);
393 }
396 function ShowMoveFrame()
397 {
398 $smarty = get_smarty();
399 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
400 $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.");
401 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
402 return($display);
403 }
405 function ShowTagFrame()
406 {
407 $smarty = get_smarty();
408 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
409 $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.");
410 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
411 return($display);
412 }
414 /* Tag objects to have the gosaAdministrativeUnitTag */
415 function tag_objects($OnlySetTagFlag = false)
416 {
417 if(!$OnlySetTagFlag){
418 $smarty= get_smarty();
419 /* Print out html introduction */
420 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
421 <html>
422 <head>
423 <title></title>
424 <style type="text/css">@import url("themes/default/style.css");</style>
425 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
426 </head>
427 <body style="background: none; margin:4px;" id="body" >
428 ';
429 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
430 }
432 $add= $this->is_administrational_unit;
433 $len= strlen($this->dn);
434 $ldap= $this->config->get_ldap_link();
435 $ldap->cd($this->dn);
436 if ($add){
437 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
438 $this->gosaUnitTag.')))', array('dn'));
439 } else {
440 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
441 }
443 while ($attrs= $ldap->fetch()){
445 /* Skip self */
446 if ($attrs['dn'] == $this->dn){
447 continue;
448 }
450 /* Check for confilicting administrative units */
451 $fix= true;
452 foreach ($this->config->adepartments as $key => $tag){
453 /* This one is shorter than our dn, its not relevant... */
454 if ($len >= strlen($key)){
455 continue;
456 }
458 /* This one matches with the latter part. Break and don't fix this entry */
459 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
460 $fix= false;
461 break;
462 }
463 }
465 /* Fix entry if needed */
466 if ($fix){
467 if($OnlySetTagFlag){
468 $this->must_be_tagged =true;
469 return;
470 }
471 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
472 }
473 }
474 if(!$OnlySetTagFlag){
475 echo '<p class="seperator"> </p>';
476 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
477 <br><input type='submit' name='back' value='"._("Continue")."'>
478 </form></div>";
479 }
480 }
483 /* Move/Rename complete trees */
484 function recursive_move($src_dn, $dst_dn,$force = false)
485 {
486 /* If force == false prepare to recursive move this object from src to dst
487 on the next call. */
488 if(!$force){
489 $this->rec_cpy = true;
490 $this->rec_src = $src_dn;
491 $this->rec_dst = $dst_dn;
492 }else{
494 /* If this is called, but not wanted, abort */
495 if(!$this->rec_cpy){
496 return;
497 }
499 $src_dn = $this->rec_src;
500 $dst_dn = $this->rec_dst;
502 /* Print header to have styles included */
503 $smarty= get_smarty();
505 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
506 <html>
507 <head>
508 <title></title>
509 <style type="text/css">@import url("themes/default/style.css");</style>
510 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
511 </head>
512 <body style="background: none; margin:4px;" id="body" >
513 ';
514 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
517 /* Check if the destination entry exists */
518 $ldap= $this->config->get_ldap_link();
520 /* Check if destination exists - abort */
521 $ldap->cat($dst_dn, array('dn'));
522 if ($ldap->fetch()){
523 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
524 E_USER_WARNING);
525 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
526 return (FALSE);
527 }
529 /* Perform a search for all objects to be moved */
530 $objects= array();
531 $ldap->cd($src_dn);
532 $ldap->search("(objectClass=*)", array("dn"));
533 while($attrs= $ldap->fetch()){
534 $dn= $attrs['dn'];
535 $objects[$dn]= strlen($dn);
536 }
538 /* Sort objects by indent level */
539 asort($objects);
540 reset($objects);
542 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
543 foreach ($objects as $object => $len){
546 $src= str_replace("\\","\\\\",$object);
547 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
548 $dst= str_replace($src_dn,$dst_dn,$object);
550 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
552 $this->update_acls($object, $dst,TRUE);
554 if (!$this->copy($src, $dst)){
555 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
556 return (FALSE);
557 }
558 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
559 flush();
560 }
562 /* Remove src_dn */
563 $ldap->cd($src_dn);
564 $ldap->recursive_remove();
565 $this->dn = $this->rec_dst;
566 $this->rec_src = $this->rec_dst = "";
567 $this->rec_cpy =false;
569 echo '<p class="seperator"> </p>';
571 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
572 <br><input type='submit' name='back' value='"._("Continue")."'>
573 </form></div>";
575 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
576 echo "</body></html>";
578 return (TRUE);
579 }
580 }
583 /* Return plugin informations for acl handling */
584 static function plInfo()
585 {
586 return (array("plShortName" => _("Generic"),
587 "plDescription" => _("Departments"),
588 "plSelfModify" => FALSE,
589 "plPriority" => 0,
590 "plDepends" => array(),
591 "plSection" => array("admin"),
592 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
594 "plProvidedAcls" => array(
595 "description" => _("Description"),
596 "c" => _("Country"),
597 "base" => _("Base"),
598 "l" => _("Location"),
599 "telephoneNumber" => _("Telephone"),
600 "ou" => _("Department name"),
601 "businessCategory" => _("Category"),
602 "st" => _("State"),
603 "postalAddress" => _("Address"),
604 "gosaUnitTag" => _("Administrative settings"),
605 "facsimileTelephoneNumber" => _("Fax"))
606 ));
607 }
608 }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>