diff --git a/plugin/Live/Live.php b/plugin/Live/Live.php index 55258e25a..1e422ae70 100644 --- a/plugin/Live/Live.php +++ b/plugin/Live/Live.php @@ -1,2489 +1,2490 @@ and receive HLS streaming from servers"; $lu = AVideoPlugin::loadPlugin("LiveUsers"); if (!empty($lu)) { if (version_compare($lu->getPluginVersion(), "2.0") < 0) { $desc .= "
',
empty($obj->disableGifThumbs) ? ('') : "",
"col-lg-2 col-md-4 col-sm-4 col-xs-6",
($liveUsers ? getLiveUsersLabelLive($value->name, $live_servers_id) : ''),
'liveVideo',
'LiveVideo'
);
$newContent = str_replace($search, $replace, $content);
$newContentExtra = str_replace($search, $replace, $contentExtra);
$newContentExtraVideoPage = str_replace($search, $replace, $contentExtraVideoPage);
$newContentVideoListItem = str_replace($search, $replace, $contentListem);
$obj->applications[] = array(
"html" => $newContent,
"htmlExtra" => $newContentExtra,
"htmlExtraVideoPage" => $newContentExtraVideoPage,
"htmlExtraVideoListItem" => $newContentVideoListItem,
"live_index" => $live_index,
"live_cleanKey" => $parameters['cleanKey'],
"key" => $value->name,
"isAdaptive" => self::isAdaptive($value->name),
"isPrivate" => self::isAPrivateLiveFromLiveKey($value->name),
"users" => $users,
"name" => $userName,
"user" => $user,
"photo" => $photo,
"UserPhoto" => $UserPhoto,
"title" => $title,
'channelName' => $channelName,
'poster' => $poster,
'imgGif' => $p->getLivePosterImage($row['users_id'], $live_servers_id, $playlists_id_live, $live_index, 'webp'),
'link' => addQueryStringParameter($link, 'embed', 1),
'href' => $link,
'playlists_id_live' => $playlists_id_live,
'm3u8' => $m3u8,
'isURL200' => isURL200($m3u8),
'users_id' => $row['users_id'],
'live_servers_id' => $live_servers_id,
'categories_id' => intval($row['categories_id']),
'className' => $uid
);
if ($value->name === $obj->name) {
$obj->error = property_exists($value, 'publishing') ? false : true;
$obj->msg = (!$obj->error) ? "ONLINE" : "Waiting for Streamer";
$obj->stream = $value;
$obj->nclients = intval($value->nclients);
break;
}
}
}
$obj->countLiveStream = count($obj->applications);
$obj->error = false;
$_getStats[$live_servers_id][$_REQUEST['name']] = $obj;
//_error_log("Live::_getStats NON cached result {$_REQUEST['name']} " . json_encode($obj));
ObjectYPT::setCache($cacheName, json_encode($obj));
return $obj;
}
static function getLiveParametersFromKey($key) {
$key = preg_replace('/[^a-z0-9_-]/i', '', $key);
$obj = AVideoPlugin::getObjectData('Live');
$playlists_id_live = false;
if (preg_match("/.*_([0-9]+)/", $key, $matches)) {
if (!empty($matches[1])) {
$playlists_id_live = intval($matches[1]);
}
}
$live_index = '';
if (preg_match("/.*-([0-9a-zA-Z]+)/", $key, $matches)) {
if (!empty($matches[1])) {
$live_index = strip_tags($matches[1]);
if ($live_index === 'false') {
$live_index = '';
}
}
}
$cleanKey = self::cleanUpKey($key);
return array('key' => $key, 'cleanKey' => $cleanKey, 'live_index' => $live_index, 'playlists_id_live' => $playlists_id_live);
}
static function getLiveIndexFromKey($key) {
$parameters = self::getLiveParametersFromKey($key);
return $parameters['live_index'];
}
static function cleanUpKey($key) {
if ($adapKey = self::isAdaptiveTransmition($key)) {
$key = $adapKey;
}
if ($plKey = self::isPlayListTransmition($key)) {
$key = $plKey;
}
if ($subKey = self::isSubTransmition($key)) {
$key = $subKey;
}
return $key;
}
static function isAdaptiveTransmition($key) {
// check if is a subtransmition
$parts = explode("_", $key);
if (!empty($parts[1])) {
$adaptive = array('hi', 'low', 'mid');
if (in_array($parts[1], $adaptive)) {
return $parts[0];
;
}
}
return false;
}
static function isPlayListTransmition($key) {
// check if is a subtransmition
$parts = explode("_", $key);
if (!empty($parts[1])) {
return $parts[0];
} else {
return false;
}
}
static function isSubTransmition($key) {
// check if is a subtransmition
$parts = explode("-", $key);
if (!empty($parts[1])) {
return $parts[0];
} else {
return false;
}
}
static function getImage($users_id, $live_servers_id, $playlists_id_live = 0, $live_index = '') {
$p = AVideoPlugin::loadPlugin("Live");
if (self::isLive($users_id, $live_servers_id, $live_index)) {
$url = $p->getLivePosterImage($users_id, $live_servers_id, $playlists_id_live, $live_index);
$url = addQueryStringParameter($url, "playlists_id_live", $playlists_id_live);
} else {
$url = self::getOfflineImage(false);
}
return $url;
}
static function getLatestKeyFromUser($users_id) {
if (empty($users_id)) {
return false;
}
$latest = LiveTransmitionHistory::getLatestFromUser($users_id);
if (empty($latest)) {
return false;
}
return $latest['key'];
}
static function isLive($users_id, $live_servers_id = 0, $live_index = '', $force_recreate = false) {
global $_live_is_live;
if (empty($users_id)) {
return false;
}
if (!isset($_live_is_live)) {
$_live_is_live = array();
}
$name = "{$users_id}_{$live_servers_id}";
if (!empty($_live_is_live[$name])) {
return $_live_is_live[$name];
}
$lh = LiveTransmitionHistory::getActiveLiveFromUser($users_id, $live_servers_id, '');
if (empty($lh)) {
_error_log("Live::isLive we could not found any active livestream for user $users_id, $live_servers_id");
return false;
}
$key = $lh['key'];
$_live_is_live[$name] = self::isLiveAndIsReadyFromKey($key, $live_servers_id, $live_index, $force_recreate);
return $_live_is_live[$name];
}
static function isKeyLiveInStats($key, $live_servers_id = 0, $live_index = '', $force_recreate = false) {
global $_isLiveFromKey;
if (empty($key) || $key == '-1') {
_error_log('Live::isKeyLiveInStats key is empty');
return false;
}
$index = "$key, $live_servers_id,$live_index";
if (!isset($_isLiveFromKey)) {
$_isLiveFromKey = array();
}
if (empty($force_recreate) && isset($_isLiveFromKey[$index])) {
_error_log('Live::isKeyLiveInStats key is already set');
return $_isLiveFromKey[$index];
}
//_error_log('getStats execute getStats: ' . __LINE__ . ' ' . __FILE__);
//$json = getStatsNotifications($force_recreate);
//_error_log('getStats execute getStats: ' . ($force_recreate?'force_recreate':'DO NOT force_recreate'));
$json = self::getStats($force_recreate);
+ _error_log('Live::isKeyLiveInStats:self::getStats ' . json_encode($json));
$_isLiveFromKey[$index] = false;
if (!empty($json)) {
_error_log("Live::isLiveFromKey {$key} JSON was not empty");
if (!is_array($json)) {
$json = array($json);
}
foreach ($json as $item) {
$applications = array();
if (empty($item->applications) && is_array($item)) {
$applications = $item;
} else if (is_object($item) && !empty($item->applications)) {
$applications = $item->applications;
}
foreach ($applications as $value) {
$value = object_to_array($value);
if (!is_array($value) || empty($value) || empty($value['key'])) {
continue;
}
if (preg_match("/{$key}.*/", $value['key'])) {
if (empty($live_servers_id)) {
$_isLiveFromKey[$index] = true;
$_isLiveFromKey[$index] = $_isLiveFromKey[$index];
break 2;
} else {
if (intval(@$value['live_servers_id']) == $live_servers_id) {
$_isLiveFromKey[$index] = true;
$_isLiveFromKey[$index] = $_isLiveFromKey[$index];
break 2;
}
}
}
}
if (!empty($item->hidden_applications)) {
$applications = $item->hidden_applications;
foreach ($applications as $value) {
$value = object_to_array($value);
if (!is_array($value) || empty($value) || empty($value['key'])) {
continue;
}
if (preg_match("/{$key}.*/", $value['key'])) {
if (empty($live_servers_id)) {
$_isLiveFromKey[$index] = true;
$_isLiveFromKey[$index] = $_isLiveFromKey[$index];
break 2;
} else {
if (intval(@$value['live_servers_id']) == $live_servers_id) {
$_isLiveFromKey[$index] = true;
$_isLiveFromKey[$index] = $_isLiveFromKey[$index];
break 2;
}
}
}
}
}
}
}
if (empty($_isLiveFromKey[$index])) {
_error_log("Live::isLiveFromKey is NOT online [{$key}]");
} else {
_error_log("Live::isLiveFromKey is online [{$key}]");
}
return $_isLiveFromKey[$index];
}
static function isLiveAndIsReadyFromKey($key, $live_servers_id = 0, $live_index = '', $force_recreate = false) {
global $_isLiveAndIsReadyFromKey;
if (!isset($_isLiveAndIsReadyFromKey)) {
$_isLiveAndIsReadyFromKey = array();
}
$name = "getStats" . DIRECTORY_SEPARATOR . "isLiveAndIsReadyFromKey{$key}_{$live_servers_id}";
if (empty($force_recreate)) {
if (isset($_isLiveAndIsReadyFromKey[$name])) {
return $_isLiveAndIsReadyFromKey[$name];
}
$cache = ObjectYPT::getCache($name, 60, true);
}
if (!empty($cache)) {
$json = _json_decode($cache);
}
if (!empty($json) && is_object($json)) {
$_isLiveAndIsReadyFromKey[$name] = $json->result;
} else {
$json = new stdClass();
$key = self::getLiveKeyFromRequest($key, $live_index);
//_error_log('getStats execute isKeyLiveInStats: ' . __LINE__ . ' ' . __FILE__);
//_error_log("isLiveAndIsReadyFromKey::key: {$key}");
$isLiveFromKey = self::isKeyLiveInStats($key, $live_servers_id, $live_index, $force_recreate);
$_isLiveAndIsReadyFromKey[$name] = true;
if (empty($isLiveFromKey)) {
_error_log("isLiveAndIsReadyFromKey the key {$key} is not present on the stats");
$_isLiveAndIsReadyFromKey[$name] = false;
} else {
$ls = $_REQUEST['live_servers_id'];
$_REQUEST['live_servers_id'] = $live_servers_id;
$m3u8 = self::getM3U8File($key);
$_REQUEST['live_servers_id'] = $ls;
//_error_log('getStats execute isURL200: ' . __LINE__ . ' ' . __FILE__);
$is200 = isURL200($m3u8, $force_recreate);
if (empty($is200)) {
_error_log("isLiveAndIsReadyFromKey the m3u8 file is not present {$m3u8}");
$_isLiveAndIsReadyFromKey[$name] = false;
}
}
$json->result = $_isLiveAndIsReadyFromKey[$name];
ObjectYPT::setCache($name, json_encode($json));
}
return $_isLiveAndIsReadyFromKey[$name];
}
static function getOnlineLivesFromUser($users_id) {
$key = self::getLiveKey($users_id);
return self::getOnlineLivesFromKey($key);
}
static function getOnlineLivesFromKey($key) {
$json = getStatsNotifications();
$lives = array();
if (!empty($json) && is_object($json) && !empty($json->applications)) {
foreach ($json->applications as $value) {
if (preg_match("/{$key}.*/", $value['key'])) {
$lives[] = $value;
}
}
}
return $lives;
}
static function keyIsFromPlaylist($key) {
$parts = explode("_", $key);
if (empty($parts[1])) {
return false;
}
return array('key' => $parts[0], 'playlists_id' => $parts[1]);
}
static function getLiveKey($users_id) {
$lt = new LiveTransmition(0);
$lt->loadByUser($users_id);
return $lt->getKey();
}
static function getLiveKeyFromUser($users_id, $live_index = '', $playlists_id_live = '') {
$key = self::getLiveKey($users_id);
return self::getLiveKeyFromRequest($key, $live_index, $playlists_id_live);
}
static function getLiveKeyFromRequest($key, $live_index = '', $playlists_id_live = '') {
if (strpos($key, '-') === false) {
if (!empty($live_index)) {
$key .= '-' . preg_replace('/[^0-9a-z]/i', '', $live_index);
} else
if (!empty($_REQUEST['live_index'])) {
$key .= '-' . preg_replace('/[^0-9a-z]/i', '', $_REQUEST['live_index']);
}
}
if (strpos($key, '_') === false) {
if (!empty($playlists_id_live)) {
$key .= '_' . preg_replace('/[^0-9]/', '', $_REQUEST['playlists_id_live']);
} else if (!empty($_REQUEST['playlists_id_live'])) {
$key .= '_' . preg_replace('/[^0-9]/', '', $_REQUEST['playlists_id_live']);
}
}
return $key;
}
public function getImageGif($users_id, $live_servers_id = 0, $playlists_id_live = 0, $live_index = '') {
global $global;
if (empty($live_servers_id)) {
$live_servers_id = self::getCurrentLiveServersId();
}
if ($live_index === 'false') {
$live_index = '';
}
$u = new User($users_id);
$username = $u->getUser();
$file = "plugin/Live/getImage.php";
$url = $global['webSiteRootURL'] . $file;
$url = addQueryStringParameter($url, "live_servers_id", $live_servers_id);
$url = addQueryStringParameter($url, "playlists_id_live", $playlists_id_live);
$url = addQueryStringParameter($url, "live_index", $live_index);
$url = addQueryStringParameter($url, "u", $username);
$url = addQueryStringParameter($url, "format", 'gif');
return $url;
}
public static function getPosterImage($users_id, $live_servers_id) {
global $global;
$file = self::_getPosterImage($users_id, $live_servers_id);
if (!file_exists($global['systemRootPath'] . $file)) {
$file = self::getOnAirImage(false);
}
return $file;
}
public static function getPosterImageOrFalse($users_id, $live_servers_id) {
$poster = self::getPosterImage($users_id, $live_servers_id);
if (preg_match('/OnAir.jpg$/', $poster)) {
return false;
}
return $poster;
}
public function getLivePosterImage($users_id, $live_servers_id = 0, $playlists_id_live = 0, $live_index = '', $format = 'jpg') {
global $global;
return $global['webSiteRootURL'] . self::getLivePosterImageRelativePath($users_id, $live_servers_id, $playlists_id_live, $live_index, $format);
}
public function getLivePosterImageRelativePath($users_id, $live_servers_id = 0, $playlists_id_live = 0, $live_index = '', $format = 'jpg') {
global $global;
if (empty($live_servers_id)) {
$live_servers_id = self::getCurrentLiveServersId();
}
if (self::isLiveThumbsDisabled()) {
$file = self::_getPosterImage($users_id, $live_servers_id);
if (!file_exists($global['systemRootPath'] . $file)) {
$file = self::getOnAirImage(false);
}
} else {
$u = new User($users_id);
$username = $u->getUser();
$file = "plugin/Live/getImage.php?live_servers_id={$live_servers_id}&playlists_id_live={$playlists_id_live}&live_index={$live_index}&u={$username}&format={$format}";
}
return $file;
}
public static function isLiveThumbsDisabled() {
$obj = AVideoPlugin::getDataObject("Live");
if (!empty($obj->disableLiveThumbs)) {
return true;
}
return false;
}
public static function getPosterThumbsImage($users_id, $live_servers_id) {
global $global;
$file = self::_getPosterThumbsImage($users_id, $live_servers_id);
if (!file_exists($global['systemRootPath'] . $file)) {
$file = self::getOnAirImage(false);
}
return $file;
}
public static function getPoster($users_id, $live_servers_id, $key = '') {
_error_log("getPoster($users_id, $live_servers_id, $key)");
$lh = LiveTransmitionHistory::getActiveLiveFromUser($users_id, $live_servers_id, $key);
$live_index = self::getLiveIndexFromKey($lh['key']);
$poster = self::getPosterImageOrFalse($users_id, $live_servers_id, $live_index);
if (empty($poster)) {
$poster = self::getOfflineImage(false);
}
if (empty($lh)) {
_error_log("getPoster empty activity");
return $poster;
}
$parameters = self::getLiveParametersFromKey($lh['key']);
$live_index = $parameters['live_index'];
$playlists_id_live = $parameters['playlists_id_live'];
if (self::isLiveAndIsReadyFromKey($lh['key'], $lh['live_servers_id'])) {
return self::getLivePosterImageRelativePath($users_id, $live_servers_id, $playlists_id_live, $live_index);
_error_log('getImage: ' . ("[{$lh['key']}, {$lh['live_servers_id']}]") . ' is live and ready');
} else {
if (self::isKeyLiveInStats($lh['key'], $lh['live_servers_id'])) {
_error_log('getImage: ' . ("[{$lh['key']}, {$lh['live_servers_id']}]") . ' key is in the stats');
return self::getPosterImage($users_id, $live_servers_id, $live_index);
} else {
_error_log('getImage: ' . ("[{$lh['key']}, {$lh['live_servers_id']}]") . ' key is NOT in the stats');
return $poster;
}
}
}
public static function getPosterFromKey($key, $live_servers_id, $live_index = '') {
$key = self::getLatestKeyFromUser($users_id);
}
static function getOfflineImage($includeURL = true) {
global $global;
$img = "plugin/Live/view/Offline.jpg";
if ($includeURL) {
$img = "{$global['webSiteRootURL']}{$img}";
}
return $img;
}
static function getOnAirImage($includeURL = true) {
global $global;
$img = "plugin/Live/view/OnAir.jpg";
if ($includeURL) {
$img = "{$global['webSiteRootURL']}{$img}";
}
return $img;
}
public static function _getPosterImage($users_id, $live_servers_id) {
$file = "videos/userPhoto/Live/user_{$users_id}_bg_{$live_servers_id}.jpg";
return $file;
}
public static function _getPosterThumbsImage($users_id, $live_servers_id) {
$file = "videos/userPhoto/Live/user_{$users_id}_thumbs_{$live_servers_id}.jpg";
return $file;
}
public static function on_publish($liveTransmitionHistory_id) {
$obj = AVideoPlugin::getDataObject("Live");
if (empty($obj->disableRestream)) {
self::restream($liveTransmitionHistory_id);
}
$lt = new LiveTransmitionHistory($liveTransmitionHistory_id);
AVideoPlugin::onLiveStream($lt->getUsers_id(), $lt->getLive_servers_id());
}
public static function deleteStatsCache($clearFirstPage = false) {
global $getStatsLive, $_getStats, $getStatsObject, $_getStatsNotifications, $__getAVideoCache, $_isLiveFromKey, $_isLiveAndIsReadyFromKey;
_error_log_debug("Live::deleteStatsCache");
$tmpDir = ObjectYPT::getCacheDir();
$cacheDir = $tmpDir . "getstats" . DIRECTORY_SEPARATOR;
if (isset($live_servers_id)) {
$cacheDir .= "live_servers_id_{$live_servers_id}";
$pattern = "/.getStats.{$live_servers_id}.*/i";
ObjectYPT::deleteCachePattern($pattern);
}
//_error_log("Live::deleteStatsCache [{$cacheDir}]");
rrmdir($cacheDir);
exec('rm -R ' . $cacheDir);
if (is_dir($cacheDir)) {
//_error_log("Live::deleteStatsCache [{$cacheDir}] looks like the cache was not deleted", AVideoLog::$ERROR);
exec('rm -R ' . $cacheDir);
} else {
//_error_log("Live::deleteStatsCache [{$cacheDir}] Success");
}
if ($clearFirstPage) {
clearCache(true);
}
isURL200Clear();
unset($__getAVideoCache);
unset($getStatsLive);
unset($getStatsObject);
unset($_getStats);
unset($_getStatsNotifications);
unset($_isLiveFromKey);
unset($_isLiveAndIsReadyFromKey);
}
public static function getReverseRestreamObject($m3u8, $users_id, $live_servers_id = -1) {
if (!isValidURL($m3u8)) {
return false;
}
$obj = new stdClass();
$obj->m3u8 = $m3u8;
$obj->restreamerURL = self::getRestreamer($live_servers_id);
$obj->restreamsDestinations = array(Live::getRTMPLink($users_id));
$obj->token = getToken(60);
$obj->users_id = $users_id;
return $obj;
}
public static function getRestreamObject($liveTransmitionHistory_id) {
if (empty($liveTransmitionHistory_id)) {
return false;
}
$lth = new LiveTransmitionHistory($liveTransmitionHistory_id);
if (empty($lth->getKey())) {
return false;
}
$_REQUEST['live_servers_id'] = $lth->getLive_servers_id();
$obj = new stdClass();
$obj->m3u8 = self::getM3U8File($lth->getKey(), true);
$obj->restreamerURL = self::getRestreamer($lth->getLive_servers_id());
$obj->restreamsDestinations = array();
$obj->token = getToken(60);
$obj->users_id = $lth->getUsers_id();
$rows = Live_restreams::getAllFromUser($lth->getUsers_id());
foreach ($rows as $value) {
$value['stream_url'] = rtrim($value['stream_url'], "/") . '/';
$obj->restreamsDestinations[] = "{$value['stream_url']}{$value['stream_key']}";
}
return $obj;
}
public static function reverseRestream($m3u8, $users_id, $live_servers_id = -1) {
_error_log("Live:reverseRestream start");
$obj = self::getReverseRestreamObject($m3u8, $users_id, $live_servers_id);
_error_log("Live:reverseRestream obj " . _json_encode($obj));
return self::sendRestream($obj);
}
public static function restream($liveTransmitionHistory_id) {
outputAndContinueInBackground();
$obj = self::getRestreamObject($liveTransmitionHistory_id);
return self::sendRestream($obj);
}
private static function sendRestream($obj) {
_error_log("Live:sendRestream start");
try {
if (empty($obj)) {
_error_log("Live:sendRestream object is empty");
return false;
}
$data_string = json_encode($obj);
_error_log("Live:sendRestream ({$obj->restreamerURL}) {$data_string}");
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $obj->restreamerURL);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_POSTREDIR, 3);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$output = curl_exec($ch);
curl_close($ch);
if (empty($output)) {
_error_log('Live:sendRestream ERROR ' . curl_error($ch));
return false;
}
$json = _json_decode($output);
if (empty($output)) {
_error_log('Live:sendRestream JSON ERROR ' . $output);
return false;
}
_error_log('Live:sendRestream complete ' . $output);
return $json;
} catch (Exception $exc) {
_error_log("Live:sendRestream " . $exc->getTraceAsString());
return false;
}
return false;
}
public static function canStreamWithMeet() {
if (!User::canStream()) {
return false;
}
if (!User::canCreateMeet()) {
return false;
}
$mobj = AVideoPlugin::getObjectDataIfEnabled("Meet");
if (empty($mobj)) {
return false;
}
$obj = AVideoPlugin::getObjectDataIfEnabled("Live");
if (!empty($obj->disableMeetCamera)) {
return false;
}
return true;
}
public function getUploadMenuButton() {
global $global;
$obj = $this->getDataObject();
if (!empty(!User::canStream())) {
return '';
}
$buttonTitle = $this->getButtonTitle();
include $global['systemRootPath'] . 'plugin/Live/getUploadMenuButton.php';
}
public static function getAllVideos($status = "", $showOnlyLoggedUserVideos = false, $activeUsersOnly = true) {
global $global, $config, $advancedCustom;
if (AVideoPlugin::isEnabledByName("VideoTags")) {
if (!empty($_GET['tags_id']) && empty($videosArrayId)) {
TimeLogStart("video::getAllVideos::getAllVideosIdFromTagsId({$_GET['tags_id']})");
$videosArrayId = VideoTags::getAllVideosIdFromTagsId($_GET['tags_id']);
TimeLogEnd("video::getAllVideos::getAllVideosIdFromTagsId({$_GET['tags_id']})", __LINE__);
}
}
$status = str_replace("'", "", $status);
$sql = "SELECT u.*, v.*, c.iconClass, c.name as category, c.clean_name as clean_category,c.description as category_description, v.created as videoCreation, v.modified as videoModified "
. " FROM live_transmitions as v "
. " LEFT JOIN categories c ON categories_id = c.id "
. " LEFT JOIN users u ON v.users_id = u.id "
. " WHERE 1=1 ";
if ($showOnlyLoggedUserVideos === true && !Permissions::canModerateVideos()) {
$uid = intval(User::getId());
$sql .= " AND v.users_id = '{$uid}'";
} elseif (!empty($showOnlyLoggedUserVideos)) {
$uid = intval($showOnlyLoggedUserVideos);
$sql .= " AND v.users_id = '{$uid}'";
} elseif (!empty($_GET['channelName'])) {
$user = User::getChannelOwner($_GET['channelName']);
$uid = intval($user['id']);
$sql .= " AND v.users_id = '{$uid}' ";
}
if ($activeUsersOnly) {
$sql .= " AND u.status = 'a' ";
}
if ($status == "publicOnly") {
$sql .= " AND v.public = 1 ";
} elseif (!empty($status)) {
$sql .= " AND v.`public` = '{$status}'";
}
if (!empty($_GET['catName'])) {
$catName = $global['mysqli']->real_escape_string($_GET['catName']);
$sql .= " AND (c.clean_name = '{$catName}' OR c.parentId IN (SELECT cs.id from categories cs where cs.clean_name = '{$catName}' ))";
}
if (!empty($_GET['modified'])) {
$_GET['modified'] = str_replace("'", "", $_GET['modified']);
$sql .= " AND v.modified >= '{$_GET['modified']}'";
}
$sql .= AVideoPlugin::getVideoWhereClause();
if (strpos(strtolower($sql), 'limit') === false) {
if (!empty($_GET['limitOnceToOne'])) {
$sql .= " LIMIT 1";
unset($_GET['limitOnceToOne']);
} else {
$_REQUEST['rowCount'] = getRowCount();
if (!empty($_REQUEST['rowCount'])) {
$sql .= " LIMIT {$_REQUEST['rowCount']}";
} else {
_error_log("getAllVideos without limit " . json_encode(debug_backtrace()));
if (empty($global['limitForUnlimitedVideos'])) {
$global['limitForUnlimitedVideos'] = 100;
}
if ($global['limitForUnlimitedVideos'] > 0) {
$sql .= " LIMIT {$global['limitForUnlimitedVideos']}";
}
}
}
}
//echo $sql;exit;
//_error_log("getAllVideos($status, $showOnlyLoggedUserVideos , $ignoreGroup , ". json_encode($videosArrayId).")" . $sql);
$res = sqlDAL::readSql($sql);
$fullData = sqlDAL::fetchAllAssoc($res);
sqlDAL::close($res);
$videos = array();
if ($res != false) {
foreach ($fullData as $row) {
$row = cleanUpRowFromDatabase($row);
$row['live_servers_id'] = self::getLastServersIdFromUser($row['users_id']);
if (empty($otherInfo)) {
$otherInfo = array();
$otherInfo['category'] = xss_esc_back($row['category']);
$otherInfo['groups'] = UserGroups::getVideoGroups($row['id']);
//$otherInfo['title'] = UTF8encode($row['title']);
$otherInfo['description'] = UTF8encode($row['description']);
$otherInfo['descriptionHTML'] = Video::htmlDescription($otherInfo['description']);
$otherInfo['filesize'] = 0;
}
foreach ($otherInfo as $key => $value) {
$row[$key] = $value;
}
$row['rotation'] = 0;
$row['filename'] = '';
$row['type'] = 'live';
$row['duration'] = '';
$row['isWatchLater'] = 0;
$row['isFavorite'] = 0;
$row['views_count'] = 0;
$videos[] = $row;
}
//$videos = $res->fetch_all(MYSQLI_ASSOC);
} else {
$videos = false;
die($sql . '\nError : (' . $global['mysqli']->errno . ') ' . $global['mysqli']->error);
}
return $videos;
}
static function finishLive($key) {
$lh = LiveTransmitionHistory::finish($key);
}
static function updateVideosUserGroup($videos_id, $key) {
$lt = LiveTransmition::keyExists($key);
if (!empty($lt)) {
$lt = new LiveTransmition($lt['id']);
$groups = $lt->getGroups();
if (!empty($groups)) {
UserGroups::updateVideoGroups($videos_id, $groups);
}
}
}
static function notifySocketStats($callBack = 'socketLiveONCallback', $array = array()) {
if (empty($array['stats'])) {
$array['stats'] = getStatsNotifications();
}
_error_log("NGINX Live::on_publish_socket_notification sendSocketMessageToAll Start");
$socketObj = sendSocketMessageToAll($array, $callBack);
_error_log("NGINX Live::on_publish_socket_notification SocketMessageToAll END");
return $socketObj;
}
static public function getImageType($content) {
global $global;
if (empty($content)) {
return LiveImageType::$UNKNOWN;
}
$contentLen = strlen($content);
if ($contentLen < 255) {
// check if it is a file
if (file_exists($content)) {
$contentLen = strlen(file_get_contents($content));
}
}
if ($contentLen >= 2095335 && $contentLen <= 2095350) {
return LiveImageType::$DEFAULTGIF;
}
if ($contentLen >= 70805 && $contentLen <= 70810) {
return LiveImageType::$ONAIRENCODER;
}
$filesize = file_get_contents($global['systemRootPath'] . self::getOnAirImage(false));
if ($contentLen === $filesize) {
return LiveImageType::$ONAIR;
}
$filesize = file_get_contents($global['systemRootPath'] . self::getOfflineImage(false));
if ($contentLen === $filesize) {
return LiveImageType::$OFFLINE;
}
//_error_log('getImageType: is not defined yet ('.$contentLen.')');
return LiveImageType::$LIVE;
}
static function isLiveImage($content) {
return self::getImageType($content) === LiveImageType::$LIVE;
}
static function isDefaultImage($content) {
$type = self::getImageType($content);
return $type === LiveImageType::$ONAIRENCODER || $type === LiveImageType::$ONAIR || $type === LiveImageType::$OFFLINE || $type === LiveImageType::$DEFAULTGIF;
}
static function iskeyOnline($key) {
$stats = getStatsNotifications();
foreach ($stats["applications"] as $value) {
if (empty($value['key'])) {
continue;
}
if (preg_match('/' . $key . '/', $value['key'])) {
return true;
}
}
return false;
}
static function getValidNotOnlineLiveIndex($key, $live_index) {
if (empty($live_index)) {
return 1;
}
if (!Live::iskeyOnline("{$key}-{$live_index}")) {
return $live_index;
} else {
if (is_numeric($live_index)) {
return self::getValidNotOnlineLiveIndex($key, ++$live_index);
} else {
return self::getValidNotOnlineLiveIndex($key, $live_index . 'New');
}
}
}
static function getLatestValidNotOnlineLiveIndex($key) {
$live_index = LiveTransmitionHistory::getLatestIndexFromKey($key);
$live_index = self::getValidNotOnlineLiveIndex($key, $live_index);
return $live_index;
}
static function getLivesOnlineFromKey($key) {
global $_getLivesOnlineFromKey;
if (!isset($_getLivesOnlineFromKey)) {
$_getLivesOnlineFromKey = array();
}
if (!isset($_getLivesOnlineFromKey[$key])) {
$stats = getStatsNotifications();
$_getLivesOnlineFromKey[$key] = array();
foreach ($stats["applications"] as $value) {
if (empty($value['key'])) {
continue;
}
if (preg_match('/' . $key . '/', $value['key'])) {
$_getLivesOnlineFromKey[$key][] = $value;
}
}
}
return $_getLivesOnlineFromKey[$key];
}
static function getFirstLiveOnlineFromKey($key) {
$onliveApplications = self::getLivesOnlineFromKey($key);
if (!empty($onliveApplications[0])) {
return $onliveApplications[0];
}
return false;
}
static function getUserHash($users_id) {
return encryptString(_json_encode(array('users_id' => $users_id, 'time' => time())));
}
static function decryptHash($hash) {
$string = decryptString($hash);
$json = _json_decode($string);
return object_to_array($json);
}
}
class LiveImageType {
static $UNKNOWN = 'unknown';
static $OFFLINE = 'offline';
static $ONAIR = 'onair';
static $ONAIRENCODER = 'onair_encoder';
static $DEFAULTGIF = 'defaultgif';
static $LIVE = 'live';
}
class LiveStreamObject {
private $key, $live_servers_id, $live_index, $playlists_id_live;
function __construct($key, $live_servers_id = 0, $live_index = 0, $playlists_id_live = 0) {
$this->key = $key;
$this->live_servers_id = intval($live_servers_id);
$this->live_index = $live_index;
$this->playlists_id_live = intval($playlists_id_live);
$parts = Live::getLiveParametersFromKey($this->key);
$objLive = AVideoPlugin::getDataObject("Live");
if (empty($live_servers_id) && !empty($objLive->useLiveServers)) {
$live_servers_id = Live::getLiveServersIdRequest();
}
if (empty($this->live_index)) {
// check if the index is on the key already
if (!empty($parts['live_index'])) {
$this->live_index = $parts['live_index'];
} else if (!empty($_REQUEST['live_index'])) {
$this->live_index = $_REQUEST['live_index'];
}
}
$this->key = $parts['cleanKey'];
$this->live_index = preg_replace('/[^0-9a-z]/i', '', $this->live_index);
}
function getKey() {
return $this->key;
}
function getKeyWithIndex($forceIndexIfEnabled = false, $allowOnlineIndex = false) {
if ($forceIndexIfEnabled) {
$objLive = AVideoPlugin::getDataObject("Live");
if (!empty($objLive->allowMultipleLivesPerUser)) {
if (empty($allowOnlineIndex)) {
$this->live_index = Live::getLatestValidNotOnlineLiveIndex($this->key);
} else {
$this->live_index = LiveTransmitionHistory::getLatestIndexFromKey($this->key);
}
}
}
return Live::getLiveKeyFromRequest($this->key, $this->live_index, $this->playlists_id_live);
}
function getLive_servers_id() {
return $this->live_servers_id;
}
function getLive_index() {
return $this->live_index;
}
function getPlaylists_id_live() {
return $this->playlists_id_live;
}
function getURL() {
global $global;
$lt = LiveTransmition::getFromKey($this->key);
if (empty($lt)) {
return false;
}
$user = new User($lt['users_id']);
$channelName = $user->getChannelName();
if (empty($channelName)) {
return false;
}
$url = "{$global['webSiteRootURL']}live/{$this->live_servers_id}/" . urlencode($channelName);
if (!empty($this->live_index)) {
$url .= '/' . urlencode($this->live_index);
}
if (!empty($this->playlists_id_live)) {
$url = addQueryStringParameter($url, 'playlists_id_live', $this->playlists_id_live);
}
return $url;
}
function getURLEmbed() {
$url = $this->getURL();
return addQueryStringParameter($url, 'embed', 1);
}
function getM3U8($doNotProtect = false, $allowOnlineIndex = false) {
global $global;
$o = AVideoPlugin::getObjectData("Live");
$playerServer = Live::getPlayerServer();
if (!empty($this->live_servers_id)) {
$liveServer = new Live_servers($this->live_servers_id);
if ($liveServer->getStats_url()) {
$o->protectLive = $liveServer->getProtectLive();
$o->useAadaptiveMode = $liveServer->getUseAadaptiveMode();
}
}
$uuid = $this->getKeyWithIndex(false, $allowOnlineIndex);
$playerServer = addLastSlash($playerServer);
if ($o->protectLive && empty($doNotProtect)) {
return "{$global['webSiteRootURL']}plugin/Live/m3u8.php?live_servers_id={$this->live_servers_id}&uuid=" . encryptString($uuid);
} else if ($o->useAadaptiveMode) {
return $playerServer . "{$uuid}.m3u8";
} else {
return $playerServer . "{$uuid}/index.m3u8";
}
}
function getOnlineM3U8($users_id, $doNotProtect = false) {
$li = $this->live_index;
if (empty($this->live_index)) {
$online = Live::getFirstLiveOnlineFromKey($this->key);
if (!empty($online)) {
$parameters = Live::getLiveParametersFromKey($online['key']);
//var_dump($parameters, $this->live_index, $li, $online);exit;
} else {
$key = Live::getLatestKeyFromUser($users_id);
$parameters = Live::getLiveParametersFromKey($key);
}
$this->live_index = $parameters['live_index'];
}
$m3u8 = $this->getM3U8($doNotProtect, true);
$this->live_index = $li;
return $m3u8;
}
function getRTMPLink() {
return $this->getRTMPLinkWithOutKey() . $this->getKeyWithIndex(true);
}
function getRTMPLinkWithOutKey() {
$lt = LiveTransmition::getFromKey($this->key);
$user = new User($lt['users_id']);
$obj = new stdClass();
$obj->users_id = $lt['users_id'];
$obj->key = $this->key;
$encrypt = encryptString($obj);
return Live::getServer() . "?e={$encrypt}/";
}
}