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 /* Headpage attributes */
38 var $last_dep_sorting = "invalid";
39 var $departments = array();
41 /* Used to indicate that a new tagging is required */
42 var $must_be_tagged = false;
43 var $MustBeMoved = "";
44 var $MoveDestination = "";
45 var $MoveSource = "";
46 var $MoveOu = "";
48 /* attribute list for save action */
49 var $attributes = array("ou", "description", "businessCategory", "st",
50 "l", "postalAddress", "telephoneNumber",
51 "facsimileTelephoneNumber", "gosaUnitTag");
52 var $objectclasses = array("top", "gosaDepartment", "organizationalUnit");
55 function department ($config, $dn)
56 {
57 plugin::plugin($config, $dn);
58 $this->is_account = TRUE;
59 $this->ui = get_userinfo();
60 $this->dn = $dn;
61 $this->orig_dn = $dn;
62 $this->config = $config;
64 /* Set base */
65 if ($this->dn == "new"){
66 $ui= get_userinfo();
67 if(isset($_SESSION['CurrentMainBase'])){
68 $this->base= $_SESSION['CurrentMainBase'];
69 }else{
70 $this->base= dn2base($ui->dn);
71 }
72 } else {
73 $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
74 }
76 /* set permissions */
77 $acl = get_permissions ($this->ui->dn, $this->ui->subtreeACL);
78 $this->acl= get_module_permission($acl, "department", $this->ui->dn);
80 /* Is administrational Unit? */
81 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
82 $this->is_administrational_unit= true;
83 }
84 }
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 /* BASE select dialog handling */
108 if(is_object($this->dialog)){
110 /* save sub dialog vars */
111 $this->dialog->save_object();
113 /* Check if dialog was closed */
114 if($this->dialog->isClosed()){
115 $this->dialog = false;
116 }elseif($this->dialog->isSelected()){
117 $this->base = $this->dialog->isSelected();
118 $this->dialog= false;
119 }else{
120 return($this->dialog->execute());
121 }
122 }
124 /* Hide all departments, that are subtrees of this department */
125 $bases = $this->config->idepartments;
126 if(($this->dn == "new")||($this->dn == "")){
127 $tmp = $bases;
128 }else{
129 $tmp = array();
130 foreach($bases as $dn=>$base){
131 $fixed = str_replace("/","\\",$this->dn);
133 /* Only attach departments which are not a subtree of this one */
134 if(!preg_match("/".$fixed."/",$dn)){
135 $tmp[$dn]=$base;
136 }
137 }
138 }
139 $smarty->assign("bases", $tmp);
141 /* Report class vars to smarty */
142 foreach ($this->attributes as $val){
143 $smarty->assign("$val", $this->$val);
144 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
145 }
146 $smarty->assign("baseACL", chkacl($this->acl,"base"));
147 $smarty->assign("base_select", $this->base);
149 /* Set admin unit flag */
150 if ($this->is_administrational_unit) {
151 $smarty->assign("unitTag", "checked");
152 } else {
153 $smarty->assign("unitTag", "");
154 }
155 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
156 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
157 }
160 /* Remove department && all sub objects from ldap */
161 function remove_from_parent()
162 {
163 /* Remove the current department from ldap */
164 $ldap= $this->config->get_ldap_link();
165 $ldap->cd ($this->dn);
166 $ldap->recursive_remove();
167 show_ldap_error($ldap->get_error(), _("Removing department failed"));
169 /* Optionally execute a command after we're done */
170 $this->handle_post_events('remove');
171 }
174 /* Save data to object */
175 function save_object()
176 {
177 if (isset($_POST['base'])){
178 plugin::save_object();
180 /* Save base, since this is no LDAP attribute */
181 if (chkacl($this->acl, "create") == ""){
182 $this->base= $_POST['base'];
183 }
185 /* Save tagging flag */
186 if (chkacl($this->acl, "unitTag") == ""){
187 if (isset($_POST['unitTag'])){
188 $this->is_administrational_unit= true;
189 } else {
190 $this->is_administrational_unit= false;
191 }
192 }
193 }
194 }
197 /* Check values */
198 function check()
199 {
200 /* Call common method to give check the hook */
201 $message= plugin::check();
203 /* Permissions for that base? */
204 // $this->dn= "ou=$this->ou,".$this->base;
205 if (chkacl($this->acl, "create") != ""){
206 $message[]= _("You have no permissions to create a department on this 'Base'.");
207 }
209 /* Check for presence of this department */
210 $ldap= $this->config->get_ldap_link();
211 $attrs= $ldap->cat ($this->dn, array('dn'));
212 if ($this->orig_dn == "new" && !($attrs === FALSE)){
213 $message[]= _("Department with that 'Name' already exists.");
214 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
215 $message[]= _("Department with that 'Name' already exists.");
216 }
218 /* All required fields are set? */
219 if ($this->ou == ""){
220 $message[]= _("Required field 'Name' is not set.");
221 }
222 if ($this->description == ""){
223 $message[]= _("Required field 'Description' is not set.");
224 }
226 if(is_department_name_reserved($this->ou,$this->base)){
227 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
228 }
230 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
231 $message[]= _("The field 'Name' contains invalid characters.");
232 }
233 if (!is_phone_nr($this->telephoneNumber)){
234 $message[]= _("The field 'Phone' contains an invalid phone number.");
235 }
236 if (!is_phone_nr($this->facsimileTelephoneNumber)){
237 $message[]= _("The field 'Fax' contains an invalid phone number.");
238 }
240 return $message;
241 }
244 /* Save to LDAP */
245 function save()
246 {
247 $ldap= $this->config->get_ldap_link();
249 /* Add tag objects if needed */
250 if ($this->is_administrational_unit){
252 /* Add tagging objectClass if it is not available */
253 if(!in_array_ics("gosaAdministrativeUnit",$this->objectclasses)){
254 $this->objectclasses[]= "gosaAdministrativeUnit";
255 }
258 /* If tag wasn't set right now */
259 if ($this->gosaUnitTag == ""){
261 /* It's unlikely, but check if already used... */
262 $try= 5;
263 $ldap->cd($this->config->current['BASE']);
264 while ($try--){
266 /* Generate microtime stamp as tag, and check if this tag was already used somewhere */
267 list($usec, $sec)= explode(" ", microtime());
268 $time_stamp= preg_replace("/\./", "", $sec.$usec);
269 $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
270 if ($ldap->count() == 0){
271 break;
272 }
273 }
275 /* Last try, and all generated tag are already in use */
276 if($try == 0) {
277 print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
278 return;
279 }
280 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
281 }
282 } else {
284 /* Set tag to "" if department isn't tagged */
285 $this->gosaUnitTag= "";
286 }
288 plugin::save();
290 /* Remove tag information if needed */
291 if (!$this->is_administrational_unit){
292 $tmp= array();
294 /* Remove gosaAdministrativeUnit from this plugin */
295 foreach($this->attrs['objectClass'] as $oc){
296 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
297 $tmp[]= $oc;
298 }
299 }
300 $this->attrs['objectClass']= $tmp;
301 if(isset($this->attrs['gosaUnitTag'])){
302 $this->attrs['gosaUnitTag']= array();
303 }
304 }
306 /* Write back to ldap */
307 $ldap= $this->config->get_ldap_link();
308 $ldap->cat($this->dn, array('dn'));
309 $a= $ldap->fetch();
310 $ldap->cd($this->dn);
312 if (count($a)){
313 $this->cleanup();
314 $ldap->modify ($this->attrs);
316 $this->handle_post_events('modify');
317 } else {
318 $ldap->add($this->attrs);
319 $this->handle_post_events('add');
320 }
322 show_ldap_error($ldap->get_error(), _("Saving department failed"));
324 /* The parameter forces only to set must_be_tagged, and don't touch any objects
325 This will be done later */
326 $this->tag_objects(true);
328 /* Optionally execute a command after we're done */
329 $this->postcreate();
330 return(true);
331 }
334 /* Tag objects to have the gosaAdministrativeUnitTag */
335 function tag_objects($OnlySetTagFlag = false)
336 {
337 /* Skip output */
338 if(!$OnlySetTagFlag){
339 $smarty= get_smarty();
340 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
341 echo "<body style='background-image:none;margin:3px;color:black'>";
342 echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
343 }
345 /* Get all object that must be tagged, or the tag removed */
346 $ldap= $this->config->get_ldap_link();
347 $ldap->cd($this->dn);
348 if ($this->is_administrational_unit){
349 $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.$this->gosaUnitTag.')))', array('dn'));
350 } else {
351 $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
352 }
354 /* Get dn length, it is used to validate the fetched dns */
355 $len= strlen($this->dn);
357 /* Walk through all objects && verify if fetch object musst be touched */
358 while ($attrs= $ldap->fetch()){
360 /* Skip self */
361 if ($attrs['dn'] == $this->dn){
362 continue;
363 }
365 /* Check for confilicting administrative units */
366 $fix= true;
367 foreach ($this->config->adepartments as $key => $tag){
369 /* This one is shorter than our dn, its not relevant... */
370 if ($len >= strlen($key)){
371 continue;
372 }
374 /* This one matches with the latter part. Break and don't fix this entry */
375 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
376 $fix= false;
377 break;
378 }
379 }
381 /* Fix entry if needed */
382 if ($fix){
384 /* If OnlySetTagFlag is true, then only set must_be_tagged to true, and skip tagging. */
385 if($OnlySetTagFlag){
386 $this->must_be_tagged =true;
387 return;
388 }else{
389 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
390 }
391 }
392 }
394 /* Hide all output, if we only want to if we must tag */
395 if(!$OnlySetTagFlag){
396 echo '<p class="seperator"> </p>';
397 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
398 <br><input type='submit' name='back' value='"._("Continue")."'>
399 </form></div>";
400 }
401 }
404 /* Move/Rename complete trees */
405 function recursive_move()
406 {
407 /* Check if the destination entry exists */
408 $ldap= $this->config->get_ldap_link();
410 /* Get destination && source dn */
411 $src_dn = $this->MoveSource;
412 $dst_dn = $this->MoveDestination;
414 /* Print header to have styles included */
415 $smarty= get_smarty();
416 echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
417 echo "<body style='background-image:none;margin:3px;color:black'>";
418 echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
420 /* Check if destination exists - abort */
421 $ldap->cat($dst_dn, array('dn'));
422 if ($ldap->fetch()){
423 trigger_error("Recursive_move $dst_dn already exists.", E_USER_WARNING);
424 echo sprintf("Recursive_move: '%s' already exists", $dst_dn)."<br>";
425 return (FALSE);
426 }
428 /* Perform a search for all objects to be moved */
429 $objects= array();
430 $ldap->cd($src_dn);
431 $ldap->search("(objectClass=*)", array("dn"));
432 while($attrs= $ldap->fetch()){
433 $dn= $attrs['dn'];
434 $objects[$dn]= strlen($dn);
435 }
437 /* Sort objects by indent level */
438 asort($objects);
439 reset($objects);
441 /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
442 foreach ($objects as $object => $len){
444 $src= str_replace("\\","\\\\",$object);
445 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
446 $dst= str_replace($src_dn,$dst_dn,$object);
448 echo "<b>"._("Object").":</b> $src<br>";
450 if (!$this->copy($src, $dst)){
451 echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
452 return (FALSE);
453 }
455 flush();
456 }
458 /* Remove src_dn */
459 $ldap->cd($src_dn);
460 $ldap->recursive_remove();
461 $this->rec_src = $this->rec_dst = "";
462 $this->rec_cpy =false;
464 echo '<p class="seperator"> </p>';
465 echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
466 <br><input type='submit' name='back' value='"._("Continue")."'>
467 </form></div>";
469 echo "</body></html>";
470 return (TRUE);
471 }
474 /* Return an iframe, that calls the recursive move function of departmentManagement */
475 function ShowMoveFrame()
476 {
477 $smarty = get_smarty();
478 $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
479 $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.");
480 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
481 return($display);
482 }
485 /* return an iframe, that will initiate the tagging process */
486 function ShowTagFrame()
487 {
488 $smarty = get_smarty();
489 $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
490 $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.");
491 $display= $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
492 return($display);
493 }
496 }
497 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
498 ?>