Code

b9e54e3ca91d5b985bc3614a26b22f8f8f8e4695
[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         /* attribute list for save action */
46         var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
47                         "telephoneNumber", "facsimileTelephoneNumber", "gosaUnitTag");
48         var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
50         function department ($config, $dn)
51         {
53                 plugin::plugin($config, $dn);
54                 $this->is_account= TRUE;
55                 $this->ui= get_userinfo();
56                 $this->dn= $dn;
57                 $this->orig_dn= $dn;
58                 $this->config= $config;
60                 /* Set base */
61                 if ($this->dn == "new"){
62                         $ui= get_userinfo();
63                         if(isset($_SESSION['CurrentMainBase'])){
64                                 $this->base= $_SESSION['CurrentMainBase'];
65                         }else{
66                                 $this->base= dn2base($ui->dn);
67                         }
68                 } else {
69                         $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
70                 }
72                 /* set permissions */
73                 $ui= get_userinfo();
74                 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
75                 $this->acl= get_module_permission($acl, "department", $ui->dn);
77                 /* Is administrational Unit? */
78                 if ($dn != "new" && in_array_ics('gosaAdministrativeUnit', $this->attrs['objectClass'])){
79                         $this->is_administrational_unit= true;
80                 }
81         }
83         function execute()
84         {
85                 /* Call parent execute */
86                 plugin::execute();
88                 /* Reload departments */
89                 $this->config->get_departments($this->dn);
90                 $this->config->make_idepartments();
91                 $smarty= get_smarty();
93                 /* Base select dialog */
94                 $once = true;
95                 foreach($_POST as $name => $value){
96                         if(preg_match("/^chooseBase/",$name) && $once){
97                                 $once = false;
98                                 $this->dialog = new baseSelectDialog($this->config);
99                                 $this->dialog->setCurrentBase($this->base);
100                         }
101                 }
103                 /* Dialog handling */
104                 if(is_object($this->dialog)){
105                         /* Must be called before save_object */
106                         $this->dialog->save_object();
108                         if($this->dialog->isClosed()){
109                                 $this->dialog = false;
110                         }elseif($this->dialog->isSelected()){
111                                 $this->base = $this->dialog->isSelected();
112                                 $this->dialog= false;
113                         }else{
114                                 return($this->dialog->execute());
115                         }
116                 }
118                 /* Hide all departments, that are subtrees of this department */
119                 $bases  = $this->config->idepartments;
120                 if(($this->dn == "new")||($this->dn == "")){
121                         $tmp = $bases;
122                 }else{
123                         $tmp    = array();      
124                         foreach($bases as $dn=>$base){
125                                 $fixed = str_replace("/","\\",$this->dn);
126                                 /* Only attach departments which are not a subtree of this one */
127                                 if(!preg_match("/".$fixed."/",$dn)){
128                                         $tmp[$dn]=$base;
129                                 }
130                         }
131                 }
132                 $smarty->assign("bases", $tmp);
134                 foreach ($this->attributes as $val){
135                         $smarty->assign("$val", $this->$val);
136                         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
137                 }
138                 $smarty->assign("base_select", $this->base);
140                 /* Set admin unit flag */
141                 if ($this->is_administrational_unit) {
142                         $smarty->assign("unitTag", "checked");
143                 } else {
144                         $smarty->assign("unitTag", "");
145                 }
146                 $smarty->assign("unitTag"."ACL", chkacl($this->acl, "unitTag"));
147                 
148                 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
149         }
151         function clear_fields()
152         {
153                 $this->dn= "";
154                 $this->base= "";
155                 $this->acl= "#none#";
157                 foreach ($this->attributes as $val){
158                         $this->$val= "";
159                 }
160         }
163         function remove_from_parent()
164         {
165                 $ldap= $this->config->get_ldap_link();
166                 $ldap->cd ($this->dn);
167                 $ldap->recursive_remove();
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);
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                 /* Validate and modify - or: spaghetti rules! */
227                 if ($this->ou == "incoming"){
228                         $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
229                                         " Please choose another name.");
230                 }
231                 if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
232                         $message[]= _("The field 'Name' contains invalid characters.");
233                 }
234                 if (!is_phone_nr($this->telephoneNumber)){
235                         $message[]= _("The field 'Phone' contains an invalid phone number.");
236                 }
237                 if (!is_phone_nr($this->facsimileTelephoneNumber)){
238                         $message[]= _("The field 'Fax' contains an invalid phone number.");
239                 }
241                 return $message;
242         }
245         /* Save to LDAP */
246         function save()
247         {
248                 $ldap= $this->config->get_ldap_link();
250                 /* Add tag objects if needed */
251                 if ($this->is_administrational_unit){
252                         $this->objectclasses[]= "gosaAdministrativeUnit";
253                         if ($this->gosaUnitTag == ""){
255                                 /* It's unlikely, but check if already used... */
256                                 $try= 5;
257                                 $ldap->cd($this->config->current['BASE']);
258                                 while ($try--){
260                                         /* Generate microtime stamp as tag */
261                                         list($usec, $sec)= explode(" ", microtime());
262                                         $time_stamp= preg_replace("/\./", "", $sec.$usec);
264                                         $ldap->search("(&(objectClass=gosaAdministrativeUnit)(gosaUnitTag=$time_stamp))",array("gosaUnitTag"));
265                                         if ($ldap->count() == 0){
266                                                 break;
267                                         }
268                                 }
269                                 if($try == 0) {
270                                         print_red(_("Fatal error: Can't find an unused tag to mark the administrative unit!"));
271                                         return;
272                                 }
273                                 $this->gosaUnitTag= preg_replace("/\./", "", $sec.$usec);
274                         }
275                 } else {
276                                 $this->gosaUnitTag= "";
277                 }
279                 plugin::save();
281                 /* Remove tag information if needed */
282                 if (!$this->is_administrational_unit){
283                         $tmp= array();
285                         /* Remove gosaAdministrativeUnit from this plugin */
286                         foreach($this->attrs['objectClass'] as $oc){
287                                 if (!preg_match("/^gosaAdministrativeUnit$/i", $oc)){
288                                         $tmp[]= $oc;
289                                 }
290                         }
291                         $this->attrs['objectClass']= $tmp;
292                         if(isset($this->attrs['gosaUnitTag'])){
293                                 $this->attrs['gosaUnitTag']= array();
294                         }
295                 }
296                 
297                 /* Write back to ldap */
298                 $ldap= $this->config->get_ldap_link();
299                 $ldap->cat($this->dn);
300                 $a= $ldap->fetch();
301                 $ldap->cd($this->dn);
302                 
303                 if (count($a)){
304                         $this->cleanup();
305                         $ldap->modify ($this->attrs); 
307                         $this->handle_post_events('modify');
308                 } else {
309                         $ldap->add($this->attrs);
310                         $this->handle_post_events('add');
311                 }
312                 show_ldap_error($ldap->get_error());
313                 $this->tag_objects();
315                 /* Optionally execute a command after we're done */
316                 $this->postcreate();
317         }
320         /* Tag objects to have the gosaAdministrativeUnitTag */
321         function tag_objects()
322         {
323                 $add= $this->is_administrational_unit;
324                 $len= strlen($this->dn);
325                 $ldap= $this->config->get_ldap_link();
326                 $ldap->cd($this->dn);
327                 if ($add){
328                         $ldap->search('(!(&(objectClass=gosaAdministrativeUnitTag)(gosaUnitTag='.
329                                         $this->gosaUnitTag.')))', array('dn'));
330                 } else {
331                         $ldap->search('objectClass=gosaAdministrativeUnitTag', array('dn'));
332                 }
333                 while ($attrs= $ldap->fetch()){
335                         /* Skip self */
336                         if ($attrs['dn'] == $this->dn){
337                                 continue;
338                         }
340                         /* Check for confilicting administrative units */
341                         $fix= true;
342                         foreach ($this->config->adepartments as $key => $tag){
343                                 /* This one is shorter than our dn, its not relevant... */
344                                 if ($len >= strlen($key)){
345                                         continue;
346                                 }
348                                 /* This one matches with the latter part. Break and don't fix this entry */
349                                 if (preg_match('/(^|,)'.normalizePreg($key).'$/', $attrs['dn'])){
350                                         $fix= false;
351                                         break;
352                                 }
353                         }
354                         
355                         /* Fix entry if needed */
356                         if ($fix){
357                                 $this->handle_object_tagging($attrs['dn'], $this->gosaUnitTag);
358                         } else {
359                                 echo "Don't touch: ".$attrs['dn']."<br>";
360                         }
361                 }
362         }
365         /* Move/Rename complete trees */
366         function recursive_move($src_dn, $dst_dn,$force = false)
367         {
368                 if(!$force){
370                         $this->rec_cpy  = true;
371                         $this->rec_src  = $src_dn;
372                         $this->rec_dst  = $dst_dn;
374                         $smarty = get_smarty();
376                         $smarty->assign("src","?plug=".$_GET['plug']."&PerformRecMove");
378                         $display=  $smarty->fetch(get_template_path("recursive_move.tpl",TRUE));
379                         return($display);
380                         exit();
381                 }else{
382                         if(!$this->rec_cpy){ 
383                                 return;
384                         }
386                         $src_dn = $this->rec_src;
387                         $dst_dn = $this->rec_dst;
389                         /* Print header to have styles included */
390                         $smarty= get_smarty();
391                         echo "<!-- headers.tpl-->".$smarty->fetch(get_template_path('headers.tpl'));
392                         echo "<body style='background-image:none;margin:3px;color:black'>";
394                         echo "<h3>".sprintf(_("Moving %s to %s"),"<i>".$src_dn."</i>","<i>".$dst_dn."</i>")."</h3>";
397                         /* Check if the destination entry exists */
398                         $ldap= $this->config->get_ldap_link();
400                         /* Check if destination exists - abort */
401                         $ldap->cat($dst_dn);
402                         if ($ldap->fetch()){
403                                 trigger_error("Recursive_move $dst_dn already exists.",
404                                                 E_USER_WARNING);
405                                 echo "Recursive_move :$dst_dn already exists.<br>"; 
406                                 return (FALSE);
407                         }
408         
409                         /* Perform a search for all objects to be moved */
410                         $objects= array();
411                         $ldap->cd($src_dn);
412                         $ldap->search("(objectClass=*)", array("dn"));
413                         while($attrs= $ldap->fetch()){
414                                 $dn= $attrs['dn'];
415                                 $objects[$dn]= strlen($dn);
416                         }
418                         /* Sort objects by indent level */
419                         asort($objects);
420                         reset($objects);
422                         /* Copy objects from small to big indent levels by replacing src_dn by dst_dn */
423                         foreach ($objects as $object => $len){
425                                 
426                                 $src= str_replace("\\","\\\\",$object);
427                                 $dst= preg_replace("/".str_replace("\\","\\\\",$src_dn)."$/", "$dst_dn", $object);
428                                 $dst= str_replace($src_dn,$dst_dn,$object);
430                                 echo "<b>"._("Object").":</b> $src<br>";
431                                 
432                                 if (!$this->copy($src, $dst)){
433                                         echo "<font color='#FF0000'><br>".sprintf(_("FAILED to copy %s, aborting operation"),$src)."</font>";
434                                         return (FALSE);
435                                 }
437                                 flush();
438                         }
440                         /* Remove src_dn */
441                         $ldap->cd($src_dn);
442                         $ldap->recursive_remove();
443                         $this->rec_src = $this->rec_dst = "";
444                         $this->rec_cpy =false;
446                         echo '<p class="seperator">&nbsp;</p>';
448                         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
449                                 <br><input type='submit' name='back' value='"._("Continue")."'>
450                                 </form></div>";
452                         echo "</body></html>";
453                         
454                         return (TRUE);
455                 }
456         }
458         function am_i_moved()
459         {
460                 return $this->rec_cpy;
461         }
465 ?>