json.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * Class OC_JSON
  10. * @deprecated Use a AppFramework JSONResponse instead
  11. */
  12. class OC_JSON{
  13. static protected $send_content_type_header = false;
  14. /**
  15. * set Content-Type header to jsonrequest
  16. * @deprecated Use a AppFramework JSONResponse instead
  17. */
  18. public static function setContentTypeHeader($type='application/json') {
  19. if (!self::$send_content_type_header) {
  20. // We send json data
  21. header( 'Content-Type: '.$type . '; charset=utf-8');
  22. self::$send_content_type_header = true;
  23. }
  24. }
  25. /**
  26. * Check if the app is enabled, send json error msg if not
  27. * @param string $app
  28. * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
  29. */
  30. public static function checkAppEnabled($app) {
  31. if( !OC_App::isEnabled($app)) {
  32. $l = \OC::$server->getL10N('lib');
  33. self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' )));
  34. exit();
  35. }
  36. }
  37. /**
  38. * Check if the user is logged in, send json error msg if not
  39. * @deprecated Use annotation based ACLs from the AppFramework instead
  40. */
  41. public static function checkLoggedIn() {
  42. if( !OC_User::isLoggedIn()) {
  43. $l = \OC::$server->getL10N('lib');
  44. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  45. exit();
  46. }
  47. }
  48. /**
  49. * Check an ajax get/post call if the request token is valid, send json error msg if not.
  50. * @deprecated Use annotation based CSRF checks from the AppFramework instead
  51. */
  52. public static function callCheck() {
  53. if( !OC_Util::isCallRegistered()) {
  54. $l = \OC::$server->getL10N('lib');
  55. self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' )));
  56. exit();
  57. }
  58. }
  59. /**
  60. * Check if the user is a admin, send json error msg if not.
  61. * @deprecated Use annotation based ACLs from the AppFramework instead
  62. */
  63. public static function checkAdminUser() {
  64. if( !OC_User::isAdminUser(OC_User::getUser())) {
  65. $l = \OC::$server->getL10N('lib');
  66. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  67. exit();
  68. }
  69. }
  70. /**
  71. * Check is a given user exists - send json error msg if not
  72. * @param string $user
  73. * @deprecated Use a AppFramework JSONResponse instead
  74. */
  75. public static function checkUserExists($user) {
  76. if (!OCP\User::userExists($user)) {
  77. $l = \OC::$server->getL10N('lib');
  78. OCP\JSON::error(array('data' => array('message' => $l->t('Unknown user'), 'error' => 'unknown_user' )));
  79. exit;
  80. }
  81. }
  82. /**
  83. * Check if the user is a subadmin, send json error msg if not
  84. * @deprecated Use annotation based ACLs from the AppFramework instead
  85. */
  86. public static function checkSubAdminUser() {
  87. if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  88. $l = \OC::$server->getL10N('lib');
  89. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  90. exit();
  91. }
  92. }
  93. /**
  94. * Send json error msg
  95. * @deprecated Use a AppFramework JSONResponse instead
  96. */
  97. public static function error($data = array()) {
  98. $data['status'] = 'error';
  99. self::encodedPrint($data);
  100. }
  101. /**
  102. * Send json success msg
  103. * @deprecated Use a AppFramework JSONResponse instead
  104. */
  105. public static function success($data = array()) {
  106. $data['status'] = 'success';
  107. self::encodedPrint($data);
  108. }
  109. /**
  110. * Convert OC_L10N_String to string, for use in json encodings
  111. */
  112. protected static function to_string(&$value) {
  113. if ($value instanceof OC_L10N_String) {
  114. $value = (string)$value;
  115. }
  116. }
  117. /**
  118. * Encode and print $data in json format
  119. * @deprecated Use a AppFramework JSONResponse instead
  120. */
  121. public static function encodedPrint($data, $setContentType=true) {
  122. if($setContentType) {
  123. self::setContentTypeHeader();
  124. }
  125. echo self::encode($data);
  126. }
  127. /**
  128. * Encode JSON
  129. * @deprecated Use a AppFramework JSONResponse instead
  130. */
  131. public static function encode($data) {
  132. if (is_array($data)) {
  133. array_walk_recursive($data, array('OC_JSON', 'to_string'));
  134. }
  135. return json_encode($data);
  136. }
  137. }