json.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  5. * @author Felix Moeller <mail@felixmoeller.de>
  6. * @author Georg Ehrke <georg@owncloud.com>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Thomas Tanghus <thomas@tanghus.net>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. static protected $send_content_type_header = false;
  36. /**
  37. * set Content-Type header to jsonrequest
  38. * @deprecated Use a AppFramework JSONResponse instead
  39. */
  40. public static function setContentTypeHeader($type='application/json') {
  41. if (!self::$send_content_type_header) {
  42. // We send json data
  43. header( 'Content-Type: '.$type . '; charset=utf-8');
  44. self::$send_content_type_header = true;
  45. }
  46. }
  47. /**
  48. * Check if the app is enabled, send json error msg if not
  49. * @param string $app
  50. * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
  51. */
  52. public static function checkAppEnabled($app) {
  53. if( !OC_App::isEnabled($app)) {
  54. $l = \OC::$server->getL10N('lib');
  55. self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' )));
  56. exit();
  57. }
  58. }
  59. /**
  60. * Check if the user is logged in, send json error msg if not
  61. * @deprecated Use annotation based ACLs from the AppFramework instead
  62. */
  63. public static function checkLoggedIn() {
  64. if( !OC_User::isLoggedIn()) {
  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 an ajax get/post call if the request token is valid, send json error msg if not.
  72. * @deprecated Use annotation based CSRF checks from the AppFramework instead
  73. */
  74. public static function callCheck() {
  75. if( !OC_Util::isCallRegistered()) {
  76. $l = \OC::$server->getL10N('lib');
  77. self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' )));
  78. exit();
  79. }
  80. }
  81. /**
  82. * Check if the user is a admin, send json error msg if not.
  83. * @deprecated Use annotation based ACLs from the AppFramework instead
  84. */
  85. public static function checkAdminUser() {
  86. if( !OC_User::isAdminUser(OC_User::getUser())) {
  87. $l = \OC::$server->getL10N('lib');
  88. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  89. exit();
  90. }
  91. }
  92. /**
  93. * Check is a given user exists - send json error msg if not
  94. * @param string $user
  95. * @deprecated Use a AppFramework JSONResponse instead
  96. */
  97. public static function checkUserExists($user) {
  98. if (!OCP\User::userExists($user)) {
  99. $l = \OC::$server->getL10N('lib');
  100. OCP\JSON::error(array('data' => array('message' => $l->t('Unknown user'), 'error' => 'unknown_user' )));
  101. exit;
  102. }
  103. }
  104. /**
  105. * Check if the user is a subadmin, send json error msg if not
  106. * @deprecated Use annotation based ACLs from the AppFramework instead
  107. */
  108. public static function checkSubAdminUser() {
  109. if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  110. $l = \OC::$server->getL10N('lib');
  111. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  112. exit();
  113. }
  114. }
  115. /**
  116. * Send json error msg
  117. * @deprecated Use a AppFramework JSONResponse instead
  118. */
  119. public static function error($data = array()) {
  120. $data['status'] = 'error';
  121. self::encodedPrint($data);
  122. }
  123. /**
  124. * Send json success msg
  125. * @deprecated Use a AppFramework JSONResponse instead
  126. */
  127. public static function success($data = array()) {
  128. $data['status'] = 'success';
  129. self::encodedPrint($data);
  130. }
  131. /**
  132. * Convert OC_L10N_String to string, for use in json encodings
  133. */
  134. protected static function to_string(&$value) {
  135. if ($value instanceof OC_L10N_String) {
  136. $value = (string)$value;
  137. }
  138. }
  139. /**
  140. * Encode and print $data in json format
  141. * @deprecated Use a AppFramework JSONResponse instead
  142. */
  143. public static function encodedPrint($data, $setContentType=true) {
  144. if($setContentType) {
  145. self::setContentTypeHeader();
  146. }
  147. echo self::encode($data);
  148. }
  149. /**
  150. * Encode JSON
  151. * @deprecated Use a AppFramework JSONResponse instead
  152. */
  153. public static function encode($data) {
  154. if (is_array($data)) {
  155. array_walk_recursive($data, array('OC_JSON', 'to_string'));
  156. }
  157. return json_encode($data);
  158. }
  159. }