-
Notifications
You must be signed in to change notification settings - Fork 20
/
stream.php
129 lines (127 loc) · 6.19 KB
/
stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
error_reporting(E_ALL);
set_time_limit(0);
include("config.php");
function closed()
{
global $user_activity_id;
if ($user_activity_id != 0) {
$active = Activity::find($user_activity_id);
$active->date_end = date('Y-m-d H:i:s');
$active->save();
}
fastcgi_finish_request();
exit;
}
$user_activity_id = 0;
$user_ip = $_SERVER['REMOTE_ADDR'];
header("Access-Control-Allow-Origin: *");
register_shutdown_function('closed');
header("Content-Type: video/mp2t");
ob_end_flush();
if (isset($_GET['username']) && isset($_GET['password']) && isset($_GET['stream'])) {
$user_agent = (empty($_SERVER['HTTP_USER_AGENT'])) ? "0" : trim($_SERVER['HTTP_USER_AGENT']);
$username = $_GET['username'];
$password = $_GET['password'];
$stream_id = intval($_GET['stream']);
if (!BlockedUseragent::where('name', '=', $user_agent)->first())
if (!BlockedIp::where('ip', '=', $_SERVER['REMOTE_ADDR'])->first()) {
if ($user = User::where('username', '=', $username)->where('password', '=', $password)->where('active', '=', 1)->first()) {
} else {
$log = "Worning --> Ip: [".$_SERVER['REMOTE_ADDR'].'] - '.date("d-m-Y H:i:s").
" - Attempt ".('Failed Login -').
" User: ".$username.
" Pass: ".$password.
" ".PHP_EOL;
file_put_contents('/home/fos-streaming/fos/www1/log/fos-loginfail'.'.log', $log, FILE_APPEND);
sleep (10);
}
if ($user = User::where('username', '=', $username)->where('password', '=', $password)->where('active', '=', 1)->first()) {
if ($user->exp_date == "0000-00-00" || $user->exp_date > date('Y-m-d H:i:s')) {
$user_id = $user->id;
$user_max_connections = $user->max_connections;
$user_expire_date = $user->exp_date;
$user_activity = $user->activity()->where('date_end', '=', NULL)->get();
$active_cons = $user_activity->count();
if ($user_max_connections != 1 && $active_cons >= $user_max_connections) {
$maxconntactionactivity = Activity::where("user_id", "=", $user_id)->where("user_ip", "=", $user_ip)->where("date_end", "=", null)->first();
if ($maxconntactionactivity != null) {
if ($maxconntactionactivity->count() > 0) {
--$active_cons;
}
}
}
if ($user_max_connections == 0 || $active_cons < $user_max_connections) {
if ($stream = Stream::find($_GET['stream'])) {
if ($user_activity_id != 0) {
$active = Activity::find($user_activity_id);
} else {
$active = new Activity();
}
$active->user_id = $user->id;
$active->stream_id = $stream->id;
$active->user_agent = $user_agent;
$active->user_ip = $user_ip;
$active->pid = getmypid();
$active->bandwidth = 0;
$active->date_start = date('Y-m-d H:i:s');
$active->save();
$user_activity_id = $active->id;
$user->lastconnected_ip = $_SERVER['REMOTE_ADDR'];
$user->last_stream = $stream->id;
$user->useragent = $user_agent;
$user->save();
$setting = Setting::first();
if ($stream->checker == 2) {
$url = $stream->streamurl2;
} else if ($stream->checker == 3) {
$url = $stream->streamurl3;
} else {
$url = $stream->streamurl;
}
$folder = $setting->hlsfolder . '/';
$files = "";
$file = $setting->hlsfolder . '/' . $stream->id . '_.m3u8';
if (file_exists($file) && preg_match_all("/(.*?).ts/", file_get_contents($file), $data)) {
$files = $data[0];
foreach ($files as $file) {
if (!file_exists($folder . $file)) {
exit();
}
readfile($folder . $file);
}
preg_match("/_(.*)\\./", array_pop($files), $clean);
$segment = $clean[1];
$try = 0;
while (($try <= 16) && file_exists($setting->hlsfolder . '/' . $stream->id . '_.m3u8')) {
$next = sprintf($stream->id . "_%d.ts", $segment + 1);
$nextnext = sprintf($stream->id . "_%d.ts", $segment + 2);
if (!file_exists($folder . $next)) {
sleep(1);
$try++;
continue;
}
$try = 0;
$fopen = fopen($folder . $next, "r");
while (($try <= 16) && !file_exists($folder . $nextnext)) {
$line = stream_get_line($fopen, 4096);
if (empty($line)) {
sleep(1);
++$try;
continue;
}
echo $line;
$t = 0;
}
echo stream_get_line($fopen, filesize($folder . $next) - ftell($fopen));
fclose($fopen);
$try = 0;
$segment++;
}
}
}
}
}
}
}
}