json.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 Felix Moeller <mail@felixmoeller.de>
  8. * @author Georg Ehrke <georg@owncloud.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Thomas Tanghus <thomas@tanghus.net>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. /**
  32. * Class OC_JSON
  33. * @deprecated Use a AppFramework JSONResponse instead
  34. */
  35. class OC_JSON{
  36. static protected $send_content_type_header = false;
  37. /**
  38. * set Content-Type header to jsonrequest
  39. * @deprecated Use a AppFramework JSONResponse instead
  40. */
  41. public static function setContentTypeHeader($type='application/json') {
  42. if (!self::$send_content_type_header) {
  43. // We send json data
  44. header( 'Content-Type: '.$type . '; charset=utf-8');
  45. self::$send_content_type_header = true;
  46. }
  47. }
  48. /**
  49. * Check if the app is enabled, send json error msg if not
  50. * @param string $app
  51. * @deprecated Use the AppFramework instead. It will automatically check if the app is enabled.
  52. */
  53. public static function checkAppEnabled($app) {
  54. if( !OC_App::isEnabled($app)) {
  55. $l = \OC::$server->getL10N('lib');
  56. self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled'), 'error' => 'application_not_enabled' )));
  57. exit();
  58. }
  59. }
  60. /**
  61. * Check if the user is logged in, send json error msg if not
  62. * @deprecated Use annotation based ACLs from the AppFramework instead
  63. */
  64. public static function checkLoggedIn() {
  65. if( !OC_User::isLoggedIn()) {
  66. $l = \OC::$server->getL10N('lib');
  67. http_response_code(\OCP\AppFramework\Http::STATUS_UNAUTHORIZED);
  68. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  69. exit();
  70. }
  71. }
  72. /**
  73. * Check an ajax get/post call if the request token is valid, send json error msg if not.
  74. * @deprecated Use annotation based CSRF checks from the AppFramework instead
  75. */
  76. public static function callCheck() {
  77. if(!\OC::$server->getRequest()->passesStrictCookieCheck()) {
  78. header('Location: '.\OC::$WEBROOT);
  79. exit();
  80. }
  81. if( !(\OC::$server->getRequest()->passesCSRFCheck())) {
  82. $l = \OC::$server->getL10N('lib');
  83. self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' )));
  84. exit();
  85. }
  86. }
  87. /**
  88. * Check if the user is a admin, send json error msg if not.
  89. * @deprecated Use annotation based ACLs from the AppFramework instead
  90. */
  91. public static function checkAdminUser() {
  92. if( !OC_User::isAdminUser(OC_User::getUser())) {
  93. $l = \OC::$server->getL10N('lib');
  94. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  95. exit();
  96. }
  97. }
  98. /**
  99. * Check is a given user exists - send json error msg if not
  100. * @param string $user
  101. * @deprecated Use a AppFramework JSONResponse instead
  102. */
  103. public static function checkUserExists($user) {
  104. if (!OCP\User::userExists($user)) {
  105. $l = \OC::$server->getL10N('lib');
  106. OCP\JSON::error(array('data' => array('message' => $l->t('Unknown user'), 'error' => 'unknown_user' )));
  107. exit;
  108. }
  109. }
  110. /**
  111. * Check if the user is a subadmin, send json error msg if not
  112. * @deprecated Use annotation based ACLs from the AppFramework instead
  113. */
  114. public static function checkSubAdminUser() {
  115. $userObject = \OC::$server->getUserSession()->getUser();
  116. $isSubAdmin = false;
  117. if($userObject !== null) {
  118. $isSubAdmin = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdmin($userObject);
  119. }
  120. if(!$isSubAdmin) {
  121. $l = \OC::$server->getL10N('lib');
  122. self::error(array( 'data' => array( 'message' => $l->t('Authentication error'), 'error' => 'authentication_error' )));
  123. exit();
  124. }
  125. }
  126. /**
  127. * Send json error msg
  128. * @deprecated Use a AppFramework JSONResponse instead
  129. */
  130. public static function error($data = array()) {
  131. $data['status'] = 'error';
  132. self::encodedPrint($data);
  133. }
  134. /**
  135. * Send json success msg
  136. * @deprecated Use a AppFramework JSONResponse instead
  137. */
  138. public static function success($data = array()) {
  139. $data['status'] = 'success';
  140. self::encodedPrint($data);
  141. }
  142. /**
  143. * Convert OC_L10N_String to string, for use in json encodings
  144. */
  145. protected static function to_string(&$value) {
  146. if ($value instanceof OC_L10N_String) {
  147. $value = (string)$value;
  148. }
  149. }
  150. /**
  151. * Encode and print $data in json format
  152. * @deprecated Use a AppFramework JSONResponse instead
  153. */
  154. public static function encodedPrint($data, $setContentType=true) {
  155. if($setContentType) {
  156. self::setContentTypeHeader();
  157. }
  158. echo self::encode($data);
  159. }
  160. /**
  161. * Encode JSON
  162. * @deprecated Use a AppFramework JSONResponse instead
  163. */
  164. public static function encode($data) {
  165. if (is_array($data)) {
  166. array_walk_recursive($data, array('OC_JSON', 'to_string'));
  167. }
  168. return json_encode($data, JSON_HEX_TAG);
  169. }
  170. }