json.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Frank Karlitschek <frank@owncloud.org>
  5. * @author Lukas Reschke <lukas@owncloud.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Thomas Tanghus <thomas@tanghus.net>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. /**
  28. * Public interface of ownCloud for apps to use.
  29. * JSON Class
  30. */
  31. // use OCP namespace for all classes that are considered public.
  32. // This means that they should be used by apps instead of the internal ownCloud classes
  33. namespace OCP;
  34. /**
  35. * This class provides convenient functions to generate and send JSON data. Useful for Ajax calls
  36. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  37. */
  38. class JSON {
  39. /**
  40. * Encode and print $data in JSON format
  41. * @param array $data The data to use
  42. * @param bool $setContentType the optional content type
  43. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  44. */
  45. public static function encodedPrint( $data, $setContentType=true ) {
  46. \OC_JSON::encodedPrint($data, $setContentType);
  47. }
  48. /**
  49. * Check if the user is logged in, send json error msg if not.
  50. *
  51. * This method checks if a user is logged in. If not, a json error
  52. * response will be return and the method will exit from execution
  53. * of the script.
  54. * The returned json will be in the format:
  55. *
  56. * {"status":"error","data":{"message":"Authentication error."}}
  57. *
  58. * Add this call to the start of all ajax method files that requires
  59. * an authenticated user.
  60. * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead
  61. */
  62. public static function checkLoggedIn() {
  63. \OC_JSON::checkLoggedIn();
  64. }
  65. /**
  66. * Check an ajax get/post call if the request token is valid.
  67. *
  68. * This method checks for a valid variable 'requesttoken' in $_GET,
  69. * $_POST and $_SERVER. If a valid token is not found, a json error
  70. * response will be return and the method will exit from execution
  71. * of the script.
  72. * The returned json will be in the format:
  73. *
  74. * {"status":"error","data":{"message":"Token expired. Please reload page."}}
  75. *
  76. * Add this call to the start of all ajax method files that creates,
  77. * updates or deletes anything.
  78. * In cases where you e.g. use an ajax call to load a dialog containing
  79. * a submittable form, you will need to add the requesttoken first as a
  80. * parameter to the ajax call, then assign it to the template and finally
  81. * add a hidden input field also named 'requesttoken' containing the value.
  82. * @deprecated 8.1.0 Use annotation based CSRF checks from the AppFramework instead
  83. */
  84. public static function callCheck() {
  85. \OC_JSON::callCheck();
  86. }
  87. /**
  88. * Send json success msg
  89. *
  90. * Return a json success message with optional extra data.
  91. * @see OCP\JSON::error() for the format to use.
  92. *
  93. * @param array $data The data to use
  94. * @return string json formatted string.
  95. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  96. */
  97. public static function success( $data = array() ) {
  98. \OC_JSON::success($data);
  99. }
  100. /**
  101. * Send json error msg
  102. *
  103. * Return a json error message with optional extra data for
  104. * error message or app specific data.
  105. *
  106. * Example use:
  107. *
  108. * $id = [some value]
  109. * OCP\JSON::error(array('data':array('message':'An error happened', 'id': $id)));
  110. *
  111. * Will return the json formatted string:
  112. *
  113. * {"status":"error","data":{"message":"An error happened", "id":[some value]}}
  114. *
  115. * @param array $data The data to use
  116. * @return string json formatted error string.
  117. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  118. */
  119. public static function error( $data = array() ) {
  120. \OC_JSON::error( $data );
  121. }
  122. /**
  123. * Set Content-Type header to jsonrequest
  124. * @param string $type The content type header
  125. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  126. */
  127. public static function setContentTypeHeader( $type='application/json' ) {
  128. \OC_JSON::setContentTypeHeader($type);
  129. }
  130. /**
  131. * Check if the App is enabled and send JSON error message instead
  132. *
  133. * This method checks if a specific app is enabled. If not, a json error
  134. * response will be return and the method will exit from execution
  135. * of the script.
  136. * The returned json will be in the format:
  137. *
  138. * {"status":"error","data":{"message":"Application is not enabled."}}
  139. *
  140. * Add this call to the start of all ajax method files that requires
  141. * a specific app to be enabled.
  142. *
  143. * @param string $app The app to check
  144. * @deprecated 8.1.0 Use the AppFramework instead. It will automatically check if the app is enabled.
  145. */
  146. public static function checkAppEnabled( $app ) {
  147. \OC_JSON::checkAppEnabled($app);
  148. }
  149. /**
  150. * Check if the user is a admin, send json error msg if not
  151. *
  152. * This method checks if the current user has admin rights. If not, a json error
  153. * response will be return and the method will exit from execution
  154. * of the script.
  155. * The returned json will be in the format:
  156. *
  157. * {"status":"error","data":{"message":"Authentication error."}}
  158. *
  159. * Add this call to the start of all ajax method files that requires
  160. * administrative rights.
  161. *
  162. * @deprecated 8.1.0 Use annotation based ACLs from the AppFramework instead
  163. */
  164. public static function checkAdminUser() {
  165. \OC_JSON::checkAdminUser();
  166. }
  167. /**
  168. * Encode JSON
  169. * @param array $data
  170. * @return string
  171. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  172. */
  173. public static function encode($data) {
  174. return \OC_JSON::encode($data);
  175. }
  176. /**
  177. * Check is a given user exists - send json error msg if not
  178. * @param string $user
  179. * @deprecated 8.1.0 Use a AppFramework JSONResponse instead
  180. */
  181. public static function checkUserExists($user) {
  182. \OC_JSON::checkUserExists($user);
  183. }
  184. }