1 <?php
2 /*!
3 \brief user plugin
4 \author Cajus Pollmeier <pollmeier@gonicus.de>
5 \version 2.00
6 \date 24.07.2003
8 This class provides the functionality to read and write all attributes
9 relevant for person, organizationalPerson, inetOrgPerson and gosaAccount
10 from/to the LDAP. It does syntax checking and displays the formulars required.
11 */
13 class user extends plugin
14 {
15 /* Definitions */
16 var $plHeadline= "Generic";
17 var $plDescription= "This does something";
19 /* CLI vars */
20 var $cli_summary= "Handling of GOsa's user base object";
21 var $cli_description= "Some longer text\nfor help";
22 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
24 /* Plugin specific values */
25 var $base= "";
26 var $cn= "";
27 var $personalTitle= "";
28 var $academicTitle= "";
29 var $homePostalAddress= "";
30 var $homePhone= "";
31 var $labeledURI= "";
32 var $o= "";
33 var $ou= "";
34 var $departmentNumber= "";
35 var $employeeNumber= "";
36 var $employeeType= "";
37 var $roomNumber= "";
38 var $telephoneNumber= "";
39 var $facsimileTelephoneNumber= "";
40 var $mobile= "";
41 var $pager= "";
42 var $l= "";
43 var $st= "";
44 var $postalAddress= "";
45 var $dateOfBirth;
46 var $use_dob= "0";
47 var $gender="0";
48 var $preferredLanguage="0";
50 var $jpegPhoto= "*removed*";
51 var $photoData= "";
52 var $old_jpegPhoto= "";
53 var $old_photoData= "";
54 var $cert_dialog= FALSE;
55 var $picture_dialog= FALSE;
57 var $userPKCS12= "";
58 var $userSMIMECertificate= "";
59 var $userCertificate= "";
60 var $certificateSerialNumber= "";
61 var $old_certificateSerialNumber= "";
62 var $old_userPKCS12= "";
63 var $old_userSMIMECertificate= "";
64 var $old_userCertificate= "";
66 var $gouvernmentOrganizationalUnit= "";
67 var $houseIdentifier= "";
68 var $street= "";
69 var $postalCode= "";
70 var $vocation= "";
71 var $ivbbLastDeliveryCollective= "";
72 var $gouvernmentOrganizationalPersonLocality= "";
73 var $gouvernmentOrganizationalUnitDescription= "";
74 var $gouvernmentOrganizationalUnitSubjectArea= "";
75 var $functionalTitle= "";
76 var $role= "";
77 var $publicVisible= "";
79 var $dialog;
81 /* variables to trigger password changes */
82 var $pw_storage= "crypt";
83 var $last_pw_storage= "unset";
84 var $had_userCertificate= FALSE;
86 var $MyAccount_mode = false;
87 var $edit_mode = false;
89 /* attribute list for save action */
90 var $attributes= array("sn", "givenName", "uid", "personalTitle", "academicTitle",
91 "homePostalAddress", "homePhone", "labeledURI", "o", "ou", "dateOfBirth", "gender","preferredLanguage",
92 "departmentNumber", "employeeNumber", "employeeType", "l", "st",
93 "roomNumber", "telephoneNumber", "mobile", "pager", "cn", "userPKCS12",
94 "postalAddress", "facsimileTelephoneNumber", "userSMIMECertificate");
96 var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson",
97 "gosaAccount");
99 /* attributes that are part of the government mode */
100 var $govattrs= array("gouvernmentOrganizationalUnit", "houseIdentifier", "vocation",
101 "ivbbLastDeliveryCollective", "gouvernmentOrganizationalPersonLocality",
102 "gouvernmentOrganizationalUnitDescription","gouvernmentOrganizationalUnitSubjectArea",
103 "functionalTitle", "certificateSerialNumber", "publicVisible", "street", "role",
104 "postalCode");
107 /* constructor, if 'dn' is set, the node loads the given
108 'dn' from LDAP */
109 function user ($config, $dn= NULL)
110 {
111 $this->config= $config;
112 /* Configuration is fine, allways */
113 if ($this->config->current['GOVERNMENTMODE']){
114 $this->attributes=array_merge($this->attributes,$this->govattrs);
115 }
117 /* Load base attributes */
118 plugin::plugin ($config, $dn);
120 if ($this->config->current['GOVERNMENTMODE']){
121 /* Fix public visible attribute if unset */
122 if (!isset($this->attrs['publicVisible'])){
123 $this->publicVisible == "nein";
124 }
125 }
127 /* Load government mode attributes */
128 if ($this->config->current['GOVERNMENTMODE']){
129 /* Copy all attributs */
130 foreach ($this->govattrs as $val){
131 if (isset($this->attrs["$val"][0])){
132 $this->$val= $this->attrs["$val"][0];
133 }
134 }
135 }
137 /* Create me for new accounts */
138 if ($dn == "new"){
139 $this->is_account= TRUE;
140 }
142 /* Make hash default to md5 if not set in config */
143 if (!isset($this->config->current['HASH'])){
144 $hash= "md5";
145 } else {
146 $hash= $this->config->current['HASH'];
147 }
149 /* Load data from LDAP? */
150 if ($dn != NULL){
152 /* Do base conversation */
153 if ($this->dn == "new"){
154 $ui= get_userinfo();
155 $this->base= dn2base($ui->dn);
156 } else {
157 $this->base= dn2base($dn);
158 }
160 echo $this->base;
162 /* get password storage type */
163 if (isset ($this->attrs['userPassword'][0])){
164 /* Initialize local array */
165 $matches= array();
166 if (preg_match ("/^{([^}]+)}(.+)/", $this->attrs['userPassword'][0], $matches)){
167 $this->pw_storage= strtolower($matches[1]);
168 } else {
169 if ($this->attrs['userPassword'][0] != ""){
170 $this->pw_storage= "clear";
171 } else {
172 $this->pw_storage= $hash;
173 }
174 }
175 } else {
176 /* Preset with vaule from configuration */
177 $this->pw_storage= $hash;
178 }
180 /* Load extra attributes: certificate and picture */
181 $this->load_picture();
182 $this->load_cert();
183 if ($this->userCertificate != ""){
184 $this->had_userCertificate= TRUE;
185 }
186 }
188 /* Reset password storage indicator, used by password_change_needed() */
189 if ($dn == "new"){
190 $this->last_pw_storage= "unset";
191 } else {
192 $this->last_pw_storage= $this->pw_storage;
193 }
195 /* Generate dateOfBirth entry */
196 if (isset ($this->attrs['dateOfBirth'])){
197 /* This entry is ISO 8601 conform */
198 list($year, $month, $day)= split("-", $this->attrs['dateOfBirth'][0], 3);
200 $this->dateOfBirth=array( 'mon'=> $month,"mday"=> $day,"year"=> $year);
201 $this->use_dob= "1";
202 } else {
203 $this->use_dob= "0";
204 }
206 /* Put gender attribute to upper case */
207 if (isset ($this->attrs['gender'])){
208 $this->gender= strtoupper($this->attrs['gender'][0]);
209 }
210 }
213 /* execute generates the html output for this node */
214 function execute()
215 {
216 /* Call parent execute */
217 plugin::execute();
219 $smarty= get_smarty();
221 /* Fill calendar */
222 if ($this->dateOfBirth == "0"){
223 $date= getdate();
224 } else {
225 if(is_array($this->dateOfBirth)){
226 $date = $this->dateOfBirth;
227 }else{
228 $date = getdate($this->dateOfBirth);
229 }
230 }
232 $days= array();
233 for($d= 1; $d<32; $d++){
234 $days[$d]= $d;
235 }
236 $years= array();
238 if(($date['year']-100)<1901){
239 $start = 1901;
240 }else{
241 $start = $date['year']-100;
242 }
244 $end = $start +100;
246 for($y= $start; $y<=$end; $y++){
247 $years[]= $y;
248 }
249 $years['-']= "- ";
250 $months= array(_("January"), _("February"), _("March"), _("April"),
251 _("May"), _("June"), _("July"), _("August"), _("September"),
252 _("October"), _("November"), _("December"), '-' => '- ');
253 $smarty->assign("day", $date["mday"]);
254 $smarty->assign("days", $days);
255 $smarty->assign("months", $months);
256 $smarty->assign("month", $date["mon"]-1);
257 $smarty->assign("years", $years);
258 $smarty->assign("year", $date["year"]);
260 /* Assign sex */
261 $sex= array(0 => " ", "F" => _("female"), "M" => _("male"));
262 $smarty->assign("gender_list", $sex);
264 /* Assign prefered langage */
265 $language= array(0 => " ", "fr_FR" => ("fr_FR"), "en_EN" => ("en_EN"),
266 "de_DE" => ("de_DE"), "it_IT" => ("it_IT"),
267 "nl_NL" => ("nl_NL"), "ru_RU" => ("ru_RU"));
268 $smarty->assign("preferredLanguage_list", $language);
270 /* Get random number for pictures */
271 srand((double)microtime()*1000000);
272 $smarty->assign("rand", rand(0, 10000));
274 /* Do we represent a valid gosaAccount? */
275 if (!$this->is_account){
276 echo "<img alt=\"\" src=\"images/stop.png\" align=\"middle\"> <b>".
277 _("This account has no valid GOsa extensions.")."</b>";
278 return;
279 }
281 /* Base select dialog */
282 $once = true;
283 foreach($_POST as $name => $value){
284 if(preg_match("/^chooseBase/",$name) && $once){
285 $once = false;
286 $this->dialog = new baseSelectDialog($this->config,$this,$this->allowedBasesToMoveTo());
287 $this->dialog->setCurrentBase($this->base);
288 }
289 }
291 /* Dialog handling */
292 if(is_object($this->dialog)){
293 /* Must be called before save_object */
294 $this->dialog->save_object();
296 if($this->dialog->isClosed()){
297 $this->dialog = false;
298 }elseif($this->dialog->isSelected()){
299 $this->base = $this->dialog->isSelected();
300 $this->dialog= false;
301 }else{
302 return($this->dialog->execute());
303 }
304 }
306 /* Want picture edit dialog? */
307 if($this->acl_is_readable("userPicture")) {
308 if (isset($_POST['edit_picture'])){
309 /* Save values for later recovery, in case some presses
310 the cancel button. */
311 $this->old_jpegPhoto= $this->jpegPhoto;
312 $this->old_photoData= $this->photoData;
313 $this->picture_dialog= TRUE;
314 $this->dialog= TRUE;
315 }
316 }
318 /* Remove picture? */
319 if($this->acl_is_writeable("userPicture",($this->MyAccount_mode && !$this->edit_mode))) {
320 if (isset($_POST['picture_remove'])){
321 $this->jpegPhoto= "*removed*";
322 $this->set_picture ("./images/default.jpg");
323 $this->is_modified= TRUE;
325 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
326 }
327 }
329 /* Save picture */
330 if (isset($_POST['picture_edit_finish'])){
332 /* Check for clean upload */
333 if ($_FILES['picture_file']['name'] != ""){
334 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
335 print_red(_("The specified file has not been uploaded via HTTP POST! Aborted."));
336 exit;
337 }
339 /* Activate new picture */
340 $this->set_picture($_FILES['picture_file']['tmp_name']);
341 }
342 $this->picture_dialog= FALSE;
343 $this->dialog= FALSE;
344 $this->is_modified= TRUE;
345 }
348 /* Cancel picture */
349 if (isset($_POST['picture_edit_cancel'])){
351 /* Restore values */
352 $this->jpegPhoto= $this->old_jpegPhoto;
353 $this->photoData= $this->old_photoData;
355 /* Update picture */
356 $_SESSION['binary']= $this->photoData;
357 $_SESSION['binarytype']= "image/jpeg";
358 $this->picture_dialog= FALSE;
359 $this->dialog= FALSE;
360 }
362 /* Toggle dateOfBirth information */
363 if (isset($_POST['set_dob'])){
364 $this->use_dob= ($this->use_dob == "0")?"1":"0";
365 }
368 /* Want certificate= */
369 if ((isset($_POST['edit_cert'])) && $this->acl_is_readable("Certificate")){
371 /* Save original values for later reconstruction */
372 foreach (array("certificateSerialNumber", "userCertificate",
373 "userSMIMECertificate", "userPKCS12") as $val){
375 $oval= "old_$val";
376 $this->$oval= $this->$val;
377 }
379 $this->cert_dialog= TRUE;
380 $this->dialog= TRUE;
381 }
384 /* Cancel certificate dialog */
385 if (isset($_POST['cert_edit_cancel'])){
387 /* Restore original values in case of 'cancel' */
388 foreach (array("certificateSerialNumber", "userCertificate",
389 "userSMIMECertificate", "userPKCS12") as $val){
391 $oval= "old_$val";
392 $this->$val= $this->$oval;
393 }
394 $this->cert_dialog= FALSE;
395 $this->dialog= FALSE;
396 }
399 /* Remove certificate? */
400 if($this->acl_is_writeable("Certificate",($this->MyAccount_mode && !$this->edit_mode))){
401 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
402 if (isset($_POST["remove_$val"])){
404 /* Reset specified cert*/
405 $this->$val= "";
406 $this->is_modified= TRUE;
407 }
408 }
409 }
411 /* Upload new cert and close dialog? */
412 if($this->acl_is_writeable("Certificate",($this->MyAccount_mode && !$this->edit_mode))){
413 if (isset($_POST['cert_edit_finish'])){
415 /* for all certificates do */
416 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
417 as $val){
419 /* Check for clean upload */
420 if (array_key_exists($val."_file", $_FILES) &&
421 array_key_exists('name', $_FILES[$val."_file"]) &&
422 $_FILES[$val."_file"]['name'] != "" &&
423 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
424 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
425 }
426 }
428 /* Save serial number */
429 if (isset($_POST["certificateSerialNumber"]) &&
430 $_POST["certificateSerialNumber"] != ""){
432 if (!is_id($_POST["certificateSerialNumber"])){
433 print_red (_("Please enter a valid serial number"));
435 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
436 if ($this->$cert != ""){
437 $smarty->assign("$cert"."_state", "true");
438 } else {
439 $smarty->assign("$cert"."_state", "");
440 }
441 }
442 return ($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
443 }
445 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
446 $this->is_modified= TRUE;
447 }
449 $this->cert_dialog= FALSE;
450 $this->dialog= FALSE;
451 }
452 }
453 /* Display picture dialog */
454 if ($this->picture_dialog){
455 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
456 }
458 /* Display cert dialog */
459 if ($this->cert_dialog){
460 $smarty->assign("CertificateACL",$this->getacl("Certificate",($this->MyAccount_mode && !$this->edit_mode)));
461 $smarty->assign("Certificate_readable",$this->acl_is_readable("Certificate"));
463 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
464 if ($this->$cert != ""){
465 /* import certificate */
466 $certificate = new certificate;
467 $certificate->import($this->$cert);
469 /* Read out data*/
470 $timeto = $certificate->getvalidto_date();
471 $timefrom = $certificate->getvalidfrom_date();
472 $str = "<table summary=\"\" border=0><tr><td style='vertical-align:top'>CN</td><td>".preg_replace("/ /", " ", $certificate->getname())."</td></tr></table><br>".
473 sprintf(_("Certificate is valid from %s to %s and is currently %s."), "<b>".date('d M Y',$timefrom)."</b>","<b>".date('d M Y',$timeto)."</b>", $certificate->isvalid()?"<b><font style='color:green'>"._("valid")."</font></b>":"<b><font style='color:red'>"._("invalid")."</font></b>");
474 $smarty->assign($cert."info",$str);
475 $smarty->assign($cert."_state","true");
476 } else {
477 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
478 $smarty->assign($cert."_state","");
479 }
480 }
481 $smarty->assign("governmentmode", "false");
482 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
483 }
485 /* Prepare password hashes */
486 if ($this->pw_storage == ""){
487 $this->pw_storage= $this->config->current['HASH'];
488 }
490 $temp = @passwordMethod::get_available_methods();
491 $hashes = $temp['name'];
493 /* Load attributes and acl's */
494 $ui =get_userinfo();
495 foreach($this->attributes as $val){
496 $smarty->assign("$val", $this->$val);
497 $smarty->assign("$val"."ACL", $this->getacl($val,($this->MyAccount_mode && !$this->edit_mode)));
498 }
500 $smarty->assign("pwmode", $hashes);
501 $smarty->assign("pwmode_select", $this->pw_storage);
502 $smarty->assign("passwordStorageACL", $this->getacl("userPassword",($this->MyAccount_mode && !$this->edit_mode)));
503 $smarty->assign("base_select", $this->base);
504 $smarty->assign("CertificatesACL", $this->getacl("Certificate",($this->MyAccount_mode && !$this->edit_mode)));
505 $smarty->assign("userPictureACL", $this->getacl("userPicture",($this->MyAccount_mode && !$this->edit_mode)));
506 $smarty->assign("userPicture_is_readable", $this->acl_is_readable("userPicture",($this->MyAccount_mode && !$this->edit_mode)));
508 /* Create base acls */
509 $baseACL = $this->getacl("base",($this->MyAccount_mode && !$this->edit_mode));
510 if(!$this->acl_is_moveable()) {
511 $baseACL = preg_replace("/w/","",$baseACL);
512 }
513 $smarty->assign("baseACL", $baseACL);
515 /* Show us the edit screen */
516 @$smarty->assign("bases", $this->allowedBasesToMoveTo());
517 # $smarty->assign("bases", $this->config->idepartments);
520 /* Save government mode attributes */
521 if (isset($this->config->current['GOVERNMENTMODE']) &&
522 preg_match('/true/i', $this->config->current['GOVERNMENTMODE'])){
523 $smarty->assign("governmentmode", "true");
524 $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
525 "internet,ivbv", "internet,testa", "internet,ivbv,testa");
526 $smarty->assign("ivbbmodes", $ivbbmodes);
527 foreach ($this->govattrs as $val){
528 $smarty->assign("$val", $this->$val);
529 $smarty->assign("$val"."ACL", $this->getacl($val,($this->MyAccount_mode && !$this->edit_mode)));
530 }
531 } else {
532 $smarty->assign("governmentmode", "false");
533 }
535 /* Special mode for uid */
536 $uidACL= $this->getacl("uid",($this->MyAccount_mode && !$this->edit_mode));
537 if (isset ($this->dn)){
538 if ($this->dn != "new"){
539 $uidACL= preg_replace("/w/","",$uidACL);
540 }
541 } else {
542 $uidACL= preg_replace("/w/","",$uidACL);
543 }
545 $smarty->assign("uidACL", $uidACL);
546 $smarty->assign("is_template", $this->is_template);
547 $smarty->assign("use_dob", $this->use_dob);
549 if (isset($this->parent)){
550 if (isset($this->parent->by_object['phoneAccount']) &&
551 $this->parent->by_object['phoneAccount']->is_account){
552 $smarty->assign("has_phoneaccount", "true");
553 } else {
554 $smarty->assign("has_phoneaccount", "false");
555 }
556 } else {
557 $smarty->assign("has_phoneaccount", "false");
558 }
559 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
560 }
563 /* remove object from parent */
564 function remove_from_parent()
565 {
566 $ldap= $this->config->get_ldap_link();
567 $ldap->rmdir ($this->dn);
568 show_ldap_error($ldap->get_error(), sprintf(_("Removing of user/generic account with dn '%s' failed."),$this->dn));
570 /* Delete references to groups */
571 $ldap->cd ($this->config->current['BASE']);
572 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
573 while ($ldap->fetch()){
574 $g= new group($this->config, $ldap->getDN());
575 $g->removeUser($this->uid);
576 $g->save ();
577 }
579 /* Delete references to object groups */
580 $ldap->cd ($this->config->current['BASE']);
581 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
582 while ($ldap->fetch()){
583 $og= new ogroup($this->config, $ldap->getDN());
584 unset($og->member[$this->dn]);
585 $og->save ();
586 }
588 /* Optionally execute a command after we're done */
589 $this->handle_post_events("remove");
590 }
593 /* Save data to object */
594 function save_object()
595 {
596 if (isset($_POST['generic'])){
598 /* Parents save function */
599 plugin::save_object ();
601 /* Save government mode attributes */
602 if ($this->config->current['GOVERNMENTMODE']){
603 foreach ($this->govattrs as $val){
604 if ($this->acl_is_writeable($val,($this->MyAccount_mode && !$this->edit_mode)) && isset ($_POST["$val"])){
605 $data= stripcslashes($_POST["$val"]);
606 if ($data != $this->$val){
607 $this->is_modified= TRUE;
608 }
609 $this->$val= $data;
610 }
611 }
612 }
614 /* In template mode, the uid is autogenerated... */
615 if ($this->is_template){
616 $this->uid= strtolower($this->sn);
617 $this->givenName= $this->sn;
618 }
620 /* Save base and pw_storage, since these are no LDAP attributes */
621 if (isset($_POST['base'])){
622 foreach(array("base", "pw_storage") as $val){
623 if(isset($_POST[$val])){
624 $data= validate($_POST[$val]);
625 if ($data != $this->$val){
626 $this->is_modified= TRUE;
627 }
628 $this->$val= $data;
629 }
630 }
631 }
632 }
633 }
635 function rebind($ldap, $referral)
636 {
637 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
638 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
639 $this->error = "Success";
640 $this->hascon=true;
641 $this->reconnect= true;
642 return (0);
643 } else {
644 $this->error = "Could not bind to " . $credentials['ADMIN'];
645 return NULL;
646 }
647 }
649 /* Save data to LDAP, depending on is_account we save or delete */
650 function save()
651 {
652 /* Only force save of changes ....
653 If this attributes aren't changed, avoid saving.
654 */
655 if ($this->use_dob == "1"){
656 $this->dateOfBirth= date("Y-m-d", $this->dateOfBirth);
657 }
658 if($this->gender=="0") $this->gender ="";
659 if($this->preferredLanguage=="0") $this->preferredLanguage ="";
662 /* First use parents methods to do some basic fillup in $this->attrs */
663 plugin::save ();
665 /* Remove additional objectClasses */
666 $tmp= array();
667 foreach ($this->attrs['objectClass'] as $key => $set){
668 $found= false;
669 foreach (array("ivbbentry", "gosaUserTemplate") as $val){
670 if (preg_match ("/^$set$/i", $val)){
671 $found= true;
672 break;
673 }
674 }
675 if (!$found){
676 $tmp[]= $set;
677 }
678 }
680 /* Replace the objectClass array. This is done because of the
681 separation into government and normal mode. */
682 $this->attrs['objectClass']= $tmp;
684 /* Add objectClasss for template mode? */
685 if ($this->is_template){
686 $this->attrs['objectClass'][]= "gosaUserTemplate";
687 }
689 /* Hard coded government mode? */
690 if ($this->config->current['GOVERNMENTMODE'] != 'false'){
691 $this->attrs['objectClass'][]= "ivbbentry";
693 /* Copy standard attributes */
694 foreach ($this->govattrs as $val){
695 if ($this->$val != ""){
696 $this->attrs["$val"]= $this->$val;
697 } elseif (!$this->new) {
698 $this->attrs["$val"]= array();
699 }
700 }
702 /* Remove attribute if set to "nein" */
703 if ($this->publicVisible == "nein"){
704 $this->attrs['publicVisible']= array();
705 if($this->new){
706 unset($this->attrs['publicVisible']);
707 }else{
708 $this->attrs['publicVisible']=array();
709 }
711 }
713 }
715 /* Special handling for attribute userCertificate needed */
716 if ($this->userCertificate != ""){
717 $this->attrs["userCertificate;binary"]= $this->userCertificate;
718 $remove_userCertificate= false;
719 } else {
720 $remove_userCertificate= true;
721 }
723 /* Special handling for dateOfBirth value */
724 if ($this->use_dob != "1"){
725 if ($this->new) {
726 unset($this->attrs["dateOfBirth"]);
727 } else {
728 $this->attrs["dateOfBirth"]= array();
729 }
730 }
731 if (!$this->gender){
732 if ($this->new) {
733 unset($this->attrs["gender"]);
734 } else {
735 $this->attrs["gender"]= array();
736 }
737 }
738 if (!$this->preferredLanguage){
739 if ($this->new) {
740 unset($this->attrs["preferredLanguage"]);
741 } else {
742 $this->attrs["preferredLanguage"]= array();
743 }
744 }
746 /* Special handling for attribute jpegPhote needed, scale image via
747 image magick to 147x200 pixels and inject resulting data. */
748 if ($this->jpegPhoto != "*removed*"){
750 /* Fallback if there's no image magick inside PHP */
751 if (!function_exists("imagick_blob2image")){
752 /* Get temporary file name for conversation */
753 $fname = tempnam ("/tmp", "GOsa");
755 /* Open file and write out photoData */
756 $fp = fopen ($fname, "w");
757 fwrite ($fp, $this->photoData);
758 fclose ($fp);
760 /* Build conversation query. Filename is generated automatically, so
761 we do not need any special security checks. Exec command and save
762 output. For PHP safe mode, you'll need a configuration which respects
763 image magick as executable... */
764 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
765 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
766 $query, "Execute");
768 /* Read data written by convert */
769 $output= "";
770 $sh= popen($query, 'r');
771 while (!feof($sh)){
772 $output.= fread($sh, 4096);
773 }
774 pclose($sh);
776 unlink($fname);
778 /* Save attribute */
779 $this->attrs["jpegPhoto"] = $output;
781 } else {
783 /* Load the new uploaded Photo */
784 if(!$handle = imagick_blob2image($this->photoData)) {
785 gosa_log("Can't Load image");
786 }
788 /* Resizing image to 147x200 and blur */
789 if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
790 gosa_log("imagick_resize failed");
791 }
793 /* Converting image to JPEG */
794 if(!imagick_convert($handle,"JPEG")) {
795 gosa_log("Can't Convert to JPEG");
796 }
798 /* Creating binary Code for the Image */
799 if(!$dump = imagick_image2blob($handle)){
800 gosa_log("Can't create blob for image");
801 }
803 /* Sending Image */
804 $output= $dump;
806 /* Save attribute */
807 $this->attrs["jpegPhoto"] = $output;
808 }
810 } elseif(!$this->new) {
811 $this->attrs["jpegPhoto"] = array();
812 }
814 /* Build new dn */
815 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
816 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
817 } else {
818 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
819 }
821 /* This only gets called when user is renaming himself */
822 $ldap= $this->config->get_ldap_link();
823 if ($this->dn != $new_dn){
825 /* Write entry on new 'dn' */
826 $this->move($this->dn, $new_dn);
828 /* Happen to use the new one */
829 change_ui_dn($this->dn, $new_dn);
830 $this->dn= $new_dn;
831 }
834 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
835 new entries. So do a check first... */
836 $ldap->cat ($this->dn, array('dn'));
837 if ($ldap->fetch()){
838 $mode= "modify";
839 } else {
840 $mode= "add";
841 $ldap->cd($this->config->current['BASE']);
842 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
843 }
845 /* Set password to some junk stuff in case of templates */
846 if ($this->is_template){
847 $this->attrs['userPassword']= '{crypt}N0T$3T4N0W';
848 }
850 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
851 $this->attributes, "Save via $mode");
853 /* Finally write data with selected 'mode' */
854 $this->cleanup();
855 $ldap->cd ($this->dn);
856 $ldap->$mode ($this->attrs);
857 if (show_ldap_error($ldap->get_error(), sprintf(_("Saving of user/generic account with dn '%s' failed."),$this->dn))){
858 return (1);
859 }
861 /* Remove cert?
862 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
863 to work around myself. */
864 if ($remove_userCertificate == true && !$this->new && $this->had_userCertificate){
866 /* Reset array, assemble new, this should be reworked */
867 $this->attrs= array();
868 $this->attrs['userCertificate;binary']= array();
870 /* Prepare connection */
871 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
872 die ("Could not connect to LDAP server");
873 }
874 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
875 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
876 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
877 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
878 }
879 if(isset($config->current['TLS']) && $config->current['TLS'] == "true"){
880 ldap_start_tls($ds);
881 }
882 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
883 $this->config->current['PASSWORD']))) {
884 die ("Could not bind to LDAP");
885 }
887 /* Modify using attrs */
888 ldap_mod_del($ds,$this->dn,$this->attrs);
889 ldap_close($ds);
890 }
892 /* Kerberos server defined? */
893 if (isset($this->config->data['SERVERS']['KERBEROS'])){
894 $cfg= $this->config->data['SERVERS']['KERBEROS'];
895 }
896 if (isset($cfg['SERVER']) && function_exists('kadm5_init_with_password')){
898 /* Connect to the admin interface */
899 $handle = kadm5_init_with_password($cfg['SERVER'], $cfg['REALM'],
900 $cfg['ADMIN'], $cfg['PASSWORD']);
902 /* Errors? */
903 if ($handle === FALSE){
904 print_red (_("Kerberos database communication failed"));
905 return (2);
906 }
908 /* Build user principal, get list of existsing principals */
909 $principal= $this->uid."@".$cfg['REALM'];
910 $principals = kadm5_get_principals($handle);
912 /* User exists in database? */
913 if (in_array($principal, $principals)){
915 /* Ok. User exists. Remove him/her when pw_storage has
916 changed to be NOT kerberos. */
917 if ($this->pw_storage != "kerberos"){
918 $ret= kadm5_delete_principal ( $handle, $principal);
920 if ($ret === FALSE){
921 print_red (_("Can't remove user from kerberos database."));
922 }
923 }
925 } else {
927 /* User doesn't exists, create it when pw_storage is kerberos. */
928 if ($this->pw_storage == "kerberos"){
929 $ret= kadm5_create_principal ( $handle, $principal);
931 if ($ret === FALSE){
932 print_red (_("Can't add user to kerberos database."));
933 }
934 }
936 }
938 /* Free kerberos admin handle */
939 kadm5_destroy($handle);
940 }
942 /* Optionally execute a command after we're done */
943 if ($mode == "add"){
944 $this->handle_post_events("add");
945 } elseif ($this->is_modified){
946 $this->handle_post_events("modify");
947 }
949 /* Fix tagging if needed */
950 $this->handle_object_tagging();
952 return (0);
953 }
956 /* Check formular input */
957 function check()
958 {
959 /* Call common method to give check the hook */
960 $message= plugin::check();
962 /* Assemble cn */
963 $this->cn= $this->givenName." ".$this->sn;
965 /* Permissions for that base? */
966 if (isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid"){
967 $new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
968 } else {
969 $new_dn= 'cn='.$this->cn.','.get_people_ou().$this->base;
970 }
972 if ($this->dn == "new" && !$this->acl_is_createable()){
973 $message[]= _("You have no permissions to create a user on this 'Base'.");
974 } elseif ($this->dn != $new_dn && $this->dn != "new"){
975 if (!$this->acl_is_writeable($this->dn, "user","create",($this->MyAccount_mode && !$this->edit_mode))){
976 $message[]= _("You have no permissions to move a user from the original 'Base'.");
977 }
978 }
980 /* must: sn, givenName, uid */
981 if ($this->sn == "" && ($this->acl_is_writeable("sn",($this->MyAccount_mode && !$this->edit_mode)) || ($this->new))){
982 $message[]= _("The required field 'Name' is not set.");
983 }
985 /* UID already used? */
986 $ldap= $this->config->get_ldap_link();
987 $ldap->cd($this->config->current['BASE']);
988 $ldap->search("(uid=$this->uid)", array("uid"));
989 $ldap->fetch();
990 if ($ldap->count() != 0 && $this->dn == 'new'){
991 $message[]= _("There's already a person with this 'Login' in the database.");
992 }
994 /* In template mode, the uid and givenName are autogenerated... */
995 if (!$this->is_template){
996 if ($this->givenName == "" && $this->acl_is_writeable("givenName",($this->MyAccount_mode && !$this->edit_mode))){
997 $message[]= _("The required field 'Given name' is not set.");
998 }
999 if ($this->uid == "" && $this->acl_is_writeable("uid",($this->MyAccount_mode && !$this->edit_mode))){
1000 $message[]= _("The required field 'Login' is not set.");
1001 }
1002 if (!(isset($this->config->current['DNMODE']) && $this->config->current['DNMODE'] == "uid")){
1003 $ldap->cd($this->config->current['BASE']);
1004 $ldap->search("(cn=".$this->cn.")", array("uid"));
1005 $ldap->fetch();
1006 if ($ldap->count() != 0 && $this->dn != $new_dn && $this->dn == 'new'){
1007 $message[]= _("There's already a person with this 'Name'/'Given name' combination in the database.");
1008 }
1009 }
1010 }
1012 /* Check for valid input */
1013 if ($this->is_modified && !is_uid($this->uid)){
1014 $message[]= _("The field 'Login' contains invalid characters. Lowercase, numbers and dashes are allowed.");
1015 }
1016 if (!is_url($this->labeledURI)){
1017 $message[]= _("The field 'Homepage' contains an invalid URL definition.");
1018 }
1019 if (preg_match ("/[\\\\]/", $this->sn)){
1020 $message[]= _("The field 'Name' contains invalid characters.");
1021 }
1022 if (preg_match ("/[\\\\]/", $this->givenName)){
1023 $message[]= _("The field 'Given name' contains invalid characters.");
1024 }
1026 /* Check phone numbers */
1027 if (!is_phone_nr($this->telephoneNumber)){
1028 $message[]= _("The field 'Phone' contains an invalid phone number.");
1029 }
1030 if (!is_phone_nr($this->facsimileTelephoneNumber)){
1031 $message[]= _("The field 'Fax' contains an invalid phone number.");
1032 }
1033 if (!is_phone_nr($this->mobile)){
1034 $message[]= _("The field 'Mobile' contains an invalid phone number.");
1035 }
1036 if (!is_phone_nr($this->pager)){
1037 $message[]= _("The field 'Pager' contains an invalid phone number.");
1038 }
1040 /* Check for reserved characers */
1041 if (preg_match ('/[,+"?\'()=<>;]/', $this->givenName)){
1042 $message[]= _("The field 'Given name' contains invalid characters.");
1043 }
1044 if (preg_match ('/[,+"?\'()=<>;]/', $this->sn)){
1045 $message[]= _("The field 'Name' contains invalid characters.");
1046 }
1048 return $message;
1049 }
1052 /* Indicate whether a password change is needed or not */
1053 function password_change_needed()
1054 {
1055 return ($this->pw_storage != $this->last_pw_storage);
1056 }
1059 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
1060 function load_picture()
1061 {
1062 /* make connection and read jpegPhoto */
1063 $ds= ldap_connect($this->config->current['SERVER']);
1064 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1065 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
1066 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1067 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1068 }
1070 if(isset($this->config->current['TLS']) &&
1071 $this->config->current['TLS'] == "true"){
1073 ldap_start_tls($ds);
1074 }
1076 $r= ldap_bind($ds);
1077 $sr= @ldap_read($ds, $this->dn, "jpegPhoto=*", array("jpegPhoto"));
1079 /* in case we don't get an entry, load a default picture */
1080 $this->set_picture ("./images/default.jpg");
1081 $this->jpegPhoto= "*removed*";
1083 /* fill data from LDAP */
1084 if ($sr) {
1085 $ei=ldap_first_entry($ds, $sr);
1086 if ($ei) {
1087 if ($info = ldap_get_values_len($ds, $ei, "jpegPhoto")){
1088 $this->photoData= $info[0];
1089 $_SESSION['binary']= $this->photoData;
1090 $_SESSION['binarytype']= "image/jpeg";
1091 $this->jpegPhoto= "";
1092 }
1093 }
1094 }
1096 /* close conncetion */
1097 ldap_unbind($ds);
1098 }
1101 /* Load a certificate from LDAP, this is going to be simplified later on */
1102 function load_cert()
1103 {
1104 $ds= ldap_connect($this->config->current['SERVER']);
1105 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1106 if (function_exists("ldap_set_rebind_proc") && isset($this->config->current['RECURSIVE']) && $this->config->current['RECURSIVE'] == "true") {
1107 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1108 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1109 }
1110 if(isset($this->config->current['TLS']) &&
1111 $this->config->current['TLS'] == "true"){
1113 ldap_start_tls($ds);
1114 }
1116 $r= ldap_bind($ds);
1117 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1119 if ($sr) {
1120 $ei= @ldap_first_entry($ds, $sr);
1122 if ($ei) {
1123 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1124 $this->userCertificate= "";
1125 } else {
1126 $this->userCertificate= $info[0];
1127 }
1128 }
1129 } else {
1130 $this->userCertificate= "";
1131 }
1133 ldap_unbind($ds);
1134 }
1137 /* Load picture from file to object */
1138 function set_picture($filename)
1139 {
1140 if($this->acl_is_writeable("userPicture",($this->MyAccount_mode && !$this->edit_mode))) {
1141 if (!is_file($filename)){
1142 $filename= "./images/default.jpg";
1143 $this->jpegPhoto= "*removed*";
1144 }
1146 $fd = fopen ($filename, "rb");
1147 $this->photoData= fread ($fd, filesize ($filename));
1148 $_SESSION['binary']= $this->photoData;
1149 $_SESSION['binarytype']= "image/jpeg";
1150 $this->jpegPhoto= "";
1152 fclose ($fd);
1153 }
1154 }
1157 /* Load certificate from file to object */
1158 function set_cert($cert, $filename)
1159 {
1160 if(!$thsi->acl_is_writeable("Certificate",($this->MyAccount_mode && !$this->edit_mode))) return;
1161 $fd = fopen ($filename, "rb");
1162 if (filesize($filename)>0) {
1163 $this->$cert= fread ($fd, filesize ($filename));
1164 fclose ($fd);
1165 $this->is_modified= TRUE;
1166 } else {
1167 print_red(_("Could not open specified certificate!"));
1168 }
1169 }
1171 /* Adapt from given 'dn' */
1172 function adapt_from_template($dn)
1173 {
1174 plugin::adapt_from_template($dn);
1176 /* Get base */
1177 $this->base= preg_replace('/^[^,]+,'.get_people_ou().'/i', '', $dn);
1179 if ($this->config->current['GOVERNMENTMODE']){
1181 /* Walk through govattrs */
1182 foreach ($this->govattrs as $val){
1184 if (isset($this->attrs["$val"][0])){
1186 /* If attribute is set, replace dynamic parts:
1187 %sn, %givenName and %uid. Fill these in our local variables. */
1188 $value= $this->attrs["$val"][0];
1190 foreach (array("sn", "givenName", "uid") as $repl){
1191 if (preg_match("/%$repl/i", $value)){
1192 $value= preg_replace ("/%$repl/i",
1193 $this->parent->$repl, $value);
1194 }
1195 }
1196 $this->$val= $value;
1197 }
1198 }
1199 }
1201 /* Get back uid/sn/givenName */
1202 if ($this->parent != NULL){
1203 $this->uid= $this->parent->uid;
1204 $this->sn= $this->parent->sn;
1205 $this->givenName= $this->parent->givenName;
1206 }
1207 }
1210 /* This avoids that users move themselves out of their rights.
1211 */
1212 function allowedBasesToMoveTo()
1213 {
1214 $allowed = array();
1215 $ret_all = false;
1216 if($this->uid == $_SESSION['ui']->username){
1217 $ldap= $this->config->get_ldap_link();
1218 $ldap->cd($this->config->current['BASE']);
1219 $ldap->search("(&(objectClass=posixGroup)(memberUid=".$_SESSION['ui']->username."))",array("gosaSubtreeACL"));
1221 while($attrs = $ldap->fetch()){
1223 if(isset($attrs['gosaSubtreeACL'])){
1225 foreach($attrs['gosaSubtreeACL'] as $attr){
1226 if((preg_match("/:user#/",$attr))||(preg_match("/:all/",$attr))){
1227 $s = preg_replace("/^.*ou=groups,/","",$attrs['dn']);
1229 foreach($this->config->idepartments as $key => $dep) {
1230 if(preg_match("/".$s."/i",$key)){
1231 $allowed[$key] = $dep;
1232 }
1233 }
1234 }
1235 }
1236 }
1237 }
1238 if(count($allowed) == 0){
1239 foreach($this->config->idepartments as $key => $dep) {
1240 if($this->base==$key){
1241 $allowed[$key] = $dep;
1242 }
1243 }
1244 }
1246 return($allowed);
1248 }else{
1249 return($this->config->idepartments);
1250 }
1251 }
1254 function getCopyDialog()
1255 {
1256 $str = "";
1258 $_SESSION['binary'] = $this->photoData;
1259 $_SESSION['binarytype']= "image/jpeg";
1261 /* Get random number for pictures */
1262 srand((double)microtime()*1000000);
1263 $rand = rand(0, 10000);
1265 $smarty = get_smarty();
1267 $smarty->assign("passwordTodo","clear");
1269 if(isset($_POST['passwordTodo'])){
1270 $smarty->assign("passwordTodo",$_POST['passwordTodo']);
1271 }
1273 $smarty->assign("sn", $this->sn);
1274 $smarty->assign("givenName",$this->givenName);
1275 $smarty->assign("uid", $this->uid);
1276 $smarty->assign("rand", $rand);
1277 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1280 $ret = array();
1281 $ret['string'] = $str;
1282 $ret['status'] = "";
1283 return($ret);
1284 }
1286 function saveCopyDialog()
1287 {
1289 if((isset($_FILES['picture_file']['tmp_name'])) && ($_FILES['picture_file']['size'] > 0)){
1290 $this->set_picture($_FILES['picture_file']['tmp_name']);
1291 }
1293 /* Remove picture? */
1294 if (isset($_POST['picture_remove'])){
1295 $this->jpegPhoto= "*removed*";
1296 $this->set_picture ("./images/default.jpg");
1297 $this->is_modified= TRUE;
1298 }
1300 $attrs = array("uid","givenName","sn");
1301 foreach($attrs as $attr){
1302 if(isset($_POST[$attr])){
1303 $this->$attr = $_POST[$attr];
1304 }
1305 }
1306 }
1309 function plInfo()
1310 {
1312 $govattrs= array(
1313 "gouvernmentOrganizationalUnit" => _("Unit"),
1314 "houseIdentifier" => _("House identifier"),
1315 "vocation" => _("Vocation"),
1316 "ivbbLastDeliveryCollective" => _("Last delivery"),
1317 "gouvernmentOrganizationalPersonLocality" => _("Person locality"),
1318 "gouvernmentOrganizationalUnitDescription" => _("Unit description"),
1319 "gouvernmentOrganizationalUnitSubjectArea" => _("Subject area"),
1320 "functionalTitle" => _("Functional title"),
1321 "certificateSerialNumber" => _(""),
1322 "publicVisible" => _("Public visible"),
1323 "street" => _("Street"),
1324 "role" => _("Role"),
1325 "postalCode" => _("Postal code"));
1327 $ret = array(
1328 "plShortName" => _("Generic"),
1329 "plDescription" => _("Generic user settings"),
1330 "plSelfModify" => TRUE,
1331 "plDepends" => array(),
1332 "plPriority" => 0,
1333 "plSection" => array("personal" => _("My account")),
1334 "plCategory" => array("users" => array("description" => _("Users"),
1335 "objectClass" => "gosaAccount")),
1337 "plProvidedAcls" => array(
1338 "base" => _("Base"),
1339 "userPassword" => _("User password"),
1340 "sn" => _("Surename"),
1341 "givenName" => _("Given name"),
1342 "uid" => _("User identification"),
1343 "personalTitle" => _("Personal title"),
1344 "academicTitle" => _("Academic title"),
1345 "homePostalAddress" => _("Home postal address"),
1346 "homePhone" => _("Home phone number"),
1347 "labeledURI" => _("Homepage"),
1348 "o" => _("Organization"),
1349 "ou" => _("Department"),
1350 "dateOfBirth" => _("Date of birth"),
1351 "gender" => _("Gender"),
1352 "preferredLanguage" => _("Preferred language"),
1353 "departmentNumber" => _("Department number"),
1354 "employeeNumber" => _("Employee number"),
1355 "employeeType" => _("Employee type"),
1356 "l" => _("Location"),
1357 "st" => _("State"),
1358 "userPicture" => _("User picture"),
1359 "roomNumber" => _("Room number"),
1360 "telephoneNumber" => _("Telefon number"),
1361 "mobile" => _("Mobile number"),
1362 "pager" => _("Pager number"),
1363 "Certificate" => _("User certificates"),
1365 "postalAddress" => _("Postal address"),
1366 "facsimileTelephoneNumber" => _("Fax number"))
1367 );
1369 /* Append government attributes if required */
1370 global $config;
1371 if (isset($config->current['GOVERNMENTMODE']) && preg_match('/true/i', $config->current['GOVERNMENTMODE'])){
1372 foreach($govattrs as $attr => $desc){
1373 $ret["plProvidedAcls"][$attr] = $desc;
1374 }
1375 }
1377 return($ret);
1378 }
1379 }
1381 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1382 ?>