55 *
66 * @copyright (c) 2022, Thorsten Ahlers
77 * @license GNU General Public License, version 2 (GPL-2.0)
8+ *
89 */
910
1011namespace imcger \imgupload \controller ;
1112
1213/**
13- * @ignore
14- */
15-
16- /**
17- * Main controller
14+ * Ajax main controller
1815 */
1916class save_rotated_img_controller
2017{
@@ -39,20 +36,26 @@ class save_rotated_img_controller
3936 /** @var \phpbb\extension\manager */
4037 protected $ ext_manager ;
4138
39+ /** @var \phpbb\filesystem\filesystem */
40+ protected $ filesystem ;
41+
4242 /** @var string phpBB root path */
4343 protected $ root_path ;
4444
4545 /** @var string phpEx */
4646 protected $ php_ext ;
4747
4848 /**
49+ * Constructor for ajax controller
50+ *
4951 * @param \phpbb\config\config $config
5052 * @param \phpbb\user $user
5153 * @param \phpbb\request\request $request
5254 * @param \phpbb\db\driver\driver_interface $db
5355 * @param \phpbb\auth\auth $auth
5456 * @param \phpbb\language\language $language
5557 * @param \phpbb\extension\manager $ext_manager
58+ * @param \phpbb\filesystem\filesystem $filesystem
5659 * @param string $root_path
5760 * @param string $php_ext
5861 */
@@ -64,6 +67,7 @@ public function __construct(
6467 \phpbb \auth \auth $ auth ,
6568 \phpbb \language \language $ language ,
6669 \phpbb \extension \manager $ ext_manager ,
70+ \phpbb \filesystem \filesystem $ filesystem ,
6771 $ root_path ,
6872 $ php_ext
6973 )
@@ -75,6 +79,7 @@ public function __construct(
7579 $ this ->auth = $ auth ;
7680 $ this ->language = $ language ;
7781 $ this ->ext_manager = $ ext_manager ;
82+ $ this ->filesystem = $ filesystem ;
7883 $ this ->root_path = $ root_path ;
7984 $ this ->php_ext = $ php_ext ;
8085
@@ -85,9 +90,12 @@ public function __construct(
8590 /**
8691 * Rotate Image with ImageMagick
8792 *
88- * @var string $data String contain attach id and rotate degree
93+ * @var int attach_id contain attach id
94+ * @var int img_rotate_deg contain rotate degree
95+ * @var int creation_time creation time of token
96+ * @var string form_token form token
8997 *
90- * @return array Json arry with old and new attach id or error message
98+ * @return array Json arry with status, old and new attach id, new file size or error message
9199 */
92100 public function save_image ()
93101 {
@@ -103,14 +111,21 @@ public function save_image()
103111 $ this ->json_response (3 );
104112 }
105113
106- $ img_attach_id = $ this ->request ->variable ('attach_id ' , '' );
107- $ img_rotate_deg = $ this ->request ->variable ('img_rotate_deg ' , '' );
108-
109114 // Get name of the extension
110115 $ metadata_manager = $ this ->ext_manager ->create_extension_metadata_manager ('imcger/imgupload ' );
111116 $ ext_display_name = $ metadata_manager ->get_metadata ('display-name ' );
112117
113- if (!$ img_attach_id || !$ img_rotate_deg )
118+ // Check form token
119+ if (!check_form_key ('posting ' ))
120+ {
121+ $ this ->json_response (5 , $ ext_display_name , $ this ->language ->lang ('FORM_INVALID ' ));
122+ }
123+
124+ // Get variable, accept only integer
125+ $ img_attach_id = intval ($ this ->request ->variable ('attach_id ' , '' ));
126+ $ img_rotate_deg = intval ($ this ->request ->variable ('img_rotate_deg ' , '' ));
127+
128+ if (!$ img_attach_id || $ img_rotate_deg < 1 || $ img_rotate_deg > 360 )
114129 {
115130 $ this ->json_response (5 , $ ext_display_name , $ this ->language ->lang ('IUL_WRONG_PARAM ' ));
116131 }
@@ -135,7 +150,7 @@ public function save_image()
135150 $ image_file_path = $ this ->root_path . trim ($ this ->config ['upload_path ' ], '/ ' ) . '/ ' . $ img_data ['physical_filename ' ];
136151 $ thumb_file_path = $ this ->root_path . trim ($ this ->config ['upload_path ' ], '/ ' ) . '/ ' . 'thumb_ ' . $ img_data ['physical_filename ' ];
137152
138- if (file_exists ($ image_file_path ))
153+ if ($ this -> filesystem -> exists ($ image_file_path ))
139154 {
140155 $ img_data ['filesize ' ] = $ this ->rotate_image ($ image_file_path , $ img_rotate_deg );
141156 }
@@ -144,13 +159,14 @@ public function save_image()
144159 $ this ->json_response (4 , $ ext_display_name , $ this ->language ->lang ('IUL_IMG_NOT_EXIST ' ));
145160 }
146161
147- if ($ img_data ['thumbnail ' ] && file_exists ($ thumb_file_path ))
162+ if ($ img_data ['thumbnail ' ] && $ this -> filesystem -> exists ($ thumb_file_path ))
148163 {
149164 $ this ->rotate_image ($ thumb_file_path , $ img_rotate_deg );
150165 }
151166 else if ($ img_data ['thumbnail ' ])
152167 {
153- $ this ->json_response (4 , $ ext_display_name , $ this ->language ->lang ('IUL_THUMB_NOT_EXIST ' ));
168+ $ img_data ['thumbnail ' ] = 0 ;
169+ $ alert_msg = $ this ->language ->lang ('IUL_THUMB_NOT_EXIST ' );
154170 }
155171
156172 // Update DataBase
@@ -166,7 +182,7 @@ public function save_image()
166182 $ sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int ) $ img_attach_id ;
167183 $ this ->db ->sql_query ($ sql );
168184
169- $ this ->json_response (0 , $ ext_display_name , '' , $ img_attach_id , $ new_attach_id );
185+ $ this ->json_response (0 , $ ext_display_name , $ alert_msg ?? '' , $ img_attach_id , $ new_attach_id, $ img_data [ ' filesize ' ] );
170186 }
171187 else
172188 {
@@ -201,10 +217,11 @@ private function rotate_image($path, $deg)
201217 * @param string $message Messagebox message
202218 * @param int $old_attach_id Previous attachment id
203219 * @param int $new_attach_id New attachment id
220+ * @param int $file_size New file size
204221 *
205222 * @return string $json
206223 */
207- private function json_response ($ status , $ title = '' , $ message = '' , $ old_attach_id = 0 , $ new_attach_id = 0 )
224+ private function json_response ($ status , $ title = '' , $ message = '' , $ old_attach_id = 0 , $ new_attach_id = 0 , $ file_size = 0 )
208225 {
209226 $ json_response = new \phpbb \json_response ;
210227 $ json_response ->send ([
@@ -213,6 +230,7 @@ private function json_response($status, $title = '', $message = '', $old_attach_
213230 'message ' => $ message ,
214231 'oldAttachId ' => (int ) $ old_attach_id ,
215232 'newAttachId ' => (int ) $ new_attach_id ,
233+ 'fileSize ' => (int ) $ file_size ,
216234 ]);
217235 }
218236}
0 commit comments