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 /* Plugin specific values */
41 var $base= "";
42 var $orig_base= "";
43 var $cn= "";
44 var $new_dn= "";
45 var $personalTitle= "";
46 var $academicTitle= "";
47 var $homePostalAddress= "";
48 var $homePhone= "";
49 var $labeledURI= "";
50 var $o= "";
51 var $ou= "";
52 var $departmentNumber= "";
53 var $gosaLoginRestriction= array();
54 var $gosaLoginRestrictionWidget;
55 var $employeeNumber= "";
56 var $employeeType= "";
57 var $roomNumber= "";
58 var $telephoneNumber= "";
59 var $facsimileTelephoneNumber= "";
60 var $mobile= "";
61 var $pager= "";
62 var $l= "";
63 var $st= "";
64 var $postalAddress= "";
65 var $dateOfBirth;
66 var $use_dob= "0";
67 var $gender="0";
68 var $preferredLanguage="0";
69 var $baseSelector;
71 var $jpegPhoto= "*removed*";
72 var $photoData= "";
73 var $old_jpegPhoto= "";
74 var $old_photoData= "";
75 var $cert_dialog= FALSE;
76 var $picture_dialog= FALSE;
77 var $pwObject= NULL;
79 var $userPKCS12= "";
80 var $userSMIMECertificate= "";
81 var $userCertificate= "";
82 var $certificateSerialNumber= "";
83 var $old_certificateSerialNumber= "";
84 var $old_userPKCS12= "";
85 var $old_userSMIMECertificate= "";
86 var $old_userCertificate= "";
88 var $gouvernmentOrganizationalUnit= "";
89 var $houseIdentifier= "";
90 var $street= "";
91 var $postalCode= "";
92 var $vocation= "";
93 var $ivbbLastDeliveryCollective= "";
94 var $gouvernmentOrganizationalPersonLocality= "";
95 var $gouvernmentOrganizationalUnitDescription= "";
96 var $gouvernmentOrganizationalUnitSubjectArea= "";
97 var $functionalTitle= "";
98 var $role= "";
99 var $publicVisible= "";
101 var $orig_dn;
102 var $dialog;
104 /* variables to trigger password changes */
105 var $pw_storage= "md5";
106 var $last_pw_storage= "unset";
107 var $had_userCertificate= FALSE;
109 var $view_logged = FALSE;
111 var $manager = "";
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", "gosaLoginRestriction", "manager");
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 global $lang;
141 $this->config= $config;
142 /* Configuration is fine, allways */
143 if($this->config->get_cfg_value("honourIvbbAttributes") == "true"){
144 $this->governmentmode = TRUE;
145 $this->attributes=array_merge($this->attributes,$this->govattrs);
146 }
148 /* Load base attributes */
149 plugin::plugin ($config, $dn);
151 $this->orig_dn = $this->dn;
152 $this->new_dn = $dn;
154 if ($this->governmentmode){
155 /* Fix public visible attribute if unset */
156 if (!isset($this->attrs['publicVisible'])){
157 $this->publicVisible == "nein";
158 }
159 }
161 /* Load government mode attributes */
162 if ($this->governmentmode){
163 /* Copy all attributs */
164 foreach ($this->govattrs as $val){
165 if (isset($this->attrs["$val"][0])){
166 $this->$val= $this->attrs["$val"][0];
167 }
168 }
169 }
171 /* Create me for new accounts */
172 if ($dn == "new"){
173 $this->is_account= TRUE;
174 }
176 /* Make hash default to md5 if not set in config */
177 $hash= $this->config->get_cfg_value("passwordDefaultHash", "crypt/md5");
179 /* Load data from LDAP? */
180 if ($dn !== NULL){
182 /* Do base conversation */
183 if ($this->dn == "new"){
184 $ui= get_userinfo();
185 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
186 } else {
187 $this->base= dn2base($dn);
188 }
190 /* get password storage type */
191 if (isset ($this->attrs['userPassword'][0])){
192 /* Initialize local array */
193 $matches= array();
194 if (preg_match ("/^{[^}]+}/", $this->attrs['userPassword'][0])){
195 $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
196 if(is_object($tmp)){
197 $this->pw_storage= $tmp->get_hash();
198 }
200 } else {
201 if ($this->attrs['userPassword'][0] != ""){
202 $this->pw_storage= "clear";
203 } else {
204 $this->pw_storage= $hash;
205 }
206 }
207 } else {
208 /* Preset with vaule from configuration */
209 $this->pw_storage= $hash;
210 }
212 /* Load extra attributes: certificate and picture */
213 $this->load_cert();
214 $this->load_picture();
215 if ($this->userCertificate != ""){
216 $this->had_userCertificate= TRUE;
217 }
218 }
220 /* Reset password storage indicator, used by password_change_needed() */
221 if ($dn == "new"){
222 $this->last_pw_storage= "unset";
223 } else {
224 $this->last_pw_storage= $this->pw_storage;
225 }
227 /* Generate dateOfBirth entry */
228 if (isset ($this->attrs['dateOfBirth'])){
229 /* This entry is ISO 8601 conform */
230 list($year, $month, $day)= explode("-", $this->attrs['dateOfBirth'][0], 3);
232 #TODO: use $lang to convert date
233 $this->dateOfBirth= "$day.$month.$year";
234 } else {
235 $this->dateOfBirth= "";
236 }
238 /* Put gender attribute to upper case */
239 if (isset ($this->attrs['gender'])){
240 $this->gender= strtoupper($this->attrs['gender'][0]);
241 }
243 // Get login restrictions
244 if(isset($this->attrs['gosaLoginRestriction'])){
245 $this->gosaLoginRestriction =array();
246 for($i =0;$i < $this->attrs['gosaLoginRestriction']['count']; $i++){
247 $this->gosaLoginRestriction[] = $this->attrs['gosaLoginRestriction'][$i];
248 }
249 }
250 $this->gosaLoginRestrictionWidget= new sortableListing($this->gosaLoginRestriction);
251 $this->gosaLoginRestrictionWidget->setDeleteable(true);
252 $this->gosaLoginRestrictionWidget->setColspecs(array('*'));
253 $this->gosaLoginRestrictionWidget->setWidth("100%");
254 $this->gosaLoginRestrictionWidget->setHeight("70px");
256 $this->orig_base = $this->base;
257 $this->baseSelector= new baseSelector($this->allowedBasesToMoveTo(), $this->base);
258 $this->baseSelector->setSubmitButton(false);
259 $this->baseSelector->setHeight(300);
260 $this->baseSelector->update(true);
261 }
264 /* execute generates the html output for this node */
265 function execute()
266 {
267 /* Call parent execute */
268 plugin::execute();
270 /* Set list ACL */
271 $this->gosaLoginRestrictionWidget->setAcl($this->getacl('gosaLoginRestriction', (!is_object($this->parent) && !session::is_set('edit'))));
272 $this->gosaLoginRestrictionWidget->update();
274 /* Handle add/delete for restriction mode */
275 if (isset($_POST['add_res']) && isset($_POST['res'])) {
276 $val= validate($_POST['res']);
277 if (preg_match('/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/', $val) ||
278 preg_match('/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/', $val) ||
279 preg_match('/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/', $val)) {
280 $this->gosaLoginRestrictionWidget->addEntry($val);
281 } else {
282 msg_dialog::display(_("Error"), _("Please add a single IP address or a network/netmask combination!"), ERROR_DIALOG);
283 }
284 }
286 /* Log view */
287 if($this->is_account && !$this->view_logged){
288 $this->view_logged = TRUE;
289 new log("view","users/".get_class($this),$this->dn);
290 }
292 $smarty= get_smarty();
293 $smarty->assign("usePrototype", "true");
294 $smarty->assign("gosaLoginRestrictionWidget", $this->gosaLoginRestrictionWidget->render());
296 /* Assign managers */
297 $ldap = $this->config->get_ldap_link();
298 $ldap->cd($this->config->current["BASE"]);
299 $ldap->search("(objectClass=inetOrgPerson)", array("cn", "dn"));
300 $managers = array();
301 while($attrs = $ldap->fetch()) {
302 $managers[$attrs["dn"]] = $attrs["cn"][0];
303 }
304 asort($managers);
305 $smarty->assign("managers", $managers);
307 /* Assign sex */
308 $sex= array(0 => " ", "F" => _("female"), "M" => _("male"));
309 $smarty->assign("gender_list", $sex);
310 $language= array_merge(array(0 => " ") ,get_languages(TRUE));
311 $smarty->assign("preferredLanguage_list", $language);
313 /* Get random number for pictures */
314 srand((double)microtime()*1000000);
315 $smarty->assign("rand", rand(0, 10000));
318 /* Do we represent a valid gosaAccount? */
319 if (!$this->is_account){
320 $str = "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
321 msgPool::noValidExtension("GOsa")."</b>";
322 return($str);
323 }
325 /* Password configure dialog handling */
326 if(is_object($this->pwObject) && $this->pwObject->display){
327 $output= $this->pwObject->configure();
328 if ($output != ""){
329 $this->dialog= TRUE;
330 return $output;
331 }
332 $this->dialog= false;
333 }
335 /* Dialog handling */
336 if(is_object($this->dialog)){
337 /* Must be called before save_object */
338 $this->dialog->save_object();
340 if($this->dialog->isClosed()){
341 $this->dialog = false;
342 }elseif($this->dialog->isSelected()){
344 /* check if selected base is allowed to move to / create a new object */
345 $tmp = $this->get_allowed_bases();
346 if(isset($tmp[$this->dialog->isSelected()])){
347 $this->base = $this->dialog->isSelected();
348 }
349 $this->dialog= false;
350 }else{
351 return($this->dialog->execute());
352 }
353 }
355 /* Want password method editing? */
356 if ($this->acl_is_writeable("userPassword")){
357 if (isset($_POST['edit_pw_method'])){
358 if (!is_object($this->pwObject) || $this->pw_storage != $this->pwObject->get_hash_name()){
359 $temp= passwordMethod::get_available_methods();
360 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
361 }
362 $this->pwObject->display = TRUE;
363 $this->dialog= TRUE;
364 return ($this->pwObject->configure());
365 }
366 }
368 /* Want picture edit dialog? */
369 if($this->acl_is_writeable("userPicture")) {
370 if (isset($_POST['edit_picture'])){
371 /* Save values for later recovery, in case some presses
372 the cancel button. */
373 $this->old_jpegPhoto= $this->jpegPhoto;
374 $this->old_photoData= $this->photoData;
375 $this->picture_dialog= TRUE;
376 $this->dialog= TRUE;
377 }
378 }
380 /* Remove picture? */
381 if($this->acl_is_writeable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))) ){
382 if (isset($_POST['picture_remove'])){
383 $this->set_picture ();
384 $this->jpegPhoto= "*removed*";
385 $this->is_modified= TRUE;
386 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
387 }
388 }
390 /* Save picture */
391 if (isset($_POST['picture_edit_finish'])){
393 /* Check for clean upload */
394 if ($_FILES['picture_file']['name'] != ""){
395 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
396 msg_dialog::display(_("Error"), _("Cannot upload file!"), ERROR_DIALOG);
397 }else{
398 /* Activate new picture */
399 $this->set_picture($_FILES['picture_file']['tmp_name']);
400 }
401 }
402 $this->picture_dialog= FALSE;
403 $this->dialog= FALSE;
404 $this->is_modified= TRUE;
405 }
408 /* Cancel picture */
409 if (isset($_POST['picture_edit_cancel'])){
411 /* Restore values */
412 $this->jpegPhoto= $this->old_jpegPhoto;
413 $this->photoData= $this->old_photoData;
415 /* Update picture */
416 session::set('binary',$this->photoData);
417 session::set('binarytype',"image/jpeg");
418 $this->picture_dialog= FALSE;
419 $this->dialog= FALSE;
420 }
422 /* Want certificate= */
423 if ((isset($_POST['edit_cert'])) && $this->acl_is_readable("Certificate")){
425 /* Save original values for later reconstruction */
426 foreach (array("certificateSerialNumber", "userCertificate",
427 "userSMIMECertificate", "userPKCS12") as $val){
429 $oval= "old_$val";
430 $this->$oval= $this->$val;
431 }
433 $this->cert_dialog= TRUE;
434 $this->dialog= TRUE;
435 }
438 /* Cancel certificate dialog */
439 if (isset($_POST['cert_edit_cancel'])){
441 /* Restore original values in case of 'cancel' */
442 foreach (array("certificateSerialNumber", "userCertificate",
443 "userSMIMECertificate", "userPKCS12") as $val){
445 $oval= "old_$val";
446 $this->$val= $this->$oval;
447 }
448 $this->cert_dialog= FALSE;
449 $this->dialog= FALSE;
450 }
453 /* Remove certificate? */
454 if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){
455 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
456 if (isset($_POST["remove_$val"])){
458 /* Reset specified cert*/
459 $this->$val= "";
460 $this->is_modified= TRUE;
461 }
462 }
463 }
465 /* Upload new cert and close dialog? */
466 if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){
468 $fail =false;
470 if (isset($_POST['cert_edit_finish'])){
472 /* for all certificates do */
473 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
474 as $val){
476 /* Check for clean upload */
477 if (array_key_exists($val."_file", $_FILES) &&
478 array_key_exists('name', $_FILES[$val."_file"]) &&
479 $_FILES[$val."_file"]['name'] != "" &&
480 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
481 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
482 }
483 }
485 /* Save serial number */
486 if (isset($_POST["certificateSerialNumber"]) &&
487 $_POST["certificateSerialNumber"] != ""){
489 if (!tests::is_id($_POST["certificateSerialNumber"])){
490 $fail = true;
491 msg_dialog::display(_("Error"), msgPool::invalid(_("Serial number"),$_POST["certificateSerialNumber"],"/[0-9]/"),ERROR_DIALOG);
493 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
494 if ($this->$cert != ""){
495 $smarty->assign("$cert"."_state", "true");
496 } else {
497 $smarty->assign("$cert"."_state", "");
498 }
499 }
500 }
502 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
503 $this->is_modified= TRUE;
504 }
505 if(!$fail){
506 $this->cert_dialog= FALSE;
507 $this->dialog= FALSE;
508 }
509 }
510 }
511 /* Display picture dialog */
512 if ($this->picture_dialog){
513 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
514 }
516 /* Display cert dialog */
517 if ($this->cert_dialog){
518 $smarty->assign("CertificateACL",$this->getacl("Certificate",(!is_object($this->parent) && !session::is_set('edit'))));
519 $smarty->assign("Certificate_readable",$this->acl_is_readable("Certificate"));
520 $smarty->assign("certificateSerialNumber",$this->certificateSerialNumber);
522 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
523 if ($this->$cert != ""){
524 /* import certificate */
525 $certificate = new certificate;
526 $certificate->import($this->$cert);
528 /* Read out data*/
529 $timeto = $certificate->getvalidto_date();
530 $timefrom = $certificate->getvalidfrom_date();
533 /* Additional info if start end time is '0' */
534 $add_str_info = "";
535 if($timeto == 0 && $timefrom == 0){
536 $add_str_info = "<br><i>"._("(Some types of certificates are currently not supported and may be displayed as 'invalid'.)")."</i>";
537 }
539 $str = "<table summary=\"\" border=0>
540 <tr>
541 <td style='vertical-align:top'>CN</td>
542 <td>".preg_replace("/ /", " ", $certificate->getname())."</td>
543 </tr>
544 </table><br>".
546 sprintf(_("Certificate is valid from %s to %s and is currently %s."),
547 "<b>".date('d M Y',$timefrom)."</b>",
548 "<b>".date('d M Y',$timeto)."</b>",
549 $certificate->isvalid()?"<b><font style='color:green'>"._("valid")."</font></b>":
550 "<b><font style='color:red'>"._("invalid")."</font></b>").$add_str_info;
552 $smarty->assign($cert."info",$str);
553 $smarty->assign($cert."_state","true");
554 } else {
555 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
556 $smarty->assign($cert."_state","");
557 }
558 }
560 if($this->governmentmode){
561 $smarty->assign("honourIvbbAttributes", "true");
562 }else{
563 $smarty->assign("honourIvbbAttributes", "false");
564 }
565 $smarty->assign("governmentmode", $this->governmentmode);
566 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
567 }
569 /* Prepare password hashes */
570 if ($this->pw_storage == ""){
571 $this->pw_storage= $this->config->get_cfg_value("hash");
572 }
574 $temp= passwordMethod::get_available_methods();
575 $is_configurable= FALSE;
576 $hashes = $temp['name'];
577 if(isset($temp[$this->pw_storage])){
578 $test= new $temp[$this->pw_storage]($this->config);
579 $is_configurable= $test->is_configurable();
580 }else{
581 new msg_dialog(_("Password method"),_("The selected password method is no longer available."),WARNING_DIALOG);
582 }
585 /* Create password methods array */
586 $pwd_methods = array();
587 foreach($hashes as $id => $name){
588 if(!empty($temp['desc'][$id])){
589 $pwd_methods[$name] = $name." (".$temp['desc'][$id].")";
590 }else{
591 $pwd_methods[$name] = $name;
592 }
593 }
595 /* Load attributes and acl's */
596 $ui =get_userinfo();
597 foreach($this->attributes as $val){
598 $smarty->assign("$val", $this->$val);
599 if(in_array($val,$this->multi_boxes)){
600 $smarty->assign("use_".$val,TRUE);
601 }else{
602 $smarty->assign("use_".$val,FALSE);
603 }
604 }
605 foreach(array("base","pw_storage","edit_picture") as $val){
606 if(in_array($val,$this->multi_boxes)){
607 $smarty->assign("use_".$val,TRUE);
608 }else{
609 $smarty->assign("use_".$val,FALSE);
610 }
611 }
613 /* Set acls */
614 $tmp = $this->plinfo();
615 foreach($tmp['plProvidedAcls'] as $val => $translation){
616 $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
617 }
619 // Special ACL for gosaLoginRestrictions -
620 // In case of multiple edit, we need a readonly ACL for the list.
621 $smarty->assign('gosaLoginRestriction_ONLY_R_ACL',
622 preg_replace("/[^r]/i","", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit')))));
624 $smarty->assign("pwmode", $pwd_methods);
625 $smarty->assign("pwmode_select", $this->pw_storage);
626 $smarty->assign("pw_configurable", $is_configurable);
627 $smarty->assign("passwordStorageACL", $this->getacl("userPassword",(!is_object($this->parent) && !session::is_set('edit'))));
629 if(!session::is_set('edit')){
630 $smarty->assign("CertificatesACL","");
631 }else{
632 $smarty->assign("CertificatesACL", $this->getacl("Certificate"));
633 }
635 $smarty->assign("userPictureACL", $this->getacl("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
636 $smarty->assign("userPicture_is_readable", $this->acl_is_readable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
638 /* Create base acls */
639 $smarty->assign("base", $this->baseSelector->render());
641 /* Save government mode attributes */
642 if($this->governmentmode){
643 $smarty->assign("governmentmode", "true");
644 $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
645 "internet,ivbv", "internet,testa", "internet,ivbv,testa");
646 $smarty->assign("ivbbmodes", $ivbbmodes);
647 foreach ($this->govattrs as $val){
648 $smarty->assign("$val", $this->$val);
649 $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
650 }
651 } else {
652 $smarty->assign("governmentmode", "false");
653 }
655 /* Special mode for uid */
656 $uidACL= $this->getacl("uid",(!is_object($this->parent) && !session::is_set('edit')));
657 if (isset ($this->dn)){
658 if ($this->dn != "new"){
659 $uidACL= preg_replace("/w/","",$uidACL);
660 }
661 } else {
662 $uidACL= preg_replace("/w/","",$uidACL);
663 }
665 $smarty->assign("uidACL", $uidACL);
666 $smarty->assign("is_template", $this->is_template);
667 $smarty->assign("use_dob", $this->use_dob);
669 if (isset($this->parent)){
670 if (isset($this->parent->by_object['phoneAccount']) &&
671 $this->parent->by_object['phoneAccount']->is_account){
672 $smarty->assign("has_phoneaccount", "true");
673 } else {
674 $smarty->assign("has_phoneaccount", "false");
675 }
676 } else {
677 $smarty->assign("has_phoneaccount", "false");
678 }
679 $smarty->assign("multiple_support" , $this->multiple_support_active);
680 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
681 }
684 /* remove object from parent */
685 function remove_from_parent()
686 {
687 /* Only remove valid accounts */
688 if(!$this->initially_was_account) return;
690 /* Remove password extension */
691 $temp= passwordMethod::get_available_methods();
693 /* Remove password method from user account */
694 if(isset($temp[$this->pw_storage]) && class_available($temp[$this->pw_storage])){
695 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
696 $this->pwObject->remove_from_parent();
697 }
699 /* Remove user */
700 $ldap= $this->config->get_ldap_link();
701 $ldap->rmdir ($this->dn);
702 if (!$ldap->success()){
703 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
704 }
706 new log("remove","users/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
708 /* Delete references to groups */
709 $ldap->cd ($this->config->current['BASE']);
710 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
711 while ($ldap->fetch()){
712 $g= new group($this->config, $ldap->getDN());
713 $g->removeUser($this->uid);
714 $g->save ();
715 }
717 /* Delete references to object groups */
718 $ldap->cd ($this->config->current['BASE']);
719 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
720 while ($ldap->fetch()){
721 $og= new ogroup($this->config, $ldap->getDN());
722 unset($og->member[$this->dn]);
723 $og->member= array_values($og->member);
724 $og->save ();
725 }
727 /* Delete references to roles */
728 $ldap->cd ($this->config->current['BASE']);
729 $ldap->search ("(&(objectClass=organizationalRole)(roleOccupant=".LDAP::prepare4filter($this->dn)."))", array("cn"));
730 while ($ldap->fetch()){
731 $role= new roleGeneric($this->config, $ldap->getDN());
732 $key = array_search($this->dn,$role->roleOccupant);
733 if($key !== FALSE){
734 unset($role->roleOccupant[$key]);
735 $role->roleOccupant= array_values($role->roleOccupant);
736 $role->save ();
737 }
738 }
740 /* If needed, let the password method do some cleanup */
741 $tmp = new passwordMethod($this->config);
742 $available = $tmp->get_available_methods();
743 if (in_array_ics($this->pw_storage, $available['name'])){
744 $test= new $available[$this->pw_storage]($this->config);
745 $test->attrs= $this->attrs;
746 $test->dn= $this->dn;
747 $test->remove_from_parent();
748 }
750 /* Remove ACL dependencies too */
751 acl::remove_acl_for($this->dn);
753 /* Optionally execute a command after we're done */
754 $this->handle_post_events("remove",array("uid" => $this->uid));
755 }
758 /* Save data to object */
759 function save_object()
760 {
761 if(isset($_POST['generic']) || isset($_POST['multiple_user_posted'])){
763 /* Make a backup of the current selected base */
764 $base_tmp = $this->base;
766 /* Parents save function */
767 plugin::save_object ();
769 /* Refresh base */
770 if ($this->acl_is_moveable($this->base)){
771 if (!$this->baseSelector->update()) {
772 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
773 }
774 if ($this->base != $this->baseSelector->getBase()) {
775 $this->base= $this->baseSelector->getBase();
776 $this->is_modified= TRUE;
777 }
778 }
780 /* Sync lists */
781 $this->gosaLoginRestrictionWidget->save_object();
782 if ($this->gosaLoginRestrictionWidget->isModified()) {
783 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
784 }
786 /* Save government mode attributes */
787 if ($this->governmentmode){
788 foreach ($this->govattrs as $val){
789 if ($this->acl_is_writeable($val,(!is_object($this->parent) && !session::is_set('edit'))) && isset($_POST["$val"])){
790 $data= stripcslashes($_POST["$val"]);
791 if ($data != $this->$val){
792 $this->is_modified= TRUE;
793 }
794 $this->$val= $data;
795 }
796 }
797 }
799 /* In template mode, the uid is autogenerated... */
800 if ($this->is_template){
801 $this->uid= strtolower($this->sn);
802 $this->givenName= $this->sn;
803 }
805 /* Get pw_storage mode */
806 if (isset($_POST['pw_storage'])){
807 foreach(array("pw_storage") as $val){
808 if(isset($_POST[$val])){
809 $data= validate($_POST[$val]);
810 if ($data != $this->$val){
811 $this->is_modified= TRUE;
812 }
813 $this->$val= $data;
814 }
815 }
816 }
818 if($this->pw_storage != $this->last_pw_storage && isset($_POST['pw_storage'])){
819 if ($this->acl_is_writeable("userPassword")){
820 $temp= passwordMethod::get_available_methods();
821 if (!is_object($this->pwObject) || !($this->pwObject instanceOf $temp[$this->pw_storage])){
822 foreach($temp as $id => $data){
823 if(isset($data['name']) && $data['name'] == $this->pw_storage && $data['is_configurable']){
824 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
825 break;
826 }
827 }
828 }
829 }
830 }
832 /* Save current cn
833 */
834 $this->cn = $this->givenName." ".$this->sn;
835 }
836 }
838 function rebind($ldap, $referral)
839 {
840 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
841 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
842 $this->error = "Success";
843 $this->hascon=true;
844 $this->reconnect= true;
845 return (0);
846 } else {
847 $this->error = "Could not bind to " . $credentials['ADMIN'];
848 return NULL;
849 }
850 }
853 /* Save data to LDAP, depending on is_account we save or delete */
854 function save()
855 {
856 global $lang;
858 /* Only force save of changes ....
859 If this attributes aren't changed, avoid saving.
860 */
862 if($this->gender=="0") $this->gender ="";
863 if($this->preferredLanguage=="0") $this->preferredLanguage ="";
865 /* First use parents methods to do some basic fillup in $this->attrs */
866 plugin::save ();
868 if ($this->dateOfBirth != ""){
869 if(!is_array($this->attrs['dateOfBirth'])) {
870 #TODO: use $lang to convert date
871 list($day, $month, $year)= explode(".", $this->dateOfBirth);
872 $this->attrs['dateOfBirth'] = sprintf("%04d-%02d-%02d", $year, $month, $day);
873 }
874 }
876 /* Remove additional objectClasses */
877 $tmp= array();
878 foreach ($this->attrs['objectClass'] as $key => $set){
879 $found= false;
880 foreach (array("ivbbentry", "gosaUserTemplate") as $val){
881 if (preg_match ("/^$set$/i", $val)){
882 $found= true;
883 break;
884 }
885 }
886 if (!$found){
887 $tmp[]= $set;
888 }
889 }
891 /* Replace the objectClass array. This is done because of the
892 separation into government and normal mode. */
893 $this->attrs['objectClass']= $tmp;
895 /* Add objectClasss for template mode? */
896 if ($this->is_template){
897 $this->attrs['objectClass'][]= "gosaUserTemplate";
898 }
900 /* Hard coded government mode? */
901 if ($this->governmentmode){
902 $this->attrs['objectClass'][]= "ivbbentry";
904 /* Copy standard attributes */
905 foreach ($this->govattrs as $val){
906 if ($this->$val != ""){
907 $this->attrs["$val"]= $this->$val;
908 } elseif (!$this->is_new) {
909 $this->attrs["$val"]= array();
910 }
911 }
913 /* Remove attribute if set to "nein" */
914 if ($this->publicVisible == "nein"){
915 $this->attrs['publicVisible']= array();
916 if($this->is_new){
917 unset($this->attrs['publicVisible']);
918 }else{
919 $this->attrs['publicVisible']=array();
920 }
922 }
924 }
926 /* Special handling for attribute userCertificate needed */
927 if ($this->userCertificate != ""){
928 $this->attrs["userCertificate;binary"]= $this->userCertificate;
929 $remove_userCertificate= false;
930 } else {
931 $remove_userCertificate= true;
932 }
934 /* Special handling for dateOfBirth value */
935 if ($this->dateOfBirth == ""){
936 if ($this->is_new) {
937 unset($this->attrs["dateOfBirth"]);
938 } else {
939 $this->attrs["dateOfBirth"]= array();
940 }
941 }
942 if (!$this->gender){
943 if ($this->is_new) {
944 unset($this->attrs["gender"]);
945 } else {
946 $this->attrs["gender"]= array();
947 }
948 }
949 if (!$this->preferredLanguage){
950 if ($this->is_new) {
951 unset($this->attrs["preferredLanguage"]);
952 } else {
953 $this->attrs["preferredLanguage"]= array();
954 }
955 }
957 /* Special handling for attribute jpegPhote needed, scale image via
958 image magick to 147x200 pixels and inject resulting data. */
959 if ($this->jpegPhoto == "*removed*"){
961 /* Reset attribute to avoid writing *removed* as value */
962 $this->attrs["jpegPhoto"] = array();
964 } else {
966 /* Fallback if there's no image magick inside PHP */
967 if (!function_exists("imagick_blob2image")){
968 /* Get temporary file name for conversation */
969 $fname = tempnam (TEMP_DIR, "GOsa");
971 /* Open file and write out photoData */
972 $fp = fopen ($fname, "w");
973 fwrite ($fp, $this->photoData);
974 fclose ($fp);
976 /* Build conversation query. Filename is generated automatically, so
977 we do not need any special security checks. Exec command and save
978 output. For PHP safe mode, you'll need a configuration which respects
979 image magick as executable... */
980 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
981 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
982 $query, "Execute");
984 /* Read data written by convert */
985 $output= "";
986 $sh= popen($query, 'r');
987 while (!feof($sh)){
988 $output.= fread($sh, 4096);
989 }
990 pclose($sh);
992 unlink($fname);
994 /* Save attribute */
995 $this->attrs["jpegPhoto"] = $output;
997 } else {
999 /* Load the new uploaded Photo */
1000 if(!$handle = imagick_blob2image($this->photoData)) {
1001 new log("debug","users/".get_class($this),$this->dn,array(),"Could not access uploaded image");
1002 }
1004 /* Resizing image to 147x200 and blur */
1005 if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
1006 new log("debug","users/".get_class($this),$this->dn,array(),"Could not resize uploaded image");
1007 }
1009 /* Converting image to JPEG */
1010 if(!imagick_convert($handle,"JPEG")) {
1011 new log("debug","users/".get_class($this),$this->dn,array(),"Could not convert uploaded image to jepg");
1012 }
1014 /* Creating binary Code for the Image */
1015 if(!$dump = imagick_image2blob($handle)){
1016 new log("debug","users/".get_class($this),$this->dn,array(),"Could not create new user image");
1017 }
1019 /* Sending Image */
1020 $output= $dump;
1022 /* Save attribute */
1023 $this->attrs["jpegPhoto"] = $output;
1024 }
1026 }
1028 /* This only gets called when user is renaming himself */
1029 $ldap= $this->config->get_ldap_link();
1030 if ($this->dn != $this->new_dn){
1032 /* Write entry on new 'dn' */
1033 $this->update_acls($this->dn,$this->new_dn);
1034 $this->move($this->dn, $this->new_dn);
1036 /* Happen to use the new one */
1037 change_ui_dn($this->dn, $this->new_dn);
1038 $this->dn= $this->new_dn;
1039 }
1042 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1043 new entries. So do a check first... */
1044 $ldap->cat ($this->dn, array('dn'));
1045 if ($ldap->fetch()){
1046 $mode= "modify";
1047 } else {
1048 $mode= "add";
1049 $ldap->cd($this->config->current['BASE']);
1050 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1051 }
1053 /* Set password to some junk stuff in case of templates */
1054 if ($this->is_template){
1055 $temp= passwordMethod::get_available_methods();
1056 foreach($temp as $id => $data){
1057 if(isset($data['name']) && $data['name'] == $this->pw_storage){
1058 $tmp = new $temp[$this->pw_storage]($this->config,$this->dn);
1059 $tmp->set_hash($this->pw_storage);
1060 $this->attrs['userPassword'] = $tmp->create_template_hash($this->attrs);
1061 break;
1062 }
1063 }
1064 }
1066 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
1067 $this->attributes, "Save via $mode");
1069 /* Finally write data with selected 'mode' */
1070 $this->cleanup();
1072 /* Update current locale settings, if we have edited ourselves */
1073 $ui = session::get('ui');
1074 if(isset($this->attrs['preferredLanguage']) && $this->dn == $ui->dn){
1075 $ui->language = $this->preferredLanguage;
1076 session::set('ui',$ui);
1077 session::set('Last_init_lang',"update");
1078 }
1080 $ldap->cd ($this->dn);
1081 $ldap->$mode ($this->attrs);
1082 if (!$ldap->success()){
1083 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
1084 return (1);
1085 }
1087 /* Remove ACL dependencies too */
1088 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1089 $tmp = new acl($this->config,$this->parent,$this->dn);
1090 $tmp->update_acl_membership($this->orig_dn,$this->dn);
1091 }
1093 if($mode == "modify"){
1094 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1095 }else{
1096 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1097 }
1099 /* Remove cert?
1100 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
1101 to work around myself. */
1102 if ($remove_userCertificate == true && !$this->is_new && $this->had_userCertificate){
1104 /* Reset array, assemble new, this should be reworked */
1105 $this->attrs= array();
1106 $this->attrs['userCertificate;binary']= array();
1108 /* Prepare connection */
1109 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
1110 die ("Could not connect to LDAP server");
1111 }
1112 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1113 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true") {
1114 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1115 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1116 }
1117 if($this->config->get_cfg_value("ldapTLS") == "true"){
1118 ldap_start_tls($ds);
1119 }
1120 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
1121 $this->config->current['PASSWORD']))) {
1122 die ("Could not bind to LDAP");
1123 }
1125 /* Modify using attrs */
1126 ldap_mod_del($ds,$this->dn,$this->attrs);
1127 ldap_close($ds);
1128 }
1130 /* If needed, let the password method do some cleanup */
1131 if ($this->pw_storage != $this->last_pw_storage){
1132 $tmp = new passwordMethod($this->config);
1133 $available = $tmp->get_available_methods();
1134 if (in_array_ics($this->last_pw_storage, $available['name'])){
1135 $test= new $available[$this->last_pw_storage]($this->config,$this->dn);
1136 $test->attrs= $this->attrs;
1137 $test->remove_from_parent();
1138 }
1139 }
1141 /* Maybe the current password method want's to do some changes... */
1142 if (is_object($this->pwObject)){
1143 $this->pwObject->save($this->dn);
1144 }
1146 /* Optionally execute a command after we're done */
1147 if ($mode == "add"){
1148 $this->handle_post_events("add", array("uid" => $this->uid));
1149 } elseif ($this->is_modified){
1150 $this->handle_post_events("modify", array("uid" => $this->uid));
1151 }
1153 return (0);
1154 }
1157 function create_initial_rdn($pattern)
1158 {
1159 // Only generate single RDNs
1160 if (preg_match('/\+/', $pattern)){
1161 msg_dialog::display(_("Error"), _("Cannot build RDN: no + allowed to build sub RDN!"), ERROR_DIALOG);
1162 return "";
1163 }
1165 // Extract attribute
1166 $attribute= preg_replace('/=.*$/', '', $pattern);
1167 if (!in_array_ics($attribute, $this->attributes)) {
1168 msg_dialog::display(_("Error"), _("Cannot build RDN: attribute is not defined!"), ERROR_DIALOG);
1169 return "";
1170 }
1172 // Sort attributes for length
1173 $attrl= array();
1174 foreach ($this->attributes as $attr) {
1175 $attrl[$attr]= strlen($attr);
1176 }
1177 arsort($attrl);
1179 // Walk thru sorted attributes and replace them in pattern
1180 foreach ($attrl as $attr => $dummy) {
1181 if (!is_array($this->$attr)){
1182 $pattern= preg_replace("/%$attr/", $this->$attr, $pattern);
1183 } else {
1184 // Array elements cannot be used for ID generation
1185 if (preg_match("/%$attr/", $pattern)) {
1186 msg_dialog::display(_("Error"), _("Cannot build RDN: invalid attribute parameters!"), ERROR_DIALOG);
1187 break;
1188 }
1189 }
1190 }
1192 // Internally assign value
1193 $this->$attribute= preg_replace('/^[^=]+=/', '', $pattern);
1195 return $pattern;
1196 }
1199 function update_new_dn()
1200 {
1201 // Alternative way to handle DN
1202 $pattern= $this->config->get_cfg_value("accountRDN");
1203 if ($pattern != "") {
1204 $rdn= $this->create_initial_rdn($pattern);
1205 $attribute= preg_replace('/=.*$/', '', $rdn);
1206 $value= preg_replace('/^[^=]+=$/', '', $rdn);
1208 /* Don't touch dn, if $attribute hasn't changed */
1209 if (isset($this->saved_attributes[$attribute]) && $this->saved_attributes[$attribute] == $this->$attribute &&
1210 $this->orig_base == $this->base ){
1211 $this->new_dn= $this->dn;
1212 } else {
1213 $this->new_dn= $this->create_unique_dn2($rdn, get_people_ou().$this->base);
1214 }
1216 // Original way to handle DN
1217 } else {
1219 $pt= "";
1220 if($this->config->get_cfg_value("personalTitleInDN") == "true"){
1221 if(!empty($this->personalTitle)){
1222 $pt = $this->personalTitle." ";
1223 }
1224 }
1226 $this->cn= $pt.$this->givenName." ".$this->sn;
1228 /* Permissions for that base? */
1229 if ($this->config->get_cfg_value("accountPrimaryAttribute") == "uid"){
1230 $this->new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
1231 } else {
1232 /* Don't touch dn, if cn hasn't changed */
1233 if (isset($this->saved_attributes['cn']) && $this->saved_attributes['cn'] == $this->cn &&
1234 $this->orig_base == $this->base ){
1235 $this->new_dn= $this->dn;
1236 } else {
1237 $this->new_dn= $this->create_unique_dn('cn', get_people_ou().$this->base);
1238 }
1239 }
1240 }
1241 }
1244 /* Check formular input */
1245 function check()
1246 {
1247 /* Call common method to give check the hook */
1248 $message= plugin::check();
1250 /* Configurable password methods should be configured initially.
1251 */
1252 if($this->last_pw_storage != $this->pw_storage){
1253 $temp= passwordMethod::get_available_methods();
1254 foreach($temp['name'] as $id => $name){
1255 if($name == $this->pw_storage){
1256 if($temp['is_configurable'][$id] && !$this->pwObject instanceof $temp[$name] ){
1257 $message[] = _("The selected password method requires initial configuration!");
1258 }
1259 break;
1260 }
1261 }
1262 }
1264 $this->update_new_dn();
1266 /* Set the new acl base */
1267 if($this->dn == "new") {
1268 $this->set_acl_base($this->base);
1269 }
1271 /* Check if we are allowed to create/move this user */
1272 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1273 $message[]= msgPool::permCreate();
1274 }elseif($this->orig_dn != "new" && $this->new_dn != $this->orig_dn && !$this->acl_is_moveable($this->base)){
1275 $message[]= msgPool::permMove();
1276 }
1278 /* UID already used? */
1279 $ldap= $this->config->get_ldap_link();
1280 $ldap->cd($this->config->current['BASE']);
1281 $ldap->search("(uid=$this->uid)", array("uid"));
1282 $ldap->fetch();
1283 if ($ldap->count() != 0 && $this->dn == 'new'){
1284 $message[]= msgPool::duplicated(_("Login"));
1285 }
1287 /* In template mode, the uid and givenName are autogenerated... */
1288 if ($this->sn == ""){
1289 $message[]= msgPool::required(_("Name"));
1290 }
1292 // Check if a wrong base was supplied
1293 if(!$this->baseSelector->checkLastBaseUpdate()){
1294 $message[]= msgPool::check_base();;
1295 }
1297 if (!$this->is_template){
1298 if ($this->givenName == ""){
1299 $message[]= msgPool::required(_("Given name"));
1300 }
1301 if ($this->uid == ""){
1302 $message[]= msgPool::required(_("Login"));
1303 }
1304 if ($this->config->get_cfg_value("accountPrimaryAttribute") != "uid"){
1305 $ldap->cat($this->new_dn);
1306 if ($ldap->count() != 0 && $this->dn != $this->new_dn && $this->dn == 'new'){
1307 $message[]= msgPool::duplicated(_("Name"));
1308 }
1309 }
1310 }
1312 /* Check for valid input */
1313 if ($this->is_modified && !tests::is_uid($this->uid)){
1315 if (strict_uid_mode()){
1316 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/");
1317 } else {
1318 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/i");
1319 }
1320 }
1321 if (!tests::is_url($this->labeledURI)){
1322 $message[]= msgPool::invalid(_("Homepage"), "", "", "http://www.your-domain.com/yourname");
1323 }
1325 /* Check phone numbers */
1326 if (!tests::is_phone_nr($this->telephoneNumber)){
1327 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1328 }
1329 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
1330 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1331 }
1332 if (!tests::is_phone_nr($this->mobile)){
1333 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1334 }
1335 if (!tests::is_phone_nr($this->pager)){
1336 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1337 }
1339 /* Check dates */
1340 if (!tests::is_date($this->dateOfBirth)){
1341 $message[]= msgPool::invalid(_("Date of birth"), $this->dateOfBirth,"" ,"23.02.2009");
1342 }
1344 /* Check for reserved characers */
1345 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName)){
1346 $message[]= msgPool::invalid(_("Given name"), $this->givenName, '/[^,+"?\'()=<>;\\\\]/');
1347 }
1348 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn)){
1349 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1350 }
1352 return $message;
1353 }
1356 /* Indicate whether a password change is needed or not */
1357 function password_change_needed()
1358 {
1359 if(in_array("pw_storage",$this->multi_boxes)){
1360 return(TRUE);
1361 }
1362 return($this->pw_storage != $this->last_pw_storage && !$this->is_template);
1363 }
1366 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
1367 function load_picture()
1368 {
1369 $ldap = $this->config->get_ldap_link();
1370 $ldap->cd ($this->dn);
1371 $data = $ldap->get_attribute($this->dn,"jpegPhoto");
1373 if((!$data) || ($data == "*removed*")){
1375 /* In case we don't get an entry, load a default picture */
1376 $this->set_picture ();
1377 $this->jpegPhoto= "*removed*";
1378 }else{
1380 /* Set picture */
1381 $this->photoData= $data;
1382 session::set('binary',$this->photoData);
1383 session::set('binarytype',"image/jpeg");
1384 $this->jpegPhoto= "";
1385 }
1386 }
1389 /* Load a certificate from LDAP, this is going to be simplified later on */
1390 function load_cert()
1391 {
1392 $ds= ldap_connect($this->config->current['SERVER']);
1393 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1394 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true"){
1395 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1396 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1397 }
1398 if ($this->config->get_cfg_value("ldapTLS") == "true"){
1399 ldap_start_tls($ds);
1400 }
1402 $r= ldap_bind($ds);
1403 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1405 if ($sr) {
1406 $ei= @ldap_first_entry($ds, $sr);
1408 if ($ei) {
1409 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1410 $this->userCertificate= "";
1411 } else {
1412 $this->userCertificate= $info[0];
1413 }
1414 }
1415 } else {
1416 $this->userCertificate= "";
1417 }
1419 ldap_unbind($ds);
1420 }
1423 /* Load picture from file to object */
1424 function set_picture($filename ="")
1425 {
1426 if (!is_file($filename) || $filename =="" ){
1427 $filename= "./plugins/users/images/default.jpg";
1428 $this->jpegPhoto= "*removed*";
1429 }
1431 $fd = fopen ($filename, "rb");
1432 $this->photoData= fread ($fd, filesize ($filename));
1433 session::set('binary',$this->photoData);
1434 session::set('binarytype',"image/jpeg");
1435 $this->jpegPhoto= "";
1437 fclose ($fd);
1438 }
1441 /* Load certificate from file to object */
1442 function set_cert($cert, $filename)
1443 {
1444 if(!$this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))) return;
1445 $fd = fopen ($filename, "rb");
1446 if (filesize($filename)>0) {
1447 $this->$cert= fread ($fd, filesize ($filename));
1448 fclose ($fd);
1449 $this->is_modified= TRUE;
1450 } else {
1451 msg_dialog::display(_("Error"), _("Cannot open certificate!"), ERROR_DIALOG);
1452 }
1453 }
1455 /* Adapt from given 'dn' */
1456 function adapt_from_template($dn, $skip= array())
1457 {
1458 plugin::adapt_from_template($dn, $skip);
1460 /* Get password method from template
1461 */
1462 $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
1463 if(is_object($tmp)){
1464 if($tmp->is_configurable()){
1465 $tmp->adapt_from_template($dn);
1466 $this->pwObject = &$tmp;
1467 }
1468 $this->pw_storage= $tmp->get_hash();
1469 }
1471 /* Get base */
1472 $this->base= preg_replace('/^[^,]+,'.preg_quote(get_people_ou(), '/').'/i', '', $dn);
1474 if($this->governmentmode){
1476 /* Walk through govattrs */
1477 foreach ($this->govattrs as $val){
1479 if (in_array($val, $skip)){
1480 continue;
1481 }
1483 if (isset($this->attrs["$val"][0])){
1485 /* If attribute is set, replace dynamic parts:
1486 %sn, %givenName and %uid. Fill these in our local variables. */
1487 $value= $this->attrs["$val"][0];
1489 foreach (array("sn", "givenName", "uid") as $repl){
1490 if (preg_match("/%$repl/i", $value)){
1491 $value= preg_replace ("/%$repl/i",
1492 $this->parent->$repl, $value);
1493 }
1494 }
1495 $this->$val= $value;
1496 }
1497 }
1498 }
1500 /* Get back uid/sn/givenName - only write if nothing's skipped */
1501 if ($this->parent !== NULL && count($skip) == 0){
1502 $this->uid= $this->parent->uid;
1503 $this->sn= $this->parent->sn;
1504 $this->givenName= $this->parent->givenName;
1505 }
1507 if ($this->dateOfBirth) {
1508 /* This entry is ISO 8601 conform */
1509 list($year, $month, $day)= explode("-", $this->dateOfBirth, 3);
1511 #TODO: use $lang to convert date
1512 $this->dateOfBirth= "$day.$month.$year";
1513 }
1514 }
1517 /* This avoids that users move themselves out of their rights.
1518 */
1519 function allowedBasesToMoveTo()
1520 {
1521 /* Get bases */
1522 $bases = $this->get_allowed_bases();
1523 return($bases);
1524 }
1527 function getCopyDialog()
1528 {
1529 $str = "";
1531 session::set('binary',$this->photoData);
1532 session::set('binarytype',"image/jpeg");
1534 /* Get random number for pictures */
1535 srand((double)microtime()*1000000);
1536 $rand = rand(0, 10000);
1538 $smarty = get_smarty();
1540 $smarty->assign("passwordTodo","clear");
1542 if(isset($_POST['passwordTodo'])){
1543 $smarty->assign("passwordTodo",$_POST['passwordTodo']);
1544 }
1546 $smarty->assign("sn", $this->sn);
1547 $smarty->assign("givenName",$this->givenName);
1548 $smarty->assign("uid", $this->uid);
1549 $smarty->assign("rand", $rand);
1550 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1553 $ret = array();
1554 $ret['string'] = $str;
1555 $ret['status'] = "";
1556 return($ret);
1557 }
1559 function saveCopyDialog()
1560 {
1561 /* Set_acl_base */
1562 $this->set_acl_base($this->base);
1564 if((isset($_FILES['picture_file']['tmp_name'])) && ($_FILES['picture_file']['size'] > 0)){
1565 $this->set_picture($_FILES['picture_file']['tmp_name']);
1566 }
1568 /* Remove picture? */
1569 if (isset($_POST['picture_remove'])){
1570 $this->jpegPhoto= "*removed*";
1571 $this->set_picture ("./plugins/users/images/default.jpg");
1572 $this->is_modified= TRUE;
1573 }
1575 $attrs = array("uid","givenName","sn");
1576 foreach($attrs as $attr){
1577 if(isset($_POST[$attr])){
1578 $this->$attr = $_POST[$attr];
1579 }
1580 }
1581 }
1584 function PrepareForCopyPaste($source)
1585 {
1586 plugin::PrepareForCopyPaste($source);
1588 /* Reset certificate information addepted from source user
1589 to avoid setting the same user certificate for the destination user. */
1590 $this->userPKCS12= "";
1591 $this->userSMIMECertificate= "";
1592 $this->userCertificate= "";
1593 $this->certificateSerialNumber= "";
1594 $this->old_certificateSerialNumber= "";
1595 $this->old_userPKCS12= "";
1596 $this->old_userSMIMECertificate= "";
1597 $this->old_userCertificate= "";
1598 }
1601 static function plInfo()
1602 {
1604 $govattrs= array(
1605 "gouvernmentOrganizationalUnit" => _("Unit"),
1606 "houseIdentifier" => _("House identifier"),
1607 "vocation" => _("Vocation"),
1608 "ivbbLastDeliveryCollective" => _("Last delivery"),
1609 "gouvernmentOrganizationalPersonLocality" => _("Person locality"),
1610 "gouvernmentOrganizationalUnitDescription" => _("Unit description"),
1611 "gouvernmentOrganizationalUnitSubjectArea" => _("Subject area"),
1612 "functionalTitle" => _("Functional title"),
1613 "certificateSerialNumber" => _("Certificate serial number"),
1614 "publicVisible" => _("Public visible"),
1615 "street" => _("Street"),
1616 "role" => _("Role"),
1617 "postalCode" => _("Postal code"));
1619 $ret = array(
1620 "plShortName" => _("Generic"),
1621 "plDescription" => _("Generic user settings"),
1622 "plSelfModify" => TRUE,
1623 "plDepends" => array(),
1624 "plPriority" => 1,
1625 "plSection" => array("personal" => _("My account")),
1626 "plCategory" => array("users" => array("description" => _("Users"),
1627 "objectClass" => "gosaAccount")),
1629 "plProvidedAcls" => array(
1631 "sn" => _("Surname"),
1632 "givenName" => _("Given name"),
1633 "uid" => _("User identification"),
1634 "personalTitle" => _("Personal title"),
1635 "academicTitle" => _("Academic title"),
1637 "dateOfBirth" => _("Date of birth"),
1638 "gender" => _("Sex"),
1639 "preferredLanguage" => _("Preferred language"),
1640 "base" => _("Base"),
1642 "userPicture" => _("User picture"),
1644 "gosaLoginRestriction" => _("Login restrictions"),
1646 "o" => _("Organization"),
1647 "ou" => _("Department"),
1648 "departmentNumber" => _("Department number"),
1649 "manager" => _("Manager"),
1650 "employeeNumber" => _("Employee number"),
1651 "employeeType" => _("Employee type"),
1653 "roomNumber" => _("Room number"),
1654 "telephoneNumber" => _("Telefon number"),
1655 "pager" => _("Pager number"),
1656 "mobile" => _("Mobile number"),
1657 "facsimileTelephoneNumber" => _("Fax number"),
1659 "st" => _("State"),
1660 "l" => _("Location"),
1661 "postalAddress" => _("Postal address"),
1663 "homePostalAddress" => _("Home postal address"),
1664 "homePhone" => _("Home phone number"),
1665 "labeledURI" => _("Homepage"),
1666 "userPassword" => _("User password method"),
1667 "Certificate" => _("User certificates"))
1669 );
1671 /* Append government attributes if required */
1672 global $config;
1673 if($config->get_cfg_value("honourIvbbAttributes") == "true"){
1674 foreach($govattrs as $attr => $desc){
1675 $ret["plProvidedAcls"][$attr] = $desc;
1676 }
1677 }
1678 return($ret);
1679 }
1681 function get_multi_edit_values()
1682 {
1683 $ret = plugin::get_multi_edit_values();
1684 if(in_array("pw_storage",$this->multi_boxes)){
1685 $ret['pw_storage'] = $this->pw_storage;
1686 }
1687 if(in_array("edit_picture",$this->multi_boxes)){
1688 $ret['jpegPhoto'] = $this->jpegPhoto;
1689 $ret['photoData'] = $this->photoData;
1690 $ret['old_jpegPhoto'] = $this->old_jpegPhoto;
1691 $ret['old_photoData'] = $this->old_photoData;
1692 }
1693 if(isset($ret['dateOfBirth'])){
1694 unset($ret['dateOfBirth']);
1695 }
1696 if(isset($ret['cn'])){
1697 unset($ret['cn']);
1698 }
1699 $ret['is_modified'] = $this->is_modified;
1700 if(in_array("base",$this->multi_boxes)){
1701 $ret['orig_base']="Changed_by_Multi_Plug";
1702 $ret['base']=$this->base;
1703 }
1705 $ret['gosaLoginRestriction'] = $this->gosaLoginRestriction;
1706 $ret['gosaLoginRestriction_some'] = $this->gosaLoginRestriction_some;
1708 return($ret);
1709 }
1712 function multiple_save_object()
1713 {
1714 plugin::multiple_save_object();
1716 /* Get pw_storage mode */
1717 if (isset($_POST['pw_storage'])){
1718 foreach(array("pw_storage") as $val){
1719 if(isset($_POST[$val])){
1720 $data= validate(get_post($val));
1721 if ($data != $this->$val){
1722 $this->is_modified= TRUE;
1723 }
1724 $this->$val= $data;
1725 }
1726 }
1727 }
1729 /* Refresh base */
1730 if ($this->acl_is_moveable($this->base)){
1731 if (!$this->baseSelector->update()) {
1732 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
1733 }
1734 if ($this->base != $this->baseSelector->getBase()) {
1735 $this->base= $this->baseSelector->getBase();
1736 }
1737 }
1739 if(isset($_POST['user_mulitple_edit'])){
1740 foreach(array("base","pw_storage","edit_picture") as $val){
1741 if(isset($_POST["use_".$val])){
1742 $this->multi_boxes[] = $val;
1743 }
1744 }
1745 }
1747 /* Sync lists */
1748 $this->gosaLoginRestrictionWidget->save_object();
1749 if ($this->gosaLoginRestrictionWidget->isModified()) {
1750 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
1751 }
1752 }
1755 function multiple_check()
1756 {
1757 /* Call check() to set new_dn correctly ... */
1758 $message = plugin::multiple_check();
1760 /* Set the new acl base */
1761 if($this->dn == "new") {
1762 $this->set_acl_base($this->base);
1763 }
1764 if (!tests::is_url($this->labeledURI) && in_array("labeledURI",$this->multi_boxes)){
1765 $message[]= msgPool::invalid(_("Homepage"));
1766 }
1767 if (!tests::is_phone_nr($this->telephoneNumber) && in_array("telephoneNumber",$this->multi_boxes)){
1768 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1769 }
1770 if (!tests::is_phone_nr($this->facsimileTelephoneNumber) && in_array("facsimileTelephoneNumber",$this->multi_boxes)){
1771 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1772 }
1773 if (!tests::is_phone_nr($this->mobile) && in_array("mobile",$this->multi_boxes)){
1774 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1775 }
1776 if (!tests::is_phone_nr($this->pager) && in_array("pager",$this->multi_boxes)){
1777 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1778 }
1779 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName) && in_array("givenName",$this->multi_boxes)){
1780 $message[]= msgPool::invalid(_("Given name"), $this->giveName, '/[^,+"?\'()=<>;\\\\]/');
1781 }
1782 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn) && in_array("sn",$this->multi_boxes)){
1783 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1784 }
1785 return($message);
1786 }
1790 function multiple_execute()
1791 {
1792 return($this->execute());
1793 }
1796 /*! \brief Prepares the plugin to be used for multiple edit
1797 * Update plugin attributes with given array of attribtues.
1798 * \param array Array with attributes that must be updated.
1799 */
1800 function init_multiple_support($attrs,$all)
1801 {
1802 plugin::init_multiple_support($attrs,$all);
1804 // Get login restrictions
1805 if(isset($attrs['gosaLoginRestriction'])){
1806 $this->gosaLoginRestriction =array();
1807 for($i =0;$i < $attrs['gosaLoginRestriction']['count']; $i++){
1808 $this->gosaLoginRestriction[] = $attrs['gosaLoginRestriction'][$i];
1809 }
1810 }
1812 // Detect login restriction not used in all user objects.
1813 $this->gosaLoginRestriction_some = array();
1814 if(isset($all['gosaLoginRestriction'])){
1815 for($i=0;$i<$all['gosaLoginRestriction']['count'];$i++){
1816 $this->gosaLoginRestriction_some[] = $all['gosaLoginRestriction'][$i];
1817 }
1818 }
1821 // Reinit the login restriction list.
1822 $data = $this->convertLoginRestriction();
1823 if(count($data)){
1824 $this->gosaLoginRestrictionWidget->setListData($data['data'], $data['displayData']);
1825 }
1826 }
1829 function set_multi_edit_values($attrs)
1830 {
1831 $lR = array();
1833 // Update loginRestrictions, keep my settings while ip is optional
1834 foreach($attrs['gosaLoginRestriction_some'] as $ip){
1835 if(in_array($ip, $this->gosaLoginRestriction) && in_array($ip, $attrs['gosaLoginRestriction'])){
1836 $lR[] = $ip;
1837 }
1838 }
1840 // Add enforced loginRestrictions
1841 foreach($attrs['gosaLoginRestriction'] as $ip){
1842 $lR[] = $ip;
1843 }
1845 $lR = array_values(array_unique($lR));
1846 $this->is_modified |= array_differs($this->gosaLoginRestriction, $lR);
1847 plugin::set_multi_edit_values($attrs);
1848 $this->gosaLoginRestriction = $lR;
1849 }
1852 function convertLoginRestriction()
1853 {
1854 $all = array_unique(array_merge($this->gosaLoginRestriction,$this->gosaLoginRestriction_some));
1855 $data = array();
1856 foreach($all as $ip){
1857 $data['data'][] = $ip;
1858 if(!in_array($ip, $this->gosaLoginRestriction)){
1859 $data['displayData'][] = array('mode' => LIST_MARKED , 'data' => array($ip.' ('._("Entries differ").')'));
1860 }else{
1861 $data['displayData'][] = array('mode' => 0 , 'data' => array($ip));
1862 }
1863 }
1864 return($data);
1865 }
1866 }
1868 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1869 ?>