Skip to content

Commit

Permalink
Replace 'file_get_contents()' function with CURL
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpede committed Nov 15, 2017
1 parent 237c05a commit 2aa1d7b
Showing 1 changed file with 84 additions and 66 deletions.
150 changes: 84 additions & 66 deletions core/components/getyoutube/model/getyoutube/search.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,43 @@
* Place, Suite 330, Boston, MA 02111-1307 USA
*/

class search {
class Search {

/** @var modX $modx */
private $modx;
public function __construct(modX &$modx) {
$this->modx =& $modx;
}

/**
* CURL request and return data.
*
* @param string $url The url to fetch.
* @return mixed $data The data returned.
*/
public function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

public function channel($channelUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$totalVar){
global $modx;

$json = file_get_contents($channelUrl)
or $modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Channel API request not recognised');
$json = $this->file_get_contents_curl($channelUrl)
or $this->modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Channel API request not recognised');
$videos = json_decode($json, TRUE);

/* SETUP PAGINATION */
$total = $videos['pageInfo']['totalResults'];
$modx->setPlaceholder($totalVar,$total);
$this->modx->setPlaceholder($totalVar,$total);
$nextPageToken = $videos['nextPageToken'];
if (!empty($nextPageToken) ? $modx->setPlaceholder('nextPage',$modx->makeUrl($modx->resource->get('id'),'','?page='.$nextPageToken,'full')) : '');
if (!empty($nextPageToken) ? $this->modx->setPlaceholder('nextPage',$this->modx->makeUrl($this->modx->resource->get('id'),'','?page='.$nextPageToken,'full')) : '');
$prevPageToken = $videos['prevPageToken'];
if (!empty($prevPageToken) ? $modx->setPlaceholder('prevPage',$modx->makeUrl($modx->resource->get('id'),'','?page='.$prevPageToken,'full')) : '');
if (!empty($prevPageToken) ? $this->modx->setPlaceholder('prevPage',$this->modx->makeUrl($this->modx->resource->get('id'),'','?page='.$prevPageToken,'full')) : '');

$idx = 0; //Starts index at 0
$total = 0;
Expand All @@ -40,16 +62,16 @@ public function channel($channelUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$tota

foreach($videos['items'] as $video) {
/* SET PLACEHOLDERS */
$modx->setPlaceholder('id',$video['id']['videoId']);
$modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['id']['videoId']);
$modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['id']['videoId']);
$modx->setPlaceholder('title',$video['snippet']['title']);
$modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$modx->setPlaceholder('description',$video['snippet']['description']);
$modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
$this->modx->setPlaceholder('id',$video['id']['videoId']);
$this->modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['id']['videoId']);
$this->modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['id']['videoId']);
$this->modx->setPlaceholder('title',$video['snippet']['title']);
$this->modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$this->modx->setPlaceholder('description',$video['snippet']['description']);
$this->modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$this->modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$this->modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$this->modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
/* SET TEMPLATES */
if (!empty($tplAlt)) {
if($idx % 2 == 0) { // Checks if index can be divided by 2 (alt)
Expand All @@ -62,31 +84,30 @@ public function channel($channelUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$tota
}
$idx++; //Increases index by +1

$results .= $modx->getChunk($rowTpl,$video);
$results .= $this->modx->getChunk($rowTpl,$video);
}
if(!empty($results)) {
if (!empty($toPlaceholder)) {
$output = $modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
$output = $this->modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
}else{
$output = $results;
}
}
return $output;
}
public function playlist($playlistUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$totalVar){
global $modx;

$json = file_get_contents($playlistUrl)
or $modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Playlist API request not recognised');
$json = $this->file_get_contents_curl($playlistUrl)
or $this->modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Playlist API request not recognised');
$videos = json_decode($json, TRUE);

/* SETUP PAGINATION */
$total = $videos['pageInfo']['totalResults'];
$modx->setPlaceholder($totalVar,$total);
$this->modx->setPlaceholder($totalVar,$total);
$nextPageToken = $videos['nextPageToken'];
if (!empty($nextPageToken) ? $modx->setPlaceholder('nextPage',$modx->makeUrl($modx->resource->get('id'),'','?page='.$nextPageToken,'full')) : '');
if (!empty($nextPageToken) ? $this->modx->setPlaceholder('nextPage',$this->modx->makeUrl($this->modx->resource->get('id'),'','?page='.$nextPageToken,'full')) : '');
$prevPageToken = $videos['prevPageToken'];
if (!empty($prevPageToken) ? $modx->setPlaceholder('prevPage',$modx->makeUrl($modx->resource->get('id'),'','?page='.$prevPageToken,'full')) : '');
if (!empty($prevPageToken) ? $this->modx->setPlaceholder('prevPage',$this->modx->makeUrl($this->modx->resource->get('id'),'','?page='.$prevPageToken,'full')) : '');

$idx = 0; //Starts index at 0
$total = 0;
Expand All @@ -95,19 +116,19 @@ public function playlist($playlistUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$to

foreach($videos['items'] as $video) {
/* SET PLACEHOLDERS */
$modx->setPlaceholder('id',$video['snippet']['resourceId']['videoId']);
$modx->setPlaceholder('playlist_id',$video['snippet']['playlistId']);
$modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['snippet']['resourceId']['videoId']);
$modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['snippet']['resourceId']['videoId']);
$modx->setPlaceholder('title',$video['snippet']['title']);
$modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$modx->setPlaceholder('description',$video['snippet']['description']);
$modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
$modx->setPlaceholder('thumbnail_standard',$video['snippet']['thumbnails']['standard']['url']); //640px wide and 480px tall
$modx->setPlaceholder('thumbnail_maxres',$video['snippet']['thumbnails']['maxres']['url']); //1280px wide and 720px tall
$this->modx->setPlaceholder('id',$video['snippet']['resourceId']['videoId']);
$this->modx->setPlaceholder('playlist_id',$video['snippet']['playlistId']);
$this->modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['snippet']['resourceId']['videoId']);
$this->modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['snippet']['resourceId']['videoId']);
$this->modx->setPlaceholder('title',$video['snippet']['title']);
$this->modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$this->modx->setPlaceholder('description',$video['snippet']['description']);
$this->modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$this->modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$this->modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$this->modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
$this->modx->setPlaceholder('thumbnail_standard',$video['snippet']['thumbnails']['standard']['url']); //640px wide and 480px tall
$this->modx->setPlaceholder('thumbnail_maxres',$video['snippet']['thumbnails']['maxres']['url']); //1280px wide and 720px tall

/* SET TEMPLATES */
if (!empty($tplAlt)) {
Expand All @@ -121,57 +142,54 @@ public function playlist($playlistUrl,$tpl,$tplAlt,$toPlaceholder,$pageToken,$to
}
$idx++; //Increases index by +1

$results .= $modx->getChunk($rowTpl,$video);
$results .= $this->modx->getChunk($rowTpl,$video);
}
if(!empty($results)) {
if (!empty($toPlaceholder)) {
$output = $modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
$output = $this->modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
}else{
$output = $results;
}
}
return $output;
}
public function video($videoUrl,$tpl,$tplAlt,$toPlaceholder,$totalVar){
global $modx;

$json = file_get_contents($videoUrl)
or $modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Video API request not recognised');
$json = $this->file_get_contents_curl($videoUrl) or $this->modx->log(modX::LOG_LEVEL_ERROR, 'getYoutube() - Video API request not recognised');
$videos = json_decode($json, TRUE);

/* SET TOTAL PLACEHOLDERS */
$total = $videos['pageInfo']['totalResults'];
$modx->setPlaceholder($totalVar,$total);

$this->modx->setPlaceholder($totalVar,$total);
$idx = 0; //Starts index at 0
$total = 0;

$output = '';

foreach($videos['items'] as $video) {
/* SET SNIPPET PLACEHOLDERS */
$modx->setPlaceholder('id',$video['id']);
$modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['id']);
$modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['id']);
$modx->setPlaceholder('title',$video['snippet']['title']);
$modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$modx->setPlaceholder('description',$video['snippet']['description']);
$modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$modx->setPlaceholder('tags',implode(", ", $video['snippet']['tags']));
$this->modx->setPlaceholder('id',$video['id']);
$this->modx->setPlaceholder('url',"https://www.youtube.com/watch?v=" . $video['id']);
$this->modx->setPlaceholder('embed_url',"https://www.youtube.com/embed/" . $video['id']);
$this->modx->setPlaceholder('title',$video['snippet']['title']);
$this->modx->setPlaceholder('channel_title',$video['snippet']['channelTitle']);
$this->modx->setPlaceholder('description',$video['snippet']['description']);
$this->modx->setPlaceholder('publish_date',$video['snippet']['publishedAt']);
$this->modx->setPlaceholder('tags',implode(", ", $video['snippet']['tags']));
/* SET IMAGE PLACEHOLDERS */
$modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
$modx->setPlaceholder('thumbnail_standard',$video['snippet']['thumbnails']['standard']['url']); //640px wide and 480px tall
$modx->setPlaceholder('thumbnail_maxres',$video['snippet']['thumbnails']['maxres']['url']); //1280px wide and 720px tall
$this->modx->setPlaceholder('thumbnail_small',$video['snippet']['thumbnails']['default']['url']); //120px wide and 90px tall
$this->modx->setPlaceholder('thumbnail_medium',$video['snippet']['thumbnails']['medium']['url']); //320px wide and 180px tall
$this->modx->setPlaceholder('thumbnail_large',$video['snippet']['thumbnails']['high']['url']); //480px wide and 360px tall
$this->modx->setPlaceholder('thumbnail_standard',$video['snippet']['thumbnails']['standard']['url']); //640px wide and 480px tall
$this->modx->setPlaceholder('thumbnail_maxres',$video['snippet']['thumbnails']['maxres']['url']); //1280px wide and 720px tall
/* SET CONTENT DETAIL PLACEHOLDERS */
$modx->setPlaceholder('duration',$video['contentDetails']['duration']);
$this->modx->setPlaceholder('duration',$video['contentDetails']['duration']);
/* SET STATISTIC PLACEHOLDERS */
$modx->setPlaceholder('viewCount',$video['statistics']['viewCount']);
$modx->setPlaceholder('likeCount',$video['statistics']['likeCount']);
$modx->setPlaceholder('dislikeCount',$video['statistics']['dislikeCount']);
$modx->setPlaceholder('favoriteCount',$video['statistics']['favoriteCount']);
$modx->setPlaceholder('commentCount',$video['statistics']['commentCount']);
$this->modx->setPlaceholder('viewCount',$video['statistics']['viewCount']);
$this->modx->setPlaceholder('likeCount',$video['statistics']['likeCount']);
$this->modx->setPlaceholder('dislikeCount',$video['statistics']['dislikeCount']);
$this->modx->setPlaceholder('favoriteCount',$video['statistics']['favoriteCount']);
$this->modx->setPlaceholder('commentCount',$video['statistics']['commentCount']);
/* SET TEMPLATES */
if (!empty($tplAlt)) {
if($idx % 2 == 0) { // Checks if index can be divided by 2 (alt)
Expand All @@ -184,11 +202,11 @@ public function video($videoUrl,$tpl,$tplAlt,$toPlaceholder,$totalVar){
}
$idx++; //Increases index by +1

$results .= $modx->getChunk($rowTpl,$video);
$results .= $this->modx->getChunk($rowTpl,$video);
}
if(!empty($results)) {
if (!empty($toPlaceholder)) {
$output = $modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
$output = $this->modx->setPlaceholder($toPlaceholder,$results); //Set '$toPlaceholder' placeholder
}else{
$output = $results;
}
Expand Down

0 comments on commit 2aa1d7b

Please sign in to comment.