| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings manager class |
| 5 |
* |
| 6 |
* @since 1.4.2 |
| 7 |
*/ |
| 8 |
|
| 9 |
class Weforms_Upload_Controller extends Weforms_REST_Controller { |
| 10 |
/** |
| 11 |
* Endpoint namespace |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
protected $namespace = 'weforms/v1'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Route name |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
protected $rest_base = 'uploads'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Register all routes releated with forms |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function register_routes() { |
| 30 |
|
| 31 |
register_rest_route( $this->namespace, '/'. $this->rest_base . '/(?P<form_id>[\d]+)/files', array( |
| 32 |
'args' => array( |
| 33 |
'form_id' => array( |
| 34 |
'required' => true, |
| 35 |
'description' => __( 'Unique identifier for the object.', 'weforms' ), |
| 36 |
'sanitize_callback' => 'absint', |
| 37 |
'type' => 'integer', |
| 38 |
'validate_callback' => array( $this, 'is_form_exists' ), |
| 39 |
), |
| 40 |
'field_id' => array( |
| 41 |
'required' => true, |
| 42 |
'description' => __( 'Unique identifier for the object.', 'weforms' ), |
| 43 |
'sanitize_callback' => 'absint', |
| 44 |
'type' => 'integer', |
| 45 |
), |
| 46 |
), |
| 47 |
array( |
| 48 |
'methods' => WP_REST_Server::CREATABLE, |
| 49 |
'callback' => array( $this, 'upload_file' ), |
| 50 |
'permission_callback' => array( $this, 'upload_permissions_check' ), |
| 51 |
), |
| 52 |
) ); |
| 53 |
|
| 54 |
register_rest_route( $this->namespace, '/'. $this->rest_base . '/(?P<form_id>[\d]+)/files/(?P<id>[\d]+)/', array( |
| 55 |
'args' => array( |
| 56 |
'form_id' => array( |
| 57 |
'description' => __( 'Unique identifier for the object.', 'weforms' ), |
| 58 |
'validate_callback' => array( $this, 'is_form_exists' ), |
| 59 |
'required' => true, |
| 60 |
'sanitize_callback' => 'absint', |
| 61 |
'type' => 'integer', |
| 62 |
), |
| 63 |
'id' => array( |
| 64 |
'description' => __( 'Unique identifier for the object.', 'weforms' ), |
| 65 |
'validate_callback' => array( $this, 'is_form_attach_exist' ), |
| 66 |
'required' => true, |
| 67 |
'sanitize_callback' => 'absint', |
| 68 |
'type' => 'integer', |
| 69 |
), |
| 70 |
'force' => array( |
| 71 |
'required' => true, |
| 72 |
'type' => 'boolean', |
| 73 |
'description' => __( '', 'weforms' ), |
| 74 |
'default' => true, |
| 75 |
), |
| 76 |
), |
| 77 |
|
| 78 |
array( |
| 79 |
'methods' => WP_REST_Server::DELETABLE, |
| 80 |
'callback' => array( $this, 'delete_file' ), |
| 81 |
'permission_callback' => array( $this, 'upload_permissions_check' ), |
| 82 |
), |
| 83 |
) ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Check form Attach exists |
| 89 |
* |
| 90 |
* @since 1.4.2 |
| 91 |
* |
| 92 |
* @param string $param |
| 93 |
* @param WP_REST_Request $request |
| 94 |
* @param string $key |
| 95 |
* |
| 96 |
* @return boolean |
| 97 |
**/ |
| 98 |
public function is_form_attach_exist( $param, $request, $key ) { |
| 99 |
$form_id = $request->get_param('form_id'); |
| 100 |
$attach_id = (int) $param; |
| 101 |
$attach_form_id = get_post_meta( $attach_id, 'attachment_form_id', true ); |
| 102 |
|
| 103 |
if( $attach_form_id == $form_id ) { |
| 104 |
return true; |
| 105 |
} else { |
| 106 |
return false; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check Form Field Exist |
| 112 |
* |
| 113 |
* @since 1.4.2 |
| 114 |
* |
| 115 |
* @param string $param |
| 116 |
* @param WP_REST_Request $request |
| 117 |
* @param string $key |
| 118 |
* |
| 119 |
* @return boolean |
| 120 |
**/ |
| 121 |
public function is_file_validate( $request ) { |
| 122 |
$file_error = new WP_Error(); |
| 123 |
$form = weforms()->form->get( (int) $request['form_id'] ); |
| 124 |
$form_settings = $form->get_settings(); |
| 125 |
$form_fields = $form->get_fields(); |
| 126 |
$files = $request->get_file_params(); |
| 127 |
$headers = $request->get_headers(); |
| 128 |
$field_id = $request->get_param('field_id'); |
| 129 |
|
| 130 |
if ( ! empty( $files ) ) { |
| 131 |
foreach ($form_fields as $field) { |
| 132 |
if( 'image_upload' === $field['template'] && $field['id'] == $field_id ) { |
| 133 |
$allowed_extension = weforms_allowed_extensions(); |
| 134 |
$allowed_file_type = explode( ",", $allowed_extension['images']['ext'] ); |
| 135 |
$file_type = wp_check_filetype( $files['file']['name'], $mimes = null ); |
| 136 |
|
| 137 |
if( in_array( $file_type['ext'], $allowed_file_type ) ) { |
| 138 |
if( $files['file']['size'] <= ( $field['max_size'] * 1024 ) ) { |
| 139 |
return true; |
| 140 |
} else { |
| 141 |
$file_error->add( 'rest_weforms_invalid_file_size', __( 'File Size exceeds limit!','weforms' ), array( 'status' => 404 ) ); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if( 'file_upload' === $field['template'] && $field['id'] == $field_id ) { |
| 147 |
$file_type = wp_check_filetype( $files['file']['name'], $mimes = null ); |
| 148 |
if( $this->is_allow_file_type( $field['extension'],$file_type['ext'] ) ) { |
| 149 |
if( $files['file']['size'] <= ( $field['max_size'] * 1024 ) ) { |
| 150 |
return true; |
| 151 |
} else { |
| 152 |
$file_error->add( 'rest_weforms_invalid_file_size', __( 'File Size exceeds limit!','weforms' ), array( 'status' => 404 ) ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if( count( $file_error->get_error_messages() ) > 0 ) { |
| 160 |
return $file_error; |
| 161 |
} else { |
| 162 |
return true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Upload File |
| 168 |
* |
| 169 |
* @since 1.4.2 |
| 170 |
* |
| 171 |
* @param WP_REST_Request $request |
| 172 |
* |
| 173 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 174 |
**/ |
| 175 |
public function upload_file( $request ) { |
| 176 |
global $wp_rest_server; |
| 177 |
|
| 178 |
$endpoints = $wp_rest_server->get_routes(); |
| 179 |
$attachment_controller = $endpoints['/wp/v2/media'][0]['callback'][0]; |
| 180 |
$files = $request->get_file_params(); |
| 181 |
$headers = $request->get_headers(); |
| 182 |
$attachment_response = $attachment_controller->create_item( $request ); |
| 183 |
|
| 184 |
if ( ! empty( $attachment_response->data['id'] ) ) { |
| 185 |
$form_id = $request->get_param('form_id'); |
| 186 |
|
| 187 |
update_post_meta( $attachment_response->data['id'] , 'attachment_form_id', $form_id ); |
| 188 |
|
| 189 |
if ( wp_attachment_is_image( $attachment_response->data['id'] ) ) { |
| 190 |
$image = wp_get_attachment_image_src( $attachment_response->data['id'], 'thumbnail' ); |
| 191 |
$image = $image[0]; |
| 192 |
} else { |
| 193 |
$image = wp_mime_type_icon( $attachment_response->data['id'] ); |
| 194 |
} |
| 195 |
|
| 196 |
$data = array( |
| 197 |
'form_id' => $form_id, |
| 198 |
'attach_id' => $attachment_response->data['id'], |
| 199 |
'link' => $image |
| 200 |
); |
| 201 |
|
| 202 |
$response = $this->prepare_response_for_collection( $data, $request ); |
| 203 |
$response['html '] = $this->attach_html( $attachment_response->data['id'] ); |
| 204 |
$response = rest_ensure_response( $response ); |
| 205 |
|
| 206 |
return $response; |
| 207 |
} else { |
| 208 |
return new WP_Error( 'rest_weforms', __( 'could not upload file', 'weforms') , array( 'status' => 404 ) ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Delete File |
| 214 |
* |
| 215 |
* @since 1.4.2 |
| 216 |
* |
| 217 |
* @param WP_REST_Request $request |
| 218 |
* |
| 219 |
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 220 |
**/ |
| 221 |
public function delete_file( $request ) { |
| 222 |
global $wp_rest_server; |
| 223 |
|
| 224 |
$endpoints = $wp_rest_server->get_routes(); |
| 225 |
$attachment_controller = $endpoints['/wp/v2/media'][0]['callback'][0]; |
| 226 |
$attachment_response = $attachment_controller->delete_item( $request ); |
| 227 |
$data = array(); |
| 228 |
$data['id'] = $attachment_response->data['previous']['id']; |
| 229 |
$data['message'] = "File deleted successfully"; |
| 230 |
$data['data']['status'] = 200; |
| 231 |
$response = $this->prepare_response_for_collection( $data ); |
| 232 |
$response = rest_ensure_response( $response ); |
| 233 |
|
| 234 |
return $response; |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Image attachment response |
| 239 |
* |
| 240 |
* @since 1.4.2 |
| 241 |
* |
| 242 |
* @param integer $attach_id |
| 243 |
* @param string $type |
| 244 |
* |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public static function attach_html( $attach_id, $type = NULL ) { |
| 248 |
if ( ! $type ) { |
| 249 |
$type = isset( $_GET['type'] ) ? $_GET['type'] : 'image'; |
| 250 |
} |
| 251 |
|
| 252 |
$attachment = get_post( $attach_id ); |
| 253 |
|
| 254 |
if ( ! $attachment ) { |
| 255 |
return; |
| 256 |
} |
| 257 |
|
| 258 |
if ( wp_attachment_is_image( $attach_id ) ) { |
| 259 |
$image = wp_get_attachment_image_src( $attach_id, 'thumbnail' ); |
| 260 |
$image = $image[0]; |
| 261 |
} else { |
| 262 |
$image = wp_mime_type_icon( $attach_id ); |
| 263 |
} |
| 264 |
|
| 265 |
$html = '<li class="ui-state-default wpuf-image-wrap thumbnail">'; |
| 266 |
$html .= sprintf( '<div class="attachment-name"><img src="%s" alt="%s" /></div>', $image, esc_attr( $attachment->post_title ) ); |
| 267 |
$html .= sprintf( '<input type="hidden" name="wpuf_files[%s][]" value="%d">', $type, $attach_id ); |
| 268 |
$html .= '<div class="caption">'; |
| 269 |
$html .= sprintf( '<a href="#" class="attachment-delete" data-attach_id="%d"> <img src="%s" /></a>', $attach_id, WEFORMS_ASSET_URI . '/images/del-img.png' ); |
| 270 |
$html .= sprintf( '<span class="wpuf-drag-file"> <img src="%s" /></span>', WEFORMS_ASSET_URI . '/images/move-img.png' ); |
| 271 |
$html .= '</div>'; |
| 272 |
$html .= '</li>'; |
| 273 |
|
| 274 |
return $html; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Compare Two Array Return One Array |
| 279 |
* |
| 280 |
* @since 1.4.2 |
| 281 |
* |
| 282 |
* @return array |
| 283 |
**/ |
| 284 |
public function compare_array( $field_extensions, $allowd_extensions ) { |
| 285 |
$array_new = []; |
| 286 |
|
| 287 |
foreach( $field_extensions as $key => $value ) { |
| 288 |
if ( array_key_exists($value, $allowd_extensions ) ) { |
| 289 |
$array_new[$key] = $allowd_extensions[$value]; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return $array_new; |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Get Allowed Extension |
| 298 |
* |
| 299 |
* @since 1.4.2 |
| 300 |
* |
| 301 |
* @param string $field_extension |
| 302 |
* @param string $file_extension |
| 303 |
* |
| 304 |
* @return Boolean |
| 305 |
**/ |
| 306 |
public function is_allow_file_type( $field_extension,$file_extension ) { |
| 307 |
$allowed_extensions = weforms_allowed_extensions(); |
| 308 |
$field_allowed_extensions = $this->get_allowed_extension( $allowed_extensions,$field_extension ); |
| 309 |
|
| 310 |
if( in_array( $file_extension, $field_allowed_extensions ) ) { |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
return false; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Get Allowed Extension Type |
| 319 |
* |
| 320 |
* @since 1.4.2 |
| 321 |
* |
| 322 |
* @param array $allowd_extensions |
| 323 |
* @param array $field_extensions |
| 324 |
* |
| 325 |
* @return Array |
| 326 |
**/ |
| 327 |
public function get_allowed_extension( $allowd_extensions,$field_extensions ) { |
| 328 |
$allowd_ext_array = array(); |
| 329 |
|
| 330 |
if( is_array( $field_extensions ) ) { |
| 331 |
$allowd_extensions = $this->compare_array($field_extensions, $allowd_extensions); |
| 332 |
|
| 333 |
foreach ($allowd_extensions as $key => $allowd_extension) { |
| 334 |
$extensions = explode( ',', $allowd_extension['ext'] ); |
| 335 |
foreach ($extensions as $key => $extension) { |
| 336 |
array_push($allowd_ext_array, $extension); |
| 337 |
} |
| 338 |
} |
| 339 |
} else { |
| 340 |
if( array_key_exists( $field_extensions, $allowd_extensions ) ) { |
| 341 |
foreach ( $allowd_extensions[ $field_extensions ][ 'ext' ] as $key => $allowd_extension ) { |
| 342 |
$extensions = explode( ',', $allowd_extension['ext'] ); |
| 343 |
foreach ($extensions as $key => $extension) { |
| 344 |
array_push($allowd_ext_array, $extension); |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
return $allowd_ext_array; |
| 351 |
} |
| 352 |
|
| 353 |
public function upload_permissions_check( $request ) { |
| 354 |
|
| 355 |
if ( !is_weforms_api_allowed_guest_submission() ) { |
| 356 |
return new WP_Error( 'rest_weforms_cannot_upload', __( 'Sorry, you have no permission to upload File.','weforms' ), array( 'status' => rest_authorization_required_code() ) ); |
| 357 |
} |
| 358 |
|
| 359 |
$form = weforms()->form->get( (int) $request['form_id'] ); |
| 360 |
$form_is_open = $form->is_submission_open(); |
| 361 |
|
| 362 |
if ( is_wp_error( $form_is_open ) ) { |
| 363 |
return new WP_Error( 'rest_weforms_form_permission', $form_is_open->get_error_message(), array( 'status' => 404 ) ); |
| 364 |
} |
| 365 |
|
| 366 |
$file_validations = $this->is_file_validate( $request ); |
| 367 |
|
| 368 |
if( is_wp_error( $file_validations ) ) { |
| 369 |
$file_message = array(); |
| 370 |
foreach ( $file_validations->get_error_messages() as $error ) { |
| 371 |
$file_message[] = $error; |
| 372 |
} |
| 373 |
return new WP_Error( 'error', $file_message, array( 'status' => 404 ) ); |
| 374 |
} |
| 375 |
|
| 376 |
return true; |
| 377 |
} |
| 378 |
} |
| 379 |
|