13
13
/**
14
14
* Ajax main controller
15
15
*/
16
- class save_rotated_img_controller
16
+ class ajax_controller
17
17
{
18
18
/** @var \phpbb\config\config */
19
19
protected $ config ;
@@ -45,6 +45,9 @@ class save_rotated_img_controller
45
45
/** @var string phpEx */
46
46
protected $ php_ext ;
47
47
48
+ /** @var string Extension name */
49
+ protected $ ext_display_name ;
50
+
48
51
/**
49
52
* Constructor for ajax controller
50
53
*
@@ -85,19 +88,18 @@ public function __construct(
85
88
86
89
// Add language file
87
90
$ 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 ' );
88
95
}
89
96
90
97
/**
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}
97
99
*
98
- * @return array Json arry with status, old and new attach id, new file size or error message
100
+ * @access public
99
101
*/
100
- public function save_image ( )
102
+ public function request ( $ order )
101
103
{
102
104
// No ajax request, redirect to forum index
103
105
if (!$ this ->request ->is_ajax ())
@@ -111,23 +113,53 @@ public function save_image()
111
113
$ this ->json_response (3 );
112
114
}
113
115
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
-
118
116
// Check form token
119
- if (!check_form_key ('posting ' ))
117
+ if (!( check_form_key ('posting ' ) || check_form_key ( ' ucp_pm_compose ' ) ))
120
118
{
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 ' ));
122
120
}
123
121
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
+ }
127
147
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
+ {
128
160
if (!$ img_attach_id || $ img_rotate_deg < 1 || $ img_rotate_deg > 360 )
129
161
{
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 ' ));
131
163
}
132
164
133
165
if ($ this ->auth ->acl_gets ('u_attach ' , 'a_attach ' , 'f_attach ' ))
@@ -143,7 +175,7 @@ public function save_image()
143
175
144
176
if (!isset ($ img_data ) || $ img_data == false )
145
177
{
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 ' ));
147
179
}
148
180
149
181
// Get image file path
@@ -156,7 +188,7 @@ public function save_image()
156
188
}
157
189
else
158
190
{
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 ' ));
160
192
}
161
193
162
194
if ($ img_data ['thumbnail ' ] && $ this ->filesystem ->exists ($ thumb_file_path ))
@@ -182,21 +214,41 @@ public function save_image()
182
214
$ sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int ) $ img_attach_id ;
183
215
$ this ->db ->sql_query ($ sql );
184
216
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 ' ]);
186
218
}
187
219
else
188
220
{
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 ' ));
190
222
}
191
223
}
192
224
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
+
193
245
/**
194
246
* Rotate Image with ImageMagick
195
247
*
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
198
250
*
199
- * @return int Image file size
251
+ * @return int $filesize Image file size
200
252
*/
201
253
private function rotate_image ($ path , $ deg )
202
254
{
0 commit comments