| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: read NotificationX global settings and module state. |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Read |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Read; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
use NotificationX\Core\Modules; |
| 12 |
use NotificationX\Admin\Settings; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns global settings and per-module enabled state. Anything that looks |
| 20 |
* like a secret (key/secret/token/password) is redacted before it leaves. |
| 21 |
*/ |
| 22 |
class GetSettings extends AbilityBase { |
| 23 |
|
| 24 |
protected $id = 'notificationx/get-settings'; |
| 25 |
protected $label = 'Get settings'; |
| 26 |
protected $description = 'Get NotificationX global settings and the enabled/disabled state of each module. Secret values (API keys, tokens, passwords) are redacted.'; |
| 27 |
|
| 28 |
public function input_schema() { |
| 29 |
return array( |
| 30 |
'type' => 'object', |
| 31 |
'properties' => (object) array(), |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function output_schema() { |
| 36 |
return array( |
| 37 |
'type' => 'object', |
| 38 |
'properties' => array( |
| 39 |
'modules' => array( 'type' => 'array' ), |
| 40 |
'settings' => array( 'type' => 'object' ), |
| 41 |
), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
public function execute( $input ) { |
| 46 |
// Module state. |
| 47 |
$modules_list = array(); |
| 48 |
$registered = Modules::get_instance()->get_all(); |
| 49 |
if ( is_array( $registered ) ) { |
| 50 |
foreach ( $registered as $key => $module ) { |
| 51 |
$value = is_array( $module ) && isset( $module['value'] ) ? $module['value'] : $key; |
| 52 |
$label = is_array( $module ) && isset( $module['label'] ) ? $module['label'] : $value; |
| 53 |
$modules_list[] = array( |
| 54 |
'id' => $value, |
| 55 |
'label' => $label, |
| 56 |
'enabled' => (bool) Modules::get_instance()->is_enabled( $value ), |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// General settings, with secrets redacted. |
| 62 |
$settings = (array) Settings::get_instance()->get( 'settings' ); |
| 63 |
unset( $settings['modules'] ); // already reported above |
| 64 |
$settings = $this->redact( $settings ); |
| 65 |
|
| 66 |
return array( |
| 67 |
'modules' => $modules_list, |
| 68 |
'settings' => $settings, |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Recursively redact values whose key suggests a credential. |
| 74 |
* |
| 75 |
* @param array $data Settings array. |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
protected function redact( $data ) { |
| 79 |
$needles = array( 'key', 'secret', 'token', 'password', 'client_id', 'auth', 'nonce' ); |
| 80 |
foreach ( $data as $key => $value ) { |
| 81 |
if ( is_array( $value ) ) { |
| 82 |
$data[ $key ] = $this->redact( $value ); |
| 83 |
continue; |
| 84 |
} |
| 85 |
$lower = strtolower( (string) $key ); |
| 86 |
foreach ( $needles as $needle ) { |
| 87 |
if ( false !== strpos( $lower, $needle ) && '' !== (string) $value ) { |
| 88 |
$data[ $key ] = '***redacted***'; |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
return $data; |
| 94 |
} |
| 95 |
} |
| 96 |
|