00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 @require_once('config.inc.php');
00013 require_once('log.inc.php');
00014 log_msg('info', '--- json request ---');
00015 require_once('common.inc.php');
00016 require_once('modules.inc.php');
00017 require_once('util.inc.php');
00018
00019
00020
00021 header('Content-Type: application/json; charset=UTF-8');
00022
00023
00024 $args = array();
00025 switch ($_SERVER['REQUEST_METHOD']) {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 case 'POST':
00046 foreach ($_POST as $key=>$val) {
00047 if (get_magic_quotes_gpc()) {
00048 $val = stripslashes($val);
00049 }
00050 $dec = @json_decode($val, true);
00051 if ($dec === NULL) {
00052 $err = response('Error decoding the argument '.quot($key).' => '.var_dump_inl($val), 400);
00053 echo json_encode($err);
00054 log_msg('warn', 'json: '.$err['#data']);
00055 die();
00056 } else {
00057 $args[$key] = $dec;
00058 }
00059 }
00060 break;
00061 default:
00062
00063 $err = response('Only HTTP POST requests supported', 400);
00064 echo json_encode($err);
00065 log_msg('warn', 'json: '.$err['#data']);
00066 die();
00067 }
00068
00069
00070 if (!empty($args['method'])) {
00071 $method = $args['method'];
00072 unset($args['method']);
00073 log_msg('debug', 'json: method is '.quot($method));
00074 log_msg('debug', 'json: arguments are '.var_dump_inl($args));
00075 log_msg('debug', 'json: base url is '.quot(base_url()));
00076 } else {
00077
00078
00079 $err = response('Required argument "method" missing', 400);
00080 echo json_encode($err);
00081 log_msg('warn', 'json: '.$err['#data']);
00082 die();
00083 }
00084
00085 load_modules($method);
00086
00087 if (!($m = get_service($method))) {
00088 $err = response('Unknown method '.quot($method), 400);
00089 echo json_encode($err);
00090 log_msg('warn', 'json: '.$err['#data']);
00091 die();
00092 }
00093
00094
00095 if (isset($m['auth']) && $m['auth']) {
00096 if (!is_auth()) {
00097 prompt_auth(true);
00098 }
00099 }
00100
00101 if (isset($m['cross-origin']) && $m['cross-origin']) {
00102
00103 header('Access-Controll-Allow-Origin: *');
00104 } else {
00105
00106 if (!empty($_SERVER['HTTP_REFERER'])) {
00107 $bu = base_url();
00108 if (substr($_SERVER['HTTP_REFERER'], 0, strlen($bu)) != $bu) {
00109 echo json_encode(response('Cross-origin requests not supported for this method', 400));
00110 log_msg('warn', 'json: possible xsrf detected, referer is '.quot($_SERVER['HTTP_REFERER']).', arguments '.var_dump_inl($args));
00111 die();
00112 }
00113 }
00114 }
00115
00116
00117 $ret = run_service($method, $args);
00118 if (is_array($ret) && isset($ret['#error']) && $ret['#error']) {
00119 log_msg('warn', 'json: service '.$method.' returned error '.quot($ret['#data']));
00120 } elseif (is_array($ret) && isset($ret['#data'])) {
00121 log_msg('debug', 'json: service returned '.var_dump_inl($ret['#data']));
00122 }
00123 echo json_encode($ret);