2018-10-18 14:59:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
2022-11-29 22:13:24 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2018-10-18 14:59:38 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2022-11-29 22:13:24 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2022-11-18 15:12:52 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-10-18 14:59:38 +00:00
|
|
|
|
2022-11-29 22:13:24 +00:00
|
|
|
/**
|
|
|
|
* App\Application
|
|
|
|
*
|
|
|
|
* @property string $appid
|
|
|
|
* @property string $name
|
|
|
|
* @property string|null $sha
|
|
|
|
* @property string|null $icon
|
|
|
|
* @property string|null $website
|
|
|
|
* @property string|null $license
|
|
|
|
* @property string|null $description
|
|
|
|
* @property int $enhanced
|
|
|
|
* @property string $tile_background
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
* @property string|null $class
|
|
|
|
* @method static Builder|Application newModelQuery()
|
|
|
|
* @method static Builder|Application newQuery()
|
|
|
|
* @method static Builder|Application query()
|
|
|
|
* @method static Builder|Application whereAppid($value)
|
|
|
|
* @method static Builder|Application whereClass($value)
|
|
|
|
* @method static Builder|Application whereCreatedAt($value)
|
|
|
|
* @method static Builder|Application whereDescription($value)
|
|
|
|
* @method static Builder|Application whereEnhanced($value)
|
|
|
|
* @method static Builder|Application whereIcon($value)
|
|
|
|
* @method static Builder|Application whereLicense($value)
|
|
|
|
* @method static Builder|Application whereName($value)
|
|
|
|
* @method static Builder|Application whereSha($value)
|
|
|
|
* @method static Builder|Application whereTileBackground($value)
|
|
|
|
* @method static Builder|Application whereUpdatedAt($value)
|
|
|
|
* @method static Builder|Application whereWebsite($value)
|
|
|
|
*/
|
2018-10-18 14:59:38 +00:00
|
|
|
class Application extends Model
|
|
|
|
{
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2018-10-25 13:42:14 +00:00
|
|
|
public $incrementing = false;
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2018-10-25 13:42:14 +00:00
|
|
|
protected $primaryKey = 'appid';
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-10-19 14:10:05 +00:00
|
|
|
public function icon()
|
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
if (! file_exists(storage_path('app/public/'.$this->icon))) {
|
2018-11-09 23:07:01 +00:00
|
|
|
$img_src = app_path('SupportedApps/'.$this->name.'/'.str_replace('icons/', '', $this->icon));
|
|
|
|
$img_dest = storage_path('app/public/'.$this->icon);
|
|
|
|
//die("i: ".$img_src);
|
|
|
|
@copy($img_src, $img_dest);
|
|
|
|
}
|
|
|
|
|
2018-10-21 11:39:12 +00:00
|
|
|
return $this->icon;
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function iconView(): string
|
2018-10-21 11:39:12 +00:00
|
|
|
{
|
|
|
|
return asset('storage/'.$this->icon);
|
2018-10-19 14:10:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function defaultColour(): string
|
2018-10-19 14:10:05 +00:00
|
|
|
{
|
|
|
|
// check if light or dark
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($this->tile_background == 'light') {
|
|
|
|
return '#fafbfc';
|
|
|
|
}
|
|
|
|
|
2018-10-19 14:10:05 +00:00
|
|
|
return '#161b1f';
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function class(): string
|
2018-10-19 14:10:05 +00:00
|
|
|
{
|
2018-10-29 15:12:47 +00:00
|
|
|
$name = $this->name;
|
2022-03-19 13:54:32 +00:00
|
|
|
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
|
2018-10-29 15:12:47 +00:00
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
return '\App\SupportedApps\\'.$name.'\\'.$name;
|
2018-10-19 14:10:05 +00:00
|
|
|
}
|
2022-03-16 15:49:44 +00:00
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-11-25 00:35:56 +00:00
|
|
|
public static function classFromName($name): string
|
2022-03-16 15:49:44 +00:00
|
|
|
{
|
2022-03-19 13:54:32 +00:00
|
|
|
$name = preg_replace('/[^\p{L}\p{N}]/u', '', $name);
|
2022-03-16 15:49:44 +00:00
|
|
|
|
2022-03-19 14:05:01 +00:00
|
|
|
$class = '\App\SupportedApps\\'.$name.'\\'.$name;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-03-16 15:49:44 +00:00
|
|
|
return $class;
|
|
|
|
}
|
2018-10-21 11:39:12 +00:00
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
2022-11-29 22:13:24 +00:00
|
|
|
* @return Collection
|
2022-11-18 15:12:52 +00:00
|
|
|
*/
|
2022-11-29 22:13:24 +00:00
|
|
|
public static function apps(): Collection
|
2022-03-14 15:56:36 +00:00
|
|
|
{
|
|
|
|
$json = json_decode(file_get_contents(storage_path('app/supportedapps.json'))) ?? [];
|
|
|
|
$apps = collect($json->apps);
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
return $apps->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE);
|
2022-03-14 15:56:36 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function autocomplete(): array
|
2022-03-14 15:56:36 +00:00
|
|
|
{
|
|
|
|
$apps = self::apps();
|
|
|
|
$list = [];
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach ($apps as $app) {
|
|
|
|
$list[] = (object) [
|
2022-03-14 15:56:36 +00:00
|
|
|
'label' => $app->name,
|
2022-03-19 13:54:32 +00:00
|
|
|
'value' => $app->appid,
|
2022-03-14 15:56:36 +00:00
|
|
|
];
|
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-03-14 15:56:36 +00:00
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @param $appid
|
|
|
|
* @return mixed|null
|
2022-11-25 00:35:56 +00:00
|
|
|
* @throws GuzzleException
|
2022-11-18 15:12:52 +00:00
|
|
|
*/
|
2022-03-15 18:19:01 +00:00
|
|
|
public static function getApp($appid)
|
|
|
|
{
|
2022-11-18 15:12:52 +00:00
|
|
|
Log::debug("Get app triggered for: $appid");
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
$localapp = self::where('appid', $appid)->first();
|
2022-03-15 18:19:01 +00:00
|
|
|
$app = self::single($appid);
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
$application = ($localapp) ? $localapp : new self;
|
2022-03-15 18:19:01 +00:00
|
|
|
|
2022-11-21 23:05:57 +00:00
|
|
|
// Files missing? || app not in db || old sha version
|
2022-11-25 00:35:56 +00:00
|
|
|
if (! file_exists(app_path('SupportedApps/'.className($app->name))) ||
|
2022-11-21 23:05:57 +00:00
|
|
|
! $localapp ||
|
|
|
|
$localapp->sha !== $app->sha
|
|
|
|
) {
|
|
|
|
$gotFiles = SupportedApps::getFiles($app);
|
2022-11-25 00:35:56 +00:00
|
|
|
if ($gotFiles) {
|
2022-03-15 18:19:01 +00:00
|
|
|
$app = SupportedApps::saveApp($app, $application);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
return $app;
|
2022-03-15 18:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @param $appid
|
|
|
|
* @return mixed|null
|
|
|
|
*/
|
2022-03-14 15:56:36 +00:00
|
|
|
public static function single($appid)
|
|
|
|
{
|
|
|
|
$apps = self::apps();
|
|
|
|
$app = $apps->where('appid', $appid)->first();
|
2022-10-28 09:55:16 +00:00
|
|
|
|
|
|
|
if ($app === null) {
|
|
|
|
// Try in db for Private App
|
|
|
|
$appModel = self::where('appid', $appid)->first();
|
2022-11-25 00:35:56 +00:00
|
|
|
if ($appModel) {
|
2022-11-05 07:02:20 +00:00
|
|
|
$app = json_decode($appModel->toJson());
|
|
|
|
}
|
2022-10-28 09:55:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-19 13:54:32 +00:00
|
|
|
if ($app === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$classname = preg_replace('/[^\p{L}\p{N}]/u', '', $app->name);
|
2022-03-19 14:05:01 +00:00
|
|
|
$app->class = '\App\SupportedApps\\'.$classname.'\\'.$classname;
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-03-14 15:56:36 +00:00
|
|
|
return $app;
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:12:52 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function applist(): array
|
2018-10-23 14:53:56 +00:00
|
|
|
{
|
|
|
|
$list = [];
|
|
|
|
$list['null'] = 'None';
|
2022-03-14 15:56:36 +00:00
|
|
|
$apps = self::apps();
|
2022-03-19 13:54:32 +00:00
|
|
|
foreach ($apps as $app) {
|
2022-03-14 15:56:36 +00:00
|
|
|
$list[$app->appid] = $app->name;
|
2018-10-23 14:53:56 +00:00
|
|
|
}
|
2022-03-19 13:54:32 +00:00
|
|
|
|
2022-10-27 21:57:23 +00:00
|
|
|
// Check for private apps in the db
|
|
|
|
$appsListFromDB = self::all(['appid', 'name']);
|
|
|
|
|
2022-11-25 00:35:56 +00:00
|
|
|
foreach ($appsListFromDB as $app) {
|
2022-10-27 21:57:23 +00:00
|
|
|
// Already existing keys are overwritten,
|
|
|
|
// only private apps should be added at the end
|
|
|
|
$list[$app->appid] = $app->name;
|
|
|
|
}
|
|
|
|
|
2018-10-23 14:53:56 +00:00
|
|
|
return $list;
|
|
|
|
}
|
2018-10-18 14:59:38 +00:00
|
|
|
}
|