Skip to content

Commit

Permalink
Added Downloadable Windows Script
Browse files Browse the repository at this point in the history
Added downloadable 'Send to' Windows Context Menu script as requested in sergix44#580 . All .bat code provided by https://github.com/Sectly

Preview: https://senkawolf.fyi/me/RUgOWoQI51.png
  • Loading branch information
SenkaWolf committed Jan 19, 2025
1 parent 5fed0e7 commit 14cf9a6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
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

// 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;
$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;
$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

0 comments on commit 14cf9a6

Please sign in to comment.