Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit c1d5292

Browse files
committed
Improve output of phpinfo
1 parent 00228ee commit c1d5292

File tree

6 files changed

+111
-2
lines changed

6 files changed

+111
-2
lines changed

src/extension/ddappsec.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,20 @@ static PHP_MINFO_FUNCTION(ddappsec)
412412
PUTS("(c) Datadog 2021\n");
413413
php_info_print_box_end();
414414

415+
char *state;
416+
if (DDAPPSEC_G(enabled) == ENABLED) {
417+
state = "Enabled";
418+
} else if (DDAPPSEC_G(enabled) == DISABLED) {
419+
state = "Disabled";
420+
} else {
421+
state = "Not configured";
422+
}
423+
415424
php_info_print_table_start();
416-
php_info_print_table_row(2, "Datadog AppSec support",
417-
DDAPPSEC_G(enabled) == ENABLED ? "enabled" : "disabled");
425+
php_info_print_table_row(2,
426+
"Datadog AppSec status managed by remote config",
427+
DDAPPSEC_G(enabled_by_configuration) == NOT_CONFIGURED ? "Yes" : "No");
428+
php_info_print_table_row(2, "Datadog AppSec state", state);
418429
php_info_print_table_row(2, "Version", PHP_DDAPPSEC_VERSION);
419430
php_info_print_table_end();
420431

tests/extension/inc/phpinfo.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
function get_php_info_as_string() {
4+
ob_start();
5+
phpinfo();
6+
$variable = ob_get_contents();
7+
ob_get_clean();
8+
return $variable;
9+
}
10+
11+
function get_configuration_value($configuration) {
12+
$separator = "=>";
13+
$phpinfo = get_php_info_as_string();
14+
$start = strpos($phpinfo, $configuration);
15+
$start = strpos($phpinfo,$separator , $start);
16+
$end = strpos($phpinfo, "\n", $start);
17+
return trim(substr($phpinfo, $start + strlen($separator), $end - $start - strlen($separator)));
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Check enablement status by default
3+
--FILE--
4+
<?php
5+
include __DIR__ . '/inc/phpinfo.php';
6+
7+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
8+
var_dump(get_configuration_value("Datadog AppSec state"));
9+
10+
--EXPECT--
11+
string(3) "Yes"
12+
string(14) "Not configured"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Check enablement status when enabled by config
3+
--INI--
4+
datadog.appsec.enabled=1
5+
--FILE--
6+
<?php
7+
include __DIR__ . '/inc/phpinfo.php';
8+
9+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
10+
var_dump(get_configuration_value("Datadog AppSec state"));
11+
12+
--EXPECT--
13+
string(2) "No"
14+
string(7) "Enabled"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Check enablement status when disabled by config
3+
--INI--
4+
datadog.appsec.enabled=0
5+
--FILE--
6+
<?php
7+
include __DIR__ . '/inc/phpinfo.php';
8+
9+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
10+
var_dump(get_configuration_value("Datadog AppSec state"));
11+
12+
--EXPECT--
13+
string(2) "No"
14+
string(8) "Disabled"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
--TEST--
2+
Check enablement status when configured by remote config
3+
--FILE--
4+
<?php
5+
include __DIR__ . '/inc/phpinfo.php';
6+
use function datadog\appsec\testing\{rinit, rshutdown};
7+
8+
include __DIR__ . '/inc/mock_helper.php';
9+
$helper = Helper::createInitedRun([
10+
response_list(response_config_features(true)), //First rinit enables
11+
response_list(response_request_init(['ok'])), //Since it got enabled, it should call to request init
12+
response_list(response_request_shutdown(['ok', [], new ArrayObject(), new ArrayObject()])), //End of request
13+
response_list(response_config_features(false)), //Second call at rinit disabled it
14+
response_list(response_config_sync()), //Third call which does not change anything
15+
]);
16+
17+
//Enabled not configured
18+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
19+
var_dump(get_configuration_value("Datadog AppSec state"));
20+
rinit(); //On this rinit it gets enabled
21+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
22+
var_dump(get_configuration_value("Datadog AppSec state"));
23+
rshutdown();
24+
rinit(); //Second rinit. This time it got disabled
25+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
26+
var_dump(get_configuration_value("Datadog AppSec state"));
27+
rshutdown();
28+
rinit(); //Third rinit. Nothing changes
29+
rshutdown();
30+
var_dump(get_configuration_value("Datadog AppSec status managed by remote config"));
31+
var_dump(get_configuration_value("Datadog AppSec state"));
32+
--EXPECT--
33+
string(3) "Yes"
34+
string(14) "Not configured"
35+
string(3) "Yes"
36+
string(7) "Enabled"
37+
string(3) "Yes"
38+
string(8) "Disabled"
39+
string(3) "Yes"
40+
string(8) "Disabled"

0 commit comments

Comments
 (0)