Skip to content
Snippets Groups Projects
Key.php 2.11 KiB
Newer Older
  • Learn to ignore specific revisions
  • Davide Aprea's avatar
    Davide Aprea committed
    
    namespace App\Models;
    
    
    Davide Aprea's avatar
    Davide Aprea committed
        public $key;
    
    Davide Aprea's avatar
    Davide Aprea committed
        public $status; # valid key = true, invalid key = false, unidentified key = null
    
        private $keyserver = "https://key.metager.de/";
    
        public function __construct($key, $status = null)
        {
    
    Davide Aprea's avatar
    Davide Aprea committed
            $this->key = $key;
    
    Davide Aprea's avatar
    Davide Aprea committed
            $this->status = $status;
    
            if (getenv("APP_ENV") !== "production") {
                $this->keyserver = "https://dev.key.metager.de/";
            }
    
    Davide Aprea's avatar
    Davide Aprea committed
        # always returns true or false
    
        public function getStatus()
        {
            if ($this->key !== '' && $this->status === null) {
    
                $this->updateStatus();
    
    Davide Aprea's avatar
    Davide Aprea committed
            }
    
            if ($this->status === null || $this->status === false) {
    
    Davide Aprea's avatar
    Davide Aprea committed
                return false;
            } else {
                return true;
            }
    
    Davide Aprea's avatar
    Davide Aprea committed
        
    
        public function updateStatus()
        {
    
    Davide Aprea's avatar
    Davide Aprea committed
            try {
    
                $link = $this->keyserver . urlencode($this->key) . "/request-permission/api-access";
    
    Davide Aprea's avatar
    Davide Aprea committed
                $result = json_decode(file_get_contents($link));
                if ($result->{'api-access'} == true) {
                    $this->status = true;
                    return true;
                } else {
                    $this->status = false;
                    return false;
                }
            } catch (\ErrorException $e) {
                return false;
            }
        }
    
    
        public function requestPermission()
        {
    
    Davide Aprea's avatar
    Davide Aprea committed
            $postdata = http_build_query(array(
                'dummy' => 0,
            ));
    
    Davide Aprea's avatar
    Davide Aprea committed
            $opts = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata,
                ),
            );
    
            $context = stream_context_create($opts);
    
            try {
    
                $link = $this->keyserver . urlencode($this->key) . "/request-permission/api-access";
    
    Davide Aprea's avatar
    Davide Aprea committed
                $result = json_decode(file_get_contents($link, false, $context));
                if ($result->{'api-access'} == true) {
                    return true;
                } else {
    
                    $this->status = false;
    
    Davide Aprea's avatar
    Davide Aprea committed
                    return false;
                }
            } catch (\ErrorException $e) {
                return false;
            }
        }