Code

Fixed permission problem in addressbook plugin.
[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("serverou"),
400                       get_ou("terminalou"),
401                       get_ou("workstationou")); 
403       $res= get_sub_list($filter, $acls, $deps_a, get_ou("systemsou").$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     }
560     
562     $smarty->assign("force_idsACL", $this->getacl("uidNumber",$SkipWrite).$this->getacl("gidNumber",$SkipWrite));
564     foreach(array("primaryGroup","trustmode","activate_shadowWarning","activate_shadowInactive","activate_shadowMin","activate_shadowMax","activate_shadowExpire","mustchangepassword") as $val){
565       if(in_array($val,$this->multi_boxes)){
566         $smarty->assign("use_".$val,TRUE);
567       }else{
568         $smarty->assign("use_".$val,FALSE);
569       }
570     }
573     /* Load attributes and acl's */
574     foreach($this->attributes as $val){
575       if(in_array($val,$this->multi_boxes)){
576         $smarty->assign("use_".$val,TRUE);
577       }else{
578         $smarty->assign("use_".$val,FALSE);
579       }
581       if((session::get("js"))&&(($val=="uidNumber")||($val=="gidNumber")))
582       {
583         $smarty->assign("$val"."ACL",$this->getacl($val,$SkipWrite));
584         $smarty->assign("$val", $this->$val);
585         continue;
586       }
587       $smarty->assign("$val", $this->$val);
588       $smarty->assign("$val"."ACL", $this->getacl($val,$SkipWrite));
589     }
590     if($SkipWrite){
591       $smarty->assign("groupMembershipACL","r");
592     }else{
593       $smarty->assign("groupMembershipACL","rw");
594     }
595     $smarty->assign("status", $this->status);
597     /* Work on trust modes */
598     $smarty->assign("trusthide", " disabled ");
599     $smarty->assign("trustmodeACL",  $this->getacl("trustModel",$SkipWrite));
600     if ($this->trustModel == "fullaccess"){
601       $trustmode= 1;
602       // pervent double disable tag in html code, this will disturb our clean w3c html
603       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
605     } elseif ($this->trustModel == "byhost"){
606       $trustmode= 2;
607       $smarty->assign("trusthide", "");
608     } else {
609       // pervent double disable tag in html code, this will disturb our clean w3c html
610       $smarty->assign("trustmode",  $this->getacl("trustModel",$SkipWrite));
611       $trustmode= 0;
612     }
613     $smarty->assign("trustmode", $trustmode);
614     $smarty->assign("trustmodes", array( 0 => _("disabled"), 1 => _("full access"),
615           2 => _("allow access to these hosts")));
619     if((count($this->accessTo))==0)
620       $smarty->assign("emptyArrAccess",true);
621     else
622       $smarty->assign("emptyArrAccess",false);
624       if($this->mustchangepassword){
625         $smarty->assign("mustchangepassword", " checked ");
626       } else {
627         $smarty->assign("mustchangepassword", "");
628       }
630     $smarty->assign("workstations", $this->accessTo);
632     $smarty->assign("apply", apply_filter());
633     $smarty->assign("multiple_support" , $this->multiple_support_active);
634     $display.= $smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__)));
635     return($display);
636   }
639   /* remove object from parent */
640   function remove_from_parent()
641   {
642     /* Cancel if there's nothing to do here */
643     if ((!$this->initially_was_account) || (!$this->acl_is_removeable())){
644       return;
645     }
648     /* Remove and write to LDAP */
649     plugin::remove_from_parent();
651     /* Zero out array */
652     $this->attrs['gosaHostACL']= array();
654     /* Keep uid, because we need it for authentification! */
655     unset($this->attrs['uid']);
656     unset($this->attrs['trustModel']);
658     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
659     /* include global link_info */
660      $this->attributes, "Save");
661     $ldap= $this->config->get_ldap_link();
662     $ldap->cd($this->dn);
663     $this->cleanup();
664     $ldap->modify ($this->attrs); 
666     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
668     if (!$ldap->success()){
669       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
670     }
672     /* Delete group only if cn is uid and there are no other
673        members inside */
674     $ldap->cd ($this->config->current['BASE']);
675     $ldap->search ("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn", "memberUid"));
676     if ($ldap->count() != 0){
677       $attrs= $ldap->fetch();
678       if ($attrs['cn'][0] == $this->uid &&
679           !isset($this->attrs['memberUid'])){
681         $ldap->rmDir($ldap->getDN());
682       }
683     }
685     /* Optionally execute a command after we're done */
686     $this->handle_post_events("remove",array("uid" => $this->uid));
687   }
690   function save_object()
691   {
692     if (isset($_POST['posixTab'])){
693       /* Save values to object */
694       plugin::save_object();
697       /* Save force GID checkbox */
698       if($this->acl_is_writeable("gidNumber") || $this->acl_is_writeable("uidNumber")){
699         if (isset ($_POST['force_ids'])){
700           $data= 1;
701         } else {
702           $data= 0;
703         }
704         if ($this->force_ids != $data){
705           $this->is_modified= TRUE;
706         }
707         $this->force_ids= $data;
708       }
710       /*Save primary group settings */
711       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
712         $data= $_POST['primaryGroup'];
713         if ($this->primaryGroup != $data){
714           $this->is_modified= TRUE;
715         }
716         $this->primaryGroup= $_POST['primaryGroup'];
717       }
719       /* Get seelcted shadow checkboxes */
720       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
721         if($this->acl_is_writeable($var)){
722           $activate_var = "activate_".$var;
723           if(isset($_POST['activate_'.$var])){
724             $this->$activate_var  = true;
725             $this->$var      = $_POST[$var];
726           }else{
727             $this->$activate_var  = false;
728             $this->$var      = 0;
729           }
730         }
731       }
733       /* Force change password ? */
734       if(isset($_POST['mustchangepassword'])){
735         $this->mustchangepassword = TRUE;
736       }else{
737         $this->mustchangepassword = FALSE;
738       }
740       /* Trust mode - special handling */
741       if($this->acl_is_writeable("trustModel")){
742         if (isset($_POST['trustmode'])){
743           $saved= $this->trustModel;
744           if ($_POST['trustmode'] == "1"){
745             $this->trustModel= "fullaccess";
746           } elseif ($_POST['trustmode'] == "2"){
747             $this->trustModel= "byhost";
748           } else {
749             $this->trustModel= "";
750           }
751           if ($this->trustModel != $saved){
752             $this->is_modified= TRUE;
753           }
754         }
755       }
756     }
758     /* Get regex from alphabet */
759     if(isset($_GET['search'])){
760       $this->GroupRegex = $_GET['search']."*";
761     }
763     /* Check checkboxes and regexes */
764     if(isset($_POST["PosixGroupDialogPosted"])){
766       if(isset($_POST['SubSearch']) && ($_POST['SubSearch'])){
767         $this->SubSearch = true;
768       }else{
769         $this->SubSearch = false;
770       }
771       if(isset($_POST['guser'])){
772         $this->GroupUserRegex = $_POST['guser'];
773       }
774       if(isset($_POST['regex'])){
775         $this->GroupRegex = $_POST['regex'];
776       }
777     }
778     $this->GroupRegex = preg_replace("/\*\**/","*",$this->GroupRegex);
779     $this->GroupUserRegex = preg_replace("/\*\**/","*",$this->GroupUserRegex);
780   }
783   /* Save data to LDAP, depending on is_account we save or delete */
784   function save()
785   {
787     /* Adapt shadow values */
788     if (!$this->activate_shadowExpire){
789       $this->shadowExpire= "0";
790     } else {
791       /* Transform seconds to days here */
792       $this->shadowExpire= (int)($this->shadowExpire / (60 * 60 * 24)) ;
793     }
794     if (!$this->activate_shadowMax){
795       $this->shadowMax= "0";
796     }
797     if ($this->mustchangepassword){
798       $this->shadowLastChange= (int)(date("U") / 86400) - $this->shadowMax - 1;
799     } else {
800       $this->shadowLastChange= (int)(date("U") / 86400);
801     }
802     if (!$this->activate_shadowWarning){
803       $this->shadowWarning= "0";
804     }
806     /* Check what to do with ID's */
807     if ($this->force_ids == 0){
809       /* Use id's that are already set */
810       if ($this->savedUidNumber != ""){
811         $this->uidNumber= $this->savedUidNumber;
812         $this->gidNumber= $this->savedGidNumber;
813       } else {
815         /* Calculate new id's. We need to place a lock before calling get_next_id
816            to get real unique values. */
817         $wait= 10;
818         while (get_lock("uidnumber") != ""){
819           sleep (1);
821           /* Oups - timed out */
822           if ($wait-- == 0){
823             msg_dialog::display(_("Warning"), _("Timeout while waiting for lock. Ignoring lock!"), WARNING_DIALOG);
824             break;
825           }
826         }
828         add_lock ("uidnumber", "gosa");
829         $this->uidNumber= $this->get_next_id("uidNumber", $this->dn);
830         if ($this->savedGidNumber != ""){
831           $this->gidNumber= $this->savedGidNumber;
832         } else {
833           $this->gidNumber= $this->get_next_id("gidNumber", $this->dn);
834         }
835       }
837       if ($this->primaryGroup != 0){
838         $this->gidNumber= $this->primaryGroup;
839       }
840     }
842     if ($this->activate_shadowMin != "1" ) {
843       $this->shadowMin = "";
844     }
846     if (($this->activate_shadowMax != "1") && ($this->mustchangepassword != "1")) {
847       $this->shadowMax = "";
848     }
850     if ($this->activate_shadowWarning != "1" ) {
851       $this->shadowWarning = "";
852     }
854     if ($this->activate_shadowInactive != "1" ) {
855       $this->shadowInactive = "";
856     }
858     if ($this->activate_shadowExpire != "1" ) {
859       $this->shadowExpire = "";
860     }
862     /* Fill gecos */
863     if (isset($this->parent) && $this->parent !== NULL){
864       $this->gecos= rewrite($this->parent->by_object['user']->cn);
865       if (!preg_match('/^[a-z0-9 -]+$/i', $this->gecos)){
866         $this->gecos= "";
867       }
868     }
870     foreach(array("shadowMin","shadowMax","shadowWarning","shadowInactive","shadowExpire") as $attr){
871       $this->$attr = (int) $this->$attr;
872     }
873     /* Call parents save to prepare $this->attrs */
874     plugin::save();
876     /* Trust accounts */
877     $objectclasses= array();
878     foreach ($this->attrs['objectClass'] as $key => $class){
879       if (preg_match('/trustAccount/i', $class)){
880         continue;
881       }
882       $objectclasses[]= $this->attrs['objectClass'][$key];
883     }
884     $this->attrs['objectClass']= $objectclasses;
885     if ($this->trustModel != ""){
886       $this->attrs['objectClass'][]= "trustAccount";
887       $this->attrs['trustModel']= $this->trustModel;
888       $this->attrs['accessTo']= array();
889       if ($this->trustModel == "byhost"){
890         foreach ($this->accessTo as $host){
891           $this->attrs['accessTo'][]= $host;
892         }
893       }
894     } else {
895       if ($this->was_trust_account){
896         $this->attrs['accessTo']= array();
897         $this->attrs['trustModel']= array();
898       }
899     }
901     if(empty($this->attrs['gosaDefaultPrinter'])){
902       $thid->attrs['gosaDefaultPrinter']=array();
903     }
906     /* include global link_info */
907     $this->cleanup();
908  
909     /* This is just a test, we have had duplicated ids 
910         in the past when copy & paste was used. 
911        Normaly this should not happen.
912      */ 
913     if(isset($this->attrs['uidNumber']) && !$this->force_ids){
914       $used = $this->get_used_uid_numbers();
915       if(isset($used[$this->attrs['uidNumber']]) && $used[$this->attrs['uidNumber']] != $this->dn){
916         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);
917       }
918     }
920     $ldap= $this->config->get_ldap_link();
921     $ldap->cd($this->dn);
922     unset($this->attrs['uid']);
923     $ldap->modify ($this->attrs); 
925     /* Log last action */ 
926     if($this->initially_was_account){
927       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
928     }else{
929       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
930     }
932     if (!$ldap->success()){
933       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
934     }
936     /* Remove lock needed for unique id generation */
937     del_lock ("uidnumber");
939     /* Posix accounts have group interrelationship, 
940        take care about these here if this is a new user without forced gidNumber. */
941     if ($this->force_ids == 0 && $this->primaryGroup == 0 && !$this->initially_was_account){
942       $ldap->cd($this->config->current['BASE']);
943       $ldap->search("(&(objectClass=posixGroup)(gidNumber=".$this->gidNumber."))", array("cn"));
945       /* Create group if it doesn't exist */
946       if ($ldap->count() == 0){
947         $groupdn= preg_replace ('/^'.$this->config->current['DNMODE'].'=[^,]+,'.get_people_ou().'/i', 'cn='.$this->uid.','.get_groups_ou(), $this->dn);
949         $g= new group($this->config, $groupdn);
950         $g->cn= $this->uid;
951         $g->force_gid= 1;
952         $g->gidNumber= $this->gidNumber;
953         $g->description= _("Group of user")." ".$this->givenName." ".$this->sn;
954         $g->save ();
955       }
956     }
958     /* Take care about groupMembership values: add to groups */
959     foreach ($this->groupMembership as $key => $value){
960       if (!isset($this->savedGroupMembership[$key])){
961         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
962         $g->set_acl_base($key);
963         $g->by_object['group']->addUser($this->uid);
964         $g->save();
965       }
966     }
968     /* Remove from groups not listed in groupMembership */
969     foreach ($this->savedGroupMembership as $key => $value){
970       if (!isset($this->groupMembership[$key])){
971         $g= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $key,"groups");
972         $g->set_acl_base($key);
973         $g->by_object['group']->removeUser ($this->uid);
974         $g->save();
975       }
976     }
978     /* Optionally execute a command after we're done */
979     if ($this->initially_was_account == $this->is_account){
980       if ($this->is_modified){
981         $this->handle_post_events("modify",array("uid" => $this->uid));
982       }
983     } else {
984       $this->handle_post_events("add" ,array("uid"=> $this->uid));
985     }
986   }
988   /* Check formular input */
989   function check()
990   {
991     /* Include global link_info */
992     $ldap= $this->config->get_ldap_link();
994     /* Append groups as memberGroup: to check hook 
995      */
996     $tmp_attributes  = $this->attributes;    
997     $this->attributes[] = "memberGroup";
998     $this->memberGroup = array();
999     foreach($this->groupMembership as $dn => $name){
1000       $this->memberGroup[] = $name;
1001     }
1003     /* Call common method to give check the hook */
1004     $message= plugin::check();
1005     $this->attributes = $tmp_attributes;
1007     /* must: homeDirectory */
1008     if ($this->homeDirectory == ""){
1009       $message[]= msgPool::required(_("Home directory"));
1010     }
1011     if (!tests::is_path($this->homeDirectory)){
1012       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1013     }
1015     /* Check ID's if they are forced by user */
1016     if ($this->force_ids == "1"){
1018       /* Valid uid/gid? */
1019       if (!tests::is_id($this->uidNumber)){
1020         $message[]= msgPool::invalid(_("UID"), $this->uidNumber, "/[0-9]/");
1021       } else {
1022         if ($this->uidNumber < $this->config->current['MINID']){
1023           $message[]= msgPool::toosmall(_("UID"), $this->config->current['MINID']);
1024         }
1025       }
1026       if (!tests::is_id($this->gidNumber)){
1027         $message[]= msgPool::invalid(_("GID"), $this->gidNumber, "/[0-9]/");
1028       } else {
1029         if ($this->gidNumber < $this->config->current['MINID']){
1030           $message[]= msgPool::toosmall(_("GID"), $this->config->current['MINID']);
1031         }
1032       }
1033     }
1035     /* Check shadow settings, well I like spaghetties... */
1036     if ($this->activate_shadowMin){
1037       if (!tests::is_id($this->shadowMin)){
1038         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1039       }
1040     }
1041     if ($this->activate_shadowMax){
1042       if (!tests::is_id($this->shadowMax)){
1043         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1044       }
1045     }
1046     if ($this->activate_shadowWarning){
1047       if (!tests::is_id($this->shadowWarning)){
1048         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1049       }
1050       if (!$this->activate_shadowMax){
1051         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1052       }
1053       if ($this->shadowWarning > $this->shadowMax){
1054         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1055       }
1056       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin){
1057         $message[]= msgPool::toosmall("shadowWarning", "shadowMin");
1058       }
1059     }
1060     if ($this->activate_shadowInactive){
1061       if (!tests::is_id($this->shadowInactive)){
1062         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1063       }
1064       if (!$this->activate_shadowMax){
1065         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1066       }
1067     }
1068     if ($this->activate_shadowMin && $this->activate_shadowMax){
1069       if ($this->shadowMin > $this->shadowMax){
1070         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1071       }
1072     }
1074     return ($message);
1075   }
1078   function multiple_check()
1079   {
1080     $message = plugin::multiple_check();
1081     if ($this->homeDirectory == "" && in_array("homeDirectory",$this->multi_boxes)){
1082       $message[]= msgPool::required(_("Home directory"));
1083     }
1084     if (!tests::is_path($this->homeDirectory) && in_array("homeDirectory",$this->multi_boxes)){
1085       $message[]= msgPool::invalid(_("Home directory"), "", "", "/home/yourname" );
1086     }
1088     /* Check shadow settings, well I like spaghetties... */
1089     if ($this->activate_shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1090       if (!tests::is_id($this->shadowMin)){
1091         $message[]= msgPool::invalid(_("shadowMin"), $this->shadowMin, "/[0-9]/");
1092       }
1093     }
1094     if ($this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1095       if (!tests::is_id($this->shadowMax)){
1096         $message[]= msgPool::invalid(_("shadowMax"), $this->shadowMax, "/[0-9]/");
1097       }
1098     }
1099     if ($this->activate_shadowWarning && in_array("activate_shadowWarning",$this->multi_boxes)){
1100       if (!tests::is_id($this->shadowWarning)){
1101         $message[]= msgPool::invalid(_("shadowWarning"), $this->shadowWarning, "/[0-9]/");
1102       }
1103       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1104         $message[]= msgPool::depends("shadowWarning", "shadowMax");
1105       }
1106       if ($this->shadowWarning > $this->shadowMax && in_array("activate_shadowWarning",$this->multi_boxes)){
1107         $message[]= msgPool::toobig("shadowWarning", "shadowMax");
1108       }
1109       if ($this->activate_shadowMin && $this->shadowWarning < $this->shadowMin && in_array("activate_shadowMin",$this->multi_boxes)){
1110         $message[]= msgPool::tosmall("shadowWarning", "shadowMin");
1111       }
1112     }
1113     if ($this->activate_shadowInactive && in_array("activate_shadowInactive",$this->multi_boxes)){
1114       if (!tests::is_id($this->shadowInactive)){
1115         $message[]= msgPool::invalid(_("shadowInactive"), $this->shadowInactive, "/[0-9]/");
1116       }
1117       if (!$this->activate_shadowMax && in_array("activate_shadowMax",$this->multi_boxes)){
1118         $message[]= msgPool::depends("shadowInactive", "shadowMax");
1119       }
1120     }
1121     if ($this->activate_shadowMin && $this->activate_shadowMax && in_array("activate_shadowMin",$this->multi_boxes)){
1122       if ($this->shadowMin > $this->shadowMax){
1123         $message[]= msgPool::toobig("shadowMin", "shadowMax");
1124       }
1125     }
1127     return($message);
1128   }
1131   function addGroup ($groups)
1132   {
1133     /* include global link_info */
1134     $ldap= $this->config->get_ldap_link();
1136     /* Walk through groups and add the descriptive entry if not exists */
1137     foreach ($groups as $value){
1139       if (!array_key_exists($value, $this->groupMembership)){
1140         $ldap->cat($value, array('cn', 'description', 'dn'));
1141         $attrs= $ldap->fetch();
1142         error_reporting (0);
1143         if (!isset($attrs['description'][0])){
1144           $entry= $attrs["cn"][0];
1145         } else {
1146           $dsc= preg_replace ('/^Group of user/', _("Group of user"), $attrs["description"][0]);
1147           $entry= $attrs["cn"][0]." [$dsc]";
1148         }
1149         error_reporting (E_ALL | E_STRICT);
1151         if(obj_is_writable($attrs['dn'],"groups/group","memberUid")){
1152           $this->groupMembership[$attrs['dn']]= $entry;
1153           if($this->multiple_support_active && isset($this->groupMembership_some[$attrs['dn']])){
1154             unset($this->groupMembership_some[$attrs['dn']]);
1155           }
1156         }
1157       }
1158     }
1160     /* Sort groups */
1161     asort ($this->groupMembership);
1162     reset ($this->groupMembership);
1163   }
1166   /* Del posix user from some groups */
1167   function delGroup ($groups)
1168   {
1169     $dest= array();
1170     foreach($groups as $dn_to_del){
1171       if(isset($this->groupMembership[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1172         unset($this->groupMembership[$dn_to_del]);
1173       }
1174       if($this->multiple_support_active){
1175         if(isset($this->groupMembership_some[$dn_to_del]) && obj_is_writable($dn_to_del,"groups/group","memberUid")){
1176           unset($this->groupMembership_some[$dn_to_del]);
1177         }
1178       }
1179     }
1180   }
1183   /* Adapt from template, using 'dn' */
1184   function adapt_from_template($dn, $skip= array())
1185   {
1186     /* Include global link_info */
1187     $ldap= $this->config->get_ldap_link();
1189     plugin::adapt_from_template($dn, $skip);
1190     $template= $this->attrs['uid'][0];
1192     /* Adapt group membership */
1193     $ldap->cd($this->config->current['BASE']);
1194     $ldap->search("(&(objectClass=posixGroup)(memberUid=".$this->attrs["uid"][0]."))", array("description", "cn"));
1196     while ($this->attrs= $ldap->fetch()){
1197       if (!isset($this->attrs["description"][0])){
1198         $entry= $this->attrs["cn"][0];
1199       } else {
1200         $entry= $this->attrs["cn"][0]." [".$this->attrs["description"][0]."]";
1201       }
1202       $this->groupMembership[$ldap->getDN()]= $entry;
1203     }
1205     /* Fix primary group settings */
1206     $ldap->cd($this->config->current['BASE']);
1207     $ldap->search("(&(objectClass=posixGroup)(cn=$template)(gidNumber=".$this->gidNumber."))", array("cn"));
1208     if ($ldap->count() != 1){
1209       $this->primaryGroup= $this->gidNumber;
1210     }
1212     $ldap->cd($this->config->current['BASE']);
1213     $ldap->search("(&(objectClass=gosaUserTemplate)(uid=".$template.")(accessTo=*))", array("cn","accessTo"));
1214     while($attr = $ldap->fetch()){
1215       $tmp = $attr['accessTo'];
1216       unset ($tmp['count']);
1217       $this->accessTo = $tmp;   
1218     }
1220     /* Adjust shadow checkboxes */
1221     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive") as $val){
1222       if ($this->$val != 0){
1223         $oval= "activate_".$val;
1224         $this->$oval= "1";
1225       }
1226     }
1228     /* FIXME: NEED review of this section */
1229     /* Need to check shadowExpire separately */
1231     /* 
1232      * If shadowExpire is not enabled in the template, it's a UNIX timestamp - so don't convert it to seconds.
1233      * The check is a hack - if difference between timestamp generated above and here is max 1 day.
1234      */
1235     if(abs($this->shadowExpire - time())>86400) {
1236       $this->shadowExpire= $this->convertToSeconds($this->shadowExpire);
1237     }
1238     
1239     /* Only enable checkbox, if shadowExpire is in the future */
1240     if($this->shadowExpire > time()) {
1241       $this->activate_shadowExpire= "1";
1242     }
1243   }
1245   function convertToSeconds($val)
1246   {
1247     if ($val != 0){
1248       $val*= 60 * 60 * 24;
1249     } else {
1250       $date= getdate();
1251       $val= floor($date[0] / (60*60*24)) * 60 * 60 * 24;
1252     }
1253     return($val);
1254   }
1257   function get_used_uid_numbers()
1258   {
1259     $ids= array();
1260     $ldap= $this->config->get_ldap_link();
1262     $ldap->cd ($this->config->current['BASE']);
1263     $ldap->search ("(&(objectClass=posixAccount)(uidNumber=*))", array("uidNumber"));
1265     /* Get list of ids */
1266     while ($attrs= $ldap->fetch()){
1267       $ids[$attrs['uidNumber'][0]] = $attrs['dn'];
1268     }
1269     return($ids);
1270   }
1272   
1273   function get_next_id($attrib, $dn)
1274   {
1275     $ids= array();
1276     $ldap= $this->config->get_ldap_link();
1278     $ldap->cd ($this->config->current['BASE']);
1279     if (preg_match('/gidNumber/i', $attrib)){
1280       $oc= "posixGroup";
1281     } else {
1282       $oc= "posixAccount";
1283     }
1284     $ldap->search ("(&(objectClass=$oc)($attrib=*))", array("$attrib"));
1286     /* Get list of ids */
1287     while ($attrs= $ldap->fetch()){
1288       $ids[]= (int)$attrs["$attrib"][0];
1289     }
1291     /* Add the nobody id */
1292     $ids[]= 65534;
1294     /* get the ranges */
1295     $tmp = array('0'=> 1000); 
1296     if (preg_match('/posixAccount/', $oc) && isset($this->config->current['UIDBASE'])) {
1297       $tmp= split('-',$this->config->current['UIDBASE']);
1298     } elseif(isset($this->config->current['GIDBASE'])){
1299       $tmp= split('-',$this->config->current['GIDBASE']);
1300     }
1302     /* Set hwm to max if not set - for backward compatibility */
1303     $lwm= $tmp[0];
1304     if (isset($tmp[1])){
1305       $hwm= $tmp[1];
1306     } else {
1307       $hwm= pow(2,32);
1308     }
1310     /* Find out next free id near to UID_BASE */
1311     if (!isset($this->config->current['BASE_HOOK'])){
1312       $base= $lwm;
1313     } else {
1314       /* Call base hook */
1315       $base= get_base_from_hook($dn, $attrib);
1316     }
1317     for ($id= $base; $id++; $id < pow(2,32)){
1318       if (!in_array($id, $ids)){
1319         return ($id);
1320       }
1321     }
1323     /* Should not happen */
1324     if ($id == $hwm){
1325       msg_dialog::display(_("Error"), _("Cannot allocate a free ID!"), ERROR_DIALOG);
1326       exit;
1327     }
1329   }
1331   function reload()
1332   {
1333     /* Set base for all searches */
1334     $base      = session::get('CurrentMainBase');
1335     $base     = $base;
1336     $ldap     = $this->config->get_ldap_link();    
1337     $attrs    =  array("cn", "description", "gidNumber");
1338     $Flags    = GL_SIZELIMIT;
1340     /* Get groups */
1341     if ($this->GroupUserRegex == '*'){
1342       $filter = "(&(objectClass=posixGroup)(cn=".$this->GroupRegex."))";
1343     } else {
1344       $filter= "(&(objectClass=posixGroup)(cn=".$this->GroupRegex.")(memberUid=".$this->GroupUserRegex."))";
1345     }
1346     if($this->SubSearch){
1347       $Flags |= GL_SUBSEARCH;
1348     }else{
1349       $base = get_groups_ou().$base;
1350     }
1352     $res= get_list($filter, "groups", $base,$attrs, $Flags);
1353   
1354     /* check sizelimit */
1355     if (preg_match("/size limit/i", $ldap->get_error())){
1356       session::set('limit_exceeded',TRUE);
1357     }
1359     /* Create a list of users */
1360     $this->grouplist = array();
1361     foreach ($res as $value){
1362       $this->grouplist[$value['gidNumber'][0]]= $value;
1363     }
1365     $tmp=array();
1366     foreach($this->grouplist as $tkey => $val ){
1367       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
1368     }
1370     /* Sort index */
1371     ksort($tmp);
1373     /* Recreate index array[dn]=cn[description]*/
1374     $this->grouplist=array();
1375     foreach($tmp as $val){
1376       if(isset($val['description'])){
1377         $this->grouplist[$val['dn']]=$val['cn'][0]."&nbsp;[".$val['description'][0]."]";
1378       }else{
1379         $this->grouplist[$val['dn']]=$val['cn'][0];
1380       }
1381     }
1383     reset ($this->grouplist);
1384   }
1387   /* Get posts from copy & paste dialog */ 
1388   function saveCopyDialog()
1389   {
1390     if(isset($_POST['homeDirectory'])){
1391       $this->homeDirectory = $_POST['homeDirectory'];
1392       if (isset ($_POST['force_ids'])){
1393         $data= 1;
1394         $this->gidNumber = $_POST['gidNumber'];
1395         $this->uidNumber = $_POST['uidNumber'];
1396       } else {
1397         $data= 0;
1398       }
1399       if ($this->force_ids != $data){
1400         $this->is_modified= TRUE;
1401       }
1402       $this->force_ids= $data;
1403     }
1404   }
1405  
1407   /* Create the posix dialog part for copy & paste */
1408   function getCopyDialog()
1409   {
1410     /* Skip dialog creation if this is not a valid account*/
1411     if(!$this->is_account) return("");
1412     if ($this->force_ids == 1){
1413       $force_ids = "checked";
1414       if (session::get('js')){
1415         $forceMode = "";
1416       }
1417     } else {
1418       if (session::get('js')){
1419         if($this->acl != "#none#")
1420           $forceMode ="disabled";
1421       }
1422       $force_ids = "";
1423     }
1425     $sta = "";
1427     /* Open group add dialog */
1428     if(isset($_POST['edit_groupmembership'])){
1429       $this->group_dialog = TRUE;
1430       $sta = "SubDialog";
1431     }
1433     /* If the group-add dialog is closed, call execute 
1434        to ensure that the membership is updatd */
1435     if(isset($_POST['add_groups_finish']) || isset($_POST['add_groups_cancel'])){
1436       $this->execute();
1437       $this->group_dialog =FALSE;
1438     }
1440     if($this->group_dialog){
1441       $str = $this->execute(true);
1442       $ret = array();
1443       $ret['string'] = $str;
1444       $ret['status'] = $sta;
1445       return($ret);
1446     }
1448     /* If a group member should be deleted, simply call execute */
1449     if(isset($_POST['delete_groupmembership'])){
1450       $this->execute();
1451     }
1453     /* Assigned informations to smarty */
1454     $smarty = get_smarty();
1455     $smarty->assign("homeDirectory",$this->homeDirectory);
1456     $smarty->assign("uidNumber",$this->uidNumber);
1457     $smarty->assign("gidNumber",$this->gidNumber);
1458     $smarty->assign("forceMode",$forceMode);
1459     $smarty->assign("force_ids",$force_ids);
1460     if (!count($this->groupMembership)){
1461       $smarty->assign("groupMembership", array("&nbsp;"));
1462     } else {
1463       $smarty->assign("groupMembership", $this->groupMembership);
1464     }
1466     /* Display wars message if there are more than 16 group members */
1467     if (count($this->groupMembership) > 16){
1468       $smarty->assign("groups", "too_many_for_nfs");
1469     } else {
1470       $smarty->assign("groups", "");
1471     }
1472     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1474     $ret = array();
1475     $ret['string'] = $str;
1476     $ret['status'] = $sta;
1477     return($ret);
1478   }
1481   function PrepareForCopyPaste($source)
1482   {
1483     plugin::PrepareForCopyPaste($source);
1485     /* Avoid using the same gid/uid number as source user */
1486     $this->savedUidNumber = $this->get_next_id("uidNumber", $this->dn);
1487     $this->savedGidNumber = $this->get_next_id("gidNumber", $this->dn);
1488   }
1491   function multiple_execute()
1492   {
1493     return($this->execute());
1494   }
1497   static function plInfo()
1498   {
1499     return (array(
1500           "plDescription"     => _("POSIX account"),
1501           "plSelfModify"      => TRUE,
1502           "plDepends"         => array("user"),
1503           "plPriority"        => 2,
1504           "plSection"         => array("personal" => _("My account")),
1505           "plCategory"        => array("users"),
1506           "plOptions"         => array(),
1508           "plProvidedAcls"  => array(
1510             "homeDirectory"       =>  _("Home directory"), 
1511             "loginShell"          =>  _("Shell"),
1512             "uidNumber"           =>  _("User ID"),
1513             "gidNumber"           =>  _("Group ID"),
1515             "mustchangepassword"=>  _("Force password change on login"),
1516             "shadowMin"           =>  _("Shadow min"),
1517             "shadowMax"           =>  _("Shadow max"),
1518             "shadowWarning"       =>  _("Shadow warning"),
1519             "shadowInactive"      =>  _("Shadow inactive"),
1520             "shadowExpire"        =>  _("Shadow expire"),
1521             "trustModel"          =>  _("System trust model")))
1522             );
1523   }
1526   /* Return selected values for multiple edit */
1527   function get_multi_edit_values()
1528   {
1529     $ret = plugin::get_multi_edit_values();
1530     $ret['groupMembership']     = $this->groupMembership;
1531     $ret['groupMembership_some']= $this->groupMembership_some;
1533     if(in_array("primaryGroup",$this->multi_boxes)){
1534       $ret['primaryGroup'] = $this->primaryGroup;
1535     }
1536     if(in_array("trustmode",$this->multi_boxes)){
1537       $ret['trustModel'] = $this->trustModel;
1538       $ret['accessTo'] = $this->accessTo;
1539     }
1540     foreach(array("shadowWarning","shadowInactive","shadowMin","shadowMax", "shadowExpire") as $entry){
1541       $active = "activate_".$entry;
1542       if(in_array($active,$this->multi_boxes)){
1543         $ret[$entry] = $this->$entry;
1544         $ret[$active] = $this->$active;
1545       }
1546     }
1547     if(in_array("mustchangepassword",$this->multi_boxes)){
1548       $ret['mustchangepassword'] = $this->mustchangepassword;
1549     }
1550     return($ret);
1551   }
1554   /* Save posts for multiple edit 
1555    */
1556   function multiple_save_object()
1557   {
1558     if(isset($_POST['posix_mulitple_edit'])){
1559  
1560       /* Backup expire value */ 
1561       $expire_tmp = $this->shadowExpire;
1562   
1563       /* Update all values */
1564       plugin::multiple_save_object();
1566       /* Get selected checkboxes */
1567       foreach(array("primaryGroup","trustmode","mustchangepassword","activate_shadowWarning","activate_shadowInactive","activate_shadowMin", "activate_shadowMax","activate_shadowExpire") as $val){
1568         if(isset($_POST["use_".$val])){
1569           $this->multi_boxes[] = $val;
1570         }
1571       }
1573       /* Update special values, checkboxes for posixShadow */
1574       foreach(array("shadowMin","shadowMax","shadowExpire","shadowInactive","shadowWarning") as $var) {
1575         if($this->acl_is_writeable($var)){
1576           $activate_var = "activate_".$var;
1577           if(in_array($activate_var, $this->multi_boxes)){
1578             if(isset($_POST['activate_'.$var])){
1579               $this->$activate_var  = true;
1580               $this->$var      = $_POST[$var];
1581             }else{
1582               $this->$activate_var  = false;
1583               $this->$var      = 0;
1584             }
1585           }
1586         }
1587       }
1589       /* Restore shadow value, if the shadow attribute isn't used */
1590       if(!in_array("activate_shadowExpire",$this->multi_boxes)){
1591         $this->shadowExpire = $expire_tmp;
1592       }
1594       /* Force change password ? */
1595       if(isset($_POST['mustchangepassword'])){
1596         $this->mustchangepassword = TRUE;
1597       }else{
1598         $this->mustchangepassword = FALSE;
1599       }
1601       /* Trust mode - special handling */
1602       if($this->acl_is_writeable("trustModel")){
1603         if (isset($_POST['trustmode'])){
1604           $saved= $this->trustModel;
1605           if ($_POST['trustmode'] == "1"){
1606             $this->trustModel= "fullaccess";
1607           } elseif ($_POST['trustmode'] == "2"){
1608             $this->trustModel= "byhost";
1609           } else {
1610             $this->trustModel= "";
1611           }
1612           if ($this->trustModel != $saved){
1613             $this->is_modified= TRUE;
1614           }
1615         }
1616       }
1618       /* Save primary group settings */
1619       if($this->acl_is_writeable("primaryGroup") && isset($_POST['primaryGroup'])){
1620         $data= $_POST['primaryGroup'];
1621         if ($this->primaryGroup != $data){
1622           $this->is_modified= TRUE;
1623         }
1624         $this->primaryGroup= $_POST['primaryGroup'];
1625       }
1626     }
1627   }
1629   
1630   /* Initialize plugin with given atribute arrays 
1631    */
1632   function init_multiple_support($attrs,$all)
1633   {
1634     plugin::init_multiple_support($attrs,$all);
1636     /* Some dummy values */
1637     $groups_some = array();
1638     $groups_all  = array();
1639     $groups_uid  = array();
1640     $uids        = array();
1641     $first       = TRUE;
1643     /* Get all groups used by currently edited users */
1644     $uid_filter="";  
1645     for($i =0; $i < $this->multi_attrs_all['uid']['count'] ; $i ++){
1646       $uid = $this->multi_attrs_all['uid'][$i];
1647       $uids[] = $uid;
1648       $uid_filter.= "(memberUid=".$uid.")"; 
1649     }
1650     $uid_filter = "(&(objectClass=posixGroup)(|".$uid_filter."))";
1651     $ldap = $this->config->get_ldap_link();
1652     $ldap->cd($this->config->current['BASE']);
1653     $ldap->search($uid_filter,array("dn","cn","memberUid"));
1654     while($group = $ldap->fetch()){
1655       $groups_some[$group['dn']] = $group['cn'][0];
1656       for($i = 0 ; $i < $group['memberUid']['count'] ; $i++){
1657         $groups_uid[$group['dn']][] = $group['memberUid'][$i];
1658       }
1659     }
1661     /* Create an array, containing all used groups */
1662     $groups_all = $groups_some;
1663     foreach($groups_all as $id => $group){
1664       foreach($uids as $uid){
1665         if(!in_array($uid,$groups_uid[$id])){
1666           unset($groups_all[$id]);
1667           break;
1668         }
1669       }
1670     }
1672     /* Assign group array */
1673     $this->groupMembership = $groups_all;
1675     /* Create an array of all grouops used by all users */
1676     foreach( $groups_all as $dn => $cn){
1677       if(isset($groups_some[$dn])){
1678         unset($groups_some[$dn]);
1679       }
1680     }
1681     $this->groupMembership_some = $groups_some;
1682     $this->primaryGroup = $this->gidNumber;
1684     /* Is this account a trustAccount? */
1685     if (isset($this->multi_attrs['trustModel'])){
1686       $this->trustModel= $this->multi_attrs['trustModel'][0];
1687       $this->was_trust_account= TRUE;
1688       $this->multi_boxes[] = "trustmode";
1689     } else {
1690       $this->was_trust_account= FALSE;
1691       $this->trustModel= "";
1692     }
1694     /* Create access informations */
1695     $this->accessTo = array();
1696     if (isset($this->multi_attrs['accessTo'])){
1697       for ($i= 0; $i<$this->multi_attrs['accessTo']['count']; $i++){
1698         $tmp= $this->multi_attrs['accessTo'][$i];
1699         $this->accessTo[$tmp]= $tmp;
1700       }
1701     }
1703     /* Adjust shadow checkboxes */
1704     foreach (array("shadowMin", "shadowMax", "shadowWarning", "shadowInactive",
1705           "shadowExpire") as $val){
1706       if ($this->$val != 0){
1707         $oval= "activate_".$val;
1708         $this->$oval= "1";
1709       }
1710     }
1712     /* Convert to seconds */
1713     if(isset($this->multi_attrs['shadowExpire'])){
1714       $this->shadowExpire = $this->convertToSeconds($this->multi_attrs['shadowExpire'][0]);
1715     }else{
1716       $this->activate_shadowExpire = FALSE;
1717     }
1718   }
1721   function set_multi_edit_values($attrs)
1722   {
1723     $groups = array();
1725     /* Update groupMembership, keep optinal group */
1726     foreach($attrs['groupMembership_some'] as $dn => $cn){
1727       if(isset($this->groupMembership[$dn])){
1728         $groups[$dn] = $cn;
1729       }
1730     }
1731     /* Update groupMembership, add forced groups */
1732     foreach($attrs['groupMembership'] as $dn => $cn){
1733       $groups[$dn] = $cn;
1734     }
1735     plugin::set_multi_edit_values($attrs);
1736     $this->groupMembership = $groups;
1737   }
1740 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1741 ?>