|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Image upload use ImageMagick |
| 4 | + * An extension for the phpBB Forum Software package. |
| 5 | + * |
| 6 | + * @copyright (c) 2022, Thorsten Ahlers |
| 7 | + * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | + */ |
| 9 | + |
| 10 | +namespace imcger\imgupload\controller; |
| 11 | + |
| 12 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 13 | + |
| 14 | +/** |
| 15 | + * Main controller |
| 16 | + */ |
| 17 | +class save_rotated_img_controller |
| 18 | +{ |
| 19 | + /** @var \phpbb\config\config */ |
| 20 | + protected $config; |
| 21 | + |
| 22 | + /** @var \phpbb\user */ |
| 23 | + protected $user; |
| 24 | + |
| 25 | + /** @var \phpbb\request\request */ |
| 26 | + private $request; |
| 27 | + |
| 28 | + /** @var \phpbb\db\driver\driver_interface */ |
| 29 | + private $db; |
| 30 | + |
| 31 | + /** @var \phpbb\auth\auth */ |
| 32 | + private $auth; |
| 33 | + |
| 34 | + /** @var \phpbb\language\language */ |
| 35 | + protected $language; |
| 36 | + |
| 37 | + /** @var \phpbb\extension\manager */ |
| 38 | + protected $ext_manager; |
| 39 | + |
| 40 | + /** @var string phpBB root path */ |
| 41 | + protected $root_path; |
| 42 | + |
| 43 | + /** @var string phpEx */ |
| 44 | + protected $php_ext; |
| 45 | + |
| 46 | + /** |
| 47 | + * @param \phpbb\config\config $config |
| 48 | + * @param \phpbb\user $user |
| 49 | + * @param \phpbb\request\request $request |
| 50 | + * @param \phpbb\db\driver\driver_interface $db |
| 51 | + * @param \phpbb\auth\auth $auth |
| 52 | + * @param \phpbb\language\language $language |
| 53 | + * @param \phpbb\extension\manager $ext_manager |
| 54 | + * @param string $root_path |
| 55 | + * @param string $php_ext |
| 56 | + */ |
| 57 | + public function __construct( |
| 58 | + \phpbb\config\config $config, |
| 59 | + \phpbb\user $user, |
| 60 | + \phpbb\request\request $request, |
| 61 | + \phpbb\db\driver\driver_interface $db, |
| 62 | + \phpbb\auth\auth $auth, |
| 63 | + \phpbb\language\language $language, |
| 64 | + \phpbb\extension\manager $ext_manager, |
| 65 | + $root_path, |
| 66 | + $php_ext |
| 67 | + ) |
| 68 | + { |
| 69 | + $this->config = $config; |
| 70 | + $this->user = $user; |
| 71 | + $this->request = $request; |
| 72 | + $this->db = $db; |
| 73 | + $this->auth = $auth; |
| 74 | + $this->language = $language; |
| 75 | + $this->ext_manager = $ext_manager; |
| 76 | + $this->root_path = $root_path; |
| 77 | + $this->php_ext = $php_ext; |
| 78 | + |
| 79 | + // Add language file |
| 80 | + $this->language->add_lang('attachment', 'imcger/imgupload'); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Rotate Image with ImageMagick |
| 85 | + * |
| 86 | + * @var string $data String contain attach id and rotate degree |
| 87 | + * |
| 88 | + * @return array Json arry with old and new attach id or error message |
| 89 | + */ |
| 90 | + public function save_image() |
| 91 | + { |
| 92 | + // No ajax request, redirect to forum index |
| 93 | + if (!$this->request->is_ajax()) |
| 94 | + { |
| 95 | + redirect($this->root_path . '/index.' . $this->php_ext); |
| 96 | + } |
| 97 | + |
| 98 | + // No user logged in, redirect in js to login page |
| 99 | + if ($this->user->data['user_id'] == ANONYMOUS) |
| 100 | + { |
| 101 | + return $this->json_response(3); |
| 102 | + } |
| 103 | + |
| 104 | + $img_attach_id = $this->request->variable('attach_id', ''); |
| 105 | + $img_rotate_deg = $this->request->variable('img_rotate_deg', ''); |
| 106 | + |
| 107 | + // Get name of the extension |
| 108 | + $metadata_manager = $this->ext_manager->create_extension_metadata_manager('imcger/imgupload'); |
| 109 | + $ext_display_name = $metadata_manager->get_metadata('display-name'); |
| 110 | + |
| 111 | + if (!$img_attach_id || !$img_rotate_deg) |
| 112 | + { |
| 113 | + return $this->json_response(5, $ext_display_name, $this->language->lang('IUL_WRONG_PARAM')); |
| 114 | + } |
| 115 | + |
| 116 | + if ($this->auth->acl_gets('u_attach', 'a_attach', 'f_attach')) |
| 117 | + { |
| 118 | + $sql = 'SELECT * |
| 119 | + FROM ' . ATTACHMENTS_TABLE . ' |
| 120 | + WHERE attach_id = ' . (int) $img_attach_id; |
| 121 | + |
| 122 | + $result = $this->db->sql_query($sql); |
| 123 | + $img_data = $this->db->sql_fetchrow($result); |
| 124 | + $this->db->sql_freeresult($result); |
| 125 | + } |
| 126 | + |
| 127 | + if (!isset($img_data) || $img_data == false) |
| 128 | + { |
| 129 | + return $this->json_response(4, $ext_display_name, $this->language->lang('IUL_NO_IMG_IN_DATABASE')); |
| 130 | + } |
| 131 | + |
| 132 | + // Get image file path |
| 133 | + $image_file_path = $this->root_path . trim($this->config['upload_path'], '/') . '/' . $img_data['physical_filename']; |
| 134 | + $thumb_file_path = $this->root_path . trim($this->config['upload_path'], '/') . '/' . 'thumb_' . $img_data['physical_filename']; |
| 135 | + |
| 136 | + if (file_exists($image_file_path)) |
| 137 | + { |
| 138 | + $img_data['filesize'] = $this->rotate_image($image_file_path, $img_rotate_deg); |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + return $this->json_response(4, $ext_display_name, $this->language->lang('IUL_IMG_NOT_EXIST')); |
| 143 | + } |
| 144 | + |
| 145 | + if ($img_data['thumbnail'] && file_exists($thumb_file_path)) |
| 146 | + { |
| 147 | + $this->rotate_image($thumb_file_path, $img_rotate_deg); |
| 148 | + } |
| 149 | + else if ($img_data['thumbnail']) |
| 150 | + { |
| 151 | + return $this->json_response(4, $ext_display_name, $this->language->lang('IUL_THUMB_NOT_EXIST')); |
| 152 | + } |
| 153 | + |
| 154 | + // Update DataBase |
| 155 | + unset($img_data['attach_id']); |
| 156 | + $sql = 'INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $this->db->sql_build_array('INSERT', $img_data); |
| 157 | + $this->db->sql_query($sql); |
| 158 | + |
| 159 | + // sql_nextid() to be removed in 4.1.0-a1, in future use sql_last_inserted_id() it exsits since 3.3.11-RC1 |
| 160 | + $new_attach_id = $this->db->sql_nextid(); |
| 161 | + |
| 162 | + if ($new_attach_id) |
| 163 | + { |
| 164 | + $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $img_attach_id; |
| 165 | + $this->db->sql_query($sql); |
| 166 | + |
| 167 | + return $this->json_response(0, $ext_display_name, '', $img_attach_id, $new_attach_id); |
| 168 | + } |
| 169 | + else |
| 170 | + { |
| 171 | + return $this->json_response(5, $ext_display_name, $this->language->lang('IUL_DATABASE_NOT_UPDATE')); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Rotate Image with ImageMagick |
| 177 | + * |
| 178 | + * @param string $path Path to the image file |
| 179 | + * @param int $deg Rotation angle |
| 180 | + * |
| 181 | + * @return int Image file size |
| 182 | + */ |
| 183 | + private function rotate_image($path, $deg) |
| 184 | + { |
| 185 | + $image = new \Imagick($path); |
| 186 | + $image->rotateImage('#000', $deg); |
| 187 | + $image->writeImage($path); |
| 188 | + $filesize = strlen($image->getImageBlob()); |
| 189 | + $image->clear(); |
| 190 | + |
| 191 | + return $filesize; |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Generate json return |
| 196 | + * |
| 197 | + * @param int $status Status 0=id's, 3=redirect, 4=file not found, 5=error |
| 198 | + * @param string $title Messagebox title |
| 199 | + * @param string $message Messagebox message |
| 200 | + * @param int $old_attach_id Rotation angle |
| 201 | + * @param int $new_attach_id Path to the image file |
| 202 | + * |
| 203 | + * @return string $json |
| 204 | + */ |
| 205 | + private function json_response($status, $title = '', $message = '', $old_attach_id = 0, $new_attach_id = 0) |
| 206 | + { |
| 207 | + $json = new JsonResponse(array( |
| 208 | + 'status' => (int) $status, |
| 209 | + 'title' => $title, |
| 210 | + 'message' => $message, |
| 211 | + 'oldAttachId' => (int) $old_attach_id, |
| 212 | + 'newAttachId' => (int) $new_attach_id, |
| 213 | + )); |
| 214 | + |
| 215 | + return $json; |
| 216 | + } |
| 217 | +} |
0 commit comments