HEX
Server: Apache
System: Linux d5123.usc1.stableserver.net 5.14.0-570.17.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Sat May 24 12:53:17 EDT 2025 x86_64
User: d5123 (1001)
PHP: 8.4.21
Disabled: NONE
Upload Files
File: /home/d5123/myboofola_com/wp-content/plugins/fluent-support/app/Services/AvatarUploder.php
<?php

namespace FluentSupport\App\Services;

use Exception;
use FluentSupport\App\Models\Agent;
use FluentSupport\App\Models\Customer;
use FluentSupport\App\Services\Includes\FileSystem;

class AvatarUploder
{
    /**
     * @param $file - file object
     * @param int $userid - user id
     * @param string $type - Customer or Agent
     * @throws Exception
     * @return array
     */
    public function addOrUpdateProfileImage ( $file, $userid, $type )
    {
        $this->validateExtension($file);

        $user = $type == 'customer'? Customer::findOrFail($userid) : Agent::findOrFail($userid);

        $uploadedImage = FileSystem::setSubDir(strtolower($type).'_avatars')->put($file);


        if ( !$uploadedImage ) {
            throw new Exception(esc_html__('Something went wrong while updating the profile picture', 'fluent-support'), 403);
        }

        $user->avatar = $uploadedImage[0]['url'];
        $user->save();

        return [
            'message' => __('Profile picture has been updated successfully', 'fluent-support'),
            'image'   => $user->avatar,
            $type     => $user
        ];
    }

    /**
     * This Method Will Validate The Extension Of The File
     * @param $file - file object
     * @throws Exception
     * @return bool
     */
    private function validateExtension($file)
    {
        /**
         * Filter profile picture upload types
         * @param array $allowedExtension
         */
        $allowedExtension = apply_filters('fluent_support/allowed_customer_profile_picture_file_type',
        array('jpeg', 'jpe', 'jpg', 'png'));

        $ext = $file['file']->getClientOriginalExtension();

        if( !in_array($ext, $allowedExtension) ) {
            throw new Exception(
                sprintf(
                    // translators: %s is a comma-separated list of allowed file extensions (e.g., "jpg, png, gif")
                    esc_html__('Unsupported file submitted, allowed image file types are: %s', 'fluent-support'),
                    esc_html(implode(", ", $allowedExtension))
                ), 403
            );
        }

        return true;
    }
}