~ubuntu-branches/debian/sid/ampache/sid

« back to all changes in this revision

Viewing changes to modules/plugins/Lastfm.plugin.php

  • Committer: Package Import Robot
  • Author(s): Charlie Smotherman
  • Date: 2013-08-27 13:19:48 UTC
  • mfrom: (1.2.9)
  • Revision ID: package-import@ubuntu.com-20130827131948-1czew0zxn6u70dtv
Tags: 3.6-rzb2752+dfsg-1
* New upsteam snapshot.  Contains important bug fixes to the installer.
* Correct typo in ampache-common.postrm.
* Remove courtousy copy of php-getid3, during repack.  Closes: #701526
* Update package to use dh_linktree to make the needed sym links to the
  needed system libs that were removed during repack.
* Update debian/rules to reflect upstreams removing/moving of modules.
* Update debian/ampache-common.install to reflect upstreams removal of files.
* Updated to use new apache2.4 API. Closes: #669756
* Updated /debian/po/de.po thx David Prévot for the patch.  Closes:  #691963
* M3U import is now ordered, fixed upstream.  Closes: #684984
* Text input area has been resized so IPv6 addresses will now fit, fixed
  upstream.  Closes:  #716230
* Added ampache-common.preinst to make sure that the courtousy copies of code
  dirs are empty so dh_linktree can do it's magic on upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
/* vim:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab: */
 
2
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
3
3
/**
4
 
 * AmpacheLastfm Class
5
 
 *
6
4
 *
7
5
 * LICENSE: GNU General Public License, version 2 (GPLv2)
8
 
 * Copyright (c) 2001 - 2011 Ampache.org All Rights Reserved
 
6
 * Copyright 2001 - 2013 Ampache.org
9
7
 *
10
8
 * This program is free software; you can redistribute it and/or
11
9
 * modify it under the terms of the GNU General Public License v2
20
18
 * along with this program; if not, write to the Free Software
21
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
20
 *
23
 
 * @category    AmpacheLastfm
24
 
 * @package     Ampache
25
 
 * @copyright   2001 - 2011 Ampache.org
26
 
 * @license     http://opensource.org/licenses/gpl-2.0 GPLv2
27
 
 * @link        http://www.ampache.org/
28
21
 */
29
22
 
30
 
/**
31
 
 * AmpacheLastfm Class
32
 
 *
33
 
 * Description here...
34
 
 *
35
 
 * @category    AmpacheLastfm
36
 
 * @package     Ampache
37
 
 * @copyright   2001 - 2011 Ampache.org
38
 
 * @license     http://opensource.org/licenses/gpl-2.0 GPLv2
39
 
 * @link        http://www.ampache.org/
40
 
 */
41
23
class AmpacheLastfm {
42
24
 
43
 
        public $name            ='Last.FM';
44
 
        public $description     ='Records your played songs to your Last.FM Account';
45
 
        public $url             ='';
46
 
        public $version         ='000004';
47
 
        public $min_ampache     ='360003';
48
 
        public $max_ampache     ='999999';
49
 
 
50
 
        // These are internal settings used by this class, run this->load to
51
 
        // fill them out
52
 
        private $username;
53
 
        private $password;
54
 
        private $hostname;
55
 
        private $port;
56
 
        private $path;
57
 
        private $challenge;
58
 
        private $user_id;
59
 
 
60
 
        /**
61
 
         * Constructor
62
 
         * This function does nothing...
63
 
         */
64
 
        public function __construct() {
65
 
 
66
 
                return true;
67
 
 
68
 
        } // constructor
69
 
 
70
 
        /**
71
 
         * install
72
 
         * This is a required plugin function. It inserts our preferences
73
 
         * into Ampache
74
 
         */
75
 
        public function install() {
76
 
 
77
 
                // Check and see if it's already installed (they've just hit refresh, those dorks)
78
 
                if (Preference::exists('lastfm_user')) { return false; }
79
 
 
80
 
                Preference::insert('lastfm_user','Last.FM Username','','25','string','plugins');
81
 
                Preference::insert('lastfm_md5_pass','Last.FM Password','','25','string','plugins');
82
 
                Preference::insert('lastfm_port','Last.FM Submit Port','','25','string','internal');
83
 
                Preference::insert('lastfm_host','Last.FM Submit Host','','25','string','internal');
84
 
                Preference::insert('lastfm_url','Last.FM Submit URL','','25','string','internal');
85
 
                Preference::insert('lastfm_challenge','Last.FM Submit Challenge','','25','string','internal');
86
 
 
87
 
                return true;
88
 
 
89
 
        } // install
90
 
 
91
 
        /**
92
 
         * uninstall
93
 
         * This is a required plugin function. It removes our preferences from
94
 
         * the database returning it to its original form
95
 
         */
96
 
        public function uninstall() {
97
 
 
98
 
                Preference::delete('lastfm_md5_pass');
99
 
                Preference::delete('lastfm_user');
100
 
                Preference::delete('lastfm_url');
101
 
                Preference::delete('lastfm_host');
102
 
                Preference::delete('lastfm_port');
103
 
                Preference::delete('lastfm_challenge');
104
 
 
105
 
        } // uninstall
106
 
 
107
 
        /**
108
 
         * upgrade
109
 
         * This is a recommended plugin function
110
 
         */
111
 
        public function upgrade() {
112
 
                $from_version = Plugin::get_plugin_version($this->name);
113
 
                if ($from_version < 4) {
114
 
                        Preference::rename('lastfm_pass', 'lastfm_md5_pass');
115
 
                }
116
 
                return true;
117
 
        } // upgrade
118
 
 
119
 
        /**
120
 
         * save_songplay
121
 
         * This takes care of queueing and then submitting the tracks.
122
 
         */
123
 
        public function save_songplay($song) {
124
 
 
125
 
                // Let's pull the last song submitted by this user
126
 
                $previous = Stats::get_last_song($this->user_id);
127
 
 
128
 
                $diff = time() - $previous['date'];
129
 
 
130
 
                // Make sure it wasn't within the last min
131
 
                if ($diff < 60) {
132
 
                        debug_event($this->name,'Last song played within ' . $diff . ' seconds, not recording stats','3');
133
 
                        return false;
134
 
                }
135
 
 
136
 
                if ($song->time < 30) {
137
 
                        debug_event($this->name,'Song less then 30 seconds not queueing','3');
138
 
                        return false;
139
 
                }
140
 
 
141
 
                // Make sure there's actually a username and password before we keep going
142
 
                if (!$this->username || !$this->password) {
143
 
                        debug_event($this->name,'Username or password missing','3');
144
 
                        return false;
145
 
                }
146
 
 
147
 
                // Create our scrobbler with everything this time and then queue it
148
 
                $scrobbler = new scrobbler($this->username,$this->password,$this->hostname,$this->port,$this->path,$this->challenge);
149
 
 
150
 
                // Check to see if the scrobbling works
151
 
                if (!$scrobbler->queue_track($song->f_artist_full,$song->f_album_full,$song->title,time(),$song->time,$song->track)) {
152
 
                        // Depending on the error we might need to do soemthing here
153
 
                        return false;
154
 
                }
155
 
 
156
 
                // Go ahead and submit it now
157
 
                if (!$scrobbler->submit_tracks()) {
158
 
                        debug_event($this->name,'Error Submit Failed: ' . $scrobbler->error_msg,'3');
159
 
                        if ($scrobbler->reset_handshake) {
160
 
                                debug_event($this->name,'Re-running Handshake due to error','3');
161
 
                                $this->set_handshake($this->user_id);
162
 
                                // Try try again
163
 
                                if ($scrobbler->submit_tracks()) {
164
 
                                        debug_event($this->name,'Submission Successful','5'); 
165
 
                                        return true;
166
 
                                }
167
 
                        }
168
 
                        return false;
169
 
                }
170
 
 
171
 
                debug_event($this->name,'Submission Successful','5');
172
 
 
173
 
                return true;
174
 
 
175
 
        } // submit
176
 
 
177
 
        /**
178
 
         * set_handshake
179
 
         * This runs a handshake and properly updates the preferences as needed.
180
 
         * It returns the data as an array so we don't have to requery the db.
181
 
         * This requires a userid so it knows whose crap to update.
182
 
         */
183
 
        public function set_handshake($user_id) {
184
 
 
185
 
                $scrobbler = new scrobbler($this->username,$this->password);
186
 
                $data = $scrobbler->handshake();
187
 
 
188
 
                if (!$data) {
189
 
                        debug_event($this->name,'Handshake Failed: ' . $scrobbler->error_msg,'3');
190
 
                        return false;
191
 
                }
192
 
 
193
 
                $this->hostname = $data['submit_host'];
194
 
                $this->port = $data['submit_port'];
195
 
                $this->path = $data['submit_url'];
196
 
                $this->challenge = $data['challenge'];
197
 
 
198
 
                // Update the preferences
199
 
                Preference::update('lastfm_port',$user_id,$data['submit_port']);
200
 
                Preference::update('lastfm_host',$user_id,$data['submit_host']);
201
 
                Preference::update('lastfm_url',$user_id,$data['submit_url']);
202
 
                Preference::update('lastfm_challenge',$user_id,$data['challenge']);
203
 
 
204
 
                return true;
205
 
 
206
 
        } // set_handshake
207
 
 
208
 
        /**
209
 
         * load
210
 
         * This loads up the data we need into this object, this stuff comes 
211
 
         * from the preferences.
212
 
         */
213
 
        public function load() {
214
 
 
215
 
                $GLOBALS['user']->set_preferences();
216
 
                $data = $GLOBALS['user']->prefs;
217
 
 
218
 
                if (strlen(trim($data['lastfm_user']))) {
219
 
                        $this->username = trim($data['lastfm_user']);
220
 
                }
221
 
                else {
222
 
                        debug_event($this->name,'No Username, not scrobbling','3');
223
 
                        return false;
224
 
                }
225
 
                if (strlen(trim($data['lastfm_md5_pass']))) {
226
 
                        $this->password = trim($data['lastfm_md5_pass']);
227
 
                }
228
 
                else {
229
 
                        debug_event($this->name,'No Password, not scrobbling','3');
230
 
                        return false;
231
 
                }
232
 
 
233
 
                $this->user_id = $GLOBALS['user']->id;
234
 
 
235
 
                // If we don't have the other stuff try to get it before giving up
236
 
                if (!$data['lastfm_host'] || !$data['lastfm_port'] || !$data['lastfm_url'] || !$data['lastfm_challenge']) {
237
 
                        debug_event($this->name,'Running Handshake, missing information','3');
238
 
                        if (!$this->set_handshake($this->user_id)) {
239
 
                                debug_event($this->name,'Handshake failed, you lose','3');
240
 
                                return false;
241
 
                        }
242
 
                }
243
 
                else {
244
 
                        $this->hostname = $data['lastfm_host'];
245
 
                        $this->port = $data['lastfm_port'];
246
 
                        $this->path = $data['lastfm_url'];
247
 
                        $this->challenge = $data['lastfm_challenge'];
248
 
                }
249
 
 
250
 
                return true;
251
 
 
252
 
        } // load
 
25
    public $name        ='Last.FM';
 
26
    public $description    ='Records your played songs to your Last.FM Account';
 
27
    public $url        ='';
 
28
    public $version        ='000004';
 
29
    public $min_ampache    ='360003';
 
30
    public $max_ampache    ='999999';
 
31
 
 
32
    // These are internal settings used by this class, run this->load to
 
33
    // fill them out
 
34
    private $username;
 
35
    private $password;
 
36
    private $hostname;
 
37
    private $port;
 
38
    private $path;
 
39
    private $challenge;
 
40
    private $user_id;
 
41
 
 
42
    /**
 
43
     * Constructor
 
44
     * This function does nothing...
 
45
     */
 
46
    public function __construct() {
 
47
 
 
48
        return true;
 
49
 
 
50
    } // constructor
 
51
 
 
52
    /**
 
53
     * install
 
54
     * This is a required plugin function. It inserts our preferences
 
55
     * into Ampache
 
56
     */
 
57
    public function install() {
 
58
 
 
59
        // Check and see if it's already installed (they've just hit refresh, those dorks)
 
60
        if (Preference::exists('lastfm_user')) { return false; }
 
61
 
 
62
        Preference::insert('lastfm_user','Last.FM Username','','25','string','plugins');
 
63
        Preference::insert('lastfm_md5_pass','Last.FM Password','','25','string','plugins');
 
64
        Preference::insert('lastfm_port','Last.FM Submit Port','','25','string','internal');
 
65
        Preference::insert('lastfm_host','Last.FM Submit Host','','25','string','internal');
 
66
        Preference::insert('lastfm_url','Last.FM Submit URL','','25','string','internal');
 
67
        Preference::insert('lastfm_challenge','Last.FM Submit Challenge','','25','string','internal');
 
68
 
 
69
        return true;
 
70
 
 
71
    } // install
 
72
 
 
73
    /**
 
74
     * uninstall
 
75
     * This is a required plugin function. It removes our preferences from
 
76
     * the database returning it to its original form
 
77
     */
 
78
    public function uninstall() {
 
79
 
 
80
        Preference::delete('lastfm_md5_pass');
 
81
        Preference::delete('lastfm_user');
 
82
        Preference::delete('lastfm_url');
 
83
        Preference::delete('lastfm_host');
 
84
        Preference::delete('lastfm_port');
 
85
        Preference::delete('lastfm_challenge');
 
86
 
 
87
    } // uninstall
 
88
 
 
89
    /**
 
90
     * upgrade
 
91
     * This is a recommended plugin function
 
92
     */
 
93
    public function upgrade() {
 
94
        $from_version = Plugin::get_plugin_version($this->name);
 
95
        if ($from_version < 4) {
 
96
            Preference::rename('lastfm_pass', 'lastfm_md5_pass');
 
97
        }
 
98
        return true;
 
99
    } // upgrade
 
100
 
 
101
    /**
 
102
     * save_songplay
 
103
     * This takes care of queueing and then submitting the tracks.
 
104
     */
 
105
    public function save_songplay($song) {
 
106
 
 
107
        // Let's pull the last song submitted by this user
 
108
        $previous = Stats::get_last_song($this->user_id);
 
109
 
 
110
        $diff = time() - $previous['date'];
 
111
 
 
112
        // Make sure it wasn't within the last min
 
113
        if ($diff < 60) {
 
114
            debug_event($this->name,'Last song played within ' . $diff . ' seconds, not recording stats','3');
 
115
            return false;
 
116
        }
 
117
 
 
118
        if ($song->time < 30) {
 
119
            debug_event($this->name,'Song less then 30 seconds not queueing','3');
 
120
            return false;
 
121
        }
 
122
 
 
123
        // Make sure there's actually a username and password before we keep going
 
124
        if (!$this->username || !$this->password) {
 
125
            debug_event($this->name,'Username or password missing','3');
 
126
            return false;
 
127
        }
 
128
 
 
129
        // Create our scrobbler with everything this time and then queue it
 
130
        $scrobbler = new scrobbler($this->username,$this->password,$this->hostname,$this->port,$this->path,$this->challenge);
 
131
 
 
132
        // Check to see if the scrobbling works
 
133
        if (!$scrobbler->queue_track($song->f_artist_full,$song->f_album_full,$song->title,time(),$song->time,$song->track)) {
 
134
            // Depending on the error we might need to do soemthing here
 
135
            return false;
 
136
        }
 
137
 
 
138
        // Go ahead and submit it now
 
139
        if (!$scrobbler->submit_tracks()) {
 
140
            debug_event($this->name,'Error Submit Failed: ' . $scrobbler->error_msg,'3');
 
141
            if ($scrobbler->reset_handshake) {
 
142
                debug_event($this->name,'Re-running Handshake due to error','3');
 
143
                $this->set_handshake($this->user_id);
 
144
                // Try try again
 
145
                if ($scrobbler->submit_tracks()) {
 
146
                    debug_event($this->name,'Submission Successful','5'); 
 
147
                    return true;
 
148
                }
 
149
            }
 
150
            return false;
 
151
        }
 
152
 
 
153
        debug_event($this->name,'Submission Successful','5');
 
154
 
 
155
        return true;
 
156
 
 
157
    } // submit
 
158
 
 
159
    /**
 
160
     * set_handshake
 
161
     * This runs a handshake and properly updates the preferences as needed.
 
162
     * It returns the data as an array so we don't have to requery the db.
 
163
     * This requires a userid so it knows whose crap to update.
 
164
     */
 
165
    public function set_handshake($user_id) {
 
166
 
 
167
        $scrobbler = new scrobbler($this->username,$this->password);
 
168
        $data = $scrobbler->handshake();
 
169
 
 
170
        if (!$data) {
 
171
            debug_event($this->name,'Handshake Failed: ' . $scrobbler->error_msg,'3');
 
172
            return false;
 
173
        }
 
174
 
 
175
        $this->hostname = $data['submit_host'];
 
176
        $this->port = $data['submit_port'];
 
177
        $this->path = $data['submit_url'];
 
178
        $this->challenge = $data['challenge'];
 
179
 
 
180
        // Update the preferences
 
181
        Preference::update('lastfm_port',$user_id,$data['submit_port']);
 
182
        Preference::update('lastfm_host',$user_id,$data['submit_host']);
 
183
        Preference::update('lastfm_url',$user_id,$data['submit_url']);
 
184
        Preference::update('lastfm_challenge',$user_id,$data['challenge']);
 
185
 
 
186
        return true;
 
187
 
 
188
    } // set_handshake
 
189
 
 
190
    /**
 
191
     * load
 
192
     * This loads up the data we need into this object, this stuff comes 
 
193
     * from the preferences.
 
194
     */
 
195
    public function load() {
 
196
 
 
197
        $GLOBALS['user']->set_preferences();
 
198
        $data = $GLOBALS['user']->prefs;
 
199
 
 
200
        if (strlen(trim($data['lastfm_user']))) {
 
201
            $this->username = trim($data['lastfm_user']);
 
202
        }
 
203
        else {
 
204
            debug_event($this->name,'No Username, not scrobbling','3');
 
205
            return false;
 
206
        }
 
207
        if (strlen(trim($data['lastfm_md5_pass']))) {
 
208
            $this->password = trim($data['lastfm_md5_pass']);
 
209
        }
 
210
        else {
 
211
            debug_event($this->name,'No Password, not scrobbling','3');
 
212
            return false;
 
213
        }
 
214
 
 
215
        $this->user_id = $GLOBALS['user']->id;
 
216
 
 
217
        // If we don't have the other stuff try to get it before giving up
 
218
        if (!$data['lastfm_host'] || !$data['lastfm_port'] || !$data['lastfm_url'] || !$data['lastfm_challenge']) {
 
219
            debug_event($this->name,'Running Handshake, missing information','3');
 
220
            if (!$this->set_handshake($this->user_id)) {
 
221
                debug_event($this->name,'Handshake failed, you lose','3');
 
222
                return false;
 
223
            }
 
224
        }
 
225
        else {
 
226
            $this->hostname = $data['lastfm_host'];
 
227
            $this->port = $data['lastfm_port'];
 
228
            $this->path = $data['lastfm_url'];
 
229
            $this->challenge = $data['lastfm_challenge'];
 
230
        }
 
231
 
 
232
        return true;
 
233
 
 
234
    } // load
253
235
 
254
236
} // end AmpacheLastfm
255
237
?>