JSON.php 6.4 KB

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