Code

Reverted global session changes. Too time consuming.
[gosa.git] / gosa-core / plugins / personal / generic / class_user.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   user 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 person, organizationalPerson, inetOrgPerson and gosaAccount
31   from/to the LDAP. It does syntax checking and displays the formulars required.
32  */
34 class user extends plugin
35 {
36   /* Definitions */
37   var $plHeadline= "Generic";
38   var $plDescription= "Edit organizational user settings";
40   /* The attribute gotoLastSystemLogin represents the timestamp of the last 
41       successfull login on the users workstation. 
42      Read the FAQ to get a hint about how to configure this.
43    */
44   var $gotoLastSystemLogin = "";
46   /* Plugin specific values */
47   var $base= "";
48   var $orig_base= "";
49   var $cn= "";
50   var $new_dn= "";
51   var $personalTitle= "";
52   var $academicTitle= "";
53   var $homePostalAddress= "";
54   var $homePhone= "";
55   var $labeledURI= "";
56   var $o= "";
57   var $ou= "";
58   var $departmentNumber= "";
59   var $employeeNumber= "";
60   var $employeeType= "";
61   var $roomNumber= "";
62   var $telephoneNumber= "";
63   var $facsimileTelephoneNumber= "";
64   var $mobile= "";
65   var $pager= "";
66   var $l= "";
67   var $st= "";
68   var $postalAddress= "";
69   var $dateOfBirth;
70   var $use_dob= "0";
71   var $gender="0";
72   var $preferredLanguage="0";
74   var $jpegPhoto= "*removed*";
75   var $photoData= "";
76   var $old_jpegPhoto= "";
77   var $old_photoData= "";
78   var $cert_dialog= FALSE;
79   var $picture_dialog= FALSE;
80   var $pwObject= NULL;
82   var $userPKCS12= "";
83   var $userSMIMECertificate= "";
84   var $userCertificate= "";
85   var $certificateSerialNumber= "";
86   var $old_certificateSerialNumber= "";
87   var $old_userPKCS12= "";
88   var $old_userSMIMECertificate= "";
89   var $old_userCertificate= "";
91   var $gouvernmentOrganizationalUnit= "";
92   var $houseIdentifier= "";
93   var $street= "";
94   var $postalCode= "";
95   var $vocation= "";
96   var $ivbbLastDeliveryCollective= "";
97   var $gouvernmentOrganizationalPersonLocality= "";
98   var $gouvernmentOrganizationalUnitDescription= "";
99   var $gouvernmentOrganizationalUnitSubjectArea= "";
100   var $functionalTitle= "";
101   var $role= "";
102   var $publicVisible= "";
104   var $orig_dn;
105   var $dialog;
107   /* variables to trigger password changes */
108   var $pw_storage= "md5";
109   var $last_pw_storage= "unset";
110   var $had_userCertificate= FALSE;
112   var $view_logged = FALSE;
114   /* attribute list for save action */
115   var $attributes= array("sn", "givenName", "uid", "personalTitle", "academicTitle",
116       "homePostalAddress", "homePhone", "labeledURI", "ou", "o", "dateOfBirth", "gender","preferredLanguage",
117       "departmentNumber", "employeeNumber", "employeeType", "l", "st","jpegPhoto",
118       "roomNumber", "telephoneNumber", "mobile", "pager", "cn", "userPKCS12",
119       "postalAddress", "facsimileTelephoneNumber", "userSMIMECertificate");
121   var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson",
122       "gosaAccount");
124   /* attributes that are part of the government mode */
125   var $govattrs= array("gouvernmentOrganizationalUnit", "houseIdentifier", "vocation",
126       "ivbbLastDeliveryCollective", "gouvernmentOrganizationalPersonLocality",
127       "gouvernmentOrganizationalUnitDescription","gouvernmentOrganizationalUnitSubjectArea",
128       "functionalTitle", "certificateSerialNumber", "publicVisible", "street", "role",
129       "postalCode");
131   var $multiple_support = TRUE;
133   var $governmentmode = FALSE;
135   /* constructor, if 'dn' is set, the node loads the given
136      'dn' from LDAP */
137   function user (&$config, $dn= NULL)
138   {
139     $this->config= $config;
140     /* Configuration is fine, allways */
141     if($this->config->get_cfg_value("honourIvbbAttributes") == "true"){
142       $this->governmentmode = TRUE;
143       $this->attributes=array_merge($this->attributes,$this->govattrs);
144     }
146     /* Load base attributes */
147     plugin::plugin ($config, $dn);
149     /*  If gotoLastSystemLogin is available read it from ldap and create a readable
150         date time string.
151      */
152     if(isset($this->attrs['gotoLastSystemLogin'][0]) && preg_match("/^[0-9]*$/",$this->attrs['gotoLastSystemLogin'][0])){
153       $this->gotoLastSystemLogin = date("d.m.Y H:i:s", $this->attrs['gotoLastSystemLogin'][0]);
154     }
156     $this->orig_dn  = $this->dn;
157     $this->new_dn   = $dn;
159     if ($this->governmentmode){
160       /* Fix public visible attribute if unset */
161       if (!isset($this->attrs['publicVisible'])){
162         $this->publicVisible == "nein";
163       }
164     }
166     /* Load government mode attributes */
167     if ($this->governmentmode){
168       /* Copy all attributs */
169       foreach ($this->govattrs as $val){
170         if (isset($this->attrs["$val"][0])){
171           $this->$val= $this->attrs["$val"][0];
172         }
173       }
174     }
176     /* Create me for new accounts */
177     if ($dn == "new"){
178       $this->is_account= TRUE;
179     }
181     /* Make hash default to md5 if not set in config */
182     $hash= $this->config->get_cfg_value("passwordDefaultHash", "crypt/md5");
184     /* Load data from LDAP? */
185     if ($dn !== NULL){
187       /* Do base conversation */
188       if ($this->dn == "new"){
189         $ui= get_userinfo();
190         $this->base= dn2base($ui->dn);
191       } else {
192         $this->base= dn2base($dn);
193       }
195       /* get password storage type */
196       if (isset ($this->attrs['userPassword'][0])){
197         /* Initialize local array */
198         $matches= array();
199         if (preg_match ("/^{[^}]+}/", $this->attrs['userPassword'][0])){
200           $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
201           if(is_object($tmp)){
202             $this->pw_storage= $tmp->get_hash(); 
203           }
205         } else {
206           if ($this->attrs['userPassword'][0] != ""){
207             $this->pw_storage= "clear";
208           } else {
209             $this->pw_storage= $hash;
210           }
211         }
212       } else {
213         /* Preset with vaule from configuration */
214         $this->pw_storage= $hash;
215       }
217       /* Load extra attributes: certificate and picture */
218       $this->load_cert();
219       $this->load_picture();
220       if ($this->userCertificate != ""){
221         $this->had_userCertificate= TRUE;
222       }
223     }
225     /* Reset password storage indicator, used by password_change_needed() */
226     if ($dn == "new"){
227       $this->last_pw_storage= "unset";
228     } else {
229       $this->last_pw_storage= $this->pw_storage;
230     }
232     /* Generate dateOfBirth entry */
233     if (isset ($this->attrs['dateOfBirth'])){
234       /* This entry is ISO 8601 conform */
235       list($year, $month, $day)= split("-", $this->attrs['dateOfBirth'][0], 3);
236     
237       $this->dateOfBirth=array( 'mon'=> $month,"mday"=> $day,"year"=> $year);
238       $this->use_dob= "1";
239     } else {
240       $this->use_dob= "0";
241     }
243     /* Put gender attribute to upper case */
244     if (isset ($this->attrs['gender'])){
245       $this->gender= strtoupper($this->attrs['gender'][0]);
246     }
247  
248     $this->orig_base = $this->base;
249   }
254   /* execute generates the html output for this node */
255   function execute()
256   {
257     /* Call parent execute */
258     plugin::execute();
260     /* Log view */
261     if($this->is_account && !$this->view_logged){
262       $this->view_logged = TRUE;
263       new log("view","users/".get_class($this),$this->dn);
264     }
266     $smarty= get_smarty();
267     $smarty->assign("gotoLastSystemLogin",$this->gotoLastSystemLogin);
269     /* Fill calendar */
270     if ($this->dateOfBirth == "0"){
271       $date= getdate();
272     } else {
273       if(is_array($this->dateOfBirth)){
274         $date = $this->dateOfBirth;
275   
276         // Trigger on dates like 1985-04-01, getdate only understands timestamps
277       } else if (!empty($this->dateOfBirth) && !is_numeric($this->dateOfBirth)){
278         $date= getdate(strtotime($this->dateOfBirth));
280       } else {
281         $date = getdate($this->dateOfBirth);
282       }
283     }
285     $days= array();
286     for($d= 1; $d<32; $d++){
287       $days[$d]= $d;
288     }
289     $years= array();
291     if(($date['year']-100)<1901){
292       $start = 1901;
293     }else{
294       $start = $date['year']-100;
295     }
297     $end = $start +100;
298     
299     for($y= $start; $y<=$end; $y++){
300       $years[]= $y;
301     }
302     $years['-']= "-&nbsp;";
303     $months= msgPool::months();
304     $months['-'] = '-&nbsp;';
306     $smarty->assign("day", $date["mday"]);
307     $smarty->assign("days", $days);
308     $smarty->assign("months", $months);
309     $smarty->assign("month", $date["mon"]-1);
310     $smarty->assign("years", $years);
311     $smarty->assign("year", $date["year"]);
313     /* Assign sex */
314     $sex= array(0 => "&nbsp;", "F" => _("female"), "M" => _("male"));
315     $smarty->assign("gender_list", $sex);
316     $language= array_merge(array(0 => "&nbsp;") ,get_languages(TRUE));
317     $smarty->assign("preferredLanguage_list", $language);
319     /* Get random number for pictures */
320     srand((double)microtime()*1000000); 
321     $smarty->assign("rand", rand(0, 10000));
324     /* Do we represent a valid gosaAccount? */
325     if (!$this->is_account){
326       $str = "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
327         msgPool::noValidExtension("GOsa")."</b>";
328       return($str);
329     }
331     /* Base select dialog */
332     $once = true;
333     foreach($_POST as $name => $value){
334       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
335         $once = false;
336         $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
337         $this->dialog->setCurrentBase($this->base);
338       }
339     }
341     /* Password configure dialog handling */
342     if(is_object($this->pwObject) && $this->pwObject->display){
343       $output= $this->pwObject->configure();
344       if ($output != ""){
345         $this->dialog= TRUE;
346         return $output;
347       }
348       $this->dialog= false;
349     }
351     /* Dialog handling */
352     if(is_object($this->dialog)){
353       /* Must be called before save_object */
354       $this->dialog->save_object();
355    
356       if($this->dialog->isClosed()){
357         $this->dialog = false;
358       }elseif($this->dialog->isSelected()){
360         /* check if selected base is allowed to move to / create a new object */
361         $tmp = $this->get_allowed_bases();
362         if(isset($tmp[$this->dialog->isSelected()])){
363           $this->base = $this->dialog->isSelected();
364         }
365         $this->dialog= false;
366       }else{
367         return($this->dialog->execute());
368       }
369     }
371     /* Want password method editing? */
372     if ($this->acl_is_writeable("userPassword")){
373       if (isset($_POST['edit_pw_method'])){
374         if (!is_object($this->pwObject) || $this->pw_storage != $this->pwObject->get_hash_name()){
375           $temp= passwordMethod::get_available_methods();
376           $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
377         }
378         $this->pwObject->display = TRUE;
379         $this->dialog= TRUE;
380         return ($this->pwObject->configure());
381       }
382     }
384     /* Want picture edit dialog? */
385     if($this->acl_is_writeable("userPicture")) {
386       if (isset($_POST['edit_picture'])){
387         /* Save values for later recovery, in case some presses
388            the cancel button. */
389         $this->old_jpegPhoto= $this->jpegPhoto;
390         $this->old_photoData= $this->photoData;
391         $this->picture_dialog= TRUE;
392         $this->dialog= TRUE;
393       }
394     }
396     /* Remove picture? */
397     if($this->acl_is_writeable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))) ){
398       if (isset($_POST['picture_remove'])){
399         $this->set_picture ();
400         $this->jpegPhoto= "*removed*";
401         $this->is_modified= TRUE;
402         return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
403       }
404     }
406     /* Save picture */
407     if (isset($_POST['picture_edit_finish'])){
409       /* Check for clean upload */
410       if ($_FILES['picture_file']['name'] != ""){
411         if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
412           msg_dialog::display(_("Error"), _("Cannot upload file!"), ERROR_DIALOG);
413         }else{
414           /* Activate new picture */
415           $this->set_picture($_FILES['picture_file']['tmp_name']);
416         }
417       }
418       $this->picture_dialog= FALSE;
419       $this->dialog= FALSE;
420       $this->is_modified= TRUE;
421     }
424     /* Cancel picture */
425     if (isset($_POST['picture_edit_cancel'])){
427       /* Restore values */
428       $this->jpegPhoto= $this->old_jpegPhoto;
429       $this->photoData= $this->old_photoData;
431       /* Update picture */
432       session::set('binary',$this->photoData);
433       session::set('binarytype',"image/jpeg");
434       $this->picture_dialog= FALSE;
435       $this->dialog= FALSE;
436     }
438     /* Toggle dateOfBirth information */
439     if (isset($_POST['set_dob'])){
440       $this->use_dob= ($this->use_dob == "0")?"1":"0";
441     }
444     /* Want certificate= */
445     if ((isset($_POST['edit_cert'])) && $this->acl_is_readable("Certificate")){
447       /* Save original values for later reconstruction */
448       foreach (array("certificateSerialNumber", "userCertificate",
449             "userSMIMECertificate", "userPKCS12") as $val){
451         $oval= "old_$val";
452         $this->$oval= $this->$val;
453       }
455       $this->cert_dialog= TRUE;
456       $this->dialog= TRUE;
457     }
460     /* Cancel certificate dialog */
461     if (isset($_POST['cert_edit_cancel'])){
463       /* Restore original values in case of 'cancel' */
464       foreach (array("certificateSerialNumber", "userCertificate",
465             "userSMIMECertificate", "userPKCS12") as $val){
467         $oval= "old_$val";
468         $this->$val= $this->$oval;
469       }
470       $this->cert_dialog= FALSE;
471       $this->dialog= FALSE;
472     }
475     /* Remove certificate? */
476     if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){ 
477       foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
478         if (isset($_POST["remove_$val"])){
480           /* Reset specified cert*/
481           $this->$val= "";
482           $this->is_modified= TRUE;
483         }
484       }
485     }
487     /* Upload new cert and close dialog? */     
488     if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){ 
490       $fail =false;
492       if (isset($_POST['cert_edit_finish'])){
494         /* for all certificates do */
495         foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
496             as $val){
498           /* Check for clean upload */
499           if (array_key_exists($val."_file", $_FILES) &&
500               array_key_exists('name', $_FILES[$val."_file"]) &&
501               $_FILES[$val."_file"]['name'] != "" &&
502               is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
503             $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
504           }
505         }
507         /* Save serial number */
508         if (isset($_POST["certificateSerialNumber"]) &&
509             $_POST["certificateSerialNumber"] != ""){
511           if (!tests::is_id($_POST["certificateSerialNumber"])){
512             $fail = true;
513             msg_dialog::display(_("Error"), msgPool::invalid(_("Serial number"),$_POST["certificateSerialNumber"],"/[0-9]/"),ERROR_DIALOG);
515             foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
516               if ($this->$cert != ""){
517                 $smarty->assign("$cert"."_state", "true");
518               } else {
519                 $smarty->assign("$cert"."_state", "");
520               }
521             }
522           }
524           $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
525           $this->is_modified= TRUE;
526         }
527         if(!$fail){
528           $this->cert_dialog= FALSE;
529           $this->dialog= FALSE;
530         }
531       }
532     }
533     /* Display picture dialog */
534     if ($this->picture_dialog){
535       return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
536     }
538     /* Display cert dialog */
539     if ($this->cert_dialog){
540       $smarty->assign("CertificateACL",$this->getacl("Certificate",(!is_object($this->parent) && !session::is_set('edit'))));
541       $smarty->assign("Certificate_readable",$this->acl_is_readable("Certificate"));
542       $smarty->assign("certificateSerialNumber",$this->certificateSerialNumber);
544       foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
545         if ($this->$cert != ""){
546           /* import certificate */
547           $certificate = new certificate;
548           $certificate->import($this->$cert);
549       
550           /* Read out data*/
551           $timeto   = $certificate->getvalidto_date();
552           $timefrom = $certificate->getvalidfrom_date();
553          
554           
555           /* Additional info if start end time is '0' */
556           $add_str_info = "";
557           if($timeto == 0 && $timefrom == 0){
558             $add_str_info = "<br><i>"._("(Some types of certificates are currently not supported and may be displayed as 'invalid'.)")."</i>";
559           }
561           $str = "<table summary=\"\" border=0>
562                     <tr>
563                       <td style='vertical-align:top'>CN</td>
564                       <td>".preg_replace("/ /", "&nbsp;", $certificate->getname())."</td>
565                     </tr>
566                   </table><br>".
568                   sprintf(_("Certificate is valid from %s to %s and is currently %s."),
569                         "<b>".date('d M Y',$timefrom)."</b>",
570                         "<b>".date('d M Y',$timeto)."</b>",
571                         $certificate->isvalid()?"<b><font style='color:green'>"._("valid")."</font></b>":
572                                                 "<b><font style='color:red'>"._("invalid")."</font></b>").$add_str_info;
574           $smarty->assign($cert."info",$str);
575           $smarty->assign($cert."_state","true");
576         } else {
577           $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
578           $smarty->assign($cert."_state","");
579         }
580       }
581   
582       if($this->governmentmode){
583         $smarty->assign("honourIvbbAttributes", "true");
584       }else{
585         $smarty->assign("honourIvbbAttributes", "false");
586       }
587       $smarty->assign("governmentmode", $this->governmentmode);
588       return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
589     }
591     /* Prepare password hashes */
592     if ($this->pw_storage == ""){
593       $this->pw_storage= $this->config->get_cfg_value("hash");
594     }
596     $temp= passwordMethod::get_available_methods();
597     $is_configurable= FALSE;
598     $hashes = $temp['name'];
599     if(isset($temp[$this->pw_storage])){
600       $test= new $temp[$this->pw_storage]($this->config);
601       $is_configurable= $test->is_configurable();
602     }else{
603       new msg_dialog(_("Password method"),_("The selected password method is no longer available."),WARNING_DIALOG);
604     }
607     /* Create password methods array */
608     $pwd_methods = array();
609     foreach($hashes as $id => $name){
610       if(!empty($temp['desc'][$id])){
611         $pwd_methods[$name] = $name." (".$temp['desc'][$id].")";
612       }else{
613         $pwd_methods[$name] = $name;
614       }
615     }
616  
617     /* Load attributes and acl's */
618     $ui =get_userinfo();
619     foreach($this->attributes as $val){
620       $smarty->assign("$val", $this->$val);
621       if(in_array($val,$this->multi_boxes)){
622         $smarty->assign("use_".$val,TRUE);
623       }else{
624         $smarty->assign("use_".$val,FALSE);
625       }
626     }
627     foreach(array("base","pw_storage","edit_picture") as $val){
628       if(in_array($val,$this->multi_boxes)){
629         $smarty->assign("use_".$val,TRUE);
630       }else{
631         $smarty->assign("use_".$val,FALSE);
632       }
633     }
635     /* Set acls */
636     $tmp = $this->plinfo();
637     foreach($tmp['plProvidedAcls'] as $val => $translation){
638       $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
639     }
641     $smarty->assign("pwmode", $pwd_methods);
642     $smarty->assign("pwmode_select", $this->pw_storage);
643     $smarty->assign("pw_configurable", $is_configurable);
644     $smarty->assign("passwordStorageACL", $this->getacl("userPassword",(!is_object($this->parent) && !session::is_set('edit'))));
645     $smarty->assign("base_select",      $this->base);
647     if(!session::is_set('edit')){
648       $smarty->assign("CertificatesACL","");
649     }else{
650       $smarty->assign("CertificatesACL",  $this->getacl("Certificate"));
651     }
652     
653     $smarty->assign("userPictureACL",   $this->getacl("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
654     $smarty->assign("userPicture_is_readable",   $this->acl_is_readable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
656     /* Create base acls */
657     $tmp = @$this->allowedBasesToMoveTo();
658     $smarty->assign("bases", $tmp);
660     /* Save government mode attributes */
661     if($this->governmentmode){
662       $smarty->assign("governmentmode", "true");
663       $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
664           "internet,ivbv", "internet,testa", "internet,ivbv,testa");
665       $smarty->assign("ivbbmodes", $ivbbmodes);
666       foreach ($this->govattrs as $val){
667         $smarty->assign("$val", $this->$val);
668         $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
669       }
670     } else {
671       $smarty->assign("governmentmode", "false");
672     }
674     /* Special mode for uid */
675     $uidACL= $this->getacl("uid",(!is_object($this->parent) && !session::is_set('edit')));
676     if (isset ($this->dn)){
677       if ($this->dn != "new"){
678         $uidACL= preg_replace("/w/","",$uidACL);
679       }
680     }  else {
681       $uidACL= preg_replace("/w/","",$uidACL);
682     }
683     
684     $smarty->assign("uidACL", $uidACL);
685     $smarty->assign("is_template", $this->is_template);
686     $smarty->assign("use_dob", $this->use_dob);
688     if (isset($this->parent)){
689       if (isset($this->parent->by_object['phoneAccount']) &&
690           $this->parent->by_object['phoneAccount']->is_account){
691         $smarty->assign("has_phoneaccount", "true");
692       } else {
693         $smarty->assign("has_phoneaccount", "false");
694       }
695     } else {
696       $smarty->assign("has_phoneaccount", "false");
697     }
698     $smarty->assign("multiple_support" , $this->multiple_support_active);
699     return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
700   }
703   /* remove object from parent */
704   function remove_from_parent()
705   {
706     /* Only remove valid accounts */
707     if(!$this->initially_was_account) return;
709     /* Remove password extension */
710     $temp= passwordMethod::get_available_methods();
712     /* Remove password method from user account */
713     if(isset($temp[$this->pw_storage]) && class_available($temp[$this->pw_storage])){
714       $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
715       $this->pwObject->remove_from_parent();
716     }
718     /* Remove user */
719     $ldap= $this->config->get_ldap_link();
720     $ldap->rmdir ($this->dn);
721     if (!$ldap->success()){
722       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
723     }
724   
725     new log("remove","users/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
726   
727     /* Delete references to groups */
728     $ldap->cd ($this->config->current['BASE']);
729     $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
730     while ($ldap->fetch()){
731       $g= new group($this->config, $ldap->getDN());
732       $g->removeUser($this->uid);
733       $g->save ();
734     }
736     /* Delete references to object groups */
737     $ldap->cd ($this->config->current['BASE']);
738     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
739     while ($ldap->fetch()){
740       $og= new ogroup($this->config, $ldap->getDN());
741       unset($og->member[$this->dn]);
742       $og->save ();
743     }
745     /* If needed, let the password method do some cleanup */
746     $tmp = new passwordMethod($this->config);
747     $available = $tmp->get_available_methods();
748     if (in_array_ics($this->pw_storage, $available['name'])){
749       $test= new $available[$this->pw_storage]($this->config);
750       $test->attrs= $this->attrs;
751       $test->dn= $this->dn;
752       $test->remove_from_parent();
753     }
755     /* Remove ACL dependencies too */
756     $tmp = new acl($this->config,$this->parent,$this->dn);
757     $tmp->remove_acl();
759     /* Optionally execute a command after we're done */
760     $this->handle_post_events("remove",array("uid" => $this->uid));
761   }
764   /* Save data to object */
765   function save_object()
766   {
767     if(isset($_POST['generic']) || isset($_POST['multiple_user_posted'])){
769       /* Make a backup of the current selected base */
770       $base_tmp = $this->base;
772       /* Parents save function */
773       plugin::save_object ();
775       /* Save government mode attributes */
776       if ($this->governmentmode){
777         foreach ($this->govattrs as $val){
778           if ($this->acl_is_writeable($val,(!is_object($this->parent) && !session::is_set('edit'))) && isset($_POST["$val"])){
779             $data= stripcslashes($_POST["$val"]);
780             if ($data != $this->$val){
781               $this->is_modified= TRUE;
782             }
783             $this->$val= $data;
784           }
785         }
786       }
788       /* In template mode, the uid is autogenerated... */
789       if ($this->is_template){
790         $this->uid= strtolower($this->sn);
791         $this->givenName= $this->sn;
792       }
794       /* Save base and pw_storage, since these are no LDAP attributes */
795       if (isset($_POST['base'])){
797         $tmp = $this->get_allowed_bases();
798         if(isset($tmp[$_POST['base']])){
799           $base= validate($_POST['base']);
800           if ($base != $this->base){
801             $this->is_modified= TRUE;
802           }
803           $this->base= $base;
804         }else{
805           $this->base = $base_tmp;
806           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
807         }
808       }
810       /* Get pw_storage mode */
811       if (isset($_POST['pw_storage'])){
812         foreach(array("pw_storage") as $val){
813           if(isset($_POST[$val])){
814             $data= validate($_POST[$val]);
815             if ($data != $this->$val){
816               $this->is_modified= TRUE;
817             }
818             $this->$val= $data;
819           }
820         }
821       }
823       if($this->pw_storage != $this->last_pw_storage && isset($_POST['pw_storage'])){
824         if ($this->acl_is_writeable("userPassword")){
825           $temp= passwordMethod::get_available_methods();
826           if (!is_object($this->pwObject) || !($this->pwObject instanceOf $temp[$this->pw_storage])){
827             foreach($temp as $id => $data){
828               if(isset($data['name']) && $data['name'] == $this->pw_storage && $data['is_configurable']){
829                 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
830                 break;
831               }
832             }
833           }
834         }
835       }
837       /* Save current cn
838        */
839       $this->cn = $this->givenName." ".$this->sn;
840     }
841   }
843   function rebind($ldap, $referral)
844   {
845     $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
846     if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
847       $this->error = "Success";
848       $this->hascon=true;
849       $this->reconnect= true;
850       return (0);
851     } else {
852       $this->error = "Could not bind to " . $credentials['ADMIN'];
853       return NULL;
854     }
855   }
857   
858   /* Save data to LDAP, depending on is_account we save or delete */
859   function save()
860   {
861     /* Only force save of changes .... 
862        If this attributes aren't changed, avoid saving.
863      */
864     if($this->gender=="0") $this->gender ="";
865     if($this->preferredLanguage=="0") $this->preferredLanguage ="";
867     /* First use parents methods to do some basic fillup in $this->attrs */
868     plugin::save ();
870     if ($this->use_dob == "1"){
871       /* If it is an array, the generic page has never been loaded - so there's no difference. Using an array would cause an error btw. */
872       if(!is_array($this->attrs['dateOfBirth'])) {
873         $this->attrs['dateOfBirth'] = date("Y-m-d", $this->dateOfBirth);
874       }
875     }
877     /* Remove additional objectClasses */
878     $tmp= array();
879     foreach ($this->attrs['objectClass'] as $key => $set){
880       $found= false;
881       foreach (array("ivbbentry", "gosaUserTemplate") as $val){
882         if (preg_match ("/^$set$/i", $val)){
883           $found= true;
884           break;
885         }
886       }
887       if (!$found){
888         $tmp[]= $set;
889       }
890     }
892     /* Replace the objectClass array. This is done because of the
893        separation into government and normal mode. */
894     $this->attrs['objectClass']= $tmp;
896     /* Add objectClasss for template mode? */
897     if ($this->is_template){
898       $this->attrs['objectClass'][]= "gosaUserTemplate";
899     }
901     /* Hard coded government mode? */
902     if ($this->governmentmode){
903       $this->attrs['objectClass'][]= "ivbbentry";
905       /* Copy standard attributes */
906       foreach ($this->govattrs as $val){
907         if ($this->$val != ""){
908           $this->attrs["$val"]= $this->$val;
909         } elseif (!$this->is_new) {
910           $this->attrs["$val"]= array();
911         }
912       }
914       /* Remove attribute if set to "nein" */
915       if ($this->publicVisible == "nein"){
916         $this->attrs['publicVisible']= array();
917         if($this->is_new){
918           unset($this->attrs['publicVisible']);
919         }else{
920           $this->attrs['publicVisible']=array();
921         }
923       }
925     }
927     /* Special handling for attribute userCertificate needed */
928     if ($this->userCertificate != ""){
929       $this->attrs["userCertificate;binary"]= $this->userCertificate;
930       $remove_userCertificate= false;
931     } else {
932       $remove_userCertificate= true;
933     }
935     /* Special handling for dateOfBirth value */
936     if ($this->use_dob != "1"){
937       if ($this->is_new) {
938         unset($this->attrs["dateOfBirth"]);
939       } else {
940         $this->attrs["dateOfBirth"]= array();
941       }
942     }
943     if (!$this->gender){
944       if ($this->is_new) {
945         unset($this->attrs["gender"]);
946       } else {
947         $this->attrs["gender"]= array();
948       }
949     }
950     if (!$this->preferredLanguage){
951       if ($this->is_new) {
952         unset($this->attrs["preferredLanguage"]);
953       } else {
954         $this->attrs["preferredLanguage"]= array();
955       }
956     }
958     /* Special handling for attribute jpegPhote needed, scale image via
959        image magick to 147x200 pixels and inject resulting data. */
960     if ($this->jpegPhoto == "*removed*"){
961     
962       /* Reset attribute to avoid writing *removed* as value */    
963       $this->attrs["jpegPhoto"] = array();
965     } else {
967       /* Fallback if there's no image magick inside PHP */
968       if (!function_exists("imagick_blob2image")){
969         /* Get temporary file name for conversation */
970         $fname = tempnam (TEMP_DIR, "GOsa");
971   
972         /* Open file and write out photoData */
973         $fp = fopen ($fname, "w");
974         fwrite ($fp, $this->photoData);
975         fclose ($fp);
977         /* Build conversation query. Filename is generated automatically, so
978            we do not need any special security checks. Exec command and save
979            output. For PHP safe mode, you'll need a configuration which respects
980            image magick as executable... */
981         $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
982         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
983             $query, "Execute");
984   
985         /* Read data written by convert */
986         $output= "";
987         $sh= popen($query, 'r');
988         while (!feof($sh)){
989           $output.= fread($sh, 4096);
990         }
991         pclose($sh);
993         unlink($fname);
995         /* Save attribute */
996         $this->attrs["jpegPhoto"] = $output;
998       } else {
1000         /* Load the new uploaded Photo */
1001         if(!$handle  =  imagick_blob2image($this->photoData))  {
1002           new log("debug","users/".get_class($this),$this->dn,array(),"Could not access uploaded image");
1003         }
1005         /* Resizing image to 147x200 and blur */
1006         if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
1007           new log("debug","users/".get_class($this),$this->dn,array(),"Could not resize uploaded image");
1008         }
1010         /* Converting image to JPEG */
1011         if(!imagick_convert($handle,"JPEG")) {
1012           new log("debug","users/".get_class($this),$this->dn,array(),"Could not convert uploaded image to jepg");
1013         }
1015         /* Creating binary Code for the Image */
1016         if(!$dump = imagick_image2blob($handle)){
1017           new log("debug","users/".get_class($this),$this->dn,array(),"Could not create new user image");
1018         }
1020         /* Sending Image */
1021         $output=  $dump;
1023         /* Save attribute */
1024         $this->attrs["jpegPhoto"] = $output;
1025       }
1027     }
1029     /* This only gets called when user is renaming himself */
1030     $ldap= $this->config->get_ldap_link();
1031     if ($this->dn != $this->new_dn){
1033       /* Write entry on new 'dn' */
1034       $this->update_acls($this->dn,$this->new_dn);
1035       $this->move($this->dn, $this->new_dn);
1037       /* Happen to use the new one */
1038       change_ui_dn($this->dn, $this->new_dn);
1039       $this->dn= $this->new_dn;
1040     }
1043     /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1044        new entries. So do a check first... */
1045     $ldap->cat ($this->dn, array('dn'));
1046     if ($ldap->fetch()){
1047       $mode= "modify";
1048     } else {
1049       $mode= "add";
1050       $ldap->cd($this->config->current['BASE']);
1051       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1052     }
1054     /* Set password to some junk stuff in case of templates */
1055     if ($this->is_template){
1056       $temp= passwordMethod::get_available_methods();
1057       foreach($temp as $id => $data){
1058         if(isset($data['name']) && $data['name'] == $this->pw_storage){
1059           $tmp = new  $temp[$this->pw_storage]($this->config,$this->dn);
1060           $tmp->set_hash($this->pw_storage);
1061           $this->attrs['userPassword'] = $tmp->create_template_hash($this->attrs);
1062           break;
1063         }
1064       }
1065     }
1067     @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
1068         $this->attributes, "Save via $mode");
1070     /* Finally write data with selected 'mode' */
1071     $this->cleanup();
1073     /* Update current locale settings, if we have edited ourselves */
1074     $ui = session::get('ui');
1075     if(isset($this->attrs['preferredLanguage']) && $this->dn == $ui->dn){
1076       $ui->language = $this->preferredLanguage;
1077       session::set('ui',$ui);
1078       session::set('Last_init_lang',"update");
1079     }
1081     $ldap->cd ($this->dn);
1082     $ldap->$mode ($this->attrs);
1083     if (!$ldap->success()){
1084       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
1085       return (1);
1086     }
1088     /* Remove ACL dependencies too */
1089     if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1090       $tmp = new acl($this->config,$this->parent,$this->dn);
1091       $tmp->update_acl_membership($this->orig_dn,$this->dn);
1092     }
1094     if($mode == "modify"){
1095       new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1096     }else{
1097       new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1098     }
1100     /* Remove cert? 
1101        For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
1102        to work around myself. */
1103     if ($remove_userCertificate == true && !$this->is_new && $this->had_userCertificate){
1105       /* Reset array, assemble new, this should be reworked */
1106       $this->attrs= array();
1107       $this->attrs['userCertificate;binary']= array();
1109       /* Prepare connection */
1110       if (!($ds = ldap_connect($this->config->current['SERVER']))) {
1111         die ("Could not connect to LDAP server");
1112       }
1113       ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1114       if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true") {
1115         ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1116         ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1117       }
1118       if($this->config->get_cfg_value("ldapTLS") == "true"){
1119         ldap_start_tls($ds);
1120       }
1121       if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
1122               $this->config->current['PASSWORD']))) {
1123         die ("Could not bind to LDAP");
1124       }
1126       /* Modify using attrs */
1127       ldap_mod_del($ds,$this->dn,$this->attrs);
1128       ldap_close($ds);
1129     }
1131     /* If needed, let the password method do some cleanup */
1132     if ($this->pw_storage != $this->last_pw_storage){
1133       $tmp = new passwordMethod($this->config);
1134       $available = $tmp->get_available_methods();
1135       if (in_array_ics($this->last_pw_storage, $available['name'])){
1136         $test= new $available[$this->last_pw_storage]($this->config,$this->dn);
1137         $test->attrs= $this->attrs;
1138         $test->remove_from_parent();
1139       }
1140     }
1142     /* Maybe the current password method want's to do some changes... */
1143     if (is_object($this->pwObject)){
1144       $this->pwObject->save($this->dn);
1145     }
1147     /* Optionally execute a command after we're done */
1148     if ($mode == "add"){
1149       $this->handle_post_events("add", array("uid" => $this->uid));
1150     } elseif ($this->is_modified){
1151       $this->handle_post_events("modify", array("uid" => $this->uid));
1152     }
1154     return (0);
1155   }
1157   
1158   function update_new_dn()
1159   {
1160     $pt= "";
1161     if($this->config->get_cfg_value("personalTitleInDN") == "true"){
1162       if(!empty($this->personalTitle)){
1163         $pt = $this->personalTitle." ";
1164       }
1165     }
1166     $this->cn= $pt.$this->givenName." ".$this->sn;
1168     /* Permissions for that base? */
1169     if ($this->config->get_cfg_value("accountPrimaryAttribute") == "uid"){
1170       $this->new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
1171     } else {
1172       /* Don't touch dn, if cn hasn't changed */
1173       if (isset($this->saved_attributes['cn']) && $this->saved_attributes['cn'] == $this->cn &&
1174           $this->orig_base == $this->base ){
1175         $this->new_dn= $this->dn;
1176       } else {
1177         $this->new_dn= $this->create_unique_dn('cn', get_people_ou().$this->base);
1178       }
1179     }
1180   }
1181   
1183   /* Check formular input */
1184   function check()
1185   {
1186     /* Call common method to give check the hook */
1187     $message= plugin::check();
1189     /* Configurable password methods should be configured initially. 
1190      */ 
1191     if($this->last_pw_storage != $this->pw_storage){
1192       $temp= passwordMethod::get_available_methods();
1193       foreach($temp['name'] as $id => $name){
1194         if($name == $this->pw_storage){
1195           if($temp['is_configurable'][$id] && !$this->pwObject instanceof $temp[$name] ){
1196             $message[] = _("The selected password method requires initial configuration!");
1197           }
1198           break;
1199         }
1200       }
1201     }
1203     $this->update_new_dn();
1205     /* Set the new acl base */
1206     if($this->dn == "new") {
1207       $this->set_acl_base($this->base);
1208     }
1210     /* Check if we are allowed to create/move this user 
1211      */
1212     
1213     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1214       $message[]= msgPool::permCreate();
1215     }elseif($this->orig_dn != "new" && $this->new_dn != $this->orig_dn && !$this->acl_is_moveable($this->base)){
1216       $message[]= msgPool::permMove();
1217     }
1219     /* UID already used? */
1220     $ldap= $this->config->get_ldap_link();
1221     $ldap->cd($this->config->current['BASE']);
1222     $ldap->search("(uid=$this->uid)", array("uid"));
1223     $ldap->fetch();
1224     if ($ldap->count() != 0 && $this->dn == 'new'){
1225       $message[]= msgPool::duplicated(_("Login"));
1226     }
1228     /* In template mode, the uid and givenName are autogenerated... */
1229     if ($this->sn == ""){
1230       $message[]= msgPool::required(_("Name"));
1231     }
1233     if (!$this->is_template){
1234       if ($this->givenName == ""){
1235         $message[]= msgPool::required(_("Given name"));
1236       }
1237       if ($this->uid == ""){
1238         $message[]= msgPool::required(_("Login"));
1239       }
1240       if ($this->config->get_cfg_value("accountPrimaryAttribute") != "uid"){
1241         $ldap->cat($this->new_dn);
1242         if ($ldap->count() != 0 && $this->dn != $this->new_dn && $this->dn == 'new'){
1243           $message[]= msgPool::duplicated(_("Name"));
1244         }
1245       }
1246     }
1248     /* Check for valid input */
1249     if ($this->is_modified && !tests::is_uid($this->uid)){
1251       if (strict_uid_mode()){
1252         $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/");
1253       } else {
1254         $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/i");
1255       }
1256     }
1257     if (!tests::is_url($this->labeledURI)){
1258       $message[]= msgPool::invalid(_("Homepage"), "", "", "http://www.your-domain.com/yourname");
1259     }
1261     /* Check phone numbers */
1262     if (!tests::is_phone_nr($this->telephoneNumber)){
1263       $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1264     }
1265     if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
1266       $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1267     }
1268     if (!tests::is_phone_nr($this->mobile)){
1269       $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1270     }
1271     if (!tests::is_phone_nr($this->pager)){
1272       $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1273     }
1275     /* Check for reserved characers */
1276     if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName)){
1277       $message[]= msgPool::invalid(_("Given name"), $this->givenName, '/[^,+"?\'()=<>;\\\\]/');
1278     }
1279     if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn)){
1280       $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1281     }
1283     return $message;
1284   }
1287   /* Indicate whether a password change is needed or not */
1288   function password_change_needed()
1289   {
1290     if(in_array("pw_storage",$this->multi_boxes)){
1291       return(TRUE);
1292     }
1293     return($this->pw_storage != $this->last_pw_storage && !$this->is_template);
1294   }
1297   /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
1298   function load_picture()
1299   {
1300     $ldap = $this->config->get_ldap_link();
1301     $ldap->cd ($this->dn);
1302     $data = $ldap->get_attribute($this->dn,"jpegPhoto");
1304     if((!$data) || ($data == "*removed*")){ 
1306       /* In case we don't get an entry, load a default picture */
1307       $this->set_picture ();
1308       $this->jpegPhoto= "*removed*";
1309     }else{
1311       /* Set picture */
1312       $this->photoData= $data;
1313       session::set('binary',$this->photoData);
1314       session::set('binarytype',"image/jpeg");
1315       $this->jpegPhoto= "";
1316     }
1317   }
1320   /* Load a certificate from LDAP, this is going to be simplified later on */
1321   function load_cert()
1322   {
1323     $ds= ldap_connect($this->config->current['SERVER']);
1324     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1325     if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true"){
1326       ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1327       ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1328     }
1329     if ($this->config->get_cfg_value("ldapTLS") == "true"){
1330       ldap_start_tls($ds);
1331     }
1333     $r= ldap_bind($ds);
1334     $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1336     if ($sr) {
1337       $ei= @ldap_first_entry($ds, $sr);
1338       
1339       if ($ei) {
1340         if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1341           $this->userCertificate= "";
1342         } else {
1343           $this->userCertificate= $info[0];
1344         }
1345       }
1346     } else {
1347       $this->userCertificate= "";
1348     }
1350     ldap_unbind($ds);
1351   }
1354   /* Load picture from file to object */
1355   function set_picture($filename ="")
1356   {
1357     if (!is_file($filename) || $filename =="" ){
1358       $filename= "./plugins/users/images/default.jpg";
1359       $this->jpegPhoto= "*removed*";
1360     }
1362     $fd = fopen ($filename, "rb");
1363     $this->photoData= fread ($fd, filesize ($filename));
1364     session::set('binary',$this->photoData);
1365     session::set('binarytype',"image/jpeg");
1366     $this->jpegPhoto= "";
1368     fclose ($fd);
1369   }
1372   /* Load certificate from file to object */
1373   function set_cert($cert, $filename)
1374   {
1375     if(!$this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))) return;
1376     $fd = fopen ($filename, "rb");
1377     if (filesize($filename)>0) {
1378       $this->$cert= fread ($fd, filesize ($filename));
1379       fclose ($fd);
1380       $this->is_modified= TRUE;
1381     } else {
1382       msg_dialog::display(_("Error"), _("Cannot open certificate!"), ERROR_DIALOG);
1383     }
1384   }
1386   /* Adapt from given 'dn' */
1387   function adapt_from_template($dn, $skip= array())
1388   {
1389     plugin::adapt_from_template($dn, $skip);
1391     /* Get password method from template 
1392      */
1393     $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
1394     if(is_object($tmp)){
1395       if($tmp->is_configurable()){
1396         $tmp->adapt_from_template($dn);
1397         $this->pwObject = &$tmp;
1398       }
1399       $this->pw_storage= $tmp->get_hash();
1400     }
1402     /* Get base */
1403     $this->base= preg_replace('/^[^,]+,'.get_people_ou().'/i', '', $dn);
1405     if($this->governmentmode){
1407       /* Walk through govattrs */
1408       foreach ($this->govattrs as $val){
1410         if (in_array($val, $skip)){
1411           continue;
1412         }
1414         if (isset($this->attrs["$val"][0])){
1416           /* If attribute is set, replace dynamic parts: 
1417              %sn, %givenName and %uid. Fill these in our local variables. */
1418           $value= $this->attrs["$val"][0];
1420           foreach (array("sn", "givenName", "uid") as $repl){
1421             if (preg_match("/%$repl/i", $value)){
1422               $value= preg_replace ("/%$repl/i",
1423                   $this->parent->$repl, $value);
1424             }
1425           }
1426           $this->$val= $value;
1427         }
1428       }
1429     }
1431     /* Get back uid/sn/givenName - only write if nothing's skipped */
1432     if ($this->parent !== NULL && count($skip) == 0){
1433       $this->uid= $this->parent->uid;
1434       $this->sn= $this->parent->sn;
1435       $this->givenName= $this->parent->givenName;
1436     }
1437   }
1439  
1440   /* This avoids that users move themselves out of their rights. 
1441    */
1442   function allowedBasesToMoveTo()
1443   {
1444     /* Get bases */
1445     $bases  = $this->get_allowed_bases();
1446     return($bases);
1447   } 
1450   function getCopyDialog()
1451   {
1452     $str = "";
1454     session::set('binary',$this->photoData); 
1455     session::set('binarytype',"image/jpeg");
1457     /* Get random number for pictures */
1458     srand((double)microtime()*1000000); 
1459     $rand = rand(0, 10000);
1461     $smarty = get_smarty();
1463     $smarty->assign("passwordTodo","clear");
1465     if(isset($_POST['passwordTodo'])){
1466       $smarty->assign("passwordTodo",$_POST['passwordTodo']);
1467     }
1469     $smarty->assign("sn",       $this->sn);
1470     $smarty->assign("givenName",$this->givenName);
1471     $smarty->assign("uid",      $this->uid);
1472     $smarty->assign("rand",     $rand);
1473     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1476     $ret = array();
1477     $ret['string'] = $str;
1478     $ret['status'] = "";  
1479     return($ret);
1480   }
1482   function saveCopyDialog()
1483   {
1484     /* Set_acl_base */
1485     $this->set_acl_base($this->base);
1487     if((isset($_FILES['picture_file']['tmp_name'])) && ($_FILES['picture_file']['size'] > 0)){
1488       $this->set_picture($_FILES['picture_file']['tmp_name']);
1489     }
1491     /* Remove picture? */
1492     if (isset($_POST['picture_remove'])){
1493       $this->jpegPhoto= "*removed*";
1494       $this->set_picture ("./plugins/users/images/default.jpg");
1495       $this->is_modified= TRUE;
1496     }
1498     $attrs = array("uid","givenName","sn");
1499     foreach($attrs as $attr){
1500       if(isset($_POST[$attr])){
1501         $this->$attr = $_POST[$attr];
1502       }
1503     } 
1504   }
1507   function PrepareForCopyPaste($source)
1508   {
1509     plugin::PrepareForCopyPaste($source);
1511     /* Reset certificate information addepted from source user
1512        to avoid setting the same user certificate for the destination user. */
1513     $this->userPKCS12= "";
1514     $this->userSMIMECertificate= "";
1515     $this->userCertificate= "";
1516     $this->certificateSerialNumber= "";
1517     $this->old_certificateSerialNumber= "";
1518     $this->old_userPKCS12= "";
1519     $this->old_userSMIMECertificate= "";
1520     $this->old_userCertificate= "";
1521   }
1524   static function plInfo()
1525   {
1526   
1527     $govattrs= array(
1528         "gouvernmentOrganizationalUnit"             =>  _("Unit"), 
1529         "houseIdentifier"                           =>  _("House identifier"), 
1530         "vocation"                                  =>  _("Vocation"),
1531         "ivbbLastDeliveryCollective"                =>  _("Last delivery"), 
1532         "gouvernmentOrganizationalPersonLocality"   =>  _("Person locality"),
1533         "gouvernmentOrganizationalUnitDescription"  =>  _("Unit description"),
1534         "gouvernmentOrganizationalUnitSubjectArea"  =>  _("Subject area"),
1535         "functionalTitle"                           =>  _("Functional title"),
1536         "certificateSerialNumber"                   =>  _("Certificate serial number"),
1537         "publicVisible"                             =>  _("Public visible"),
1538         "street"                                    =>  _("Street"),
1539         "role"                                      =>  _("Role"),
1540         "postalCode"                                =>  _("Postal code"));
1542     $ret = array(
1543         "plShortName" => _("Generic"),
1544         "plDescription" => _("Generic user settings"),
1545         "plSelfModify"  => TRUE,
1546         "plDepends"     => array(),
1547         "plPriority"    => 1,
1548         "plSection"     => array("personal" => _("My account")),
1549         "plCategory"    => array("users" => array("description" => _("Users"),
1550                                                   "objectClass" => "gosaAccount")),
1552         "plProvidedAcls" => array(
1554           "sn"                => _("Surname"),
1555           "givenName"         => _("Given name"),
1556           "uid"               => _("User identification"),
1557           "personalTitle"     => _("Personal title"),
1558           "academicTitle"     => _("Academic title"),
1560           "dateOfBirth"       => _("Date of birth"),
1561           "gender"            => _("Gender"),
1562           "preferredLanguage" => _("Preferred language"),
1563           "base"              => _("Base"), 
1565           "userPicture"       => _("User picture"),
1567           "o"                 => _("Organization"),
1568           "ou"                => _("Department"),
1569           "departmentNumber"  => _("Department number"),
1570           "employeeNumber"    => _("Employee number"),
1571           "employeeType"      => _("Employee type"),
1573           "roomNumber"        => _("Room number"),
1574           "telephoneNumber"   => _("Telefon number"),
1575           "pager"             => _("Pager number"),
1576           "mobile"            => _("Mobile number"),
1577           "facsimileTelephoneNumber"     => _("Fax number"),
1579           "st"                => _("State"),
1580           "l"                 => _("Location"),
1581           "postalAddress"     => _("Postal address"),
1583           "homePostalAddress" => _("Home postal address"),
1584           "homePhone"         => _("Home phone number"),
1585           "labeledURI"        => _("Homepage"),
1586           "userPassword"      => _("User password method"), 
1587           "Certificate"       => _("User certificates"))
1589         );
1591     /* Append government attributes if required */
1592     global $config;
1593     if($config->get_cfg_value("honourIvbbAttributes") == "true"){
1594       foreach($govattrs as $attr => $desc){
1595         $ret["plProvidedAcls"][$attr] = $desc;
1596       }
1597     }
1598     return($ret);
1599   }
1601   function get_multi_edit_values()
1602   {
1603     $ret = plugin::get_multi_edit_values();
1604     if(in_array("pw_storage",$this->multi_boxes)){
1605       $ret['pw_storage'] = $this->pw_storage;
1606     }
1607     if(in_array("edit_picture",$this->multi_boxes)){
1608       $ret['jpegPhoto'] = $this->jpegPhoto;
1609       $ret['photoData'] = $this->photoData;
1610       $ret['old_jpegPhoto'] = $this->old_jpegPhoto;
1611       $ret['old_photoData'] = $this->old_photoData;
1612     }
1613     if(isset($ret['dateOfBirth'])){
1614       unset($ret['dateOfBirth']);
1615     }
1616     if(isset($ret['cn'])){
1617       unset($ret['cn']);
1618     }
1619     $ret['is_modified'] = $this->is_modified;
1620     if(in_array("base",$this->multi_boxes)){
1621       $ret['orig_base']="Changed_by_Multi_Plug";
1622       $ret['base']=$this->base;
1623     }
1624     return($ret); 
1625   }
1628   function multiple_save_object()
1629   {
1630     plugin::multiple_save_object();
1632     /* Get pw_storage mode */
1633     if (isset($_POST['pw_storage'])){
1634       foreach(array("pw_storage") as $val){
1635         if(isset($_POST[$val])){
1636           $data= validate(get_post($val));
1637           if ($data != $this->$val){
1638             $this->is_modified= TRUE;
1639           }
1640           $this->$val= $data;
1641         }
1642       }
1643     }
1644     if(isset($_POST['base'])){
1645       $this->base = get_post('base');
1646     }
1648     if(isset($_POST['user_mulitple_edit'])){
1649       foreach(array("base","pw_storage","edit_picture") as $val){
1650         if(isset($_POST["use_".$val])){
1651           $this->multi_boxes[] = $val;
1652         }
1653       }
1654     }
1655   }
1657   
1658   function multiple_check()
1659   {
1660     /* Call check() to set new_dn correctly ... */
1661     $message = plugin::multiple_check();
1663     /* Set the new acl base */
1664     if($this->dn == "new") {
1665       $this->set_acl_base($this->base);
1666     }
1667     if (!tests::is_url($this->labeledURI) && in_array("labeledURI",$this->multi_boxes)){
1668       $message[]= msgPool::invalid(_("Homepage"));
1669     }
1670     if (!tests::is_phone_nr($this->telephoneNumber) && in_array("telephoneNumber",$this->multi_boxes)){
1671       $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1672     }
1673     if (!tests::is_phone_nr($this->facsimileTelephoneNumber) &&  in_array("facsimileTelephoneNumber",$this->multi_boxes)){
1674       $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1675     }
1676     if (!tests::is_phone_nr($this->mobile) && in_array("mobile",$this->multi_boxes)){
1677       $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1678     }
1679     if (!tests::is_phone_nr($this->pager) && in_array("pager",$this->multi_boxes)){
1680       $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1681     }
1682     if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName) && in_array("givenName",$this->multi_boxes)){
1683       $message[]= msgPool::invalid(_("Given name"), $this->giveName, '/[^,+"?\'()=<>;\\\\]/');
1684     }
1685     if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn) && in_array("sn",$this->multi_boxes)){
1686       $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1687     }
1688     return($message);
1689   }
1693   function multiple_execute()
1694   {
1695     return($this->execute());
1696   }
1701 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1702 ?>