Code

PHP4 fix
[gosa.git] / plugins / admin / departments / class_departmentGeneric.inc
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                 $attrs= $ldap->cat ($this->dn, array('dn'));
232                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
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       }
274                         if ($this->gosaUnitTag == ""){
276                                 /* It's unlikely, but check if already used... */
277                                 $try= 5;
278                                 $ldap->cd($this->config->current['BASE']);
279                                 while ($try--){
281                                         /* Generate microtime stamp as tag */
282                                         list($usec, $sec)= explode(" ", microtime());
283                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
285                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
286                                         if ($ldap->count() == 0){
287                                                 break;
288                                         }
289                                 }
290                                 if($try == 0) {
291                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
292                                         return;
293                                 }
294                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
295                         }
296                 } else {
297                         $this->gosaUnitTag= "";
298                 }
300                 plugin::save();
302                 /* Remove tag information if needed */
303                 if (!$this->is_administrational_unit){
304                         $tmp= array();
306                         /* Remove gosaAdministrativeUnit from this plugin */
307                         foreach($this->attrs['objectClass'] as $oc){
308                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
309                                         $tmp[]= $oc;
310                                 }
311                         }
312                         $this->attrs['objectClass']= $tmp;
313                         if(isset($this->attrs['gosaUnitTag'])){
314                                 $this->attrs['gosaUnitTag']= array();
315                         }
316                 }
318                 /* Write back to ldap */
319                 $ldap= $this->config->get_ldap_link();
320                 $ldap->cat($this->dn, array('dn'));
321                 $a= $ldap->fetch();
322                 $ldap->cd($this->dn);
324                 if (count($a)){
325                         $this->cleanup();
326                         $ldap->modify ($this->attrs); 
328                         $this->handle_post_events('modify');
329                 } else {
330                         $ldap->add($this->attrs);
331                         $this->handle_post_events('add');
332                 }
333                 show_ldap_error($ldap->get_error(), _("Saving department failed"));
335     /* Optionally execute a command after we're done */
336                 $this->postcreate();
337     return(true);
338         }
341   function ShowMoveFrame()
342   {
343     $smarty = get_smarty();
344     $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
345     $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.");
346     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
347     return($display);
348   }
350   function ShowTagFrame()
351   {
352     $smarty = get_smarty();
353     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
354     $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.");
355     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
356     return($display);
357   }
359         /* Tag objects to have the gosaAdministrativeUnitTag */
360         function tag_objects($OnlySetTagFlag = false)
361         {
362     if(!$OnlySetTagFlag){
363       $smarty= get_smarty();
364       echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
365       echo "<body style='background-image:none;margin:3px;color:black'>";
366       echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
367     }
369                 $add= $this->is_administrational_unit;
370                 $len= strlen($this->dn);
371                 $ldap= $this->config->get_ldap_link();
372                 $ldap->cd($this->dn);
373                 if ($add){
374                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
375                                                                 $this->gosaUnitTag.')))', array('dn'));
376                 } else {
377                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
378                 }
379                 while ($attrs= $ldap->fetch()){
381                         /* Skip self */
382                         if ($attrs['dn'] == $this->dn){
383                                 continue;
384                         }
386                         /* Check for confilicting administrative units */
387                         $fix= true;
388                         foreach ($this->config->adepartments as $key => $tag){
389                                 /* This one is shorter than our dn, its not relevant... */
390                                 if ($len >= strlen($key)){
391                                         continue;
392                                 }
394                                 /* This one matches with the latter part. Break and don't fix this entry */
395                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
396                                         $fix= false;
397                                         break;
398                                 }
399                         }
401       /* Fix entry if needed */
402       if ($fix){
403         if($OnlySetTagFlag){
404           $this->must_be_tagged =true;
405           return;
406         }
407                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
408                         }
409                 }
410     if(!$OnlySetTagFlag){
411       echo '<p class="seperator">&nbsp;</p>';
412       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
413         <br><input type='submit' name='back' value='"._("Continue")."'>
414         </form></div>";
415     }
416         }
419         /* Move/Rename complete trees */
420         function recursive_move($src_dn, $dst_dn,$force = false)
421         {
422     /* If force == false prepare to recursive move this object from src to dst 
423         on the next call. */
424                 if(!$force){
425                         $this->rec_cpy  = true;
426                         $this->rec_src  = $src_dn;
427                         $this->rec_dst  = $dst_dn;
428                 }else{
430       /* If this is called, but not wanted, abort */
431                         if(!$this->rec_cpy){ 
432                                 return;
433                         }
435                         $src_dn = $this->rec_src;
436                         $dst_dn = $this->rec_dst;
438                         /* Print header to have styles included */
439                         $smarty= get_smarty();
440                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
441                         echo "<body style='background-image:none;margin:3px;color:black'>";
443                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
446                         /* Check if the destination entry exists */
447                         $ldap= $this->config->get_ldap_link();
449                         /* Check if destination exists - abort */
450                         $ldap->cat($dst_dn, array('dn'));
451                         if ($ldap->fetch()){
452                                 trigger_error("Recursive_move $dst_dn already exists.",
453                                                 E_USER_WARNING);
454                                 echo sprintf("Recursive_move: '%s' already exists", $dst_dn)."<br>"; 
455                                 return (FALSE);
456                         }
458                         /* Perform a search for all objects to be moved */
459                         $objects= array();
460                         $ldap->cd($src_dn);
461                         $ldap->search("(objectClass=*)", array("dn"));
462                         while($attrs= $ldap->fetch()){
463                                 $dn= $attrs['dn'];
464                                 $objects[$dn]= strlen($dn);
465                         }
467                         /* Sort objects by indent level */
468                         asort($objects);
469                         reset($objects);
471                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
472                         foreach ($objects as $object => $len){
475                                 $src= str_replace("\\","\\\\",$object);
476                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
477                                 $dst= str_replace($src_dn,$dst_dn,$object);
479                                 echo "<b>"._("Object").":</b> $src<br>";
481                                 if (!$this->copy($src, $dst)){
482                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
483                                         return (FALSE);
484                                 }
486                                 flush();
487                         }
489                         /* Remove src_dn */
490                         $ldap->cd($src_dn);
491                         $ldap->recursive_remove();
492                         $this->rec_src = $this->rec_dst = "";
493                         $this->rec_cpy =false;
495                         echo '<p class="seperator">&nbsp;</p>';
497                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
498                                 <br><input type='submit' name='back' value='"._("Continue")."'>
499                                 </form></div>";
501                         echo "</body></html>";
503                         return (TRUE);
504                 }
505         }
507 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
508 ?>