266 lines
7.1 KiB
PHP
266 lines
7.1 KiB
PHP
<?php
|
|
|
|
ini_set('display_errors', 0); // Desabilita a exibição de erros na tela
|
|
error_reporting(E_ALL);
|
|
ini_set('log_errors', 1);
|
|
ini_set('error_log', 'erro_log.txt');
|
|
|
|
|
|
|
|
function reduzirArk($tamanho, $pedacos) {
|
|
|
|
$ark_sem_NAAN = "";
|
|
|
|
for($i=1; $i < $tamanho; $i++){ //Começa em 1 porque o primeiro pedaço é o NAAN
|
|
|
|
$ark_sem_NAAN.= "/".$pedacos[$i];
|
|
|
|
}//Fecha for
|
|
|
|
return $ark_sem_NAAN;
|
|
|
|
}
|
|
|
|
function limpar_ark($str){
|
|
|
|
// Lista de caracteres a serem removidos
|
|
$caracteresParaRemover = array('<', '>', ':', ';', '(', ')', '&', '$');
|
|
|
|
// Substitui os caracteres da lista por uma string vazia
|
|
$stringLimpa = str_replace($caracteresParaRemover, '', $str);
|
|
|
|
return $stringLimpa;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
if(isset($_GET["ark"])){
|
|
|
|
$ark = $_GET["ark"];
|
|
|
|
} else {
|
|
|
|
$ark = "";
|
|
|
|
return ['ok'=>false,'destino'=>'','msg'=>'Nenhum ARK foi passado...','qrark'=> false,'ark'=>''];
|
|
|
|
}
|
|
|
|
|
|
//limpar o ARK
|
|
$ark = limpar_ark($ark);
|
|
|
|
|
|
//Eliminando espaços em branco no início e no final do $ark
|
|
$ark = trim($ark);
|
|
|
|
$qrark = (isset($_GET['qrark']) && $_GET['qrark'] == '1') ? true : false;
|
|
|
|
// Verifique se $ark começa com uma barra. Pois, em vez de terem digitado ark:68745/eM485/B6, por exemplo, podem ter digitado ark:/68745/eM485/B6, o que seria passado para o parâmetro ark com uma barra no início
|
|
if (strpos($ark, '/') === 0) {
|
|
|
|
// Remova a barra no início
|
|
$ark= substr($ark, 1);
|
|
}
|
|
|
|
$pedacos = explode("/", $ark);
|
|
|
|
$NAAN = $pedacos[0]; //NAAN é o número de registro da Editora Moan na Ark Alliance
|
|
|
|
$ark_sem_NAAN = reduzirArk(count($pedacos), $pedacos);
|
|
|
|
$ark_completo_sem_NAAN = $ark_sem_NAAN;
|
|
|
|
$ark_completo_sem_NAAN = ltrim($ark_completo_sem_NAAN, '/');
|
|
|
|
if ($ark === '') {
|
|
return ['ok'=>false,'destino'=>'','msg'=>'ARK vazio.','qrark'=>$qrark,'ark'=>''];
|
|
}
|
|
|
|
|
|
//-------------------------- PREFIXOS ARK --------------------------
|
|
$prefixo_editora = "eM";
|
|
|
|
$prefixo_museu_da_matematica = "M";
|
|
|
|
$buscar_ark = null;
|
|
|
|
if (str_starts_with($ark_completo_sem_NAAN, $prefixo_editora)) {
|
|
// Prefixo eM
|
|
require __DIR__ . '/LogicaPrefixo/processarPrefixo_eM.php';
|
|
$buscar_ark = 'processarPrefixo_eM';
|
|
|
|
} elseif (str_starts_with($ark_completo_sem_NAAN, $prefixo_museu_da_matematica)) {
|
|
// Prefixo M
|
|
require __DIR__ . '/LogicaPrefixo/processarPrefixo_M.php';
|
|
$buscar_ark = 'processarPrefixo_M';
|
|
|
|
} else {
|
|
// Nenhum dos dois
|
|
|
|
}
|
|
//-------------------------- FIM PREFIXOS ARK --------------------------
|
|
|
|
if (!is_callable($buscar_ark)) {
|
|
return ['ok'=>false,'destino'=>'','msg'=>'Prefixo ARK desconhecido.','qrark'=>$qrark,'ark'=>$ark_completo_sem_NAAN];
|
|
}
|
|
|
|
|
|
$index = 0;
|
|
|
|
$tamanho_inicial = count($pedacos); //Na verdade o tamanho de $pedacos é fixo. Mais abaixo o valor do tamanho só muda porque precisamos reduzir até encontrar o ark registrado, mas o tamanho real de $pedacos continua o mesmo
|
|
|
|
$identificador_ark = false;
|
|
|
|
$resultado = ['ok'=>false,'destino'=>'','msg'=>'ARK não encontrado.Erro não identificado.','qrark'=>$qrark,'ark'=>$ark_completo_sem_NAAN];
|
|
|
|
do {
|
|
|
|
$resto = "";
|
|
|
|
$tamanho = count($pedacos) - $index;
|
|
|
|
$inicio_do_resto = $tamanho;
|
|
|
|
if($inicio_do_resto < $tamanho_inicial){
|
|
|
|
for($i = $inicio_do_resto; $i < $tamanho_inicial; $i++){
|
|
|
|
$resto.= "/".$pedacos[$i];
|
|
|
|
}//Fecha for
|
|
|
|
|
|
}//Fecha if
|
|
|
|
|
|
$ark_sem_NAAN = reduzirArk($tamanho, $pedacos);
|
|
|
|
if($ark_sem_NAAN == ""){
|
|
break;
|
|
}
|
|
|
|
$len_ark_sem_NAAN = strlen($ark_sem_NAAN);
|
|
|
|
$tem_interrogacao = false;
|
|
|
|
|
|
if($ark_sem_NAAN[$len_ark_sem_NAAN-1] == "?"){// Um ARK com ? é uma pergunta para saber seus metadados e, por isso, em vez da obra em si, temos que retornar os metadados do ARK
|
|
|
|
$tem_interrogacao = true;
|
|
|
|
$ark_sem_NAAN = substr($ark_sem_NAAN, 0, -1);
|
|
|
|
}//Fecha if
|
|
|
|
|
|
$len_ark_sem_NAAN = strlen($ark_sem_NAAN);
|
|
|
|
if($ark_sem_NAAN[$len_ark_sem_NAAN-1] == "?"){// É executado duas vezes porque um ARK, pela especificação, pode conter duas interrogações e nesse caso deveria retornar se o arquivo é temporário ou não. Mas na Editora Moan não implementamos isso.
|
|
|
|
$ark_sem_NAAN = substr($ark_sem_NAAN, 0, -1);
|
|
|
|
}//Fecha if
|
|
|
|
|
|
|
|
$resultado = $buscar_ark($NAAN, $ark_sem_NAAN, $tem_interrogacao, $resto);
|
|
|
|
$identificador_ark = $resultado["ok"];
|
|
|
|
$index++;
|
|
|
|
} while ($tamanho > 1 && !$identificador_ark);
|
|
|
|
$resultado['qrark'] = $qrark;
|
|
$resultado['ark'] = $ark_completo_sem_NAAN;
|
|
|
|
return $resultado;
|
|
|
|
}//Fecha a funcao init
|
|
|
|
$conteudo = init();
|
|
|
|
$ok = $conteudo["ok"];
|
|
|
|
$destino = $conteudo["destino"];
|
|
|
|
$msg = $conteudo["msg"];
|
|
|
|
$ark = $conteudo["ark"];
|
|
|
|
$qrark = $conteudo["qrark"];
|
|
|
|
?>
|
|
|
|
|
|
<!doctype html>
|
|
<html lang="pt-br">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
<title><?= $ok ? "Redirecionando…" : "ARK não encontrado" ?></title>
|
|
|
|
<style>
|
|
html,body{margin:0;width:100%;height:100%;display:flex;align-items:center;justify-content:center;font-family:system-ui,sans-serif;background:#fff}
|
|
.spinner{width:52px;height:52px;border:5px solid #ddd;border-top-color:#555;border-radius:50%;animation:spin 1s linear infinite}
|
|
@keyframes spin{to{transform:rotate(360deg)}}
|
|
.msg{max-width:420px;text-align:center;padding:24px}
|
|
</style>
|
|
|
|
<?php
|
|
// Injeta o snippet do plausible (arquivo ignorado no git)
|
|
@include __DIR__ . '/analytics/plausible.php';
|
|
?>
|
|
|
|
|
|
|
|
<?php if ($ok): ?>
|
|
<meta http-equiv="refresh" content="0;url=<?= htmlspecialchars($destino, ENT_QUOTES, 'UTF-8') ?>">
|
|
<?php endif; ?>
|
|
</head>
|
|
<body>
|
|
<div class="spinner" id="spinner" aria-label="Carregando"></div>
|
|
|
|
<div class="msg" id="msgBox" style="display:none">
|
|
|
|
<h1 id="msgTitle">Redirecionando...</h1>
|
|
<p id="msgText"></p>
|
|
|
|
</div>
|
|
|
|
<script>
|
|
// Passando PHP -> JS (seguro)
|
|
const ok = <?= json_encode($ok) ?>;
|
|
const destino = <?= json_encode($destino) ?>;
|
|
const msg = <?= json_encode($msg) ?>;
|
|
const ark = <?= json_encode($ark) ?>;
|
|
const qrark = <?= json_encode($qrark) ?>;
|
|
|
|
const spinner = document.getElementById('spinner');
|
|
const msgBox = document.getElementById('msgBox');
|
|
const msgTitle = document.getElementById('msgTitle');
|
|
const msgText = document.getElementById('msgText');
|
|
|
|
if (ok) {
|
|
if (typeof plausible === "function") {
|
|
plausible("ARK Resolve", { props: { ark, qrark } });
|
|
}
|
|
setTimeout(() => location.replace(destino), 120);
|
|
} else {
|
|
// terminou e deu ruim: troca spinner por mensagem
|
|
spinner.style.display = 'none';
|
|
msgBox.style.display = 'block';
|
|
msgTitle.textContent = 'Erro';
|
|
msgText.textContent = msg;
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|