2 /*******************************************************************************
3 * Copyright (C) 2007 Easter-eggs
4 * http://ldapsaisie.labs.libre-entreprise.org
6 * Author: See AUTHORS file in top-level directory.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 ******************************************************************************/
23 LSsession :: loadLSclass('LSformElement');
26 * Element password d'un formulaire pour LdapSaisie
28 * Cette classe définis les éléments password des formulaires.
29 * Elle étant la classe basic LSformElement.
31 * @author Benjamin Renard <brenard@easter-eggs.com>
34 class LSformElement_password extends LSformElement {
36 var $fieldTemplate = 'LSformElement_password_field.tpl';
37 var $template = 'LSformElement_password.tpl';
39 var $sendMail = false;
42 * Recupère la valeur de l'élement passée en POST
44 * Cette méthode vérifie la présence en POST de la valeur de l'élément et la récupère
45 * pour la mettre dans le tableau passer en paramètre avec en clef le nom de l'élément
47 * @param[] array Pointeur sur le tableau qui recupèrera la valeur.
49 * @retval boolean true si la valeur est présente en POST, false sinon
51 function getPostData(&$return) {
52 // Récupère la valeur dans _POST, et les vérifie avec la fonction générale
53 $retval = parent :: getPostData($return);
54 // Si une valeur est recupérée
56 $val = $this -> form -> ldapObject -> attrs[$this -> name] -> getValue();
57 if( (empty($return[$this -> name][0]) ) && ( ! empty( $val ) ) ) {
58 unset($return[$this -> name]);
59 $this -> form -> _notUpdate[$this -> name] == true;
63 if (!isset($this -> params['html_options']['isLoginPassword']) || $this -> params['html_options']['isLoginPassword']) {
64 if ($this -> verifyPassword($return[$this -> name][0])) {
65 LSdebug("Password : no change");
66 unset($return[$this -> name]);
67 $this -> form -> _notUpdate[$this -> name] == true;
73 if (isset($_POST['LSformElement_password_'.$this -> name.'_send'])) {
74 if ($_POST['LSformElement_password_'.$this -> name.'_send']==1) {
75 $this -> sendMail = true;
76 LSdebug ('send by form');
79 else if ($this -> params['html_options']['mail']['send']==1) {
80 $this -> sendMail = true;
81 LSdebug ('send by config');
83 if ($this -> sendMail && LSsession :: loadLSaddon('mail')) {
84 $msg = $this -> params['html_options']['mail']['msg'];
85 $subject = $this -> params['html_options']['mail']['subject'];
86 if (isset($_POST['LSformElement_password_'.$this -> name.'_msg'])) {
87 $msgInfos = json_decode($_POST['LSformElement_password_'.$this -> name.'_msg']);
88 if ($msgInfos -> subject) {
89 $subject = $msgInfos -> subject;
91 if ($msgInfos -> msg) {
92 $msg = $msgInfos -> msg;
94 if ($msgInfos -> mail) {
95 $mail = $msgInfos -> mail;
98 $this -> sendMail = array (
99 'subject' => $subject,
102 'pwd' => $return[$this -> name][0]
104 $this -> attr_html -> attribute -> addObjectEvent('after_modify',$this,'send');
111 * Retourne les infos d'affichage de l'élément
113 * Cette méthode retourne les informations d'affichage de l'élement
117 function getDisplay(){
118 LSsession :: addCssFile('LSformElement_password.css');
119 $return = $this -> getLabelInfos();
121 if (!$this -> isFreeze()) {
124 LSsession :: addHelpInfos(
125 'LSformElement_password',
127 'generate' => _('Generate a password.'),
128 'verify' => _('Compare with stored password.'),
129 'view' => _('Display password.'),
130 'viewHash' => _('Display hashed password.'),
131 'hide' => _('Hide password.'),
132 'mail' => _("The password will be sent by mail if changed. Click to disable automatic notification."),
133 'nomail' => _("The password will not be sent if changed. Click to enable automatic notification."),
134 'editmail' => _("Modify the mail sent to notice the user")
138 if (($this -> params['html_options']['generationTool'])&&($this -> params['html_options']['autoGenerate'])&&(empty($this -> values))) {
139 $pwd=$this->generatePassword($this -> params);
143 'generate' => ($this -> params['html_options']['generationTool']==True),
144 'viewHash' => ($this -> params['html_options']['viewHash']==True),
145 'verify' => ( (!$this -> attr_html -> attribute -> ldapObject-> isNew()) && ( (isset($this -> params['html_options']['verify']) && $this -> params['html_options']['verify']) || (!isset($this -> params['html_options']['verify'])) ) )
147 if (isset($this -> params['html_options']['mail'])) {
148 $params['mail'] = $this -> params['html_options']['mail'];
150 LSsession :: addJSconfigParam($this -> name,$params);
152 LSsession :: addJSscript('LSformElement_password_field.js');
153 LSsession :: addJSscript('LSformElement_password.js');
155 $return['html'] = $this -> fetchTemplate(NULL,array('pwd' => $pwd));
159 function generatePassword($params=NULL) {
160 return generatePassword($params['html_options']['chars'],$params['html_options']['lenght']);
163 function verifyPassword($pwd) {
164 if ($this -> attr_html -> attribute -> ldapObject -> isNew()) {
167 return LSsession :: checkUserPwd($this -> attr_html -> attribute -> ldapObject,$pwd);
170 function send($params) {
171 if (is_array($this -> sendMail)) {
172 $mail = (String)$this -> sendMail['mail'];
175 $mail_attr = $this -> attr_html -> attribute -> ldapObject -> attrs[$this -> params['html_options']['mail']['mail_attr']];
176 if ($mail_attr instanceOf LSattribute) {
177 $mail = $mail_attr -> getValue();
181 LSdebug("L'attribut $mail_attr pour l'envoie du nouveau mot de passe n'existe pas.");
186 if (checkEmail($mail,NULL,true)) {
187 $this -> attr_html -> attribute -> ldapObject -> registerOtherValue('password',$this -> sendMail['pwd']);
188 $msg = $this -> attr_html -> attribute -> ldapObject -> getFData($this -> sendMail['msg']);
191 $this -> sendMail['subject'],
194 LSsession :: addInfo(_('Notice mail sent.'));
198 LSdebug('Adresse mail incorrect : '.$mail);
205 public static function ajax_verifyPassword(&$data) {
206 if ((isset($_REQUEST['attribute'])) && (isset($_REQUEST['objecttype'])) && (isset($_REQUEST['fieldValue'])) && (isset($_REQUEST['idform'])) && (isset($_REQUEST['objectdn'])) ) {
207 if (LSsession ::loadLSobject($_REQUEST['objecttype'])) {
208 $object = new $_REQUEST['objecttype']();
209 $object -> loadData($_REQUEST['objectdn']);
210 $form = $object -> getForm($_REQUEST['idform']);
212 $field=$form -> getElement($_REQUEST['attribute']);
214 $val = $field -> verifyPassword($_REQUEST['fieldValue']);
216 'verifyPassword' => $val
220 LSdebug('Impossible de récupérer le LSformElement');
224 LSdebug('Impossible de recuperer le LSform.');
230 public static function ajax_generatePassword(&$data) {
231 if ((isset($_REQUEST['attribute'])) && (isset($_REQUEST['objecttype'])) && (isset($_REQUEST['objectdn'])) && (isset($_REQUEST['idform'])) ) {
232 if (LSsession ::loadLSobject($_REQUEST['objecttype'])) {
233 $params = LSconfig :: get("LSobjects.".$_REQUEST['objecttype'].".attrs.".$_REQUEST['attribute']);
234 $val = self :: generatePassword($params);
237 'generatePassword' => $val
244 public static function ajax_viewHash(&$data) {
245 if ((isset($_REQUEST['attribute'])) && (isset($_REQUEST['objecttype'])) && (isset($_REQUEST['objectdn'])) ) {
246 if (LSsession ::loadLSobject($_REQUEST['objecttype'])) {
247 $object = new $_REQUEST['objecttype']();
248 $object -> loadData($_REQUEST['objectdn']);
249 if (LSsession::canAccess($_REQUEST['objecttype'],$_REQUEST['objectdn'],null,$_REQUEST['attribute'])) {
250 $values = $object -> getValue($_REQUEST['attribute']);
251 if (is_string($values[0])) {