1313/**
1414 * Ajax main controller
1515 */
16- class save_rotated_img_controller
16+ class ajax_controller
1717{
1818 /** @var \phpbb\config\config */
1919 protected $ config ;
@@ -45,6 +45,9 @@ class save_rotated_img_controller
4545 /** @var string phpEx */
4646 protected $ php_ext ;
4747
48+ /** @var string Extension name */
49+ protected $ ext_display_name ;
50+
4851 /**
4952 * Constructor for ajax controller
5053 *
@@ -85,19 +88,18 @@ public function __construct(
8588
8689 // Add language file
8790 $ this ->language ->add_lang ('attachment ' , 'imcger/imgupload ' );
91+
92+ // Get name of the extension
93+ $ metadata_manager = $ this ->ext_manager ->create_extension_metadata_manager ('imcger/imgupload ' );
94+ $ this ->ext_display_name = $ metadata_manager ->get_metadata ('display-name ' );
8895 }
8996
9097 /**
91- * Rotate Image with ImageMagick
92- *
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
98+ * Assigns the Ajax request app.php/imgupload/{order}
9799 *
98- * @return array Json arry with status, old and new attach id, new file size or error message
100+ * @access public
99101 */
100- public function save_image ( )
102+ public function request ( $ order )
101103 {
102104 // No ajax request, redirect to forum index
103105 if (!$ this ->request ->is_ajax ())
@@ -111,23 +113,53 @@ public function save_image()
111113 $ this ->json_response (3 );
112114 }
113115
114- // Get name of the extension
115- $ metadata_manager = $ this ->ext_manager ->create_extension_metadata_manager ('imcger/imgupload ' );
116- $ ext_display_name = $ metadata_manager ->get_metadata ('display-name ' );
117-
118116 // Check form token
119- if (!check_form_key ('posting ' ))
117+ if (!( check_form_key ('posting ' ) || check_form_key ( ' ucp_pm_compose ' ) ))
120118 {
121- $ this ->json_response (5 , $ ext_display_name , $ this ->language ->lang ('FORM_INVALID ' ));
119+ $ this ->json_response (5 , $ this -> ext_display_name , $ this ->language ->lang ('FORM_INVALID ' ));
122120 }
123121
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 ' , '' ));
122+ switch ($ order )
123+ {
124+ // Ajax request to save image after rotation
125+ case 'save_image ' :
126+ // Get variable, accept only integer
127+ $ img_attach_id = intval ($ this ->request ->variable ('attach_id ' , 0 ));
128+ $ img_rotate_deg = intval ($ this ->request ->variable ('img_rotate_deg ' , 0 ));
129+
130+ $ this ->save_image ($ img_attach_id , $ img_rotate_deg );
131+ break ;
132+
133+ // Ajax request for image size
134+ case 'image_size ' :
135+ // Get variable, accept only integer
136+ $ img_attach_id = intval ($ this ->request ->variable ('attach_id ' , 0 ));
137+
138+ $ this ->image_size ($ img_attach_id );
139+ break ;
140+
141+ // Displays the start page of phpBB
142+ default :
143+ redirect ($ this ->phpbb_root_path . 'index. ' . $ this ->phpEx );
144+ break ;
145+ }
146+ }
127147
148+ /**
149+ * Rotate and save Image with ImageMagick
150+ *
151+ * @var int attach_id contain attach id
152+ * @var int img_rotate_deg contain rotate degree
153+ * @var int creation_time creation time of token
154+ * @var string form_token form token
155+ *
156+ * @return array Json arry with status, old and new attach id, new file size or error message
157+ */
158+ private function save_image ($ img_attach_id , $ img_rotate_deg )
159+ {
128160 if (!$ img_attach_id || $ img_rotate_deg < 1 || $ img_rotate_deg > 360 )
129161 {
130- $ this ->json_response (5 , $ ext_display_name , $ this ->language ->lang ('IUL_WRONG_PARAM ' ));
162+ $ this ->json_response (5 , $ this -> ext_display_name , $ this ->language ->lang ('IUL_WRONG_PARAM ' ));
131163 }
132164
133165 if ($ this ->auth ->acl_gets ('u_attach ' , 'a_attach ' , 'f_attach ' ))
@@ -143,7 +175,7 @@ public function save_image()
143175
144176 if (!isset ($ img_data ) || $ img_data == false )
145177 {
146- $ this ->json_response (4 , $ ext_display_name , $ this ->language ->lang ('IUL_NO_IMG_IN_DATABASE ' ));
178+ $ this ->json_response (4 , $ this -> ext_display_name , $ this ->language ->lang ('IUL_NO_IMG_IN_DATABASE ' ));
147179 }
148180
149181 // Get image file path
@@ -156,7 +188,7 @@ public function save_image()
156188 }
157189 else
158190 {
159- $ this ->json_response (4 , $ ext_display_name , $ this ->language ->lang ('IUL_IMG_NOT_EXIST ' ));
191+ $ this ->json_response (4 , $ this -> ext_display_name , $ this ->language ->lang ('IUL_IMG_NOT_EXIST ' ));
160192 }
161193
162194 if ($ img_data ['thumbnail ' ] && $ this ->filesystem ->exists ($ thumb_file_path ))
@@ -182,21 +214,41 @@ public function save_image()
182214 $ sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int ) $ img_attach_id ;
183215 $ this ->db ->sql_query ($ sql );
184216
185- $ this ->json_response (0 , $ ext_display_name , $ alert_msg ?? '' , $ img_attach_id , $ new_attach_id , $ img_data ['filesize ' ]);
217+ $ this ->json_response (0 , $ this -> ext_display_name , $ alert_msg ?? '' , $ img_attach_id , $ new_attach_id , $ img_data ['filesize ' ]);
186218 }
187219 else
188220 {
189- $ this ->json_response (5 , $ ext_display_name , $ this ->language ->lang ('IUL_DATABASE_NOT_UPDATE ' ));
221+ $ this ->json_response (5 , $ this -> ext_display_name , $ this ->language ->lang ('IUL_DATABASE_NOT_UPDATE ' ));
190222 }
191223 }
192224
225+ /**
226+ * Get Image size
227+ *
228+ * @var int $attach_id contain attach id
229+ *
230+ * @return array Json arry with attach id and file size
231+ */
232+ private function image_size ($ attach_id )
233+ {
234+ $ sql = 'SELECT filesize
235+ FROM ' . ATTACHMENTS_TABLE . '
236+ WHERE attach_id = ' . (int ) $ attach_id ;
237+
238+ $ result = $ this ->db ->sql_query ($ sql );
239+ $ img_data = $ this ->db ->sql_fetchrow ($ result );
240+ $ this ->db ->sql_freeresult ($ result );
241+
242+ $ this ->json_response (0 , $ this ->ext_display_name , '' , $ attach_id , $ attach_id , $ img_data ['filesize ' ]);
243+ }
244+
193245 /**
194246 * Rotate Image with ImageMagick
195247 *
196- * @param string $path Path to the image file
197- * @param int $deg Rotation angle
248+ * @param string $path Path to the image file
249+ * @param int $deg Rotation angle
198250 *
199- * @return int Image file size
251+ * @return int $filesize Image file size
200252 */
201253 private function rotate_image ($ path , $ deg )
202254 {
0 commit comments