55 *
66 * @package Plugin_API
77 * @author Shuaib Yusuf Shuaib
8- * @version 0.1 .0
8+ * @version 0.2 .0
99 */
1010
1111// Ensure the plugin is installed properly
@@ -552,7 +552,7 @@ function api_plugins(): void {
552552 array (
553553 'code ' => 200 ,
554554 'status ' => false ,
555- 'message ' => 'Plugin ' . ( $ App ->installed ( $ plugin ) ? ' already installed ' : ' not installed ' ),
555+ 'message ' => 'Plugin ' . ( $ App ->installed ( $ plugin ) ? 'already installed ' : 'not installed ' ),
556556 'data ' => array (
557557 'plugin ' => $ plugin
558558 )
@@ -613,15 +613,32 @@ function api_settings(): void {
613613 'status ' => true ,
614614 'message ' => 'Settings ' ,
615615 'data ' => api_field_selection (
616- $ data [ 'site ' ]
616+ api_hide_fields (
617+ $ data [ 'site ' ],
618+ array (
619+ 'username ' ,
620+ 'password ' ,
621+ 'api ' => array (
622+ 'write '
623+ )
624+ )
625+ )
617626 )
618627 )
619628 );
620629 } else if ( 'PUT ' === $ method ) {
621630 $ inputs = api_request ();
631+ $ old_theme = $ data [ 'site ' ][ 'theme ' ];
622632 $ inputs [ 'data ' ] = api_input_array ( 'data ' );
623633 $ data [ 'site ' ] = array_merge ( $ data [ 'site ' ], $ inputs [ 'data ' ] );
634+ $ keys = array_keys ( $ data [ 'site ' ] );
635+ $ data [ 'site ' ] = array_map ( fn ( $ value , $ key ) => in_array ( $ key , [ 'lang ' , 'title ' , 'subtitle ' , 'keywords ' , 'descr ' , 'footer ' ] ) ? htmlspecialchars ( $ value , ENT_QUOTES | ENT_HTML5 , 'UTF-8 ' , false ) : $ value , $ data [ 'site ' ], $ keys );
636+ $ data [ 'site ' ] = array_combine ( $ keys , $ data [ 'site ' ] );
624637 if ( $ App ->save ( $ data ) ) {
638+ $ new_theme = $ data [ 'site ' ][ 'theme ' ];
639+ if ( $ new_theme !== $ old_theme ) {
640+ $ App ->get_action ( 'change_theme ' , $ new_theme );
641+ }
625642 api_response (
626643 array (
627644 'code ' => 200 ,
@@ -873,7 +890,7 @@ function api_pagination( array $data ): array {
873890 $ max = $ App ->get ( 'api ' )[ 'limit ' ];
874891 $ offset = api_input_integer ( 'offset ' , 0 );
875892 $ limit = api_input_integer ( 'limit ' , $ max );
876- $ limit = ( $ limit > $ max ? $ max : $ limit );
893+ $ limit = ( $ limit > $ max ? $ max : $ limit );
877894 $ args = [ $ data , $ offset , $ limit , true ];
878895 return array_slice ( ...$ args );
879896}
@@ -894,6 +911,31 @@ function api_field_selection( array $data ): array {
894911 return $ data ;
895912}
896913
914+ /**
915+ * Hide sensitive data
916+ * @param array $data
917+ * @param array $fields
918+ * @param bool $allow_auth
919+ * @return array
920+ */
921+ function api_hide_fields ( array $ data , array $ fields , bool $ allow_auth = true ): array {
922+ global $ App ;
923+ $ api = $ App ->get ( 'api ' );
924+ $ auth = api_input_string ( 'auth ' );
925+ $ valid = hash_equals ( $ api [ 'write ' ], $ auth );
926+ if ( $ allow_auth && $ valid ) return $ data ;
927+ foreach ( $ fields as $ index => $ field ) {
928+ if ( is_array ( $ field ) ) {
929+ $ data [ $ index ] = api_hide_fields ( $ data [ $ index ], $ field );
930+ } else {
931+ if ( isset ( $ data [ $ field ] ) ) {
932+ $ data [ $ field ] = '<hidden> ' ;
933+ }
934+ }
935+ }
936+ return $ data ;
937+ }
938+
897939/**
898940 * API string input
899941 * @param string $index
@@ -986,8 +1028,9 @@ function api_data_input_bool( string $index, bool $default = false ): bool {
9861028 */
9871029function api_rate_limit (): void {
9881030 global $ App ;
1031+ $ slug = ( $ App ->page === 'api/ ' . API_VERSION . '/ ' ? $ App ->page : rtrim ( $ App ->page , '/ ' ) );
9891032 if ( 0 === ( $ limit = $ App ->get ( 'api ' )[ 'rate ' ] ) ) return ;
990- $ data = ( $ _SESSION [ 'api ' ][ $ App -> page ] ?? null );
1033+ $ data = ( $ _SESSION [ 'api ' ][ $ slug ] ?? null );
9911034 $ data = ( $ data ?? [ 'hits ' => 0 , 'time ' => time () ] );
9921035 $ elapsed = ( time () - $ data [ 'time ' ] );
9931036 if ( $ elapsed > 60 ) {
@@ -1010,7 +1053,7 @@ function api_rate_limit(): void {
10101053 );
10111054 }
10121055 }
1013- $ _SESSION [ 'api ' ][ $ App -> page ] = $ data ;
1056+ $ _SESSION [ 'api ' ][ $ slug ] = $ data ;
10141057}
10151058
10161059/**
0 commit comments