HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/dev-telemedicina.teky.com.co/app/Http/Controllers/OpenTokController.php
<?php

namespace Telemedicina\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Telemedicina\Http\Controllers\OneSignalController;
use OpenTok\OpenTok;
use OpenTok\Role;
use Telemedicina\User;
use Telemedicina\VideoOpenTok;
use Config;
use Carbon\Carbon;
use Session;

class OpenTokController extends Controller
{
    private $opentok;
    protected $TIME_SESSION = 10 * 60; // Day

    public function __construct(){
        $this->opentok = new OpenTok(Config::get('services.opentok.api_key'), Config::get('services.opentok.secret'));
    }

    // Create a session that attempts to use peer-to-peer streaming:
    public function createSessionP2P(Request $request){
        if(Auth::user()->id == $request->host_id){
            Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.same_user_error')));
            return redirect()->back();
        }
        if(!$request->guest_id && !$request->document){
            Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.not_valid_user_error')));
            return redirect()->back();
        }
        $request_guest_id = $request->guest_id;

        if(!$request_guest_id){
            $patient = User::where('document', $request->document)->first();
            $request_guest_id = $patient->id;
            
        }else{
            $patient = User::where('id', $request_guest_id)->first();
        }
        
        $exist = VideoOpenTok::where('host_id', Auth::user()->id)->where('guest_id', $request_guest_id)->first();

        if($exist){ // Ya existe la sala.

            if($exist->is_host_login && $exist->is_guest_login){
                Session::flash('data', array('type' => 'error', 'message' => __('messages.opentok.busy_session')));
                return redirect()->back();
            }

            $startTime  = Carbon::parse($exist->created_at);
            $current    =  Carbon::now();

            $diff = $startTime->diffInMinutes($current);

            if($diff < 55){ // Si esta activa.
                $this->sendNotificationUser($request_guest_id);
                $token = $this->createTokenFromSessionId($exist->session_id);

                return view ('video-chat.video-join')
                            ->with('session_id', $exist->session_id)
                            ->with('token', $token)
                            ->with('api_key', Config::get('services.opentok.api_key'))
                            ->with('patient_name', $patient->name . " " . $patient->last_name);

            }else{
                $exist->delete();
            }
        }

        $session = $this->opentok->createSession();

        // Set some options in a token
        $token = $session->generateToken(array(
            'role'       => Role::PUBLISHER,
            'expireTime' => time()+($this->TIME_SESSION),
            'data'       => 'name=' . Auth::user()->name . Auth::user()->last_name,
            'initialLayoutClassList' => array('focus')
        ));

        // Store Session.
        $v = new VideoOpenTok;

        $v->session_id = $session->getSessionId();
        $v->host_id = Auth::user()->id;
        $v->guest_id = $request_guest_id;
        $v->is_host_login = true;
        $v->is_full = false;
        $v->active = true;

        $v->save();

        $patient = User::where('id', $request_guest_id)->first();

        $this->sendNotificationUser($request_guest_id);

        return view ('video-chat.video-join')
                ->with('session_id', $v->session_id)
                ->with('token', $token)
                ->with('api_key', Config::get('services.opentok.api_key'))
                ->with('patient_name', $patient->name . " " . $patient->last_name);
    }

    private function createTokenFromSessionId($sessionId){
        return $this->opentok->generateToken(
            $sessionId,
            array(
            'role'       => Role::PUBLISHER,
            'expireTime' => time()+($this->TIME_SESSION),
            'data'       => 'name=' . Auth::user()->name . Auth::user()->last_name,
            'initialLayoutClassList' => array('focus')
        ));
    }

    private function sendNotificationUser($user_id){
        $tags = array(
            ["field" => "tag", "key" => "user_id", "relation" => "=", "value" => $user_id],
            ["field" => "tag", "key" => "app_name", "relation" => "=", "value" => Config::get('app.name')]
        );

        $data = array(
            "type" => "videocall",
            "id" => Auth::user()->id
        );

        OneSignalController::sendToTags(__('messages.opentok.notification_text'), $tags, null, $data, null, null, 'U');
    }


    // Create a token from session ID.
    public function joinSessionUser(Request $request){
        $v = VideoOpenTok::where('session_id', $request->session_id)->first();
        if(!$v){
            return array('m' => __('messages.opentok.not_valid_session'));
        }

        $token = $this->opentok->generateToken($v->session_id);

        return view ('video-chat.video-join')
                ->with('session_id', $v->session_id)
                ->with('token', $token)
                ->with('api_key', Config::get('services.opentok.api_key'));
    }

    public function unjoinSessionUser(Request $request){
        $v = VideoOpenTok::where('session_id', $request->session_id)
                        ->where('host_id', Auth::user()->id)->first();

        if(!$v){
            return array('m' => __('messages.opentok.not_valid_session'));
        }

        $v->delete();
        return array('m' => 'true');
    }

}