Code

53afc78d764a2522553ad083b63b4cf79b46d4e5
[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("base_select", $this->base);
142                 /* Set admin unit flag */
143                 if ($this->is_administrational_unit) {
144                         $smarty->assign("unitTag", "checked");
145                 } else {
146                         $smarty->assign("unitTag", "");
147                 }
148                 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
150                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
151         }
153         function clear_fields()
154         {
155                 $this->dn= "";
156                 $this->base= "";
157                 $this->acl= "#none#";
159                 foreach ($this->attributes as $val){
160                         $this->$val= "";
161                 }
162         }
164         function remove_from_parent()
165         {
166                 $ldap= $this->config->get_ldap_link();
167                 $ldap->cd ($this->dn);
168                 $ldap->recursive_remove();
170                 /* Optionally execute a command after we're done */
171                 $this->handle_post_events('remove');
172         }
174         function must_be_tagged()
175         {
176                 return $this->must_be_tagged;
177         }
179         function am_i_moved()
180         {
181                 return $this->rec_cpy;
182         }
185         /* Save data to object */
186         function save_object()
187         {
188                 if (isset($_POST['base'])){
189                         plugin::save_object();
191                         /* Save base, since this is no LDAP attribute */
192                         if (chkacl($this->acl, "create") == ""){
193                                 $this->base= $_POST['base'];
194                         }
196                         /* Save tagging flag */
197                         if (chkacl($this->acl, "unitTag") == ""){
198                                 if (isset($_POST['unitTag'])){
199                                         $this->is_administrational_unit= true;
200                                 } else {
201                                         $this->is_administrational_unit= false;
202                                 }
203                         }
204                 }
205         }
208         /* Check values */
209         function check()
210         {
211                 /* Call common method to give check the hook */
212                 $message= plugin::check();
214                 /* Permissions for that base? */
215                 //      $this->dn= "ou=$this->ou,".$this->base;
216                 if (chkacl($this->acl, "create") != ""){
217                         $message[]= _("You have no permissions to create a department on this 'Base'.");
218                 }
220                 /* Check for presence of this department */
221                 $ldap= $this->config->get_ldap_link();
222                 $attrs= $ldap->cat ($this->dn, array('dn'));
223                 if ($this->orig_dn == "new" && !($attrs === FALSE)){
224                         $message[]= _("Department with that 'Name' already exists.");
225                 } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
226                         $message[]= _("Department with that 'Name' already exists.");
227                 }
229                 /* All required fields are set? */
230                 if ($this->ou == ""){
231                         $message[]= _("Required field 'Name' is not set.");
232                 }
233                 if ($this->description == ""){
234                         $message[]= _("Required field 'Description' is not set.");
235                 }
237                 /* Validate and modify - or: spaghetti rules! */
238                 $SkipNames = array(     "incoming","apps","systems","fai","config",     
239                                 preg_replace("/ou=(.*),/","\\1",get_people_ou()),
240                                 preg_replace("/ou=(.*),/","\\1",get_groups_ou())
241                                 );      
242                 foreach($SkipNames as $name){
243                         if ($this->ou == $name){
244                                 $message[]= sprintf(_("The field 'Name' contains the reserved word '%s'. Please choose another name."),$name);
245                         }
246                 }
249                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
250                         $message[]= _("The field 'Name' contains invalid characters.");
251                 }
252                 if (!is_phone_nr($this->telephoneNumber)){
253                         $message[]= _("The field 'Phone' contains an invalid phone number.");
254                 }
255                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
256                         $message[]= _("The field 'Fax' contains an invalid phone number.");
257                 }
259                 return $message;
260         }
263         /* Save to LDAP */
264         function save()
265         {
266                 $ldap= $this->config->get_ldap_link();
268                 /* Add tag objects if needed */
269                 if ($this->is_administrational_unit){
270       if(!in_array_ics("gosaAdministrativeUnit",$this->objectclasses)){
271                         $this->objectclasses[]= "gosaAdministrativeUnit";
272       }
273                         if ($this->gosaUnitTag == ""){
275                                 /* It's unlikely, but check if already used... */
276                                 $try= 5;
277                                 $ldap->cd($this->config->current['BASE']);
278                                 while ($try--){
280                                         /* Generate microtime stamp as tag */
281                                         list($usec, $sec)= explode(" ", microtime());
282                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
284                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
285                                         if ($ldap->count() == 0){
286                                                 break;
287                                         }
288                                 }
289                                 if($try == 0) {
290                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
291                                         return;
292                                 }
293                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
294                         }
295                 } else {
296                         $this->gosaUnitTag= "";
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                         foreach($this->attrs['objectClass'] as $oc){
307                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
308                                         $tmp[]= $oc;
309                                 }
310                         }
311                         $this->attrs['objectClass']= $tmp;
312                         if(isset($this->attrs['gosaUnitTag'])){
313                                 $this->attrs['gosaUnitTag']= array();
314                         }
315                 }
317                 /* Write back to ldap */
318                 $ldap= $this->config->get_ldap_link();
319                 $ldap->cat($this->dn, array('dn'));
320                 $a= $ldap->fetch();
321                 $ldap->cd($this->dn);
323                 if (count($a)){
324                         $this->cleanup();
325                         $ldap->modify ($this->attrs); 
327                         $this->handle_post_events('modify');
328                 } else {
329                         $ldap->add($this->attrs);
330                         $this->handle_post_events('add');
331                 }
332                 show_ldap_error($ldap->get_error());
334     /* The parameter forces only to set must_be_tagged, and don't touch any objects 
335         This will be done later */
336     $this->tag_objects(true);
337     
338     /* Optionally execute a command after we're done */
339                 $this->postcreate();
340     return(true);
341         }
344   function ShowMoveFrame()
345   {
346     $smarty = get_smarty();
347     $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
348     $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
349     return($display);
350   }
352   function ShowTagFrame()
353   {
354     $smarty = get_smarty();
355     $smarty->assign("src","?plug=".$_GET['plug']."&TagDepartment");
356     $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
357     return($display);
358   }
360         /* Tag objects to have the gosaAdministrativeUnitTag */
361         function tag_objects($OnlySetTagFlag = false)
362         {
363                 $add= $this->is_administrational_unit;
364                 $len= strlen($this->dn);
365                 $ldap= $this->config->get_ldap_link();
366                 $ldap->cd($this->dn);
367                 if ($add){
368                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
369                                                                 $this->gosaUnitTag.')))', array('dn'));
370                 } else {
371                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
372                 }
373                 while ($attrs= $ldap->fetch()){
375                         /* Skip self */
376                         if ($attrs['dn'] == $this->dn){
377                                 continue;
378                         }
380                         /* Check for confilicting administrative units */
381                         $fix= true;
382                         foreach ($this->config->adepartments as $key => $tag){
383                                 /* This one is shorter than our dn, its not relevant... */
384                                 if ($len >= strlen($key)){
385                                         continue;
386                                 }
388                                 /* This one matches with the latter part. Break and don't fix this entry */
389                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
390                                         $fix= false;
391                                         break;
392                                 }
393                         }
395       /* Fix entry if needed */
396       if ($fix){
397         if($OnlySetTagFlag){
398           $this->must_be_tagged =true;
399           return;
400         }
401                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag, TRUE );
402                         }
403                 }
404     if(!$OnlySetTagFlag){
405       echo '<p class="seperator">&nbsp;</p>';
406       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
407         <br><input type='submit' name='back' value='"._("Continue")."'>
408         </form></div>";
409     }
410         }
413         /* Move/Rename complete trees */
414         function recursive_move($src_dn, $dst_dn,$force = false)
415         {
416     /* If force == false prepare to recursive move this object from src to dst 
417         on the next call. */
418                 if(!$force){
419                         $this->rec_cpy  = true;
420                         $this->rec_src  = $src_dn;
421                         $this->rec_dst  = $dst_dn;
422                 }else{
424       /* If this is called, but not wanted, abort */
425                         if(!$this->rec_cpy){ 
426                                 return;
427                         }
429                         $src_dn = $this->rec_src;
430                         $dst_dn = $this->rec_dst;
432                         /* Print header to have styles included */
433                         $smarty= get_smarty();
434                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
435                         echo "<body style='background-image:none;margin:3px;color:black'>";
437                         echo "<h3>".sprintf(_("Moving '%s' to '%s'"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
440                         /* Check if the destination entry exists */
441                         $ldap= $this->config->get_ldap_link();
443                         /* Check if destination exists - abort */
444                         $ldap->cat($dst_dn, array('dn'));
445                         if ($ldap->fetch()){
446                                 trigger_error("Recursive_move $dst_dn already exists.",
447                                                 E_USER_WARNING);
448                                 echo sprintf("Recursive_move: '%s' already exists", $dst_dn)."<br>"; 
449                                 return (FALSE);
450                         }
452                         /* Perform a search for all objects to be moved */
453                         $objects= array();
454                         $ldap->cd($src_dn);
455                         $ldap->search("(objectClass=*)", array("dn"));
456                         while($attrs= $ldap->fetch()){
457                                 $dn= $attrs['dn'];
458                                 $objects[$dn]= strlen($dn);
459                         }
461                         /* Sort objects by indent level */
462                         asort($objects);
463                         reset($objects);
465                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
466                         foreach ($objects as $object => $len){
469                                 $src= str_replace("\\","\\\\",$object);
470                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
471                                 $dst= str_replace($src_dn,$dst_dn,$object);
473                                 echo "<b>"._("Object").":</b> $src<br>";
475                                 if (!$this->copy($src, $dst)){
476                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
477                                         return (FALSE);
478                                 }
480                                 flush();
481                         }
483                         /* Remove src_dn */
484                         $ldap->cd($src_dn);
485                         $ldap->recursive_remove();
486                         $this->rec_src = $this->rec_dst = "";
487                         $this->rec_cpy =false;
489                         echo '<p class="seperator">&nbsp;</p>';
491                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
492                                 <br><input type='submit' name='back' value='"._("Continue")."'>
493                                 </form></div>";
495                         echo "</body></html>";
497                         return (TRUE);
498                 }
499         }
501 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
502 ?>