Code

Reverted global session changes. Too time consuming.
[gosa.git] / gosa-core / plugins / personal / posix / class_posixAccount.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 /*!
24   \brief   posixAccount plugin
25   \author  Cajus Pollmeier <pollmeier@gonicus.de>
26   \version 2.00
27   \date    24.07.2003
29   This class provides the functionality to read and write all attributes
30   relevant for posixAccounts and shadowAccounts from/to the LDAP. It
31   does syntax checking and displays the formulars required.
32  */
34 class posixAccount extends plugin
35 {
36   /* Definitions */
37   var $plHeadline= "UNIX";
38   var $plDescription= "Edit users POSIX settings";
40   /* Plugin specific values */
41   var $homeDirectory= "";
42   var $loginShell= "/bin/bash";
43   var $uidNumber= "";
44   var $gidNumber= "";
45   var $gecos= "";
46   var $shadowMin= "0";
47   var $shadowMax= "0";
48   var $shadowWarning= "0";
49   var $shadowLastChange= "0";
50   var $shadowInactive= "0";
51   var $shadowExpire= "0";
52   var $gosaDefaultPrinter= "";
53   var $accessTo= array();
54   var $trustModel= "";
56   var $glist=array();
57   var $status= "";
58   var $loginShellList= array();
59   var $groupMembership= array();
60   var $savedGroupMembership= array();
61   var $savedUidNumber= "";
62   var $savedGidNumber= "";
63   var $activate_shadowMin= "0";
64   var $activate_shadowMax= "0";
65   var $activate_shadowWarning= "0";
66   var $activate_shadowInactive= "0";
67   var $activate_shadowExpire= "0";
68   var $mustchangepassword= "0";
69   var $force_ids= 0;
70   var $group_dialog= FALSE;
71   var $show_ws_dialog= FALSE;
72   var $secondaryGroups= array();
73   var $primaryGroup= 0;
74   var $was_trust_account= FALSE;
75   var $memberGroup = array();
76   var $grouplist  = array();
77   var $ui         = array();
79   var $GroupRegex       = "*";
80   var $GroupUserRegex   = "*";
81   var $SubSearch        = false;
83   var $view_logged = FALSE;
85   /* attribute list for save action */
86   var $CopyPasteVars  = 
87       array("grouplist","groupMembership","activate_shadowMin",
88       "activate_shadowMax","activate_shadowWarning","activate_shadowInactive","activate_shadowExpire",
89       "must_change_password","printerList","grouplist","savedGidNumber","savedUidNumber");
91   var $attributes     = array("homeDirectory", "loginShell", "uidNumber", "gidNumber", "gecos",
92       "shadowMin", "shadowMax", "shadowWarning", "shadowInactive", "shadowLastChange",
93       "shadowExpire", "gosaDefaultPrinter", "uid","accessTo","trustModel");
95   var $objectclasses= array("posixAccount", "shadowAccount");
97   var $uid= "";
98   var $multiple_support = TRUE;
99   var $groupMembership_some = array();
101   /* constructor, if 'dn' is set, the node loads the given
102      'dn' from LDAP */
103   function posixAccount (&$config, $dn= NULL)
104   {
105     /* Configuration is fine, allways */
106     $this->config= $config;
108     /* Load bases attributes */
109     plugin::plugin($config, $dn);
111     /* Setting uid to default */
112     if(isset($this->attrs['uid'][0])){
113       $this->uid = $this->attrs['uid'][0];
114     }
116     $ldap= $this->config->get_ldap_link();
118     if ($dn !== NULL){
120       /* Correct is_account. shadowAccount is not required. */
121       if (isset($this->attrs['objectClass']) &&
122           in_array ('posixAccount', $this->attrs['objectClass'])){
124         $this->is_account= TRUE;
125       }
127       /* Is this account a trustAccount? */
128       if ($this->is_account && isset($this->attrs['trustModel'])){
129         $this->trustModel= $this->attrs['trustModel'][0];
130         $this->was_trust_account= TRUE;
131       } else {
132         $this->was_trust_account= FALSE;
133         $this->trustModel= "";
134       }
136       $this->accessTo = array(); 
137       if ($this->is_account && isset($this->attrs['accessTo'])){
138         for ($i= 0; $i<$this->attrs['accessTo']['count']; $i++){
139           $tmp= $this->attrs['accessTo'][$i];
140           $this->accessTo[$tmp]= $tmp;
141         }
142       }
143       $this->initially_was_account= $this->is_account;
145       /* Fill group */
146       $this->primaryGroup= $this->gidNumber;
148       /* Generate status text */
149       $current= date("U");
151       $current= floor($current / 60 /60 / 24);
153       if (($current >= $this->shadowExpire) && $this->shadowExpire){
154         $this->status= _("expired");
155         if (($current - $this->shadowExpire) < $this->shadowInactive){
156           $this->status.= ", "._("grace time active");
157         }
158       } elseif (($this->shadowLastChange + $this->shadowMin) >= $current){
159         $this->status= _("active").", "._("password not changeable");
160       } elseif (($this->shadowLastChange + $this->shadowMax) >= $current){
161         $this->status= _("active").", "._("password expired");
162       } else {
163         $this->status= _("active");
164       }
166       /* Get group membership */
167       $ldap->cd($this->config->current['BASE']);
168       $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("cn", "description"));
170       while ($attrs= $ldap->fetch()){
171         if (!isset($attrs["description"][0])){
172           $entry= $attrs["cn"][0];
173         } else {
174           $entry= $attrs["cn"][0]." [".$attrs["description"][0]."]";
175         }
176         $this->groupMembership[$ldap->getDN()]= $entry;
177       }
178       asort($this->groupMembership);
179       reset($this->groupMembership);
180       $this->savedGroupMembership= $this->groupMembership;
181       $this->savedUidNumber= $this->uidNumber;
182       $this->savedGidNumber= $this->gidNumber;
183     }
185     /* Adjust shadow checkboxes */
186     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
187           "shadowExpire") as $val){
189       if ($this->$val != 0){
190         $oval= "activate_".$val;
191         $this->$oval= "1";
192       }
193     }
195     /* Convert to seconds */
196     $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
198     /* Generate shell list from CONFIG_DIR./shells */
199     if (file_exists(CONFIG_DIR.'/shells')){
200       $shells = file (CONFIG_DIR.'/shells');
201       foreach ($shells as $line){
202         if (!preg_match ("/^#/", $line)){
203           $this->loginShellList[]= trim($line);
204         }
205       }
206     } else {
207       if ($this->loginShell == ""){
208         $this->loginShellList[]= _("unconfigured");
209       }
210     }
212     /* Insert possibly missing loginShell */
213     if ($this->loginShell != "" && !in_array($this->loginShell, $this->loginShellList)){
214       $this->loginShellList[]= $this->loginShell;
215     }
217     /* Generate group list */
218     $this->ui = get_userinfo(); 
219     $this->secondaryGroups[]= "- "._("automatic")." -";
220     $ldap->cd($this->config->current['BASE']);
221     $ldap->search("(objectClass=posixGroup)", array("cn", "gidNumber"));
222     while($attrs = $ldap->fetch()){
223       $this->secondaryGroups[$attrs['gidNumber'][0]]= $attrs['cn'][0];
224     }
225     asort ($this->secondaryGroups);
227     /* Get global filter config */
228     if (!session::is_set("sysfilter")){
229       $ui= get_userinfo();
230       $base= get_base_from_people($ui->dn);
231       $sysfilter= array( "depselect"       => $base,
232           "regex"           => "*");
233       session::set("sysfilter", $sysfilter);
234     }
235     $this->ui = get_userinfo();
236   }
239   /* execute generates the html output for this node */
240   function execute($isCopyPaste = false)
241   {
242     /* Call parent execute */
243     plugin::execute();
244     $display= "";
246     /* Log view */
247     if($this->is_account && !$this->view_logged){
248       $this->view_logged = TRUE;
249       new log("view","users/".get_class($this),$this->dn);
250     }
252     /* Department has changed? */
253     if(isset($_POST['depselect'])){
254       session::set('CurrentMainBase',validate($_POST['depselect']));
255     }
257     if($this->multiple_support_active){
258       $this->is_account = TRUE;
259     }
261     if(!$isCopyPaste && ! $this->multiple_support_active){
263       /* Do we need to flip is_account state? */
264       if(isset($_POST['modify_state'])){
265         if($this->is_account && $this->acl_is_removeable()){
266           $this->is_account= FALSE;
267         }elseif(!$this->is_account && $this->acl_is_createable()){
268           $this->is_account= TRUE;
269         }
270       }
272       /* Do we represent a valid posixAccount? */
273       if (!$this->is_account && $this->parent === NULL ){
274         $display= "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
275           msgPool::noValidExtension(_("POSIX"))."</b>";
276         $display.= back_to_main();
277         return ($display);
278       }
281       /* Show tab dialog headers */
282       if ($this->parent !== NULL){
283         if ($this->is_account){
284           if (isset($this->parent->by_object['sambaAccount'])){
285             $obj= $this->parent->by_object['sambaAccount'];
286           }
287           if (isset($obj) && $obj->is_account == TRUE &&
288               ((isset($this->parent->by_object['sambaAccount']))&&($this->parent->by_object['sambaAccount']->is_account))
289               ||(isset($this->parent->by_object['environment'] ))&&($this->parent->by_object['environment'] ->is_account)){
291             /* Samba3 dependency on posix accounts are enabled
292                in the moment, because I need to rely on unique
293                uidNumbers. There'll be a better solution later
294                on. */
295             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX"), array(_("Samba"), _("Environment"))), TRUE);
296           } else {
297             $display= $this->show_disable_header(msgPool::removeFeaturesButton(_("POSIX")), msgPool::featuresEnabled(_("POSIX")));
298           }
299         } else {
300           $display= $this->show_enable_header(msgPool::addFeaturesButton(_("POSIX")), msgPool::featuresDisabled(_("POSIX")));
301           return($display);
302         }
303       }
304     }
305     /* Trigger group edit? */
306     if (isset($_POST['edit_groupmembership'])){
307       $this->group_dialog= TRUE;
308       $this->dialog= TRUE;
309     }
311     /* Cancel group edit? */
312     if (isset($_POST['add_groups_cancel']) ||
313         isset($_POST['add_groups_finish'])){
314       $this->group_dialog= FALSE;
315       $this->dialog= FALSE;
316     }
318     /* Add selected groups */
319     if (isset($_POST['add_groups_finish']) && isset($_POST['groups']) &&
320         count($_POST['groups'])){
322       $this->addGroup ($_POST['groups']);
323     }
325     /* Delete selected groups */
326     if (isset($_POST['delete_groupmembership']) && 
327         isset($_POST['group_list']) && count($_POST['group_list'])){
329       $this->delGroup ($_POST['group_list']);
330     }
332     /* Add user workstation? */
333     if (isset($_POST["add_ws"])){
334       $this->show_ws_dialog= TRUE;
335       $this->dialog= TRUE;
336     }
338     /* Add user workstation? */
339     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
340       foreach($_POST['wslist'] as $ws){
341         $this->accessTo[$ws]= $ws;
342       }
343       ksort($this->accessTo);
344       $this->is_modified= TRUE;
345     }
347     /* Remove user workstations? */
348     if (isset($_POST["delete_ws"]) && isset($_POST['workstation_list'])){
349       foreach($_POST['workstation_list'] as $name){
350         unset ($this->accessTo[$name]);
351       }
352       $this->is_modified= TRUE;
353     }
355     /* Add user workstation finished? */
356     if (isset($_POST["add_ws_finish"]) || isset($_POST["add_ws_cancel"])){
357       $this->show_ws_dialog= FALSE;
358       $this->dialog= FALSE;
359     }
361     /* Templates now! */
362     $smarty= get_smarty();
364     /* Show ws dialog */
365     if ($this->show_ws_dialog){
366       /* Save data */
367       $sysfilter= session::get("sysfilter");
368       foreach( array("depselect", "regex") as $type){
369         if (isset($_POST[$type])){
370           $sysfilter[$type]= $_POST[$type];
371         }
372       }
373       if (isset($_GET['search'])){
374         $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
375         if ($s == "**"){
376           $s= "*";
377         }
378         $sysfilter['regex']= $s;
379       }
380       session::set("sysfilter", $sysfilter);
382       /* Get workstation list */
383       $exclude= "";
384       foreach($this->accessTo as $ws){
385         $exclude.= "(cn=$ws)";
386       }
387       if ($exclude != ""){
388         $exclude= "(!(|$exclude))";
389       }
390       $regex= $sysfilter['regex'];
392       $acls = array();
393       if(class_available("servgeneric")) $acls[] = "server";
394       if(class_available("workgeneric")) $acls[] = "worstation";
395       if(class_available("termgeneric")) $acls[] = "terminal";
397       $filter= "(&(|(objectClass=goServer)(objectClass=gotoWorkstation)(objectClass=gotoTerminal))$exclude(cn=*)(cn=$regex))";
399       $deps_a = array(get_ou("serverRDN"),
400                       get_ou("terminalRDN"),
401                       get_ou("workstationRDN")); 
403       $res= get_sub_list($filter, $acls, $deps_a, get_ou("systemRDN").$sysfilter['depselect'], array("cn"), GL_SUBSEARCH | GL_SIZELIMIT);
404       $wslist= array();
405       foreach ($res as $attrs){
406         $wslist[]= preg_replace('/\$/', '', $attrs['cn'][0]);
407       }
408       asort($wslist);
409       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
410       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
411       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
412       $smarty->assign("deplist", $this->config->idepartments);
413       $smarty->assign("alphabet", generate_alphabet());
414       foreach( array("depselect", "regex") as $type){
415         $smarty->assign("$type", $sysfilter[$type]);
416       }
417       $smarty->assign("hint", print_sizelimit_warning());
418       $smarty->assign("wslist", $wslist);
419       $smarty->assign("apply", apply_filter());
420       $display= $smarty->fetch (get_template_path('trust_machines.tpl', TRUE, dirname(__FILE__)));
421       return ($display);
422     }
424     /* Manage group add dialog */
425     if ($this->group_dialog){
427       /* Get global filter config */
428       $this->reload();
430       /* remove already assigned groups */
431       $glist= array();
432       foreach ($this->grouplist as $key => $value){
433         if (!isset($this->groupMembership[$key]) && obj_is_writable($key,"groups/group","memberUid")){
434           $glist[$key]= $value;
435         }
436       }
438       if($this->SubSearch){
439         $smarty->assign("SubSearchCHK"," checked ");
440       }else{
441         $smarty->assign("SubSearchCHK","");
442       }
444       $smarty->assign("regex",$this->GroupRegex);
445       $smarty->assign("guser",$this->GroupUserRegex);
446       $smarty->assign("groups", $glist);
447       $smarty->assign("search_image", get_template_path('images/lists/search.png'));
448       $smarty->assign("launchimage", get_template_path('images/lists/action.png'));
449       $smarty->assign("tree_image", get_template_path('images/lists/search-subtree.png'));
450       $smarty->assign("deplist", $this->config->idepartments);
451       $smarty->assign("alphabet", generate_alphabet());
452       $smarty->assign("depselect", session::get('CurrentMainBase'));
453       $smarty->assign("hint", print_sizelimit_warning());
455       $smarty->assign("apply", apply_filter());
456       $display.= $smarty->fetch (get_template_path('posix_groups.tpl', TRUE, dirname(__FILE__)));
457       return ($display);
458     }
460     /* Show main page */
461     $smarty= get_smarty();
463     /* In 'MyAccount' mode, we must remove write acls if we are not in editing mode. */ 
464     $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
466     /* Depending on pwmode, currently hardcoded because there are no other methods */
467     if ( 1 == 1 ){
468       $smarty->assign("pwmode", dirname(__FILE__)."/posix_shadow");
470       $shadowMinACL     =  $this->getacl("shadowMin",$SkipWrite);
471       $smarty->assign("shadowmins", sprintf(_("Password can't be changed up to %s days after last change"), 
472                                               "<input name=\"shadowMin\" size=3 maxlength=4 value=\"".$this->shadowMin."\">"));
474       $shadowMaxACL     =  $this->getacl("shadowMax",$SkipWrite);
475       $smarty->assign("shadowmaxs", sprintf(_("Password must be changed after %s days"), 
476                                               "<input name=\"shadowMax\" size=3 maxlength=4 value=\"".$this->shadowMax."\">"));
478       $shadowInactiveACL=  $this->getacl("shadowInactive",$SkipWrite);
479       $smarty->assign("shadowinactives", sprintf(_("Disable account after %s days of inactivity after password expiry"), 
480                                               "<input name=\"shadowInactive\" size=3 maxlength=4 value=\"".$this->shadowInactive."\">"));
482       $shadowWarningACL =  $this->getacl("shadowWarning",$SkipWrite);
483       $smarty->assign("shadowwarnings", sprintf(_("Warn user %s days before password expiry"), 
484                                               "<input name=\"shadowWarning\" size=3 maxlength=4 value=\"".$this->shadowWarning."\">"));
486       foreach( array("activate_shadowMin", "activate_shadowMax",
487                      "activate_shadowExpire", "activate_shadowInactive","activate_shadowWarning") as $val){
488         if ($this->$val == 1){
489           $smarty->assign("$val", "checked");
490         } else {
491           $smarty->assign("$val", "");
492         }
493         $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
494       }
496       $smarty->assign("mustchangepasswordACL", $this->getacl("mustchangepassword",$SkipWrite));
497     }
499     /* Fill calendar */
500     /* If this $this->shadowExpire is empty 
501         use current date as base for calculating selectbox values.
502        (This attribute is empty if this is a new user )*/ 
503     if(empty($this->shadowExpire)){
504       $date= getdate(time());
505     }else{
506       $date= getdate($this->shadowExpire);
507     }
508  
509     $days= array();
510     for($d= 1; $d<32; $d++){
511       $days[$d]= $d;
512     }
513     $years= array();
514     for($y= $date['year']-10; $y<$date['year']+10; $y++){
515       $years[]= $y;
516     }
517     $months= msgPool::months();
518     $smarty->assign("day", $date["mday"]);
519     $smarty->assign("days", $days);
520     $smarty->assign("months", $months);
521     $smarty->assign("month", $date["mon"]-1);
522     $smarty->assign("years", $years);
523     $smarty->assign("year", $date["year"]);
525     /* Fill arrays */
526     $smarty->assign("shells", $this->loginShellList);
527     $smarty->assign("secondaryGroups", $this->secondaryGroups);
528     $smarty->assign("primaryGroup", $this->primaryGroup);
529     if(!$this->multiple_support_active){
530       if (!count($this->groupMembership)){
531         $smarty->assign("groupMembership", array("&nbsp;"));
532       } else {
533         $smarty->assign("groupMembership", $this->groupMembership);
534       }
535     }else{
536       $smarty->assign("groupMembership", $this->groupMembership);
537       $smarty->assign("groupMembership_some", $this->groupMembership_some);
538     }
539     if (count($this->groupMembership) > 16){
540       $smarty->assign("groups", "too_many_for_nfs");
541     } else {
542       $smarty->assign("groups", "");
543     }
545     /* Avoid "Undefined index: forceMode" */
546     $smarty->assign("forceMode", "");
548     /* Checkboxes */
549     if ($this->force_ids == 1){
550       $smarty->assign("force_ids", "checked");
551       if (session::get('js')){
552         $smarty->assign("forceMode", "");
553       }
554     } else {
555       if (session::get('js')){
556         $smarty->assign("forceMode", "disabled");
557       }
558       $smarty->assign("force_ids", "");
559     }
561     /* Create onClick="" action string for the "Force UID/GID" option 
562      */
563     $onClickIDS ="";
564     if(preg_match("/w/",$this->getacl("uidNumber",$SkipWrite))){
565       $onClickIDS .= "changeState('uidNumber');";
566     }
567     if(preg_match("/w/",$this->getacl("gidNumber",$SkipWrite))){
568       $onClickIDS .= "changeState('gidNumber');";
569     }
570     $smarty->assign("onClickIDS", $onClickIDS);
571     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
573     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
574       if(in_array($val,$this->multi_boxes)){
575         $smarty->assign("use_".$val,TRUE);
576       }else{
577         $smarty->assign("use_".$val,FALSE);
578       }
579     }
582     /* Load attributes and acl's */
583     foreach($this->attributes as $val){
584       if(in_array($val,$this->multi_boxes)){
585         $smarty->assign("use_".$val,TRUE);
586       }else{
587         $smarty->assign("use_".$val,FALSE);
588       }
590       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
591       {
592         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
593         $smarty->assign("$val", $this->$val);
594         continue;
595       }
596       $smarty->assign("$val", $this->$val);
597       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
598     }
599     if($SkipWrite){
600       $smarty->assign("groupMembershipACL","r");
601     }else{
602       $smarty->assign("groupMembershipACL","rw");
603     }
604     $smarty->assign("status", $this->status);
606     /* Work on trust modes */
607     $smarty->assign("trusthide", " disabled ");
608     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
609     if ($this->trustModel == "fullaccess"){
610       $trustmode= 1;
611       // pervent double disable tag in html code, this will disturb our clean w3c html
612       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
614     } elseif ($this->trustModel == "byhost"){
615       $trustmode= 2;
616       $smarty->assign("trusthide", "");
617     } else {
618       // pervent double disable tag in html code, this will disturb our clean w3c html
619       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
620       $trustmode= 0;
621     }
622     $smarty->assign("trustmode", $trustmode);
623     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
624           2 => _("allow access to these hosts")));
628     if((count($this->accessTo))==0)
629       $smarty->assign("emptyArrAccess",true);
630     else
631       $smarty->assign("emptyArrAccess",false);
633       if($this->mustchangepassword){
634         $smarty->assign("mustchangepassword", " checked ");
635       } else {
636         $smarty->assign("mustchangepassword", "");
637       }
639     $smarty->assign("workstations", $this->accessTo);
641     $smarty->assign("apply", apply_filter());
642     $smarty->assign("multiple_support" , $this->multiple_support_active);
643     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
644     return($display);
645   }
648   /* remove object from parent */
649   function remove_from_parent()
650   {
651     /* Cancel if there's nothing to do here */
652     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
653       return;
654     }
657     /* Remove and write to LDAP */
658     plugin::remove_from_parent();
660     /* Zero out array */
661     $this->attrs['gosaHostACL']= array();
663     /* Keep uid, because we need it for authentification! */
664     unset($this->attrs['uid']);
665     unset($this->attrs['trustModel']);
667     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
668     /* include global link_info */
669      $this->attributes, "Save");
670     $ldap= $this->config->get_ldap_link();
671     $ldap->cd($this->dn);
672     $this->cleanup();
673     $ldap->modify ($this->attrs); 
675     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
677     if (!$ldap->success()){
678       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
679     }
681     /* Delete group only if cn is uid and there are no other
682        members inside */
683     $ldap->cd ($this->config->current['BASE']);
684     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
685     if ($ldap->count() != 0){
686       $attrs= $ldap->fetch();
687       if ($attrs['cn'][0] == $this->uid &&
688           !isset($this->attrs['memberUid'])){
690         $ldap->rmDir($ldap->getDN());
691       }
692     }
694     /* Optionally execute a command after we're done */
695     $this->handle_post_events("remove",array("uid" => $this->uid));
696   }
699   function save_object()
700   {
701     if (isset($_POST['posixTab'])){
702       /* Save values to object */
703       plugin::save_object();
706       /* Save force GID checkbox */
707       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
708         if (isset ($_POST['force_ids'])){
709           $data= 1;
710         } else {
711           $data= 0;
712         }
713         if ($this->force_ids != $data){
714           $this->is_modified= TRUE;
715         }
716         $this->force_ids= $data;
717       }
719       /*Save primary group settings */
720       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
721         $data= $_POST['primaryGroup'];
722         if ($this->primaryGroup != $data){
723           $this->is_modified= TRUE;
724         }
725         $this->primaryGroup= $_POST['primaryGroup'];
726       }
728       /* Get seelcted shadow checkboxes */
729       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
730         if($this->acl_is_writeable($var)){
731           $activate_var = "activate_".$var;
732           if(isset($_POST['activate_'.$var])){
733             $this->$activate_var  = true;
734             $this->$var      = $_POST[$var];
735           }else{
736             $this->$activate_var  = false;
737             $this->$var      = 0;
738           }
739         }
740       }
742       /* Force change password ? */
743       if(isset($_POST['mustchangepassword'])){
744         $this->mustchangepassword = TRUE;
745       }else{
746         $this->mustchangepassword = FALSE;
747       }
749       /* Trust mode - special handling */
750       if($this->acl_is_writeable("trustModel")){
751         if (isset($_POST['trustmode'])){
752           $saved= $this->trustModel;
753           if ($_POST['trustmode'] == "1"){
754             $this->trustModel= "fullaccess";
755           } elseif ($_POST['trustmode'] == "2"){
756             $this->trustModel= "byhost";
757           } else {
758             $this->trustModel= "";
759           }
760           if ($this->trustModel != $saved){
761             $this->is_modified= TRUE;
762           }
763         }
764       }
765     }
767     /* Get regex from alphabet */
768     if(isset($_GET['search'])){
769       $this->GroupRegex = $_GET['search']."*";
770     }
772     /* Check checkboxes and regexes */
773     if(isset($_POST["PosixGroupDialogPosted"])){
775       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
776         $this->SubSearch = true;
777       }else{
778         $this->SubSearch = false;
779       }
780       if(isset($_POST['guser'])){
781         $this->GroupUserRegex = $_POST['guser'];
782       }
783       if(isset($_POST['regex'])){
784         $this->GroupRegex = $_POST['regex'];
785       }
786     }
787     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
788     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
789   }
792   /* Save data to LDAP, depending on is_account we save or delete */
793   function save()
794   {
796     /* Adapt shadow values */
797     if (!$this->activate_shadowExpire){
798       $this->shadowExpire= "0";
799     } else {
800       /* Transform seconds to days here */
801       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
802     }
803     if (!$this->activate_shadowMax){
804       $this->shadowMax= "0";
805     }
806     if ($this->mustchangepassword){
807       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
808     } else {
809       $this->shadowLastChange= (int)(date("U") / 86400);
810     }
811     if (!$this->activate_shadowWarning){
812       $this->shadowWarning= "0";
813     }
815     /* Check what to do with ID's */
816     if ($this->force_ids == 0){
818       /* Use id's that are already set */
819       if ($this->savedUidNumber != ""){
820         $this->uidNumber= $this->savedUidNumber;
821         $this->gidNumber= $this->savedGidNumber;
822       } else {
824         /* Calculate new id's. We need to place a lock before calling get_next_id
825            to get real unique values. */
826         $wait= 10;
827         while (get_lock("uidnumber") != ""){
828           sleep (1);
830           /* Oups - timed out */
831           if ($wait-- == 0){
832             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
833             break;
834           }
835         }
837         add_lock ("uidnumber", "gosa");
838         $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
839         if ($this->savedGidNumber != ""){
840           $this->gidNumber= $this->savedGidNumber;
841         } else {
842           $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
843         }
844       }
846       if ($this->primaryGroup != 0){
847         $this->gidNumber= $this->primaryGroup;
848       }
849     }
851     if ($this->activate_shadowMin != "1" ) {
852       $this->shadowMin = "";
853     }
855     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
856       $this->shadowMax = "";
857     }
859     if ($this->activate_shadowWarning != "1" ) {
860       $this->shadowWarning = "";
861     }
863     if ($this->activate_shadowInactive != "1" ) {
864       $this->shadowInactive = "";
865     }
867     if ($this->activate_shadowExpire != "1" ) {
868       $this->shadowExpire = "";
869     }
871     /* Fill gecos */
872     if (isset($this->parent) && $this->parent !== NULL){
873       $this->gecos= rewrite($this->parent->by_object['user']->cn);
874       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
875         $this->gecos= "";
876       }
877     }
879     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
880       $this->$attr = (int) $this->$attr;
881     }
882     /* Call parents save to prepare $this->attrs */
883     plugin::save();
885     /* Trust accounts */
886     $objectclasses= array();
887     foreach ($this->attrs['objectClass'] as $key => $class){
888       if (preg_match('/trustAccount/i', $class)){
889         continue;
890       }
891       $objectclasses[]= $this->attrs['objectClass'][$key];
892     }
893     $this->attrs['objectClass']= $objectclasses;
894     if ($this->trustModel != ""){
895       $this->attrs['objectClass'][]= "trustAccount";
896       $this->attrs['trustModel']= $this->trustModel;
897       $this->attrs['accessTo']= array();
898       if ($this->trustModel == "byhost"){
899         foreach ($this->accessTo as $host){
900           $this->attrs['accessTo'][]= $host;
901         }
902       }
903     } else {
904       if ($this->was_trust_account){
905         $this->attrs['accessTo']= array();
906         $this->attrs['trustModel']= array();
907       }
908     }
910     if(empty($this->attrs['gosaDefaultPrinter'])){
911       $thid->attrs['gosaDefaultPrinter']=array();
912     }
915     /* include global link_info */
916     $this->cleanup();
917  
918     /* This is just a test, we have had duplicated ids 
919         in the past when copy & paste was used. 
920        Normaly this should not happen.
921      */ 
922     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
923       $used = $this->get_used_uid_numbers();
924       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
925         msg_dialog::display(_("Warning"),_("A duplicated UID number was written for this user. If this was not intended please verify all used uidNumbers!"), WARNING_DIALOG);
926       }
927     }
929     $ldap= $this->config->get_ldap_link();
930     $ldap->cd($this->dn);
931     unset($this->attrs['uid']);
932     $ldap->modify ($this->attrs); 
934     /* Log last action */ 
935     if($this->initially_was_account){
936       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
937     }else{
938       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
939     }
941     if (!$ldap->success()){
942       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
943     }
945     /* Remove lock needed for unique id generation */
946     del_lock ("uidnumber");
948     /* Posix accounts have group interrelationship, 
949        take care about these here if this is a new user without forced gidNumber. */
950     if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
951       $ldap->cd($this->config->current['BASE']);
952       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
954       /* Create group if it doesn't exist */
955       if ($ldap->count() == 0){
956         $groupdn= preg_replace ('/^'.$this->config->get_cfg_value("accountPrimaryAttribute").'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
958         $g= new group($this->config, $groupdn);
959         $g->cn= $this->uid;
960         $g->force_gid= 1;
961         $g->gidNumber= $this->gidNumber;
962         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
963         $g->save ();
964       }
965     }
967     /* Take care about groupMembership values: add to groups */
968     foreach ($this->groupMembership as $key => $value){
969       if (!isset($this->savedGroupMembership[$key])){
970         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
971         $g->set_acl_base($key);
972         $g->by_object['group']->addUser($this->uid);
973         $g->save();
974       }
975     }
977     /* Remove from groups not listed in groupMembership */
978     foreach ($this->savedGroupMembership as $key => $value){
979       if (!isset($this->groupMembership[$key])){
980         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
981         $g->set_acl_base($key);
982         $g->by_object['group']->removeUser ($this->uid);
983         $g->save();
984       }
985     }
987     /* Optionally execute a command after we're done */
988     if ($this->initially_was_account == $this->is_account){
989       if ($this->is_modified){
990         $this->handle_post_events("modify",array("uid" => $this->uid));
991       }
992     } else {
993       $this->handle_post_events("add" ,array("uid"=> $this->uid));
994     }
995   }
997   /* Check formular input */
998   function check()
999   {
1000     /* Include global link_info */
1001     $ldap= $this->config->get_ldap_link();
1003     /* Append groups as memberGroup: to check hook 
1004      */
1005     $tmp_attributes  = $this->attributes;    
1006     $this->attributes[] = "memberGroup";
1007     $this->memberGroup = array();
1008     foreach($this->groupMembership as $dn => $name){
1009       $this->memberGroup[] = $name;
1010     }
1012     /* Call common method to give check the hook */
1013     $message= plugin::check();
1014     $this->attributes = $tmp_attributes;
1016     /* must: homeDirectory */
1017     if ($this->homeDirectory == ""){
1018       $message[]= msgPool::required(_("Home directory"));
1019     }
1020     if (!tests::is_path($this->homeDirectory)){
1021       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1022     }
1024     /* Check ID's if they are forced by user */
1025     if ($this->force_ids == "1"){
1027       /* Valid uid/gid? */
1028       if (!tests::is_id($this->uidNumber)){
1029         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1030       } else {
1031         if ($this->uidNumber < $this->config->get_cfg_value("minId")){
1032           $message[]= msgPool::toosmall(_("UID"), $this->config->get_cfg_value("minId"));
1033         }
1034       }
1035       if (!tests::is_id($this->gidNumber)){
1036         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1037       } else {
1038         if ($this->gidNumber < $this->config->get_cfg_value("minId")){
1039           $message[]= msgPool::toosmall(_("GID"), $this->config->get_cfg_value("minId"));
1040         }
1041       }
1042     }
1044     /* Check shadow settings, well I like spaghetties... */
1045     if ($this->activate_shadowMin){
1046       if (!tests::is_id($this->shadowMin)){
1047         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1048       }
1049     }
1050     if ($this->activate_shadowMax){
1051       if (!tests::is_id($this->shadowMax)){
1052         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1053       }
1054     }
1055     if ($this->activate_shadowWarning){
1056       if (!tests::is_id($this->shadowWarning)){
1057         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1058       }
1059       if (!$this->activate_shadowMax){
1060         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1061       }
1062       if ($this->shadowWarning > $this->shadowMax){
1063         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1064       }
1065       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1066         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1067       }
1068     }
1069     if ($this->activate_shadowInactive){
1070       if (!tests::is_id($this->shadowInactive)){
1071         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1072       }
1073       if (!$this->activate_shadowMax){
1074         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1075       }
1076     }
1077     if ($this->activate_shadowMin && $this->activate_shadowMax){
1078       if ($this->shadowMin > $this->shadowMax){
1079         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1080       }
1081     }
1083     return ($message);
1084   }
1087   function multiple_check()
1088   {
1089     $message = plugin::multiple_check();
1090     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1091       $message[]= msgPool::required(_("Home directory"));
1092     }
1093     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1094       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1095     }
1097     /* Check shadow settings, well I like spaghetties... */
1098     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1099       if (!tests::is_id($this->shadowMin)){
1100         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1101       }
1102     }
1103     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1104       if (!tests::is_id($this->shadowMax)){
1105         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1106       }
1107     }
1108     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1109       if (!tests::is_id($this->shadowWarning)){
1110         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1111       }
1112       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1113         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1114       }
1115       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1116         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1117       }
1118       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1119         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1120       }
1121     }
1122     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1123       if (!tests::is_id($this->shadowInactive)){
1124         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1125       }
1126       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1127         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1128       }
1129     }
1130     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1131       if ($this->shadowMin > $this->shadowMax){
1132         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1133       }
1134     }
1136     return($message);
1137   }
1140   function addGroup ($groups)
1141   {
1142     /* include global link_info */
1143     $ldap= $this->config->get_ldap_link();
1145     /* Walk through groups and add the descriptive entry if not exists */
1146     foreach ($groups as $value){
1148       if (!array_key_exists($value, $this->groupMembership)){
1149         $ldap->cat($value, array('cn', 'description', 'dn'));
1150         $attrs= $ldap->fetch();
1151         error_reporting (0);
1152         if (!isset($attrs['description'][0])){
1153           $entry= $attrs["cn"][0];
1154         } else {
1155           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1156           $entry= $attrs["cn"][0]." [$dsc]";
1157         }
1158         error_reporting (E_ALL | E_STRICT);
1160         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1161           $this->groupMembership[$attrs['dn']]= $entry;
1162           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1163             unset($this->groupMembership_some[$attrs['dn']]);
1164           }
1165         }
1166       }
1167     }
1169     /* Sort groups */
1170     asort ($this->groupMembership);
1171     reset ($this->groupMembership);
1172   }
1175   /* Del posix user from some groups */
1176   function delGroup ($groups)
1177   {
1178     $dest= array();
1179     foreach($groups as $dn_to_del){
1180       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1181         unset($this->groupMembership[$dn_to_del]);
1182       }
1183       if($this->multiple_support_active){
1184         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1185           unset($this->groupMembership_some[$dn_to_del]);
1186         }
1187       }
1188     }
1189   }
1192   /* Adapt from template, using 'dn' */
1193   function adapt_from_template($dn, $skip= array())
1194   {
1195     /* Include global link_info */
1196     $ldap= $this->config->get_ldap_link();
1198     plugin::adapt_from_template($dn, $skip);
1199     $template= $this->attrs['uid'][0];
1201     /* Adapt group membership */
1202     $ldap->cd($this->config->current['BASE']);
1203     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1205     while ($this->attrs= $ldap->fetch()){
1206       if (!isset($this->attrs["description"][0])){
1207         $entry= $this->attrs["cn"][0];
1208       } else {
1209         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1210       }
1211       $this->groupMembership[$ldap->getDN()]= $entry;
1212     }
1214     /* Fix primary group settings */
1215     $ldap->cd($this->config->current['BASE']);
1216     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1217     if ($ldap->count() != 1){
1218       $this->primaryGroup= $this->gidNumber;
1219     }
1221     $ldap->cd($this->config->current['BASE']);
1222     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1223     while($attr = $ldap->fetch()){
1224       $tmp = $attr['accessTo'];
1225       unset ($tmp['count']);
1226       $this->accessTo = $tmp;   
1227     }
1229     /* Adjust shadow checkboxes */
1230     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1231       if ($this->$val != 0){
1232         $oval= "activate_".$val;
1233         $this->$oval= "1";
1234       }
1235     }
1237     /* FIXME: NEED review of this section */
1238     /* Need to check shadowExpire separately */
1240     /* 
1241      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1242      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1243      */
1244     if(abs($this->shadowExpire - time())>86400) {
1245       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1246     }
1247     
1248     /* Only enable checkbox, if shadowExpire is in the future */
1249     if($this->shadowExpire > time()) {
1250       $this->activate_shadowExpire= "1";
1251     }
1252   }
1254   function convertToSeconds($val)
1255   {
1256     if ($val != 0){
1257       $val*= 60 * 60 * 24;
1258     } else {
1259       $date= getdate();
1260       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1261     }
1262     return($val);
1263   }
1266   function get_used_uid_numbers()
1267   {
1268     $ids= array();
1269     $ldap= $this->config->get_ldap_link();
1271     $ldap->cd ($this->config->current['BASE']);
1272     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1274     /* Get list of ids */
1275     while ($attrs= $ldap->fetch()){
1276       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1277     }
1278     return($ids);
1279   }
1281   
1282   function get_next_id($attrib, $dn)
1283   {
1284     $ids= array();
1285     $ldap= $this->config->get_ldap_link();
1287     $ldap->cd ($this->config->current['BASE']);
1288     if (preg_match('/gidNumber/i', $attrib)){
1289       $oc= "posixGroup";
1290     } else {
1291       $oc= "posixAccount";
1292     }
1293     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1295     /* Get list of ids */
1296     while ($attrs= $ldap->fetch()){
1297       $ids[]= (int)$attrs["$attrib"][0];
1298     }
1300     /* Add the nobody id */
1301     $ids[]= 65534;
1303     /* get the ranges */
1304     $tmp = array('0'=> 1000); 
1305     if (preg_match('/posixAccount/', $oc) && $this->config->get_cfg_value("uidNumberBase") != ""){
1306       $tmp= split('-',$this->config->get_cfg_value("uidNumberBase"));
1307     } elseif($this->config->get_cfg_value("gidNumberBase") != ""){
1308       $tmp= split('-',$this->config->get_cfg_value("gidNumberBase"));
1309     }
1311     /* Set hwm to max if not set - for backward compatibility */
1312     $lwm= $tmp[0];
1313     if (isset($tmp[1])){
1314       $hwm= $tmp[1];
1315     } else {
1316       $hwm= pow(2,32);
1317     }
1319     /* Find out next free id near to UID_BASE */
1320     if ($this->config->get_cfg_value("baseIdHook") == ""){
1321       $base= $lwm;
1322     } else {
1323       /* Call base hook */
1324       $base= get_base_from_hook($dn, $attrib);
1325     }
1326     for ($id= $base; $id++; $id < pow(2,32)){
1327       if (!in_array($id, $ids)){
1328         return ($id);
1329       }
1330     }
1332     /* Should not happen */
1333     if ($id == $hwm){
1334       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1335       exit;
1336     }
1338   }
1340   function reload()
1341   {
1342     /* Set base for all searches */
1343     $base      = session::get('CurrentMainBase');
1344     $base     = $base;
1345     $ldap     = $this->config->get_ldap_link();    
1346     $attrs    =  array("cn", "description", "gidNumber");
1347     $Flags    = GL_SIZELIMIT;
1349     /* Get groups */
1350     if ($this->GroupUserRegex == '*'){
1351       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1352     } else {
1353       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1354     }
1355     if($this->SubSearch){
1356       $Flags |= GL_SUBSEARCH;
1357     }else{
1358       $base = get_groups_ou().$base;
1359     }
1361     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1362   
1363     /* check sizelimit */
1364     if (preg_match("/size limit/i", $ldap->get_error())){
1365       session::set('limit_exceeded',TRUE);
1366     }
1368     /* Create a list of users */
1369     $this->grouplist = array();
1370     foreach ($res as $value){
1371       $this->grouplist[$value['gidNumber'][0]]= $value;
1372     }
1374     $tmp=array();
1375     foreach($this->grouplist as $tkey => $val ){
1376       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1377     }
1379     /* Sort index */
1380     ksort($tmp);
1382     /* Recreate index array[dn]=cn[description]*/
1383     $this->grouplist=array();
1384     foreach($tmp as $val){
1385       if(isset($val['description'])){
1386         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1387       }else{
1388         $this->grouplist[$val['dn']]=$val['cn'][0];
1389       }
1390     }
1392     reset ($this->grouplist);
1393   }
1396   /* Get posts from copy & paste dialog */ 
1397   function saveCopyDialog()
1398   {
1399     if(isset($_POST['homeDirectory'])){
1400       $this->homeDirectory = $_POST['homeDirectory'];
1401       if (isset ($_POST['force_ids'])){
1402         $data= 1;
1403         $this->gidNumber = $_POST['gidNumber'];
1404         $this->uidNumber = $_POST['uidNumber'];
1405       } else {
1406         $data= 0;
1407       }
1408       if ($this->force_ids != $data){
1409         $this->is_modified= TRUE;
1410       }
1411       $this->force_ids= $data;
1412     }
1413   }
1414  
1416   /* Create the posix dialog part for copy & paste */
1417   function getCopyDialog()
1418   {
1419     /* Skip dialog creation if this is not a valid account*/
1420     if(!$this->is_account) return("");
1421     if ($this->force_ids == 1){
1422       $force_ids = "checked";
1423       if (session::get('js')){
1424         $forceMode = "";
1425       }
1426     } else {
1427       if (session::get('js')){
1428         if($this->acl != "#none#")
1429           $forceMode ="disabled";
1430       }
1431       $force_ids = "";
1432     }
1434     $sta = "";
1436     /* Open group add dialog */
1437     if(isset($_POST['edit_groupmembership'])){
1438       $this->group_dialog = TRUE;
1439       $sta = "SubDialog";
1440     }
1442     /* If the group-add dialog is closed, call execute 
1443        to ensure that the membership is updatd */
1444     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1445       $this->execute();
1446       $this->group_dialog =FALSE;
1447     }
1449     if($this->group_dialog){
1450       $str = $this->execute(true);
1451       $ret = array();
1452       $ret['string'] = $str;
1453       $ret['status'] = $sta;
1454       return($ret);
1455     }
1457     /* If a group member should be deleted, simply call execute */
1458     if(isset($_POST['delete_groupmembership'])){
1459       $this->execute();
1460     }
1462     /* Assigned informations to smarty */
1463     $smarty = get_smarty();
1464     $smarty->assign("homeDirectory",$this->homeDirectory);
1465     $smarty->assign("uidNumber",$this->uidNumber);
1466     $smarty->assign("gidNumber",$this->gidNumber);
1467     $smarty->assign("forceMode",$forceMode);
1468     $smarty->assign("force_ids",$force_ids);
1469     if (!count($this->groupMembership)){
1470       $smarty->assign("groupMembership", array("&nbsp;"));
1471     } else {
1472       $smarty->assign("groupMembership", $this->groupMembership);
1473     }
1475     /* Display wars message if there are more than 16 group members */
1476     if (count($this->groupMembership) > 16){
1477       $smarty->assign("groups", "too_many_for_nfs");
1478     } else {
1479       $smarty->assign("groups", "");
1480     }
1481     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1483     $ret = array();
1484     $ret['string'] = $str;
1485     $ret['status'] = $sta;
1486     return($ret);
1487   }
1490   function PrepareForCopyPaste($source)
1491   {
1492     plugin::PrepareForCopyPaste($source);
1494     /* Avoid using the same gid/uid number as source user 
1495         empty numbers to enforce new ones. */
1496 #    $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1497     $this->savedUidNumber = "";
1498 #    $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1499     $this->savedGidNumber = "";
1500   }
1503   function multiple_execute()
1504   {
1505     return($this->execute());
1506   }
1509   static function plInfo()
1510   {
1511     return (array(
1512           "plDescription"     => _("POSIX account"),
1513           "plSelfModify"      => TRUE,
1514           "plDepends"         => array("user"),
1515           "plPriority"        => 2,
1516           "plSection"         => array("personal" => _("My account")),
1517           "plCategory"        => array("users"),
1518           "plOptions"         => array(),
1520           "plProvidedAcls"  => array(
1522             "homeDirectory"       =>  _("Home directory"), 
1523             "loginShell"          =>  _("Shell"),
1524             "uidNumber"           =>  _("User ID"),
1525             "gidNumber"           =>  _("Group ID"),
1527             "mustchangepassword"=>  _("Force password change on login"),
1528             "shadowMin"           =>  _("Shadow min"),
1529             "shadowMax"           =>  _("Shadow max"),
1530             "shadowWarning"       =>  _("Shadow warning"),
1531             "shadowInactive"      =>  _("Shadow inactive"),
1532             "shadowExpire"        =>  _("Shadow expire"),
1533             "trustModel"          =>  _("System trust model")))
1534             );
1535   }
1538   /* Return selected values for multiple edit */
1539   function get_multi_edit_values()
1540   {
1541     $ret = plugin::get_multi_edit_values();
1542     $ret['groupMembership']     = $this->groupMembership;
1543     $ret['groupMembership_some']= $this->groupMembership_some;
1545     if(in_array("primaryGroup",$this->multi_boxes)){
1546       $ret['primaryGroup'] = $this->primaryGroup;
1547     }
1548     if(in_array("trustmode",$this->multi_boxes)){
1549       $ret['trustModel'] = $this->trustModel;
1550       $ret['accessTo'] = $this->accessTo;
1551     }
1552     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1553       $active = "activate_".$entry;
1554       if(in_array($active,$this->multi_boxes)){
1555         $ret[$entry] = $this->$entry;
1556         $ret[$active] = $this->$active;
1557       }
1558     }
1559     if(in_array("mustchangepassword",$this->multi_boxes)){
1560       $ret['mustchangepassword'] = $this->mustchangepassword;
1561     }
1562     return($ret);
1563   }
1566   /* Save posts for multiple edit 
1567    */
1568   function multiple_save_object()
1569   {
1570     if(isset($_POST['posix_mulitple_edit'])){
1571  
1572       /* Backup expire value */ 
1573       $expire_tmp = $this->shadowExpire;
1574   
1575       /* Update all values */
1576       plugin::multiple_save_object();
1578       /* Get selected checkboxes */
1579       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1580         if(isset($_POST["use_".$val])){
1581           $this->multi_boxes[] = $val;
1582         }
1583       }
1585       /* Update special values, checkboxes for posixShadow */
1586       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1587         if($this->acl_is_writeable($var)){
1588           $activate_var = "activate_".$var;
1589           if(in_array($activate_var, $this->multi_boxes)){
1590             if(isset($_POST['activate_'.$var])){
1591               $this->$activate_var  = true;
1592               $this->$var      = $_POST[$var];
1593             }else{
1594               $this->$activate_var  = false;
1595               $this->$var      = 0;
1596             }
1597           }
1598         }
1599       }
1601       /* Restore shadow value, if the shadow attribute isn't used */
1602       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1603         $this->shadowExpire = $expire_tmp;
1604       }
1606       /* Force change password ? */
1607       if(isset($_POST['mustchangepassword'])){
1608         $this->mustchangepassword = TRUE;
1609       }else{
1610         $this->mustchangepassword = FALSE;
1611       }
1613       /* Trust mode - special handling */
1614       if($this->acl_is_writeable("trustModel")){
1615         if (isset($_POST['trustmode'])){
1616           $saved= $this->trustModel;
1617           if ($_POST['trustmode'] == "1"){
1618             $this->trustModel= "fullaccess";
1619           } elseif ($_POST['trustmode'] == "2"){
1620             $this->trustModel= "byhost";
1621           } else {
1622             $this->trustModel= "";
1623           }
1624           if ($this->trustModel != $saved){
1625             $this->is_modified= TRUE;
1626           }
1627         }
1628       }
1630       /* Save primary group settings */
1631       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1632         $data= $_POST['primaryGroup'];
1633         if ($this->primaryGroup != $data){
1634           $this->is_modified= TRUE;
1635         }
1636         $this->primaryGroup= $_POST['primaryGroup'];
1637       }
1638     }
1639   }
1641   
1642   /* Initialize plugin with given atribute arrays 
1643    */
1644   function init_multiple_support($attrs,$all)
1645   {
1646     plugin::init_multiple_support($attrs,$all);
1648     /* Some dummy values */
1649     $groups_some = array();
1650     $groups_all  = array();
1651     $groups_uid  = array();
1652     $uids        = array();
1653     $first       = TRUE;
1655     /* Get all groups used by currently edited users */
1656     $uid_filter="";  
1657     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1658       $uid = $this->multi_attrs_all['uid'][$i];
1659       $uids[] = $uid;
1660       $uid_filter.= "(memberUid=".$uid.")"; 
1661     }
1662     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1663     $ldap = $this->config->get_ldap_link();
1664     $ldap->cd($this->config->current['BASE']);
1665     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1666     while($group = $ldap->fetch()){
1667       $groups_some[$group['dn']] = $group['cn'][0];
1668       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1669         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1670       }
1671     }
1673     /* Create an array, containing all used groups */
1674     $groups_all = $groups_some;
1675     foreach($groups_all as $id => $group){
1676       foreach($uids as $uid){
1677         if(!in_array($uid,$groups_uid[$id])){
1678           unset($groups_all[$id]);
1679           break;
1680         }
1681       }
1682     }
1684     /* Assign group array */
1685     $this->groupMembership = $groups_all;
1687     /* Create an array of all grouops used by all users */
1688     foreach( $groups_all as $dn => $cn){
1689       if(isset($groups_some[$dn])){
1690         unset($groups_some[$dn]);
1691       }
1692     }
1693     $this->groupMembership_some = $groups_some;
1694     $this->primaryGroup = $this->gidNumber;
1696     /* Is this account a trustAccount? */
1697     if (isset($this->multi_attrs['trustModel'])){
1698       $this->trustModel= $this->multi_attrs['trustModel'][0];
1699       $this->was_trust_account= TRUE;
1700       $this->multi_boxes[] = "trustmode";
1701     } else {
1702       $this->was_trust_account= FALSE;
1703       $this->trustModel= "";
1704     }
1706     /* Create access informations */
1707     $this->accessTo = array();
1708     if (isset($this->multi_attrs['accessTo'])){
1709       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1710         $tmp= $this->multi_attrs['accessTo'][$i];
1711         $this->accessTo[$tmp]= $tmp;
1712       }
1713     }
1715     /* Adjust shadow checkboxes */
1716     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1717           "shadowExpire") as $val){
1718       if ($this->$val != 0){
1719         $oval= "activate_".$val;
1720         $this->$oval= "1";
1721       }
1722     }
1724     /* Convert to seconds */
1725     if(isset($this->multi_attrs['shadowExpire'])){
1726       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1727     }else{
1728       $this->activate_shadowExpire = FALSE;
1729     }
1730   }
1733   function set_multi_edit_values($attrs)
1734   {
1735     $groups = array();
1737     /* Update groupMembership, keep optinal group */
1738     foreach($attrs['groupMembership_some'] as $dn => $cn){
1739       if(isset($this->groupMembership[$dn])){
1740         $groups[$dn] = $cn;
1741       }
1742     }
1743     /* Update groupMembership, add forced groups */
1744     foreach($attrs['groupMembership'] as $dn => $cn){
1745       $groups[$dn] = $cn;
1746     }
1747     plugin::set_multi_edit_values($attrs);
1748     $this->groupMembership = $groups;
1749   }
1752 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1753 ?>