MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 8.0.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/.trash/app.2/Http/Controllers/Website/WebsiteController.php
<?php

namespace App\Http\Controllers\Website;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreConsultancyRequest;
use App\Http\Requests\UpdateConsultancyRequest;
use App\Role;
use App\User;
use App\Partner;
use App\Volunteer;
use App\Country;
use App\Organization;
use App\Opportunity;
use App\OpportunityVolunteer;
use Validator;
use Gate;
use URL;
use Auth;
use File;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Notifications\NewUserEmailNotification;
use App\Notifications\NewUser;

class WebsiteController extends Controller
{
    public function index()
    {

        $user = Auth::user();
        $role = $user->roles->pluck('title','id')->toArray();
        
        /*if(in_array('Partner',$role)){
            $user_type = 'Partner';
            $opportunities = Opportunity::join('users', 'opportunities.partner_id', '=', 'users.id')->where('opportunities.partner_id',$user->id)->select('opportunities.*','users.*','opportunities.id as opportunity_id')->get();

            return view('admin.opportunities.index', compact('opportunities','user_type'));
        }else */
        if(in_array('Partner',$role) || in_array('Admin',$role) || in_array('Volunteer',$role)){
            $user_type = 'Admin';
            $opportunities = Opportunity::join('users', 'opportunities.partner_id', '=', 'users.id')->where('opportunity_status',1)->select('opportunities.*','users.*','opportunities.id as opportunity_id')->get();

            
            
            return view('website.opportunities.index', compact('opportunities','user_type'));
        }else{
            return redirect()->back()->with('error','Wrong access!');
        }
        
        
    }

    

    public function show(Request $request,$id)
    {
        abort_if(Gate::denies('opportunity_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
        

        if(Opportunity::where('id',$id)->count()){

            $opportunity = Opportunity::find($id);
            
            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();

            $add_array = array();
            if(in_array('Admin',$role)){
                $user_type = 'Admin';    
                
            }else if(in_array('Partner',$role)){
                $user_type = 'Partner';    
                
            }else if(in_array('Volunteer',$role)){
                $user_type = 'Volunteer';    
            }else{
                return redirect()->back()->with('error','Wrong access');
            }

            $partners = Partner::where('id',$opportunity->partner_id)->get();
            $volunteers = OpportunityVolunteer::join('users','opportunity_volunteers.volunteer_id','=','users.id')->select('opportunity_volunteers.*','users.name','users.user_profile_img','users.email')->where('opportunity_id',$id)->get();

            return view('website.opportunities.show', compact('opportunity','partners','volunteers','user','user_type'));
        }else{
            return redirect()->back()->with('error','Wrong access');
        }
    }

    public function apply_to_opportunity(Request $request,$id){
        abort_if(Gate::denies('opportunity_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
        

        if(Opportunity::where('id',$id)->count()){

            $opportunity = Opportunity::find($id);
            
            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();

            $add_array = array();
            if(in_array('Volunteer',$role)){
                if(OpportunityVolunteer::where('opportunity_id',$id)->where('volunteer_id',$user->id)->get()->count()){
                    return redirect()->back()->with('error','Already applied!');
                }else{

                    $approve = OpportunityVolunteer::where('opportunity_id',$id)->where('volunteer_id',$user->id)->first();
                    $volunteers_needed = $opportunity->no_of_volunteers;
                    $volunteers_approved = OpportunityVolunteer::where('opportunity_id',$id)->where('volunteer_status',1)->get()->count();

                    
                    if($volunteers_needed > $volunteers_approved){
                        $apply = new OpportunityVolunteer();
                        $apply->opportunity_id = $id;
                        $apply->volunteer_id = $user->id;
                        $apply->volunteer_status = 0;
                        $apply->save();
                        return redirect()->back()->with('success','Applied Successfully!');
                    }else{
                        return redirect()->back()->with('error','Max Volunteers are applied for this job. You can find some other good Opportunities!');            
                    }
                }
                
            }else{
                return redirect()->back()->with('error','Only Volunteers can apply!');    
            }

        }else{
            return redirect()->back()->with('error','Wrong access');
        }
    }
    public function destroy(Request $request,$id)
    {
        abort_if(Gate::denies('opportunity_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden');

        if(Opportunity::where('id',$id)->count()){

            $opportunity = Opportunity::find($id);
            
            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();
            $delete_permission = true;
            $add_array = array();
            if(in_array('Admin',$role)){
                //$opportunity->delete();
                $delete_permission = true;
            }else if(in_array('Partner',$role) && $opportunity->partner_id == $user->id){
                $delete_permission = true;
            }else{
                $delete_permission = false;
                return redirect()->back()->with('error','Wrong access');
            }

            $old_dir = public_path().'/Opportunity/'.$opportunity->partner_id.'/'.$opportunity->id;
            if(File::exists($old_dir)){

                File::deleteDirectory($old_dir);
            }
            $opportunity->delete();
           return back()->with('success','Opportunity deleted successfully!');
        }else{
            return redirect()->back()->with('error','Wrong access');
        }

    }

    public function upload_feature_image(Request $request)
    { 
        if($request->ajax()){
            $field_value = $request->field_value;
            $field_name = $request->field_name;
            $user_id = $request->partner_id;
            $opportunity_id = $request->opportunity_id;


            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();
            
            if(empty($field_name)){
                return json_encode(array('success' => 'error','msg'=>'Please add Field Name!'));
            }
            if(empty($user_id)){
                return json_encode(array('success' => 'error','msg'=>'Please add partner!'));
            }
            if(empty($opportunity_id) || !Opportunity::where('id',$opportunity_id)->get()->count()){
                return json_encode(array('success' => 'error','msg'=>'Please add Opportunity!'));
            }

            if(!Opportunity::where('id',$opportunity_id)->where('partner_id',$user_id)->get()->count()){
                return json_encode(array('success' => 'error','msg'=>'Wrong Opportunity access!'));   
            }

            if(in_array('Admin',$role) || in_array('Partner',$role)) {
                $folder= public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id;
                if(!is_dir($folder))
                {
                    //File::makeDirectory($folder);
                    File::makeDirectory($folder,0777,true);
                }

                if($request->hasFile('field_value')) {
                    
                    $opportunity = Opportunity::find($opportunity_id);
                    if(!empty($opportunity->feature_image)){
                        $old_feature_image = public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id.'/'.$opportunity->feature_image;
                        if(File::exists($old_feature_image)){
                            File::delete($old_feature_image);
                            $opportunity->feature_image = '';
                            $opportunity->save();
                        }
                    }
                    
                    $file = $request->file('field_value');
                    $ext = $file->getClientOriginalExtension();
                    $filename = 'feature_image_'.$user_id.'_'.$opportunity_id.'_'.time().'.' . $ext;
                    $destinationPath = $folder;
                    
                    $upload_success = $file->move($destinationPath, $filename);
                    if($upload_success){
                        
                        $opportunity->feature_image = $filename;
                        $opportunity->save();

                        $doc_url = URL::asset('Opportunity').'/'.$user_id.'/'.$opportunity_id.'/'.$filename;
                        $ddd = array('success' => 'done','doc_url'=>$doc_url,'msg'=>'Feature image uploaded successfully!');
                        return json_encode($ddd);
                    }else{
                        return json_encode(array('success' => 'error','doc_name'=>'kk'));
                    }
                }
                return json_encode(array('success' => 'error','doc_name'=>'kk'));
            }

            
        }
    }

    public function delete_feature_image(Request $request)
    { 
        if($request->ajax()){
            $data_col = $request->data_col;
            $opportunity_id = $request->opportunity_id;
            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();

            if(empty($opportunity_id) || !Opportunity::where('id',$opportunity_id)->get()->count()){
                return json_encode(array('success' => 'error','msg'=>'Opportunity not Found!'));
            }
            
            if(in_array('Admin',$role) || (in_array('Partner',$role) && Opportunity::where('partner_id',$user->id)->where('id',$opportunity_id)->get()->count())) {
                $opportunity = Opportunity::find($opportunity_id);
                if(in_array('Admin',$role)){
                    $user_id = $opportunity->partner_id;    
                }else{
                    $user_id = $user->id; 
                }
                $folder= public_path().'/Opportunity/'.$user_id.'/'.$opportunity_id;
                
                if(isset($opportunity->$data_col) && !empty($opportunity->$data_col)){
                    $old_feature_image = public_path().('/Opportunity').'/'.$user_id.'/'.$opportunity_id.'/'.$opportunity->$data_col;
                    if(File::exists($old_feature_image)){
                        File::delete($old_feature_image);
                    }
                    $opportunity->$data_col = '';
                    $opportunity->save();
                    $doc_url = URL::asset('images/feature_image.png');
                    $ddd = array('success' => 'done','doc_url'=>$doc_url,'msg'=>'Feature Image deleted Successfully!');
                    return json_encode($ddd);
                }else{
                    return json_encode(array('success' => 'error','msg'=>'Field Value Null!'));
                }
            }else{
                return json_encode(array('success' => 'error','msg'=>'Opportunity not Found!'));
            }
        }
        return json_encode(array('success' => 'error','msg'=>'Wrong access'));
    }

    public function change_opportunity_status(Request $request){
        $id = $request->opportunity_id;
        if($request->ajax()){

            if(empty($request->opportunity_id) && !Opportunity::where('id',$id)->get()->count()){
                return json_encode(array('status'=>'error','msg'=>'Opportunity not Found!'));
            }

            $user = Auth::user();
            $role = $user->roles->pluck('title','id')->toArray();

            if(in_array('Admin',$role)){
                $opportunity = Opportunity::find($id);
                $opportunity->opportunity_status = $request->opportunity_status;
                $opportunity->updated_at = now();
                $opportunity->save();
             
                /*$mailData = [
                    'greeting' => 'Hi '.$user->name.',',
                    'body' => 'Your Account on LiftCincy is Approved.',
                    'thanks' => 'Thank you,',
                    'actionText' => 'View Account',
                    'actionURL' => url('/'),
                ];
    

                $user->notify(new NewUserEmailNotification($mailData));*/


                return json_encode(array('status'=>'success','msg'=>'Opportunity status changed successfully!'));

             
                
            
            }
        }
        return json_encode(array('status'=>'error','msg'=>'Request Failed!'));
    }
}