XBackBone/app/Controllers/ClientController.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2019-11-20 17:46:47 +00:00
<?php
namespace App\Controllers;
2021-08-13 07:17:24 +00:00
use App\Database\Repositories\UserRepository;
2019-11-20 17:46:47 +00:00
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpNotFoundException;
use ZipStream\Option\Archive;
use ZipStream\ZipStream;
2019-11-20 17:46:47 +00:00
class ClientController extends Controller
{
/**
* @param Request $request
* @param Response $response
* @param int $id
2019-11-20 17:49:31 +00:00
*
* @return Response
2019-11-20 17:46:47 +00:00
*/
public function getShareXConfig(Request $request, Response $response, int $id): Response
{
2021-08-13 07:17:24 +00:00
$user = make(UserRepository::class)->get($request, $id, true);
2019-11-20 17:46:47 +00:00
2021-08-13 07:17:24 +00:00
if (!$user->token) {
2019-11-20 17:46:47 +00:00
$this->session->alert(lang('no_upload_token'), 'danger');
2019-11-20 17:49:31 +00:00
2019-11-20 17:46:47 +00:00
return redirect($response, $request->getHeaderLine('Referer'));
}
2019-11-21 17:00:47 +00:00
$json = [
2019-11-20 17:46:47 +00:00
'DestinationType' => 'ImageUploader, TextUploader, FileUploader',
'RequestURL' => route('upload'),
'FileFormName' => 'upload',
'Arguments' => [
'file' => '$filename$',
'text' => '$input$',
2019-11-20 17:46:47 +00:00
'token' => $user->token,
2019-11-21 17:00:47 +00:00
],
'URL' => '$json:url$',
2019-11-20 17:46:47 +00:00
'ThumbnailURL' => '$json:url$/raw',
'DeletionURL' => '$json:url$/delete/'.$user->token,
2019-11-21 17:00:47 +00:00
];
2019-11-20 17:46:47 +00:00
return json($response, $json, 200, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
->withHeader('Content-Disposition', 'attachment;filename="'.$user->username.'-ShareX.sxcu"');
}
/**
* @param Request $request
* @param string|null $token
* @return Response
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
* @throws HttpNotFoundException
*/
public function getScreenCloudConfig(Request $request, string $token): Response
{
$user = $this->database->query('SELECT * FROM `users` WHERE `token` = ? LIMIT 1', $token)->fetch();
if (!$user) {
throw new HttpNotFoundException($request);
}
$config = [
'token' => $token,
'host' => route('root'),
];
ob_end_clean();
$options = new Archive();
$options->setSendHttpHeaders(true);
$zip = new ZipStream($user->username.'-screencloud.zip', $options);
$zip->addFileFromPath('main.py', BASE_DIR.'resources/uploaders/screencloud/main.py');
$zip->addFileFromPath('icon.png', BASE_DIR.'static/images/favicon-32x32.png');
$zip->addFileFromPath('metadata.xml', BASE_DIR.'resources/uploaders/screencloud/metadata.xml');
$zip->addFileFromPath('settings.ui', BASE_DIR.'resources/uploaders/screencloud/settings.ui');
$zip->addFile('config.json', json_encode($config, JSON_UNESCAPED_SLASHES));
$zip->finish();
exit(0);
}
2019-11-20 17:46:47 +00:00
/**
* @param Request $request
* @param Response $response
* @param int $id
2019-11-20 17:49:31 +00:00
*
* @return Response
2019-11-20 17:46:47 +00:00
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function getBashScript(Request $request, Response $response, int $id): Response
{
2021-08-13 07:17:24 +00:00
$user = make(UserRepository::class)->get($request, $id, true);
2019-11-20 17:46:47 +00:00
2021-08-13 07:17:24 +00:00
if (!$user->token) {
2019-11-20 17:46:47 +00:00
$this->session->alert(lang('no_upload_token'), 'danger');
2019-11-20 17:49:31 +00:00
2019-11-20 17:46:47 +00:00
return redirect($response, $request->getHeaderLine('Referer'));
}
return view()->render(
$response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_'.$user->username.'.sh"'),
2019-11-20 17:46:47 +00:00
'scripts/xbackbone_uploader.sh.twig',
2019-11-21 17:00:47 +00:00
[
'username' => $user->username,
2019-11-20 17:46:47 +00:00
'upload_url' => route('upload'),
'token' => $user->token,
2019-11-21 17:00:47 +00:00
]
2019-11-20 17:46:47 +00:00
);
}
2023-12-17 21:44:29 +00:00
/**
* @param Request $request
* @param Response $response
* @param int $id
*
* @return Response
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function getKDEScript(Request $request, Response $response, int $id): Response
{
$user = make(UserRepository::class)->get($request, $id, true);
if (!$user->token) {
$this->session->alert(lang('no_upload_token'), 'danger');
return redirect($response, $request->getHeaderLine('Referer'));
}
return view()->render(
$response->withHeader('Content-Disposition', 'attachment;filename="xbackbone_uploader_'.$user->username.'.sh"'),
'scripts/xbackbone_kde_uploader.sh.twig',
[
'username' => $user->username,
'upload_url' => route('upload'),
'token' => $user->token,
]
);
}
2019-11-20 17:49:31 +00:00
}