<?php
// +-------------------------------------------------+
// © 2002-2004 PMB Services / www.sigb.net pmb@sigb.net et contributeurs (voir www.sigb.net)
// +-------------------------------------------------+
// $Id: ArkAPIController.php,v 1.2.10.3 2026/06/19 13:01:46 qvarin Exp $
namespace Pmb\Ark\Controller;

use Pmb\Ark\Models\NaanModel;
use Pmb\Ark\Models\ArkModel;
use Pmb\Common\Helper\Response;

class ArkAPIController
{

    /**
     *
     * @var int
     */
    protected $naan;

    /**
     *
     * @var string
     */
    protected $identifier;

    /**
     *
     * @var string
     */
    protected $qualifiers;

    /**
     * Resolves an ARK identifier to its target URL
     *
     * @param int $naan
     * @param string $identifier
     * @param string $qualifiers
     * @return void
     */
    public function resolve($naan, $identifier, $qualifiers = "")
    {
        global $opac_url_base, $pmb_ark_activate;

        if (!$pmb_ark_activate) {
            Response::forbidden();
        }

        $this->naan = $naan;
        $this->identifier = $identifier;
        $this->qualifiers = $qualifiers;
        $this->checkNaan();

        $url = ArkModel::resolve($this->naan, $this->identifier, $this->qualifiers);
        if (strpos($url, $opac_url_base) === false) {
            $url = $opac_url_base.$url;
        }

        Response::redirect($url, Response::REDIRECT_TEMP_CODE);
    }

    private function checkNaan()
    {
        global $opac_url_base;

        $naanModel = new NaanModel();
        $naanList = $naanModel->getNaanData();
        if(!in_array($this->naan, $naanList['list'])){
            Response::redirect($opac_url_base, Response::REDIRECT_TEMP_CODE);
        }
    }
}