setupchecks.js 3.7 KB

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