json.php 4.7 KB

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