Advertisement
Guest User

PHP Snippet

a guest
Sep 27th, 2017
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.06 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Http\Requests;
  7. use App;
  8. use App\User;
  9. use App\Availability;
  10. use Illuminate\Support\Facades\Input;
  11. use Illuminate\Support\Facades\Auth;
  12. use App\Interviewee;
  13. use App\Interviewer;
  14. use Intervention\Image\ImageManager;
  15. use \Mail;
  16. use Log;
  17. use Mockery\CountValidator\Exception;
  18. use \URL;
  19. use Carbon\Carbon;
  20. use Laravel\Socialite\Facades\Socialite;
  21. use Illuminate\Support\Facades\Session;
  22. use Illuminate\Support\Facades\Hash;
  23.  
  24. class AuthenticationController extends Controller
  25. {
  26.  
  27.     public function getForcePasswordChange(Request $request){
  28.         return view('auth.forcepass')->with('userid', $request['id']);
  29.     }
  30.  
  31.     public function postForcePassword(Request $request){
  32.         $user = User::where('id', $request['userid'])->first();
  33.         if ($request['current'] == $user['password']){
  34.             $user['password'] = Hash::make($request['new']);
  35.             $user->save();
  36.             return redirect('login')->with('status', 'Password Changed successfully!');
  37.         } else{
  38.             return redirect()->back()->with('status', 'Old Password Does Not Match the Password Set By Admin');
  39.         }
  40.  
  41.     }
  42.  
  43.     public function getLogin(){
  44.         if(Auth::check()){
  45.             return redirect()->back();
  46.         }
  47.         return view('auth.login');
  48.     }
  49.  
  50.     public function postLogin(Request $request){
  51.         $user = User::where('email', $request['email'])->first();
  52.         if ($request['redirect']){
  53.             $redirect = urldecode($request['redirect']);
  54.         } else{
  55.             $redirect = '/';
  56.         }
  57.         if ($user){
  58.             if ($user->activated){
  59.                 if(Auth::viaRemember() || ($user['password']!==null && Auth::attempt(['email' => $request['email'],'password' => $request['password']],$request['remember_me']))){
  60.                     $interviewee = Interviewee::where('user_id', $user['id'])->first();
  61.                     $interviewer = Interviewer::where('user_id', $user['id'])->first();
  62.                     if ($interviewee){
  63.                         Session::put('type', 'interviewee');
  64.                     }
  65.                     if ($interviewer){
  66.                         Session::put('type', 'interviewer');
  67.                         Session::forget('cart_ids');
  68.                     }
  69.                     return redirect($redirect);
  70.                 }
  71.                 else if($user['password'] == $request['password']){
  72.                     // Redirect to change password page
  73.                     $url = action('AuthenticationController@getForcePasswordChange', ['id' => $user['id']]);
  74.                     return redirect($url);
  75.                     //return redirect('register');
  76.                 }
  77.                 else{
  78.                     return redirect()->back()->withInput()->with('status', 'Invalid Email or Password');
  79.                 }
  80.             }
  81.             else{
  82.                 return redirect()->back()->withInput()->with('status', 'Please Check Your Mail for Activation Link');
  83.             }
  84.         }
  85.         return redirect()->back()->withInput()->with('status', 'Invalid Email or Password');
  86.     }
  87.  
  88.     public function getRegisterInterviewee(Request $request){
  89.  
  90.         if (Auth::check()){
  91.             return redirect()->back();
  92.         }
  93.         return view('auth.register_interviewee');
  94.     }
  95.  
  96.     public function getRegisterInterviewer(Request $request){
  97.  
  98.         if (Auth::check()){
  99.             return redirect()->back();
  100.         }
  101.         return view('auth.register_interviewer');
  102.     }
  103.  
  104.     public function postRegister(Request $request){
  105.         $users = User::where('email', $request['email'])->first();
  106.         if ($users) {
  107.             return redirect()->back()->withInput()->with('status', 'Already Registered User');
  108.         } else {
  109.             $user = User::create(['email' => $request['email'],
  110.                 'first_name' => $request['fname'], 'last_name' => $request['lname'],
  111.                 'password' => Hash::make($request['password']), 'profile_image_url' => '0_profile_image.jpg',
  112.             ]);
  113.             try {
  114.                 $hash = strrev(md5($user['password']));
  115.                 $url = action('AuthenticationController@getActivation', ['email' => $user['email'], 'hash' => $hash]);
  116.                 $str = "Hi,\nPlease Visit the following link to activate your account\n" . $url;
  117.                 Mail::raw($str, function ($message) use ($user) {
  118.                     $message->from('noreply@mockinterviews.in', 'Mockinterviews.in');
  119.                     $message->to($user['email'], $user['fname'] . ' ' . $user['lname'])->subject('Welcome to Mockinterviews');
  120.                 });
  121.                 $user->save();
  122.                 if ($request['type'] == 'interviewee') {
  123.                     Interviewee::create(['user_id' => $user['id']]);
  124.                 } else if ($request['type'] == 'intervieweer') {
  125.                     Interviewer::create(['user_id' => $user['id']]);
  126.                 } else {
  127.                     throw new Exception();
  128.                 }
  129.                 return redirect('login')->with('status', 'Successfully registered! An account activation link has been sent to your email');;
  130.             } catch (Exception $e) {
  131.                 Log::error("catch mail");
  132.                 $user->delete();
  133.                 return redirect()->back()->withInput()->with('status', 'Something went wrong.Please Try Again');
  134.  
  135.             }
  136.         }
  137.     }
  138.  
  139.     public function getActivation(Request $request){
  140.         $user = User::where('email', $request['email'])->first();
  141.         if ($user['activated'] == false && md5($user['password']) == strrev($request['hash'])){
  142.             $user->activated = true;
  143.             $user->save();
  144.             return redirect('login')->with('status', 'Successfully activated');
  145.         }
  146.         return redirect('login');
  147.     }
  148.  
  149.     public function getLogout(){
  150.         Session::flush();
  151.         Auth::logout();
  152.         return redirect('/');
  153.     }
  154.  
  155.     public function getForgot(){
  156.         if (Auth::check()){
  157.             return redirect()->back();
  158.         }
  159.         return view('auth.forgot');
  160.     }
  161.  
  162.     public function postForgot(Request $request){
  163.         $user = User::where('email', $request['email'])->first();
  164.         if ($user){
  165.             $url = action('AuthenticationController@getForgotLink', ['email' => $user['email'], 'hash' => md5($user['password'])]);
  166.             $str = "Hi " . $user['first_name'] . ",\nYou requested that your password be reset.\nIf you didn't make this request then ignore this email,no changes have been made.\nIf you did request a password reset,please visit the following link to recover your account\n" . $url;
  167.             Mail::raw($str, function ($message) use ($user){
  168.                 $message->from('noreply@mockinterviews.in', 'Mockinterviews.in');
  169.                 $message->to($user['email'], $user['fname'] . ' ' . $user['lanme'])->subject('Mockinterviews, Forgot Password Link');
  170.             });
  171.             return redirect('login')->with('status', 'A password reset link has been sent to your email');
  172.         }
  173.         return view('auth.forgot')->with('status', 'Account not found');
  174.     }
  175.  
  176.     public function getForgotLink(Request $request){
  177.         if(Auth::check()){
  178.             return redirect()->back();
  179.         }
  180.         return view('auth.changepass');
  181.     }
  182.  
  183.     public function postForgotLink(Request $request){
  184.         $user = User::where('email', $request['userid'])->first();
  185.         if($request['hash'] == md5($user['password'])){
  186.             $user->password = Hash::make($request['new']);
  187.             $user->save();
  188.             return redirect('login')->with('status', 'Password Changed Successfully');
  189.         } else{
  190.             return redirect('forgot_password')->with('status', 'Link is Invalid');
  191.         }
  192.  
  193.     }
  194.  
  195.     public function setSession(Request $request){
  196.         $a = (Session::get('cart_ids'));
  197.         if ($a){
  198.             $s = array_values($a);
  199.         }
  200.         else{
  201.             $s = array();
  202.         }
  203.         if (!in_array($request['value'], $s, true)){
  204.             Session::push('cart_ids', $request['value']);
  205.         }
  206.         return '200';
  207.     }
  208.  
  209.     public function sendContactMail(Request $request){
  210.         $str = "From : " . $request['name'] . "\nMail : " . $request['email'] . "\nPhone No : " . $request['phone'] . "\nMessage : " . $request['message'];
  211.         Mail::raw($str, function ($message) use ($request){
  212.             $message->from('noreply@mockinterviews.in', 'Mockinterviews.in');
  213.             $message->to('contact@mockinterviews.in', 'Contact Us')->subject('Query email');
  214.         });
  215.         return redirect()->back()->with('status', 'Something went wrong.Please Try Again');
  216.     }
  217.  
  218.     public function getAbout(Request $request){
  219.         return view("about");
  220.     }
  221.  
  222.     public function getDashboard(Request $request){
  223.         if (Auth::check() && Session::get('type') == 'interviewer'){
  224.             return $this->getInterviewerProfile($request, Interviewer::where('user_id', '=', Auth::user()->id)->first()->id);
  225.         } elseif (Auth::check() && Session::get('type') == 'interviewee'){
  226.             return $this->getIntervieweeProfile($request, Interviewee::where('user_id', '=', Auth::user()->id)->first()->id);
  227.         } else{
  228.             return redirect('/');
  229.         }
  230.     }
  231.  
  232.     public function getInterviewerProfile(Request $request, $id){
  233.         if(!self::isMinimumProfileComplete()){
  234.             return redirect(self::$minimumProfileCompletionRedirectPath);
  235.         }
  236.         $interviewer = Interviewer::where('id', '=', $id)->first();
  237.         $availabilites_as_unix_timestamp = array();
  238.         $appointments_as_unix_timestamp = array();
  239.         $availabilites = $interviewer->availabilitiesNotAppointments();
  240.         $appointments = $interviewer->appointments();
  241.         foreach ($availabilites as $availability){
  242.             $availabilites_as_unix_timestamp[Carbon::parse($availability->start_timestamp)->timestamp] = true;
  243.         }
  244.         foreach ($appointments as $appointment){
  245.             $appointments_as_unix_timestamp[Carbon::parse($appointment->availability->start_timestamp)->timestamp] = true;
  246.         }
  247.         return view('main.interviewer_dashboard', compact('interviewer', 'availabilites_as_unix_timestamp', 'appointments_as_unix_timestamp'));
  248.  
  249.     }
  250.  
  251.     public function getIntervieweeProfile(Request $request, $id){
  252.         if(!self::isMinimumProfileComplete()){
  253.             return redirect(self::$minimumProfileCompletionRedirectPath);
  254.         }
  255.         $interviewer = Interviewee::where('id', '=', $id)->first();
  256.         return view('main.interviewee_dashboard', compact('interviewer'));
  257.     }
  258.  
  259.     public function redirectToProvider($register_type, $provider){
  260.         Session::put('register_type', $register_type);
  261.         return Socialite::driver($provider)->redirect();
  262.     }
  263.  
  264.     public function handleProviderCallback($provider){
  265.         try {
  266.             $user = Socialite::driver($provider)->user();
  267.         }
  268.         catch(\Exception $e){
  269.             return redirect('/login')->with('status','Access denied for '.$provider);
  270.         }
  271.         $data = [
  272.             'first_name' => null,
  273.             'last_name' => null,
  274.             'email' => null,
  275.             'google_access_token' => null,
  276.             'linkedin_access_token' => null,
  277.             'activated' => true,
  278.         ];
  279.         if($provider=='google'){
  280.             $data['first_name'] = $user['name']['givenName'];
  281.             $data['last_name'] = $user['name']['familyName'];
  282.             $data['email'] = $user->getEmail();
  283.             $data['google_access_token'] = $user->token;
  284.         }
  285.         else if($provider=='linkedin'){
  286.             $data['first_name'] = $user['firstName'];
  287.             $data['last_name'] = $user['lastName'];
  288.             $data['email'] = $user->getEmail();
  289.             $data['linkedin_access_token'] = $user->token;
  290.         }
  291.         $register_type = Session::get('register_type');
  292.         if($data['email']!=null && ($register_type=='interviewer' || $register_type=='interviewee' || $register_type=='login')){
  293.             $user=User::where('email', $data['email'])->first();
  294.             if(!$user &&  $register_type=='login'){
  295.                 return redirect('login')->with('status','Please register first');
  296.             }
  297.             if($user){
  298.                 if ($provider=='google'){
  299.                     $user->google_access_token=$data['google_access_token'];
  300.                 } else{
  301.                     $user->linkedin_access_token=$data['linkedin_access_token'];
  302.                 }
  303.                 $user->save();
  304.             }
  305.             else{
  306.                 $user=User::create($data);
  307.                 if($register_type=='interviewee'){
  308.                     Interviewee::create(['user_id' => $user['id']]);
  309.                 }
  310.                 else if($register_type=='interviewer'){
  311.                     Interviewer::create(['user_id' => $user['id']]);
  312.                 }
  313.                 Session::forget('register_type');
  314.             }
  315.             Auth::login($user);
  316.             $interviewee=Interviewee::where('user_id', $user['id'])->first();
  317.             $interviewer=Interviewer::where('user_id', $user['id'])->first();
  318.             if ($interviewee){
  319.                 Session::put('type', 'interviewee');
  320.             }
  321.             else if($interviewer){
  322.                 Session::put('type', 'interviewer');
  323.                 Session::forget('cart_ids');
  324.             }
  325.             if(!self::isMinimumProfileComplete()){
  326.                 return redirect(self::$minimumProfileCompletionRedirectPath);
  327.             }
  328.             return redirect('/');
  329.         }
  330.         return redirect()->back()->with('status', 'Something went wrong.Please try again.');
  331.     }
  332.  
  333.     public function getMinimumCompleteProfile(Request $request){
  334.         if(Auth::check()){
  335.             return view('auth.complete_profile');
  336.         }
  337.         return redirect('login');
  338.     }
  339.  
  340.     public function postMinimumCompleteProfile(Request $request){
  341.         if(Auth::check() && !self::isMinimumProfileComplete()){
  342.             $user = Auth::user();
  343.             $d = explode('/', $request['dob']);
  344.             $date = $d[2] . '-' . $d[1] . '-' . $d[0];
  345.             $user->phone_no = $request['phone_number'];
  346.             $user->city = $request['city'];
  347.             $user->bio = $request['bio'];
  348.             $user->dob = $date;
  349.             $user->gender = $request['gender'];
  350.             $user->save();
  351.             return redirect('/');
  352.             //return redirect('/dashboard');
  353.         }
  354.         return redirect('login');
  355.     }
  356.  
  357.     public function saveAvailabilities(Request $request){
  358.         //get user id
  359.         $availability_post_data = Input::get('availability_post_data', -1);
  360.         if ($availability_post_data != -1 && Auth::check()){
  361.             $availability_post_data = explode(',', $availability_post_data);
  362.             $interviewer_id = Interviewer::where('user_id', '=', Auth::user()->id)->first()->id;
  363.             //Delele All availaibilities of the requester except appointments
  364.             Availability::where('interviewer_id', '=', $interviewer_id)->whereNotIn('id', App\Appointment::all()->lists('availability_id')->toArray())->delete();
  365.             //parse and store new ones
  366.             $insert_data = array();
  367.             foreach ($availability_post_data as $availability){
  368.                 $temp = explode('-', $availability);
  369.                 array_push($insert_data, array('start_timestamp' => Carbon::createFromTimestamp($temp[0]),
  370.                     'end_timestamp' => Carbon::createFromTimestamp($temp[1]),
  371.                     'interviewer_id' => $interviewer_id,
  372.                     'created_at' => Carbon::now(),
  373.                     'updated_at' => Carbon::now()
  374.                 ));
  375.             }
  376.             Availability::insert(array_values($insert_data));
  377.         } else{
  378.             Availability::where('end_timestamp', '<', Carbon::now())->whereNotIn('id', App\Appointment::all()->lists('availability_id')->toArray())->delete();
  379.             return redirect('/');
  380.         }
  381.         return "OK";
  382.     }
  383.    
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement