0de16e5188d466c0eb5b922fad42311cf3731725
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 = "";
113 var $passwordClass = NULL;
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", "street", "postalCode",
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', (!is_object($this->parent) && !session::is_set('edit'))));
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 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 /* Want password method editing? */
368 if ($this->acl_is_writeable("userPassword")){
369 if (isset($_POST['edit_pw_method'])){
370 if (!is_object($this->pwObject) || $this->pw_storage != $this->pwObject->get_hash_name()){
371 $temp= passwordMethod::get_available_methods();
372 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
373 }
374 $this->pwObject->display = TRUE;
375 $this->dialog= TRUE;
376 return ($this->pwObject->configure());
377 }
378 }
380 /* Want picture edit dialog? */
381 if($this->acl_is_writeable("userPicture")) {
382 if (isset($_POST['edit_picture'])){
383 /* Save values for later recovery, in case some presses
384 the cancel button. */
385 $this->old_jpegPhoto= $this->jpegPhoto;
386 $this->old_photoData= $this->photoData;
387 $this->picture_dialog= TRUE;
388 $this->dialog= TRUE;
389 }
390 }
392 /* Remove picture? */
393 if($this->acl_is_writeable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))) ){
394 if (isset($_POST['picture_remove'])){
395 $this->set_picture ();
396 $this->jpegPhoto= "*removed*";
397 $this->is_modified= TRUE;
398 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
399 }
400 }
402 /* Save picture */
403 if (isset($_POST['picture_edit_finish'])){
405 /* Check for clean upload */
406 if ($_FILES['picture_file']['name'] != ""){
407 if (!is_uploaded_file($_FILES['picture_file']['tmp_name'])) {
408 msg_dialog::display(_("Error"), _("Cannot upload file!"), ERROR_DIALOG);
409 }else{
410 /* Activate new picture */
411 $this->set_picture($_FILES['picture_file']['tmp_name']);
412 }
413 }
414 $this->picture_dialog= FALSE;
415 $this->dialog= FALSE;
416 $this->is_modified= TRUE;
417 }
420 /* Cancel picture */
421 if (isset($_POST['picture_edit_cancel'])){
423 /* Restore values */
424 $this->jpegPhoto= $this->old_jpegPhoto;
425 $this->photoData= $this->old_photoData;
427 /* Update picture */
428 session::set('binary',$this->photoData);
429 session::set('binarytype',"image/jpeg");
430 $this->picture_dialog= FALSE;
431 $this->dialog= FALSE;
432 }
434 /* Want certificate= */
435 if ((isset($_POST['edit_cert'])) && $this->acl_is_readable("Certificate")){
437 /* Save original values for later reconstruction */
438 foreach (array("certificateSerialNumber", "userCertificate",
439 "userSMIMECertificate", "userPKCS12") as $val){
441 $oval= "old_$val";
442 $this->$oval= $this->$val;
443 }
445 $this->cert_dialog= TRUE;
446 $this->dialog= TRUE;
447 }
450 /* Cancel certificate dialog */
451 if (isset($_POST['cert_edit_cancel'])){
453 /* Restore original values in case of 'cancel' */
454 foreach (array("certificateSerialNumber", "userCertificate",
455 "userSMIMECertificate", "userPKCS12") as $val){
457 $oval= "old_$val";
458 $this->$val= $this->$oval;
459 }
460 $this->cert_dialog= FALSE;
461 $this->dialog= FALSE;
462 }
465 /* Remove certificate? */
466 if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){
467 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12") as $val){
468 if (isset($_POST["remove_$val"])){
470 /* Reset specified cert*/
471 $this->$val= "";
472 $this->is_modified= TRUE;
473 }
474 }
475 }
477 /* Upload new cert and close dialog? */
478 if($this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))){
480 $fail =false;
482 if (isset($_POST['cert_edit_finish'])){
484 /* for all certificates do */
485 foreach (array ("userCertificate", "userSMIMECertificate", "userPKCS12")
486 as $val){
488 /* Check for clean upload */
489 if (array_key_exists($val."_file", $_FILES) &&
490 array_key_exists('name', $_FILES[$val."_file"]) &&
491 $_FILES[$val."_file"]['name'] != "" &&
492 is_uploaded_file($_FILES[$val."_file"]['tmp_name'])) {
493 $this->set_cert("$val", $_FILES[$val."_file"]['tmp_name']);
494 }
495 }
497 /* Save serial number */
498 if (isset($_POST["certificateSerialNumber"]) &&
499 $_POST["certificateSerialNumber"] != ""){
501 if (!tests::is_id($_POST["certificateSerialNumber"])){
502 $fail = true;
503 msg_dialog::display(_("Error"), msgPool::invalid(_("Serial number"),$_POST["certificateSerialNumber"],"/[0-9]/"),ERROR_DIALOG);
505 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
506 if ($this->$cert != ""){
507 $smarty->assign("$cert"."_state", "true");
508 } else {
509 $smarty->assign("$cert"."_state", "");
510 }
511 }
512 }
514 $this->certificateSerialNumber= $_POST["certificateSerialNumber"];
515 $this->is_modified= TRUE;
516 }
517 if(!$fail){
518 $this->cert_dialog= FALSE;
519 $this->dialog= FALSE;
520 }
521 }
522 }
523 /* Display picture dialog */
524 if ($this->picture_dialog){
525 return($smarty->fetch (get_template_path('generic_picture.tpl', TRUE, dirname(__FILE__))));
526 }
528 /* Display cert dialog */
529 if ($this->cert_dialog){
530 $smarty->assign("CertificateACL",$this->getacl("Certificate",(!is_object($this->parent) && !session::is_set('edit'))));
531 $smarty->assign("Certificate_readable",$this->acl_is_readable("Certificate"));
532 $smarty->assign("certificateSerialNumber",$this->certificateSerialNumber);
534 foreach(array("userCertificate", "userSMIMECertificate", "userPKCS12") as $cert){
535 if ($this->$cert != ""){
536 /* import certificate */
537 $certificate = new certificate;
538 $certificate->import($this->$cert);
540 /* Read out data*/
541 $timeto = $certificate->getvalidto_date();
542 $timefrom = $certificate->getvalidfrom_date();
545 /* Additional info if start end time is '0' */
546 $add_str_info = "";
547 if($timeto == 0 && $timefrom == 0){
548 $add_str_info = "<br><i>"._("(Some types of certificates are currently not supported and may be displayed as 'invalid'.)")."</i>";
549 }
551 $str = "<table summary=\"\" border=0>
552 <tr>
553 <td style='vertical-align:top'>CN</td>
554 <td>".preg_replace("/ /", " ", $certificate->getname())."</td>
555 </tr>
556 </table><br>".
558 sprintf(_("Certificate is valid from %s to %s and is currently %s."),
559 "<b>".date('d M Y',$timefrom)."</b>",
560 "<b>".date('d M Y',$timeto)."</b>",
561 $certificate->isvalid()?"<b><font style='color:green'>"._("valid")."</font></b>":
562 "<b><font style='color:red'>"._("invalid")."</font></b>").$add_str_info;
564 $smarty->assign($cert."info",$str);
565 $smarty->assign($cert."_state","true");
566 } else {
567 $smarty->assign($cert."info", "<i>"._("No certificate installed")."</i>");
568 $smarty->assign($cert."_state","");
569 }
570 }
572 if($this->governmentmode){
573 $smarty->assign("honourIvbbAttributes", "true");
574 }else{
575 $smarty->assign("honourIvbbAttributes", "false");
576 }
577 $smarty->assign("governmentmode", $this->governmentmode);
578 return($smarty->fetch (get_template_path('generic_certs.tpl', TRUE, dirname(__FILE__))));
579 }
581 /* Prepare password hashes */
582 if ($this->pw_storage == ""){
583 $this->pw_storage= $this->config->get_cfg_value("passwordDefaultHash");
584 }
586 $temp= passwordMethod::get_available_methods();
587 $is_configurable= FALSE;
588 $hashes = $temp['name'];
589 if(isset($temp[$this->pw_storage])){
590 $test= new $temp[$this->pw_storage]($this->config);
591 $is_configurable= $test->is_configurable();
592 }else{
593 new msg_dialog(_("Password method"),_("The selected password method is no longer available."),WARNING_DIALOG);
594 }
597 /* Create password methods array */
598 $pwd_methods = array();
599 foreach($hashes as $id => $name){
600 if(!empty($temp['desc'][$id])){
601 $pwd_methods[$name] = $name." (".$temp['desc'][$id].")";
602 }else{
603 $pwd_methods[$name] = $name;
604 }
605 }
607 /* Load attributes and acl's */
608 $ui =get_userinfo();
609 foreach($this->attributes as $val){
610 $smarty->assign("$val", $this->$val);
611 if(in_array_strict($val,$this->multi_boxes)){
612 $smarty->assign("use_".$val,TRUE);
613 }else{
614 $smarty->assign("use_".$val,FALSE);
615 }
616 }
617 foreach(array("base","pw_storage","edit_picture") as $val){
618 if(in_array_strict($val,$this->multi_boxes)){
619 $smarty->assign("use_".$val,TRUE);
620 }else{
621 $smarty->assign("use_".$val,FALSE);
622 }
623 }
625 /* Set acls */
626 $tmp = $this->plinfo();
627 foreach($tmp['plProvidedAcls'] as $val => $translation){
628 $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
629 }
631 // Special ACL for gosaLoginRestrictions -
632 // In case of multiple edit, we need a readonly ACL for the list.
633 $smarty->assign('gosaLoginRestriction_ONLY_R_ACL',
634 preg_replace("/[^r]/i","", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit')))));
636 $smarty->assign("pwmode", $pwd_methods);
637 $smarty->assign("pwmode_select", $this->pw_storage);
638 $smarty->assign("pw_configurable", $is_configurable);
639 $smarty->assign("passwordStorageACL", $this->getacl("userPassword",(!is_object($this->parent) && !session::is_set('edit'))));
641 if(!session::is_set('edit')){
642 $smarty->assign("CertificatesACL","");
643 }else{
644 $smarty->assign("CertificatesACL", $this->getacl("Certificate"));
645 }
647 $smarty->assign("userPictureACL", $this->getacl("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
648 $smarty->assign("userPicture_is_readable", $this->acl_is_readable("userPicture",(!is_object($this->parent) && !session::is_set('edit'))));
650 /* Create base acls */
651 $smarty->assign("base", $this->baseSelector->render());
653 /* Save government mode attributes */
654 if($this->governmentmode){
655 $smarty->assign("governmentmode", "true");
656 $ivbbmodes= array("nein", "ivbv", "testa", "ivbv,testa", "internet",
657 "internet,ivbv", "internet,testa", "internet,ivbv,testa");
658 $smarty->assign("ivbbmodes", $ivbbmodes);
659 foreach ($this->govattrs as $val){
660 $smarty->assign("$val", $this->$val);
661 $smarty->assign("$val"."ACL", $this->getacl($val,(!is_object($this->parent) && !session::is_set('edit'))));
662 }
663 } else {
664 $smarty->assign("governmentmode", "false");
665 }
667 /* Special mode for uid */
668 $uidACL= $this->getacl("uid",(!is_object($this->parent) && !session::is_set('edit')));
669 if (isset ($this->dn)){
670 if ($this->dn != "new"){
671 $uidACL= preg_replace("/w/","",$uidACL);
672 }
673 } else {
674 $uidACL= preg_replace("/w/","",$uidACL);
675 }
677 $smarty->assign("uidACL", $uidACL);
678 $smarty->assign("is_template", $this->is_template);
679 $smarty->assign("use_dob", $this->use_dob);
681 if (isset($this->parent)){
682 if (isset($this->parent->by_object['phoneAccount']) &&
683 $this->parent->by_object['phoneAccount']->is_account){
684 $smarty->assign("has_phoneaccount", "true");
685 } else {
686 $smarty->assign("has_phoneaccount", "false");
687 }
688 } else {
689 $smarty->assign("has_phoneaccount", "false");
690 }
691 $smarty->assign("multiple_support" , $this->multiple_support_active);
692 $smarty->assign("manager_name",$this->manager_name);
693 return($smarty->fetch (get_template_path('generic.tpl', TRUE, dirname(__FILE__))));
694 }
697 /* remove object from parent */
698 function remove_from_parent()
699 {
700 /* Only remove valid accounts */
701 if(!$this->initially_was_account) return;
703 /* Remove password extension */
704 $temp= passwordMethod::get_available_methods();
706 /* Remove password method from user account */
707 if(isset($temp[$this->pw_storage]) && class_available($temp[$this->pw_storage])){
708 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
709 $this->pwObject->remove_from_parent();
710 }
712 /* Remove user */
713 $ldap= $this->config->get_ldap_link();
714 $ldap->rmdir ($this->dn);
715 if (!$ldap->success()){
716 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
717 }
719 new log("remove","users/".get_class($this),$this->dn,$this->attributes,$ldap->get_error());
721 /* Delete references to groups */
722 $ldap->cd ($this->config->current['BASE']);
723 $ldap->search ("(&(objectClass=posixGroup)(memberUid=".$this->uid."))", array("uid"));
724 while ($ldap->fetch()){
725 $g= new group($this->config, $ldap->getDN());
726 $g->removeUser($this->uid);
727 $g->save ();
728 }
730 /* Delete references to object groups */
731 $ldap->cd ($this->config->current['BASE']);
732 $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".LDAP::prepare4filter($this->dn)."))", array("cn"));
733 while ($ldap->fetch()){
734 $og= new ogroup($this->config, $ldap->getDN());
735 unset($og->member[$this->dn]);
736 $og->save ();
737 }
739 // Update 'manager' attributes from gosaDepartment and inetOrgPerson
740 $filter = "(&(objectClass=inetOrgPerson)(manager=".LDAP::prepare4filter($this->dn)."))";
741 $ocs = $ldap->get_objectclasses();
742 if(isset($ocs['gosaDepartment']['MAY']) && in_array_strict('manager', $ocs['gosaDepartment']['MAY'])){
743 $filter = "(|".$filter."(&(objectClass=gosaDepartment)(manager=".LDAP::prepare4filter($this->dn).")))";
744 }
745 $leaf_deps= get_list($filter,array("all"),$this->config->current['BASE'],
746 array("manager","dn","objectClass"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
747 foreach($leaf_deps as $entry){
748 $update = array('manager' => array());
749 $ldap->cd($entry['dn']);
750 $ldap->modify($update);
751 if(!$ldap->success()){
752 trigger_error(sprintf("Failed to update manager for '%s', error was '%s'", $entry['dn'], $ldap->get_error()));
753 }
754 }
756 /* Delete references to roles */
757 $ldap->cd ($this->config->current['BASE']);
758 $ldap->search ("(&(objectClass=organizationalRole)(roleOccupant=".LDAP::prepare4filter($this->dn)."))", array("cn"));
759 while ($ldap->fetch()){
760 $role= new roleGeneric($this->config, $ldap->getDN());
761 $key = array_search($this->dn,$role->roleOccupant);
762 if($key !== FALSE){
763 unset($role->roleOccupant[$key]);
764 $role->roleOccupant= array_values($role->roleOccupant);
765 $role->save ();
766 }
767 }
769 /* If needed, let the password method do some cleanup */
770 $tmp = new passwordMethod($this->config);
771 $available = $tmp->get_available_methods();
772 if (in_array_ics($this->pw_storage, $available['name'])){
773 $test= new $available[$this->pw_storage]($this->config);
774 $test->attrs= $this->attrs;
775 $test->dn= $this->dn;
776 $test->remove_from_parent();
777 }
779 /* Remove ACL dependencies too */
780 acl::remove_acl_for($this->dn);
782 /* Optionally execute a command after we're done */
783 $this->handle_post_events("remove",array("uid" => $this->uid));
784 }
787 /* Save data to object */
788 function save_object()
789 {
790 if(isset($_POST['generic']) || isset($_POST['multiple_user_posted'])){
792 /* Make a backup of the current selected base */
793 $base_tmp = $this->base;
795 /* Parents save function */
796 plugin::save_object ();
798 /* Refresh base */
799 if ($this->acl_is_moveable($this->base) ||
800 ($this->dn == "new" && $this->acl_is_createable($this->base))){
801 if (!$this->baseSelector->update()) {
802 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
803 }
804 if ($this->base != $this->baseSelector->getBase()) {
805 $this->base= $this->baseSelector->getBase();
806 $this->is_modified= TRUE;
807 }
808 }
810 /* Sync lists */
811 $this->gosaLoginRestrictionWidget->save_object();
812 if ($this->gosaLoginRestrictionWidget->isModified()) {
813 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
814 }
816 /* Save government mode attributes */
817 if ($this->governmentmode){
818 foreach ($this->govattrs as $val){
819 if ($this->acl_is_writeable($val,(!is_object($this->parent) && !session::is_set('edit'))) && isset($_POST["$val"])){
820 $data= stripcslashes($_POST["$val"]);
821 if ($data != $this->$val){
822 $this->is_modified= TRUE;
823 }
824 $this->$val= $data;
825 }
826 }
827 }
829 /* In template mode, the uid is autogenerated... */
830 if ($this->is_template){
831 $this->uid= strtolower($this->sn);
832 $this->givenName= $this->sn;
833 }
835 /* Get pw_storage mode */
836 if (isset($_POST['pw_storage'])){
837 foreach(array("pw_storage") as $val){
838 if(isset($_POST[$val])){
839 $data= validate($_POST[$val]);
840 if ($data != $this->$val){
841 $this->is_modified= TRUE;
842 }
843 $this->$val= $data;
844 }
845 }
846 }
848 if($this->pw_storage != $this->last_pw_storage && isset($_POST['pw_storage'])){
849 if ($this->acl_is_writeable("userPassword")){
850 $temp= passwordMethod::get_available_methods();
851 if (!is_object($this->pwObject) || !($this->pwObject instanceOf $temp[$this->pw_storage])){
852 foreach($temp as $id => $data){
853 if(isset($data['name']) && $data['name'] == $this->pw_storage && $data['is_configurable']){
854 $this->pwObject= new $temp[$this->pw_storage]($this->config,$this->dn);
855 break;
856 }
857 }
858 }
859 }
860 }
862 /* Save current cn
863 */
864 $this->cn = $this->givenName." ".$this->sn;
865 }
866 }
868 function rebind($ldap, $referral)
869 {
870 $credentials= LDAP::get_credentials($referral, $this->config->current['REFERRAL']);
871 if (ldap_bind($ldap, $credentials['ADMIN'], $credentials['PASSWORD'])) {
872 $this->error = "Success";
873 $this->hascon=true;
874 $this->reconnect= true;
875 return (0);
876 } else {
877 $this->error = "Could not bind to " . $credentials['ADMIN'];
878 return NULL;
879 }
880 }
883 /* Save data to LDAP, depending on is_account we save or delete */
884 function save()
885 {
886 global $lang;
888 /* Only force save of changes ....
889 If this attributes aren't changed, avoid saving.
890 */
892 if($this->gender=="0") $this->gender ="";
893 if($this->preferredLanguage=="0") $this->preferredLanguage ="";
895 /* First use parents methods to do some basic fillup in $this->attrs */
896 plugin::save ();
898 if ($this->dateOfBirth != ""){
899 if(!is_array($this->attrs['dateOfBirth'])) {
900 #TODO: use $lang to convert date
901 list($day, $month, $year)= explode(".", $this->dateOfBirth);
902 $this->attrs['dateOfBirth'] = sprintf("%04d-%02d-%02d", $year, $month, $day);
903 }
904 }
906 /* Remove additional objectClasses */
907 $tmp= array();
908 foreach ($this->attrs['objectClass'] as $key => $set){
909 $found= false;
910 foreach (array("ivbbentry", "gosaUserTemplate") as $val){
911 if (preg_match ("/^$set$/i", $val)){
912 $found= true;
913 break;
914 }
915 }
916 if (!$found){
917 $tmp[]= $set;
918 }
919 }
921 /* Replace the objectClass array. This is done because of the
922 separation into government and normal mode. */
923 $this->attrs['objectClass']= $tmp;
925 /* Add objectClasss for template mode? */
926 if ($this->is_template){
927 $this->attrs['objectClass'][]= "gosaUserTemplate";
928 }
930 /* Hard coded government mode? */
931 if ($this->governmentmode){
932 $this->attrs['objectClass'][]= "ivbbentry";
934 /* Copy standard attributes */
935 foreach ($this->govattrs as $val){
936 if ($this->$val != ""){
937 $this->attrs["$val"]= $this->$val;
938 } elseif (!$this->is_new) {
939 $this->attrs["$val"]= array();
940 }
941 }
943 /* Remove attribute if set to "nein" */
944 if ($this->publicVisible == "nein"){
945 $this->attrs['publicVisible']= array();
946 if($this->is_new){
947 unset($this->attrs['publicVisible']);
948 }else{
949 $this->attrs['publicVisible']=array();
950 }
952 }
954 }
956 /* Special handling for attribute userCertificate needed */
957 if ($this->userCertificate != ""){
958 $this->attrs["userCertificate;binary"]= $this->userCertificate;
959 $remove_userCertificate= false;
960 } else {
961 $remove_userCertificate= true;
962 }
964 /* Special handling for dateOfBirth value */
965 if ($this->dateOfBirth == ""){
966 if ($this->is_new) {
967 unset($this->attrs["dateOfBirth"]);
968 } else {
969 $this->attrs["dateOfBirth"]= array();
970 }
971 }
972 if (!$this->gender){
973 if ($this->is_new) {
974 unset($this->attrs["gender"]);
975 } else {
976 $this->attrs["gender"]= array();
977 }
978 }
979 if (!$this->preferredLanguage){
980 if ($this->is_new) {
981 unset($this->attrs["preferredLanguage"]);
982 } else {
983 $this->attrs["preferredLanguage"]= array();
984 }
985 }
987 /* Special handling for attribute jpegPhote needed, scale image via
988 image magick to 147x200 pixels and inject resulting data. */
989 if ($this->jpegPhoto == "*removed*"){
991 /* Reset attribute to avoid writing *removed* as value */
992 $this->attrs["jpegPhoto"] = array();
994 } else {
996 if(class_exists('Imagick')){
998 $im = new Imagick();
999 $im->readImageBlob($this->photoData);
1000 $im->setImageOpacity(1.0);
1001 $im->resizeImage(147,200,Imagick::FILTER_UNDEFINED,0.5,TRUE);
1002 $im->setCompressionQuality(90);
1003 $im->setImageFormat('jpeg');
1004 $this->attrs["jpegPhoto"] = $im->getImageBlob();
1006 }elseif(exec('convert')){
1008 /* Get temporary file name for conversation */
1009 $fname = tempnam (TEMP_DIR, "GOsa");
1011 /* Open file and write out photoData */
1012 $fp = fopen ($fname, "w");
1013 fwrite ($fp, $this->photoData);
1014 fclose ($fp);
1016 /* Build conversation query. Filename is generated automatically, so
1017 we do not need any special security checks. Exec command and save
1018 output. For PHP safe mode, you'll need a configuration which respects
1019 image magick as executable... */
1020 $query= "convert -size 147x200 $fname -resize 147x200 +profile \"*\" -";
1021 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
1022 $query, "Execute");
1024 /* Read data written by convert */
1025 $output= "";
1026 $sh= popen($query, 'r');
1027 while (!feof($sh)){
1028 $output.= fread($sh, 4096);
1029 }
1030 pclose($sh);
1032 unlink($fname);
1034 /* Save attribute */
1035 $this->attrs["jpegPhoto"] = $output;
1036 }else{
1037 msg_dialog::display(_("Error"),
1038 _("Cannot save user picture, GOsa requires the package 'imagemagick' or 'php5-imagick' to be installed!"),
1039 ERROR_DIALOG);
1040 }
1041 }
1043 /* This only gets called when user is renaming himself */
1044 $ldap= $this->config->get_ldap_link();
1045 if ($this->dn != $this->new_dn){
1047 /* Write entry on new 'dn' */
1048 $this->update_acls($this->dn,$this->new_dn);
1049 $this->move($this->dn, $this->new_dn);
1051 /* Happen to use the new one */
1052 change_ui_dn($this->dn, $this->new_dn);
1053 $this->dn= $this->new_dn;
1054 }
1057 /* Save data. Using 'modify' implies that the entry is already present, use 'add' for
1058 new entries. So do a check first... */
1059 $ldap->cat ($this->dn, array('dn'));
1060 if ($ldap->fetch()){
1061 $mode= "modify";
1062 } else {
1063 $mode= "add";
1064 $ldap->cd($this->config->current['BASE']);
1065 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
1066 }
1068 /* Set password to some junk stuff in case of templates */
1069 if ($this->is_template){
1070 $temp= passwordMethod::get_available_methods();
1071 foreach($temp as $id => $data){
1072 if(isset($data['name']) && $data['name'] == $this->pw_storage){
1073 $tmp = new $temp[$this->pw_storage]($this->config,$this->dn);
1074 $tmp->set_hash($this->pw_storage);
1075 $this->attrs['userPassword'] = $tmp->create_template_hash($this->attrs);
1076 break;
1077 }
1078 }
1079 }
1081 @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
1082 $this->attributes, "Save via $mode");
1084 /* Finally write data with selected 'mode' */
1085 $this->cleanup();
1087 /* Update current locale settings, if we have edited ourselves */
1088 $ui = session::get('ui');
1089 if(isset($this->attrs['preferredLanguage']) && $this->dn == $ui->dn){
1090 $ui->language = $this->preferredLanguage;
1091 session::set('ui',$ui);
1092 session::set('Last_init_lang',"update");
1093 }
1095 $ldap->cd ($this->dn);
1096 $ldap->$mode ($this->attrs);
1097 if (!$ldap->success()){
1098 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_DEL, get_class()));
1099 return (1);
1100 }
1102 /* Remove ACL dependencies too */
1103 if($this->dn != $this->orig_dn && $this->orig_dn != "new"){
1104 $tmp = new acl($this->config,$this->parent,$this->dn);
1105 $tmp->update_acl_membership($this->orig_dn,$this->dn);
1106 }
1108 if($mode == "modify"){
1109 new log("modify","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1110 }else{
1111 new log("create","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1112 }
1114 /* Remove cert?
1115 For some reason, the 'ldap' class doesn't want to remove binary entries, so I need
1116 to work around myself. */
1117 if ($remove_userCertificate == true && !$this->is_new && $this->had_userCertificate){
1119 /* Reset array, assemble new, this should be reworked */
1120 $this->attrs= array();
1121 $this->attrs['userCertificate;binary']= array();
1123 /* Prepare connection */
1124 if (!($ds = ldap_connect($this->config->current['SERVER']))) {
1125 die ("Could not connect to LDAP server");
1126 }
1127 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1128 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true") {
1129 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1130 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1131 }
1132 if($this->config->get_cfg_value("ldapTLS") == "true"){
1133 ldap_start_tls($ds);
1134 }
1135 if (!($res = @ldap_bind($ds, $this->config->current['ADMIN'],
1136 $this->config->current['PASSWORD']))) {
1137 die ("Could not bind to LDAP");
1138 }
1140 /* Modify using attrs */
1141 ldap_mod_del($ds,$this->dn,$this->attrs);
1142 ldap_close($ds);
1143 }
1145 /* If needed, let the password method do some cleanup */
1146 if ($this->pw_storage != $this->last_pw_storage){
1147 $tmp = new passwordMethod($this->config);
1148 $available = $tmp->get_available_methods();
1149 if (in_array_ics($this->last_pw_storage, $available['name'])){
1150 $test= new $available[$this->last_pw_storage]($this->config,$this->dn);
1151 $test->attrs= $this->attrs;
1152 $test->remove_from_parent();
1153 }
1154 }
1156 /* Maybe the current password method want's to do some changes... */
1157 if (is_object($this->pwObject)){
1158 $this->pwObject->save($this->dn);
1159 }
1161 /* Optionally execute a command after we're done */
1162 if ($mode == "add"){
1163 $this->handle_post_events("add", array("uid" => $this->uid));
1164 } elseif ($this->is_modified){
1165 $this->handle_post_events("modify", array("uid" => $this->uid));
1166 }
1168 return (0);
1169 }
1172 function create_initial_rdn($pattern)
1173 {
1174 // Only generate single RDNs
1175 if (preg_match('/\+/', $pattern)){
1176 msg_dialog::display(_("Error"), _("Cannot build RDN: no + allowed to build sub RDN!"), ERROR_DIALOG);
1177 return "";
1178 }
1180 // Extract attribute
1181 $attribute= preg_replace('/=.*$/', '', $pattern);
1182 if (!in_array_ics($attribute, $this->attributes)) {
1183 msg_dialog::display(_("Error"), _("Cannot build RDN: attribute is not defined!"), ERROR_DIALOG);
1184 return "";
1185 }
1187 // Sort attributes for length
1188 $attrl= array();
1189 foreach ($this->attributes as $attr) {
1190 $attrl[$attr]= strlen($attr);
1191 }
1192 arsort($attrl);
1194 // Walk thru sorted attributes and replace them in pattern
1195 foreach ($attrl as $attr => $dummy) {
1196 if (!is_array($this->$attr)){
1197 $pattern= preg_replace("/%$attr/", $this->$attr, $pattern);
1198 } else {
1199 // Array elements cannot be used for ID generation
1200 if (preg_match("/%$attr/", $pattern)) {
1201 msg_dialog::display(_("Error"), _("Cannot build RDN: invalid attribute parameters!"), ERROR_DIALOG);
1202 break;
1203 }
1204 }
1205 }
1207 // Internally assign value
1208 $this->$attribute= preg_replace('/^[^=]+=/', '', $pattern);
1210 return $pattern;
1211 }
1214 function update_new_dn()
1215 {
1216 // Alternative way to handle DN
1217 $pattern= $this->config->get_cfg_value("accountRDN");
1218 if ($pattern != "") {
1219 $rdn= $this->create_initial_rdn($pattern);
1220 $attribute= preg_replace('/=.*$/', '', $rdn);
1221 $value= preg_replace('/^[^=]+=$/', '', $rdn);
1223 /* Don't touch dn, if $attribute hasn't changed */
1224 if (isset($this->saved_attributes[$attribute]) && $this->saved_attributes[$attribute] == $this->$attribute &&
1225 $this->orig_base == $this->base ){
1226 $this->new_dn= $this->dn;
1227 } else {
1228 $this->new_dn= $this->create_unique_dn2($rdn, get_people_ou().$this->base);
1229 }
1231 // Original way to handle DN
1232 } else {
1234 $pt= "";
1235 if($this->config->get_cfg_value("personalTitleInDN") == "true"){
1236 if(!empty($this->personalTitle)){
1237 $pt = $this->personalTitle." ";
1238 }
1239 }
1241 $this->cn= $pt.$this->givenName." ".$this->sn;
1243 /* Permissions for that base? */
1244 if ($this->config->get_cfg_value("accountPrimaryAttribute") == "uid"){
1245 $this->new_dn= 'uid='.$this->uid.','.get_people_ou().$this->base;
1246 } else {
1247 /* Don't touch dn, if cn hasn't changed */
1248 if (isset($this->saved_attributes['cn']) && $this->saved_attributes['cn'] == $this->cn &&
1249 $this->orig_base == $this->base ){
1250 $this->new_dn= $this->dn;
1251 } else {
1252 $this->new_dn= $this->create_unique_dn('cn', get_people_ou().$this->base);
1253 }
1254 }
1255 }
1256 }
1259 /* Check formular input */
1260 function check()
1261 {
1262 /* Call common method to give check the hook */
1263 $message= plugin::check();
1265 /* Configurable password methods should be configured initially.
1266 */
1267 if($this->last_pw_storage != $this->pw_storage){
1268 $temp= passwordMethod::get_available_methods();
1269 foreach($temp['name'] as $id => $name){
1270 if($name == $this->pw_storage){
1271 if($temp['is_configurable'][$id] && !$this->pwObject instanceof $temp[$name] ){
1272 $message[] = _("The selected password method requires initial configuration!");
1273 }
1274 break;
1275 }
1276 }
1277 }
1279 $this->update_new_dn();
1281 /* Set the new acl base */
1282 if($this->dn == "new") {
1283 $this->set_acl_base($this->base);
1284 }
1286 /* Check if we are allowed to create/move this user */
1287 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
1288 $message[]= msgPool::permCreate();
1289 }elseif($this->orig_dn != "new" && $this->new_dn != $this->orig_dn && !$this->acl_is_moveable($this->base)){
1290 $message[]= msgPool::permMove();
1291 }
1293 /* UID already used? */
1294 $ldap= $this->config->get_ldap_link();
1295 $ldap->cd($this->config->current['BASE']);
1296 $ldap->search("(uid=$this->uid)", array("uid"));
1297 $ldap->fetch();
1298 if ($ldap->count() != 0 && $this->dn == 'new'){
1299 $message[]= msgPool::duplicated(_("Login"));
1300 }
1302 /* In template mode, the uid and givenName are autogenerated... */
1303 if ($this->sn == ""){
1304 $message[]= msgPool::required(_("Name"));
1305 }
1307 // Check if a wrong base was supplied
1308 if(!$this->baseSelector->checkLastBaseUpdate()){
1309 $message[]= msgPool::check_base();;
1310 }
1312 if (!$this->is_template){
1313 if ($this->givenName == ""){
1314 $message[]= msgPool::required(_("Given name"));
1315 }
1316 if ($this->uid == ""){
1317 $message[]= msgPool::required(_("Login"));
1318 }
1319 if ($this->config->get_cfg_value("accountPrimaryAttribute") != "uid"){
1320 $ldap->cat($this->new_dn);
1321 if ($ldap->count() != 0 && $this->dn != $this->new_dn && $this->dn == 'new'){
1322 $message[]= msgPool::duplicated(_("Name"));
1323 }
1324 }
1325 }
1327 /* Check for valid input */
1328 if ($this->is_modified && !tests::is_uid($this->uid)){
1330 if (strict_uid_mode()){
1331 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/");
1332 } else {
1333 $message[]= msgPool::invalid(_("Login"), $this->uid, "/[a-z0-9_-]/i");
1334 }
1335 }
1336 if (!tests::is_url($this->labeledURI)){
1337 $message[]= msgPool::invalid(_("Homepage"), "", "", "http://www.your-domain.com/yourname");
1338 }
1340 /* Check phone numbers */
1341 if (!tests::is_phone_nr($this->telephoneNumber)){
1342 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1343 }
1344 if (!tests::is_phone_nr($this->facsimileTelephoneNumber)){
1345 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1346 }
1347 if (!tests::is_phone_nr($this->mobile)){
1348 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1349 }
1350 if (!tests::is_phone_nr($this->pager)){
1351 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1352 }
1354 /* Check dates */
1355 if (!tests::is_date($this->dateOfBirth)){
1356 $message[]= msgPool::invalid(_("Date of birth"), $this->dateOfBirth,"" ,"23.02.2009");
1357 }
1359 /* Check for reserved characers */
1360 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName)){
1361 $message[]= msgPool::invalid(_("Given name"), $this->givenName, '/[^,+"?\'()=<>;\\\\]/');
1362 }
1363 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn)){
1364 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1365 }
1367 return $message;
1368 }
1371 /* Indicate whether a password change is needed or not */
1372 function password_change_needed()
1373 {
1374 if($this->multiple_support_active){
1375 return(FALSE);
1376 }else{
1378 if(in_array_strict("pw_storage",$this->multi_boxes)){
1379 return(TRUE);
1380 }
1381 return($this->pw_storage != $this->last_pw_storage && !$this->is_template);
1382 }
1383 }
1386 /* Load a jpegPhoto from LDAP, this is going to be simplified later on */
1387 function load_picture()
1388 {
1389 $ldap = $this->config->get_ldap_link();
1390 $ldap->cd ($this->dn);
1391 $data = $ldap->get_attribute($this->dn,"jpegPhoto");
1393 if((!$data) || ($data == "*removed*")){
1395 /* In case we don't get an entry, load a default picture */
1396 $this->set_picture ();
1397 $this->jpegPhoto= "*removed*";
1398 }else{
1400 /* Set picture */
1401 $this->photoData= $data;
1402 session::set('binary',$this->photoData);
1403 session::set('binarytype',"image/jpeg");
1404 $this->jpegPhoto= "";
1405 }
1406 }
1409 /* Load a certificate from LDAP, this is going to be simplified later on */
1410 function load_cert()
1411 {
1412 $ds= ldap_connect($this->config->current['SERVER']);
1413 ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
1414 if (function_exists("ldap_set_rebind_proc") && $this->config->get_cfg_value("ldapFollowReferrals") == "true"){
1415 ldap_set_option($this->cid, LDAP_OPT_REFERRALS, 1);
1416 ldap_set_rebind_proc($ds, array(&$this, "rebind"));
1417 }
1418 if ($this->config->get_cfg_value("ldapTLS") == "true"){
1419 ldap_start_tls($ds);
1420 }
1422 $r= ldap_bind($ds);
1423 $sr= @ldap_read($ds, $this->dn, "userCertificate=*", array("userCertificate"));
1425 if ($sr) {
1426 $ei= @ldap_first_entry($ds, $sr);
1428 if ($ei) {
1429 if (!$info = @ldap_get_values_len($ds, $ei, "userCertificate;binary")){
1430 $this->userCertificate= "";
1431 } else {
1432 $this->userCertificate= $info[0];
1433 }
1434 }
1435 } else {
1436 $this->userCertificate= "";
1437 }
1439 ldap_unbind($ds);
1440 }
1443 /* Load picture from file to object */
1444 function set_picture($filename ="")
1445 {
1446 if (!is_file($filename) || $filename =="" ){
1447 $filename= "./plugins/users/images/default.jpg";
1448 $this->jpegPhoto= "*removed*";
1449 }
1451 $fd = fopen ($filename, "rb");
1452 $this->photoData= fread ($fd, filesize ($filename));
1453 session::set('binary',$this->photoData);
1454 session::set('binarytype',"image/jpeg");
1455 $this->jpegPhoto= "";
1457 fclose ($fd);
1458 }
1461 /* Load certificate from file to object */
1462 function set_cert($cert, $filename)
1463 {
1464 if(!$this->acl_is_writeable("Certificate",(!is_object($this->parent) && !session::is_set('edit')))) return;
1465 $fd = fopen ($filename, "rb");
1466 if (filesize($filename)>0) {
1467 $this->$cert= fread ($fd, filesize ($filename));
1468 fclose ($fd);
1469 $this->is_modified= TRUE;
1470 } else {
1471 msg_dialog::display(_("Error"), _("Cannot open certificate!"), ERROR_DIALOG);
1472 }
1473 }
1475 /* Adapt from given 'dn' */
1476 function adapt_from_template($dn, $skip= array())
1477 {
1478 plugin::adapt_from_template($dn, $skip);
1480 /* Get password method from template
1481 */
1482 $tmp= passwordMethod::get_method($this->attrs['userPassword'][0]);
1483 if(is_object($tmp)){
1484 if($tmp->is_configurable()){
1485 $tmp->adapt_from_template($dn);
1486 $this->pwObject = &$tmp;
1487 }
1488 $this->pw_storage= $tmp->get_hash();
1489 }
1491 /* Get base */
1492 $this->base= preg_replace('/^[^,]+,'.preg_quote(get_people_ou(), '/').'/i', '', $dn);
1493 $this->baseSelector->setBase($this->base);
1495 if($this->governmentmode){
1497 /* Walk through govattrs */
1498 foreach ($this->govattrs as $val){
1500 if (in_array_strict($val, $skip)){
1501 continue;
1502 }
1504 if (isset($this->attrs["$val"][0])){
1506 /* If attribute is set, replace dynamic parts:
1507 %sn, %givenName and %uid. Fill these in our local variables. */
1508 $value= $this->attrs["$val"][0];
1510 foreach (array("sn", "givenName", "uid") as $repl){
1511 if (preg_match("/%$repl/i", $value)){
1512 $value= preg_replace ("/%$repl/i",
1513 $this->parent->$repl, $value);
1514 }
1515 }
1516 $this->$val= $value;
1517 }
1518 }
1519 }
1521 /* Get back uid/sn/givenName - only write if nothing's skipped */
1522 if ($this->parent !== NULL && count($skip) == 0){
1523 $this->uid= $this->parent->uid;
1524 $this->sn= $this->parent->sn;
1525 $this->givenName= $this->parent->givenName;
1526 }
1529 /* Generate dateOfBirth entry */
1530 if (isset ($this->attrs['dateOfBirth'])){
1531 /* This entry is ISO 8601 conform */
1532 list($year, $month, $day)= explode("-", $this->attrs['dateOfBirth'][0], 3);
1534 #TODO: use $lang to convert date
1535 $this->dateOfBirth= "$day.$month.$year";
1536 } else {
1537 $this->dateOfBirth= "";
1538 }
1540 }
1543 /* This avoids that users move themselves out of their rights.
1544 */
1545 function allowedBasesToMoveTo()
1546 {
1547 /* Get bases */
1548 $bases = $this->get_allowed_bases();
1549 return($bases);
1550 }
1553 function getCopyDialog()
1554 {
1555 $str = "";
1557 session::set('binary',$this->photoData);
1558 session::set('binarytype',"image/jpeg");
1560 /* Get random number for pictures */
1561 srand((double)microtime()*1000000);
1562 $rand = rand(0, 10000);
1564 $smarty = get_smarty();
1566 $smarty->assign("passwordTodo","clear");
1568 if(isset($_POST['passwordTodo'])){
1569 $smarty->assign("passwordTodo",$_POST['passwordTodo']);
1570 }
1572 $smarty->assign("sn", $this->sn);
1573 $smarty->assign("givenName",$this->givenName);
1574 $smarty->assign("uid", $this->uid);
1575 $smarty->assign("rand", $rand);
1576 $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
1579 $ret = array();
1580 $ret['string'] = $str;
1581 $ret['status'] = "";
1582 return($ret);
1583 }
1585 function saveCopyDialog()
1586 {
1587 /* Set_acl_base */
1588 $this->set_acl_base($this->base);
1590 if((isset($_FILES['picture_file']['tmp_name'])) && ($_FILES['picture_file']['size'] > 0)){
1591 $this->set_picture($_FILES['picture_file']['tmp_name']);
1592 }
1594 /* Remove picture? */
1595 if (isset($_POST['picture_remove'])){
1596 $this->jpegPhoto= "*removed*";
1597 $this->set_picture ("./plugins/users/images/default.jpg");
1598 $this->is_modified= TRUE;
1599 }
1601 $attrs = array("uid","givenName","sn");
1602 foreach($attrs as $attr){
1603 if(isset($_POST[$attr])){
1604 $this->$attr = $_POST[$attr];
1605 }
1606 }
1607 }
1610 function PrepareForCopyPaste($source)
1611 {
1612 plugin::PrepareForCopyPaste($source);
1614 /* Reset certificate information addepted from source user
1615 to avoid setting the same user certificate for the destination user. */
1616 $this->userPKCS12= "";
1617 $this->userSMIMECertificate= "";
1618 $this->userCertificate= "";
1619 $this->certificateSerialNumber= "";
1620 $this->old_certificateSerialNumber= "";
1621 $this->old_userPKCS12= "";
1622 $this->old_userSMIMECertificate= "";
1623 $this->old_userCertificate= "";
1625 /* Generate dateOfBirth entry */
1626 if (isset ($source['dateOfBirth'])){
1627 list($year, $month, $day)= explode("-", $source['dateOfBirth'][0], 3);
1628 $this->dateOfBirth= "$day.$month.$year";
1629 } else {
1630 $this->dateOfBirth= "";
1631 }
1633 // Try to load the user picture
1634 $tmp_dn = $this->dn;
1635 $this->dn = $source['dn'];
1636 $this->load_picture();
1637 $this->dn = $tmp_dn;
1638 }
1641 static function plInfo()
1642 {
1644 $govattrs= array(
1645 "gouvernmentOrganizationalUnit" => _("Unit"),
1646 "houseIdentifier" => _("House identifier"),
1647 "vocation" => _("Vocation"),
1648 "ivbbLastDeliveryCollective" => _("Last delivery"),
1649 "gouvernmentOrganizationalPersonLocality" => _("Person locality"),
1650 "gouvernmentOrganizationalUnitDescription" => _("Unit description"),
1651 "gouvernmentOrganizationalUnitSubjectArea" => _("Subject area"),
1652 "functionalTitle" => _("Functional title"),
1653 "certificateSerialNumber" => _("Certificate serial number"),
1654 "publicVisible" => _("Public visible"),
1655 "street" => _("Street"),
1656 "role" => _("Role"),
1657 "postalCode" => _("Postal code"));
1659 $ret = array(
1660 "plShortName" => _("Generic"),
1661 "plDescription" => _("Generic user settings"),
1662 "plSelfModify" => TRUE,
1663 "plDepends" => array(),
1664 "plPriority" => 1,
1665 "plSection" => array("personal" => _("My account")),
1666 "plCategory" => array("users" => array("description" => _("Users"),
1667 "objectClass" => "gosaAccount")),
1669 "plProvidedAcls" => array(
1671 "sn" => _("Surname"),
1672 "givenName" => _("Given name"),
1673 "uid" => _("User identification"),
1674 "personalTitle" => _("Personal title"),
1675 "academicTitle" => _("Academic title"),
1677 "dateOfBirth" => _("Date of birth"),
1678 "gender" => _("Sex"),
1679 "preferredLanguage" => _("Preferred language"),
1680 "base" => _("Base"),
1682 "userPicture" => _("User picture"),
1684 "gosaLoginRestriction" => _("Login restrictions"),
1686 "o" => _("Organization"),
1687 "ou" => _("Department"),
1688 "departmentNumber" => _("Department number"),
1689 "manager" => _("Manager"),
1690 "employeeNumber" => _("Employee number"),
1691 "employeeType" => _("Employee type"),
1693 "roomNumber" => _("Room number"),
1694 "telephoneNumber" => _("Telefon number"),
1695 "pager" => _("Pager number"),
1696 "mobile" => _("Mobile number"),
1697 "facsimileTelephoneNumber" => _("Fax number"),
1699 "st" => _("State"),
1700 "l" => _("Location"),
1701 "postalAddress" => _("Postal address"),
1703 "homePostalAddress" => _("Home postal address"),
1704 "homePhone" => _("Home phone number"),
1705 "labeledURI" => _("Homepage"),
1706 "userPassword" => _("User password method"),
1707 "Certificate" => _("User certificates"))
1709 );
1711 /* Append government attributes if required */
1712 global $config;
1713 if($config->get_cfg_value("honourIvbbAttributes") == "true"){
1714 foreach($govattrs as $attr => $desc){
1715 $ret["plProvidedAcls"][$attr] = $desc;
1716 }
1717 }
1718 return($ret);
1719 }
1721 function get_multi_edit_values()
1722 {
1723 $ret = plugin::get_multi_edit_values();
1724 if(in_array_strict("pw_storage",$this->multi_boxes)){
1725 $ret['pw_storage'] = $this->pw_storage;
1726 }
1727 if(in_array_strict("edit_picture",$this->multi_boxes)){
1728 $ret['jpegPhoto'] = $this->jpegPhoto;
1729 $ret['photoData'] = $this->photoData;
1730 $ret['old_jpegPhoto'] = $this->old_jpegPhoto;
1731 $ret['old_photoData'] = $this->old_photoData;
1732 }
1733 if(isset($ret['dateOfBirth'])){
1734 unset($ret['dateOfBirth']);
1735 }
1736 if(isset($ret['cn'])){
1737 unset($ret['cn']);
1738 }
1739 $ret['is_modified'] = $this->is_modified;
1740 if(in_array_strict("base",$this->multi_boxes)){
1741 $ret['orig_base']="Changed_by_Multi_Plug";
1742 $ret['base']=$this->base;
1743 }
1745 $ret['gosaLoginRestriction'] = $this->gosaLoginRestriction;
1746 $ret['gosaLoginRestriction_some'] = $this->gosaLoginRestriction_some;
1748 return($ret);
1749 }
1752 function multiple_save_object()
1753 {
1755 if(!isset($_POST['user_mulitple_edit'])) return;
1757 plugin::multiple_save_object();
1759 /* Get pw_storage mode */
1760 if (isset($_POST['pw_storage'])){
1761 foreach(array("pw_storage") as $val){
1762 if(isset($_POST[$val])){
1763 $data= validate(get_post($val));
1764 if ($data != $this->$val){
1765 $this->is_modified= TRUE;
1766 }
1767 $this->$val= $data;
1768 }
1769 }
1770 }
1772 /* Refresh base */
1773 if ($this->acl_is_moveable($this->base)){
1774 if (!$this->baseSelector->update()) {
1775 msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
1776 }
1777 if ($this->base != $this->baseSelector->getBase()) {
1778 $this->base= $this->baseSelector->getBase();
1779 }
1780 }
1782 if(isset($_POST['user_mulitple_edit'])){
1783 foreach(array("base","pw_storage","edit_picture") as $val){
1784 if(isset($_POST["use_".$val])){
1785 $this->multi_boxes[] = $val;
1786 }
1787 }
1788 }
1790 /* Sync lists */
1791 $this->gosaLoginRestrictionWidget->save_object();
1792 if ($this->gosaLoginRestrictionWidget->isModified()) {
1793 $this->gosaLoginRestriction= array_values($this->gosaLoginRestrictionWidget->getMaintainedData());
1794 }
1795 }
1798 function multiple_check()
1799 {
1800 /* Call check() to set new_dn correctly ... */
1801 $message = plugin::multiple_check();
1803 /* Set the new acl base */
1804 if($this->dn == "new") {
1805 $this->set_acl_base($this->base);
1806 }
1807 if (!tests::is_url($this->labeledURI) && in_array_strict("labeledURI",$this->multi_boxes)){
1808 $message[]= msgPool::invalid(_("Homepage"));
1809 }
1810 if (!tests::is_phone_nr($this->telephoneNumber) && in_array_strict("telephoneNumber",$this->multi_boxes)){
1811 $message[]= msgPool::invalid(_("Phone"), $this->telephoneNumber, "/[\/0-9 ()+*-]/");
1812 }
1813 if (!tests::is_phone_nr($this->facsimileTelephoneNumber) && in_array_strict("facsimileTelephoneNumber",$this->multi_boxes)){
1814 $message[]= msgPool::invalid(_("Fax"), $this->facsimileTelephoneNumber, "/[\/0-9 ()+*-]/");
1815 }
1816 if (!tests::is_phone_nr($this->mobile) && in_array_strict("mobile",$this->multi_boxes)){
1817 $message[]= msgPool::invalid(_("Mobile"), $this->mobile, "/[\/0-9 ()+*-]/");
1818 }
1819 if (!tests::is_phone_nr($this->pager) && in_array_strict("pager",$this->multi_boxes)){
1820 $message[]= msgPool::invalid(_("Pager"), $this->pager, "/[\/0-9 ()+*-]/");
1821 }
1822 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->givenName) && in_array_strict("givenName",$this->multi_boxes)){
1823 $message[]= msgPool::invalid(_("Given name"), $this->giveName, '/[^,+"?\'()=<>;\\\\]/');
1824 }
1825 if (preg_match ('/[,+"?\'()=<>;\\\\]/', $this->sn) && in_array_strict("sn",$this->multi_boxes)){
1826 $message[]= msgPool::invalid(_("Name"), $this->sn, '/[^,+"?\'()=<>;\\\\]/');
1827 }
1828 return($message);
1829 }
1833 function multiple_execute()
1834 {
1835 return($this->execute());
1836 }
1839 /*! \brief Prepares the plugin to be used for multiple edit
1840 * Update plugin attributes with given array of attribtues.
1841 * \param array Array with attributes that must be updated.
1842 */
1843 function init_multiple_support($attrs,$all)
1844 {
1845 plugin::init_multiple_support($attrs,$all);
1847 // Get login restrictions
1848 if(isset($attrs['gosaLoginRestriction'])){
1849 $this->gosaLoginRestriction =array();
1850 for($i =0;$i < $attrs['gosaLoginRestriction']['count']; $i++){
1851 $this->gosaLoginRestriction[] = $attrs['gosaLoginRestriction'][$i];
1852 }
1853 }
1855 // Detect the managers name
1856 $this->manager_name = "";
1857 $ldap = $this->config->get_ldap_link();
1858 if(!empty($this->manager)){
1859 $ldap->cat($this->manager, array('cn'));
1860 if($ldap->count()){
1861 $attrs = $ldap->fetch();
1862 $this->manager_name = $attrs['cn'][0];
1863 }else{
1864 $this->manager_name = "("._("Unknown")."!): ".$this->manager;
1865 }
1866 }
1868 // Detect login restriction not used in all user objects.
1869 $this->gosaLoginRestriction_some = array();
1870 if(isset($all['gosaLoginRestriction'])){
1871 for($i=0;$i<$all['gosaLoginRestriction']['count'];$i++){
1872 $this->gosaLoginRestriction_some[] = $all['gosaLoginRestriction'][$i];
1873 }
1874 }
1877 // Reinit the login restriction list.
1878 $data = $this->convertLoginRestriction();
1879 if(count($data)){
1880 $this->gosaLoginRestrictionWidget->setListData($data['data'], $data['displayData']);
1881 }
1882 }
1885 function set_multi_edit_values($attrs)
1886 {
1887 $lR = array();
1889 // Update loginRestrictions, keep my settings while ip is optional
1890 foreach($attrs['gosaLoginRestriction_some'] as $ip){
1891 if(in_array_strict($ip, $this->gosaLoginRestriction) && in_array_strict($ip, $attrs['gosaLoginRestriction'])){
1892 $lR[] = $ip;
1893 }
1894 }
1896 // Add enforced loginRestrictions
1897 foreach($attrs['gosaLoginRestriction'] as $ip){
1898 $lR[] = $ip;
1899 }
1901 $lR = array_values(array_unique($lR));
1902 $this->is_modified |= array_differs($this->gosaLoginRestriction, $lR);
1903 plugin::set_multi_edit_values($attrs);
1904 $this->gosaLoginRestriction = $lR;
1905 }
1908 function convertLoginRestriction()
1909 {
1910 $all = array_unique(array_merge($this->gosaLoginRestriction,$this->gosaLoginRestriction_some));
1911 $data = array();
1912 foreach($all as $ip){
1913 $data['data'][] = $ip;
1914 if(!in_array_strict($ip, $this->gosaLoginRestriction)){
1915 $data['displayData'][] = array('mode' => LIST_MARKED , 'data' => array($ip.' ('._("Entries differ").')'));
1916 }else{
1917 $data['displayData'][] = array('mode' => 0 , 'data' => array($ip));
1918 }
1919 }
1920 return($data);
1921 }
1922 }
1924 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1925 ?>