Skip to content
← Back to Functions
Code

Debug Context Redactor

Builds debug context that preserves useful troubleshooting data while removing tokens, passwords, cookies, and secrets.

Function signature

ogBuildSafeDebugContext(context = array(), options = array())

Categories

  • Security

Parameters

contextRuntime context used for feature, cache, or debug decisions.optionsOptional documented policy controls for this helper.

Return value

Public-safe status string returned by the function for explicit controller branching or logging.

  • success
  • message
  • data

Compatibility

Existing function name, slug, path, and call order preserved; advertised metadata corrected to the actual source behavior.

Minimum PHP version: 7.4

Security notes

Use caller-owned allowlists and context-specific escaping; validate admin actions, export fields, privacy plans, cache keys, templates, settings, routes, and ecommerce policies before production use.

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Builds a debug context array with sensitive values redacted.
 *
 * Debug data should remain server-side unless a production-safe policy explicitly
 * allows a small public subset.
 *
 * @param array $context Raw debug context.
 * @param array $options Redaction options.
 * @return array Safe debug context.
 */
function ogBuildSafeDebugContext($context = array(), $options = array()) {
	$result = array(
		'success' => false,
		'message' => '',
		'data' => array()
	);

	if (!is_array($context)) {
		$result['message'] = 'Debug context must be an array.';
		return $result;
	}
	if (!is_array($options)) {
		$options = array();
	}

	$redact_keys = array('password', 'pass', 'token', 'secret', 'api_key', 'apikey', 'cookie', 'authorization', 'csrf', 'session');
	if (!empty($options['redact_keys']) && is_array($options['redact_keys'])) {
		$redact_keys = array_merge($redact_keys, $options['redact_keys']);
	}

	$safe = array();
	foreach ($context as $key => $value) {
		$key_string = strtolower((string)$key);
		$should_redact = false;
		foreach ($redact_keys as $redact_key) {
			$redact_key = strtolower((string)$redact_key);
			if (!empty($redact_key) && strpos($key_string, $redact_key) !== false) {
				$should_redact = true;
			}
		}

		if ($should_redact) {
			$safe[$key] = '[redacted]';
		} elseif (is_array($value)) {
			$safe[$key] = array();
			foreach ($value as $child_key => $child_value) {
				$child_key_string = strtolower((string)$child_key);
				$child_redact = false;
				foreach ($redact_keys as $redact_key) {
					$redact_key = strtolower((string)$redact_key);
					if (!empty($redact_key) && strpos($child_key_string, $redact_key) !== false) {
						$child_redact = true;
					}
				}
				if ($child_redact) {
					$safe[$key][$child_key] = '[redacted]';
				} else {
					$safe[$key][$child_key] = $child_value;
				}
			}
		} else {
			$safe[$key] = $value;
		}
	}

	$result['success'] = true;
	$result['message'] = 'Safe debug context built.';
	$result['data'] = array(
		'context' => $safe,
		'public_safe' => false
	);

	return $result;
}