| Current Path : /var/www/administrator/components/com_yendifvideoshare/libraries/ |
| Current File : /var/www/administrator/components/com_yendifvideoshare/libraries/utils.php |
<?php
/**
* @version 1.2.9
* @package Com_YendifVideoShare
* @author PluginsWare Interactive Pvt. Ltd <admin@yendifplayer.com>
* @copyright Copyright (c) 2012 - 2021 PluginsWare Interactive Pvt. Ltd. All Rights Reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// Prevent direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class YendifVideoShareUtils {
public static function hasPermission( $access = 1, $viewLevels = '' ) {
if ( $access == '' ) {
return true;
}
if ( $viewLevels == '' ) {
$user = JFactory::getUser();
$viewLevels = $user->getAuthorisedViewLevels();
}
return in_array( $access, $viewLevels ) ? true : false;
}
public static function addSubMenu( $view ) {
$app = JFactory::getApplication();
$views = array(
'dashboard' => false,
'categories' => false,
'videos' => false,
'approval' => false,
'adverts' => false,
'ratings' => false,
'import' => false,
'config' => false
);
$view = $app->input->get( 'view', 'dashboard' );
$views[ $view ] = true;
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_CONTROL_PANEL' ), 'index.php?option=com_yendifvideoshare', $views['dashboard'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_CATEGORIES' ), 'index.php?option=com_yendifvideoshare&view=categories', $views['categories'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_VIDEOS' ), 'index.php?option=com_yendifvideoshare&view=videos', $views['videos'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_APPROVAL_QUEUE' ), 'index.php?option=com_yendifvideoshare&view=approval', $views['approval'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_ADVERTISEMENTS' ), 'index.php?option=com_yendifvideoshare&view=adverts', $views['adverts'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_RATINGS' ), 'index.php?option=com_yendifvideoshare&view=ratings', $views['ratings'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_QUICK_IMPORT_YOUTUBE_VIDEOS' ), 'index.php?option=com_yendifvideoshare&view=import', $views['import'] );
JSubMenuHelper::addEntry( JText::_( 'YENDIF_VIDEO_SHARE_GLOBAL_CONFIGURATION' ), 'index.php?option=com_yendifvideoshare&view=config', $views['config'] );
}
public static function getToken() {
return JSession::getFormToken();
}
public static function checkToken() {
if ( JSession::checkToken( 'get' ) ) {
JSession::checkToken( 'get' ) or die( 'Invalid Token' );
} else {
JSession::checkToken() or die( 'Invalid Token' );
}
}
public static function safeString( $value = '' ) {
return htmlspecialchars( trim( $value ) );
}
public static function stringURLSafe( $string ) {
jimport( 'joomla.filter.output' );
if ( JFactory::getConfig()->get( 'unicodeslugs' ) == 1 ) {
$output = JFilterOutput::stringURLUnicodeSlug( $string );
} else {
$output = JFilterOutput::stringURLSafe( $string );
}
return $output;
}
public static function Truncate( $text, $length = 150 ) {
$text = strip_tags( $text );
if ( $length > 0 && JString::strlen( $text ) > $length ) {
$tmp = JString::substr( $text, 0, $length );
$tmp = JString::substr( $tmp, 0, JString::strrpos( $tmp, ' ' ) );
if( JString::strlen( $tmp ) >= $length - 3 ) {
$tmp = JString::substr( $tmp, 0, JString::strrpos( $tmp, ' ' ) );
}
$text = $tmp . '...';
}
return $text;
}
public static function getUsers() {
$db = JFactory::getDbo();
$query = 'SELECT id,username FROM #__users';
$db->setQuery( $query );
$items = $db->loadObjectList();
return $items;
}
public static function getColumn( $table, $column, $id ) {
$db = JFactory::getDbo();
$query = "SELECT {$column} FROM #__yendifvideoshare_{$table} WHERE id={$id}";
$db->setQuery($query);
$item = $db->loadResult();
return $item;
}
public static function getConfig( $column = '*' ) {
$db = JFactory::getDbo();
$query = 'SELECT * FROM #__yendifvideoshare_options';
$db->setQuery( $query );
$options = $db->loadObjectList();
$config = new JObject();
foreach ( $options as $option ) {
$config->{$option->name} = $option->value;
}
if ( ! isset( $config->access ) ) {
$config->share = 0;
$config->embed = 0;
$config->download = 0;
$config->show_rating = 0;
$config->allow_guest_rating = 0;
$config->show_likes = 0;
$config->allow_guest_like = 0;
}
if ( $column === '*' ) {
return $config;
} else {
return $config->{$column};
}
}
public static function getVideoInsertId() {
$db = JFactory::getDbo();
$user_id = JFactory::getUser()->get( 'id' );
$insert_id = 0;
$query = 'SELECT id FROM #__yendifvideoshare_videos WHERE title=' . $db->quote( '' ) . ' AND userid=' . (int) $user_id . ' ORDER BY id DESC';
$db->setQuery($query);
$items = $db->loadObjectList();
foreach ( $items as $key => $item ) {
if ( 0 == $key && $item->id > 0 ) {
$insert_id = $item->id;
} else {
$query = 'DELETE FROM #__yendifvideoshare_videos WHERE id=' . $item->id;
$db->setQuery( $query );
$db->query();
}
jimport( 'joomla.filesystem.folder' );
$folder = YENDIF_VIDEO_SHARE_UPLOAD_BASE . 'videos' . DS . $item->id . DS;
if ( JFolder::exists( $folder ) ) {
JFolder::delete( $folder );
}
}
if ( 0 == $insert_id ) {
$row = new JObject();
$row->id = NULL;
$row->userid = $user_id;
$row->title = '';
$db->insertObject( '#__yendifvideoshare_videos', $row );
$insert_id = $db->insertid();
}
return $insert_id;
}
public static function getAdvertInsertId() {
$db = JFactory::getDBO();
$insert_id = 0;
$query = 'SELECT id FROM #__yendifvideoshare_adverts WHERE title=' . $db->quote( '' ) . ' ORDER BY id DESC';
$db->setQuery( $query );
$items = $db->loadObjectList();
foreach ( $items as $key => $item ) {
if ( 0 == $key && $item->id > 0 ) {
$insert_id = $item->id;
} else {
$query = 'DELETE FROM #__yendifvideoshare_adverts WHERE id=' . $item->id;
$db->setQuery( $query );
$db->query();
}
jimport( 'joomla.filesystem.folder' );
$folder = YENDIF_VIDEO_SHARE_UPLOAD_BASE . 'videos' . DS . $item->id . DS;
if ( JFolder::exists( $folder ) ) {
JFolder::delete( $folder );
}
}
if ( 0 == $insert_id ) {
$row = new JObject();
$row->id = NULL;
$row->title = '';
$db->insertObject( '#__yendifvideoshare_adverts', $row );
$insert_id = $db->insertid();
}
return $insert_id;
}
public static function getCategoryTree( $ids ) {
$db = JFactory::getDbo();
$ids = (array) $ids;
JArrayHelper::toInteger( $ids );
$catid = array_unique( $ids );
sort( $ids );
$array = $ids;
while ( count( $array ) ) {
$query = "SELECT id FROM #__yendifvideoshare_categories WHERE parent IN (" . implode( ',', $array ) . ") AND id NOT IN (" . implode( ',', $array ) . ")";
$db->setQuery( $query );
$array = $db->loadColumn();
$ids = array_merge( $ids, $array );
}
JArrayHelper::toInteger( $ids );
$ids = array_unique( $ids );
return $ids;
}
public static function getMediaCount( $id ) {
$db = JFactory::getDbo();
$config = self::getConfig();
$date = JFactory::getDate();
$nowDate = $db->quote( $date->toSql() );
$nullDate = $db->quote( $db->getNullDate() );
$query = 'SELECT COUNT(id) FROM #__yendifvideoshare_videos WHERE published=1';
if ( $config->schedule_video_publishing ) {
$query .= " AND ( published_up = " . $nullDate . " OR published_up <= " . $nowDate . ' )';
$query .= " AND ( published_down = " . $nullDate . " OR published_down >= " . $nowDate . ' )';
}
$query .= " AND catid IN(" . implode( ',', self::getCategoryTree( $id ) ) . ")";
$db->setQuery( $query );
$count = $db->loadResult();
return $count;
}
public static function getSubCategoryMediaCount( $id, $check_publishing_options ) {
$db = JFactory::getDbo();
$query = 'SELECT COUNT(id) FROM #__yendifvideoshare_videos WHERE published=1';
if ( $check_publishing_options ) {
$date = JFactory::getDate();
$nowDate = $db->quote( $date->toSql() );
$nullDate = $db->quote( $db->getNullDate() );
$query .= " AND ( published_up = " . $nullDate . " OR published_up <= " . $nowDate . ' )';
$query .= " AND ( published_down = " . $nullDate . " OR published_down >= " . $nowDate . ' )';
}
$query .= " AND catid IN(" . $id . ")";
$db->setQuery( $query );
$count = $db->loadResult();
return $count;
}
public static function getImage( $file, $format, $default ) {
if ( empty( $file ) ) {
if ( empty ( $default ) ) {
$default = JURI::root( true ) . '/media/yendifvideoshare/assets/site/images/placeholder.jpg';
}
return $default;
}
// If an uploaded image file
$isUploaded = strpos( $file, 'media/yendifvideoshare/' );
if ( $isUploaded !== false ) {
// Remove Protocols
$file = explode( 'media', $file );
$file = '/media' . $file[1];
// Check if Image available for the given "format"
$ext = substr( $file, strrpos( $file, "." ) + 1 );
$file2 = str_replace( $ext, $format . '.' . $ext, $file );
if ( file_exists( JPATH_ROOT . str_replace( '/', DS, $file2 ) ) ) {
$file = $file2;
}
// Add the Joomla! folder
$file = JURI::root( true ) . $file;
}
// If YouTube
$isYouTube = strpos( $file, 'youtube.com' );
if ( $isYouTube !== false ) {
$file = str_replace( 'http:', 'https:', $file );
}
// Remove spaces
$file = str_replace( ' ', '%20', $file );
// Return
return $file;
}
public static function RatingWidget( $rating = 0, $id, $total_users ) {
$html = '<div class="yendif-ratings">';
$html .= '<span class="yendif-ratings-stars">';
$html .= '<span class="yendif-current-ratings" style="width:' . $rating . '%;"></span>';
$j = 0.5;
for ( $i = 0; $i < 10; $i++ ) {
$j += 0.5;
$html .= '<span class="yendif-ratings-star">';
$html .= '<a class="yendif-rating-trigger yendif-star-' . ( $j * 10 ) . '" title="' . JText::sprintf( 'YENDIF_VIDEO_SHARE_RATING_TITLE', $j, 5 ) . '" data-id="' . $id . '" data-value="' . $j . '">1</a>';
$html .= '</span>';
}
$html .= '</span>';
$html .= '<span class="yendif-ratings-info">' . JText::sprintf( 'YENDIF_VIDEO_SHARE_RATING_INFO', ( $rating * 5 ) / 100, $total_users ) . '</span>';
$html .= '</div>';
return $html;
}
public static function showRating( $rating = 0 ) {
$html = '<div class="yendif-ratings-small">';
$html .= '<span class="yendif-ratings-stars"><span class="yendif-current-ratings" style="width:' . $rating . '%;"></span></span>';
$html .= '</div>';
return $html;
}
public static function VotingWidget( $id, $allow_guest_like ) {
$status = self::getLikeDislikeStatus( $id, $allow_guest_like );
$like_class_suffix = '';
$dislike_class_suffix = '';
if ( ! empty( $status ) ) {
if ( $status->likes > 0 ) {
$like_class_suffix = ' active';
}
if ( $status->dislikes > 0 ) {
$dislike_class_suffix = ' active';
}
}
$html = '<div class="yendif-likes-dislikes">';
$html .= '<span class="yendif-like-btn" data-id="' . $id . '" data-like="1" data-dislike="0">';
$html .= '<span class="yendif-like-icon' . $like_class_suffix . '"></span>';
$html .= '<span class="yendif-like-dislike-divider"></span>';
$html .= '<span class="yendif-like-count">' . self::getLikes( $id ) . '</span>';
$html .= '</span>';
$html .= '<span class="yendif-dislike-btn" data-id="' . $id . '" data-like="0" data-dislike="1">';
$html .= '<span class="yendif-dislike-icon' . $dislike_class_suffix . '"></span>';
$html .= '<span class="yendif-like-dislike-divider"></span>';
$html .= '<span class="yendif-dislike-count">' . self::getDislikes( $id ) . '</span>';
$html .= '</span>';
$html .= '</div>';
return $html;
}
public static function getLikes( $id ) {
$db = JFactory::getDbo();
$query = 'SELECT COUNT(id) FROM #__yendifvideoshare_likes_dislikes WHERE videoid=' . (int) $id . ' AND likes=1';
$db->setQuery( $query );
return $db->loadResult();
}
public static function getDislikes( $id ) {
$db = JFactory::getDbo();
$query = 'SELECT COUNT(id) FROM #__yendifvideoshare_likes_dislikes WHERE videoid=' . (int) $id . ' AND dislikes=1';
$db->setQuery( $query );
return $db->loadResult();
}
public static function getLikeDislikeStatus( $id, $allow_guest_like ) {
$db = JFactory::getDbo();
$session = JFactory::getSession();
$userid = JFactory::getUser()->get( 'id' );
$sessionid = $session->getId();
$query = 'SELECT likes, dislikes FROM #__yendifvideoshare_likes_dislikes WHERE videoid=' . (int) $id;
$query .= $allow_guest_like == 1 ? " AND sessionid=" . $db->quote( $sessionid ) : " AND userid=" . $userid;
$db->setQuery( $query );
return $db->loadObject();
}
public static function buildRoute( $item = '', $view = 'video', $itemId = 0 ) {
$id = 0;
$alias = '';
if ( empty( $item ) ) {
if ( 'category' != $view && 'video' != $view ) {
return '';
}
} else {
$id = $item->id;
$alias = $item->alias;
}
$needles = array();
$needles[] = 'index.php?option=com_yendifvideoshare';
$needles[] = 'view=' . $view;
$needles[] = 'id=' . $id . ':' . $alias;
if ( ! $itemId ) {
$app = JFactory::getApplication();
$itemId = $app->input->getInt('Itemid');
$_item = self::_findItem( array( 'Itemid' => $itemId, 'option' => 'com_yendifvideoshare', 'view' => $view, 'layout' => 'default', 'id' => $id ) );
$itemId = $_item['itemId'];
if ( $_item['is_exact_match'] ) {
$needles = array();
}
}
if ( $itemId ) {
$needles[] = 'Itemid=' . $itemId;
}
// Build Route
if ( 1 == count( $needles ) ) {
$url = 'index.php?' . $needles[0];
} else {
$url = implode( '&', $needles );
}
return JRoute::_( $url );
}
protected static function _findItem( $query = null ) {
$app = JFactory::getApplication();
// Define empty array to avoid having unset properties in my query
$base_array = array( 'Itemid' => '', 'option' => '', 'view' => '', 'layout' => '', 'id' => '' );
$menu = $app->getMenu();
$active = $menu->getActive();
// Start with no match found
$match = false;
$match_level = 0;
$item_match = 0;
$is_exact_match = 0;
// Load all menu items
$items = $menu->getMenu();
// Use the current item over others if it ties for the best match
if ( ! empty( $active->query ) ) {
$active->query += $base_array;
if ( $active->query['option'] == $query['option'] ) {
$match_level = 1;
$match = $active;
if ( $active->query['view'] == $query['view'] ) {
$match_level = 2;
if ( $active->query['layout'] == $query['layout'] || ( $query['layout'] == 'default' && ! $active->query['layout'] ) ) {
$match_level = 3;
if ( $active->query['id'] == $query['id'] ) {
$match_level = 4;
$is_exact_match = 1;
}
}
}
}
}
// Loop through each menu item in order
foreach ( $items as $item ) {
$item->query += $base_array;
// base check is that it is for this component
// then cycle through each possibility finding it's match level
if ( $item->query['option'] == $query['option'] ) {
$item_match = 1;
if ( $item->query['view'] == $query['view'] ) {
$item_match = 2;
if ( $item->query['layout'] == $query['layout'] || ( $query['layout']=='default' && ! $item->query['layout'] ) ) {
$item_match = 3;
if ( $item->query['id'] == $query['id'] ) {
$item_match = 4;
$is_exact_match = 1;
}
}
}
}
// If this item is a better match than our current match, set it as the best match
if ( $item_match > $match_level ) {
$match_level = $item_match;
$match = $item;
}
}
// If there is a match update Itemid to match that menu item
if ( $match ) {
$itemId = $match->id;
}
return array( 'itemId' => $itemId, 'is_exact_match' => $is_exact_match );
}
public static function prepareURL( $url, $pathonly = true ) {
return JURI::root( $pathonly ) . '/' . $url . '?v=1.2.9';
}
public static function getVideoImageFromEmbedCode( $embedcode ) {
$image = '';
$document = new DOMDocument();
@$document->loadHTML( $embedcode );
$iframes = $document->getElementsByTagName( 'iframe' );
if ( $iframes->length > 0 ) {
if ( $iframes->item(0)->hasAttribute( 'src' ) ) {
$src = $iframes->item(0)->getAttribute( 'src' );
// YouTube
if ( false !== strpos( $src, 'youtube.com' ) || false !== strpos( $src, 'youtu.be' ) ) {
$image = self::getYouTubeVideoImage( $src );
}
// Vimeo
elseif ( false !== strpos( $src, 'vimeo.com' ) ) {
$image = self::getVimeoVideoImage( $src );
}
}
}
return $image;
}
public static function getYouTubeVideoImage( $url ) {
if ( empty( $url ) ) {
return '';
}
$image = '';
$videoId = self::getYouTubeVideoId( $url );
if ( ! empty( $videoId ) ) {
$image = 'https://img.youtube.com/vi/' . $videoId . '/0.jpg';
}
return $image;
}
public static function getYouTubeVideoId( $url ) {
if ( empty( $url ) ) {
return '';
}
$videoId = false;
$url = parse_url( $url );
if ( strcasecmp( $url['host'], 'youtu.be' ) === 0 ) {
$videoId = substr( $url['path'], 1 );
} elseif ( strcasecmp( $url['host'], 'www.youtube.com' ) === 0 ) {
if ( isset( $url['query'] ) ) {
parse_str( $url['query'], $url['query'] );
if ( isset( $url['query']['v'] ) ) {
$videoId = $url['query']['v'];
}
}
if ( $videoId == false ) {
$url['path'] = explode( '/', substr( $url['path'], 1 ) );
if ( in_array( $url['path'][0], array( 'e', 'embed', 'v' ) ) ) {
$videoId = $url['path'][1];
}
}
}
return $videoId;
}
public static function getVimeoVideoImage( $url ) {
if ( empty( $url ) ) {
return '';
}
$image = '';
$videoId = '';
// Get image using the standard OEmbed API
if ( function_exists( 'file_get_contents' ) ) {
$oembed = json_decode( file_get_contents( "https://vimeo.com/api/oembed.json?url={$url}" ) );
if ( $oembed ) {
if ( isset( $oembed->video_id ) ) {
$videoId = $oembed->video_id;
}
if ( isset( $oembed->thumbnail_url ) ) {
$image = $oembed->thumbnail_url;
}
}
}
// Fallback to our old method to get the Vimeo ID
if ( empty( $videoId ) ) {
$isVimeo = preg_match( '/vimeo\.com/i', $url );
if ( $isVimeo ) {
$videoId = preg_replace( '/[^\/]+[^0-9]|(\/)/', '', rtrim( $url, '/' ) );
}
}
// Find large thumbnail using the Vimeo API v2
if ( ! empty( $videoId ) ) {
if ( function_exists( 'file_get_contents' ) ) {
$response = unserialize( file_get_contents( "https://vimeo.com/api/v2/video/{$videoId}.php" ) );
if ( is_array( $response ) && isset( $response[0]['thumbnail_large'] ) ) {
$image = $response[0]['thumbnail_large'];
}
} elseif ( function_exists( 'curl_init' ) ) {
$url = "https://vimeo.com/api/v2/video/{$videoId}.json";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_URL, $url );
$response = curl_exec( $ch );
curl_close( $ch );
if ( $response ) {
$json = json_decode( $response );
if ( is_array( $json ) && isset( $json[0]->thumbnail_large ) ) {
$image = $json[0]->thumbnail_large;
}
}
}
}
// Get image from private videos
if ( ! empty( $videoId ) && empty( $image ) ) {
if ( function_exists( 'curl_init' ) ) {
$config = self::getConfig();
if ( $token = $config->vimeo_authorization_token ) {
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "https://api.vimeo.com/videos/{$videoId}/pictures" );
$authorization = "Authorization: Bearer " . $token; // Prepare the authorisation token
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json' , $authorization )); // Inject the token into the header
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$response = curl_exec( $ch );
curl_close( $ch );
if ( $response ) {
$json = json_decode( $response );
if ( $json && isset( $json->data ) ) {
$canBreak = false;
foreach ( $json->data as $item ) {
foreach ( $item->sizes as $picture ) {
$image = $picture->link;
if ( $picture->width >= 400 ) {
$canBreak = true;
break;
}
}
if ( $canBreak ) break;
}
}
}
}
}
}
return $image;
}
public static function canDo() {
$db = JFactory::getDbo();
$query = 'SELECT id FROM #__yendifvideoshare_options WHERE name=' . $db->quote( 'access' );
$db->setQuery( $query );
$count = $db->loadResult();
return ( $count > 0 ) ? true : false;
}
}