<?php

/* * ********************************************************************
 * ProxmoxVPS product developed. (2016-12-12)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 * ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\Models;

use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;

/**
 * Description of KeyPair
 *
 * @author Pawel Kopec <pawelk@modulesgarden.com>
 * @version 1.0.0
 * @property int $hosting_id
 * @property int $vm_id
 * @method static $this ofHostingId($hostingId)
 * @method $this ofVmId($id)
 */
class KeyPair extends ExtendedEloquentModel
{

    /** @var string */
    protected $table = 'KeyPair';
    protected $fillable = ['id', 'vm_id', 'hosting_id', 'public', 'private'];
    public $timestamps = false;

    public function getId()
    {
        return $this->id;
    }

    public function getHostingId()
    {
        return $this->hosting_id;
    }

    public function getPublic()
    {
        return decrypt($this->public);
    }

    public function getPrivate()
    {
        return decrypt($this->private);
    }

    public function isPublic()
    {
        return decrypt($this->public) != null;
    }

    public function isPrivate()
    {
        return decrypt($this->private) != null;
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function setHostingId($hostingId)
    {
        $this->hosting_id = $hostingId;
        return $this;
    }

    public function setPublic($public)
    {
        $this->public = encrypt($public);
        return $this;
    }

    public function setPrivate($private)
    {
        $this->private = encrypt($private);
        return $this;
    }

    /**
     * @return KeyPair
     */
    public static function make()
    {
        $config = ['private_key_type' => OPENSSL_KEYTYPE_RSA];
        $res    = openssl_pkey_new($config);
        openssl_pkey_export($res, $privkey);
        $pubkey = openssl_pkey_get_details($res);
        $pubKey = self::encodePublicKey($res);
        $obj    = new KeyPair();
        $obj->setPublic($pubKey)
            ->setPrivate($privkey);
        return $obj;

    }

    private static function encodePublicKey($privKey)
    {
        $keyInfo = openssl_pkey_get_details($privKey);
        $buffer  = pack("N", 7) . "ssh-rsa" .
                   self::encodeBuffer($keyInfo['rsa']['e']) .
                   self::encodeBuffer($keyInfo['rsa']['n']);
        return "ssh-rsa " . base64_encode($buffer);
    }

    private static function encodeBuffer($buffer)
    {
        $len = strlen($buffer);
        if (ord($buffer[0]) & 0x80)
        {
            $len++;
            $buffer = "\x00" . $buffer;
        }
        return pack("Na*", $len, $buffer);
    }

    public function scopeOfHostingId($query, $hostingId)
    {
        return $query->where("hosting_id", $hostingId);
    }

    public function scopeOfVmId($query, $vmId)
    {
        return $query->where("vm_id", $vmId);
    }

}
