2323
2424class admin_controller
2525{
26+ protected const FORM_KEY = 'acp_pwakit ' ;
27+
2628 /** @var string $id */
2729 public string $ id ;
2830
@@ -122,45 +124,65 @@ public function main(string $id, string $mode = ''): void
122124 {
123125 $ this ->id = $ id ;
124126
125- if ($ mode !== 'settings ' )
127+ if ($ mode !== 'settings ' )
126128 {
127129 return ;
128130 }
129131
130- $ form_key = 'acp_pwakit ' ;
131- add_form_key ($ form_key );
132+ add_form_key (self ::FORM_KEY );
132133
133- $ submit = $ this ->request ->is_set_post ('submit ' );
134- $ upload = $ this ->request ->is_set_post ('upload ' );
135- $ resync = $ this ->request ->is_set_post ('resync ' );
136- $ delete = $ this ->request ->is_set_post ('delete ' );
134+ $ action = $ this ->get_action ();
137135
138- if ($ submit || $ upload || $ resync )
136+ if ($ action )
139137 {
140- if (!check_form_key ($ form_key ))
141- {
142- $ this ->error ($ this ->language ->lang ('FORM_INVALID ' ));
143- }
138+ $ this ->execute_action ($ action );
139+ }
144140
145- if ($ upload )
146- {
147- $ this ->upload ();
148- }
149- else if ($ resync )
150- {
151- $ this ->helper ->resync_icons ();
152- }
153- else
141+ $ this ->display_settings ();
142+ }
143+
144+ /**
145+ * Get the action from the request. We need to check is_set_post() for all actions
146+ *
147+ * @return string|null
148+ */
149+ protected function get_action (): string |null
150+ {
151+ $ actions = ['submit ' , 'resync ' , 'upload ' , 'delete ' ];
152+ foreach ($ actions as $ action )
153+ {
154+ if ($ this ->request ->is_set_post ($ action ))
154155 {
155- $ this -> save_settings () ;
156+ return $ action ;
156157 }
157158 }
158- else if ($ delete )
159+ return null ;
160+ }
161+
162+ /**
163+ * Execute the action
164+ *
165+ * @param string $action
166+ * @return void
167+ */
168+ protected function execute_action (string $ action ): void
169+ {
170+ // Actions that require form key validation (not using confirm_box())
171+ $ form_key_actions = ['submit ' , 'resync ' , 'upload ' ];
172+
173+ // Check form key validation
174+ if (in_array ($ action , $ form_key_actions , true ) && !check_form_key (self ::FORM_KEY ))
159175 {
160- $ this ->delete ( );
176+ $ this ->error ( ' FORM_INVALID ' );
161177 }
162178
163- $ this ->display_settings ();
179+ // Using match expression (PHP 8.0+)
180+ match ($ action ) {
181+ 'submit ' => $ this ->save_settings (),
182+ 'resync ' => $ this ->helper ->resync_icons (),
183+ 'upload ' => $ this ->upload (),
184+ 'delete ' => $ this ->delete (),
185+ };
164186 }
165187
166188 /**
@@ -176,8 +198,8 @@ protected function display_settings(): void
176198 'PWA_IMAGES_DIR ' => $ this ->helper ->get_storage_path (),
177199 'PWA_KIT_ICONS ' => $ this ->helper ->get_icons ($ this ->phpbb_root_path ),
178200 'STYLES ' => $ this ->get_styles (),
179- 'U_BOARD_SETTINGS ' => append_sid ("{$ this ->phpbb_admin_path }index. $ this ->php_ext " , " i=acp_board&mode=settings " ),
180- 'U_STORAGE_SETTINGS ' => append_sid ("{$ this ->phpbb_admin_path }index. $ this ->php_ext " , " i=acp_storage&mode=settings " ),
201+ 'U_BOARD_SETTINGS ' => append_sid ("{$ this ->phpbb_admin_path }index. $ this ->php_ext " , ' i=acp_board&mode=settings ' ),
202+ 'U_STORAGE_SETTINGS ' => append_sid ("{$ this ->phpbb_admin_path }index. $ this ->php_ext " , ' i=acp_storage&mode=settings ' ),
181203 'U_ACTION ' => $ this ->u_action ,
182204 ]);
183205
@@ -200,8 +222,7 @@ protected function save_settings(): void
200222 $ pwa_bg_color = $ this ->request ->variable ('pwa_bg_color_ ' . $ style_id , '' );
201223 $ pwa_theme_color = $ this ->request ->variable ('pwa_theme_color_ ' . $ style_id , '' );
202224
203- $ updates [] = [
204- 'style_id ' => $ style_id ,
225+ $ updates [$ style_id ] = [
205226 'pwa_bg_color ' => $ this ->validate_hex_color ($ pwa_bg_color ) ? $ pwa_bg_color : $ row ['pwa_bg_color ' ],
206227 'pwa_theme_color ' => $ this ->validate_hex_color ($ pwa_theme_color ) ? $ pwa_theme_color : $ row ['pwa_theme_color ' ],
207228 ];
@@ -391,21 +412,20 @@ protected function get_styles(): array
391412 /**
392413 * Set style data in the styles table
393414 *
394- * @param array $data
415+ * @param array $rows Array of style table data to update; style_id is key
395416 * @return void
396417 */
397- protected function set_styles (array $ data ): void
418+ protected function set_styles (array $ rows ): void
398419 {
399- if (!empty ($ data ))
420+ if (!empty ($ rows ))
400421 {
401422 $ this ->db ->sql_transaction ('begin ' );
402423
403- foreach ($ data as $ row )
424+ foreach ($ rows as $ style_id => $ row )
404425 {
405- $ sql = 'UPDATE ' . STYLES_TABLE . "
406- SET pwa_bg_color = ' " . $ this ->db ->sql_escape ($ row ['pwa_bg_color ' ]) . "',
407- pwa_theme_color = ' " . $ this ->db ->sql_escape ($ row ['pwa_theme_color ' ]) . "'
408- WHERE style_id = " . (int ) $ row ['style_id ' ];
426+ $ sql = 'UPDATE ' . STYLES_TABLE . '
427+ SET ' . $ this ->db ->sql_build_array ('UPDATE ' , $ row ) . '
428+ WHERE style_id = ' . (int ) $ style_id ;
409429 $ this ->db ->sql_query ($ sql );
410430 }
411431
0 commit comments