json.php 6.0 KB

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