2019-04-24 18:46:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\GUI;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Resources\oAuth\AccessToken;
|
|
|
|
use App\Models\App;
|
|
|
|
use App\Models\User;
|
2019-09-24 14:02:39 +00:00
|
|
|
use Aws\S3\S3Client;
|
2019-04-24 18:46:41 +00:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class AppController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
public function appList() {
|
2019-04-28 12:49:10 +00:00
|
|
|
if(!Auth::check()) {
|
|
|
|
abort(401);
|
|
|
|
}
|
2019-04-24 18:46:41 +00:00
|
|
|
$apps = App::query()->where("user_id", "=", Auth::user()->id)->get();
|
2019-04-30 12:55:13 +00:00
|
|
|
|
2019-04-24 18:46:41 +00:00
|
|
|
return view('app/list', ["msg"=>"", "apps" => $apps]);
|
|
|
|
}
|
|
|
|
public function newAppView() {
|
|
|
|
return view('app/new', ["msg"=>""]);
|
|
|
|
}
|
|
|
|
public function newApp(Request $request) {
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|max:255|min:3|regex:@^[a-zA-Z0-9]*$@|unique:apps',
|
|
|
|
'description' => 'required|min:3',
|
|
|
|
'url' => 'required|url'
|
|
|
|
]);
|
|
|
|
|
2019-04-25 12:19:16 +00:00
|
|
|
$app = App::createApp($request->input("name"), htmlspecialchars($request->input("description")), $request->input("url"), Auth::user());
|
2019-04-24 18:46:41 +00:00
|
|
|
|
|
|
|
return "App created";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function viewApp(Request $request, $id) {
|
|
|
|
$app = App::query()->where("id", "=", $id)->first();
|
|
|
|
|
|
|
|
if($app->user_id != Auth::user()->id) {
|
|
|
|
abort(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('app/details', ["msg"=>"", "app" => $app]);
|
|
|
|
}
|
|
|
|
|
2019-04-25 10:03:34 +00:00
|
|
|
public function updateApp(Request $request, $id) {
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|max:255|min:3|regex:@^[a-zA-Z0-9]*$@',
|
|
|
|
'description' => 'required|min:3',
|
|
|
|
'url' => 'required|url',
|
|
|
|
'direct_url' => 'url'
|
|
|
|
]);
|
|
|
|
|
|
|
|
// ToDO Unique App Name
|
|
|
|
|
|
|
|
|
|
|
|
$app = App::query()->where("id", "=", $id)->first();
|
|
|
|
|
|
|
|
if($app->user_id != Auth::user()->id) {
|
|
|
|
abort(401);
|
|
|
|
}
|
|
|
|
|
|
|
|
$app->name = $request->input("name");
|
2019-04-25 12:19:16 +00:00
|
|
|
$app->description = htmlspecialchars($request->input("description"));
|
2019-04-25 10:03:34 +00:00
|
|
|
$app->url = $request->input("url");
|
|
|
|
$app->direct_url = $request->input("direct_url");
|
|
|
|
|
|
|
|
$app->saveOrFail();
|
|
|
|
return redirect('/gui/apps/'.$id);
|
|
|
|
}
|
|
|
|
|
2019-04-24 18:46:41 +00:00
|
|
|
public function changeIcon(Request $request, $id) {
|
|
|
|
// Todo: Replace prove of concept with better code
|
|
|
|
$newTmp = tempnam("", "icon_upload");
|
|
|
|
$request->file("icon")->move("/tmp", $newTmp);
|
|
|
|
$info = getimagesize($newTmp);
|
|
|
|
if($info["0"] != $info["1"]) {
|
2019-04-25 10:03:34 +00:00
|
|
|
echo "Icon is not a squader. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($info[0] > 500) {
|
2019-04-25 10:03:34 +00:00
|
|
|
echo "Icon is to big, max 500 px. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
if($info[0] < 50) {
|
2019-04-25 10:03:34 +00:00
|
|
|
echo "Icon is to small, min 50px. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$app = App::query()->where("id", "=", $id)->firstOrFail();
|
|
|
|
if($app->user_id != Auth::user()->id) {
|
2019-04-25 10:03:34 +00:00
|
|
|
echo "Its not your app. <a href='/gui/apps/".$id."'>Zurück</a>";exit();
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:02:39 +00:00
|
|
|
$s3 = app(S3Client::class);
|
|
|
|
|
|
|
|
$result = $s3->putObject([
|
|
|
|
"Bucket" => getenv("S3_Bucket"),
|
|
|
|
"Key" => "icons/icon_".$app->id.".png",
|
|
|
|
"SourceFile" => $newTmp,
|
|
|
|
'ACL' => 'public-read'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$app->iconURL = $result['ObjectURL'];
|
2019-04-24 18:46:41 +00:00
|
|
|
$app->saveOrFail();
|
2019-09-24 14:02:39 +00:00
|
|
|
|
2019-04-25 10:03:34 +00:00
|
|
|
return redirect('/gui/apps/'.$id);
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAppIcon($id) {
|
|
|
|
$app = App::query()->where("id", "=", $id)->firstOrFail();
|
|
|
|
|
2019-09-24 14:02:39 +00:00
|
|
|
if(!is_dir(storage_path("icon"))) {
|
|
|
|
mkdir(storage_path("icon"));
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheFile = storage_path("icon/".$app->id.".png");
|
|
|
|
|
|
|
|
if(file_exists($cacheFile)) {
|
|
|
|
$icon = file_get_contents($cacheFile);
|
|
|
|
} else {
|
|
|
|
if(!empty($app->iconURL)) {
|
|
|
|
$icon = file_get_contents($app->iconURL);
|
|
|
|
file_put_contents($cacheFile, $icon);
|
|
|
|
} else {
|
|
|
|
$icon = file_get_contents(resource_path("images/app.png"));
|
|
|
|
}
|
2019-04-25 10:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:02:39 +00:00
|
|
|
$r = getimagesizefromstring($icon);
|
2019-04-24 18:46:41 +00:00
|
|
|
|
2019-09-24 14:02:39 +00:00
|
|
|
return response($icon)
|
2019-04-24 18:46:41 +00:00
|
|
|
->header('Content-Type',$r["mime"]);
|
2019-05-02 13:22:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function regenerateAppSecret(Request $request, $id) {
|
|
|
|
$app = App::query()->where("id", "=", $id)->firstOrFail();
|
|
|
|
|
|
|
|
if($app->user_id != Auth::user()->id) {
|
|
|
|
abort(401);
|
|
|
|
}
|
2019-04-24 18:46:41 +00:00
|
|
|
|
2019-05-02 13:22:37 +00:00
|
|
|
$app->regenerateApiSecret();
|
|
|
|
$app->saveOrFail();
|
|
|
|
|
|
|
|
return redirect('/gui/apps/'.$id);
|
2019-04-24 18:46:41 +00:00
|
|
|
}
|
|
|
|
}
|