Heimdall/app/Http/Controllers/ItemController.php

453 lines
12 KiB
PHP
Raw Normal View History

2018-01-29 12:41:57 +00:00
<?php
namespace App\Http\Controllers;
2018-10-18 14:59:38 +00:00
use App\Application;
2018-01-31 15:55:26 +00:00
use App\Item;
use App\Jobs\ProcessApps;
use App\Search;
2018-02-04 21:28:11 +00:00
use App\Setting;
use App\SupportedApps;
use App\User;
use Artisan;
2018-10-18 14:59:38 +00:00
use GrahamCampbell\GitHub\Facades\GitHub;
2022-03-16 15:49:44 +00:00
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\Request;
2022-03-16 15:49:44 +00:00
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;
2022-03-16 15:49:44 +00:00
2018-01-29 12:41:57 +00:00
class ItemController extends Controller
{
2018-10-15 12:02:16 +00:00
public function __construct()
{
$this->middleware('allowed');
}
/**
2018-02-01 14:45:59 +00:00
* Display a listing of the resource on the dashboard.
*
* @return \Illuminate\Http\Response
*/
public function dash()
{
2019-06-13 14:57:54 +00:00
$data['apps'] = Item::whereHas('parents', function ($query) {
$query->where('id', 0);
2019-06-22 16:03:01 +00:00
})->orWhere('type', 1)->pinned()->orderBy('order', 'asc')->get();
2019-06-13 14:57:54 +00:00
$data['all_apps'] = Item::whereHas('parents', function ($query) {
$query->where('id', 0);
2019-06-22 16:03:01 +00:00
})->orWhere('type', 1)->orderBy('order', 'asc')->get();
2019-06-13 14:57:54 +00:00
//$data['all_apps'] = Item::doesntHave('parents')->get();
//die(print_r($data['apps']));
2018-02-01 14:45:59 +00:00
return view('welcome', $data);
}
/**
* Set order on the dashboard.
*
* @return \Illuminate\Http\Response
*/
public function setOrder(Request $request)
{
$order = array_filter($request->input('order'));
foreach ($order as $o => $id) {
$item = Item::find($id);
$item->order = $o;
$item->save();
}
}
2018-10-18 14:59:38 +00:00
/**
* Pin item on the dashboard.
*
* @return \Illuminate\Http\Response
*/
public function pin($id)
{
$item = Item::findOrFail($id);
$item->pinned = true;
$item->save();
2019-06-18 09:51:51 +00:00
$route = route('dash', []);
2018-02-18 22:36:32 +00:00
return redirect($route);
}
/**
* Unpin item on the dashboard.
*
* @return \Illuminate\Http\Response
*/
public function unpin($id)
{
$item = Item::findOrFail($id);
$item->pinned = false;
$item->save();
2019-06-18 09:51:51 +00:00
$route = route('dash', []);
2018-02-18 22:36:32 +00:00
return redirect($route);
}
/**
* Unpin item on the dashboard.
*
* @return \Illuminate\Http\Response
*/
public function pinToggle($id, $ajax = false, $tag = false)
{
$item = Item::findOrFail($id);
$new = ((bool) $item->pinned === true) ? false : true;
$item->pinned = $new;
$item->save();
if ($ajax) {
if (is_numeric($tag) && $tag > 0) {
$item = Item::whereId($tag)->first();
$data['apps'] = $item->children()->pinned()->orderBy('order', 'asc')->get();
} else {
$data['apps'] = Item::pinned()->orderBy('order', 'asc')->get();
}
$data['ajax'] = true;
return view('sortable', $data);
} else {
2019-06-18 09:51:51 +00:00
$route = route('dash', []);
2018-02-18 22:36:32 +00:00
return redirect($route);
}
}
2018-01-29 12:41:57 +00:00
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
2018-02-03 15:46:14 +00:00
public function index(Request $request)
2018-01-29 12:41:57 +00:00
{
$trash = (bool) $request->input('trash');
2018-02-03 15:46:14 +00:00
2018-02-17 00:13:38 +00:00
$data['apps'] = Item::ofType('item')->orderBy('title', 'asc')->get();
$data['trash'] = Item::ofType('item')->onlyTrashed()->get();
if ($trash) {
2018-02-03 15:46:14 +00:00
return view('items.trash', $data);
} else {
return view('items.list', $data);
}
2018-01-29 12:41:57 +00:00
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
2018-02-17 00:13:38 +00:00
$data['tags'] = Item::ofType('tag')->orderBy('title', 'asc')->pluck('title', 'id');
2019-06-13 18:07:38 +00:00
$data['tags']->prepend(__('app.dashboard'), 0);
$data['current_tags'] = '0';
2018-02-01 06:57:12 +00:00
return view('items.create', $data);
2018-01-29 12:41:57 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
// Get the item
$item = Item::find($id);
if ($item->appid === null && $item->class !== null) { // old apps wont have an app id so set it
$app = Application::where('class', $item->class)->first();
if ($app) {
$item->appid = $app->appid;
}
}
$data['item'] = $item;
$data['tags'] = Item::ofType('tag')->orderBy('title', 'asc')->pluck('title', 'id');
$data['tags']->prepend(__('app.dashboard'), 0);
$data['current_tags'] = $data['item']->tags();
//$data['current_tags'] = $data['item']->parent;
//die(print_r($data['current_tags']));
// show the edit form and pass the nerd
return view('items.edit', $data);
}
2022-03-15 18:19:01 +00:00
public function storelogic($request, $id = null)
2018-01-29 12:41:57 +00:00
{
2022-03-15 18:19:01 +00:00
$application = Application::single($request->input('appid'));
2018-02-01 06:57:12 +00:00
$validatedData = $request->validate([
'title' => 'required|max:255',
2019-06-11 13:11:13 +00:00
'url' => 'required',
2018-02-01 06:57:12 +00:00
]);
if ($request->hasFile('file')) {
2018-02-02 15:16:55 +00:00
$path = $request->file('file')->store('icons');
$request->merge([
'icon' => $path,
2018-02-02 15:16:55 +00:00
]);
} elseif (strpos($request->input('icon'), 'http') === 0) {
2022-04-07 08:34:07 +00:00
$options=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$contents = file_get_contents($request->input('icon'), false, stream_context_create($options));
2022-03-16 15:49:44 +00:00
if ($application) {
$icon = $application->icon;
} else {
$file = $request->input('icon');
$path_parts = pathinfo($file);
$icon = md5($contents);
$icon .= '.'.$path_parts['extension'];
}
2022-03-15 18:19:01 +00:00
$path = 'icons/'.$icon;
Storage::disk('public')->put($path, $contents);
$request->merge([
'icon' => $path,
2022-03-15 18:19:01 +00:00
]);
2018-02-02 15:16:55 +00:00
}
2018-02-06 15:27:02 +00:00
$config = Item::checkConfig($request->input('config'));
$current_user = User::currentUser();
2018-02-08 20:00:24 +00:00
$request->merge([
'description' => $config,
'user_id' => $current_user->id,
2018-02-08 20:00:24 +00:00
]);
2018-02-06 15:27:02 +00:00
if ($request->input('appid') === 'null') {
2018-10-23 14:53:56 +00:00
$request->merge([
'class' => null,
]);
2022-03-16 15:49:44 +00:00
} else {
$request->merge([
'class' => Application::classFromName($application->name),
]);
2018-10-23 14:53:56 +00:00
}
if ($id === null) {
2022-03-15 18:19:01 +00:00
$item = Item::create($request->all());
} else {
$item = Item::find($id);
$item->update($request->all());
}
2018-02-17 00:13:38 +00:00
$item->parents()->sync($request->tags);
2022-03-15 18:19:01 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->storelogic($request);
2019-06-18 09:51:51 +00:00
$route = route('dash', []);
2018-02-18 22:36:32 +00:00
return redirect($route)
->with('success', __('app.alert.success.item_created'));
2018-01-29 12:41:57 +00:00
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
2022-03-15 18:19:01 +00:00
$this->storelogic($request, $id);
2019-06-18 09:51:51 +00:00
$route = route('dash', []);
2018-02-18 22:36:32 +00:00
return redirect($route)
->with('success', __('app.alert.success.item_updated'));
2018-01-29 12:41:57 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2018-02-03 15:46:14 +00:00
public function destroy(Request $request, $id)
2018-01-29 12:41:57 +00:00
{
//
$force = (bool) $request->input('force');
if ($force) {
2018-02-03 15:46:14 +00:00
Item::withTrashed()
->where('id', $id)
->forceDelete();
} else {
Item::find($id)->delete();
}
2018-02-18 22:36:32 +00:00
2019-06-18 09:51:51 +00:00
$route = route('items.index', []);
return redirect($route)
->with('success', __('app.alert.success.item_deleted'));
2018-01-29 12:41:57 +00:00
}
2018-02-03 15:46:14 +00:00
/**
* Restore the specified resource from soft deletion.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function restore($id)
{
//
Item::withTrashed()
->where('id', $id)
->restore();
2019-07-14 08:46:10 +00:00
$route = route('items.index', []);
2018-02-18 22:36:32 +00:00
return redirect($route)
->with('success', __('app.alert.success.item_restored'));
}
2018-02-05 19:43:24 +00:00
/**
* Return details for supported apps
*
* @return Json
*/
public function appload(Request $request)
{
$output = [];
$appid = $request->input('app');
2022-03-15 18:19:01 +00:00
if ($appid === 'null') {
return null;
}
/*$appname = $request->input('app');
2018-10-20 23:17:36 +00:00
//die($appname);
2018-10-19 14:10:05 +00:00
$app_details = Application::where('name', $appname)->firstOrFail();
2018-10-20 23:17:36 +00:00
$appclass = $app_details->class();
$app = new $appclass;
2018-10-19 14:10:05 +00:00
// basic details
$output['icon'] = $app_details->icon();
2018-10-28 20:41:46 +00:00
$output['name'] = $app_details->name;
2018-10-21 11:39:12 +00:00
$output['iconview'] = $app_details->iconView();
2018-10-19 14:10:05 +00:00
$output['colour'] = $app_details->defaultColour();
2018-10-23 14:53:56 +00:00
$output['class'] = $appclass;
2018-10-19 14:10:05 +00:00
// live details
2018-10-20 23:17:36 +00:00
if($app instanceof \App\EnhancedApps) {
2018-10-29 19:43:08 +00:00
$output['config'] = className($app_details->name).'.config';
2018-10-19 14:10:05 +00:00
} else {
$output['config'] = null;
}*/
2022-03-15 18:19:01 +00:00
$output['config'] = null;
$output['custom'] = null;
$app = Application::single($appid);
$output = (array) $app;
2022-03-15 18:19:01 +00:00
$appdetails = Application::getApp($appid);
if ((bool) $app->enhanced === true) {
2022-03-16 15:49:44 +00:00
// if(!isset($app->config)) { // class based config
$output['custom'] = className($appdetails->name).'.config';
2022-03-16 15:49:44 +00:00
// }
2022-03-15 18:19:01 +00:00
}
$output['colour'] = ($app->tile_background == 'light') ? '#fafbfc' : '#161b1f';
$output['iconview'] = config('app.appsource').'icons/'.$app->icon;
2018-02-05 20:59:38 +00:00
return json_encode($output);
2018-02-05 19:43:24 +00:00
}
public function testConfig(Request $request)
{
$data = $request->input('data');
//$url = $data[array_search('url', array_column($data, 'name'))]['value'];
2022-03-17 19:03:06 +00:00
$single = Application::single($data['type']);
$app = $single->class;
$app_details = new $app();
$app_details->config = (object) $data;
2018-10-21 12:23:23 +00:00
$app_details->test();
}
public function execute($url, $attrs = [], $overridevars = false)
2022-03-16 15:49:44 +00:00
{
$res = null;
$vars = ($overridevars !== false) ?
$overridevars : [
'http_errors' => false,
'timeout' => 15,
2022-03-16 15:49:44 +00:00
'connect_timeout' => 15,
'verify' => false,
2022-03-16 15:49:44 +00:00
];
$client = new Client($vars);
$method = 'GET';
try {
return $client->request($method, $url, $attrs);
2022-03-16 15:49:44 +00:00
} catch (\GuzzleHttp\Exception\ConnectException $e) {
Log::error('Connection refused');
2022-03-16 15:49:44 +00:00
Log::debug($e->getMessage());
} catch (\GuzzleHttp\Exception\ServerException $e) {
Log::debug($e->getMessage());
}
2022-03-16 15:49:44 +00:00
return $res;
}
public function websitelookup($url)
{
$url = \base64_decode($url);
$data = $this->execute($url);
2022-03-16 15:49:44 +00:00
return $data->getBody();
}
2018-02-08 15:50:53 +00:00
public function getStats($id)
{
$item = Item::find($id);
2018-10-21 12:23:23 +00:00
$config = $item->getconfig();
if (isset($item->class)) {
2018-10-28 10:59:59 +00:00
$application = new $item->class;
2018-10-21 11:39:12 +00:00
$application->config = $config;
echo $application->livestats();
2018-02-08 15:50:53 +00:00
}
}
2018-10-18 14:59:38 +00:00
public function checkAppList()
{
2018-11-01 08:55:21 +00:00
ProcessApps::dispatch();
$route = route('items.index');
2018-11-01 08:55:21 +00:00
return redirect($route)
->with('success', __('app.alert.success.updating'));
2018-10-18 14:59:38 +00:00
}
2018-01-29 12:41:57 +00:00
}