OC_JSON.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OC\Authentication\TwoFactorAuth\Manager as TwoFactorAuthManager;
  8. class OC_JSON {
  9. /**
  10. * Check if the app is enabled, send json error msg if not
  11. * @param string $app
  12. * @deprecated 12.0.0 Use the AppFramework instead. It will automatically check if the app is enabled.
  13. * @suppress PhanDeprecatedFunction
  14. */
  15. public static function checkAppEnabled($app) {
  16. if (!\OC::$server->getAppManager()->isEnabledForUser($app)) {
  17. $l = \OC::$server->getL10N('lib');
  18. self::error([ 'data' => [ 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ]]);
  19. exit();
  20. }
  21. }
  22. /**
  23. * Check if the user is logged in, send json error msg if not
  24. * @deprecated 12.0.0 Use annotation based ACLs from the AppFramework instead
  25. * @suppress PhanDeprecatedFunction
  26. */
  27. public static function checkLoggedIn() {
  28. $twoFactorAuthManger = \OC::$server->get(TwoFactorAuthManager::class);
  29. if (!\OC::$server->getUserSession()->isLoggedIn()
  30. || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
  31. $l = \OC::$server->getL10N('lib');
  32. http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
  33. self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
  34. exit();
  35. }
  36. }
  37. /**
  38. * Check an ajax get/post call if the request token is valid, send json error msg if not.
  39. * @deprecated 12.0.0 Use annotation based CSRF checks from the AppFramework instead
  40. * @suppress PhanDeprecatedFunction
  41. */
  42. public static function callCheck() {
  43. if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
  44. header('Location: ' . \OC::$WEBROOT);
  45. exit();
  46. }
  47. if (!\OC::$server->getRequest()->passesCSRFCheck()) {
  48. $l = \OC::$server->getL10N('lib');
  49. self::error([ 'data' => [ 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ]]);
  50. exit();
  51. }
  52. }
  53. /**
  54. * Check if the user is a admin, send json error msg if not.
  55. * @deprecated 12.0.0 Use annotation based ACLs from the AppFramework instead
  56. * @suppress PhanDeprecatedFunction
  57. */
  58. public static function checkAdminUser() {
  59. if (!OC_User::isAdminUser(OC_User::getUser())) {
  60. $l = \OC::$server->getL10N('lib');
  61. self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
  62. exit();
  63. }
  64. }
  65. /**
  66. * Send json error msg
  67. * @deprecated 12.0.0 Use a AppFramework JSONResponse instead
  68. * @suppress PhanDeprecatedFunction
  69. * @psalm-taint-escape html
  70. */
  71. public static function error($data = []) {
  72. $data['status'] = 'error';
  73. header('Content-Type: application/json; charset=utf-8');
  74. echo self::encode($data);
  75. }
  76. /**
  77. * Send json success msg
  78. * @deprecated 12.0.0 Use a AppFramework JSONResponse instead
  79. * @suppress PhanDeprecatedFunction
  80. * @psalm-taint-escape html
  81. */
  82. public static function success($data = []) {
  83. $data['status'] = 'success';
  84. header('Content-Type: application/json; charset=utf-8');
  85. echo self::encode($data);
  86. }
  87. /**
  88. * Encode JSON
  89. * @deprecated 12.0.0 Use a AppFramework JSONResponse instead
  90. */
  91. private static function encode($data) {
  92. return json_encode($data, JSON_HEX_TAG);
  93. }
  94. }