Skip to content

Commit

Permalink
兼容 PHP 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
fghrsh committed Feb 9, 2019
1 parent bf2147e commit 5b26346
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 20 deletions.
16 changes: 10 additions & 6 deletions add/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@
$modelList = new modelList();
$modelTextures = new modelTextures();

$modelList = $modelList->get_list()['models'];
$modelList = $modelList->get_list();
$modelList = $modelList['models'];

foreach ($modelList as $modelName) {
if (file_exists('../model/'.$modelName.'/textures.cache')) {
if (!is_array($modelName) && file_exists('../model/'.$modelName.'/textures.cache')) {

$textures = $texturesNew = array();
foreach ($modelTextures->get_list($modelName)['textures'] as $v) $textures[] = json_encode($v, JSON_UNESCAPED_SLASHES);
foreach ($modelTextures->get_textures($modelName) as $v) $texturesNew[] = json_encode($v, JSON_UNESCAPED_SLASHES);
$modelTexturesList = $modelTextures->get_list($modelName);
$modelNameTextures = $modelTextures->get_textures($modelName);
if (is_array($modelTexturesList)) foreach ($modelTexturesList['textures'] as $v) $textures[] = str_replace('\/', '/', json_encode($v));
if (is_array($modelNameTextures)) foreach ($modelNameTextures as $v) $texturesNew[] = str_replace('\/', '/', json_encode($v));

if ($textures == NULL) continue; elseif (empty(array_diff($texturesNew, $textures))) {
$texturesDiff = array_diff($texturesNew, $textures);
if (empty($textures)) continue; elseif (empty($texturesDiff)) {
echo '<p>'.$modelName.' / textures.cache / No Update.</p>';
} else {
foreach (array_values(array_unique(array_merge($textures, $texturesNew))) as $v) $texturesMerge[] = json_decode($v, 1);
file_put_contents('../model/'.$modelName.'/textures.cache', json_encode($texturesMerge, JSON_UNESCAPED_SLASHES));
file_put_contents('../model/'.$modelName.'/textures.cache', str_replace('\/', '/', json_encode($texturesMerge)));
echo '<p>'.$modelName.' / textures.cache / Updated.</p>';
}

Expand Down
4 changes: 3 additions & 1 deletion get/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

require '../tools/modelList.php';
require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';

$modelList = new modelList();
$modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();

$id = explode('-', $id);
$modelId = (int)$id[0];
Expand Down Expand Up @@ -49,4 +51,4 @@
}

header("Content-type: application/json");
echo json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
echo $jsonCompatible->json_encode($json);
6 changes: 4 additions & 2 deletions rand/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error');

require '../tools/modelList.php';
require '../tools/jsonCompatible.php';

$modelList = new modelList();
$jsonCompatible = new jsonCompatible();

$modelList = $modelList->get_list();

Expand All @@ -14,8 +16,8 @@
}

header("Content-type: application/json");
echo json_encode(array('model' => array(
echo $jsonCompatible->json_encode(array('model' => array(
'id' => $modelRandId,
'name' => $modelList['models'][$modelRandId-1],
'message' => $modelList['messages'][$modelRandId-1]
)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
)));
8 changes: 5 additions & 3 deletions rand_textures/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@

require '../tools/modelList.php';
require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';

$modelList = new modelList();
$modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();

$id = explode('-', $id);
$modelId = (int)$id[0];
$modelTexturesId = isset($id[1]) ? (int)$id[1] : false;

$modelName = $modelList->id_to_name($modelId);
$modelTexturesList = is_array($modelName) ? ['textures' => $modelName] : $modelTextures->get_list($modelName);
$modelTexturesList = is_array($modelName) ? array('textures' => $modelName) : $modelTextures->get_list($modelName);

if (count($modelTexturesList['textures']) <= 1) {
$modelTexturesNewId = 1;
Expand All @@ -26,8 +28,8 @@
}

header("Content-type: application/json");
echo json_encode(array('textures' => array(
echo $jsonCompatible->json_encode(array('textures' => array(
'id' => $modelTexturesNewId,
'name' => $modelTexturesList['textures'][$modelTexturesNewId-1],
'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName
)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
)));
6 changes: 4 additions & 2 deletions switch/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
isset($_GET['id']) ? $modelId = (int)$_GET['id'] : exit('error');

require '../tools/modelList.php';
require '../tools/jsonCompatible.php';

$modelList = new modelList();
$jsonCompatible = new jsonCompatible();

$modelList = $modelList->get_list();
$modelSwitchId = $modelId + 1;
if (!isset($modelList['models'][$modelSwitchId-1])) $modelSwitchId = 1;

header("Content-type: application/json");
echo json_encode(array('model' => array(
echo $jsonCompatible->json_encode(array('model' => array(
'id' => $modelSwitchId,
'name' => $modelList['models'][$modelSwitchId-1],
'message' => $modelList['messages'][$modelSwitchId-1]
)), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
)));
8 changes: 5 additions & 3 deletions switch_textures/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@

require '../tools/modelList.php';
require '../tools/modelTextures.php';
require '../tools/jsonCompatible.php';

$modelList = new modelList();
$modelTextures = new modelTextures();
$jsonCompatible = new jsonCompatible();

$id = explode('-', $id);
$modelId = (int)$id[0];
$modelTexturesId = isset($id[1]) ? (int)$id[1] : 0;

$modelName = $modelList->id_to_name($modelId);
$modelTexturesList = is_array($modelName) ? ['textures' => $modelName] : $modelTextures->get_list($modelName);
$modelTexturesList = is_array($modelName) ? array('textures' => $modelName) : $modelTextures->get_list($modelName);
$modelTexturesNewId = $modelTexturesId == 0 ? 2 : $modelTexturesId + 1;
if (!isset($modelTexturesList['textures'][$modelTexturesNewId-1])) $modelTexturesNewId = 1;

header("Content-type: application/json");
echo json_encode(array('textures' => array(
echo $jsonCompatible->json_encode(array('textures' => array(
'id' => $modelTexturesNewId,
'name' => $modelTexturesList['textures'][$modelTexturesNewId-1],
'model' => is_array($modelName) ? $modelName[$modelTexturesNewId-1] : $modelName
)), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
)));
49 changes: 49 additions & 0 deletions tools/jsonCompatible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php class jsonCompatible {
public function json_encode($json) {
if (version_compare(PHP_VERSION,'5.4.0','<')) {
$json = json_encode($json);
$json = str_replace('\/', '/', $json);
$json = preg_replace_callback("/\\\u([0-9a-f]{4})/i", array($this,'json_preg_replace'), $json);
return $this->json_pretty_print($json, ' ');
} else return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}

protected function json_preg_replace($matchs) {
return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
}

protected function json_pretty_print($json, $indent = "\t") {
$result = '';
$indentCount = 0;
$inString = false;
$len = strlen($json);
for ($c = 0; $c < $len; $c++) {
$char = $json[$c];
if ($char === '{' || $char === '[') {
if (!$inString) {
$indentCount++;
if ($char === '[' && $json[$c+1] == "]") $result .= $char . PHP_EOL;
elseif ($char === '{' && $json[$c+1] == "}") $result .= $char . PHP_EOL;
else $result .= $char . PHP_EOL . str_repeat($indent, $indentCount);
} else $result .= $char;
} elseif ($char === '}' || $char === ']') {
if (!$inString) {
$indentCount--;
$result .= PHP_EOL . str_repeat($indent, $indentCount) . $char;
} else $result .= $char;
} elseif ($char === ',') {
if (!$inString) $result .= ',' . PHP_EOL . str_repeat($indent, $indentCount);
else $result .= $char;
} elseif ($char === ':') {
if (!$inString) $result .= ': ';
else $result .= $char;
} elseif ($char === '"') {
if (($c > 0 && $json[$c - 1] !== '\\') || ($c > 1 && $json[$c - 2].$json[$c - 1] === '\\\\')) $inString = !$inString;
$result .= $char;
} else {
$result .= $char;
}
}
return $result;
}
}
6 changes: 3 additions & 3 deletions tools/modelTextures.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function get_list($modelName) {
$textures = json_decode(file_get_contents('../model/'.$modelName.'/textures.cache'), true);
} else {
$textures = self::get_textures($modelName);
file_put_contents('../model/'.$modelName.'/textures.cache', json_encode($textures, JSON_UNESCAPED_SLASHES));
if (!empty($textures)) file_put_contents('../model/'.$modelName.'/textures.cache', str_replace('\/', '/', json_encode($textures)));
} return isset($textures) ? array('textures' => $textures) : false;
}

Expand All @@ -24,13 +24,13 @@ function get_textures($modelName) {
$tmp3 = array(); foreach (glob('../model/'.$modelName.'/'.$textures_dir.'/*') as $n => $m)
$tmp3['merge'.$n] = str_replace('../model/'.$modelName.'/', '', $m);
$tmp2 = array_merge_recursive($tmp2, $tmp3); }
foreach ($tmp2 as $v4) $tmp4[$k][] = json_encode($v4, JSON_UNESCAPED_SLASHES);
foreach ($tmp2 as $v4) $tmp4[$k][] = str_replace('\/', '/', json_encode($v4));
$tmp = self::array_exhaustive($tmp, $tmp4[$k]); }
foreach ($tmp as $v) $textures[] = json_decode('['.$v.']', 1); return $textures;
} else {
foreach (glob('../model/'.$modelName.'/textures/*') as $v)
$textures[] = str_replace('../model/'.$modelName.'/', '', $v);
return $textures;
return empty($textures) ? null : $textures;
}
}

Expand Down

0 comments on commit 5b26346

Please sign in to comment.