1
0

setupchecks.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2014-2016 ownCloud Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. (function() {
  7. OC.SetupChecks = {
  8. /* Message types */
  9. MESSAGE_TYPE_INFO:0,
  10. MESSAGE_TYPE_WARNING:1,
  11. MESSAGE_TYPE_ERROR:2,
  12. /**
  13. * Runs setup checks on the server side
  14. *
  15. * @return $.Deferred object resolved with an array of error messages
  16. */
  17. checkSetup: function() {
  18. var deferred = $.Deferred();
  19. var afterCall = function(data, statusText, xhr) {
  20. var messages = [];
  21. if (xhr.status === 200 && data) {
  22. if (Object.keys(data.generic).length > 0) {
  23. Object.keys(data.generic).forEach(function(key){
  24. Object.keys(data.generic[key]).forEach(function(title){
  25. if (data.generic[key][title].severity != 'success') {
  26. data.generic[key][title].pass = false;
  27. OC.SetupChecks.addGenericSetupCheck(data.generic[key], title, messages);
  28. }
  29. });
  30. });
  31. }
  32. } else {
  33. messages.push({
  34. msg: t('core', 'Error occurred while checking server setup'),
  35. type: OC.SetupChecks.MESSAGE_TYPE_ERROR
  36. });
  37. }
  38. deferred.resolve(messages);
  39. };
  40. $.ajax({
  41. type: 'GET',
  42. url: OC.generateUrl('settings/ajax/checksetup'),
  43. allowAuthErrors: true
  44. }).then(afterCall, afterCall);
  45. return deferred.promise();
  46. },
  47. escapeHTML: function(text) {
  48. return text.toString()
  49. .split('&').join('&')
  50. .split('<').join('&lt;')
  51. .split('>').join('&gt;')
  52. .split('"').join('&quot;')
  53. .split('\'').join('&#039;')
  54. },
  55. /**
  56. * @param message The message string containing placeholders.
  57. * @param parameters An object with keys as placeholders and values as their replacements.
  58. *
  59. * @return The message with placeholders replaced by values.
  60. */
  61. richToParsed: function (message, parameters) {
  62. for (var [placeholder, parameter] of Object.entries(parameters)) {
  63. var replacement;
  64. if (parameter.type === 'user') {
  65. replacement = '@' + this.escapeHTML(parameter.name);
  66. } else if (parameter.type === 'file') {
  67. replacement = this.escapeHTML(parameter.path) || this.escapeHTML(parameter.name);
  68. } else if (parameter.type === 'highlight') {
  69. replacement = '<a href="' + encodeURI(parameter.link) + '">' + this.escapeHTML(parameter.name) + '</a>';
  70. } else {
  71. replacement = this.escapeHTML(parameter.name);
  72. }
  73. message = message.replace('{' + placeholder + '}', replacement);
  74. }
  75. return message;
  76. },
  77. addGenericSetupCheck: function(data, check, messages) {
  78. var setupCheck = data[check] || { pass: true, description: '', severity: 'info', linkToDoc: null}
  79. var type = OC.SetupChecks.MESSAGE_TYPE_INFO
  80. if (setupCheck.severity === 'warning') {
  81. type = OC.SetupChecks.MESSAGE_TYPE_WARNING
  82. } else if (setupCheck.severity === 'error') {
  83. type = OC.SetupChecks.MESSAGE_TYPE_ERROR
  84. }
  85. var message = setupCheck.description;
  86. if (message) {
  87. message = this.escapeHTML(message)
  88. }
  89. if (setupCheck.descriptionParameters) {
  90. message = this.richToParsed(message, setupCheck.descriptionParameters);
  91. }
  92. if (setupCheck.linkToDoc) {
  93. message += ' ' + t('core', 'For more details see the {linkstart}documentation ↗{linkend}.')
  94. .replace('{linkstart}', '<a target="_blank" rel="noreferrer noopener" class="external" href="' + setupCheck.linkToDoc + '">')
  95. .replace('{linkend}', '</a>');
  96. }
  97. if (setupCheck.elements) {
  98. message += '<br><ul>'
  99. setupCheck.elements.forEach(function(element){
  100. message += '<li>';
  101. message += element
  102. message += '</li>';
  103. });
  104. message += '</ul>'
  105. }
  106. if (!setupCheck.pass) {
  107. messages.push({
  108. msg: message,
  109. type: type,
  110. })
  111. }
  112. },
  113. };
  114. })();