audioscrobbler(); ?> * **/ class AudioScrobbler extends Plugin { private $config = array(); private $class_name = ''; private $default_options = array( 'username' => '', 'cache' => '60' ); /** * Required plugin information * @return array The array of information **/ public function info() { return array( 'name' => 'AudioScrobbler', 'version' => '0.2.1', 'url' => 'http://blog.bcse.info/', 'author' => 'Joel Lee', 'authorurl' => 'http://blog.bcse.info/', 'license' => 'MIT License', 'description' => 'Display what you recent listen to.', 'copyright' => '2008' ); } /** * 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'); } 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') : $ui = new FormUI($this->class_name); $ui->append( 'text', 'username', 'option:' . $this->class_name . '__username', _t('Last.fm Username') ); $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)') ); $ui->cache->add_validator( array( $this, 'validate_cache' ) ); $ui->append( 'submit', 'save', _t('Save') ); $ui->set_option( 'success_message', _t('Options saved') ); $ui->out(); break; } } } public function validate_username( $username ) { if ( ! ctype_alnum($username) ) { return array(_t('Your Last.fm username is not valid.')); } return array(); } public function validate_cache( $cache ) { if ( ! ctype_digit($cache) || $cache < 0 ) { return array(_t('The cache expiry time must be positive integer.')); } 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 ) { if ( $this->config['username'] != '' ) { $track_url = 'http://ws.audioscrobbler.com/1.0/user/' . urlencode( $this->config['username'] ) . '/recenttracks.xml'; if ( Cache::has( $this->class_name . '__recenttracks' ) ) { $xml = new SimpleXMLElement( Cache::get( $this->class_name . '__recenttracks' ) ); $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 AudioScrobbler.') ); } $response = $call->get_response_body(); // Parse XML content $xml = new SimpleXMLElement( $response ); $theme->track = $xml->track; Cache::set( $this->class_name . '__recenttracks', $response, $this->config['cache'] ); } catch ( Exception $e ) { $theme->track = $e->getMessage(); } } } else { $theme->track = _t('Please set your username in the AudioScrobbler plugin config.'); } 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->add_template('audioscrobbler', dirname(__FILE__) . '/audioscrobbler.php'); } } ?>