-
Notifications
You must be signed in to change notification settings - Fork 46
/
app-call.php
464 lines (318 loc) · 11.1 KB
/
app-call.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
//app-call.php : called by app to communicate with web server (login, config, status, interactions)
//This is sample code for demostrating some features. Not designed for production.
//Integrate with own platform login and database system.
//For a functional turnkey integration see PaidVideochat.com turnkey site solution and plugin source code https://plugins.svn.wordpress.org/ppv-live-webcams/trunk/inc/h5videochat.php .
include_once 'settings.php';
include_once 'app-functions.php';
//session info received trough VideoWhisper POST var
if ($VideoWhisper = $_POST['VideoWhisper'] )
{
$userID = intval($VideoWhisper['userID']);
$sessionID = intval($VideoWhisper['sessionID']);
$roomID = intval($VideoWhisper['roomID']);
$sessionKey = intval($VideoWhisper['sessionKey']);
$privateUID = intval($VideoWhisper['privateUID'] ?? 0);
$roomActionID = intval($VideoWhisper['roomActionID'] ?? 0);
}
$response['VideoWhisper'] = 'https://videowhisper.com';
$task = $_POST['task'];
if ($task != 'login')
{
//verify user login, session validty
}
if ($task == 'login')
{
//user session parameters and info, updates
$response['user'] = [
'ID'=> intval($userID),
'name'=> (($userID>10000)?'Performer':'User') . $userID,
'sessionID'=> intval($sessionID),
'loggedIn' => true,
'balance' => 100,
'avatar' => VW_H5V_URL .'images/avatar.png',
];
//on login check if any private request was active to restore
//return private room/session if active, depending on integration
$response['room'] = appPublicRoom($roomID, $userID, $options, 'Login success!');
//config params, const
$response['config'] = [
'serverType' => $options['serverType'],
'vwsSocket' => $options['vwsSocket'],
'vwsToken' => $options['vwsToken'],
'wss' => $options['wsURLWebRTC'],
'application' => $options['applicationWebRTC'],
'videoCodec' => $options['webrtcVideoCodec'],
'videoBitrate' => $options['webrtcVideoBitrate'],
'audioBitrate' => $options['webrtcAudioBitrate'],
'audioCodec' => $options['webrtcAudioCodec'],
'snapshotInterval' => 180,
'snapshotDisable' => true,
// 'autoBroadcast' => false,
'actionFullscreen' => true,
'actionFullpage' => false,
'serverURL' => VW_H5V_CALL,
'modeVersion' => '',
];
$response['config']['text'] = appText(); //translations
$response['config']['sfx'] = appSfx();
$response['config']['exitURL'] = VW_H5V_URL . 'info.php?i=exit';
$response['config']['balanceURL'] = VW_H5V_URL . 'info.php?i=wallet' ;
//pass app setup config parameters
if (is_array($options['appSetup']))
if (array_key_exists('Config', $options['appSetup']))
if (is_array($options['appSetup']['Config']))
foreach ($options['appSetup']['Config'] as $key => $value)
$response['config'][$key] = $value;
if (VW_DEVMODE)
{
// $response['config']['cameraAutoBroadcast'] = '0';
// $response['config']['videoAutoPlay '] = '0';
}
//if (!$isPerformer) $response['config']['cameraAutoBroadcast'] = '0';
$response['config']['loaded'] = true;
}
//end: task==login
//room
$roomName = 'Room' . $roomID;
$userName = (($userID>10000)?'Performer':'User') . $userID;
$ztime = time();
//create/update session in room session list
$sessions = arrayLoad($roomID . '_sessions');
if (array_key_exists($sessionID, $sessions)) $session = $sessions[$sessionID];
else $session = array(
'id' => $sessionID,
'uid' => $userID,
'sdate' => time(),
'madeBy' => 'access',
);
$session['edate'] = time();
$sessions[$sessionID] = $session;
varSave($roomID . '_sessions', $sessions);
//update private session if in private mode
$needUpdate = array();
//process app task (other than login)
switch ($task)
{
case 'snapshot':
break;
case 'login':
break;
case 'tick':
break;
case 'options':
break;
case 'update':
//something changed - let everybody know : update room
$update = filter_var($_POST['update'], FILTER_SANITIZE_STRING);
$room = arrayLoad($roomID . '_room');
$room['updated_' . $update] = time();
varSave($roomID . '_room', $room);
$needUpdate[$update] = 1;
break;
case 'media':
//notify user media (streaming) updates
$connected = ($_POST['connected'] == 'true'?true:false);
if ($session['meta'])
if (!is_array($userMeta = unserialize($session['meta']))) $userMeta = array();
$userMeta['connected'] = $connected;
$userMeta['connectedUpdate'] = time();
$userMetaS = serialize($userMeta);
$sessions = arrayLoad($roomID . '_sessions');
if (!array_key_exists($sessionID, $sessions)) $sessions[$sessionID] = array('id' =>$sessionID, 'sdate' => time(),
'madeBy'=>'media');
$sessions[$sessionID]['meta'] = $userMetaS;
varSave($roomID . '_sessions', $sessions);
break;
case 'message':
$message = $_POST['message']; //array
if ($message)
{
$response['message'] = $message;
}
$messageText = filter_var( $message['text'], FILTER_SANITIZE_STRING);
$messageUser = filter_var( $message['userName'], FILTER_SANITIZE_STRING);
$messageUserAvatar = filter_var( $message['userAvatar'], FILTER_SANITIZE_URL);
$meta = array(
'notification'=> filter_var($message['notification'],FILTER_SANITIZE_STRING),
'userAvatar' => $messageUserAvatar,
'mentionMessage' => intval($message['mentionMessage']),
'mentionUser'=> filter_var($message['mentionUser'],FILTER_SANITIZE_STRING),
);
$metaS = serialize($meta);
if (!$privateUID) $privateUID = 0; //public room
$messages = arrayLoad($roomID . '_messages');
$ix = count($messages)+1;
$messageNew = array(
'id' => $ix,
'username' => $messageUser,
'room' => $roomName,
'room_id' => $roomID,
'message' => $messageText,
'mdate' => $ztime,
'type' => 2,
'user_id' => $userID,
'meta' => $metaS,
'private_uid' => $privateUID
);
$messages[$ix] = $messageNew;
$response['messageNew'] = $messageNew;
varSave($roomID . '_messages', $messages);
break;
case 'recorder_upload':
if (!$roomName) appFail('No room for recording.');
if (strstr($filename, ".php")) appFail('Bad uploader!');
$mode = $_POST['mode']; // video/audio
$scenario = $_POST['scenario']; // chat/standalone
if (!$privateUID) $privateUID = 0; //public room
//generate same private room folder for both users
if ($privateUID)
{
if ($isPerformer) $proom = $userID . "_" . $privateUID; //performer id first
else $proom = $privateUID ."_". $userID;
}
$destination = 'uploads';
if (!file_exists($destination)) mkdir($destination);
$destination.="/$roomName";
if (!file_exists($destination)) mkdir($destination);
if ($proom)
{
$destination.="/$proom";
if (!file_exists($destination)) mkdir($destination);
}
$response['_FILES'] = $_FILES;
$allowed = array('mp3', 'ogg', 'opus', 'mp4', 'webm', 'mkv');
$uploads = 0;
$filename = '';
if ($_FILES) if (is_array($_FILES))
foreach ($_FILES as $ix => $file)
{
$filename = filter_var( $file['name'], FILTER_SANITIZE_STRING);
$ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$response['uploadRecLastExt'] = $ext;
$response['uploadRecLastF'] = $filename;
$filepath = $destination . '/' . $filename;
if (in_array($ext, $allowed))
if (file_exists($file['tmp_name']))
{
move_uploaded_file($file['tmp_name'], $filepath);
$response['uploadRecLast'] = $destination . $filename;
$uploads++;
}
}
$response['uploadCount'] = $uploads;
if (!file_exists($filepath))
{
$response['warning'] = 'Recording upload failed!';
}
if ( !$response['warning'] && $scenario == 'chat' )
{
$url = path2url($filepath);
$response['recordingUploadSize'] = filesize($filepath);
$response['recordingUploadURL'] = $url;
$messageText = '';
$messageUser = $userName;
$userAvatar = VW_H5V_URL .'images/avatar.png';
$messageUserAvatar = $userAvatar;
$meta = array(
'userAvatar' => $messageUserAvatar,
);
if ($mode == 'video') $meta['video']= $url;
else $meta['audio']= $url;
$metaS = serialize($meta);
$messages = arrayLoad($roomID . '_messages');
$ix = count($messages)+1;
$messageNew = array(
'id' => $ix,
'username' => $messageUser,
'room' => $roomName,
'room_id' => $roomID,
'message' => $messageText,
'mdate' => $ztime,
'type' => 2,
'user_id' => $userID,
'meta' => $metaS,
'private_uid' => $privateUID
);
$messages[$ix] = $messageNew;
varSave($roomID . '_messages', $messages);
$response['messageNew'] = $messageNew;
//also update chat log file
/*
if ($roomName)
{
$messageText = strip_tags($messageText, '<p><a><img><font><b><i><u>');
$messageText = date("F j, Y, g:i a", $ztime) . " <b>$userName</b>: $messageText <video controls src='$url'></video>";
$day=date("y-M-j", time());
$dfile = fopen($destination . "/Log$day.html", "a");
fputs($dfile, $messageText."<BR>");
fclose($dfile);
}
*/
}
break;
default:
$response['warning'] = 'Not implemented in this integration: ' . $task;
}
//update time
$lastMessage = intval($_POST['lastMessage'] ?? 0);
$lastMessageID = intval($_POST['lastMessageID'] ?? 0);
//retrieve only messages since user came online / updated
$sdate = 0;
if ($session) $sdate = $session['sdate'];
$startTime = max($sdate, $lastMessage);
$response['startTime'] = $startTime;
//!messages
$messages = arrayLoad($roomID . '_messages');
//clean old chat logs
$closeTime = time() - 900; //only keep for 15min
foreach ($messages as $key => $message) if ($message['mdate'] < $closeTime) unset($messages[$key]);
varSave($roomID . '_messages', $messages);
//sort by key (time)
ksort($messages);
$items = array();
$idMax = 0;
foreach ($messages as $key => $message)
{
$show = 1;
//chat message or own notification (type 3)
if ($message['type'] > 3) $show = 0;
if ($message['type'] == 3 && ($message['user_id'] != $userID && $message['username'] != $userName) ) $show = 0;
if (!$privateUID) if ($message['private_uid'] != 0) $show = 0; //private messages only in private
if ($privateUID)
{
if ($message['private_uid'] != $privateUID) $show = 0; //not in this private
if ($message['user_id'] != $userID && $message['user_id'] != $privateUID) $show = 0; //not for user or other
}
//enable this to show only messages after entering $startTime:
//if ($message['mdate'] < $startTime || $message['mdate'] > $ztime) $show = 0;
if ($message['id'] <= $lastMessageID) $show = 0;
$idMax = 0;
if ($show)
{
$item = [];
$item['ID'] = intval($message['id']);
if ($item['ID']>$idMax) $idMax = $item['ID'];
$item['userName'] = $message['username'];
$item['userID'] = intval($message['user_id']);
$item['text'] = $message['message'];
$item['time'] = intval($message['mdate'] * 1000); //time in ms for js
$item['userAvatar'] = VW_H5V_URL .'images/avatar.png';
//meta
if ($message['meta'])
{
$meta = unserialize($message['meta']);
foreach ($meta as $key=>$value) $item[$key] = $value;
$item['notification'] = (isset($meta['notification']) && $meta['notification'] == 'true' ? true : false );
}
if ($message['type'] == 3) $item['notification'] = true;
$items[] = $item;
}
}
$response['messages'] = $items; //messages list
$response['timestamp'] = $ztime; //update time
///update message
$response['lastMessageID'] = $idMax;
$response['roomUpdate']['users'] = appRoomUsers($roomID, $options);
//send response to app
echo json_encode($response);