<?php

namespace Ministra\Lib;

use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\K131970f0a776522b1a53ba419cd5e32c;
use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e;
use Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1;
class Downloads implements \Ministra\Lib\StbApi\Downloads
{
    public function getAll()
    {
        return \Ministra\Lib\System::base64_decode(\Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->from('user_downloads')->where(['uid' => \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::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\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->from('user_downloads')->where(['uid' => \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::getInstance()->id])->get()->first();
        if (empty($record)) {
            return \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->insert('user_downloads', ['downloads' => $downloads, 'uid' => \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::getInstance()->id])->insert_id();
        }
        return \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::getInstance()->update('user_downloads', ['downloads' => $downloads], ['uid' => \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\bcb5276dcc10d28aee463111bf6dfaa1::getInstance()->id])->result();
    }
    public function createDownloadLink($type, $media_id, $user_id, $param = '')
    {
        $link_hash = \md5(\microtime(1) . \uniqid());
        $id = \Ministra\Lib\bd5c9022e9a9029209e5fd05775ea272\B4291b8d8fa2d898727b1f446db1e661e::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\bd5c9022e9a9029209e5fd05775ea272\K131970f0a776522b1a53ba419cd5e32c::getSafe('portal_url', '/stalker_portal/')) . '/server/api/get_download_link.php?lid=' . ($id ? $link_hash : '');
    }
}
