<?php

namespace Ministra\Lib;

use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf;
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2;
use Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336;
class Downloads implements \Ministra\Lib\StbApi\Downloads
{
    public function getAll()
    {
        return \Ministra\Lib\System::base64_decode(\Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->from('user_downloads')->where(['uid' => \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id])->get()->first('downloads'));
    }
    public function save()
    {
        $downloads = @$_REQUEST['downloads'];
        if (empty($downloads)) {
            $downloads = '""';
        }
        $downloads = \Ministra\Lib\System::base64_encode($downloads);
        $record = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->from('user_downloads')->where(['uid' => \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id])->get()->first();
        if (empty($record)) {
            return \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->insert('user_downloads', ['downloads' => $downloads, 'uid' => \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id])->insert_id();
        }
        return \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->update('user_downloads', ['downloads' => $downloads], ['uid' => \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\U7b993fd611168684ddb26c8f5ab7e336::getInstance()->id])->result();
    }
    public function createDownloadLink($type, $media_id, $user_id, $param = '')
    {
        $link_hash = \md5(\microtime(1) . \uniqid());
        $id = \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\d3507c2999d38573a0a0c91771d70af2::getInstance()->insert('download_links', ['link_hash' => $link_hash, 'uid' => $user_id, 'type' => $type, 'media_id' => $media_id, 'param1' => $param, 'added' => 'NOW()'])->insert_id();
        return 'http' . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443 ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . '/' . \str_replace('/', '', \Ministra\Lib\w13ec15f0067ea75861e61e27c990e4ba\k8281faec2a4a41f470b00112682ddabf::getSafe('portal_url', '/stalker_portal/')) . '/server/api/get_download_link.php?lid=' . ($id ? $link_hash : '');
    }
}
