File size: 6,295 Bytes
5f32fbd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | <?php
namespace App\Repositories;
use App\Helpers\Classes\Helper;
use App\Models\Extension;
use App\Models\SettingTwo;
use App\Repositories\Contracts\ExtensionRepositoryInterface;
use Closure;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use RachidLaasri\LaravelInstaller\Repositories\ApplicationStatusRepository;
class ExtensionRepository implements ExtensionRepositoryInterface
{
public const APP_VERSION = 6.5;
public const API_URL = 'https://liquidlabs.uk/market/api/';
public function licensed(array $data): array
{
return collect($data)->filter(fn ($extension) => $extension['licensed'])->toArray();
}
public function extensions()
{
return $this->all();
}
public function themes()
{
return $this->all(true);
}
public function all(bool $isTheme = false)
{
$appVersion = $this->appVersion();
$response = $this->request('get', 'extension', [
'is_theme' => $isTheme,
'is_beta' => false,
'app_version' => $appVersion ?: 6.5,
]);
if ($response->ok()) {
$data = $response->json('data');
if (count($data) === $this->dbExtensionCount($isTheme)) {
return $this->mergedInstalled($data);
}
$this->updateExtensionsTable($data);
return $this->mergedInstalled($data);
}
return [];
}
public function findId(int $id)
{
return collect($this->extensions())->where('id', $id)->first();
}
public function find(string $slug): array
{
$response = $this->request('get', "extension/{$slug}");
if ($response->ok()) {
$data = $response->json('data');
$extension = Extension::query()->firstWhere('slug', $slug);
return array_merge($data, [
'db_version' => $extension?->version,
'installed' => (bool) $extension?->installed,
'upgradable' => $extension?->version !== $data['version'],
]);
}
return [];
}
public function install(string $slug, string $version)
{
return $this->request('post', "extension/{$slug}/install/{$version}");
}
public function request(string $method, string $route, array $body = [], $fullUrl = null)
{
$fullUrl = $fullUrl ?? self::API_URL . $route;
return Http::withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'x-domain' => request()->getHost(),
'x-domain-key' => $this->domainKey(),
'x-license-type' => $this->licenseType(),
'x-app-key' => $this->appKey(),
])->when($method === 'post', function ($http) use ($fullUrl, $body) {
return $http->post($fullUrl, $body);
}, function ($http) use ($fullUrl, $body) {
return $http->get($fullUrl, $body);
});
}
public function check($request, Closure $next)
{
$domain = $request->getHost();
$check = cache()->remember('check_license_domain_' . $domain, 60 * 60 * 24, function () {
return $this
->request('post', 'check')
->json('licensed');
});
if (! $check) {
if (Storage::disk('local')->exists('portal')) {
Storage::disk('local')->delete('portal');
}
SettingTwo::first()->update(['liquid_license_domain_key' => null]);
cache()->delete('check_license_domain_' . $domain);
return redirect()->route('LaravelInstaller::license')->with(['message' => 'License for this domain is invalid. Please contact support.']);
}
return $next($request);
}
public function mergedInstalled(array $data): array
{
$extensions = Extension::query()->get();
return collect($data)->map(function ($extension) use ($extensions) {
$value = $extensions->firstWhere('slug', $extension['slug']);
return array_merge($extension, [
'db_version' => $value?->version,
'installed' => (bool) $value?->installed,
'upgradable' => $value?->version !== $extension['version'],
]);
})->toArray();
}
private function updateExtensionsTable(array $data): void
{
foreach ($data as $extension) {
Extension::query()->firstOrCreate([
'slug' => $extension['slug'],
'is_theme' => $extension['is_theme'],
], [
'version' => $extension['version'],
]);
}
}
private function dbExtensionCount(bool $isTheme = false): int
{
return Extension::query()
->where('is_theme', $isTheme)
->count();
}
public function appKey()
{
return md5(config('app.key'));
}
public function licenseType()
{
return app(ApplicationStatusRepository::class)->getVariable('liquid_license_type') ?: Helper::settingTwo('liquid_license_type');
}
public function domainKey()
{
return app(ApplicationStatusRepository::class)->getVariable('liquid_license_domain_key') ?: Helper::settingTwo('liquid_license_domain_key');
}
public function subscription()
{
return $this->request('get', 'subscription');
}
public function subscriptionPayment()
{
return cache()->remember('subscription_payment', 60 * 60 * 24, function () {
if ($this->subscription()->json('payment')) {
return $this->subscription()->json('payment');
}
return '';
});
}
public function appVersion(): bool|string|int
{
$file = base_path('version.txt');
if (file_exists($file)) {
return file_get_contents($file);
}
return self::APP_VERSION;
}
public function cart(): ?array
{
$response = $this->request('get', 'cart' . DIRECTORY_SEPARATOR . $this->domainKey());
if ($response->successful()) {
return $response->json();
}
return [];
}
}
|