You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

json.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Copyright (c) 2011 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class OC_JSON{
  9. static protected $send_content_type_header = false;
  10. /**
  11. * set Content-Type header to jsonrequest
  12. */
  13. public static function setContentTypeHeader($type='application/json') {
  14. if (!self::$send_content_type_header) {
  15. // We send json data
  16. header( 'Content-Type: '.$type );
  17. self::$send_content_type_header = true;
  18. }
  19. }
  20. /**
  21. * Check if the app is enabled, send json error msg if not
  22. */
  23. public static function checkAppEnabled($app) {
  24. if( !OC_App::isEnabled($app)) {
  25. $l = OC_L10N::get('lib');
  26. self::error(array( 'data' => array( 'message' => $l->t('Application is not enabled') )));
  27. exit();
  28. }
  29. }
  30. /**
  31. * Check if the user is logged in, send json error msg if not
  32. */
  33. public static function checkLoggedIn() {
  34. if( !OC_User::isLoggedIn()) {
  35. $l = OC_L10N::get('lib');
  36. self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  37. exit();
  38. }
  39. }
  40. /**
  41. * @brief Check an ajax get/post call if the request token is valid.
  42. * @return json Error msg if not valid.
  43. */
  44. public static function callCheck() {
  45. if( !OC_Util::isCallRegistered()) {
  46. $l = OC_L10N::get('lib');
  47. self::error(array( 'data' => array( 'message' => $l->t('Token expired. Please reload page.') )));
  48. exit();
  49. }
  50. }
  51. /**
  52. * Check if the user is a admin, send json error msg if not
  53. */
  54. public static function checkAdminUser() {
  55. if( !OC_User::isAdminUser(OC_User::getUser())) {
  56. $l = OC_L10N::get('lib');
  57. self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  58. exit();
  59. }
  60. }
  61. /**
  62. * Check if the user is a subadmin, send json error msg if not
  63. */
  64. public static function checkSubAdminUser() {
  65. if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  66. $l = OC_L10N::get('lib');
  67. self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  68. exit();
  69. }
  70. }
  71. /**
  72. * Send json error msg
  73. */
  74. public static function error($data = array()) {
  75. $data['status'] = 'error';
  76. self::encodedPrint($data);
  77. }
  78. /**
  79. * Send json success msg
  80. */
  81. public static function success($data = array()) {
  82. $data['status'] = 'success';
  83. self::encodedPrint($data);
  84. }
  85. /**
  86. * Convert OC_L10N_String to string, for use in json encodings
  87. */
  88. protected static function to_string(&$value) {
  89. if ($value instanceof OC_L10N_String) {
  90. $value = (string)$value;
  91. }
  92. }
  93. /**
  94. * Encode and print $data in json format
  95. */
  96. public static function encodedPrint($data, $setContentType=true) {
  97. // Disable mimesniffing, don't move this to setContentTypeHeader!
  98. header( 'X-Content-Type-Options: nosniff' );
  99. if($setContentType) {
  100. self::setContentTypeHeader();
  101. }
  102. array_walk_recursive($data, array('OC_JSON', 'to_string'));
  103. echo json_encode($data);
  104. }
  105. }