Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Mixcloud Integration #46

Open
wants to merge 8 commits into
base: 2.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions airtime_mvc/application/configs/ACL.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
->add(new Zend_Acl_Resource('usersettings'))
->add(new Zend_Acl_Resource('audiopreview'))
->add(new Zend_Acl_Resource('webstream'))
->add(new Zend_Acl_Resource('mixcloud'))
->add(new Zend_Acl_Resource('locale'));

/** Creating permissions */
Expand All @@ -52,6 +53,7 @@
->allow('A', 'listenerstat')
->allow('A', 'user')
->allow('A', 'systemstatus')
->allow('A', 'mixcloud')
->allow('A', 'preference');


Expand Down
11 changes: 11 additions & 0 deletions airtime_mvc/application/configs/conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ public static function loadConfig() {

$CC_CONFIG['soundcloud-connection-retries'] = $values['soundcloud']['connection_retries'];
$CC_CONFIG['soundcloud-connection-wait'] = $values['soundcloud']['time_between_retries'];

if (array_key_exists('mixcloud', $values))
{
$CC_CONFIG['mixcloud'] = true;
$CC_CONFIG['mixcloud_client_id'] = $values['mixcloud']['client_id'];
$CC_CONFIG['mixcloud_client_secret'] = $values['mixcloud']['client_secret'];
} else {
$CC_CONFIG['mixcloud'] = false;
$CC_CONFIG['mixcloud_client_id'] = '';
$CC_CONFIG['mixcloud_client_secret'] = '';
}

if(isset($values['demo']['demo'])){
$CC_CONFIG['demo'] = $values['demo']['demo'];
Expand Down
4 changes: 4 additions & 0 deletions airtime_mvc/application/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ public function uploadRecordedActionParam($show_instance_id, $file_id)
$id = $file->getId();
Application_Model_Soundcloud::uploadSoundcloud($id);
}
if (!$showCanceled && Application_Model_Preference::GetAutoUploadRecordedShowToMixcloud()) {
$id = $file->getId();
Application_Model_Mixcloud::uploadMixcloud($id);
}
}

public function mediaMonitorSetupAction()
Expand Down
37 changes: 37 additions & 0 deletions airtime_mvc/application/controllers/LibraryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function init()
->addActionContext('context-menu', 'json')
->addActionContext('get-file-metadata', 'html')
->addActionContext('upload-file-soundcloud', 'json')
->addActionContext('upload-file-mixcloud', 'json')
->addActionContext('get-upload-to-soundcloud-status', 'json')
->addActionContext('set-num-entries', 'json')
->addActionContext('edit-file-md', 'json')
Expand Down Expand Up @@ -291,6 +292,33 @@ public function contextMenuAction()
$menu["soundcloud"]["items"]["upload"] = array("name" => $text, "icon" => "soundcloud", "url" => $baseUrl."library/upload-file-soundcloud/id/{$id}");
}

//Mixcloud menu options
if ($type === "audioclip" && Application_Model_Preference::GetMixcloudEnabled()) {

//create a menu separator
$menu["sep1"] = "-----------";

//create a sub menu for Mixcloud actions.
$menu["mixcloud"] = array("name" => _("Mixcloud"), "icon" => "", "items" => array());

/*
$scid = $file->getMixcloudId();
if ($scid > 0) {
$url = $file->getMixcloudLinkToFile();
$menu["mixcloud"]["items"]["view"] = array("name" => _("View on Mixcloud"), "icon" => "mixcloud", "url" => $url);
}
*/

//if (!is_null($scid)) {
// $text = _("Re-upload to Mixcloud");
//} else {
$text = _("Upload to Mixcloud");
//}

$menu["mixcloud"]["items"]["upload"] = array("name" => $text, "icon" => "", "url" => $baseUrl."library/upload-file-mixcloud/id/{$id}");
}


if (empty($menu)) {
$menu["noaction"] = array("name"=>_("No action available"));
}
Expand Down Expand Up @@ -570,4 +598,13 @@ public function getUploadToSoundcloudStatusAction()
Logging::warn("Trying to upload unknown type: $type with id: $id");
}
}

public function uploadFileMixcloudAction()
{
$id = $this->_getParam('id');
Application_Model_Mixcloud::uploadMixcloud($id);
// we should die with ui info
$this->_helper->json->sendJson(null);
}

}
118 changes: 118 additions & 0 deletions airtime_mvc/application/controllers/MixcloudController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

require_once('php-oauth2/Client.php');
require_once('php-oauth2/GrantType/IGrantType.php');
require_once('php-oauth2/GrantType/AuthorizationCode.php');

/**
The PHP PECL extension for OAuth only supports OAuth 1 and
the Zend Framework 1.x OAuth stuff is only OAuth 1.
That's why we're using this third party php-oauth2 thing.
*/

/** This controller provides a simple API for managing our OAuth
access to Mixcloud. It provides a few URLs that can be called:
/mixcloud/authorize
/mixcloud/deauthorize
/mixcloud/redirect

*/
class MixcloudController extends Zend_Controller_Action
{
protected $_clientId = '';
protected $_clientSecret = '';

const AUTHORIZATION_ENDPOINT = 'https://www.mixcloud.com/oauth/authorize';
const TOKEN_ENDPOINT = 'https://www.mixcloud.com/oauth/access_token';


/** Common initialization that gets called before any of the below action
functions get called. Zend doesn't recommend you override the constructor for some reason.
*/
public function init()
{
$CC_CONFIG = Config::getConfig();
$this->_clientId = $CC_CONFIG['mixcloud_client_id'];
$this->_clientSecret = $CC_CONFIG['mixcloud_client_secret'];

//Disable rendering of this controller
$this->view->layout()->disableLayout(); //Don't inject the standard Now Playing header.
$this->_helper->viewRenderer->setNoRender(true); //Don't use (phtml) templates
}

/** http://myairtime/mixcloud/authorize
* Prompt the user for their Mixcloud credentials using OAuth.
*/
public function authorizeAction()
{
$CC_CONFIG = Config::getConfig();
$request = $this->getRequest();
$baseUrl = $CC_CONFIG['baseUrl'] . ":" . $CC_CONFIG['basePort'];
$user = Application_Model_User::GetCurrentUser();
$userType = $user->getType();

$redirectUri = 'http://' . $baseUrl . '/mixcloud/redirect';

$client = new OAuth2\Client($this->_clientId, $this->_clientSecret);
if (!isset($_GET['code']))
{
$auth_url = $client->getAuthenticationUrl(self::AUTHORIZATION_ENDPOINT, $redirectUri);
header('Location: ' . $auth_url);
die('Redirect');
}
}

/** http://myairtime/mixcloud/redirect
* The URL that a user gets redirected to after
* a successful OAuth authorization.
*/
public function redirectAction()
{
$this->_helper->viewRenderer->setNoRender(false);

$CC_CONFIG = Config::getConfig();
$request = $this->getRequest();
$baseUrl = $CC_CONFIG['baseUrl'] . ":" . $CC_CONFIG['basePort'];

//We have an OAuth code now, so next we need to ask for a request token.
$redirectUri = 'http://' . $baseUrl . '/mixcloud/redirect';

$client = new OAuth2\Client($this->_clientId, $this->_clientSecret);
$params = array('code' => $_GET['code'], 'redirect_uri' => $redirectUri);
$response = $client->getAccessToken(self::TOKEN_ENDPOINT, 'authorization_code', $params);
//var_dump($response, $response['result']);
//parse_str($response['result'], $info);
$info = $response['result'];
$accessToken = $info['access_token'];

//Save the request token to the Airtime preferences so we can use the Mixcloud API
//at any time later.
Application_Model_Preference::setMixcloudRequestToken($accessToken);
Application_Model_Preference::SetMixcloudUser("Connected");
//Here's a test of the Mixcloud API using this access token:
/*
$client->setAccessToken($info['access_token']);
$response = $client->fetch('https://api.mixcloud.com/spartacus/party-time/');
var_dump($response, $response['result']);
*/
}

/** http://myairtime/mixcloud/deauthorize
* Deauthorize the Airtime application by forgetting the OAuth request token.
*/
public function deauthorizeAction()
{
$this->_helper->viewRenderer->setNoRender(false);

$CC_CONFIG = Config::getConfig();
$request = $this->getRequest();
$baseUrl = $CC_CONFIG['baseUrl'] . ":" . $CC_CONFIG['basePort'];
$user = Application_Model_User::GetCurrentUser();
$userType = $user->getType();

//Clear the previously saved request token from the preferences.
Application_Model_Preference::setMixcloudRequestToken("");
}
}


4 changes: 4 additions & 0 deletions airtime_mvc/application/controllers/PreferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function indexAction()
Application_Model_Preference::SetSoundCloudTrackType($values["SoundCloudTrackType"]);
Application_Model_Preference::SetSoundCloudLicense($values["SoundCloudLicense"]);

Application_Model_Preference::SetAutoUploadRecordedShowToMixcloud($values["MixcloudAutoUpload"]);
//Application_Model_Preference::SetUploadToMixcloudOption($values["UploadToMixcloudOption"]);
//Application_Model_Preference::SetMixcloudRequestToken($values["MixcloudToken"]);

$this->view->statusMsg = "<div class='success'>". _("Preferences updated.")."</div>";
$this->view->form = $form;
$this->_helper->json->sendJson(array("valid"=>"true", "html"=>$this->view->render('preference/index.phtml')));
Expand Down
58 changes: 58 additions & 0 deletions airtime_mvc/application/forms/MixcloudPreferences.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
require_once 'customvalidators/ConditionalNotEmpty.php';
require_once 'customvalidators/PasswordNotEmpty.php';

class Application_Form_MixcloudPreferences extends Zend_Form_SubForm
{
public function init()
{
$CC_CONFIG = Config::getConfig();
if (!$CC_CONFIG['mixcloud'] ||
$CC_CONFIG['mixcloud_client_id'] === '')
{
return;
}

$this->setDecorators(array(
array('ViewScript', array('viewScript' => 'form/preferences_mixcloud.phtml'))
));

$isMixcloudConnected = true;
if (Application_Model_Preference::GetMixcloudRequestToken() === "") {
$isMixcloudConnected = false;
}

//Connect to MixCloud
$elem = $this->addElement(
( $isMixcloudConnected ? 'hidden' : 'button'),
'ConnectToMixcloud', array(
'label' => _('Connect to Mixcloud'),
'required' => false,
'decorators' => array(
'ViewHelper'
),
));

//Disconnect from MixCloud
$this->addElement(
( $isMixcloudConnected ? 'button' : 'hidden'),
'DisconnectFromMixcloud', array(
'label' => _('Disconnect from Mixcloud'),
'required' => false,
'decorators' => array(
'ViewHelper'
),
));

//Automatic Mixcloud uploads
$this->addElement('checkbox', 'MixcloudAutoUpload', array(
'label' => _('Automatically Upload Recorded Shows'),
'required' => false,
'value' => Application_Model_Preference::GetAutoUploadRecordedShowToMixcloud(),
'decorators' => array(
'ViewHelper'
)
));
}

}
3 changes: 3 additions & 0 deletions airtime_mvc/application/forms/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ public function init()
$soundcloud_pref = new Application_Form_SoundcloudPreferences();
$this->addSubForm($soundcloud_pref, 'preferences_soundcloud');

$mixcloud_pref = new Application_Form_MixcloudPreferences();
$this->addSubForm($mixcloud_pref, 'preferences_mixcloud');

}
}
11 changes: 11 additions & 0 deletions airtime_mvc/application/models/Mixcloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class Application_Model_Mixcloud
{
public static function uploadMixcloud($id)
{
$cmd = "/usr/lib/airtime/utils/mixcloud-uploader $id > /dev/null &";
Logging::info("Uploading to mixcloud with command: $cmd");
exec($cmd);
}
}
46 changes: 46 additions & 0 deletions airtime_mvc/application/models/Preference.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,42 @@ public static function GetSoundCloudLicense()
return self::getValue("soundcloud_license");
}

public static function SetAutoUploadRecordedShowToMixcloud($upload)
{
self::setValue("mixcloud_auto_upload_recorded_show", $upload);
}

public static function GetAutoUploadRecordedShowToMixcloud()
{
return self::getValue("mixcloud_auto_upload_recorded_show");
}


public static function SetMixcloudUser($user)
{
self::setValue("mixcloud_user", $user);
}

public static function GetMixcloudUser()
{
return self::getValue("mixcloud_user");
}

public static function SetMixcloudRequestToken($token)
{
self::setValue("mixcloud_request_token", $token);
}

public static function GetMixcloudRequestToken()
{
return self::getValue("mixcloud_request_token");
}

public static function GetMixcloudEnabled()
{
return (self::getValue("mixcloud_request_token") !== "");
}

public static function SetAllow3rdPartyApi($bool)
{
self::setValue("third_party_api", $bool);
Expand Down Expand Up @@ -933,6 +969,16 @@ public static function GetSoundCloudDownloadbleOption()
return self::getValue("soundcloud_downloadable");
}

public static function SetUploadToMixcloudOption($upload)
{
self::setValue("mixcloud_upload_option", $upload);
}

public static function GetUploadToMixcloudOption()
{
return self::getValue("mixcloud_upload_option");
}

public static function SetWeekStartDay($day)
{
self::setValue("week_start_day", $day);
Expand Down
Loading