2022-03-19 13:54:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
2018-10-18 09:37:18 +00:00
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
2022-11-29 18:47:58 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2022-11-25 00:35:56 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2022-11-29 18:47:58 +00:00
|
|
|
use GuzzleHttp\Exception\ServerException;
|
2018-11-01 08:55:21 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2022-11-25 00:35:56 +00:00
|
|
|
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;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-28 10:59:59 +00:00
|
|
|
protected $method = 'GET';
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-31 15:42:34 +00:00
|
|
|
protected $error;
|
2018-10-28 10:59:59 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $url
|
|
|
|
* @param array $attrs
|
|
|
|
* @return object
|
2022-11-26 13:12:13 +00:00
|
|
|
* @throws GuzzleException
|
2022-11-25 00:35:56 +00:00
|
|
|
*/
|
2022-11-26 13:12:13 +00:00
|
|
|
public function appTest($url, array $attrs = []): object
|
2018-10-18 09:37:18 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +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',
|
2022-03-19 13:54:32 +00:00
|
|
|
];
|
2018-11-07 11:24:26 +00:00
|
|
|
}
|
2018-10-28 10:59:59 +00:00
|
|
|
$res = $this->execute($url, $attrs);
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($res == null) {
|
|
|
|
return (object) [
|
2018-10-31 15:42:34 +00:00
|
|
|
'code' => null,
|
|
|
|
'status' => $this->error,
|
|
|
|
'response' => 'Connection failed',
|
|
|
|
];
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $url
|
|
|
|
* @param array $attrs
|
2022-11-29 16:03:45 +00:00
|
|
|
* @param array|bool|null $overridevars
|
|
|
|
* @param string|bool|null $overridemethod
|
2022-11-25 00:35:56 +00:00
|
|
|
* @return ResponseInterface|null
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
|
|
|
public function execute(
|
|
|
|
$url,
|
|
|
|
array $attrs = [],
|
2022-11-29 16:03:45 +00:00
|
|
|
$overridevars = null,
|
|
|
|
$overridemethod = null
|
2022-11-25 00:35:56 +00:00
|
|
|
): ?ResponseInterface {
|
2018-10-31 15:42:34 +00:00
|
|
|
$res = null;
|
|
|
|
|
2022-11-29 18:47:58 +00:00
|
|
|
$vars = ($overridevars === null || $overridevars === false) ?
|
|
|
|
[
|
2022-03-19 13:54:32 +00:00
|
|
|
'http_errors' => false,
|
|
|
|
'timeout' => 15,
|
2018-10-28 10:59:59 +00:00
|
|
|
'connect_timeout' => 15,
|
2022-11-29 18:47:58 +00:00
|
|
|
] : $overridevars;
|
2018-10-31 15:42:34 +00:00
|
|
|
|
2018-10-28 10:59:59 +00:00
|
|
|
$client = new Client($vars);
|
|
|
|
|
2022-11-29 18:47:58 +00:00
|
|
|
$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 {
|
2022-03-19 13:54:32 +00:00
|
|
|
return $client->request($method, $url, $attrs);
|
2022-11-29 18:47:58 +00:00
|
|
|
} catch (ConnectException $e) {
|
2022-03-19 13:54:32 +00:00
|
|
|
Log::error('Connection refused');
|
2018-10-31 15:42:34 +00:00
|
|
|
Log::debug($e->getMessage());
|
2022-03-19 13:54:32 +00:00
|
|
|
$this->error = 'Connection refused - '.(string) $e->getMessage();
|
2022-11-29 18:47:58 +00:00
|
|
|
} 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';
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-31 15:42:34 +00:00
|
|
|
return $res;
|
2018-10-18 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-10-18 09:37:18 +00:00
|
|
|
public function login()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @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, '/');
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($addslash) {
|
|
|
|
$url .= '/';
|
|
|
|
}
|
2018-10-31 15:42:34 +00:00
|
|
|
|
|
|
|
return $url;
|
2018-10-18 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +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();
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-21 11:39:12 +00:00
|
|
|
return json_encode(['status' => $status, 'html' => $html]);
|
2022-03-19 13:54:32 +00:00
|
|
|
//return
|
2018-10-21 11:39:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @return ResponseInterface
|
|
|
|
* @throws GuzzleException
|
|
|
|
*/
|
|
|
|
public static function getList(): ResponseInterface
|
2018-10-20 23:17:36 +00:00
|
|
|
{
|
2022-03-14 15:56: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';
|
2022-03-30 12:14:35 +00:00
|
|
|
$client = new Client(['http_errors' => false, 'verify' => false, 'timeout' => 15, 'connect_timeout' => 15]);
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2018-10-28 20:41:46 +00:00
|
|
|
return $client->request('GET', $list_url);
|
2018-10-20 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
public static function configValue($item = null, $key = null)
|
2018-11-06 11:28:22 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if (isset($item) && ! empty($item)) {
|
2018-11-06 11:28:22 +00:00
|
|
|
return $item->getconfig()->$key;
|
2022-03-19 13:54:32 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-06 11:28:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-21 23:05:57 +00:00
|
|
|
/**
|
|
|
|
* @param $app
|
|
|
|
* @return bool|false
|
2022-11-25 00:35:56 +00:00
|
|
|
* @throws GuzzleException
|
2022-11-21 23:05:57 +00:00
|
|
|
*/
|
|
|
|
public static function getFiles($app): bool
|
2018-10-20 23:17:36 +00:00
|
|
|
{
|
2022-11-21 23:05:57 +00:00
|
|
|
Log::debug("Download triggered for ".print_r($app, true));
|
2022-11-18 15:12:52 +00:00
|
|
|
|
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
|
|
|
|
2022-11-21 23:05:57 +00:00
|
|
|
// Something went wrong?
|
|
|
|
if ($res->getStatusCode() !== 200) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! file_exists(app_path('SupportedApps'))) {
|
2018-10-23 14:53:56 +00:00
|
|
|
mkdir(app_path('SupportedApps'), 0777, true);
|
|
|
|
}
|
|
|
|
|
2018-10-29 15:12:47 +00:00
|
|
|
$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);
|
2022-11-21 23:05:57 +00:00
|
|
|
return false;
|
2018-10-20 23:17:36 +00:00
|
|
|
}
|
2022-11-21 23:05:57 +00:00
|
|
|
return true;
|
2018-10-20 23:17:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
/**
|
|
|
|
* @param $details
|
|
|
|
* @param $app
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-10-20 23:17:36 +00:00
|
|
|
public static function saveApp($details, $app)
|
2022-03-19 13:54:32 +00:00
|
|
|
{
|
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;
|
2018-11-05 15:27:29 +00:00
|
|
|
$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;
|
2022-03-19 13:54:32 +00:00
|
|
|
$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();
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
}
|