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.

139 lines
4.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. /**
  31. * Class OC_JSON
  32. * @deprecated Use a AppFramework JSONResponse instead
  33. */
  34. class OC_JSON {
  35. /**
  36. * Check if the app is enabled, send json error msg if not
  37. * @param string $app
  38. * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
  39. * @suppress PhanDeprecatedFunction
  40. */
  41. public static function checkAppEnabled($app) {
  42. if (!\OC::$server->getAppManager()->isEnabledForUser($app)) {
  43. $l = \OC::$server->getL10N('lib');
  44. self::error([ 'data' => [ 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' ]]);
  45. exit();
  46. }
  47. }
  48. /**
  49. * Check if the user is logged in, send json error msg if not
  50. * @deprecated Use annotation based ACLs from the AppFramework instead
  51. * @suppress PhanDeprecatedFunction
  52. */
  53. public static function checkLoggedIn() {
  54. $twoFactorAuthManger = \OC::$server->getTwoFactorAuthManager();
  55. if (!\OC::$server->getUserSession()->isLoggedIn()
  56. || $twoFactorAuthManger->needsSecondFactor(\OC::$server->getUserSession()->getUser())) {
  57. $l = \OC::$server->getL10N('lib');
  58. http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
  59. self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
  60. exit();
  61. }
  62. }
  63. /**
  64. * Check an ajax get/post call if the request token is valid, send json error msg if not.
  65. * @deprecated Use annotation based CSRF checks from the AppFramework instead
  66. * @suppress PhanDeprecatedFunction
  67. */
  68. public static function callCheck() {
  69. if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
  70. header('Location: '.\OC::$WEBROOT);
  71. exit();
  72. }
  73. if (!\OC::$server->getRequest()->passesCSRFCheck()) {
  74. $l = \OC::$server->getL10N('lib');
  75. self::error([ 'data' => [ 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ]]);
  76. exit();
  77. }
  78. }
  79. /**
  80. * Check if the user is a admin, send json error msg if not.
  81. * @deprecated Use annotation based ACLs from the AppFramework instead
  82. * @suppress PhanDeprecatedFunction
  83. */
  84. public static function checkAdminUser() {
  85. if (!OC_User::isAdminUser(OC_User::getUser())) {
  86. $l = \OC::$server->getL10N('lib');
  87. self::error([ 'data' => [ 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' ]]);
  88. exit();
  89. }
  90. }
  91. /**
  92. * Send json error msg
  93. * @deprecated Use a AppFramework JSONResponse instead
  94. * @suppress PhanDeprecatedFunction
  95. */
  96. public static function error($data = []) {
  97. $data['status'] = 'error';
  98. header('Content-Type: application/json; charset=utf-8');
  99. echo self::encode($data);
  100. }
  101. /**
  102. * Send json success msg
  103. * @deprecated Use a AppFramework JSONResponse instead
  104. * @suppress PhanDeprecatedFunction
  105. */
  106. public static function success($data = []) {
  107. $data['status'] = 'success';
  108. header('Content-Type: application/json; charset=utf-8');
  109. echo self::encode($data);
  110. }
  111. /**
  112. * Convert OC_L10N_String to string, for use in json encodings
  113. */
  114. protected static function to_string(&$value) {
  115. if ($value instanceof \OC\L10N\L10NString) {
  116. $value = (string)$value;
  117. }
  118. }
  119. /**
  120. * Encode JSON
  121. * @deprecated Use a AppFramework JSONResponse instead
  122. */
  123. public static function encode($data) {
  124. if (is_array($data)) {
  125. array_walk_recursive($data, ['OC_JSON', 'to_string']);
  126. }
  127. return json_encode($data, JSON_HEX_TAG);
  128. }
  129. }