Heimdall/app/SupportedApps.php

158 lines
4.8 KiB
PHP
Raw 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-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';
$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
}
public static function getFiles($app)
{
$zipurl = $app->files;
$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
}
}
public static function saveApp($details, $app)
{
2018-11-05 18:47:56 +00:00
if(!file_exists(public_path('storage/icons'))) {
mkdir(public_path('storage/icons'), 0777, true);
}
$img_src = app_path('SupportedApps/'.className($details->name).'/'.$details->icon);
$img_dest = public_path('storage/icons/'.$details->icon);
2018-10-23 14:53:56 +00:00
//die("i: ".$img_src);
2018-10-20 23:17:36 +00:00
copy($img_src, $img_dest);
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;
$app->description = $details->description;
2018-10-21 11:39:12 +00:00
$appclass = $app->class();
$application = new $appclass;
$enhanced = (bool)($application instanceof \App\EnhancedApps);
$app->enhanced = $enhanced;
2018-10-20 23:17:36 +00:00
$app->tile_background = $details->tile_background;
$app->save();
}
2018-10-18 09:37:18 +00:00
}