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 /* 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,$this);
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(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
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 // $this->dn= "ou=$this->ou,".$this->base;
220 if (chkacl($this->acl, "create") != ""){
221 $message[]= _("You have no permissions to create a department on this 'Base'.");
222 }
224 /* Check for presence of this department */
225 $ldap= $this->config->get_ldap_link();
226 $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
227 if ($this->orig_dn == "new" && $ldap->count()){
228 $message[]= _("Department with that 'Name' already exists.");
229 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
230 $message[]= _("Department with that 'Name' already exists.");
231 }
233 /* All required fields are set? */
234 if ($this->ou == ""){
235 $message[]= _("Required field 'Name' is not set.");
236 }
237 if ($this->description == ""){
238 $message[]= _("Required field 'Description' is not set.");
239 }
241 if(is_department_name_reserved($this->ou,$this->base)){
242 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
243 }
245 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
246 $message[]= _("The field 'Name' contains invalid characters.");
247 }
248 if (!is_phone_nr($this->telephoneNumber)){
249 $message[]= _("The field 'Phone' contains an invalid phone number.");
250 }
251 if (!is_phone_nr($this->facsimileTelephoneNumber)){
252 $message[]= _("The field 'Fax' contains an invalid phone number.");
253 }
255 return $message;
256 }
259 /* Save to LDAP */
260 function save()
261 {
262 $ldap= $this->config->get_ldap_link();
264 /* Add tag objects if needed */
265 if ($this->is_administrational_unit){
267 /* If this wasn't tagged before add oc an reset unit tag */
268 if(!$this->initially_was_tagged){
269 $this->objectclasses[]= "gosaAdministrativeUnit";
270 $this->gosaUnitTag= "";
272 /* It seams that this method is called twice,
273 set this to true. to avoid adding this oc twice */
274 $this->initially_was_tagged = true;
275 }
277 if ($this->gosaUnitTag == ""){
279 /* It's unlikely, but check if already used... */
280 $try= 5;
281 $ldap->cd($this->config->current['BASE']);
282 while ($try--){
284 /* Generate microtime stamp as tag */
285 list($usec, $sec)= explode(" ", microtime());
286 $time_stamp= preg_replace("/\./", "", $sec.$usec);
288 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
289 if ($ldap->count() == 0){
290 break;
291 }
292 }
293 if($try == 0) {
294 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
295 return;
296 }
297 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
298 }
299 }
301 plugin::save();
303 /* Remove tag information if needed */
304 if (!$this->is_administrational_unit){
305 $tmp= array();
307 /* Remove gosaAdministrativeUnit from this plugin */
308 $has_unit_tag= false;
309 foreach($this->attrs['objectClass'] as $oc){
310 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
311 $tmp[]= $oc;
312 }
313 if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
314 $has_unit_tag= true;
315 }
316 }
317 $this->attrs['objectClass']= $tmp;
319 if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
320 $this->attrs['gosaUnitTag']= array();
321 }
322 $this->gosaUnitTag = "";
323 }
325 /* Write back to ldap */
326 $ldap= $this->config->get_ldap_link();
327 $ldap->cat($this->dn, array('dn'));
328 $a= $ldap->fetch();
329 $ldap->cd($this->dn);
331 if (count($a)){
332 $this->cleanup();
333 $ldap->modify ($this->attrs);
335 $this->handle_post_events('modify');
336 } else {
337 $ldap->add($this->attrs);
338 $this->handle_post_events('add');
339 }
340 show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
342 /* The parameter forces only to set must_be_tagged, and don't touch any objects
343 This will be done later */
344 $this->tag_objects(true);
346 /* Fix tagging if needed */
347 if (!$this->is_administrational_unit){
348 $this->handle_object_tagging();
349 }
351 /* Optionally execute a command after we're done */
352 $this->postcreate();
353 return(false);
354 }
357 function ShowMoveFrame()
358 {
359 $smarty = get_smarty();
360 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
361 $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.");
362 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
363 return($display);
364 }
366 function ShowTagFrame()
367 {
368 $smarty = get_smarty();
369 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
370 $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.");
371 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
372 return($display);
373 }
375 /* Tag objects to have the gosaAdministrativeUnitTag */
376 function tag_objects($OnlySetTagFlag = false)
377 {
378 if(!$OnlySetTagFlag){
379 $smarty= get_smarty();
380 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
381 echo "<body style='background-image:none;margin:3px;color:black'>";
382 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
383 }
385 $add= $this->is_administrational_unit;
386 $len= strlen($this->dn);
387 $ldap= $this->config->get_ldap_link();
388 $ldap->cd($this->dn);
389 if ($add){
390 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
391 $this->gosaUnitTag.')))', array('dn'));
392 } else {
393 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
394 }
396 while ($attrs= $ldap->fetch()){
398 /* Skip self */
399 if ($attrs['dn'] == $this->dn){
400 continue;
401 }
403 /* Check for confilicting administrative units */
404 $fix= true;
405 foreach ($this->config->adepartments as $key => $tag){
406 /* This one is shorter than our dn, its not relevant... */
407 if ($len >= strlen($key)){
408 continue;
409 }
411 /* This one matches with the latter part. Break and don't fix this entry */
412 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
413 $fix= false;
414 break;
415 }
416 }
418 /* Fix entry if needed */
419 if ($fix){
420 if($OnlySetTagFlag){
421 $this->must_be_tagged =true;
422 return;
423 }
424 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
425 }
426 }
427 if(!$OnlySetTagFlag){
428 echo '<p class="seperator"> </p>';
429 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
430 <br><input type='submit' name='back' value='"._("Continue")."'>
431 </form></div>";
432 }
433 }
436 /* Move/Rename complete trees */
437 function recursive_move($src_dn, $dst_dn,$force = false)
438 {
439 /* If force == false prepare to recursive move this object from src to dst
440 on the next call. */
441 if(!$force){
442 $this->rec_cpy = true;
443 $this->rec_src = $src_dn;
444 $this->rec_dst = $dst_dn;
445 }else{
447 /* If this is called, but not wanted, abort */
448 if(!$this->rec_cpy){
449 return;
450 }
452 $src_dn = $this->rec_src;
453 $dst_dn = $this->rec_dst;
455 /* Print header to have styles included */
456 $smarty= get_smarty();
457 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
458 echo "<body style='background-image:none;margin:3px;color:black'>";
460 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
463 /* Check if the destination entry exists */
464 $ldap= $this->config->get_ldap_link();
466 /* Check if destination exists - abort */
467 $ldap->cat($dst_dn, array('dn'));
468 if ($ldap->fetch()){
469 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
470 E_USER_WARNING);
471 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>";
472 return (FALSE);
473 }
475 /* Perform a search for all objects to be moved */
476 $objects= array();
477 $ldap->cd($src_dn);
478 $ldap->search("(objectClass=*)", array("dn"));
479 while($attrs= $ldap->fetch()){
480 $dn= $attrs['dn'];
481 $objects[$dn]= strlen($dn);
482 }
484 /* Sort objects by indent level */
485 asort($objects);
486 reset($objects);
488 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
489 foreach ($objects as $object => $len){
492 $src= str_replace("\\","\\\\",$object);
493 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
494 $dst= str_replace($src_dn,$dst_dn,$object);
496 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
498 if (!$this->copy($src, $dst)){
499 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
500 return (FALSE);
501 }
503 flush();
504 }
506 /* Remove src_dn */
507 $ldap->cd($src_dn);
508 $ldap->recursive_remove();
509 $this->rec_src = $this->rec_dst = "";
510 $this->rec_cpy =false;
512 echo '<p class="seperator"> </p>';
514 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
515 <br><input type='submit' name='back' value='"._("Continue")."'>
516 </form></div>";
518 echo "</body></html>";
520 return (TRUE);
521 }
522 }
525 /* Return plugin informations for acl handling */
526 function plInfo()
527 {
528 return (array("plDescription" => _("Departments"),
529 "plSelfModify" => FALSE,
530 "plDepends" => array("objectClass" => "gosaDepartment", "description" => _("Departments")),
531 "description" => _("Description"),
532 "c" => _("Country"),
533 "l" => _("Location"),
534 "telephoneNumber" => _("Telephone"),
535 "ou" => _("Department name"),
536 "businessCategory" => _("Category"),
537 "st" => _("State"),
538 "postalAddress" => _("Address"),
539 "gosaUnitTag" => _("Administrative settings"),
540 "facsimileTelephoneNumber" => _("Fax")));
541 }
542 }
543 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
544 ?>