first() ? exit('Your IP Is Blocked!') : ''; BlockedUserAgent::where('name', '=', $user_agent)->first() ? exit('Your IP Is Blocked!') : ''; $user = User::where('username', '=', $username) ->where('password', '=', $password) ->where('active', '=', 1)->first(); !$user ? die('No User Found') : ''; $user_id = $user->id; $max_connections = $user->max_connections; $exp_date = $user->exp_date; $activity = $user->activity()->where('date_end', '=', null)->get(); $count = $activity->count(); if($user->exp_date != '0000-00-00') { if($user->exp_date <= $today::today()) { die('Expired'); } } if($max_connections != 0 && $count >= $max_connections) { $activityTmp = Activity::where('user_id', '=', $user_id) ->where('user_ip','=', $user_ip) ->where('date_end', '=', null)->first(); if($activityTmp != null) { if($activityTmp->count() > 0) { if(getActivity($activityTmp->id) === true) { --$count; } } } } $stream=Stream::find($_GET["stream"]); if(!$stream) { die("stream not found2"); } if($max_connections==0 || $count < $max_connections) { $info["type"] = "live"; if($user_activity_id != 0) { $activity = Activity::find($user_activity_id); } else { $activity = new Activity(); } $activity->user_id = $user->id; $activity->stream_id = $stream->id; $activity->user_agent = $user_agent; $activity->user_ip = $user_ip; $activity->pid = getmypid(); $activity->bandwidth = 0; $activity->date_start = $today::NOw(); $activity->save(); $user_activity_id = $activity->id; $user->lastconnected_ip = $user_ip; $user->last_stream = $stream->id; $user->useragent = $user_agent; $user->SAVE(); $setting = Setting::first(); $streamurl = $stream->streamurl; if($stream->checker == 2) { $streamurl=$stream->streamurl2; } if($stream->checker == 3) { $streamurl=$stream->streamurl3; } if($stream->restream == false) { $hlsFolder = $setting->hlsfolder."/"; $blarg = 16; if($files = getFiles($setting->hlsfolder."/".$stream->id."_.m3u8")) { foreach($files as $file) { if(file_exists($hlsFolder.$file)) { clientConnected(); readfile($hlsFolder.$file); } else { exit(); } } $file = array_pop($files); preg_match("/_(.*)\./", $file, $matches); $tsCounter = $matches[1]; $counter = 0; while(($counter <= $blarg) && clientConnected() && file_exists($setting->hlsfolder."/").$stream->id."_.m3u8") { $ts1 = sprintf($stream->id."_%d.ts", $tsCounter + 1); $ts2 = sprintf($stream->id."_%d.ts", $tsCounter + 2); if(!file_exists($hlsFolder.$ts1)) { sleep(1); $counter2++; continue; } $counter = 0; $fhandle = fopen($hlsFolder.$ts1, "r"); while(($counter2 <= $blarg) && clientConnected() &&!file_exists($hlsFolder.$ts2)) { $content = stream_get_line($fhandle, 4096); if(empty($content)) { sleep(1); ++$counter2; continue; } echo $content; $counter = 0; } echo stream_get_line($fhandle,filesize($hlsFolder.$ts1)-ftell($fhandle)); fclose($fhandle); $counter = 0; $tsCounter++; } } } else { $fhandle = fopen($streamurl, "r"); while(!feof($fhandle) && clientConnected()) { echo fread($fhandle, 5120); $bytes += 5120; ob_flush(); flush(); if(connection_aborted()) { $activity->date_end = $today::now(); $activity->pid = null; $activity->save(); break; } } fclose($fhandle); exit(); die(); } } else { die("Total Max Connections Reached"); } function pidExists($pid) { if(empty($pid)) { return false; } return file_exists("/proc/".$pid); } function getActivity($id) { $activity = Activity::find($id); if($activity->count() > 0) { if(!is_null($activity->pid)) { if(pidExists($activity->pid)) { if(posix_kill($activity->pid, 9)) { $activity->date_end = $today::now(); $activity->pid = null; $activity->save(); } else { return posix_strerror(posix_get_last_error()); } } else { $activity->date_end = $today::now(); $activity->pid = null; $activity->save(); } } } return true; } function getFiles($file, $bla = null) { if(file_exists($file)) { $file = file_get_contents($file); if(preg_match_all("/(.*?).ts/", $file, $matches)) { return $matches[0]; } } return false; } function clientConnected() { if(connection_status() != 0 || connection_aborted()) { return false; } return true; } function shutdown() { global $user_activity_id, $info; if ( $user_activity_id != 0 ) { $active = Activity::find($user_activity_id); $active->date_end = Carbon::now(); $active->save(); } if ( empty( $info['type'] ) || $info['type'] == 'live' ) { if(function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } if(function_exists('posix_kill')) { posix_kill(getmypid(), 9); } } else { exit; } }