Example #1
0
<?php

session_start();
error_reporting(E_ALL & ~E_NOTICE ^ E_DEPRECATED);
$clientId = "10795ee3fb0bc2f1ba76f33d92e75ac7";
$clientSecret = "b1bb0d93211f90dd1648a76947bd4b9d";
$callback = "http://localhost/sound/sound.php";
require_once 'lib/Soundcloud.php';
$soundcloud = new Services_Soundcloud($clientId, $clientSecret, $callback);
$soundcloud->setDevelopment(false);
$soundcloud->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1, CURLOPT_SSL_VERIFYPEER => 0));
$authorizeUrl = $soundcloud->getAuthorizeUrl();
?>


  <a class="login"href="<?php 
echo $authorizeUrl;
?>
">Connect to Souncloud</a>


<?php 
try {
    // if(!isset($_SESSION['token']))
    // {
    //      print_r($_SESSION);
    $accessToken = $soundcloud->accessToken($_GET['code']);
    //       $_SESSION['token'] = $accessToken['access_token'];
    // }
    // else
    // {
Example #2
0
function mg_get_soundcloud_embed($track_url)
{
    include_once MG_DIR . '/classes/soundcloud-api/Services/Soundcloud.php';
    $pub = '69c06a70f88e8ec80a414ae55dab369c';
    $sec = 'f7fe291ae99ef71d3fd81c084897309e';
    $callback = 'http://www.lcweb.it/';
    $client = new Services_Soundcloud($pub, $sec, $callback);
    $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
    $track = json_decode($client->get('resolve', array('url' => $track_url)));
    $autoplay = get_option('mg_audio_autoplay') ? 'true' : 'false';
    return '<iframe width="100%" height="166" scrolling="no" frameborder="no" src="//w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/' . $track->id . '&amp;color=ff5500&amp;auto_play=' . $autoplay . '&amp;hide_related=true&amp;show_artwork=true" style="margin-top: 7px;"></iframe>';
}
Example #3
0
function sc_render_manager_page()
{
    require_once plugin_dir_path(__FILE__) . 'includes/Services/Soundcloud.php';
    require_once plugin_dir_path(__FILE__) . 'includes/class/pagination.class.php';
    $options = get_option('sc_options');
    $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']);
    // Error message if app settings are not filled in.
    if ($options['client_id'] == "Your client ID goes here" || $options['client_secret'] == "Your client secret goes here" || $options['callback_url'] == "Your callback url goes here") {
        echo '<div class="error fade"><p>Warning: One or more of the required fields have not been set. Please insure that your cliend ID, client secret, and callback url are defined <a href="' . admin_url('options-general.php?page=soundcloud/soundcloud.php') . '">HERE</a>.</br>Soundcloud functionality will not be available until these fields are set.</p></div></br>';
    }
    // SoundCloud login/logout
    if (isset($_GET['code']) || get_option('soundcloud_access_token')) {
        echo '<a href="' . admin_url('admin.php?page=soundcloud-disconnect') . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-disconnect-l.png" alt="Disconnect"/></a>';
        try {
            $client = new Services_Soundcloud($options['client_id'], $options['client_secret'], $options['callback_url']);
            if (isset($_GET['code'])) {
                $accessToken = $client->accessToken($_GET['code']);
                update_option('soundcloud_access_token', $accessToken['access_token']);
                update_option('soundcloud_refresh_token', $accessToken['refresh_token']);
                update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']);
            } else {
                if (get_option('soundcloud_access_token')) {
                    // refresh token code deprecated due to non-expiring tokens
                    /*if (time() > get_option('soundcloud_token_expiration')) {
                    			$client->setAccessToken(get_option('soundcloud_access_token'));
                    			
                    			// refresh access token
                    			$accessToken = $client->accessTokenRefresh(get_option('soundcloud_refresh_token'));	
                    			//$client->setAccessToken($accessToken['access_token']);							
                    			update_option('soundcloud_access_token', $accessToken['access_token']);
                    			update_option('soundcloud_refresh_token', $accessToken['refresh_token']);
                    			update_option('soundcloud_token_expiration', time() + $accessToken['expires_in']);
                    		}
                    		else
                    		{*/
                    $client->setAccessToken(get_option('soundcloud_access_token'));
                    //}
                }
            }
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
            var_dump($e->getMessage());
            exit;
        }
    } else {
        echo '<a href="' . $client->getAuthorizeUrl(array('scope' => 'non-expiring')) . '" class="soundcloud-icon"><img src="' . plugins_url() . '/soundcloud/includes/images/btn-connect-sc-l.png" alt="Connect with Soundcloud"/></a>';
    }
    // tab switching
    if (isset($_GET['tab'])) {
        sc_admin_tabs($_GET['tab']);
        $tab = $_GET['tab'];
    } else {
        sc_admin_tabs('user');
        $tab = 'user';
    }
    if (get_option('soundcloud_access_token')) {
        try {
            $me = json_decode($client->get('me'), true);
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
            var_dump($e->getMessage());
            exit;
        }
        echo '<h3>Logged in as: ' . $me['username'] . '</h3>';
    } else {
        echo '<h3>Not logged in.</h3>';
    }
    switch ($tab) {
        case 'user':
            try {
                $me = json_decode($client->get('me'), true);
                ?>
				<form action="" method="post">
					<table class="form-table">
						<tr valign="top">
						  <th scope="row"><label for="username">Username:</label></th>
						  <td><input type="text" name="username" value="<?php 
                echo $me['username'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="permalink">Permalink:</label></th>
						  <td><input type="text" name="permalink" value="<?php 
                echo $me['permalink'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="description">Description:</label></th>
						  <td><input type="text" name="description" value="<?php 
                echo $me['description'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="website">Website:</label></th>
						  <td><input type="text" name="website" value="<?php 
                echo $me['website'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
						<tr valign="top">
						  <th scope="row"><label for="website_title">Website Name:</label></th>
						  <td><input type="text" name="website_title" value="<?php 
                echo $me['website_title'];
                ?>
" size="30" class="regular-text code"></td>
						</tr>
					</table>
					<p class="submit"><input type="submit" value="Update Account" class="button-primary"></p>
					
				</form>
				<?php 
                if (isset($_POST['username']) || isset($_POST['permalink']) || isset($_POST['description']) || isset($_POST['website']) || isset($_POST['website_title'])) {
                    $response = json_decode($client->put('me', array('user[username]' => $_POST['username'], 'user[permalink]' => $_POST['permalink'], 'user[description]' => $_POST['description'], 'user[website]' => strlen($_POST['website']) ? $_POST['website'] : null, 'user[website_title]' => strlen($_POST['website_title']) ? $_POST['website_title'] : null)));
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
        case 'upload':
            ?>
	
			<form action="" enctype="multipart/form-data" method="post" id ="add_track">
				<table class="form-table">
					<tr valign="top">
					  <th scope="row"><label for="track_title">Track title:</label></th>
					  <td><input type="text" name="track_title" size="30" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="track_file">Please specify a track:</label></th>
					  <td><input type="file" name="track_file" id="track_file" size="40" accept="audio/*" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="track_art">Please specify track artwork:</label></th>
					  <td><input type="file" name="track_art" id="track_art" size="40" accept="image/*" class="regular-text code"></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="tag">Track tags:</label></th>
					  <td><input type="text" name="tag" id="tag" size="30"><input type="button" id="add_tag" name="add_tag" class="button" value="Add Tag" />
						<br><ul id="tags"></ul></td>
					</tr>
					<tr valign="top">
					  <th scope="row"><label for="sharing">Track privacy:</label></th>
					  <td><select name="sharing">
						<option value="public">Public</option>
						<option value="private">Private</option>
					  </select></td>
					</tr>
					</table>
			<p class="submit"><input type="submit" value="Upload" class="button-primary"></p>
			</form>					
			<?php 
            try {
                $tmp_file = '/tmp/' . stripslashes($_FILES['track_file']['name']);
                $tmp_art_file = '/tmp/' . stripslashes($_FILES['track_art']['name']);
                if (move_uploaded_file($_FILES['track_file']['tmp_name'], $tmp_file) && move_uploaded_file($_FILES['track_art']['tmp_name'], $tmp_art_file)) {
                    // upload audio file
                    $track = json_decode($client->post('tracks', array('track[title]' => $_POST['track_title'], 'track[asset_data]' => '@' . $tmp_file, 'track[artwork_data]' => '@' . $tmp_art_file, 'track[tags]' => strlen($_POST['tags']) ? $_POST['tags'] : null, 'track[sharing]' => $_POST['sharing'])));
                    unlink(realpath($tmp_file));
                    unlink(realpath($tmp_art_file));
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
        case 'tracks':
            try {
                echo '</br>';
                $page_size = 5;
                // Pagination code
                $p = new pagination();
                $p->items($me['track_count']);
                $p->limit($page_size);
                // Limit entries per page
                $p->target("admin.php?page=soundcloud-manager&tab=tracks");
                $p->currentPage($_GET[$p->paging]);
                // Gets and validates the current page
                $p->calculate();
                // Calculates what to show
                $p->parameterName('paging');
                $p->adjacents(1);
                //No. of page away from the current page
                if (!isset($_GET['paging'])) {
                    $p->page = 1;
                } else {
                    $p->page = $_GET['paging'];
                }
                if ($p->page == 1) {
                    // get first page of tracks
                    $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size)));
                } else {
                    // get additional pages of tracks
                    $tracks = json_decode($client->get('users/' . $me['id'] . '/tracks', array('order' => 'created_at', 'limit' => $page_size, 'offset' => $page_size * $page)));
                }
                ?>
				<div class="tablenav">
					<div class='tablenav-pages'>
						<?php 
                echo $p->show();
                ?>
					</div>
				</div>
				<?php 
                foreach ($tracks as $track) {
                    $client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
                    $embed_info = json_decode($client->get('oembed', array('url' => $track->permalink_url)));
                    // render the html for the player widget
                    echo $embed_info->html . '</br></br>';
                }
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                var_dump($e->getMessage());
                exit;
            }
            break;
    }
}
}
//run search
$username = '******';
$tracksSearchQ = json_decode($scUser->get('tracks', array('q' => $username)), true);
//print_r($tracksSearchQ);
//print_r($tracksSearchQ[0]['permalink_url']);
//select trackurls(permalink url) from returned array
//cycle through top level array
foreach ($tracksSearchQ as $trackInfo) {
    //cycle through track info. array
    foreach ($trackInfo as $key => $value) {
        // echo "$key . 'is' . $value . '<br>'";
        //and identify urls
        if ($key == 'permalink_url') {
            //not sure what this does
            $scUser->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
            //echo "$key . 'is' . $value . '<br>'";
            try {
                //set var to array urls
                $track_url = $value;
                //print_r($track_url) ;
                echo "<br>";
                //$embed_info = json_decode($scUser->get('oembed', array('url' => $track_url)));
                //embed tracks using url and oembed code
                $embed_info = json_decode($scUser->get('oembed', array('url' => $track_url, 'maxheight' => '150', 'maxwidth' => '400')));
                // render the html for the player widget
                print $embed_info->html;
                echo "<br>";
                //print_r($embed_info);
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                exit($e->getMessage());
        function get_soundcloud_widget_code($track, $auto_play = false)
        {
            //get soundcloud options
            require_once 'includes/Services/Soundcloud.php';
            $sc_options = get_option('soundcloud_settings');
            if ($sc_options) {
                $sc_id = $sc_options['sc_client_id'];
                $sc_secret = $sc_options['sc_client_secret'];
                $sc_token = $sc_options['sc_client_access_token'];
                $sc_redirect_uri = $sc_options['sc_redirect_uri'];
            }
            $soundcloud = new Services_Soundcloud($sc_id, $sc_secret, $sc_redirect_uri);
            if ($sc_token && $sc_id && $sc_secret) {
                $soundcloud->setAccessToken($sc_token);
            } else {
                //display an error stating that the user needs to authenticate first
                return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: You need to authenticate your connection to the SoundCloud API.
				<br />Please go to the SoundCloud Ultimate plugin settings and configure your API credentials.</strong></p></div>';
            }
            //first check if track has finished uploading
            //$tracks = $soundcloud->get('tracks/');
            $soundcloud->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => 1));
            try {
                $track_list = $soundcloud->get('me/tracks');
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                if ($e->getHttpCode() == '302') {
                    //Special case - 302 received - continue to try and render the widget.....
                    $track_widget = $this->track_widget($soundcloud, $track, $auto_play);
                    return $track_widget;
                } else {
                    return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: Could not display your SoundCloud track - Error code (' . $e->getHttpCode() . ').</strong></p></div>';
                }
            }
            $tracks = json_decode($track_list, true);
            foreach ($tracks as $item) {
                if ($item['permalink_url'] == $track && $item['state'] != 'finished') {
                    return false;
                }
            }
            try {
                $track_embed_data = $soundcloud->get('oembed', array('url' => $track, 'auto_play' => $auto_play));
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                if ($e->getHttpCode() == '302') {
                    //Special case - 302 received - continue to try and render the widget.....
                    $track_widget = $this->track_widget($soundcloud, $track, $auto_play);
                    return $track_widget;
                } else {
                    if ($e->getHttpCode() == '404') {
                        return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: The track you specified in the shortcode does not exist in your account.</strong></div>';
                    } else {
                        return '<div style="color:red;"><p><strong>SoundCloud Ultimate Error: Could not display your SoundCloud track - Error code (' . $e->getHttpCode() . ').</strong></p></div>';
                    }
                }
            }
            //$embed_info = json_decode($soundcloud->get('oembed', array('url' => $track)));
            $embed_info = json_decode($track_embed_data);
            //now get the html code for the player widget
            //print $embed_info->html;
            $track_widget = $embed_info->html;
            return $track_widget;
        }