Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Downloadable Windows Script #626

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions app/Controllers/ClientController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,83 @@ public function getShareXConfig(Request $request, Response $response, int $id):
return json($response, $json, 200, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)
->withHeader('Content-Disposition', 'attachment;filename="' . $fileName . '"');
}

/**
* @param Request $request
* @param Response $response
* @param int $id
*
* @return Response
*/
public function getWindowsConfig(Request $request, Response $response, int $id): Response
{
// Retrieve the user
$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'));
}

// Load the app name from your config.php file
$config = include __DIR__ . '/../../config.php'; // Adjust the path as necessary
$appName = $config['app_name'] ?? 'XBackBone'; // Provide a default if not set
Comment on lines +74 to +75
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 issue: ‏config must be fetch from the container, as it can be altered during bootstrapping. The config file is never read during the app runtime


// Define the file base name and extension
$fileBaseName = $user->username . " - 'Send to' Windows Context Menu Script"; // Base file name without extension
$fileName = $fileBaseName . '.bat'; // Full file name with extension

// Prepare the content of the .txt file
$batscript = '@echo off' . PHP_EOL;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 issue: ‏Can you please put the script in a proper template like for the others? This is very hard to understand and maintain

$batscript .= 'setlocal' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= 'if "%~1"=="" (' . PHP_EOL;
$batscript .= ' call :CreateSendToShortcut' . PHP_EOL;
$batscript .= ' exit /b' . PHP_EOL;
$batscript .= ')' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= 'set "Token=' .$user->token. '"' . PHP_EOL;
$batscript .= 'set "UploadUrl=' .route('upload'). '"' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= ':UploadToXBackBone' . PHP_EOL;
$batscript .= 'curl -k -s -F "token=%Token%" -F "upload=@%1" %UploadUrl%' . PHP_EOL;
$batscript .= 'echo.' . PHP_EOL;
$batscript .= 'if %errorlevel% neq 0 (' . PHP_EOL;
$batscript .= ' echo Upload failed. Curl error code: %errorlevel%' . PHP_EOL;
$batscript .= ') else (' . PHP_EOL;
$batscript .= ' echo File uploaded successfully to ' .$appName.'.' . PHP_EOL;
$batscript .= ')' . PHP_EOL;
$batscript .= 'exit /b' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= ':CreateSendToShortcut' . PHP_EOL;
$batscript .= 'set "ShortcutName=Upload to ' .$appName. ' (@'.$user->username.').lnk"' . PHP_EOL;
$batscript .= 'set "ShortcutPath=%APPDATA%\\Microsoft\\Windows\\SendTo\\%ShortcutName%"' . PHP_EOL;
$batscript .= 'set "IconUrl=https://xbackbone.app/favicon.ico"' . PHP_EOL;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 issue: ‏This shuld point to the current instance, not the xbb website.

$batscript .= 'set "IconPath=%APPDATA%\\Microsoft\\Windows\\SendTo\\xbackbone.ico"' . PHP_EOL;
$batscript .= 'set "ScriptPath=%~dp0%~nx0"' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= 'curl -k -s -o "%IconPath%" "%IconUrl%" > nul 2>&1' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= 'echo Set oWS = WScript.CreateObject("WScript.Shell") > CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo sLinkFile = "%ShortcutPath%" >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo Set oLink = oWS.CreateShortcut(sLinkFile) >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo oLink.TargetPath = "cmd.exe" >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo oLink.Arguments = "/c ""%ScriptPath%""" >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo oLink.IconLocation = "%IconPath%" >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'echo oLink.Save >> CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'cscript /nologo CreateShortcut.vbs' . PHP_EOL;
$batscript .= 'del CreateShortcut.vbs' . PHP_EOL;
$batscript .= '' . PHP_EOL;
$batscript .= 'echo Send To shortcut created: %ShortcutPath%' . PHP_EOL;
$batscript .= 'exit /b' . PHP_EOL;

// Return the .txt file as a downloadable response
$response->getBody()->write($batscript);

return $response
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment;filename="' . $fileName . '"');
}

/**
* @param Request $request
Expand Down
1 change: 1 addition & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
$group->post('/profile/{id}', [ProfileController::class, 'profileEdit'])->setName('profile.update');
$group->post('/user/{id}/refreshToken', [UserController::class, 'refreshToken'])->setName('refreshToken');
$group->get('/user/{id}/config/sharex', [ClientController::class, 'getShareXConfig'])->setName('config.sharex');
$group->get('/user/{id}/config/windows', [ClientController::class, 'getWindowsConfig'])->setName('config.windows');
$group->get('/user/{id}/config/script', [ClientController::class, 'getBashScript'])->setName('config.script');
$group->get('/user/{id}/config/kde_script', [ClientController::class, 'getKDEScript'])->setName('kde_config.script');

Expand Down
3 changes: 2 additions & 1 deletion resources/templates/user/edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@
<div class="col-sm-9">
<div class="btn-group">
<a href="{{ route('config.sharex', {'id': user.id}) }}" class="btn btn-lg btn-outline-dark"><i class="fas fa-fw fa-download"></i> ShareX</a>
<a href="javascript:alert('{{ lang('copied') }}')" data-clipboard-text="{{ route('config.screencloud', {'token': user.token}) }}" class="btn btn-lg btn-outline-info btn-clipboard"><i class="fas fa-fw fa-download"></i> Screencloud</a>
<a href="javascript:alert('{{ lang('copied') }}')" data-clipboard-text="{{ route('config.screencloud', {'token': user.token}) }}" class="btn btn-lg btn-outline-info btn-clipboard"><i class="fas fa-fw fa-copy"></i> Screencloud</a>
<a href="{{ route('config.windows', {'id': user.id}) }}" type="button" class="btn btn-lg btn-outline-primary"><i class="fas fa-fw fa-download"></i> Windows</a>
<a href="{{ route('config.script', {'id': user.id}) }}" type="button" class="btn btn-lg btn-outline-danger"><i class="fas fa-fw fa-download"></i> Linux Script</a>
<button type="button" class="btn btn-outline-danger dropdown-toggle dropdown-toggle-split" id="userDropdown" data-toggle="dropdown" aria-expanded="false"></button>
<ul class="dropdown-menu">
Expand Down
Loading