PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Services / AvatarUploder.php

AvatarUploder.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Services/AvatarUploder.php

71 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\App\Services;
4
5 use Exception;
6 use FluentSupport\App\Models\Agent;
7 use FluentSupport\App\Models\Customer;
8 use FluentSupport\App\Services\Includes\FileSystem;
9
10 class AvatarUploder
11 {
12 /**
13 * @param array $file
14 * @param int $userid Person id
15 * @param string $type 'customer' or 'agent'
16 * @throws Exception
17 * @return array
18 */
19 public function addOrUpdateProfileImage ( $file, $userid, $type )
20 {
21 $this->validateExtension($file);
22
23 $user = $type == 'customer'? Customer::findOrFail($userid) : Agent::findOrFail($userid);
24
25 $uploadedImage = FileSystem::setSubDir(strtolower($type).'_avatars')->put($file);
26
27
28 if ( !$uploadedImage ) {
29 throw new Exception(esc_html__('Something went wrong while updating the profile picture', 'fluent-support'), 403);
30 }
31
32 $user->avatar = $uploadedImage[0]['url'];
33 $user->save();
34
35 return [
36 'message' => __('Profile picture has been updated successfully', 'fluent-support'),
37 'image' => $user->avatar,
38 $type => $user
39 ];
40 }
41
42 /**
43 * @param array $file
44 * @throws Exception
45 * @return bool
46 */
47 private function validateExtension($file)
48 {
49 /**
50 * Filter allowed profile picture extensions.
51 * @param array $allowedExtension
52 */
53 $allowedExtension = apply_filters('fluent_support/allowed_customer_profile_picture_file_type',
54 array('jpeg', 'jpe', 'jpg', 'png'));
55
56 $ext = $file['file']->getClientOriginalExtension();
57
58 if( !in_array($ext, $allowedExtension) ) {
59 throw new Exception(
60 sprintf(
61 // translators: %s is a comma-separated list of allowed file extensions (e.g., "jpg, png, gif")
62 esc_html__('Unsupported file submitted, allowed image file types are: %s', 'fluent-support'),
63 esc_html(implode(", ", $allowedExtension))
64 ), 403
65 );
66 }
67
68 return true;
69 }
70 }
71