You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

requestremoteaddressplugin.js 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  3. *
  4. * @license GNU AGPL version 3 or any later version
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. (function() {
  21. OCA.WorkflowEngine = OCA.WorkflowEngine || {};
  22. OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
  23. OCA.WorkflowEngine.Plugins.RequestRemoteAddressPlugin = {
  24. getCheck: function() {
  25. return {
  26. 'class': 'OCA\\WorkflowEngine\\Check\\RequestRemoteAddress',
  27. 'name': t('workflowengine', 'Request remote address'),
  28. 'operators': [
  29. {'operator': 'matchesIPv4', 'name': t('workflowengine', 'matches IPv4')},
  30. {'operator': '!matchesIPv4', 'name': t('workflowengine', 'does not match IPv4')},
  31. {'operator': 'matchesIPv6', 'name': t('workflowengine', 'matches IPv6')},
  32. {'operator': '!matchesIPv6', 'name': t('workflowengine', 'does not match IPv6')}
  33. ]
  34. };
  35. },
  36. render: function(element, check) {
  37. if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\RequestRemoteAddress') {
  38. return;
  39. }
  40. var placeholder = '127.0.0.1/32'; // Do not translate!!!
  41. if (check['operator'] === 'matchesIPv6' || check['operator'] === '!matchesIPv6') {
  42. placeholder = '::1/128'; // Do not translate!!!
  43. if (this._validateIPv6(check['value'])) {
  44. $(element).removeClass('invalid-input');
  45. } else {
  46. $(element).addClass('invalid-input');
  47. }
  48. } else {
  49. if (this._validateIPv4(check['value'])) {
  50. $(element).removeClass('invalid-input');
  51. } else {
  52. $(element).addClass('invalid-input');
  53. }
  54. }
  55. $(element).css('width', '300px')
  56. .attr('placeholder', placeholder)
  57. .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: placeholder}))
  58. .addClass('has-tooltip')
  59. .tooltip({
  60. placement: 'bottom'
  61. });
  62. },
  63. _validateIPv4: function(string) {
  64. var regexRegex = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(3[0-2]|[1-2][0-9]|[1-9])$/,
  65. result = regexRegex.exec(string);
  66. return result !== null;
  67. },
  68. _validateIPv6: function(string) {
  69. var regexRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(1([01][0-9]|2[0-8])|[1-9][0-9]|[0-9])$/,
  70. result = regexRegex.exec(string);
  71. return result !== null;
  72. }
  73. };
  74. })();
  75. OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.RequestRemoteAddressPlugin);