7 private static function rRestructuringFilesArray(&$arrayForFill, $currentKey, $currentMixedValue, $fileDescriptionParam){
8 if( is_array($currentMixedValue) ){
9 foreach( $currentMixedValue
as $nameKey => $mixedValue ){
10 self::rRestructuringFilesArray($arrayForFill[$currentKey],
13 $fileDescriptionParam);
16 $arrayForFill[$currentKey][$fileDescriptionParam] = $currentMixedValue;
21 private static function determineMimeType(&$file){
22 if( function_exists(
'mime_content_type') ){
23 if( isset($file[
'tmp_name']) && is_string($file[
'tmp_name']) ){
24 if( $file[
'type'] ==
'application/octet-stream' ){
25 $mime = mime_content_type($file[
'tmp_name']);
27 $file[
'type'] = $mime;
31 else if( is_array($file) ){
32 foreach( $file
as &$entry ){
33 self::determineMimeType($entry);
45 if( is_null($options) ){
49 if( !isset($options[
'origin']) ){
50 $options[
'origin'] = $_SERVER[
'HTTP_ORIGIN'];
53 if( !isset($options[
'methods']) ){
54 $options[
'methods'] =
'POST, GET';
57 if( !isset($options[
'headers']) ){
58 $options[
'headers'] = array();
61 header(
'Access-Control-Allow-Origin: ' . $options[
'origin']);
62 header(
'Access-Control-Allow-Methods: ' . $options[
'methods']);
63 header(
'Access-Control-Allow-Headers: ' . implode(
', ', array_merge($options[
'headers'], array(
'X-Requested-With',
'Content-Range',
'Content-Disposition'))));
65 if( !isset($options[
'cookie']) || $options[
'cookie'] ){
66 header(
'Access-Control-Allow-Credentials: true');
78 foreach( $_SERVER
as $key => $value ){
79 if( substr($key, 0, 5) ==
'HTTP_' ){
80 $header = str_replace(
' ',
'-', ucwords(str_replace(
'_',
' ', strtolower(substr($key, 5)))));
81 $headers[$header] = $value;
97 foreach( $_FILES
as $firstNameKey => $arFileDescriptions ){
98 foreach( $arFileDescriptions
as $fileDescriptionParam => $mixedValue ){
99 self::rRestructuringFilesArray($files, $firstNameKey, $_FILES[$firstNameKey][$fileDescriptionParam], $fileDescriptionParam);
103 self::determineMimeType($files);
115 $body = $res[
'body'];
116 $json = is_array($body) ? json_encode($body) : $body;
118 $httpStatus = isset($res[
'status']) ? $res[
'status'] :
self::OK;
119 $httpStatusText = addslashes(isset($res[
'statusText']) ? $res[
'statusText'] :
'OK');
120 $httpHeaders = isset($res[
'headers']) ? $res[
'headers'] : array();
123 header(
"HTTP/1.1 $httpStatus $httpStatusText");
124 $httpHeaders[
'Content-Type'] =
'application/json';
125 foreach( $httpHeaders
as $header => $value ){
126 header(
"$header: $value");
131 $json = addslashes($json);
135 (
function (ctx, jsonp){
137 var status = $httpStatus, statusText =
"$httpStatusText", response =
"$json";
139 ctx[jsonp](status, statusText, response);
141 var data =
"{\"id\":\"$jsonp\",\"status\":"+status+
",\"statusText\":\""+statusText+
"\",\"response\":\""+response.replace(/\
"/g, '\\\\\"')+"\
"}";
143 ctx.postMessage(data,
document.referrer);
146 })(window.parent,
'$jsonp');