Update index.js

This commit is contained in:
fanategorius
2026-07-06 16:27:52 +03:00
committed by GitHub
parent b98df89971
commit b6ea7b9d92
+85 -76
View File
@@ -1,92 +1,101 @@
(function(OC, window, $, undefined) {
'use strict';
<?php
$(document).ready(function() {
var sqlMode = false;
namespace OCA\OCCWeb\Controller;
var term = $('body').terminal(function(command, term) {
if (command === '') {
return;
use OCP\AppFramework\Controller;
use OCP\IRequest;
use OCP\IDBConnection;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IGroupManager;
use OCP\IUserSession;
class DbController extends Controller
{
private $db;
private $groupManager;
private $userSession;
public function __construct(
$AppName,
IRequest $request,
IDBConnection $db,
IGroupManager $groupManager,
IUserSession $userSession
) {
parent::__construct($AppName, $request);
$this->db = $db;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
}
// Команды переключения режимов
if (command === ':sql') {
sqlMode = true;
term.set_prompt('sql $ ');
term.echo('SQL mode enabled. Type :occ to return to OCC mode.');
return;
/**
* @NoCSRFRequired
*/
public function query()
{
// Проверка прав администратора
$user = $this->userSession->getUser();
if (!$user) {
return new JSONResponse(['error' => 'Not authenticated'], 401);
}
if (command === ':occ') {
sqlMode = false;
term.set_prompt('occ $ ');
term.echo('OCC mode enabled.');
return;
if (!$this->groupManager->isAdmin($user->getUID())) {
return new JSONResponse(['error' => 'Admin privileges required'], 403);
}
if (command === ':help') {
term.echo('Available commands:');
term.echo(' :sql - Switch to SQL mode');
term.echo(' :occ - Switch to OCC mode');
term.echo(' :help - Show this help');
term.echo('');
term.echo('In OCC mode: execute Nextcloud occ commands');
term.echo('In SQL mode: execute SQL queries directly');
return;
$sql = $this->request->getParam('sql', '');
if (empty(trim($sql))) {
return new JSONResponse(['success' => false, 'error' => 'Empty query']);
}
term.pause();
// Разделяем запросы по точке с запятой. Все запросы выполняются
// последовательно на одном и том же соединении с БД (в рамках
// одного HTTP-запроса), поэтому SET сохраняет своё значение
// для current_setting() в последующих запросах пачки.
$queries = array_values(array_filter(array_map('trim', explode(';', $sql)), function ($q) {
return $q !== '';
}));
if (sqlMode) {
// SQL режим
$.post(OC.generateUrl('/apps/occweb/db/query'), {
sql: command
}, function(response) {
if (response.success) {
response.results.forEach(function(result) {
if (result.type === 'select') {
term.echo('Query: ' + result.query);
term.echo('Rows: ' + result.count);
if (result.data.length > 0) {
term.echo(JSON.stringify(result.data, null, 2));
$results = [];
foreach ($queries as $query) {
$isSelect = stripos($query, 'SELECT') === 0;
$isSet = stripos($query, 'SET ') === 0;
try {
$stmt = $this->db->prepare($query);
$stmt->execute();
if ($isSelect) {
$rows = $stmt->fetchAll();
$results[] = [
'query' => $query,
'type' => 'select',
'count' => count($rows),
'data' => $rows
];
} else {
term.echo('(no results)');
$affected = $stmt->rowCount();
$results[] = [
'query' => $query,
'type' => $isSet ? 'set' : 'write',
'affected_rows' => $affected
];
}
} else if (result.type === 'write') {
term.echo('Query: ' + result.query);
term.echo('Affected rows: ' + result.affected_rows);
} else if (result.type === 'set') {
term.echo('Skipped: ' + result.query);
} catch (\Exception $e) {
$results[] = [
'query' => $query,
'type' => 'error',
'error' => $e->getMessage()
];
// Останавливаемся на первой ошибке: не продолжаем выполнять
// оставшиеся запросы пачки (например, серию DELETE),
// если один из предыдущих шагов не выполнился.
break;
}
term.echo('');
});
} else {
term.echo('ERROR: ' + response.error);
}
term.resume();
}).fail(function(xhr, status, error) {
term.echo('ERROR: Request failed - ' + error);
term.resume();
});
} else {
// OCC режим
$.post(OC.generateUrl('/apps/occweb/cmd'), {
command: command
}, function(response) {
term.echo('\n' + response).resume();
}).fail(function(xhr, status, error) {
term.echo('ERROR: Request failed - ' + error);
term.resume();
});
}
}, {
prompt: 'occ $ ',
name: 'occweb',
greetings: 'Nextcloud OCC Web Terminal\nType :help for available commands\n',
onBlur: function() {
return false;
}
});
});
})(OC, window, jQuery);
return new JSONResponse(['success' => true, 'results' => $results]);
}
}