Files
QRForge-selfhosted/src/bulk_action.php
T
dillard b5ef4ac0cb Admin-scoped user accounts, secure qr code storage, PHP 8.4 upgrade
Admin-scoped users (answers: who can create a 'user' account, only super or
also an admin within their own scope?):
- New owner_admin_id column on users (migration 004). NULL means created by
  super (company-wide, previous behavior); otherwise scoped to that admin's
  own codes.
- Users::addUser/editUser/deleteUser now allow an 'admin' session, but force
  type='user' and owner_admin_id to their own id regardless of submitted
  input. user.php/users.php open up to admins with a restricted UI (no type
  picker, listing limited to their own created users).
- New qr_compute_scope_owner_id()/qr_apply_owner_scope()/qr_has_full_visibility()
  helpers in includes/security.php, replacing the ad-hoc type==='admin' checks
  in index.php, dynamic_qrcodes.php, static_qrcodes.php and bulk_action.php.
  A 'user' account created by an admin is now scoped to that admin's codes
  instead of seeing everything company-wide.

Qr code storage hardening: images were served as plain static files under the
document root with no auth check at all. Storage now lives outside the web
root; qrcode_image.php and qrcode_zip_download.php gate access with the same
permission model as the list pages, and the bulk zip download is bound to the
session that generated it.

PHP 8.4 + chillerlan/php-qrcode 6.0.1: bumped since this is a dockerized app,
so the PHP version shipped doesn't matter to end users. Note: the 6.0.1 tag
itself only requires PHP 8.2 - the earlier "needs 8.4" read was from an
unpinned clone of master, which has since moved past the tag. Fixed along the
way, surfaced by testing on 8.4:
- The hardcoded Imagick build (an old pinned master commit, workaround for
  3.7.0 being broken on PHP 8.3+) no longer compiles on 8.4. Imagick 3.8.1 is
  now a normal stable release, so the workaround is gone.
- config.php had display_errors=On + error_reporting(E_ALL), so PHP 8.4's new
  deprecation notices got dumped straight into the response before
  session_start() could run, breaking login outright. Also an info-disclosure
  risk on its own. Now logged instead of displayed.
- MysqliDb::insertMulti() had an implicit nullable parameter, now explicit.
- includes/auth_validate.php redirected unauthenticated requests but never
  called exit(), so the rest of the script kept running.
- Dockerfile.fpm was missing both git (needed to clone chillerlan/php-qrcode)
  and the imagick extension entirely.

Also removes the unused sample qr code images that shipped in the original
repo; storage now lives outside the document root so they were never going
to be served again.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-08 20:38:58 +02:00

133 lines
4.0 KiB
PHP

<?php
require_once 'includes/bootstrap.php';
require_once BASE_PATH . '/includes/auth_validate.php';
require_once BASE_PATH . '/lib/DynamicQrcode/DynamicQrcode.php';
require_once BASE_PATH . '/lib/StaticQrcode/StaticQrcode.php';
header('Content-Type: application/json');
csrf_verify_header_or_die();
$allowed_types = ['dynamic', 'static'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$db = getDbInstance();
$json = json_decode(file_get_contents('php://input'), true);
if($json["action"] == "download") {
$params = $json['params'];
$files = [];
if (isset($json['type']) && in_array($json['type'], $allowed_types, true)) {
$type = $json['type'];
} else {
echo json_encode([
'data' => 'Type action field in the request.',
'status' => 400
]);
exit();
}
if (count($params) == 0) {
echo json_encode([
'data' => 'No qrcodes were selected.',
'status' => 400
]);
exit();
}
if ($_SESSION['type'] === 'user') {
$view_flag = $type === 'dynamic' ? 'can_view_dynamic' : 'can_view_static';
if (empty($_SESSION[$view_flag] ?? null)) {
http_response_code(403);
echo json_encode(['data' => 'Not allowed to view this qr code type.', 'status' => 403]);
exit();
}
}
foreach ($params as $param) {
$db->where('id', $param);
qr_apply_owner_scope($db);
$row = $db->getOne("{$type}_qrcodes");
if ($row !== NULL) {
$files[] = SAVED_QRCODE_DIRECTORY . $row['qrcode'];
}
}
$zip = new ZipArchive();
$zip_filename = 'qrcodes_' . uniqid() . '.zip';
$zip_path = SAVED_QRCODE_DIRECTORY . 'zip/' . $zip_filename;
@unlink($zip_path);
$zip->open($zip_path, ZipArchive::CREATE);
foreach ($files as $file) {
$download_file = @file_get_contents($file, true);
$zip->addFromString(basename($file), $download_file);
}
$zip->close();
// Proof-of-generation: only this session may download this specific zip file.
$_SESSION['generated_zips'][] = $zip_filename;
audit_log('bulk_download', $type, implode(',', $params));
echo json_encode([
'data' => 'qrcode_zip_download.php?file=' . rawurlencode($zip_filename),
'status' => 200
]);
exit();
} else if($json["action"] == "delete") {
if ($_SESSION['type'] === 'user') {
http_response_code(403);
echo json_encode(['data' => 'The "user" role is read-only.', 'status' => 403]);
exit();
}
$params = $json['params'];
if (isset($json['type']) && in_array($json['type'], $allowed_types, true)) {
$type = $json['type'];
} else {
echo json_encode([
'data' => 'Type action field in the request.',
'status' => 400
]);
exit();
}
if (count($params) == 0) {
echo json_encode([
'data' => 'No qrcodes were selected.',
'status' => 400
]);
exit();
}
if($type == "dynamic")
$instance = new DynamicQrcode();
else
$instance = new StaticQrcode();
foreach ($params as $param) {
$instance->deleteQrcode($param, true);
}
audit_log('bulk_delete', $type, implode(',', $params));
echo json_encode([
'action' => "delete",
'data' => "Qrcode deleted",
'status' => 200
]);
exit();
} else {
echo json_encode(['data' => 'Action not allowed', 'status' => 400]);
exit();
}
} else {
http_response_code(405);
echo json_encode(['data' => 'Direct access to this script not allowed.', 'status' => 405]);
exit();
}