<?php
$_REQUEST = [
"username" => "admin",
"password" => "pass",
"credit_card" => "000000"
];
$_SESSION = [
"id" => 1
];
function create_log($filename, $log)
{
// Copia as superglobais para variáveis locais:
$request = $_REQUEST;
$session = $_SESSION;
// Define quais são as informações sensíveis:
$filter = ["password", "credit_card"];
// Filtra as informações sensíveis:
$request = array_filter($request, function ($key) use ($filter) { }, ARRAY_FILTER_USE_KEY);
$session = array_filter($session, function ($key) use ($filter) { }, ARRAY_FILTER_USE_KEY);
// Gera a mensagem de log:
// file_put_contents(
// $filename.'.log', '[START]'.
// json_encode($request).PHP_EOL.
// json_encode($session).PHP_EOL.
// "Log ".$log.PHP_EOL.'[END]'.PHP_EOL
// , FILE_APPEND );
}
create_log("foo", "Mensagem de log");
// Variáveis globais não foram alteradas:
PD9waHAKCiRfUkVRVUVTVCA9IFsKICAgICJ1c2VybmFtZSIgPT4gImFkbWluIiwKICAgICJwYXNzd29yZCIgPT4gInBhc3MiLAogICAgImNyZWRpdF9jYXJkIiA9PiAiMDAwMDAwIgpdOwoKJF9TRVNTSU9OID0gWwogICAgImlkIiA9PiAxCl07CgpmdW5jdGlvbiBjcmVhdGVfbG9nKCRmaWxlbmFtZSwgJGxvZykKewogICAgLy8gQ29waWEgYXMgc3VwZXJnbG9iYWlzIHBhcmEgdmFyacOhdmVpcyBsb2NhaXM6CiAgICAkcmVxdWVzdCA9ICRfUkVRVUVTVDsKICAgICRzZXNzaW9uID0gJF9TRVNTSU9OOwoKICAgIC8vIERlZmluZSBxdWFpcyBzw6NvIGFzIGluZm9ybWHDp8O1ZXMgc2Vuc8OtdmVpczoKICAgICRmaWx0ZXIgPSBbInBhc3N3b3JkIiwgImNyZWRpdF9jYXJkIl07CgogICAgLy8gRmlsdHJhIGFzIGluZm9ybWHDp8O1ZXMgc2Vuc8OtdmVpczoKICAgICRyZXF1ZXN0ID0gYXJyYXlfZmlsdGVyKCRyZXF1ZXN0LCBmdW5jdGlvbiAoJGtleSkgdXNlICgkZmlsdGVyKSB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