json.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. */
  27. // use OCP namespace for all classes that are considered public.
  28. // This means that they should be used by apps instead of the internal ownCloud classes
  29. namespace OCP;
  30. /**
  31. * This class provides convinient functions to generate and send JSON data. Usefull for Ajax calls
  32. */
  33. class JSON {
  34. /**
  35. * Encode and print $data in JSON format
  36. * @param array $data The data to use
  37. * @param string $setContentType the optional content type
  38. * @return string json formatted string.
  39. */
  40. public static function encodedPrint( $data, $setContentType=true ) {
  41. return(\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. *
  56. * @return string json formatted error string if not authenticated.
  57. */
  58. public static function checkLoggedIn() {
  59. return(\OC_JSON::checkLoggedIn());
  60. }
  61. /**
  62. * Check an ajax get/post call if the request token is valid.
  63. *
  64. * This method checks for a valid variable 'requesttoken' in $_GET,
  65. * $_POST and $_SERVER. If a valid token is not found, a json error
  66. * response will be return and the method will exit from execution
  67. * of the script.
  68. * The returned json will be in the format:
  69. *
  70. * {"status":"error","data":{"message":"Token expired. Please reload page."}}
  71. *
  72. * Add this call to the start of all ajax method files that creates,
  73. * updates or deletes anything.
  74. * In cases where you e.g. use an ajax call to load a dialog containing
  75. * a submittable form, you will need to add the requesttoken first as a
  76. * parameter to the ajax call, then assign it to the template and finally
  77. * add a hidden input field also named 'requesttoken' containing the value.
  78. *
  79. * @return \json|null json formatted error string if not valid.
  80. */
  81. public static function callCheck() {
  82. return(\OC_JSON::callCheck());
  83. }
  84. /**
  85. * Send json success msg
  86. *
  87. * Return a json success message with optional extra data.
  88. * @see OCP\JSON::error() for the format to use.
  89. *
  90. * @param array $data The data to use
  91. * @return string json formatted string.
  92. */
  93. public static function success( $data = array() ) {
  94. return(\OC_JSON::success( $data ));
  95. }
  96. /**
  97. * Send json error msg
  98. *
  99. * Return a json error message with optional extra data for
  100. * error message or app specific data.
  101. *
  102. * Example use:
  103. *
  104. * $id = [some value]
  105. * OCP\JSON::error(array('data':array('message':'An error happened', 'id': $id)));
  106. *
  107. * Will return the json formatted string:
  108. *
  109. * {"status":"error","data":{"message":"An error happened", "id":[some value]}}
  110. *
  111. * @param array $data The data to use
  112. * @return string json formatted error string.
  113. */
  114. public static function error( $data = array() ) {
  115. return(\OC_JSON::error( $data ));
  116. }
  117. /**
  118. * Set Content-Type header to jsonrequest
  119. * @param array $type The contwnt type header
  120. * @return string json formatted string.
  121. */
  122. public static function setContentTypeHeader( $type='application/json' ) {
  123. return(\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. * @return string json formatted string if not enabled.
  140. */
  141. public static function checkAppEnabled( $app ) {
  142. return(\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. * @return string json formatted string if not admin user.
  158. */
  159. public static function checkAdminUser() {
  160. \OC_JSON::checkAdminUser();
  161. }
  162. /**
  163. * Encode JSON
  164. * @param array $data
  165. */
  166. public static function encode($data) {
  167. return(\OC_JSON::encode($data));
  168. }
  169. /**
  170. * Check is a given user exists - send json error msg if not
  171. * @param string $user
  172. */
  173. public static function checkUserExists($user) {
  174. \OC_JSON::checkUserExists($user);
  175. }
  176. }