Heimdall/app/SupportedApps.php

233 lines
6.3 KiB
PHP
Raw Normal View History

<?php
namespace App;
2018-10-18 09:37:18 +00:00
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ServerException;
2018-11-01 08:55:21 +00:00
use Illuminate\Support\Facades\Log;
use Psr\Http\Message\ResponseInterface;
2018-10-18 09:37:18 +00:00
abstract class SupportedApps
{
2018-10-28 10:59:59 +00:00
protected $jar = false;
2018-10-28 10:59:59 +00:00
protected $method = 'GET';
2018-10-31 15:42:34 +00:00
protected $error;
2018-10-28 10:59:59 +00:00
/**
* @param $url
* @param array $attrs
* @return object
2022-11-26 13:12:13 +00:00
* @throws GuzzleException
*/
2022-11-26 13:12:13 +00:00
public function appTest($url, array $attrs = []): object
2018-10-18 09:37:18 +00:00
{
if (empty($this->config->url)) {
return (object) [
2018-11-07 11:24:26 +00:00
'code' => 404,
'status' => 'No URL has been specified',
'response' => 'No URL has been specified',
];
2018-11-07 11:24:26 +00:00
}
2018-10-28 10:59:59 +00:00
$res = $this->execute($url, $attrs);
if ($res == null) {
return (object) [
2018-10-31 15:42:34 +00:00
'code' => null,
'status' => $this->error,
'response' => 'Connection failed',
];
}
switch ($res->getStatusCode()) {
2018-10-21 12:23:23 +00:00
case 200:
2018-10-28 10:59:59 +00:00
$status = 'Successfully communicated with the API';
2018-10-21 12:23:23 +00:00
break;
case 401:
2018-10-28 10:59:59 +00:00
$status = 'Failed: Invalid credentials';
2018-10-21 12:23:23 +00:00
break;
case 404:
2018-10-28 10:59:59 +00:00
$status = 'Failed: Please make sure your URL is correct and that there is a trailing slash';
2018-10-21 12:23:23 +00:00
break;
default:
2018-10-28 10:59:59 +00:00
$status = 'Something went wrong... Code: '.$res->getStatusCode();
2018-10-21 12:23:23 +00:00
break;
}
return (object) [
2018-10-28 10:59:59 +00:00
'code' => $res->getStatusCode(),
'status' => $status,
'response' => $res->getBody(),
];
2018-10-18 09:37:18 +00:00
}
/**
* @param $url
* @param array $attrs
* @param array|bool|null $overridevars
* @param string|bool|null $overridemethod
* @return ResponseInterface|null
* @throws GuzzleException
*/
public function execute(
$url,
array $attrs = [],
$overridevars = null,
$overridemethod = null
): ?ResponseInterface {
2018-10-31 15:42:34 +00:00
$res = null;
$vars = ($overridevars === null || $overridevars === false) ?
[
'http_errors' => false,
'timeout' => 15,
2018-10-28 10:59:59 +00:00
'connect_timeout' => 15,
] : $overridevars;
2018-10-31 15:42:34 +00:00
2018-10-28 10:59:59 +00:00
$client = new Client($vars);
$method = ($overridemethod === null || $overridemethod === false) ? $this->method : $overridemethod;
2018-10-31 15:42:34 +00:00
2018-10-28 10:59:59 +00:00
try {
return $client->request($method, $url, $attrs);
} catch (ConnectException $e) {
Log::error('Connection refused');
2018-10-31 15:42:34 +00:00
Log::debug($e->getMessage());
$this->error = 'Connection refused - '.(string) $e->getMessage();
} catch (ServerException $e) {
2018-10-31 15:42:34 +00:00
Log::debug($e->getMessage());
$this->error = (string) $e->getResponse()->getBody();
}
$this->error = 'General error connecting with API';
2018-10-31 15:42:34 +00:00
return $res;
2018-10-18 09:37:18 +00:00
}
/**
* @return void
*/
2018-10-18 09:37:18 +00:00
public function login()
{
}
/**
* @param string $url
* @param bool $addslash
* @return string
*/
public function normaliseurl(string $url, bool $addslash = true): string
2018-10-18 09:37:18 +00:00
{
2018-10-31 15:42:34 +00:00
$url = rtrim($url, '/');
if ($addslash) {
$url .= '/';
}
2018-10-31 15:42:34 +00:00
return $url;
2018-10-18 09:37:18 +00:00
}
/**
* @param $status
* @param $data
* @return false|string
*/
2018-10-21 11:39:12 +00:00
public function getLiveStats($status, $data)
{
$className = get_class($this);
$explode = explode('\\', $className);
$name = end($explode);
$html = view('SupportedApps::'.$name.'.livestats', $data)->with('data', $data)->render();
2018-10-21 11:39:12 +00:00
return json_encode(['status' => $status, 'html' => $html]);
//return
2018-10-21 11:39:12 +00:00
}
/**
* @return ResponseInterface
* @throws GuzzleException
*/
public static function getList(): ResponseInterface
2018-10-20 23:17:36 +00:00
{
// $list_url = 'https://apps.heimdall.site/list';
2022-03-16 15:49:44 +00:00
$list_url = config('app.appsource').'list.json';
$client = new Client(['http_errors' => false, 'verify' => false, 'timeout' => 15, 'connect_timeout' => 15]);
2018-10-28 20:41:46 +00:00
return $client->request('GET', $list_url);
2018-10-20 23:17:36 +00:00
}
public static function configValue($item = null, $key = null)
2018-11-06 11:28:22 +00:00
{
if (isset($item) && ! empty($item)) {
2018-11-06 11:28:22 +00:00
return $item->getconfig()->$key;
} else {
return null;
}
2018-11-06 11:28:22 +00:00
}
/**
* @param $app
* @return bool|false
* @throws GuzzleException
*/
public static function getFiles($app): bool
2018-10-20 23:17:36 +00:00
{
Log::debug("Download triggered for ".print_r($app, true));
2022-03-16 15:49:44 +00:00
$zipurl = config('app.appsource').'files/'.$app->sha.'.zip';
2022-03-15 18:19:01 +00:00
2022-04-07 08:33:31 +00:00
$client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15, 'verify' => false]);
2018-10-28 20:41:46 +00:00
$res = $client->request('GET', $zipurl);
2018-10-20 23:17:36 +00:00
// Something went wrong?
if ($res->getStatusCode() !== 200) {
return false;
}
if (! file_exists(app_path('SupportedApps'))) {
2018-10-23 14:53:56 +00:00
mkdir(app_path('SupportedApps'), 0777, true);
}
$src = app_path('SupportedApps/'.className($app->name).'.zip');
2018-10-20 23:17:36 +00:00
file_put_contents($src, $res->getBody());
$zip = new \ZipArchive();
$x = $zip->open($src); // open the zip file to extract
if ($x === true) {
$zip->extractTo(app_path('SupportedApps')); // place in the directory with same name
$zip->close();
unlink($src); //Deleting the Zipped file
2022-03-15 18:19:01 +00:00
} else {
var_dump($x);
return false;
2018-10-20 23:17:36 +00:00
}
return true;
2018-10-20 23:17:36 +00:00
}
/**
* @param $details
* @param $app
* @return mixed
*/
2018-10-20 23:17:36 +00:00
public static function saveApp($details, $app)
{
2018-10-25 13:42:14 +00:00
$app->appid = $details->appid;
2018-10-20 23:17:36 +00:00
$app->name = $details->name;
2018-10-25 13:42:14 +00:00
$app->sha = $details->sha ?? null;
$app->icon = 'icons/'.$details->icon;
2018-10-20 23:17:36 +00:00
$app->website = $details->website;
$app->license = $details->license;
2018-10-21 11:39:12 +00:00
$appclass = $app->class();
$application = new $appclass;
$enhanced = (bool) ($application instanceof \App\EnhancedApps);
2022-03-16 15:49:44 +00:00
$app->class = $appclass;
2018-10-21 11:39:12 +00:00
$app->enhanced = $enhanced;
2018-10-20 23:17:36 +00:00
$app->tile_background = $details->tile_background;
$app->save();
return $app;
}
}