JSON.php 6.7 KB

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