ui.passwordwidget.class.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * Class UIPasswordWidget
  20. * UI wdiget for displaying and editing one-way encrypted passwords
  21. *
  22. * @copyright Copyright (C) 2010-2012 Combodo SARL
  23. * @license http://opensource.org/licenses/AGPL-3.0
  24. */
  25. require_once(APPROOT.'/application/webpage.class.inc.php');
  26. require_once(APPROOT.'/application/displayblock.class.inc.php');
  27. class UIPasswordWidget
  28. {
  29. protected static $iWidgetIndex = 0;
  30. protected $sAttCode;
  31. protected $sNameSuffix;
  32. protected $iId;
  33. public function __construct($sAttCode, $iInputId, $sNameSuffix = '')
  34. {
  35. self::$iWidgetIndex++;
  36. $this->sAttCode = $sAttCode;
  37. $this->sNameSuffix = $sNameSuffix;
  38. $this->iId = $iInputId;
  39. }
  40. /**
  41. * Get the HTML fragment corresponding to the linkset editing widget
  42. * @param WebPage $oP The web page used for all the output
  43. * @param Hash $aArgs Extra context arguments
  44. * @return string The HTML fragment to be inserted into the page
  45. */
  46. public function Display(WebPage $oPage, $aArgs = array())
  47. {
  48. $sCode = $this->sAttCode.$this->sNameSuffix;
  49. $iWidgetIndex = self::$iWidgetIndex;
  50. $aPasswordValues = utils::ReadPostedParam("attr_{$sCode}", null, 'raw_data');
  51. $sPasswordValue = $aPasswordValues ? $aPasswordValues['value'] : '*****';
  52. $sConfirmPasswordValue = $aPasswordValues ? $aPasswordValues['confirm'] : '*****';
  53. $sChangedValue = (($sPasswordValue != '*****') || ($sConfirmPasswordValue != '*****')) ? 1 : 0;
  54. $sHtmlValue = '';
  55. $sHtmlValue = '<input type="password" maxlength="255" name="attr_'.$sCode.'[value]" id="'.$this->iId.'" value="'.htmlentities($sPasswordValue, ENT_QUOTES, 'UTF-8').'"/>&nbsp;<span class="form_validation" id="v_'.$this->iId.'"></span><br/>';
  56. $sHtmlValue .= '<input type="password" maxlength="255" id="'.$this->iId.'_confirm" value="'.htmlentities($sConfirmPasswordValue, ENT_QUOTES, 'UTF-8').'" name="attr_'.$sCode.'[confirm]"/> '.Dict::S('UI:PasswordConfirm').' <input id="'.$this->iId.'_reset" type="button" value="'.Dict::S('UI:Button:ResetPassword').'" onClick="ResetPwd(\''.$this->iId.'\');">';
  57. $sHtmlValue .= '<input type="hidden" id="'.$this->iId.'_changed" name="attr_'.$sCode.'[changed]" value="'.$sChangedValue.'"/>';
  58. $oPage->add_ready_script("$('#$this->iId').bind('keyup change', function(evt) { return PasswordFieldChanged('$this->iId') } );"); // Bind to a custom event: validate
  59. $oPage->add_ready_script("$('#$this->iId').bind('keyup change validate', function(evt, sFormId) { return ValidatePasswordField('$this->iId', sFormId) } );"); // Bind to a custom event: validate
  60. $oPage->add_ready_script("$('#{$this->iId}_confirm').bind('keyup change', function(evt, sFormId) { return ValidatePasswordField('$this->iId', sFormId) } );"); // Bind to a custom event: validate
  61. $oPage->add_ready_script("$('#{$this->iId}').bind('update', function(evt, sFormId)
  62. {
  63. if ($(this).attr('disabled'))
  64. {
  65. $('#{$this->iId}_confirm').attr('disabled', 'disabled');
  66. $('#{$this->iId}_changed').attr('disabled', 'disabled');
  67. $('#{$this->iId}_reset').attr('disabled', 'disabled');
  68. }
  69. else
  70. {
  71. $('#{$this->iId}_confirm').removeAttr('disabled');
  72. $('#{$this->iId}_changed').removeAttr('disabled');
  73. $('#{$this->iId}_reset').removeAttr('disabled');
  74. }
  75. }
  76. );"); // Bind to a custom event: update to handle enabling/disabling
  77. return $sHtmlValue;
  78. }
  79. }
  80. ?>