Code

Added some logging
[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");
51   var $initially_was_tagged = false;
53   
54   /* Temporary disable the Unit Tag functionalityi.
55      This is not used anymore, cause of the new acl implementation. 
56      Setting this to TRUE will disable 
57       all POST / GET activity, "Department tagging" and the checkbox within the generic template.
58    */
59   var $UnitTagDiabled     = TRUE;
60   var $orig_base = "";
61   var $orig_ou = "";
63         function department ($config, $dn)
64         {
66                 plugin::plugin($config, $dn);
67                 $this->is_account= TRUE;
68                 $this->ui= get_userinfo();
69                 $this->dn= $dn;
70                 $this->orig_dn= $dn;
71                 $this->orig_ou= $this->ou;
72                 $this->config= $config;
74                 /* Set base */
75                 if ($this->dn == "new"){
76                         $ui= get_userinfo();
77                         if(isset($_SESSION['CurrentMainBase'])){
78                                 $this->base= $_SESSION['CurrentMainBase'];
79                         }else{
80                                 $this->base= dn2base($ui->dn);
81                         }
82                 } else {
83                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
84                 }
86     $this->orig_base = $this->base;
88                 /* Is administrational Unit? */
89                 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
90                         $this->is_administrational_unit= true;
91       $this->initially_was_tagged = true;
92                 }
94     if($this->is_account && $dn != "new"){
95       @log::log("view","department/".get_class($this),$this->dn);
96     }
97         }
99         function execute()
100         {
101                 /* Call parent execute */
102                 plugin::execute();
104                 /* Reload departments */
105                 $this->config->get_departments($this->dn);
106                 $this->config->make_idepartments();
107                 $smarty= get_smarty();
109     $tmp = $this->plInfo();
110     foreach($tmp['plProvidedAcls'] as $name => $translation){
111       $smarty->assign($name."ACL",$this->getacl($name));
112     }
114                 /* Base select dialog */
115                 $once = true;
116                 foreach($_POST as $name => $value){
117                         if((preg_match("/^chooseBase/",$name) && $once) && ($this->acl_is_moveable())){
118                                 $once = false;
119                                 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
120                                 $this->dialog->setCurrentBase($this->base);
121                         }
122                 }
124                 /* Dialog handling */
125                 if(is_object($this->dialog)){
126                         /* Must be called before save_object */
127                         $this->dialog->save_object();
129                         if($this->dialog->isClosed()){
130                                 $this->dialog = false;
131                         }elseif($this->dialog->isSelected()){
133         /* A new base was selected, check if it is a valid one */
134         $tmp = $this->get_allowed_bases();
135         if(isset($tmp[$this->dialog->isSelected()])){
136           $this->base = $this->dialog->isSelected();
137         }
138   
139                                 $this->dialog= false;
140                         }else{
141                                 return($this->dialog->execute());
142                         }
143                 }
145                 /* Hide all departments, that are subtrees of this department */
146     $bases = $this->get_allowed_bases();
147                 if(($this->dn == "new")||($this->dn == "")){
148                         $tmp = $bases;
149                 }else{
150                         $tmp    = array();      
151                         foreach($bases as $dn=>$base){
152                                 $fixed = str_replace("/","\\",$this->dn);
153                                 /* Only attach departments which are not a subtree of this one */
154                                 if(!preg_match("/".$fixed."/",$dn)){
155                                         $tmp[$dn]=$base;
156                                 }
157                         }
158                 }
159                 $smarty->assign("bases", $tmp);
161                 foreach ($this->attributes as $val){
162                         $smarty->assign("$val", $this->$val);
163                 }
164                 $smarty->assign("base_select", $this->base);
166     /* Set admin unit flag */
167     $smarty->assign("UnitTagDiabled",$this->UnitTagDiabled);
168     if(!$this->UnitTagDiabled){
169       if ($this->is_administrational_unit) {
170         $smarty->assign("unitTag", "checked");
171       } else {
172         $smarty->assign("unitTag", "");
173       }
174     }
176                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
177         }
179         function clear_fields()
180         {
181                 $this->dn   = "";
182                 $this->base = "";
184                 foreach ($this->attributes as $val){
185                         $this->$val= "";
186                 }
187         }
189         function remove_from_parent()
190         {
191                 $ldap= $this->config->get_ldap_link();
192                 $ldap->cd ($this->dn);
193                 $ldap->recursive_remove();
194     @log::log("remove","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
195     show_ldap_error($ldap->get_error(), sprintf(_("Removing of department with dn '%s' failed."),$this->dn));
197                 /* Optionally execute a command after we're done */
198                 $this->handle_post_events('remove');
199         }
201         function must_be_tagged()
202         {
203                 return $this->must_be_tagged;
204         }
206         function am_i_moved()
207         {
208                 return $this->rec_cpy;
209         }
212         /* Save data to object */
213         function save_object()
214         {
215                 if (isset($_POST['dep_generic_posted'])){
217       /* Create a base backup and reset the
218          base directly after calling plugin::save_object();
219          Base will be set seperatly a few lines below */
220       $base_tmp = $this->base;
221       plugin::save_object();
222       $this->base = $base_tmp;
224       /* Set new base if allowed */
225       $tmp = $this->get_allowed_bases();
226       if(isset($_POST['base'])){
227         if(isset($tmp[$_POST['base']])){
228           $this->base= $_POST['base'];
229         }
230       }
232       /* Save tagging flag */
233       if(!$this->UnitTagDiabled){
234         if ($this->acl_is_writeable("unitTag")){
235           if (isset($_POST['unitTag'])){
236             $this->is_administrational_unit= true;
237           } else {
238             $this->is_administrational_unit= false;
239           }
240         }
241       }
242     }
243         }
246         /* Check values */
247         function check()
248         {
249                 /* Call common method to give check the hook */
250                 $message= plugin::check();
252                 /* Check for presence of this department */
253                 $ldap= $this->config->get_ldap_link();
254     $ldap->ls ("(&(ou=".$this->ou.")(objectClass=organizationalUnit))", $this->base, array('dn'));
255     if ($this->orig_dn == "new" && $ldap->count()){
256                         $message[]= _("Department with that 'Name' already exists.");
257                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
258                         $message[]= _("Department with that 'Name' already exists.");
259                 }
261                 /* All required fields are set? */
262                 if ($this->ou == ""){
263                         $message[]= _("Required field 'Name' is not set.");
264                 }
265                 if ($this->description == ""){
266                         $message[]= _("Required field 'Description' is not set.");
267                 }
269     if(is_department_name_reserved($this->ou,$this->base)){
270       $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$this->ou);
271     }
273                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
274                         $message[]= _("The field 'Name' contains invalid characters.");
275                 }
276                 if (!is_phone_nr($this->telephoneNumber)){
277                         $message[]= _("The field 'Phone' contains an invalid phone number.");
278                 }
279                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
280                         $message[]= _("The field 'Fax' contains an invalid phone number.");
281                 }
283                 return $message;
284         }
287         /* Save to LDAP */
288         function save()
289         {
290                 $ldap= $this->config->get_ldap_link();
292     if(!$this->UnitTagDiabled){
294       /* Add tag objects if needed */
295       if ($this->is_administrational_unit){
297         /* If this wasn't tagged before add oc an reset unit tag */
298         if(!$this->initially_was_tagged){
299           $this->objectclasses[]= "gosaAdministrativeUnit";
300           $this->gosaUnitTag= "";
302           /* It seams that this method is called twice, 
303              set this to true. to avoid adding this oc twice */
304           $this->initially_was_tagged = true;
305         }
307         if ($this->gosaUnitTag == ""){
309           /* It's unlikely, but check if already used... */
310           $try= 5;
311           $ldap->cd($this->config->current['BASE']);
312           while ($try--){
314             /* Generate microtime stamp as tag */
315             list($usec, $sec)= explode(" ", microtime());
316             $time_stamp= preg_replace("/\./", "", $sec.$usec);
318             $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
319             if ($ldap->count() == 0){
320               break;
321             }
322           }
323           if($try == 0) {
324             print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
325             return;
326           }
327           $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
328         }
329       }
330     }
331                 plugin::save();
333     if(!$this->UnitTagDiabled){
335       /* Remove tag information if needed */
336       if (!$this->is_administrational_unit){
337         $tmp= array();
339         /* Remove gosaAdministrativeUnit from this plugin */
340         $has_unit_tag= false;
341         foreach($this->attrs['objectClass'] as $oc){
342           if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
343             $tmp[]= $oc;
344           }
345           if (preg_match("/^gosaAdministrativeUnitTag$/i", $oc)){
346             $has_unit_tag= true;
347           }
348         }
349         $this->attrs['objectClass']= $tmp;
351         if(!$has_unit_tag && isset($this->attrs['gosaUnitTag'])){
352           $this->attrs['gosaUnitTag']= array();
353         }
354         $this->gosaUnitTag = "";
355       }
356     }
358                 /* Write back to ldap */
359                 $ldap= $this->config->get_ldap_link();
360                 $ldap->cat($this->dn, array('dn'));
361                 $a= $ldap->fetch();
362                 $ldap->cd($this->dn);
364       print_a(debug_backtrace());
366                 if (count($a)){
367                         $this->cleanup();
368                         $ldap->modify ($this->attrs); 
369       @log::log("modify","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
370                         $this->handle_post_events('modify');
371                 } else {
372                         $ldap->add($this->attrs);
373                         $this->handle_post_events('add');
374       @log::log("create","department/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
375                 }
376     show_ldap_error($ldap->get_error(), sprintf(_("Saving of department with dn '%s' failed."),$this->dn));
379     if(!$this->UnitTagDiabled){
380       /* The parameter forces only to set must_be_tagged, and don't touch any objects 
381          This will be done later */
382       $this->tag_objects(true);
384       /* Fix tagging if needed */
385       if (!$this->is_administrational_unit){
386         $this->handle_object_tagging();
387       }
388     }
389     
390     /* Optionally execute a command after we're done */
391                 $this->postcreate();
392     return(false);
393         }
396   function ShowMoveFrame()
397   {
398     $smarty = get_smarty();
399     $smarty->assign("src","?plug=".$_GET['plug']."&amp;PerformRecMove");
400     $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.");
401     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
402     return($display);
403   }
405   function ShowTagFrame()
406   {
407     $smarty = get_smarty();
408     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
409     $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.");
410     $display=  $smarty->fetch(get_template_path("dep_iframe.tpl",TRUE));
411     return($display);
412   }
414         /* Tag objects to have the gosaAdministrativeUnitTag */
415         function tag_objects($OnlySetTagFlag = false)
416         {
417     if(!$OnlySetTagFlag){
418       $smarty= get_smarty();
419       /* Print out html introduction */
420       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
421         <html>
422         <head>
423         <title></title>
424         <style type="text/css">@import url("themes/default/style.css");</style>
425         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
426         </head>
427         <body style="background: none; margin:4px;" id="body" >
428         ';
429       echo "<h3>".sprintf(_("Tagging '%s'."),"<i>".@LDAP::fix($this->dn)."</i>")."</h3>";
430     }
432                 $add= $this->is_administrational_unit;
433                 $len= strlen($this->dn);
434                 $ldap= $this->config->get_ldap_link();
435                 $ldap->cd($this->dn);
436                 if ($add){
437                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
438                                                                 $this->gosaUnitTag.')))', array('dn'));
439                 } else {
440                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
441                 }
443                 while ($attrs= $ldap->fetch()){
445                         /* Skip self */
446                         if ($attrs['dn'] == $this->dn){
447                                 continue;
448                         }
450                         /* Check for confilicting administrative units */
451                         $fix= true;
452                         foreach ($this->config->adepartments as $key => $tag){
453                                 /* This one is shorter than our dn, its not relevant... */
454                                 if ($len >= strlen($key)){
455                                         continue;
456                                 }
458                                 /* This one matches with the latter part. Break and don't fix this entry */
459                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
460                                         $fix= false;
461                                         break;
462                                 }
463                         }
465       /* Fix entry if needed */
466       if ($fix){
467         if($OnlySetTagFlag){
468           $this->must_be_tagged =true;
469           return;
470         }
471                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
472                         }
473                 }
474     if(!$OnlySetTagFlag){
475       echo '<p class="seperator">&nbsp;</p>';
476       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
477         <br><input type='submit' name='back' value='"._("Continue")."'>
478         </form></div>";
479     }
480         }
483         /* Move/Rename complete trees */
484         function recursive_move($src_dn, $dst_dn,$force = false)
485         {
486     /* If force == false prepare to recursive move this object from src to dst 
487         on the next call. */
488                 if(!$force){
489                         $this->rec_cpy  = true;
490                         $this->rec_src  = $src_dn;
491                         $this->rec_dst  = $dst_dn;
492                 }else{
494       /* If this is called, but not wanted, abort */
495                         if(!$this->rec_cpy){ 
496                                 return;
497                         }
499                         $src_dn = $this->rec_src;
500                         $dst_dn = $this->rec_dst;
502                         /* Print header to have styles included */
503                         $smarty= get_smarty();
505       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
506         <html>
507         <head>
508         <title></title>
509         <style type="text/css">@import url("themes/default/style.css");</style>
510         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
511         </head>
512         <body style="background: none; margin:4px;" id="body" >
513         ';
514                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".@LDAP::fix($src_dn)."</i>","<i>".@LDAP::fix($dst_dn)."</i>")."</h3>";
517                         /* Check if the destination entry exists */
518                         $ldap= $this->config->get_ldap_link();
520                         /* Check if destination exists - abort */
521                         $ldap->cat($dst_dn, array('dn'));
522                         if ($ldap->fetch()){
523                                 trigger_error("Recursive_move ".@LDAP::fix($dst_dn)." already exists.",
524                                                 E_USER_WARNING);
525                                 echo sprintf("Recursive_move: '%s' already exists", @LDAP::fix($dst_dn))."<br>"; 
526                                 return (FALSE);
527                         }
529                         /* Perform a search for all objects to be moved */
530                         $objects= array();
531                         $ldap->cd($src_dn);
532                         $ldap->search("(objectClass=*)", array("dn"));
533                         while($attrs= $ldap->fetch()){
534                                 $dn= $attrs['dn'];
535                                 $objects[$dn]= strlen($dn);
536                         }
538                         /* Sort objects by indent level */
539                         asort($objects);
540                         reset($objects);
542                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
543                         foreach ($objects as $object => $len){
546                                 $src= str_replace("\\","\\\\",$object);
547                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
548                                 $dst= str_replace($src_dn,$dst_dn,$object);
550                                 echo "<b>"._("Object").":</b> ".@LDAP::fix($src)."<br>";
552         $this->update_acls($object, $dst,TRUE);
554                                 if (!$this->copy($src, $dst)){
555                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),@LDAP::fix($src))."</font>";
556                                         return (FALSE);
557                                 }
558         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
559                                 flush();
560                         }
562                         /* Remove src_dn */
563                         $ldap->cd($src_dn);
564                         $ldap->recursive_remove();
565       $this->dn = $this->rec_dst;
566                         $this->rec_src = $this->rec_dst = "";
567                         $this->rec_cpy =false;
569                         echo '<p class="seperator">&nbsp;</p>';
571                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
572                                 <br><input type='submit' name='back' value='"._("Continue")."'>
573                                 </form></div>";
575       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
576                         echo "</body></html>";
578                         return (TRUE);
579                 }
580         }
583   /* Return plugin informations for acl handling */ 
584   function plInfo()
585   {
586     return (array("plShortName"   => _("Generic"),
587                   "plDescription" => _("Departments"),
588                   "plSelfModify"  => FALSE,
589                   "plPriority"    => 0,
590                   "plDepends"     => array(),
591                   "plSection"     => array("admin"),
592                   "plCategory"    => array("department" => array("objectClass" => "gosaDepartment", "description" => _("Departments"))),
593             
594                   "plProvidedAcls" => array(
595                     "description"       => _("Description"),
596                     "c"                 => _("Country"),
597                     "base"              => _("Base"),
598                     "l"                 => _("Location"),
599                     "telephoneNumber"   => _("Telephone"),
600                     "ou"                => _("Department name"),
601                     "businessCategory"  => _("Category"),
602                     "st"                => _("State"),
603                     "postalAddress"     => _("Address"),
604                     "gosaUnitTag"       => _("Administrative settings"),
605                     "facsimileTelephoneNumber" => _("Fax"))
606                   ));
607   }
609 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
610 ?>