json.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. self::checkLoggedIn();
  56. if( !OC_Group::inGroup( OC_User::getUser(), 'admin' )){
  57. $l = OC_L10N::get('lib');
  58. self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  59. exit();
  60. }
  61. }
  62. /**
  63. * Check if the user is a subadmin, send json error msg if not
  64. */
  65. public static function checkSubAdminUser(){
  66. self::checkLoggedIn();
  67. if(!OC_Group::inGroup(OC_User::getUser(),'admin') && !OC_SubAdmin::isSubAdmin(OC_User::getUser())){
  68. $l = OC_L10N::get('lib');
  69. self::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  70. exit();
  71. }
  72. }
  73. /**
  74. * Send json error msg
  75. */
  76. public static function error($data = array()){
  77. $data['status'] = 'error';
  78. self::encodedPrint($data);
  79. }
  80. /**
  81. * Send json success msg
  82. */
  83. public static function success($data = array()){
  84. $data['status'] = 'success';
  85. self::encodedPrint($data);
  86. }
  87. /**
  88. * Convert OC_L10N_String to string, for use in json encodings
  89. */
  90. protected static function to_string(&$value){
  91. if ($value instanceof OC_L10N_String) {
  92. $value = (string)$value;
  93. }
  94. }
  95. /**
  96. * Encode and print $data in json format
  97. */
  98. public static function encodedPrint($data,$setContentType=true){
  99. // Disable mimesniffing, don't move this to setContentTypeHeader!
  100. header( 'X-Content-Type-Options: nosniff' );
  101. if($setContentType){
  102. self::setContentTypeHeader();
  103. }
  104. array_walk_recursive($data, array('OC_JSON', 'to_string'));
  105. echo json_encode($data);
  106. }
  107. }