311 lines
8.2 KiB
PHP
311 lines
8.2 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_full'=>'', 'tem_interrogacao'=>false];
|
|
|
|
}
|
|
|
|
|
|
//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);
|
|
}
|
|
|
|
$tem_interrogacao = false;
|
|
|
|
// remove até 2 interrogações no final e marca flag. Ark para consultar metadados se coloca uma interrogacao no final. Duas interrogacoes verifica a persistencia, o tempo de existencia do ark, mas aqui na Editora Moan nao implementamos isso
|
|
$ark = rtrim($ark);
|
|
for ($k = 0; $k < 2; $k++) {
|
|
if (substr($ark, -1) === '?') {
|
|
$tem_interrogacao = true;
|
|
$ark = substr($ark, 0, -1);
|
|
}
|
|
}
|
|
|
|
$pedacos = explode("/", $ark);
|
|
|
|
// ARK completo “sem o prefixo ark:” e sem barra inicial, fixo:
|
|
$ark_full = implode("/", array_filter($pedacos, fn($p) => $p !== ''));
|
|
|
|
$NAAN = $pedacos[0]; //NAAN é o número de registro da Editora Moan na Ark Alliance
|
|
|
|
$ark_sem_NAAN = reduzirArk(count($pedacos), $pedacos);
|
|
|
|
// sem NAAN
|
|
$ark_base = implode("/", array_slice($pedacos, 1));
|
|
|
|
|
|
|
|
if ($ark === '') {
|
|
return ['ok'=>false,'destino'=>'','msg'=>'ARK vazio.','qrark'=>$qrark,'ark_full'=>'', 'tem_interrogacao'=>false];
|
|
}
|
|
|
|
|
|
|
|
|
|
//-------------------------- PREFIXOS ARK --------------------------
|
|
$prefixo_editora = "eM";
|
|
|
|
$prefixo_museu_da_matematica = "M";
|
|
|
|
$buscar_ark = null;
|
|
|
|
if (str_starts_with($ark_base, $prefixo_editora)) {
|
|
// Prefixo eM
|
|
require __DIR__ . '/LogicaPrefixos/processarPrefixo_eM.php';
|
|
$buscar_ark = 'processarPrefixo_eM';
|
|
|
|
} elseif (str_starts_with($ark_base, $prefixo_museu_da_matematica)) {
|
|
// Prefixo M
|
|
require __DIR__ . '/LogicaPrefixos/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_full'=>$ark_full, 'tem_interrogacao'=>$tem_interrogacao];
|
|
}
|
|
|
|
|
|
$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_full, 'tem_interrogacao'=>$tem_interrogacao];
|
|
|
|
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
$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_full'] = $ark_full; // <-- use este no JS/analytics
|
|
$resultado['tem_interrogacao'] = $tem_interrogacao;
|
|
|
|
|
|
return $resultado;
|
|
|
|
}//Fecha a funcao init
|
|
|
|
$conteudo = init();
|
|
|
|
$ok = $conteudo["ok"];
|
|
|
|
$destino = $conteudo["destino"];
|
|
|
|
$msg = $conteudo["msg"];
|
|
|
|
$arkFull = $conteudo['ark_full'] ?? '';
|
|
|
|
$qrark = $conteudo["qrark"];
|
|
|
|
$tem_interrogacao = $conteudo['tem_interrogacao'];
|
|
|
|
function slug_titulo($s) {
|
|
$s = trim((string)$s);
|
|
if ($s === '') return '';
|
|
|
|
// remove acentos
|
|
$s = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
|
|
$s = strtolower($s);
|
|
|
|
// troca tudo que não é letra/número por hífen
|
|
$s = preg_replace('/[^a-z0-9]+/', '-', $s);
|
|
$s = trim($s, '-');
|
|
|
|
// limita tamanho (pra não virar um trem sem freio)
|
|
return substr($s, 0, 80);
|
|
}
|
|
|
|
$tituloSlug = slug_titulo($conteudo['titulo'] ?? '');
|
|
|
|
|
|
|
|
?>
|
|
|
|
|
|
<!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: #FF5F6D; /* fallback for old browsers */
|
|
background: -webkit-linear-gradient(to right, #FFC371, #FF5F6D); /* Chrome 10-25, Safari 5.1-6 */
|
|
background: linear-gradient(to right, #FFC371, #FF5F6D); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
|
|
}
|
|
.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';
|
|
?>
|
|
|
|
</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>
|
|
const ok = <?= json_encode($ok) ?>;
|
|
const destino = <?= json_encode($destino) ?>;
|
|
const msg = <?= json_encode($msg) ?>;
|
|
|
|
const arkFull = <?= json_encode($arkFull) ?>; // "68745/eM8Rf/gJ.ht"
|
|
const qrark = <?= json_encode($qrark) ?>; // boolean
|
|
const consultaMetadados = <?= json_encode($tem_interrogacao) ?>; // boolean
|
|
const tituloSlug = <?= json_encode($tituloSlug) ?>; // "o-titulo-do-livro"
|
|
|
|
const spinner = document.getElementById('spinner');
|
|
const msgBox = document.getElementById('msgBox');
|
|
const msgTitle = document.getElementById('msgTitle');
|
|
const msgText = document.getElementById('msgText');
|
|
|
|
function pathSafe(a) {
|
|
return String(a)
|
|
.split('/')
|
|
.filter(Boolean)
|
|
.map(encodeURIComponent)
|
|
.join('/');
|
|
}
|
|
|
|
const go = () => location.replace(destino);
|
|
|
|
if (ok) {
|
|
const parts = [];
|
|
if (qrark) parts.push('qrcodeLivro');
|
|
if (consultaMetadados) parts.push('meta');
|
|
if (tituloSlug) parts.push(tituloSlug);
|
|
|
|
const suffix = parts.length ? '_' + parts.join('_') : '';
|
|
const virtualUrl = location.origin + '/' + pathSafe(arkFull) + suffix;
|
|
|
|
if (typeof plausible === "function") {
|
|
plausible("pageview", {
|
|
u: virtualUrl,
|
|
callback: () => go()
|
|
});
|
|
setTimeout(go, 1500); // fallback
|
|
} else {
|
|
go();
|
|
}
|
|
} else {
|
|
spinner.style.display = 'none';
|
|
msgBox.style.display = 'block';
|
|
msgTitle.textContent = 'Erro';
|
|
msgText.textContent = msg;
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|