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");
52 function department ($config, $dn)
53 {
55 plugin::plugin($config, $dn);
56 $this->is_account= TRUE;
57 $this->ui= get_userinfo();
58 $this->dn= $dn;
59 $this->orig_dn= $dn;
60 $this->config= $config;
62 /* Set base */
63 if ($this->dn == "new"){
64 $ui= get_userinfo();
65 if(isset($_SESSION['CurrentMainBase'])){
66 $this->base= $_SESSION['CurrentMainBase'];
67 }else{
68 $this->base= dn2base($ui->dn);
69 }
70 } else {
71 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
72 }
74 /* set permissions */
75 $ui= get_userinfo();
76 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
77 $this->acl= get_module_permission($acl, "department", $ui->dn);
79 /* Is administrational Unit? */
80 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
81 $this->is_administrational_unit= true;
82 }
83 }
85 function execute()
86 {
87 /* Call parent execute */
88 plugin::execute();
90 /* Reload departments */
91 $this->config->get_departments($this->dn);
92 $this->config->make_idepartments();
93 $smarty= get_smarty();
95 /* Base select dialog */
96 $once = true;
97 foreach($_POST as $name => $value){
98 if(preg_match("/^chooseBase/",$name) && $once){
99 $once = false;
100 $this->dialog = new baseSelectDialog($this->config);
101 $this->dialog->setCurrentBase($this->base);
102 }
103 }
105 /* Dialog handling */
106 if(is_object($this->dialog)){
107 /* Must be called before save_object */
108 $this->dialog->save_object();
110 if($this->dialog->isClosed()){
111 $this->dialog = false;
112 }elseif($this->dialog->isSelected()){
113 $this->base = $this->dialog->isSelected();
114 $this->dialog= false;
115 }else{
116 return($this->dialog->execute());
117 }
118 }
120 /* Hide all departments, that are subtrees of this department */
121 $bases = $this->config->idepartments;
122 if(($this->dn == "new")||($this->dn == "")){
123 $tmp = $bases;
124 }else{
125 $tmp = array();
126 foreach($bases as $dn=>$base){
127 $fixed = str_replace("/","\\",$this->dn);
128 /* Only attach departments which are not a subtree of this one */
129 if(!preg_match("/".$fixed."/",$dn)){
130 $tmp[$dn]=$base;
131 }
132 }
133 }
134 $smarty->assign("bases", $tmp);
136 foreach ($this->attributes as $val){
137 $smarty->assign("$val", $this->$val);
138 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
139 }
140 $smarty->assign("baseACL", chkacl($this->acl,"base"));
141 $smarty->assign("base_select", $this->base);
143 /* Set admin unit flag */
144 if ($this->is_administrational_unit) {
145 $smarty->assign("unitTag", "checked");
146 } else {
147 $smarty->assign("unitTag", "");
148 }
149 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
151 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
152 }
154 function clear_fields()
155 {
156 $this->dn= "";
157 $this->base= "";
158 $this->acl= "#none#";
160 foreach ($this->attributes as $val){
161 $this->$val= "";
162 }
163 }
165 function remove_from_parent()
166 {
167 $ldap= $this->config->get_ldap_link();
168 $ldap->cd ($this->dn);
169 $ldap->recursive_remove();
170 show_ldap_error($ldap->get_error(), _("Removing department failed"));
172 /* Optionally execute a command after we're done */
173 $this->handle_post_events('remove');
174 }
176 function must_be_tagged()
177 {
178 if((isset($this->attrs['gosaUnitTag'][0])) && ($this->is_administrational_unit)){
179 $this->must_be_tagged = false;
180 }elseif((!isset($this->attrs['gosaUnitTag'][0])) && (!$this->is_administrational_unit)){
181 $this->must_be_tagged = false;
182 }else{
183 $this->must_be_tagged = true;
184 }
185 return $this->must_be_tagged;
186 }
188 function am_i_moved()
189 {
190 return $this->rec_cpy;
191 }
194 /* Save data to object */
195 function save_object()
196 {
197 if (isset($_POST['base'])){
198 plugin::save_object();
200 /* Save base, since this is no LDAP attribute */
201 if (chkacl($this->acl, "create") == ""){
202 $this->base= $_POST['base'];
203 }
205 /* Save tagging flag */
206 if (chkacl($this->acl, "unitTag") == ""){
207 if (isset($_POST['unitTag'])){
208 $this->is_administrational_unit= true;
209 } else {
210 $this->is_administrational_unit= false;
211 }
212 }
213 }
214 }
217 /* Check values */
218 function check()
219 {
220 /* Call common method to give check the hook */
221 $message= plugin::check();
223 /* Permissions for that base? */
224 // $this->dn= "ou=$this->ou,".$this->base;
225 if (chkacl($this->acl, "create") != ""){
226 $message[]= _("You have no permissions to create a department on this 'Base'.");
227 }
229 /* Check for presence of this department */
230 $ldap= $this->config->get_ldap_link();
231 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
232 if ($this->orig_dn == "new" && $ldap->count()){
233 $message[]= _("Department with that 'Name' already exists.");
234 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
235 $message[]= _("Department with that 'Name' already exists.");
236 }
238 /* All required fields are set? */
239 if ($this->ou == ""){
240 $message[]= _("Required field 'Name' is not set.");
241 }
242 if ($this->description == ""){
243 $message[]= _("Required field 'Description' is not set.");
244 }
246 if(is_department_name_reserved($this->ou,$this->base)){
247 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
248 }
250 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
251 $message[]= _("The field 'Name' contains invalid characters.");
252 }
253 if (!is_phone_nr($this->telephoneNumber)){
254 $message[]= _("The field 'Phone' contains an invalid phone number.");
255 }
256 if (!is_phone_nr($this->facsimileTelephoneNumber)){
257 $message[]= _("The field 'Fax' contains an invalid phone number.");
258 }
260 return $message;
261 }
264 /* Save to LDAP */
265 function save()
266 {
267 $ldap= $this->config->get_ldap_link();
269 /* Add tag objects if needed */
270 if ($this->is_administrational_unit){
271 if(!in_array_ics("gosaAdministrativeUnit",$this->objectclasses)){
272 $this->objectclasses[]= "gosaAdministrativeUnit";
273 $this->gosaUnitTag= "";
274 }
275 if ($this->gosaUnitTag == ""){
277 /* It's unlikely, but check if already used... */
278 $try= 5;
279 $ldap->cd($this->config->current['BASE']);
280 while ($try--){
282 /* Generate microtime stamp as tag */
283 list($usec, $sec)= explode(" ", microtime());
284 $time_stamp= preg_replace("/\./", "", $sec.$usec);
286 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
287 if ($ldap->count() == 0){
288 break;
289 }
290 }
291 if($try == 0) {
292 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
293 return;
294 }
295 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
296 }
297 }
299 plugin::save();
301 /* Remove tag information if needed */
302 if (!$this->is_administrational_unit){
303 $tmp= array();
305 /* Remove gosaAdministrativeUnit from this plugin */
306 $has_unit_tag= false;
307 foreach($this->attrs['objectClass'] as $oc){
308 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
309 $tmp[]= $oc;
310 }
311 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
312 $has_unit_tag= true;
313 }
314 }
315 $this->attrs['objectClass']= $tmp;
317 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
318 $this->attrs['gosaUnitTag']= array();
319 }
320 }
322 /* Write back to ldap */
323 $ldap= $this->config->get_ldap_link();
324 $ldap->cat($this->dn, array('dn'));
325 $a= $ldap->fetch();
326 $ldap->cd($this->dn);
328 if (count($a)){
329 $this->cleanup();
330 $ldap->modify ($this->attrs);
332 $this->handle_post_events('modify');
333 } else {
334 $ldap->add($this->attrs);
335 $this->handle_post_events('add');
336 }
337 show_ldap_error($ldap->get_error(), _("Saving department failed"));
339 /* Fix tagging if needed */
340 if (!$this->is_administrational_unit){
341 $this->handle_object_tagging();
342 }
344 /* Optionally execute a command after we're done */
345 $this->postcreate();
346 return(true);
347 }
350 function ShowMoveFrame()
351 {
352 $smarty = get_smarty();
353 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
354 $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.");
355 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
356 return($display);
357 }
359 function ShowTagFrame()
360 {
361 $smarty = get_smarty();
362 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
363 $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.");
364 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
365 return($display);
366 }
368 /* Tag objects to have the gosaAdministrativeUnitTag */
369 function tag_objects($OnlySetTagFlag = false)
370 {
371 if(!$OnlySetTagFlag){
372 $smarty= get_smarty();
373 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
374 echo "<body style='background-image:none;margin:3px;color:black'>";
375 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
376 }
378 $add= $this->is_administrational_unit;
379 $len= strlen($this->dn);
380 $ldap= $this->config->get_ldap_link();
381 $ldap->cd($this->dn);
382 if ($add){
383 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
384 $this->gosaUnitTag.')))', array('dn'));
385 } else {
386 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
387 }
388 while ($attrs= $ldap->fetch()){
390 /* Skip self */
391 if ($attrs['dn'] == $this->dn){
392 continue;
393 }
395 /* Check for confilicting administrative units */
396 $fix= true;
397 foreach ($this->config->adepartments as $key => $tag){
398 /* This one is shorter than our dn, its not relevant... */
399 if ($len >= strlen($key)){
400 continue;
401 }
403 /* This one matches with the latter part. Break and don't fix this entry */
404 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
405 $fix= false;
406 break;
407 }
408 }
410 /* Fix entry if needed */
411 if ($fix){
412 if($OnlySetTagFlag){
413 $this->must_be_tagged =true;
414 return;
415 }
416 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
417 }
418 }
419 if(!$OnlySetTagFlag){
420 echo '<p class="seperator"> </p>';
421 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
422 <br><input type='submit' name='back' value='"._("Continue")."'>
423 </form></div>";
424 }
425 }
428 /* Move/Rename complete trees */
429 function recursive_move($src_dn, $dst_dn,$force = false)
430 {
431 /* If force == false prepare to recursive move this object from src to dst
432 on the next call. */
433 if(!$force){
434 $this->rec_cpy = true;
435 $this->rec_src = $src_dn;
436 $this->rec_dst = $dst_dn;
437 }else{
439 /* If this is called, but not wanted, abort */
440 if(!$this->rec_cpy){
441 return;
442 }
444 $src_dn = $this->rec_src;
445 $dst_dn = $this->rec_dst;
447 /* Print header to have styles included */
448 $smarty= get_smarty();
449 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
450 echo "<body style='background-image:none;margin:3px;color:black'>";
452 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
455 /* Check if the destination entry exists */
456 $ldap= $this->config->get_ldap_link();
458 /* Check if destination exists - abort */
459 $ldap->cat($dst_dn, array('dn'));
460 if ($ldap->fetch()){
461 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
462 E_USER_WARNING);
463 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
464 return (FALSE);
465 }
467 /* Perform a search for all objects to be moved */
468 $objects= array();
469 $ldap->cd($src_dn);
470 $ldap->search("(objectClass=*)", array("dn"));
471 while($attrs= $ldap->fetch()){
472 $dn= $attrs['dn'];
473 $objects[$dn]= strlen($dn);
474 }
476 /* Sort objects by indent level */
477 asort($objects);
478 reset($objects);
480 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
481 foreach ($objects as $object => $len){
484 $src= str_replace("\\","\\\\",$object);
485 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
486 $dst= str_replace($src_dn,$dst_dn,$object);
488 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
490 if (!$this->copy($src, $dst)){
491 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
492 return (FALSE);
493 }
495 flush();
496 }
498 /* Remove src_dn */
499 $ldap->cd($src_dn);
500 $ldap->recursive_remove();
501 $this->rec_src = $this->rec_dst = "";
502 $this->rec_cpy =false;
504 echo '<p class="seperator"> </p>';
506 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
507 <br><input type='submit' name='back' value='"._("Continue")."'>
508 </form></div>";
510 echo "</body></html>";
512 return (TRUE);
513 }
514 }
515 }
516 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
517 ?>