Cheetah
FileAPI.class.php
Go to the documentation of this file.
1 <?php
2  class FileAPI {
3  const OK = 200;
4  const ERROR = 500;
5 
6 
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],
11  $nameKey,
12  $mixedValue,
13  $fileDescriptionParam);
14  }
15  } else {
16  $arrayForFill[$currentKey][$fileDescriptionParam] = $currentMixedValue;
17  }
18  }
19 
20 
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']);
26  if( !empty($mime) ){
27  $file['type'] = $mime;
28  }
29  }
30  }
31  else if( is_array($file) ){
32  foreach( $file as &$entry ){
33  self::determineMimeType($entry);
34  }
35  }
36  }
37  }
38 
39 
44  public static function enableCORS($options = null){
45  if( is_null($options) ){
46  $options = array();
47  }
48 
49  if( !isset($options['origin']) ){
50  $options['origin'] = $_SERVER['HTTP_ORIGIN'];
51  }
52 
53  if( !isset($options['methods']) ){
54  $options['methods'] = 'POST, GET';
55  }
56 
57  if( !isset($options['headers']) ){
58  $options['headers'] = array();
59  }
60 
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'))));
64 
65  if( !isset($options['cookie']) || $options['cookie'] ){
66  header('Access-Control-Allow-Credentials: true');
67  }
68  }
69 
70 
75  public static function getRequestHeaders(){
76  $headers = array();
77 
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;
82  }
83  }
84 
85  return $headers;
86  }
87 
88 
93  public static function getFiles(){
94  $files = array();
95 
96  // http://www.php.net/manual/ru/reserved.variables.files.php#106558
97  foreach( $_FILES as $firstNameKey => $arFileDescriptions ){
98  foreach( $arFileDescriptions as $fileDescriptionParam => $mixedValue ){
99  self::rRestructuringFilesArray($files, $firstNameKey, $_FILES[$firstNameKey][$fileDescriptionParam], $fileDescriptionParam);
100  }
101  }
102 
103  self::determineMimeType($files);
104 
105  return $files;
106  }
107 
108 
114  public static function makeResponse(array $res, $jsonp = null){
115  $body = $res['body'];
116  $json = is_array($body) ? json_encode($body) : $body;
117 
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();
121 
122  if( empty($jsonp) ){
123  header("HTTP/1.1 $httpStatus $httpStatusText");
124  $httpHeaders['Content-Type'] = 'application/json';
125  foreach( $httpHeaders as $header => $value ){
126  header("$header: $value");
127  }
128  echo $json;
129  }
130  else {
131  $json = addslashes($json);
132 
133  echo <<<END
134  <script>
135  (function (ctx, jsonp){
136  'use strict';
137  var status = $httpStatus, statusText = "$httpStatusText", response = "$json";
138  try {
139  ctx[jsonp](status, statusText, response);
140  } catch (e){
141  var data = "{\"id\":\"$jsonp\",\"status\":"+status+",\"statusText\":\""+statusText+"\",\"response\":\""+response.replace(/\"/g, '\\\\\"')+"\"}";
142  try {
143  ctx.postMessage(data, document.referrer);
144  } catch (e){}
145  }
146  })(window.parent, '$jsonp');
147  </script>
148 END;
149  }
150  }
151 
152  }
header
</code > Be careful enabling this directive if you have a redirector script that does not use the< code > Location</code > HTTP header
Definition: URI.MungeResources.txt:10
FileAPI\getFiles
static getFiles()
Definition: FileAPI.class.php:93
document
Output SortAttr HTML Purifier will sort attributes by name before writing them back to the document
Definition: Output.SortAttr.txt:8
FileAPI\OK
const OK
Definition: FileAPI.class.php:3
php
FileAPI\makeResponse
static makeResponse(array $res, $jsonp=null)
Definition: FileAPI.class.php:114
FileAPI\enableCORS
static enableCORS($options=null)
Definition: FileAPI.class.php:44
FileAPI
Definition: FileAPI.class.php:2
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
FileAPI\ERROR
const ERROR
Definition: FileAPI.class.php:4
FileAPI\getRequestHeaders
static getRequestHeaders()
Definition: FileAPI.class.php:75