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 $initially_was_tagged = FALSE;
38 var $rec_dst=false; // Destination for recursive move
39 var $rec_src=false; // Source for recursive move
40 var $rec_cpy=false; // Is recursive move requested ?
42 /* Headpage attributes */
43 var $last_dep_sorting= "invalid";
44 var $departments= array();
46 var $must_be_tagged = false;
48 /* attribute list for save action */
49 var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
50 "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
51 var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
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 /* set permissions */
76 $ui= get_userinfo();
77 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
78 $this->acl= get_module_permission($acl, "department", $ui->dn);
80 /* Is administrational Unit? */
81 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
82 $this->is_administrational_unit= true;
83 $this->initially_was_tagged = TRUE;
84 }
85 }
87 function execute()
88 {
89 /* Call parent execute */
90 plugin::execute();
92 /* Reload departments */
93 $this->config->get_departments($this->dn);
94 $this->config->make_idepartments();
95 $smarty= get_smarty();
97 /* Base select dialog */
98 $once = true;
99 foreach($_POST as $name => $value){
100 if(preg_match("/^chooseBase/",$name) && $once){
101 $once = false;
102 $this->dialog = new baseSelectDialog($this->config);
103 $this->dialog->setCurrentBase($this->base);
104 }
105 }
107 /* Dialog handling */
108 if(is_object($this->dialog)){
109 /* Must be called before save_object */
110 $this->dialog->save_object();
112 if($this->dialog->isClosed()){
113 $this->dialog = false;
114 }elseif($this->dialog->isSelected()){
115 $this->base = $this->dialog->isSelected();
116 $this->dialog= false;
117 }else{
118 return($this->dialog->execute());
119 }
120 }
122 /* Hide all departments, that are subtrees of this department */
123 $bases = $this->config->idepartments;
124 if(($this->dn == "new")||($this->dn == "")){
125 $tmp = $bases;
126 }else{
127 $tmp = array();
128 foreach($bases as $dn=>$base){
129 $fixed = str_replace("/","\\",$this->dn);
130 /* Only attach departments which are not a subtree of this one */
131 if(!preg_match("/".$fixed."/",$dn)){
132 $tmp[$dn]=$base;
133 }
134 }
135 }
136 $smarty->assign("bases", $tmp);
138 foreach ($this->attributes as $val){
139 $smarty->assign("$val", $this->$val);
140 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
141 }
142 $smarty->assign("baseACL", chkacl($this->acl,"base"));
143 $smarty->assign("base_select", $this->base);
145 /* Set admin unit flag */
146 if ($this->is_administrational_unit) {
147 $smarty->assign("unitTag", "checked");
148 } else {
149 $smarty->assign("unitTag", "");
150 }
151 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
153 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
154 }
156 function clear_fields()
157 {
158 $this->dn= "";
159 $this->base= "";
160 $this->acl= "#none#";
162 foreach ($this->attributes as $val){
163 $this->$val= "";
164 }
165 }
167 function remove_from_parent()
168 {
169 $ldap= $this->config->get_ldap_link();
170 $ldap->cd ($this->dn);
171 $ldap->recursive_remove();
172 show_ldap_error($ldap->get_error(), _("Removing department failed"));
174 /* Optionally execute a command after we're done */
175 $this->handle_post_events('remove');
176 }
178 function must_be_tagged()
179 {
180 return $this->must_be_tagged;
181 }
183 function am_i_moved()
184 {
185 return $this->rec_cpy;
186 }
189 /* Save data to object */
190 function save_object()
191 {
192 if (isset($_POST['base'])){
193 plugin::save_object();
195 /* Save base, since this is no LDAP attribute */
196 if (chkacl($this->acl, "create") == ""){
197 $this->base= $_POST['base'];
198 }
200 /* Save tagging flag */
201 if (chkacl($this->acl, "unitTag") == ""){
202 if (isset($_POST['unitTag'])){
203 $this->is_administrational_unit= true;
204 } else {
205 $this->is_administrational_unit= false;
206 }
207 }
208 }
209 }
212 /* Check values */
213 function check()
214 {
215 /* Call common method to give check the hook */
216 $message= plugin::check();
218 /* Permissions for that base? */
219 if (chkacl($this->acl, "create") != ""){
220 $message[]= _("You have no permissions to create a department on this 'Base'.");
221 }
223 /* Check for presence of this department */
224 $ldap= $this->config->get_ldap_link();
225 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
226 if ($this->orig_dn == "new" && $ldap->count()){
227 $message[]= _("Department with that 'Name' already exists.");
228 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
229 $message[]= _("Department with that 'Name' already exists.");
230 }
232 /* All required fields are set? */
233 if ($this->ou == ""){
234 $message[]= _("Required field 'Name' is not set.");
235 }
236 if ($this->description == ""){
237 $message[]= _("Required field 'Description' is not set.");
238 }
240 if(is_department_name_reserved($this->ou,$this->base)){
241 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
242 }
244 if (preg_match ('/["+>\\\\]/', $this->ou)){
245 $message[]= _("The field 'Name' contains invalid characters.");
246 }
247 if (!is_phone_nr($this->telephoneNumber)){
248 $message[]= _("The field 'Phone' contains an invalid phone number.");
249 }
250 if (!is_phone_nr($this->facsimileTelephoneNumber)){
251 $message[]= _("The field 'Fax' contains an invalid phone number.");
252 }
254 return $message;
255 }
258 /* Save to LDAP */
259 function save()
260 {
261 $ldap= $this->config->get_ldap_link();
263 /* Add tag objects if needed */
264 if ($this->is_administrational_unit){
265 if(!$this->initially_was_tagged){
266 $this->objectclasses[]= "gosaAdministrativeUnit";
267 $this->gosaUnitTag= "";
268 $this->initially_was_tagged = TRUE;
269 }
271 if ($this->gosaUnitTag == ""){
273 /* It's unlikely, but check if already used... */
274 $try= 5;
275 $ldap->cd($this->config->current['BASE']);
276 while ($try--){
278 /* Generate microtime stamp as tag */
279 list($usec, $sec)= explode(" ", microtime());
280 $time_stamp= preg_replace("/\./", "", $sec.$usec);
282 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
283 if ($ldap->count() == 0){
284 break;
285 }
286 }
287 if($try == 0) {
288 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
289 return;
290 }
291 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
292 }
293 }
295 $this->skipTagging = TRUE;
296 plugin::save();
298 /* Remove tag information if needed */
299 if (!$this->is_administrational_unit){
300 $tmp= array();
302 /* Remove gosaAdministrativeUnit from this plugin */
303 $has_unit_tag= false;
304 foreach($this->attrs['objectClass'] as $oc){
305 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
306 $tmp[]= $oc;
307 }
308 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
309 $has_unit_tag= true;
310 }
311 }
312 $this->attrs['objectClass']= $tmp;
313 $this->attrs['gosaUnitTag']= array();
314 $this->gosaUnitTag = "";
315 }
317 /* Write back to ldap */
318 $ldap= $this->config->get_ldap_link();
319 $ldap->cat($this->dn, array('dn'));
320 $a= $ldap->fetch();
321 $ldap->cd($this->dn);
323 if (count($a)){
324 $this->cleanup();
325 $ldap->modify ($this->attrs);
327 $this->handle_post_events('modify');
328 } else {
329 $ldap->add($this->attrs);
330 $this->handle_post_events('add');
331 }
332 show_ldap_error($ldap->get_error(), _("Saving department failed"));
334 /* The parameter forces only to set must_be_tagged, and don't touch any objects
335 This will be done later */
336 $this->tag_objects(true);
338 /* Optionally execute a command after we're done */
339 $this->postcreate();
340 return(true);
341 }
344 function ShowMoveFrame()
345 {
346 $smarty = get_smarty();
347 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
348 $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.");
349 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
350 return($display);
351 }
353 function ShowTagFrame()
354 {
355 $smarty = get_smarty();
356 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
357 $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.");
358 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
359 return($display);
360 }
362 /* Tag objects to have the gosaAdministrativeUnitTag */
363 function tag_objects($OnlySetTagFlag = false)
364 {
365 if(!$OnlySetTagFlag){
366 $smarty= get_smarty();
368 /* Print out html introduction */
369 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
370 <html>
371 <head>
372 <title></title>
373 <style type="text/css">@import url("themes/default/style.css");</style>
374 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
375 </head>
376 <body style="background-image:none;margin:3px;color:black" id="body" >
377 ';
379 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
380 }
382 $add= $this->is_administrational_unit;
383 $len= strlen($this->dn);
384 $ldap= $this->config->get_ldap_link();
385 $ldap->cd($this->dn);
386 if ($add){
387 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
388 $this->gosaUnitTag.')))', array('dn'));
389 } else {
390 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
391 }
393 while ($attrs= $ldap->fetch()){
395 /* Skip self */
396 if ($attrs['dn'] == $this->dn){
397 continue;
398 }
400 /* Check for confilicting administrative units */
401 $fix= true;
402 foreach ($this->config->adepartments as $key => $tag){
403 /* This one is shorter than our dn, its not relevant... */
404 if ($len >= strlen($key)){
405 continue;
406 }
408 /* This one matches with the latter part. Break and don't fix this entry */
409 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
410 $fix= false;
411 break;
412 }
413 }
415 /* Fix entry if needed */
416 if ($fix){
417 if($OnlySetTagFlag){
418 $this->must_be_tagged =true;
419 return;
420 }
421 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
422 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
423 }
424 }
425 if(!$OnlySetTagFlag){
426 echo '<p class="seperator"> </p>';
427 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
428 <br><input type='submit' name='back' value='"._("Continue")."'>
429 </form></div>";
430 }
431 }
434 /* Move/Rename complete trees */
435 function recursive_move($src_dn, $dst_dn,$force = false)
436 {
437 /* If force == false prepare to recursive move this object from src to dst
438 on the next call. */
439 if(!$force){
440 $this->rec_cpy = true;
441 $this->rec_src = $src_dn;
442 $this->rec_dst = $dst_dn;
443 }else{
445 /* If this is called, but not wanted, abort */
446 if(!$this->rec_cpy){
447 return;
448 }
450 $src_dn = $this->rec_src;
451 $dst_dn = $this->rec_dst;
453 /* Print header to have styles included */
454 $smarty= get_smarty();
456 /* Print out html introduction */
457 echo ' <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
458 <html>
459 <head>
460 <title></title>
461 <style type="text/css">@import url("themes/default/style.css");</style>
462 <script language="javascript" src="include/focus.js" type="text/javascript"></script>
463 </head>
464 <body style="background: none; margin:4px;" id="body" >
465 ';
467 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
469 /* Check if the destination entry exists */
470 $ldap= $this->config->get_ldap_link();
472 /* Check if destination exists - abort */
473 $ldap->cat($dst_dn, array('dn'));
474 if ($ldap->fetch()){
475 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
476 E_USER_WARNING);
477 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
478 return (FALSE);
479 }
481 /* Perform a search for all objects to be moved */
482 $objects= array();
483 $ldap->cd($src_dn);
484 $ldap->search("(objectClass=*)", array("dn"));
485 while($attrs= $ldap->fetch()){
486 $dn= $attrs['dn'];
487 $objects[$dn]= strlen($dn);
488 }
490 /* Sort objects by indent level */
491 asort($objects);
492 reset($objects);
494 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
495 foreach ($objects as $object => $len){
496 $src= str_replace("\\","\\\\",$object);
497 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
498 $dst= str_replace($src_dn,$dst_dn,$object);
500 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
502 if (!$this->copy($src, $dst)){
503 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
504 return (FALSE);
505 }
507 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
508 flush();
509 }
511 /* Remove src_dn */
512 $ldap->cd($src_dn);
513 $ldap->recursive_remove();
514 $this->rec_src = $this->rec_dst = "";
515 $this->rec_cpy =false;
517 echo '<p class="seperator"> </p>';
519 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
520 <br><input type='submit' name='back' value='"._("Continue")."'>
521 </form></div>";
523 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
524 echo "</body></html>";
526 return (TRUE);
527 }
528 }
529 }
530 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
531 ?>