2015-05-13 22:05:37 +02:00
|
|
|
<?php
|
|
|
|
|
2017-03-02 14:40:29 +01:00
|
|
|
require_once 'customvalidators/ConditionalNotEmpty.php';
|
|
|
|
|
2015-05-13 22:05:37 +02:00
|
|
|
class Application_Form_TuneInPreferences extends Zend_Form_SubForm
|
|
|
|
{
|
|
|
|
public function init()
|
|
|
|
{
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->setDecorators([
|
|
|
|
['ViewScript', ['viewScript' => 'form/preferences_tunein.phtml']],
|
|
|
|
]);
|
2015-05-13 22:05:37 +02:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$enableTunein = new Zend_Form_Element_Checkbox('enable_tunein');
|
|
|
|
$enableTunein->setDecorators([
|
2015-05-14 19:13:33 +02:00
|
|
|
'ViewHelper',
|
|
|
|
'Errors',
|
2021-10-11 16:10:47 +02:00
|
|
|
'Label',
|
|
|
|
]);
|
|
|
|
$enableTunein->addDecorator('Label', ['class' => 'enable-tunein']);
|
|
|
|
$enableTunein->setLabel(_('Push metadata to your station on TuneIn?'));
|
2015-05-13 22:05:37 +02:00
|
|
|
$enableTunein->setValue(Application_Model_Preference::getTuneinEnabled());
|
|
|
|
$this->addElement($enableTunein);
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinStationId = new Zend_Form_Element_Text('tunein_station_id');
|
|
|
|
$tuneinStationId->setLabel(_('Station ID:'));
|
2015-05-13 22:05:37 +02:00
|
|
|
$tuneinStationId->setValue(Application_Model_Preference::getTuneinStationId());
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinStationId->setAttrib('class', 'input_text');
|
2015-05-13 22:05:37 +02:00
|
|
|
$this->addElement($tuneinStationId);
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinPartnerKey = new Zend_Form_Element_Text('tunein_partner_key');
|
|
|
|
$tuneinPartnerKey->setLabel(_('Partner Key:'));
|
2015-05-13 22:05:37 +02:00
|
|
|
$tuneinPartnerKey->setValue(Application_Model_Preference::getTuneinPartnerKey());
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinPartnerKey->setAttrib('class', 'input_text');
|
2015-05-13 22:05:37 +02:00
|
|
|
$this->addElement($tuneinPartnerKey);
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinPartnerId = new Zend_Form_Element_Text('tunein_partner_id');
|
|
|
|
$tuneinPartnerId->setLabel(_('Partner Id:'));
|
2015-05-13 22:05:37 +02:00
|
|
|
$tuneinPartnerId->setValue(Application_Model_Preference::getTuneinPartnerId());
|
2021-10-11 16:10:47 +02:00
|
|
|
$tuneinPartnerId->setAttrib('class', 'input_text');
|
2015-05-13 22:05:37 +02:00
|
|
|
$this->addElement($tuneinPartnerId);
|
|
|
|
}
|
2015-05-14 19:13:33 +02:00
|
|
|
|
|
|
|
public function isValid($data)
|
|
|
|
{
|
2015-05-19 20:39:05 +02:00
|
|
|
$valid = true;
|
|
|
|
// Make request to TuneIn API to test the settings are valid.
|
|
|
|
// TuneIn does not have an API to make test requests to check if
|
|
|
|
// the credentials are correct. Therefore we will make a request
|
|
|
|
// with the commercial flag set to true, which removes the metadata
|
|
|
|
// from the station on TuneIn. After that, and if the test request
|
|
|
|
// succeeds, we will make another request with the real metadata.
|
2021-10-11 16:10:47 +02:00
|
|
|
if ($data['enable_tunein']) {
|
|
|
|
$credentialsQryStr = '?partnerId=' . $data['tunein_partner_id'] . '&partnerKey=' . $data['tunein_partner_key'] . '&id=' . $data['tunein_station_id'];
|
|
|
|
$commercialFlagQryStr = '&commercial=true';
|
2015-05-14 19:13:33 +02:00
|
|
|
|
2015-05-19 21:19:09 +02:00
|
|
|
$metadata = Application_Model_Schedule::getCurrentPlayingTrack();
|
|
|
|
|
|
|
|
if (is_null($metadata)) {
|
|
|
|
$qryStr = $credentialsQryStr . $commercialFlagQryStr;
|
|
|
|
} else {
|
2021-10-11 16:10:47 +02:00
|
|
|
$metadata['artist'] = empty($metadata['artist']) ? 'n/a' : $metadata['artist'];
|
|
|
|
$metadata['title'] = empty($metadata['title']) ? 'n/a' : $metadata['title'];
|
|
|
|
$metadataQryStr = '&artist=' . $metadata['artist'] . '&title=' . $metadata['title'];
|
2015-05-19 21:19:09 +02:00
|
|
|
|
|
|
|
$qryStr = $credentialsQryStr . $metadataQryStr;
|
|
|
|
}
|
|
|
|
|
2015-05-14 19:13:33 +02:00
|
|
|
$ch = curl_init();
|
2015-05-19 21:19:09 +02:00
|
|
|
curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qryStr);
|
2015-05-14 19:13:33 +02:00
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
|
|
2015-05-19 20:39:05 +02:00
|
|
|
$xmlData = curl_exec($ch);
|
2015-05-14 19:13:33 +02:00
|
|
|
if (curl_error($ch)) {
|
2021-10-11 16:10:47 +02:00
|
|
|
Logging::error('Failed to reach TuneIn: ' . curl_errno($ch) . ' - ' . curl_error($ch) . ' - ' . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
|
|
|
|
if (curl_error($ch) == 'The requested URL returned error: 403 Forbidden') {
|
|
|
|
$this->getElement('enable_tunein')->setErrors([_('Invalid TuneIn Settings. Please ensure your TuneIn settings are correct and try again.')]);
|
2015-05-19 20:39:05 +02:00
|
|
|
$valid = false;
|
2015-05-14 19:13:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
|
2015-05-19 20:39:05 +02:00
|
|
|
if ($valid) {
|
|
|
|
$xmlObj = new SimpleXMLElement($xmlData);
|
2021-10-11 16:10:47 +02:00
|
|
|
if (!$xmlObj || $xmlObj->head->status != '200') {
|
|
|
|
$this->getElement('enable_tunein')->setErrors([_('Invalid TuneIn Settings. Please ensure your TuneIn settings are correct and try again.')]);
|
2015-05-19 20:39:05 +02:00
|
|
|
$valid = false;
|
2021-10-11 16:10:47 +02:00
|
|
|
} elseif ($xmlObj->head->status == '200') {
|
2015-05-25 21:37:45 +02:00
|
|
|
Application_Model_Preference::setLastTuneinMetadataUpdate(time());
|
2015-05-19 20:39:05 +02:00
|
|
|
$valid = true;
|
|
|
|
}
|
|
|
|
}
|
2015-05-14 19:13:33 +02:00
|
|
|
} else {
|
2015-05-19 20:39:05 +02:00
|
|
|
$valid = true;
|
2015-05-14 19:13:33 +02:00
|
|
|
}
|
2015-05-19 20:39:05 +02:00
|
|
|
|
|
|
|
if (!$valid) {
|
|
|
|
// Set values to what the user entered since the form is invalid so they
|
|
|
|
// don't have to enter in the values again and can see what they entered.
|
2021-10-11 16:10:47 +02:00
|
|
|
$this->getElement('enable_tunein')->setValue($data['enable_tunein']);
|
|
|
|
$this->getElement('tunein_partner_key')->setValue($data['tunein_partner_key']);
|
|
|
|
$this->getElement('tunein_partner_id')->setValue($data['tunein_partner_id']);
|
|
|
|
$this->getElement('tunein_station_id')->setValue($data['tunein_station_id']);
|
2015-05-19 20:39:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $valid;
|
2015-05-14 19:13:33 +02:00
|
|
|
}
|
|
|
|
}
|