-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JSON fixed to catch error and fixed mix content
- Loading branch information
Showing
1 changed file
with
57 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,72 @@ | ||
<?php | ||
|
||
namespace FileManager; | ||
|
||
class Response { | ||
/** | ||
* Sends JSON response | ||
* | ||
* @param $data | ||
* @param int $code | ||
* | ||
* @return mixed | ||
*/ | ||
* Sends JSON response | ||
* | ||
* @param $data | ||
* @param int $code | ||
* | ||
* @return mixed | ||
*/ | ||
public static function JSON($data, $code=200) | ||
{ | ||
header('Content-Type: application/json'); | ||
http_response_code($code); | ||
return print_r(json_encode($data, 128)); | ||
} | ||
// Clear json_last_error() | ||
json_encode(null); | ||
|
||
$data = self::convert_from_latin1_to_utf8_recursively($data); | ||
|
||
$json = json_encode($data, 128); | ||
if (JSON_ERROR_NONE !== json_last_error()) { | ||
$data = new \Exception(sprintf( | ||
'Unable to encode data to JSON in %s: %s', | ||
__CLASS__, | ||
json_last_error_msg() | ||
)); | ||
http_response_code('503'); | ||
$json = json_encode(['message' => $data->getMessage()]); | ||
}else{ | ||
http_response_code($code); | ||
} | ||
|
||
header('Content-Type: application/json'); | ||
return print_r($json); | ||
} | ||
/** | ||
* Sends raw response | ||
* | ||
* @param $mime | ||
* @param $content | ||
* @param int $response | ||
* | ||
* @return bool | ||
*/ | ||
* Sends raw response | ||
* | ||
* @param $mime | ||
* @param $content | ||
* @param int $response | ||
* | ||
* @return bool | ||
*/ | ||
public static function RAW($mime, $content, $response=200) | ||
{ | ||
header('Content-Type: ' . $mime); | ||
http_response_code($response); | ||
print $content; | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Encode array from latin1 to utf8 recursively | ||
* @param $dat | ||
* @return array|string | ||
*/ | ||
public static function convert_from_latin1_to_utf8_recursively($dat) | ||
{ | ||
if (is_string($dat)) { | ||
return utf8_encode($dat); | ||
} elseif (is_array($dat)) { | ||
$ret = []; | ||
foreach ($dat as $i => $d) $ret[ $i ] = self::convert_from_latin1_to_utf8_recursively($d); | ||
return $ret; | ||
} elseif (is_object($dat)) { | ||
foreach ($dat as $i => $d) $dat->$i = self::convert_from_latin1_to_utf8_recursively($d); | ||
return $dat; | ||
} else { | ||
return $dat; | ||
} | ||
} | ||
} |