mirror of
https://github.com/lrsjng/h5ai.git
synced 2025-05-28 14:04:45 -04:00
More PHP refactorings. Fixes text preview.
This commit is contained in:
parent
838a346c29
commit
029872a212
8 changed files with 141 additions and 134 deletions
73
src/_h5ai/server/php/inc/util.php
Normal file
73
src/_h5ai/server/php/inc/util.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
function json_exit($obj) {
|
||||
|
||||
$obj["code"] = 0;
|
||||
echo json_encode($obj);
|
||||
exit;
|
||||
}
|
||||
|
||||
function json_fail($code, $msg = "", $cond = true) {
|
||||
|
||||
if ($cond) {
|
||||
echo json_encode(array("code" => $code, "msg" => $msg));
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
function use_request_params($keys) {
|
||||
|
||||
if (!is_array($keys)) {
|
||||
$keys = func_get_args();
|
||||
}
|
||||
|
||||
$values = array();
|
||||
foreach ($keys as $key) {
|
||||
json_fail(101, "parameter '$key' is missing", !array_key_exists($key, $_REQUEST));
|
||||
$values[] = $_REQUEST[$key];
|
||||
unset($_REQUEST[$key]);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
function delete_tempfile($file) {
|
||||
|
||||
@unlink($file);
|
||||
}
|
||||
|
||||
function starts_with($sequence, $head) {
|
||||
|
||||
return substr($sequence, 0, strlen($head)) === $head;
|
||||
}
|
||||
|
||||
function ends_with($sequence, $tail) {
|
||||
|
||||
return substr($sequence, -strlen($tail)) === $tail;
|
||||
}
|
||||
|
||||
function load_commented_json($file) {
|
||||
|
||||
if (!file_exists($file)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$str = file_get_contents($file);
|
||||
|
||||
// remove comments to get pure json
|
||||
$str = preg_replace("/\/\*.*?\*\/|\/\/.*?(\n|$)/s", "", $str);
|
||||
|
||||
return json_decode($str, true);
|
||||
}
|
||||
|
||||
function merge_config($a, $b) {
|
||||
|
||||
$result = array_merge(array(), $a);
|
||||
|
||||
foreach ($b as $key => $value) {
|
||||
$result[$key] = array_merge($result[$key], $b[$key]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue