Heimdall/app/SupportedApps.php

167 lines
5 KiB
PHP
Raw Permalink Normal View History

2018-10-18 09:37:18 +00:00
<?php namespace App;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
2018-11-01 08:55:21 +00:00
use Illuminate\Support\Facades\Log;
2018-10-18 09:37:18 +00:00
abstract class SupportedApps
{
2018-10-28 10:59:59 +00:00
protected $jar = false;
protected $method = 'GET';
2018-10-31 15:42:34 +00:00
protected $error;
2018-10-28 10:59:59 +00:00
2018-10-31 15:42:34 +00:00
public function appTest($url, $attrs = [], $overridevars=false)
2018-10-18 09:37:18 +00:00
{
2018-11-07 11:24:26 +00:00
if(empty($this->config->url)) {
return (object)[
'code' => 404,
'status' => 'No URL has been specified',
'response' => 'No URL has been specified',
];
}
2018-10-28 10:59:59 +00:00
$res = $this->execute($url, $attrs);
2018-10-31 15:42:34 +00:00
if($res == null) {
return (object)[
'code' => null,
'status' => $this->error,
'response' => 'Connection failed',
];
}
2018-10-21 12:23:23 +00:00
switch($res->getStatusCode()) {
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;
}
2018-10-28 10:59:59 +00:00
return (object)[
'code' => $res->getStatusCode(),
'status' => $status,
'response' => $res->getBody(),
];
2018-10-18 09:37:18 +00:00
}
2018-10-31 15:42:34 +00:00
public function execute($url, $attrs = [], $overridevars=false, $overridemethod=false)
2018-10-18 09:37:18 +00:00
{
2018-10-31 15:42:34 +00:00
$res = null;
2018-11-01 08:55:21 +00:00
$vars = ($overridevars !== false) ?
2018-10-31 15:42:34 +00:00
$overridevars : [
2018-10-28 10:59:59 +00:00
'http_errors' => false,
'timeout' => 15,
'connect_timeout' => 15,
];
2018-10-31 15:42:34 +00:00
2018-10-28 10:59:59 +00:00
$client = new Client($vars);
2018-10-31 15:42:34 +00:00
$method = ($overridemethod !== false) ? $overridemethod : $this->method;
2018-10-28 10:59:59 +00:00
try {
2018-10-31 15:42:34 +00:00
return $client->request($method, $url, $attrs);
} catch (\GuzzleHttp\Exception\ConnectException $e) {
Log::error("Connection refused");
Log::debug($e->getMessage());
$this->error = "Connection refused - ".(string) $e->getMessage();
} catch (\GuzzleHttp\Exception\ServerException $e) {
Log::debug($e->getMessage());
$this->error = (string) $e->getResponse()->getBody();
}
$this->error = 'General error connecting with API';
return $res;
2018-10-18 09:37:18 +00:00
}
public function login()
{
}
2018-10-31 15:42:34 +00:00
public function normaliseurl($url, $addslash=true)
2018-10-18 09:37:18 +00:00
{
2018-10-31 15:42:34 +00:00
$url = rtrim($url, '/');
if($addslash) $url .= '/';
return $url;
2018-10-18 09:37:18 +00:00
}
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();
return json_encode(['status' => $status, 'html' => $html]);
//return
}
2018-10-20 23:17:36 +00:00
public static function getList()
{
// $list_url = 'https://apps.heimdall.site/list';
2022-03-16 15:49:44 +00:00
$list_url = config('app.appsource').'list.json';
2018-10-20 23:17:36 +00:00
$client = new Client(['http_errors' => 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
}
2018-11-06 11:28:22 +00:00
public static function configValue($item=null, $key=null)
{
if(isset($item) && !empty($item)) {
return $item->getconfig()->$key;
} else return null;
}
2018-10-20 23:17:36 +00:00
public static function getFiles($app)
{
2022-03-16 15:49:44 +00:00
$zipurl = config('app.appsource').'files/'.$app->sha.'.zip';
2022-03-15 18:19:01 +00:00
2018-10-20 23:17:36 +00:00
$client = new Client(['http_errors' => false, 'timeout' => 60, 'connect_timeout' => 15]);
2018-10-28 20:41:46 +00:00
$res = $client->request('GET', $zipurl);
2018-10-20 23:17:36 +00:00
2018-10-23 14:53:56 +00:00
if(!file_exists(app_path('SupportedApps'))) {
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);
2018-10-20 23:17:36 +00:00
}
}
public static function saveApp($details, $app)
2022-03-15 18:19:01 +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;
$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();
2022-03-15 18:19:01 +00:00
return $app;
2018-10-20 23:17:36 +00:00
}
2018-10-18 09:37:18 +00:00
}