<?php
class SoundCloudController extends BaseController
{
public function search($title)
{
$settings = Setting::first();
$ads = Ads::first();
$limit = $settings->per_page;
$p = Input::get('page');
$offset = isset($p) ? $p-1 : 0;
$offset = $offset*$limit;
$tracks = array();
$title = str_replace('-', ' ', $title);
$youtubeID = null;
$thumbnail = null;
if (isset($settings->soundcloud_id) && !empty($settings->soundcloud_id)) {
try {
//get tracks from soundcloud
$jsonData = file_get_contents('https://api.soundcloud.com/tracks.json?offset='.$offset.'&limit='.$limit.'&client_id='.$settings->soundcloud_id.'&q='.urlencode($title));
if ($jsonData != "[]") {
$jsonData = json_decode($jsonData, true);
foreach ($jsonData as $track) {
$tracks[] = array('id' => $track['id'],
'title' => $track['title'],
'size' => $track['original_content_size'],
'image' => $track['artwork_url'],
'duration' => $track['duration'],
'genre' => $track['genre']
);
}
}
//get related video link from youtube
$youtubeData = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=id&type=video&key='.$settings->youtube_key.'&maxResults=1&q='.urlencode($title));
if ($youtubeData) {
$youtubeData = json_decode($youtubeData, true);
$youtubeData = $youtubeData['items'];
if (isset($youtubeData[0])) {
$youtubeID = $youtubeData[0]['id']['videoId'];
//extract thumbnail from youtube json data
$thumbnail = 'https://i1.ytimg.com/vi/'.$youtubeID.'/sddefault.jpg';
}
}
} catch (ErrorException $ex) {
}
}
$max = 200;
$current_count = count($tracks);
if ($current_count < $settings->per_page) {
$max = $offset*$settings->per_page + $current_count;
}
//create a custom paginator
$pagination = Paginator::make($tracks, $max, $settings->per_page)->links('pagination::simple');
return View::make('search.search-results', array('settings' => $settings, 'ads' => $ads, 'keyword' => $title, 'tracks' => $tracks, 'pagination' => $pagination, 'youtubeID' => $youtubeID, 'metaImage' => $thumbnail));
}
//Download a track from soundcloud
public function download($title, $trackID)
{
$settings = Setting::first();
$trackID = empty($trackID) ? Input::get('id') : $trackID;
$headers = get_headers('http://api.soundcloud.com/tracks/'.$trackID.'/stream?client_id='.$settings->soundcloud_id, 1);
if (isset($headers['Location']) && isset($headers['Content-Length'][1])) {
$filename = $title."[".$settings->website_name."]".".mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filename).'"'); // adding quotes and stripping full path
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Cache-Control: private", false);
header('Pragma: public');
header('Content-length: ' . $headers["Content-Length"][1]);
readfile($headers['Location']);
} else {
return Redirect::to('/')->with(array('toastMsg' =>Lang::get('words.invalid_track'), 'toastType' => 'showErrorToast'));
}
}
}