-
Notifications
You must be signed in to change notification settings - Fork 0
/
audioscrobbler.plugin.php
149 lines (134 loc) · 4.41 KB
/
audioscrobbler.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/**
* AudioScrobbler Plugin
*
* Usage: <?php $theme->audioscrobbler(); ?>
*
**/
class AudioScrobbler extends Plugin
{
private $config = array();
private $class_name = '';
private $default_options = array(
'username' => '',
'cache' => '60'
);
/**
* Add update beacon support
**/
public function action_update_check()
{
Update::add('Audioscrobbler', 'cb82d3d0-231f-11dd-bd0b-0800200c9a66', $this->info->version);
}
/**
* Add actions to the plugin page for this plugin
* @param array $actions An array of actions that apply to this plugin
* @param string $plugin_id The string id of a plugin, generated by the system
* @return array The array of actions to attach to the specified $plugin_id
**/
public function filter_plugin_config($actions, $plugin_id)
{
if ($plugin_id == $this->plugin_id()) {
$actions[] = _t('Configure', $this->class_name);
}
return $actions;
}
/**
* Respond to the user selecting an action on the plugin page
* @param string $plugin_id The string id of the acted-upon plugin
* @param string $action The action string supplied via the filter_plugin_config hook
**/
public function action_plugin_ui($plugin_id, $action)
{
if ($plugin_id == $this->plugin_id()) {
switch ($action) {
case _t('Configure', $this->class_name) :
$ui = new FormUI($this->class_name);
$ui->append('text', 'username', 'option:' . $this->class_name . '__username', _t('Last.fm Username', $this->class_name));
$ui->username->add_validator(array($this, 'validate_username'));
$ui->username->add_validator('validate_required');
$ui->append('text', 'cache', 'option:' . $this->class_name . '__cache', _t('Cache Expiry (in seconds)', $this->class_name));
$ui->cache->add_validator(array($this, 'validate_uint'));
$ui->append('submit', 'save', _t('Save', $this->class_name));
$ui->set_option('success_message', _t('Options saved', $this->class_name));
$ui->out();
break;
}
}
}
public function validate_username($username)
{
if (!ctype_alnum($username)) {
return array(_t('Your Last.fm username is not valid.', $this->class_name));
}
return array();
}
public function validate_uint($value)
{
if (!ctype_digit($value) || strstr($value, '.') || $value < 0) {
return array(_t('This field must be positive integer.', $this->class_name));
}
return array();
}
/**
* Returns true if plugin config form values defined in action_plugin_ui should be stored in options by Habari
* @return bool True if options should be stored
**/
public function updated_config($ui)
{
return true;
}
/**
* Add last Audioscrobbler status, time, and image to the available template vars
* @param Theme $theme The theme that will display the template
**/
public function theme_audioscrobbler($theme, $params = array())
{
$params = array_merge($this->config, $params);
$cache_name = $this->class_name . '__' . md5(serialize($params));
if ($params['username'] != '') {
$track_url = 'http://ws.audioscrobbler.com/1.0/user/' . urlencode($params['username']) . '/recenttracks.xml';
if (Cache::has($cache_name)) {
$xml = new SimpleXMLElement(Cache::get($cache_name));
$theme->track = $xml->track;
}
else {
try {
// Get XML content via Audioscrobbler API
$call = new RemoteRequest($track_url);
$call->set_timeout(5);
$result = $call->execute();
if (Error::is_error($result)) {
throw Error::raise(_t('Unable to contact Last.fm.', $this->class_name));
}
$response = $call->get_response_body();
// Parse XML content
$xml = new SimpleXMLElement($response);
$theme->track = $xml->track;
Cache::set($cache_name, $response, $params['cache']);
}
catch (Exception $e) {
$theme->track = $e->getMessage();
}
}
}
else {
$theme->track = _t('Please set your username in the Audioscrobbler plugin config.', $this->class_name);
}
return $theme->fetch('audioscrobbler');
}
/**
* On plugin init, add the template included with this plugin to the available templates in the theme
*/
public function action_init()
{
$this->class_name = strtolower(get_class($this));
// Set the default options
foreach ($this->default_options as $name => $unused) {
$this->config[$name] = Options::get($this->class_name . '__' . $name);
}
$this->load_text_domain($this->class_name);
$this->add_template('audioscrobbler', dirname(__FILE__) . '/audioscrobbler.php');
}
}
?>