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;
53 function department ($config, $dn)
54 {
56 plugin::plugin($config, $dn);
57 $this->is_account= TRUE;
58 $this->ui= get_userinfo();
59 $this->dn= $dn;
60 $this->orig_dn= $dn;
61 $this->config= $config;
63 /* Set base */
64 if ($this->dn == "new"){
65 $ui= get_userinfo();
66 if(isset($_SESSION['CurrentMainBase'])){
67 $this->base= $_SESSION['CurrentMainBase'];
68 }else{
69 $this->base= dn2base($ui->dn);
70 }
71 } else {
72 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
73 }
75 /* Is administrational Unit? */
76 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
77 $this->is_administrational_unit= true;
78 $this->initially_was_tagged = true;
79 }
80 }
82 function execute()
83 {
84 /* Call parent execute */
85 plugin::execute();
87 /* Reload departments */
88 $this->config->get_departments($this->dn);
89 $this->config->make_idepartments();
90 $smarty= get_smarty();
92 /* Assign base ACL */
93 $baseACL = $this->getacl("base");
94 if(!$this->acl_is_moveable()) {
95 $baseACL = preg_replace("/w/","",$baseACL);
96 }
97 $smarty->assign("baseACL", $baseACL);
99 $tmp = $this->plInfo();
100 foreach($tmp['plProvidedAcls'] as $name => $translation){
101 $smarty->assign($name."ACL",$this->getacl($name));
102 }
104 /* Get bases */
105 $ui = get_userinfo();
106 $check = $ui->get_module_departments("department");
107 $check[] = $this->base;
108 $bases = array();
109 foreach($check as $dn_allowed){
110 $bases[$dn_allowed] = $this->config->idepartments[$dn_allowed];
111 }
113 /* Base select dialog */
114 $once = true;
115 foreach($_POST as $name => $value){
116 if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
117 $once = false;
118 $this->dialog = new baseSelectDialog($this->config,$this,$bases);
119 $this->dialog->setCurrentBase($this->base);
120 }
121 }
123 /* Dialog handling */
124 if(is_object($this->dialog)){
125 /* Must be called before save_object */
126 $this->dialog->save_object();
128 if($this->dialog->isClosed()){
129 $this->dialog = false;
130 }elseif($this->dialog->isSelected()){
131 $this->base = $this->dialog->isSelected();
132 $this->dialog= false;
133 }else{
134 return($this->dialog->execute());
135 }
136 }
138 /* Hide all departments, that are subtrees of this department */
139 if(($this->dn == "new")||($this->dn == "")){
140 $tmp = $bases;
141 }else{
142 $tmp = array();
143 foreach($bases as $dn=>$base){
144 $fixed = str_replace("/","\\",$this->dn);
145 /* Only attach departments which are not a subtree of this one */
146 if(!preg_match("/".$fixed."/",$dn)){
147 $tmp[$dn]=$base;
148 }
149 }
150 }
151 $smarty->assign("bases", $tmp);
153 foreach ($this->attributes as $val){
154 $smarty->assign("$val", $this->$val);
155 }
156 $smarty->assign("base_select", $this->base);
158 /* Set admin unit flag */
159 if ($this->is_administrational_unit) {
160 $smarty->assign("unitTag", "checked");
161 } else {
162 $smarty->assign("unitTag", "");
163 }
165 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
166 }
168 function clear_fields()
169 {
170 $this->dn = "";
171 $this->base = "";
173 foreach ($this->attributes as $val){
174 $this->$val= "";
175 }
176 }
178 function remove_from_parent()
179 {
180 $ldap= $this->config->get_ldap_link();
181 $ldap->cd ($this->dn);
182 $ldap->recursive_remove();
183 show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
185 /* Optionally execute a command after we're done */
186 $this->handle_post_events('remove');
187 }
189 function must_be_tagged()
190 {
191 return $this->must_be_tagged;
192 }
194 function am_i_moved()
195 {
196 return $this->rec_cpy;
197 }
200 /* Save data to object */
201 function save_object()
202 {
203 if (isset($_POST['dep_generic_posted'])){
204 plugin::save_object();
206 /* Save base, since this is no LDAP attribute */
207 if($this->acl_is_moveable() && isset($_POST['base'])){
208 $this->base= $_POST['base'];
209 }
211 /* Save tagging flag */
212 if ($this->acl_is_writeable("unitTag")){
213 if (isset($_POST['unitTag'])){
214 $this->is_administrational_unit= true;
215 } else {
216 $this->is_administrational_unit= false;
217 }
218 }
219 }
220 }
223 /* Check values */
224 function check()
225 {
226 /* Call common method to give check the hook */
227 $message= plugin::check();
229 /* Permissions for that base? */
230 // $this->dn= "ou=$this->ou,".$this->base;
231 if ($this->acl_is_createable() && $this->dn == "new"){
232 $message[]= _("You have no permissions to create a department on this 'Base'.");
233 }
235 /* Check for presence of this department */
236 $ldap= $this->config->get_ldap_link();
237 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
238 if ($this->orig_dn == "new" && $ldap->count()){
239 $message[]= _("Department with that 'Name' already exists.");
240 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
241 $message[]= _("Department with that 'Name' already exists.");
242 }
244 /* All required fields are set? */
245 if ($this->ou == ""){
246 $message[]= _("Required field 'Name' is not set.");
247 }
248 if ($this->description == ""){
249 $message[]= _("Required field 'Description' is not set.");
250 }
252 if(is_department_name_reserved($this->ou,$this->base)){
253 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
254 }
256 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
257 $message[]= _("The field 'Name' contains invalid characters.");
258 }
259 if (!is_phone_nr($this->telephoneNumber)){
260 $message[]= _("The field 'Phone' contains an invalid phone number.");
261 }
262 if (!is_phone_nr($this->facsimileTelephoneNumber)){
263 $message[]= _("The field 'Fax' contains an invalid phone number.");
264 }
266 return $message;
267 }
270 /* Save to LDAP */
271 function save()
272 {
273 $ldap= $this->config->get_ldap_link();
275 /* Add tag objects if needed */
276 if ($this->is_administrational_unit){
278 /* If this wasn't tagged before add oc an reset unit tag */
279 if(!$this->initially_was_tagged){
280 $this->objectclasses[]= "gosaAdministrativeUnit";
281 $this->gosaUnitTag= "";
283 /* It seams that this method is called twice,
284 set this to true. to avoid adding this oc twice */
285 $this->initially_was_tagged = true;
286 }
288 if ($this->gosaUnitTag == ""){
290 /* It's unlikely, but check if already used... */
291 $try= 5;
292 $ldap->cd($this->config->current['BASE']);
293 while ($try--){
295 /* Generate microtime stamp as tag */
296 list($usec, $sec)= explode(" ", microtime());
297 $time_stamp= preg_replace("/\./", "", $sec.$usec);
299 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
300 if ($ldap->count() == 0){
301 break;
302 }
303 }
304 if($try == 0) {
305 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
306 return;
307 }
308 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
309 }
310 }
312 plugin::save();
314 /* Remove tag information if needed */
315 if (!$this->is_administrational_unit){
316 $tmp= array();
318 /* Remove gosaAdministrativeUnit from this plugin */
319 $has_unit_tag= false;
320 foreach($this->attrs['objectClass'] as $oc){
321 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
322 $tmp[]= $oc;
323 }
324 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
325 $has_unit_tag= true;
326 }
327 }
328 $this->attrs['objectClass']= $tmp;
330 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
331 $this->attrs['gosaUnitTag']= array();
332 }
333 $this->gosaUnitTag = "";
334 }
336 /* Write back to ldap */
337 $ldap= $this->config->get_ldap_link();
338 $ldap->cat($this->dn, array('dn'));
339 $a= $ldap->fetch();
340 $ldap->cd($this->dn);
342 if (count($a)){
343 $this->cleanup();
344 $ldap->modify ($this->attrs);
346 $this->handle_post_events('modify');
347 } else {
348 $ldap->add($this->attrs);
349 $this->handle_post_events('add');
350 }
351 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
353 /* The parameter forces only to set must_be_tagged, and don't touch any objects
354 This will be done later */
355 $this->tag_objects(true);
357 /* Fix tagging if needed */
358 if (!$this->is_administrational_unit){
359 $this->handle_object_tagging();
360 }
362 /* Optionally execute a command after we're done */
363 $this->postcreate();
364 return(false);
365 }
368 function ShowMoveFrame()
369 {
370 $smarty = get_smarty();
371 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
372 $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.");
373 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
374 return($display);
375 }
377 function ShowTagFrame()
378 {
379 $smarty = get_smarty();
380 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
381 $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.");
382 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
383 return($display);
384 }
386 /* Tag objects to have the gosaAdministrativeUnitTag */
387 function tag_objects($OnlySetTagFlag = false)
388 {
389 if(!$OnlySetTagFlag){
390 $smarty= get_smarty();
391 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
392 echo "<body style='background-image:none;margin:3px;color:black'>";
393 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
394 }
396 $add= $this->is_administrational_unit;
397 $len= strlen($this->dn);
398 $ldap= $this->config->get_ldap_link();
399 $ldap->cd($this->dn);
400 if ($add){
401 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
402 $this->gosaUnitTag.')))', array('dn'));
403 } else {
404 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
405 }
407 while ($attrs= $ldap->fetch()){
409 /* Skip self */
410 if ($attrs['dn'] == $this->dn){
411 continue;
412 }
414 /* Check for confilicting administrative units */
415 $fix= true;
416 foreach ($this->config->adepartments as $key => $tag){
417 /* This one is shorter than our dn, its not relevant... */
418 if ($len >= strlen($key)){
419 continue;
420 }
422 /* This one matches with the latter part. Break and don't fix this entry */
423 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
424 $fix= false;
425 break;
426 }
427 }
429 /* Fix entry if needed */
430 if ($fix){
431 if($OnlySetTagFlag){
432 $this->must_be_tagged =true;
433 return;
434 }
435 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
436 }
437 }
438 if(!$OnlySetTagFlag){
439 echo '<p class="seperator"> </p>';
440 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
441 <br><input type='submit' name='back' value='"._("Continue")."'>
442 </form></div>";
443 }
444 }
447 /* Move/Rename complete trees */
448 function recursive_move($src_dn, $dst_dn,$force = false)
449 {
450 /* If force == false prepare to recursive move this object from src to dst
451 on the next call. */
452 if(!$force){
453 $this->rec_cpy = true;
454 $this->rec_src = $src_dn;
455 $this->rec_dst = $dst_dn;
456 }else{
458 /* If this is called, but not wanted, abort */
459 if(!$this->rec_cpy){
460 return;
461 }
463 $src_dn = $this->rec_src;
464 $dst_dn = $this->rec_dst;
466 /* Print header to have styles included */
467 $smarty= get_smarty();
468 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
469 echo "<body style='background-image:none;margin:3px;color:black'>";
471 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
474 /* Check if the destination entry exists */
475 $ldap= $this->config->get_ldap_link();
477 /* Check if destination exists - abort */
478 $ldap->cat($dst_dn, array('dn'));
479 if ($ldap->fetch()){
480 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
481 E_USER_WARNING);
482 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
483 return (FALSE);
484 }
486 /* Perform a search for all objects to be moved */
487 $objects= array();
488 $ldap->cd($src_dn);
489 $ldap->search("(objectClass=*)", array("dn"));
490 while($attrs= $ldap->fetch()){
491 $dn= $attrs['dn'];
492 $objects[$dn]= strlen($dn);
493 }
495 /* Sort objects by indent level */
496 asort($objects);
497 reset($objects);
499 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
500 foreach ($objects as $object => $len){
503 $src= str_replace("\\","\\\\",$object);
504 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
505 $dst= str_replace($src_dn,$dst_dn,$object);
507 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
509 if (!$this->copy($src, $dst)){
510 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
511 return (FALSE);
512 }
514 flush();
515 }
517 /* Remove src_dn */
518 $ldap->cd($src_dn);
519 $ldap->recursive_remove();
520 $this->rec_src = $this->rec_dst = "";
521 $this->rec_cpy =false;
523 echo '<p class="seperator"> </p>';
525 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
526 <br><input type='submit' name='back' value='"._("Continue")."'>
527 </form></div>";
529 echo "</body></html>";
531 return (TRUE);
532 }
533 }
536 /* Return plugin informations for acl handling */
537 function plInfo()
538 {
539 return (array("plShortName" => _("Generic"),
540 "plDescription" => _("Departments"),
541 "plSelfModify" => FALSE,
542 "plPriority" => 0,
543 "plDepends" => array(),
544 "plSection" => array("admin"),
545 "plCategory" => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
547 "plProvidedAcls" => array(
548 "description" => _("Description"),
549 "c" => _("Country"),
550 "base" => _("Base"),
551 "l" => _("Location"),
552 "telephoneNumber" => _("Telephone"),
553 "ou" => _("Department name"),
554 "businessCategory" => _("Category"),
555 "st" => _("State"),
556 "postalAddress" => _("Address"),
557 "gosaUnitTag" => _("Administrative settings"),
558 "facsimileTelephoneNumber" => _("Fax"))
559 ));
560 }
561 }
562 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
563 ?>