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 = "";
112 var $manager_name = "";
115 /* attribute list for save action */
116 var $attributes= array("sn", "givenName", "uid", "personalTitle", "academicTitle",
117 "homePostalAddress", "homePhone", "labeledURI", "ou", "o", "dateOfBirth", "gender","preferredLanguage",
118 "departmentNumber", "employeeNumber", "employeeType", "l", "st","jpegPhoto",
119 "roomNumber", "telephoneNumber", "mobile", "pager", "cn", "userPKCS12",
120 "postalAddress", "facsimileTelephoneNumber", "userSMIMECertificate", "gosaLoginRestriction", "manager");
122 var $objectclasses= array("top", "person", "organizationalPerson", "inetOrgPerson",
123 "gosaAccount");
125 /* attributes that are part of the government mode */
126 var $govattrs= array("gouvernmentOrganizationalUnit", "houseIdentifier", "vocation",
127 "ivbbLastDeliveryCollective", "gouvernmentOrganizationalPersonLocality",
128 "gouvernmentOrganizationalUnitDescription","gouvernmentOrganizationalUnitSubjectArea",
129 "functionalTitle", "certificateSerialNumber", "publicVisible", "street", "role",
130 "postalCode");
132 var $multiple_support = TRUE;
134 var $governmentmode = FALSE;
136 /* constructor, if 'dn' is set, the node loads the given
137 'dn' from LDAP */
138 function user (&$config, $dn= NULL)
139 {
140 global $lang;
142 $this->config= $config;
143 /* Configuration is fine, allways */
144 if($this->config->get_cfg_value("honourIvbbAttributes") == "true"){
145 $this->governmentmode = TRUE;
146 $this->attributes=array_merge($this->attributes,$this->govattrs);
147 }
149 /* Load base attributes */
150 plugin::plugin ($config, $dn);
152 $this->orig_dn = $this->dn;
153 $this->new_dn = $dn;
155 if ($this->governmentmode){
156 /* Fix public visible attribute if unset */
157 if (!isset($this->attrs['publicVisible'])){
158 $this->publicVisible == "nein";
159 }
160 }
162 /* Load government mode attributes */
163 if ($this->governmentmode){
164 /* Copy all attributs */
165 foreach ($this->govattrs as $val){
166 if (isset($this->attrs["$val"][0])){
167 $this->$val= $this->attrs["$val"][0];
168 }
169 }
170 }
172 /* Create me for new accounts */
173 if ($dn == "new"){
174 $this->is_account= TRUE;
175 }
177 /* Make hash default to md5 if not set in config */
178 $hash= $this->config->get_cfg_value("passwordDefaultHash", "crypt/md5");
180 /* Load data from LDAP? */
181 if ($dn !== NULL){
183 /* Do base conversation */
184 if ($this->dn == "new"){
185 $ui= get_userinfo();
186 $this->base= dn2base(session::global_is_set("CurrentMainBase")?"cn=dummy,".session::global_get("CurrentMainBase"):$ui->dn);
187 } else {
188 $this->base= dn2base($dn);
189 }
191 /* get password storage type */
192 if (isset ($this->attrs['userPassword'][0])){
193 /* Initialize local array */
194 $matches= array();
195 if (preg_match ("/^{[^}]+}/", $this->attrs['userPassword'][0])){
196 $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
197 if(is_object($tmp)){
198 $this->pw_storage= $tmp->get_hash();
199 }
201 } else {
202 if ($this->attrs['userPassword'][0] != ""){
203 $this->pw_storage= "clear";
204 } else {
205 $this->pw_storage= $hash;
206 }
207 }
208 } else {
209 /* Preset with vaule from configuration */
210 $this->pw_storage= $hash;
211 }
213 /* Load extra attributes: certificate and picture */
214 $this->load_cert();
215 $this->load_picture();
216 if ($this->userCertificate != ""){
217 $this->had_userCertificate= TRUE;
218 }
219 }
221 /* Reset password storage indicator, used by password_change_needed() */
222 if ($dn == "new"){
223 $this->last_pw_storage= "unset";
224 } else {
225 $this->last_pw_storage= $this->pw_storage;
226 }
228 /* Generate dateOfBirth entry */
229 if (isset ($this->attrs['dateOfBirth'])){
230 /* This entry is ISO 8601 conform */
231 list($year, $month, $day)= explode("-", $this->attrs['dateOfBirth'][0], 3);
233 #TODO: use $lang to convert date
234 $this->dateOfBirth= "$day.$month.$year";
235 } else {
236 $this->dateOfBirth= "";
237 }
239 /* Put gender attribute to upper case */
240 if (isset ($this->attrs['gender'])){
241 $this->gender= strtoupper($this->attrs['gender'][0]);
242 }
244 // Get login restrictions
245 if(isset($this->attrs['gosaLoginRestriction'])){
246 $this->gosaLoginRestriction =array();
247 for($i =0;$i < $this->attrs['gosaLoginRestriction']['count']; $i++){
248 $this->gosaLoginRestriction[] = $this->attrs['gosaLoginRestriction'][$i];
249 }
250 }
251 $this->gosaLoginRestrictionWidget= new sortableListing($this->gosaLoginRestriction);
252 $this->gosaLoginRestrictionWidget->setDeleteable(true);
253 $this->gosaLoginRestrictionWidget->setColspecs(array('*'));
254 $this->gosaLoginRestrictionWidget->setWidth("100%");
255 $this->gosaLoginRestrictionWidget->setHeight("70px");
257 $this->orig_base = $this->base;
258 $this->baseSelector= new baseSelector($this->allowedBasesToMoveTo(), $this->base);
259 $this->baseSelector->setSubmitButton(false);
260 $this->baseSelector->setHeight(300);
261 $this->baseSelector->update(true);
264 // Detect the managers name
265 $this->manager_name = "";
266 $ldap = $this->config->get_ldap_link();
267 if(!empty($this->manager)){
268 $ldap->cat($this->manager, array('cn'));
269 if($ldap->count()){
270 $attrs = $ldap->fetch();
271 $this->manager_name = $attrs['cn'][0];
272 }else{
273 $this->manager_name = "("._("Unknown")."!): ".$this->manager;
274 }
275 }
276 }
279 /* execute generates the html output for this node */
280 function execute()
281 {
282 /* Call parent execute */
283 plugin::execute();
285 /* Set list ACL */
286 $this->gosaLoginRestrictionWidget->setAcl($this->getacl('gosaLoginRestriction'));
287 $this->gosaLoginRestrictionWidget->update();
289 /* Handle add/delete for restriction mode */
290 if (isset($_POST['add_res']) && isset($_POST['res'])) {
291 $val= validate($_POST['res']);
292 if (preg_match('/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/', $val) ||
293 preg_match('/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+)$/', $val) ||
294 preg_match('/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/', $val)) {
295 $this->gosaLoginRestrictionWidget->addEntry($val);
296 } else {
297 msg_dialog::display(_("Error"), _("Please add a single IP address or a network/netmask combination!"), ERROR_DIALOG);
298 }
299 }
301 /* Log view */
302 if($this->is_account && !$this->view_logged){
303 $this->view_logged = TRUE;
304 new log("view","users/".get_class($this),$this->dn);
305 }
307 // Clear manager attribute if requested
308 if(preg_match("/ removeManager/i", " ".implode(array_keys($_POST),' ')." ")){
309 $this->manager = "";
310 $this->manager_name = "";
311 }
313 // Allow to select a new inetOrgPersion:manager
314 if(preg_match("/ editManager/i", " ".implode(array_keys($_POST),' ')." ")){
315 $this->dialog = new singleUserSelect($this->config, get_userinfo());
316 }
317 if($this->dialog && $this->dialog instanceOf singleUserSelect && count($this->dialog->detectPostActions())){
318 $users = $this->dialog->detectPostActions();
319 if(isset($users['targets']) && count($users['targets'])){
321 $headpage = $this->dialog->getHeadpage();
322 $dn = $users['targets'][0];
323 $attrs = $headpage->getEntry($dn);
324 $this->manager = $dn;
325 $this->manager_name = $attrs['cn'][0];
326 $this->dialog = NULL;
327 }
328 }
329 if(isset($_POST['add_users_cancel'])){
330 $this->dialog = NULL;
331 }
332 if($this->dialog instanceOf singleUserSelect) return($this->dialog->execute());
335 $smarty= get_smarty();
336 $smarty->assign("usePrototype", "true");
337 $smarty->assign("gosaLoginRestrictionWidget", $this->gosaLoginRestrictionWidget->render());
339 /* Assign sex */
340 $sex= array(0 => " ", "F" => _("female"), "M" => _("male"));
341 $smarty->assign("gender_list", $sex);
342 $language= array_merge(array(0 => " ") ,get_languages(TRUE));
343 $smarty->assign("preferredLanguage_list", $language);
345 /* Get random number for pictures */
346 srand((double)microtime()*1000000);
347 $smarty->assign("rand", rand(0, 10000));
350 /* Do we represent a valid gosaAccount? */
351 if (!$this->is_account){
352 $str = "<img alt=\"\" src=\"images/small-error.png\" align=\"middle\"> <b>".
353 msgPool::noValidExtension("GOsa")."</b>";
354 return($str);
355 }
357 /* Password configure dialog handling */
358 if(is_object($this->pwObject) && $this->pwObject->display){
359 $output= $this->pwObject->configure();
360 if ($output != ""){
361 $this->dialog= TRUE;
362 return $output;
363 }
364 $this->dialog= false;
365 }
367 /* Dialog handling */
368 if(is_object($this->dialog)){
369 /* Must be called before save_object */
370 $this->dialog->save_object();
372 if($this->dialog->isClosed()){
373 $this->dialog = false;
374 }elseif($this->dialog->isSelected()){
376 /* check if selected base is allowed to move to / create a new object */
377 $tmp = $this->get_allowed_bases();
378 if(isset($tmp[$this->dialog->isSelected()])){
379 $this->base = $this->dialog->isSelected();
380 }
381 $this->dialog= false;
382 }else{
383 return($this->dialog->execute());
384 }
385 }
387 /* Want password method editing? */
388 if ($this->acl_is_writeable("userPassword")){
389 if (isset($_POST['edit_pw_method'])){
390 if (!is_object($this->pwObject) || $this->pw_storage != $this->pwObject->get_hash_name()){
391 $temp= passwordMethod::get_available_methods();
392 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
393 }
394 $this->pwObject->display = TRUE;
395 $this->dialog= TRUE;
396 pathNavigator::registerPlugin(_("Password configuration"));
397 return ($this->pwObject->configure());
398 }
399 }
401 /* Want picture edit dialog? */
402 if($this->acl_is_writeable("userPicture")) {
403 if (isset($_POST['edit_picture'])){
404 /* Save values for later recovery, in case some presses
405 the cancel button. */
406 $this->old_jpegPhoto= $this->jpegPhoto;
407 $this->old_photoData= $this->photoData;
408 $this->picture_dialog= TRUE;
409 $this->dialog= TRUE;
410 }
411 }
413 /* Remove picture? */
414 if($this->acl_is_writeable("userPicture")){
415 if (isset($_POST['picture_remove'])){
416 $this->set_picture ();
417 $this->jpegPhoto= "*removed*";
418 $this->is_modified= TRUE;
419 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
420 }
421 }
423 /* Save picture */
424 if (isset($_POST['picture_edit_finish'])){
426 /* Check for clean upload */
427 if ($_FILES['picture_file']['name'] != ""){
428 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
429 msg_dialog::display(_("Error"), _("Cannot upload file!"), ERROR_DIALOG);
430 }else{
431 /* Activate new picture */
432 $this->set_picture($_FILES['picture_file']['tmp_name']);
433 }
434 }
435 $this->picture_dialog= FALSE;
436 $this->dialog= FALSE;
437 $this->is_modified= TRUE;
438 }
441 /* Cancel picture */
442 if (isset($_POST['picture_edit_cancel'])){
444 /* Restore values */
445 $this->jpegPhoto= $this->old_jpegPhoto;
446 $this->photoData= $this->old_photoData;
448 /* Update picture */
449 session::set('binary',$this->photoData);
450 session::set('binarytype',"image/jpeg");
451 $this->picture_dialog= FALSE;
452 $this->dialog= FALSE;
453 }
455 /* Want certificate= */
456 if ((isset($_POST['edit_cert'])) && $this->acl_is_readable("Certificate")){
458 /* Save original values for later reconstruction */
459 foreach (array("certificateSerialNumber", "userCertificate",
460 "userSMIMECertificate", "userPKCS12") as $val){
462 $oval= "old_$val";
463 $this->$oval= $this->$val;
464 }
466 $this->cert_dialog= TRUE;
467 $this->dialog= TRUE;
468 }
471 /* Cancel certificate dialog */
472 if (isset($_POST['cert_edit_cancel'])){
474 /* Restore original values in case of 'cancel' */
475 foreach (array("certificateSerialNumber", "userCertificate",
476 "userSMIMECertificate", "userPKCS12") as $val){
478 $oval= "old_$val";
479 $this->$val= $this->$oval;
480 }
481 $this->cert_dialog= FALSE;
482 $this->dialog= FALSE;
483 }
486 /* Remove certificate? */
487 if($this->acl_is_writeable("Certificate")){
488 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
489 if (isset($_POST["remove_$val"])){
491 /* Reset specified cert*/
492 $this->$val= "";
493 $this->is_modified= TRUE;
494 }
495 }
496 }
498 /* Upload new cert and close dialog? */
499 if($this->acl_is_writeable("Certificate")){
500 $fail =false;
501 if (isset($_POST['cert_edit_finish'])){
503 /* for all certificates do */
504 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
505 as $val){
507 /* Check for clean upload */
508 if (array_key_exists($val."_file", $_FILES) &&
509 array_key_exists('name', $_FILES[$val."_file"]) &&
510 $_FILES[$val."_file"]['name'] != "" &&
511 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
512 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
513 }
514 }
516 /* Save serial number */
517 if (isset($_POST["certificateSerialNumber"]) &&
518 $_POST["certificateSerialNumber"] != ""){
520 if (!tests::is_id($_POST["certificateSerialNumber"])){
521 $fail = true;
522 msg_dialog::display(_("Error"), msgPool::invalid(_("Serial number"),$_POST["certificateSerialNumber"],"/[0-9]/"),ERROR_DIALOG);
524 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
525 if ($this->$cert != ""){
526 $smarty->assign("$cert"."_state", "true");
527 } else {
528 $smarty->assign("$cert"."_state", "");
529 }
530 }
531 }
533 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
534 $this->is_modified= TRUE;
535 }
536 if(!$fail){
537 $this->cert_dialog= FALSE;
538 $this->dialog= FALSE;
539 }
540 }
541 }
542 /* Display picture dialog */
543 if ($this->picture_dialog){
544 pathNavigator::registerPlugin(_("User picture"));
545 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
546 }
548 /* Display cert dialog */
549 if ($this->cert_dialog){
550 pathNavigator::registerPlugin(_("Certificates"));
551 $smarty->assign("CertificateACL",$this->getacl("Certificate"));
552 $smarty->assign("Certificate_readable",$this->acl_is_readable("Certificate"));
553 $smarty->assign("certificateSerialNumber",$this->certificateSerialNumber);
555 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
556 if ($this->$cert != ""){
557 /* import certificate */
558 $certificate = new certificate;
559 $certificate->import($this->$cert);
561 /* Read out data*/
562 $timeto = $certificate->getvalidto_date();
563 $timefrom = $certificate->getvalidfrom_date();
566 /* Additional info if start end time is '0' */
567 $add_str_info = "";
568 if($timeto == 0 && $timefrom == 0){
569 $add_str_info = "<br><i>"._("(Some types of certificates are currently not supported and may be displayed as 'invalid'.)")."</i>";
570 }
572 $str = "<table \"\" border=0 summary='"._("Certificates")."'>
573 <tr>
574 <td>CN</td>
575 <td>".preg_replace("/ /", " ", $certificate->getname())."</td>
576 </tr>
577 </table><br>".
579 sprintf(_("Certificate is valid from %s to %s and is currently %s."),
580 "<b>".date('d M Y',$timefrom)."</b>",
581 "<b>".date('d M Y',$timeto)."</b>",
582 $certificate->isvalid()?"<b><font style='color:green'>"._("valid")."</font></b>":
583 "<b><font style='color:red'>"._("invalid")."</font></b>").$add_str_info;
585 $smarty->assign($cert."info",$str);
586 $smarty->assign($cert."_state","true");
587 } else {
588 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
589 $smarty->assign($cert."_state","");
590 }
591 }
593 if($this->governmentmode){
594 $smarty->assign("honourIvbbAttributes", "true");
595 }else{
596 $smarty->assign("honourIvbbAttributes", "false");
597 }
598 $smarty->assign("governmentmode", $this->governmentmode);
599 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
600 }
602 /* Prepare password hashes */
603 if ($this->pw_storage == ""){
604 $this->pw_storage= $this->config->get_cfg_value("hash");
605 }
607 $temp= passwordMethod::get_available_methods();
608 $is_configurable= FALSE;
609 $hashes = $temp['name'];
610 if(isset($temp[$this->pw_storage])){
611 $test= new $temp[$this->pw_storage]($this->config);
612 $is_configurable= $test->is_configurable();
613 }else{
614 new msg_dialog(_("Password method"),_("The selected password method is no longer available."),WARNING_DIALOG);
615 }
618 /* Create password methods array */
619 $pwd_methods = array();
620 foreach($hashes as $id => $name){
621 if(!empty($temp['desc'][$id])){
622 $pwd_methods[$name] = $name." (".$temp['desc'][$id].")";
623 }else{
624 $pwd_methods[$name] = $name;
625 }
626 }
628 /* Load attributes and acl's */
629 $ui =get_userinfo();
630 foreach($this->attributes as $val){
631 $smarty->assign("$val", $this->$val);
632 if(in_array($val,$this->multi_boxes)){
633 $smarty->assign("use_".$val,TRUE);
634 }else{
635 $smarty->assign("use_".$val,FALSE);
636 }
637 }
638 foreach(array("base","pw_storage","edit_picture") as $val){
639 if(in_array($val,$this->multi_boxes)){
640 $smarty->assign("use_".$val,TRUE);
641 }else{
642 $smarty->assign("use_".$val,FALSE);
643 }
644 }
646 /* Set acls */
647 $tmp = $this->plinfo();
648 foreach($tmp['plProvidedAcls'] as $val => $translation){
649 $smarty->assign("$val"."ACL", $this->getacl($val));
650 }
652 // Special ACL for gosaLoginRestrictions -
653 // In case of multiple edit, we need a readonly ACL for the list.
654 $smarty->assign('gosaLoginRestriction_ONLY_R_ACL', preg_replace("/[^r]/i","", $this->getacl($val)));
656 $smarty->assign("pwmode", $pwd_methods);
657 $smarty->assign("pwmode_select", $this->pw_storage);
658 $smarty->assign("pw_configurable", $is_configurable);
659 $smarty->assign("passwordStorageACL", $this->getacl("userPassword"));
660 $smarty->assign("CertificatesACL", $this->getacl("Certificate"));
661 $smarty->assign("userPictureACL", $this->getacl("userPicture"));
662 $smarty->assign("userPicture_is_readable", $this->acl_is_readable("userPicture"));
664 /* Create base acls */
665 $smarty->assign("base", $this->baseSelector->render());
667 /* Save government mode attributes */
668 if($this->governmentmode){
669 $smarty->assign("governmentmode", "true");
670 $ivbbmodes= array("nein", "", "ivbv", "testa", "ivbv,testa", "internet",
671 "internet,ivbv", "internet,testa", "internet,ivbv,testa");
672 $smarty->assign("ivbbmodes", $ivbbmodes);
673 foreach ($this->govattrs as $val){
674 $smarty->assign("$val", $this->$val);
675 $smarty->assign("$val"."ACL", $this->getacl($val));
676 }
677 } else {
678 $smarty->assign("governmentmode", "false");
679 }
681 /* Special mode for uid */
682 $uidACL= $this->getacl("uid");
683 if (isset ($this->dn)){
684 if ($this->dn != "new"){
685 $uidACL= preg_replace("/w/","",$uidACL);
686 }
687 } else {
688 $uidACL= preg_replace("/w/","",$uidACL);
689 }
691 $smarty->assign("uidACL", $uidACL);
692 $smarty->assign("is_template", $this->is_template);
693 $smarty->assign("use_dob", $this->use_dob);
695 if (isset($this->parent)){
696 if (isset($this->parent->by_object['phoneAccount']) &&
697 $this->parent->by_object['phoneAccount']->is_account){
698 $smarty->assign("has_phoneaccount", "true");
699 } else {
700 $smarty->assign("has_phoneaccount", "false");
701 }
702 } else {
703 $smarty->assign("has_phoneaccount", "false");
704 }
705 $smarty->assign("multiple_support" , $this->multiple_support_active);
706 $smarty->assign("manager_name",$this->manager_name);
707 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
708 }
711 /* remove object from parent */
712 function remove_from_parent()
713 {
714 /* Only remove valid accounts */
715 if(!$this->initially_was_account) return;
717 /* Remove password extension */
718 $temp= passwordMethod::get_available_methods();
720 /* Remove password method from user account */
721 if(isset($temp[$this->pw_storage]) && class_available($temp[$this->pw_storage])){
722 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
723 $this->pwObject->remove_from_parent();
724 }
726 /* Remove user */
727 $ldap= $this->config->get_ldap_link();
728 $ldap->rmdir ($this->dn);
729 if (!$ldap->success()){
730 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
731 }
733 new log("remove","users/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
735 /* Delete references to groups */
736 $ldap->cd ($this->config->current['BASE']);
737 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
738 while ($ldap->fetch()){
739 $g= new group($this->config, $ldap->getDN());
740 $g->removeUser($this->uid);
741 $g->save ();
742 }
744 /* Delete references to object groups */
745 $ldap->cd ($this->config->current['BASE']);
746 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
747 while ($ldap->fetch()){
748 $og= new ogroup($this->config, $ldap->getDN());
749 unset($og->member[$this->dn]);
750 $og->member= array_values($og->member);
751 $og->save ();
752 }
754 // Update 'manager' attributes from gosaDepartment and inetOrgPerson
755 $filter = "(&(objectClass=inetOrgPerson)(manager=".LDAP::prepare4filter($this->dn)."))";
756 $ocs = $ldap->get_objectclasses();
757 if(isset($ocs['gosaDepartment']['MAY']) && in_array('manager', $ocs['gosaDepartment']['MAY'])){
758 $filter = "(|".$filter."(&(objectClass=gosaDepartment)(manager=".LDAP::prepare4filter($this->dn).")))";
759 }
760 $leaf_deps= get_list($filter,array("all"),$this->config->current['BASE'],
761 array("manager","dn","objectClass"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
762 foreach($leaf_deps as $entry){
763 $update = array('manager' => array());
764 $ldap->cd($entry['dn']);
765 $ldap->modify($update);
766 if(!$ldap->success()){
767 trigger_error(sprintf("Failed to update manager for '%s', error was '%s'", $entry['dn'], $ldap->get_error()));
768 }
769 }
771 /* Delete references to roles */
772 $ldap->cd ($this->config->current['BASE']);
773 $ldap->search ("(&(objectClass=organizationalRole)(roleOccupant=".LDAP::prepare4filter($this->dn)."))", array("cn"));
774 while ($ldap->fetch()){
775 $role= new roleGeneric($this->config, $ldap->getDN());
776 $key = array_search($this->dn,$role->roleOccupant);
777 if($key !== FALSE){
778 unset($role->roleOccupant[$key]);
779 $role->roleOccupant= array_values($role->roleOccupant);
780 $role->save ();
781 }
782 }
784 /* If needed, let the password method do some cleanup */
785 $tmp = new passwordMethod($this->config);
786 $available = $tmp->get_available_methods();
787 if (in_array_ics($this->pw_storage, $available['name'])){
788 $test= new $available[$this->pw_storage]($this->config);
789 $test->attrs= $this->attrs;
790 $test->dn= $this->dn;
791 $test->remove_from_parent();
792 }
794 /* Remove ACL dependencies too */
795 acl::remove_acl_for($this->dn);
797 /* Optionally execute a command after we're done */
798 $this->handle_post_events("remove",array("uid" => $this->uid));
799 }
802 /* Save data to object */
803 function save_object()
804 {
805 if(isset($_POST['generic']) || isset($_POST['multiple_user_posted'])){
807 /* Make a backup of the current selected base */
808 $base_tmp = $this->base;
810 /* Parents save function */
811 plugin::save_object ();
813 /* Refresh base */
814 if ($this->acl_is_moveable($this->base)){
815 if (!$this->baseSelector->update()) {
816 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
817 }
818 if ($this->base != $this->baseSelector->getBase()) {
819 $this->base= $this->baseSelector->getBase();
820 $this->is_modified= TRUE;
821 }
822 }
824 /* Sync lists */
825 $this->gosaLoginRestrictionWidget->save_object();
826 if ($this->gosaLoginRestrictionWidget->isModified()) {
827 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
828 }
830 /* Save government mode attributes */
831 if ($this->governmentmode){
832 foreach ($this->govattrs as $val){
833 if ($this->acl_is_writeable($val)){
834 $data= stripcslashes($_POST["$val"]);
835 if ($data != $this->$val){
836 $this->is_modified= TRUE;
837 }
838 $this->$val= $data;
839 }
840 }
841 }
843 /* In template mode, the uid is autogenerated... */
844 if ($this->is_template){
845 $this->uid= strtolower($this->sn);
846 $this->givenName= $this->sn;
847 }
849 /* Get pw_storage mode */
850 if (isset($_POST['pw_storage'])){
851 foreach(array("pw_storage") as $val){
852 if(isset($_POST[$val])){
853 $data= validate($_POST[$val]);
854 if ($data != $this->$val){
855 $this->is_modified= TRUE;
856 }
857 $this->$val= $data;
858 }
859 }
860 }
862 if($this->pw_storage != $this->last_pw_storage && isset($_POST['pw_storage'])){
863 if ($this->acl_is_writeable("userPassword")){
864 $temp= passwordMethod::get_available_methods();
865 if (!is_object($this->pwObject) || !($this->pwObject instanceOf $temp[$this->pw_storage])){
866 foreach($temp as $id => $data){
867 if(isset($data['name']) && $data['name'] == $this->pw_storage && $data['is_configurable']){
868 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
869 break;
870 }
871 }
872 }
873 }
874 }
876 /* Save current cn
877 */
878 $this->cn = $this->givenName." ".$this->sn;
879 }
880 }
882 function rebind($ldap, $referral)
883 {
884 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
885 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
886 $this->error = "Success";
887 $this->hascon=true;
888 $this->reconnect= true;
889 return (0);
890 } else {
891 $this->error = "Could not bind to " . $credentials['ADMIN'];
892 return NULL;
893 }
894 }
897 /* Save data to LDAP, depending on is_account we save or delete */
898 function save()
899 {
900 global $lang;
902 /* Only force save of changes ....
903 If this attributes aren't changed, avoid saving.
904 */
906 if($this->gender=="0") $this->gender ="";
907 if($this->preferredLanguage=="0") $this->preferredLanguage ="";
909 /* First use parents methods to do some basic fillup in $this->attrs */
910 plugin::save ();
912 if ($this->dateOfBirth != ""){
913 if(!is_array($this->attrs['dateOfBirth'])) {
914 #TODO: use $lang to convert date
915 list($day, $month, $year)= explode(".", $this->dateOfBirth);
916 $this->attrs['dateOfBirth'] = sprintf("%04d-%02d-%02d", $year, $month, $day);
917 }
918 }
920 /* Remove additional objectClasses */
921 $tmp= array();
922 foreach ($this->attrs['objectClass'] as $key => $set){
923 $found= false;
924 foreach (array("ivbbentry", "gosaUserTemplate") as $val){
925 if (preg_match ("/^$set$/i", $val)){
926 $found= true;
927 break;
928 }
929 }
930 if (!$found){
931 $tmp[]= $set;
932 }
933 }
935 /* Replace the objectClass array. This is done because of the
936 separation into government and normal mode. */
937 $this->attrs['objectClass']= $tmp;
939 /* Add objectClasss for template mode? */
940 if ($this->is_template){
941 $this->attrs['objectClass'][]= "gosaUserTemplate";
942 }
944 /* Hard coded government mode? */
945 if ($this->governmentmode){
946 $this->attrs['objectClass'][]= "ivbbentry";
948 /* Copy standard attributes */
949 foreach ($this->govattrs as $val){
950 if ($this->$val != ""){
951 $this->attrs["$val"]= $this->$val;
952 } elseif (!$this->is_new) {
953 $this->attrs["$val"]= array();
954 }
955 }
957 /* Remove attribute if set to "nein" */
958 if ($this->publicVisible == "nein"){
959 $this->attrs['publicVisible']= array();
960 if($this->is_new){
961 unset($this->attrs['publicVisible']);
962 }else{
963 $this->attrs['publicVisible']=array();
964 }
966 }
968 }
970 /* Special handling for attribute userCertificate needed */
971 if ($this->userCertificate != ""){
972 $this->attrs["userCertificate;binary"]= $this->userCertificate;
973 $remove_userCertificate= false;
974 } else {
975 $remove_userCertificate= true;
976 }
978 /* Special handling for dateOfBirth value */
979 if ($this->dateOfBirth == ""){
980 if ($this->is_new) {
981 unset($this->attrs["dateOfBirth"]);
982 } else {
983 $this->attrs["dateOfBirth"]= array();
984 }
985 }
986 if (!$this->gender){
987 if ($this->is_new) {
988 unset($this->attrs["gender"]);
989 } else {
990 $this->attrs["gender"]= array();
991 }
992 }
993 if (!$this->preferredLanguage){
994 if ($this->is_new) {
995 unset($this->attrs["preferredLanguage"]);
996 } else {
997 $this->attrs["preferredLanguage"]= array();
998 }
999 }
1001 /* Special handling for attribute jpegPhote needed, scale image via
1002 image magick to 147x200 pixels and inject resulting data. */
1003 if ($this->jpegPhoto == "*removed*"){
1005 /* Reset attribute to avoid writing *removed* as value */
1006 $this->attrs["jpegPhoto"] = array();
1008 } else {
1010 /* Fallback if there's no image magick inside PHP */
1011 if (!function_exists("imagick_blob2image")){
1012 /* Get temporary file name for conversation */
1013 $fname = tempnam (TEMP_DIR, "GOsa");
1015 /* Open file and write out photoData */
1016 $fp = fopen ($fname, "w");
1017 fwrite ($fp, $this->photoData);
1018 fclose ($fp);
1020 /* Build conversation query. Filename is generated automatically, so
1021 we do not need any special security checks. Exec command and save
1022 output. For PHP safe mode, you'll need a configuration which respects
1023 image magick as executable... */
1024 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
1025 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
1026 $query, "Execute");
1028 /* Read data written by convert */
1029 $output= "";
1030 $sh= popen($query, 'r');
1031 while (!feof($sh)){
1032 $output.= fread($sh, 4096);
1033 }
1034 pclose($sh);
1036 unlink($fname);
1038 /* Save attribute */
1039 $this->attrs["jpegPhoto"] = $output;
1041 } else {
1043 /* Load the new uploaded Photo */
1044 if(!$handle = imagick_blob2image($this->photoData)) {
1045 new log("debug","users/".get_class($this),$this->dn,array(),"Could not access uploaded image");
1046 }
1048 /* Resizing image to 147x200 and blur */
1049 if(!imagick_resize($handle,147,200,IMAGICK_FILTER_GAUSSIAN,0)){
1050 new log("debug","users/".get_class($this),$this->dn,array(),"Could not resize uploaded image");
1051 }
1053 /* Converting image to JPEG */
1054 if(!imagick_convert($handle,"JPEG")) {
1055 new log("debug","users/".get_class($this),$this->dn,array(),"Could not convert uploaded image to jepg");
1056 }
1058 /* Creating binary Code for the Image */
1059 if(!$dump = imagick_image2blob($handle)){
1060 new log("debug","users/".get_class($this),$this->dn,array(),"Could not create new user image");
1061 }
1063 /* Sending Image */
1064 $output= $dump;
1066 /* Save attribute */
1067 $this->attrs["jpegPhoto"] = $output;
1068 }
1070 }
1072 /* This only gets called when user is renaming himself */
1073 $ldap= $this->config->get_ldap_link();
1074 if ($this->dn != $this->new_dn){
1076 /* Write entry on new 'dn' */
1077 $this->update_acls($this->dn,$this->new_dn);
1078 $this->move($this->dn, $this->new_dn);
1080 /* Happen to use the new one */
1081 change_ui_dn($this->dn, $this->new_dn);
1082 $this->dn= $this->new_dn;
1083 }
1086 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1087 new entries. So do a check first... */
1088 $ldap->cat ($this->dn, array('dn'));
1089 if ($ldap->fetch()){
1090 $mode= "modify";
1091 } else {
1092 $mode= "add";
1093 $ldap->cd($this->config->current['BASE']);
1094 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1095 }
1097 /* Set password to some junk stuff in case of templates */
1098 if ($this->is_template){
1099 $temp= passwordMethod::get_available_methods();
1100 foreach($temp as $id => $data){
1101 if(isset($data['name']) && $data['name'] == $this->pw_storage){
1102 $tmp = new $temp[$this->pw_storage]($this->config,$this->dn);
1103 $tmp->set_hash($this->pw_storage);
1104 $this->attrs['userPassword'] = $tmp->create_template_hash($this->attrs);
1105 break;
1106 }
1107 }
1108 }
1110 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
1111 $this->attributes, "Save via $mode");
1113 /* Finally write data with selected 'mode' */
1114 $this->cleanup();
1116 /* Update current locale settings, if we have edited ourselves */
1117 $ui = session::get('ui');
1118 if(isset($this->attrs['preferredLanguage']) && $this->dn == $ui->dn){
1119 $ui->language = $this->preferredLanguage;
1120 session::set('ui',$ui);
1121 session::set('Last_init_lang',"update");
1122 }
1124 $ldap->cd ($this->dn);
1125 $ldap->$mode ($this->attrs);
1126 if (!$ldap->success()){
1127 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
1128 return (1);
1129 }
1131 /* Remove ACL dependencies too */
1132 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1133 $tmp = new acl($this->config,$this->parent,$this->dn);
1134 $tmp->update_acl_membership($this->orig_dn,$this->dn);
1135 }
1137 if($mode == "modify"){
1138 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1139 }else{
1140 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1141 }
1143 /* Remove cert?
1144 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
1145 to work around myself. */
1146 if ($remove_userCertificate == true && !$this->is_new && $this->had_userCertificate){
1148 /* Reset array, assemble new, this should be reworked */
1149 $this->attrs= array();
1150 $this->attrs['userCertificate;binary']= array();
1152 /* Prepare connection */
1153 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
1154 die ("Could not connect to LDAP server");
1155 }
1156 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1157 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true") {
1158 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1159 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1160 }
1161 if($this->config->get_cfg_value("ldapTLS") == "true"){
1162 ldap_start_tls($ds);
1163 }
1164 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
1165 $this->config->current['PASSWORD']))) {
1166 die ("Could not bind to LDAP");
1167 }
1169 /* Modify using attrs */
1170 ldap_mod_del($ds,$this->dn,$this->attrs);
1171 ldap_close($ds);
1172 }
1174 /* If needed, let the password method do some cleanup */
1175 if ($this->pw_storage != $this->last_pw_storage){
1176 $tmp = new passwordMethod($this->config);
1177 $available = $tmp->get_available_methods();
1178 if (in_array_ics($this->last_pw_storage, $available['name'])){
1179 $test= new $available[$this->last_pw_storage]($this->config,$this->dn);
1180 $test->attrs= $this->attrs;
1181 $test->remove_from_parent();
1182 }
1183 }
1185 /* Maybe the current password method want's to do some changes... */
1186 if (is_object($this->pwObject)){
1187 $this->pwObject->save($this->dn);
1188 }
1190 /* Optionally execute a command after we're done */
1191 if ($mode == "add"){
1192 $this->handle_post_events("add", array("uid" => $this->uid));
1193 } elseif ($this->is_modified){
1194 $this->handle_post_events("modify", array("uid" => $this->uid));
1195 }
1197 return (0);
1198 }
1201 function create_initial_rdn($pattern)
1202 {
1203 // Only generate single RDNs
1204 if (preg_match('/\+/', $pattern)){
1205 msg_dialog::display(_("Error"), _("Cannot build RDN: no + allowed to build sub RDN!"), ERROR_DIALOG);
1206 return "";
1207 }
1209 // Extract attribute
1210 $attribute= preg_replace('/=.*$/', '', $pattern);
1211 if (!in_array_ics($attribute, $this->attributes)) {
1212 msg_dialog::display(_("Error"), _("Cannot build RDN: attribute is not defined!"), ERROR_DIALOG);
1213 return "";
1214 }
1216 // Sort attributes for length
1217 $attrl= array();
1218 foreach ($this->attributes as $attr) {
1219 $attrl[$attr]= strlen($attr);
1220 }
1221 arsort($attrl);
1223 // Walk thru sorted attributes and replace them in pattern
1224 foreach ($attrl as $attr => $dummy) {
1225 if (!is_array($this->$attr)){
1226 $pattern= preg_replace("/%$attr/", $this->$attr, $pattern);
1227 } else {
1228 // Array elements cannot be used for ID generation
1229 if (preg_match("/%$attr/", $pattern)) {
1230 msg_dialog::display(_("Error"), _("Cannot build RDN: invalid attribute parameters!"), ERROR_DIALOG);
1231 break;
1232 }
1233 }
1234 }
1236 // Internally assign value
1237 $this->$attribute= preg_replace('/^[^=]+=/', '', $pattern);
1239 return $pattern;
1240 }
1243 function update_new_dn()
1244 {
1245 // Alternative way to handle DN
1246 $pattern= $this->config->get_cfg_value("accountRDN");
1247 if ($pattern != "") {
1248 $rdn= $this->create_initial_rdn($pattern);
1249 $attribute= preg_replace('/=.*$/', '', $rdn);
1250 $value= preg_replace('/^[^=]+=$/', '', $rdn);
1252 /* Don't touch dn, if $attribute hasn't changed */
1253 if (isset($this->saved_attributes[$attribute]) && $this->saved_attributes[$attribute] == $this->$attribute &&
1254 $this->orig_base == $this->base ){
1255 $this->new_dn= $this->dn;
1256 } else {
1257 $this->new_dn= $this->create_unique_dn2($rdn, get_people_ou().$this->base);
1258 }
1260 // Original way to handle DN
1261 } else {
1263 $pt= "";
1264 if($this->config->get_cfg_value("personalTitleInDN") == "true"){
1265 if(!empty($this->personalTitle)){
1266 $pt = $this->personalTitle." ";
1267 }
1268 }
1270 $this->cn= $pt.$this->givenName." ".$this->sn;
1272 /* Permissions for that base? */
1273 if ($this->config->get_cfg_value("accountPrimaryAttribute") == "uid"){
1274 $this->new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
1275 } else {
1276 /* Don't touch dn, if cn hasn't changed */
1277 if (isset($this->saved_attributes['cn']) && $this->saved_attributes['cn'] == $this->cn &&
1278 $this->orig_base == $this->base ){
1279 $this->new_dn= $this->dn;
1280 } else {
1281 $this->new_dn= $this->create_unique_dn('cn', get_people_ou().$this->base);
1282 }
1283 }
1284 }
1285 }
1288 /* Check formular input */
1289 function check()
1290 {
1291 /* Call common method to give check the hook */
1292 $message= plugin::check();
1294 /* Configurable password methods should be configured initially.
1295 */
1296 if($this->last_pw_storage != $this->pw_storage){
1297 $temp= passwordMethod::get_available_methods();
1298 foreach($temp['name'] as $id => $name){
1299 if($name == $this->pw_storage){
1300 if($temp['is_configurable'][$id] && !$this->pwObject instanceof $temp[$name] ){
1301 $message[] = _("The selected password method requires initial configuration!");
1302 }
1303 break;
1304 }
1305 }
1306 }
1308 $this->update_new_dn();
1310 /* Set the new acl base */
1311 if($this->dn == "new") {
1312 $this->set_acl_base($this->base);
1313 }
1315 /* Check if we are allowed to create/move this user */
1316 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1317 $message[]= msgPool::permCreate();
1318 }elseif($this->orig_dn != "new" && $this->new_dn != $this->orig_dn && !$this->acl_is_moveable($this->base)){
1319 $message[]= msgPool::permMove();
1320 }
1322 /* UID already used? */
1323 $ldap= $this->config->get_ldap_link();
1324 $ldap->cd($this->config->current['BASE']);
1325 $ldap->search("(uid=$this->uid)", array("uid"));
1326 $ldap->fetch();
1327 if ($ldap->count() != 0 && $this->dn == 'new'){
1328 $message[]= msgPool::duplicated(_("Login"));
1329 }
1331 /* In template mode, the uid and givenName are autogenerated... */
1332 if ($this->sn == ""){
1333 $message[]= msgPool::required(_("Name"));
1334 }
1336 // Check if a wrong base was supplied
1337 if(!$this->baseSelector->checkLastBaseUpdate()){
1338 $message[]= msgPool::check_base();;
1339 }
1341 if (!$this->is_template){
1342 if ($this->givenName == ""){
1343 $message[]= msgPool::required(_("Given name"));
1344 }
1345 if ($this->uid == ""){
1346 $message[]= msgPool::required(_("Login"));
1347 }
1348 if ($this->config->get_cfg_value("accountPrimaryAttribute") != "uid"){
1349 $ldap->cat($this->new_dn);
1350 if ($ldap->count() != 0 && $this->dn != $this->new_dn && $this->dn == 'new'){
1351 $message[]= msgPool::duplicated(_("Name"));
1352 }
1353 }
1354 }
1356 /* Check for valid input */
1357 if ($this->is_modified && !tests::is_uid($this->uid)){
1359 if (strict_uid_mode()){
1360 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/");
1361 } else {
1362 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/i");
1363 }
1364 }
1365 if (!tests::is_url($this->labeledURI)){
1366 $message[]= msgPool::invalid(_("Homepage"), "", "", "http://www.your-domain.com/yourname");
1367 }
1369 /* Check phone numbers */
1370 if (!tests::is_phone_nr($this->telephoneNumber)){
1371 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1372 }
1373 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
1374 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1375 }
1376 if (!tests::is_phone_nr($this->mobile)){
1377 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1378 }
1379 if (!tests::is_phone_nr($this->pager)){
1380 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1381 }
1383 /* Check dates */
1384 if (!tests::is_date($this->dateOfBirth)){
1385 $message[]= msgPool::invalid(_("Date of birth"), $this->dateOfBirth,"" ,"23.02.2009");
1386 }
1388 /* Check for reserved characers */
1389 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName)){
1390 $message[]= msgPool::invalid(_("Given name"), $this->givenName, '/[^,+"?\'()=<>;\\\\]/');
1391 }
1392 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn)){
1393 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1394 }
1396 return $message;
1397 }
1400 /* Indicate whether a password change is needed or not */
1401 function password_change_needed()
1402 {
1403 if($this->multiple_support_active){
1404 return(FALSE);
1405 }else{
1407 if(in_array("pw_storage",$this->multi_boxes)){
1408 return(TRUE);
1409 }
1410 return($this->pw_storage != $this->last_pw_storage && !$this->is_template);
1411 }
1412 }
1415 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
1416 function load_picture()
1417 {
1418 $ldap = $this->config->get_ldap_link();
1419 $ldap->cd ($this->dn);
1420 $data = $ldap->get_attribute($this->dn,"jpegPhoto");
1422 if((!$data) || ($data == "*removed*")){
1424 /* In case we don't get an entry, load a default picture */
1425 $this->set_picture ();
1426 $this->jpegPhoto= "*removed*";
1427 }else{
1429 /* Set picture */
1430 $this->photoData= $data;
1431 session::set('binary',$this->photoData);
1432 session::set('binarytype',"image/jpeg");
1433 $this->jpegPhoto= "";
1434 }
1435 }
1438 /* Load a certificate from LDAP, this is going to be simplified later on */
1439 function load_cert()
1440 {
1441 $ds= ldap_connect($this->config->current['SERVER']);
1442 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1443 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true"){
1444 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1445 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1446 }
1447 if ($this->config->get_cfg_value("ldapTLS") == "true"){
1448 ldap_start_tls($ds);
1449 }
1451 $r= ldap_bind($ds);
1452 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1454 if ($sr) {
1455 $ei= @ldap_first_entry($ds, $sr);
1457 if ($ei) {
1458 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1459 $this->userCertificate= "";
1460 } else {
1461 $this->userCertificate= $info[0];
1462 }
1463 }
1464 } else {
1465 $this->userCertificate= "";
1466 }
1468 ldap_unbind($ds);
1469 }
1472 /* Load picture from file to object */
1473 function set_picture($filename ="")
1474 {
1475 if (!is_file($filename) || $filename =="" ){
1476 $filename= "./plugins/users/images/default.jpg";
1477 $this->jpegPhoto= "*removed*";
1478 }
1480 $fd = fopen ($filename, "rb");
1481 $this->photoData= fread ($fd, filesize ($filename));
1482 session::set('binary',$this->photoData);
1483 session::set('binarytype',"image/jpeg");
1484 $this->jpegPhoto= "";
1486 fclose ($fd);
1487 }
1490 /* Load certificate from file to object */
1491 function set_cert($cert, $filename)
1492 {
1493 if(!$this->acl_is_writeable("Certificate")) return;
1494 $fd = fopen ($filename, "rb");
1495 if (filesize($filename)>0) {
1496 $this->$cert= fread ($fd, filesize ($filename));
1497 fclose ($fd);
1498 $this->is_modified= TRUE;
1499 } else {
1500 msg_dialog::display(_("Error"), _("Cannot open certificate!"), ERROR_DIALOG);
1501 }
1502 }
1504 /* Adapt from given 'dn' */
1505 function adapt_from_template($dn, $skip= array())
1506 {
1507 plugin::adapt_from_template($dn, $skip);
1509 /* Get password method from template
1510 */
1511 $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
1512 if(is_object($tmp)){
1513 if($tmp->is_configurable()){
1514 $tmp->adapt_from_template($dn);
1515 $this->pwObject = &$tmp;
1516 }
1517 $this->pw_storage= $tmp->get_hash();
1518 }
1520 /* Get base */
1521 $this->base= preg_replace('/^[^,]+,'.preg_quote(get_people_ou(), '/').'/i', '', $dn);
1523 if($this->governmentmode){
1525 /* Walk through govattrs */
1526 foreach ($this->govattrs as $val){
1528 if (in_array($val, $skip)){
1529 continue;
1530 }
1532 if (isset($this->attrs["$val"][0])){
1534 /* If attribute is set, replace dynamic parts:
1535 %sn, %givenName and %uid. Fill these in our local variables. */
1536 $value= $this->attrs["$val"][0];
1538 foreach (array("sn", "givenName", "uid") as $repl){
1539 if (preg_match("/%$repl/i", $value)){
1540 $value= preg_replace ("/%$repl/i",
1541 $this->parent->$repl, $value);
1542 }
1543 }
1544 $this->$val= $value;
1545 }
1546 }
1547 }
1549 /* Get back uid/sn/givenName - only write if nothing's skipped */
1550 if ($this->parent !== NULL && count($skip) == 0){
1551 $this->uid= $this->parent->uid;
1552 $this->sn= $this->parent->sn;
1553 $this->givenName= $this->parent->givenName;
1554 }
1556 if ($this->dateOfBirth) {
1557 /* This entry is ISO 8601 conform */
1558 list($year, $month, $day)= explode("-", $this->dateOfBirth, 3);
1560 #TODO: use $lang to convert date
1561 $this->dateOfBirth= "$day.$month.$year";
1562 }
1563 }
1566 /* This avoids that users move themselves out of their rights.
1567 */
1568 function allowedBasesToMoveTo()
1569 {
1570 /* Get bases */
1571 $bases = $this->get_allowed_bases();
1572 return($bases);
1573 }
1576 function getCopyDialog()
1577 {
1578 $str = "";
1580 session::set('binary',$this->photoData);
1581 session::set('binarytype',"image/jpeg");
1583 /* Get random number for pictures */
1584 srand((double)microtime()*1000000);
1585 $rand = rand(0, 10000);
1587 $smarty = get_smarty();
1589 $smarty->assign("passwordTodo","clear");
1591 if(isset($_POST['passwordTodo'])){
1592 $smarty->assign("passwordTodo",$_POST['passwordTodo']);
1593 }
1595 $smarty->assign("sn", $this->sn);
1596 $smarty->assign("givenName",$this->givenName);
1597 $smarty->assign("uid", $this->uid);
1598 $smarty->assign("rand", $rand);
1599 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1602 $ret = array();
1603 $ret['string'] = $str;
1604 $ret['status'] = "";
1605 return($ret);
1606 }
1608 function saveCopyDialog()
1609 {
1610 /* Set_acl_base */
1611 $this->set_acl_base($this->base);
1613 if((isset($_FILES['picture_file']['tmp_name'])) && ($_FILES['picture_file']['size'] > 0)){
1614 $this->set_picture($_FILES['picture_file']['tmp_name']);
1615 }
1617 /* Remove picture? */
1618 if (isset($_POST['picture_remove'])){
1619 $this->jpegPhoto= "*removed*";
1620 $this->set_picture ("./plugins/users/images/default.jpg");
1621 $this->is_modified= TRUE;
1622 }
1624 $attrs = array("uid","givenName","sn");
1625 foreach($attrs as $attr){
1626 if(isset($_POST[$attr])){
1627 $this->$attr = $_POST[$attr];
1628 }
1629 }
1630 }
1633 function PrepareForCopyPaste($source)
1634 {
1635 plugin::PrepareForCopyPaste($source);
1637 /* Reset certificate information addepted from source user
1638 to avoid setting the same user certificate for the destination user. */
1639 $this->userPKCS12= "";
1640 $this->userSMIMECertificate= "";
1641 $this->userCertificate= "";
1642 $this->certificateSerialNumber= "";
1643 $this->old_certificateSerialNumber= "";
1644 $this->old_userPKCS12= "";
1645 $this->old_userSMIMECertificate= "";
1646 $this->old_userCertificate= "";
1647 }
1650 static function plInfo()
1651 {
1653 $govattrs= array(
1654 "gouvernmentOrganizationalUnit" => _("Unit"),
1655 "houseIdentifier" => _("House identifier"),
1656 "vocation" => _("Vocation"),
1657 "ivbbLastDeliveryCollective" => _("Last delivery"),
1658 "gouvernmentOrganizationalPersonLocality" => _("Person locality"),
1659 "gouvernmentOrganizationalUnitDescription" => _("Unit description"),
1660 "gouvernmentOrganizationalUnitSubjectArea" => _("Subject area"),
1661 "functionalTitle" => _("Functional title"),
1662 "certificateSerialNumber" => _("Certificate serial number"),
1663 "publicVisible" => _("Public visible"),
1664 "street" => _("Street"),
1665 "role" => _("Role"),
1666 "postalCode" => _("Postal code"));
1668 $ret = array(
1669 "plShortName" => _("Generic"),
1670 "plDescription" => _("Generic user settings"),
1671 "plSelfModify" => TRUE,
1672 "plDepends" => array(),
1673 "plPriority" => 1,
1674 "plSection" => array("personal" => _("My account")),
1675 "plCategory" => array("users" => array("description" => _("Users"),
1676 "objectClass" => "gosaAccount")),
1678 "plProvidedAcls" => array(
1680 "sn" => _("Surname"),
1681 "givenName" => _("Given name"),
1682 "uid" => _("User identification"),
1684 "gosaUserDefinedFilter" => _("Allow to define user filters"),
1686 "personalTitle" => _("Personal title"),
1687 "academicTitle" => _("Academic title"),
1689 "dateOfBirth" => _("Date of birth"),
1690 "gender" => _("Sex"),
1691 "preferredLanguage" => _("Preferred language"),
1692 "base" => _("Base"),
1694 "userPicture" => _("User picture"),
1696 "gosaLoginRestriction" => _("Login restrictions"),
1698 "o" => _("Organization"),
1699 "ou" => _("Department"),
1700 "departmentNumber" => _("Department number"),
1701 "manager" => _("Manager"),
1702 "employeeNumber" => _("Employee number"),
1703 "employeeType" => _("Employee type"),
1705 "roomNumber" => _("Room number"),
1706 "telephoneNumber" => _("Telefon number"),
1707 "pager" => _("Pager number"),
1708 "mobile" => _("Mobile number"),
1709 "facsimileTelephoneNumber" => _("Fax number"),
1711 "st" => _("State"),
1712 "l" => _("Location"),
1713 "postalAddress" => _("Postal address"),
1715 "homePostalAddress" => _("Home postal address"),
1716 "homePhone" => _("Home phone number"),
1717 "labeledURI" => _("Homepage"),
1718 "userPassword" => _("User password method"),
1719 "Certificate" => _("User certificates"))
1721 );
1723 /* Append government attributes if required */
1724 global $config;
1725 if($config->get_cfg_value("honourIvbbAttributes") == "true"){
1726 foreach($govattrs as $attr => $desc){
1727 $ret["plProvidedAcls"][$attr] = $desc;
1728 }
1729 }
1730 return($ret);
1731 }
1733 function get_multi_edit_values()
1734 {
1735 $ret = plugin::get_multi_edit_values();
1736 if(in_array("pw_storage",$this->multi_boxes)){
1737 $ret['pw_storage'] = $this->pw_storage;
1738 }
1739 if(in_array("edit_picture",$this->multi_boxes)){
1740 $ret['jpegPhoto'] = $this->jpegPhoto;
1741 $ret['photoData'] = $this->photoData;
1742 $ret['old_jpegPhoto'] = $this->old_jpegPhoto;
1743 $ret['old_photoData'] = $this->old_photoData;
1744 }
1745 if(isset($ret['dateOfBirth'])){
1746 unset($ret['dateOfBirth']);
1747 }
1748 if(isset($ret['cn'])){
1749 unset($ret['cn']);
1750 }
1751 $ret['is_modified'] = $this->is_modified;
1752 if(in_array("base",$this->multi_boxes)){
1753 $ret['orig_base']="Changed_by_Multi_Plug";
1754 $ret['base']=$this->base;
1755 }
1757 $ret['gosaLoginRestriction'] = $this->gosaLoginRestriction;
1758 $ret['gosaLoginRestriction_some'] = $this->gosaLoginRestriction_some;
1760 return($ret);
1761 }
1764 function multiple_save_object()
1765 {
1767 if(!isset($_POST['user_mulitple_edit'])) return;
1769 plugin::multiple_save_object();
1771 /* Get pw_storage mode */
1772 if (isset($_POST['pw_storage'])){
1773 foreach(array("pw_storage") as $val){
1774 if(isset($_POST[$val])){
1775 $data= validate(get_post($val));
1776 if ($data != $this->$val){
1777 $this->is_modified= TRUE;
1778 }
1779 $this->$val= $data;
1780 }
1781 }
1782 }
1784 /* Refresh base */
1785 if ($this->acl_is_moveable($this->base)){
1786 if (!$this->baseSelector->update()) {
1787 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
1788 }
1789 if ($this->base != $this->baseSelector->getBase()) {
1790 $this->base= $this->baseSelector->getBase();
1791 }
1792 }
1794 if(isset($_POST['user_mulitple_edit'])){
1795 foreach(array("base","pw_storage","edit_picture") as $val){
1796 if(isset($_POST["use_".$val])){
1797 $this->multi_boxes[] = $val;
1798 }
1799 }
1800 }
1802 /* Sync lists */
1803 $this->gosaLoginRestrictionWidget->save_object();
1804 if ($this->gosaLoginRestrictionWidget->isModified()) {
1805 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
1806 }
1807 }
1810 function multiple_check()
1811 {
1812 /* Call check() to set new_dn correctly ... */
1813 $message = plugin::multiple_check();
1815 /* Set the new acl base */
1816 if($this->dn == "new") {
1817 $this->set_acl_base($this->base);
1818 }
1819 if (!tests::is_url($this->labeledURI) && in_array("labeledURI",$this->multi_boxes)){
1820 $message[]= msgPool::invalid(_("Homepage"));
1821 }
1822 if (!tests::is_phone_nr($this->telephoneNumber) && in_array("telephoneNumber",$this->multi_boxes)){
1823 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1824 }
1825 if (!tests::is_phone_nr($this->facsimileTelephoneNumber) && in_array("facsimileTelephoneNumber",$this->multi_boxes)){
1826 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1827 }
1828 if (!tests::is_phone_nr($this->mobile) && in_array("mobile",$this->multi_boxes)){
1829 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1830 }
1831 if (!tests::is_phone_nr($this->pager) && in_array("pager",$this->multi_boxes)){
1832 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1833 }
1834 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName) && in_array("givenName",$this->multi_boxes)){
1835 $message[]= msgPool::invalid(_("Given name"), $this->giveName, '/[^,+"?\'()=<>;\\\\]/');
1836 }
1837 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn) && in_array("sn",$this->multi_boxes)){
1838 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1839 }
1840 return($message);
1841 }
1845 function multiple_execute()
1846 {
1847 return($this->execute());
1848 }
1851 /*! \brief Prepares the plugin to be used for multiple edit
1852 * Update plugin attributes with given array of attribtues.
1853 * \param array Array with attributes that must be updated.
1854 */
1855 function init_multiple_support($attrs,$all)
1856 {
1857 plugin::init_multiple_support($attrs,$all);
1859 // Get login restrictions
1860 if(isset($attrs['gosaLoginRestriction'])){
1861 $this->gosaLoginRestriction =array();
1862 for($i =0;$i < $attrs['gosaLoginRestriction']['count']; $i++){
1863 $this->gosaLoginRestriction[] = $attrs['gosaLoginRestriction'][$i];
1864 }
1865 }
1867 // Detect the managers name
1868 $this->manager_name = "";
1869 $ldap = $this->config->get_ldap_link();
1870 if(!empty($this->manager)){
1871 $ldap->cat($this->manager, array('cn'));
1872 if($ldap->count()){
1873 $attrs = $ldap->fetch();
1874 $this->manager_name = $attrs['cn'][0];
1875 }else{
1876 $this->manager_name = "("._("Unknown")."!): ".$this->manager;
1877 }
1878 }
1880 // Detect login restriction not used in all user objects.
1881 $this->gosaLoginRestriction_some = array();
1882 if(isset($all['gosaLoginRestriction'])){
1883 for($i=0;$i<$all['gosaLoginRestriction']['count'];$i++){
1884 $this->gosaLoginRestriction_some[] = $all['gosaLoginRestriction'][$i];
1885 }
1886 }
1889 // Reinit the login restriction list.
1890 $data = $this->convertLoginRestriction();
1891 if(count($data)){
1892 $this->gosaLoginRestrictionWidget->setListData($data['data'], $data['displayData']);
1893 }
1894 }
1897 function set_multi_edit_values($attrs)
1898 {
1899 $lR = array();
1901 // Update loginRestrictions, keep my settings while ip is optional
1902 foreach($attrs['gosaLoginRestriction_some'] as $ip){
1903 if(in_array($ip, $this->gosaLoginRestriction) && in_array($ip, $attrs['gosaLoginRestriction'])){
1904 $lR[] = $ip;
1905 }
1906 }
1908 // Add enforced loginRestrictions
1909 foreach($attrs['gosaLoginRestriction'] as $ip){
1910 $lR[] = $ip;
1911 }
1913 $lR = array_values(array_unique($lR));
1914 $this->is_modified |= array_differs($this->gosaLoginRestriction, $lR);
1915 plugin::set_multi_edit_values($attrs);
1916 $this->gosaLoginRestriction = $lR;
1917 }
1920 function convertLoginRestriction()
1921 {
1922 $all = array_unique(array_merge($this->gosaLoginRestriction,$this->gosaLoginRestriction_some));
1923 $data = array();
1924 foreach($all as $ip){
1925 $data['data'][] = $ip;
1926 if(!in_array($ip, $this->gosaLoginRestriction)){
1927 $data['displayData'][] = array('mode' => LIST_MARKED , 'data' => array($ip.' ('._("Entries differ").')'));
1928 }else{
1929 $data['displayData'][] = array('mode' => 0 , 'data' => array($ip));
1930 }
1931 }
1932 return($data);
1933 }
1934 }
1936 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1937 ?>