PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/php/lib/forms/signature_simulator_form.class.php

https://bitbucket.org/chamilo/chamilo-webservice-dev/
PHP | 71 lines | 51 code | 15 blank | 5 comment | 0 complexity | 914e4f081d03bab3f259af7c60f5cd5a MD5 | raw file
  1. <?php
  2. namespace webservice;
  3. use common\libraries\FormValidator;
  4. use common\libraries\Translation;
  5. use common\libraries\Utilities;
  6. use common\libraries\ChamiloWebserviceAuthentication;
  7. /**
  8. * Form for signature simulator
  9. * @package webservice.php.lib.forms
  10. * @author Alexander Van Paemel
  11. */
  12. class SignatureSimulatorForm extends FormValidator
  13. {
  14. private $user;
  15. function __construct($user, $action)
  16. {
  17. parent :: __construct('signature_properties', 'post', $action);
  18. $this->user = $user;
  19. $this->build_form();
  20. $this->setDefaults();
  21. }
  22. function build_form()
  23. {
  24. $this->addElement('static', 'username', Translation :: get('Username'));
  25. $this->addElement('static', 'password', Translation :: get('Password'));
  26. $this->addElement('static', 'security_token', Translation :: get('SecurityToken'));
  27. $this->addElement('text', 'nonce', Translation :: get('Nonce'));
  28. $this->addRule('nonce', Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES), 'required');
  29. $this->addElement('text', 'timestamp', Translation :: get('Timestamp'));
  30. $this->addRule('timestamp', Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES), 'required');
  31. $this->addElement('text', 'ip', Translation :: get('IP'));
  32. $this->addRule('ip', Translation :: get('ThisFieldIsRequired', null, Utilities :: COMMON_LIBRARIES), 'required');
  33. $buttons[] = $this->createElement('style_submit_button', 'submit', Translation :: get('Submit', null, Utilities :: COMMON_LIBRARIES), array('class' => 'positive update'));
  34. $buttons[] = $this->createElement('style_reset_button', 'reset', Translation :: get('Reset', null, Utilities :: COMMON_LIBRARIES), array('class' => 'normal empty'));
  35. $this->addGroup($buttons, 'buttons', null, '&nbsp;', false);
  36. }
  37. function setDefaults($defaults = array ())
  38. {
  39. $defaults['username'] = $this->user->get_username();
  40. $defaults['password'] = '*****';
  41. $defaults['security_token'] = $this->user->get_security_token();
  42. $defaults['nonce'] = uniqid();
  43. $defaults['ip'] = $_SERVER[ChamiloWebserviceAuthentication :: PARAM_REMOTE_ADDRESS];
  44. $defaults['timestamp'] = time();
  45. parent :: setDefaults($defaults);
  46. }
  47. function calculate_signature()
  48. {
  49. $values = $this->exportValues();
  50. $base_text = $this->user->get_username() . '&' . $values['nonce'] . '&' . $this->user->get_password()
  51. . '&' . $values['timestamp'] . '&' . $values['ip'];
  52. $secret_key = $this->user->get_security_token();
  53. return hash_hmac(ChamiloWebserviceAuthentication :: PARAM_HASH_ALGORITHM, $base_text, $secret_key);
  54. }
  55. }
  56. ?>