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

« back to all changes in this revision

Viewing changes to lib/class/ampache_rss.class.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
<?php
 
2
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
 
3
/**
 
4
 *
 
5
 * LICENSE: GNU General Public License, version 2 (GPLv2)
 
6
 * Copyright 2001 - 2013 Ampache.org
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; version 2
 
11
 * of the License.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 *
 
22
 */
 
23
 
 
24
/**
 
25
 * Ampache_RSS Class
 
26
 *
 
27
 */
 
28
class Ampache_RSS {
 
29
 
 
30
    private $type;
 
31
    public $data;
 
32
 
 
33
    /**
 
34
     * Constructor
 
35
     * This takes a flagged.id and then pulls in the information for said flag entry
 
36
     */
 
37
    public function __construct($type) {
 
38
 
 
39
        $this->type = self::validate_type($type);
 
40
 
 
41
    } // constructor
 
42
 
 
43
    /**
 
44
     * get_xml
 
45
     * This returns the xmldocument for the current rss type, it calls a sub function that gathers the data
 
46
     * and then uses the xmlDATA class to build the document
 
47
     */
 
48
    public function get_xml() {
 
49
 
 
50
        // Function call name
 
51
        $data_function = 'load_' . $this->type;
 
52
        $pub_date_function = 'pubdate_' . $this->type;
 
53
 
 
54
        $data = call_user_func(array('Ampache_RSS',$data_function));
 
55
        $pub_date = call_user_func(array('Ampache_RSS',$pub_date_function));
 
56
 
 
57
        XML_Data::set_type('rss');
 
58
        $xml_document = XML_Data::rss_feed($data,$this->get_title(),$this->get_description(),$pub_date);
 
59
 
 
60
        return $xml_document;
 
61
 
 
62
    } // get_xml
 
63
 
 
64
    /**
 
65
     * get_title
 
66
     * This returns the standardized title for the rss feed based on this->type
 
67
     */
 
68
    public function get_title() {
 
69
 
 
70
        $titles = array('now_playing' => T_('Now Playing'),
 
71
                'recently_played' => T_('Recently Played'),
 
72
                'latest_album' => T_('Newest Albums'),
 
73
                'latest_artist' => T_('Newest Artists'));
 
74
 
 
75
        return scrub_out(Config::get('site_title')) . ' - ' . $titles[$this->type];
 
76
 
 
77
    } // get_title
 
78
 
 
79
    /**
 
80
     * get_description
 
81
     * This returns the standardized description for the rss feed based on this->type
 
82
     */
 
83
    public function get_description() {
 
84
 
 
85
        //FIXME: For now don't do any kind of translating
 
86
        return 'Ampache RSS Feeds';
 
87
 
 
88
    } // get_description
 
89
 
 
90
    /**
 
91
     * validate_type
 
92
     * this returns a valid type for an rss feed, if the specified type is invalid it returns a default value
 
93
     */
 
94
    public static function validate_type($type) {
 
95
 
 
96
        $valid_types = array('now_playing','recently_played','latest_album','latest_artist','latest_song',
 
97
                'popular_song','popular_album','popular_artist');
 
98
 
 
99
        if (!in_array($type,$valid_types)) {
 
100
            return 'now_playing';
 
101
        }
 
102
 
 
103
        return $type;
 
104
 
 
105
    } // validate_type
 
106
 
 
107
    /**
 
108
      * get_display
 
109
     * This dumps out some html and an icon for the type of rss that we specify
 
110
     */
 
111
    public static function get_display($type='now_playing') {
 
112
 
 
113
        // Default to now playing
 
114
        $type = self::validate_type($type);
 
115
 
 
116
        $string = '<a href="' . Config::get('web_path') . '/rss.php?type=' . $type . '">' . UI::get_icon('feed', T_('RSS Feed')) . '</a>';
 
117
 
 
118
        return $string;
 
119
 
 
120
    } // get_display
 
121
 
 
122
    // type specific functions below, these are called semi-dynamically based on the current type //
 
123
 
 
124
    /**
 
125
     * load_now_playing
 
126
     * This loads in the now playing information. This is just the raw data with key=>value pairs that could be turned
 
127
     * into an xml document if we so wished
 
128
     */
 
129
    public static function load_now_playing() {
 
130
 
 
131
        $data = Stream::get_now_playing();
 
132
 
 
133
        $results = array();
 
134
        $format = Config::get('rss_format') ?: '%t - %a - %A';
 
135
        $string_map = array(
 
136
            '%t' => 'title',
 
137
            '%a' => 'artist',
 
138
            '%A' => 'album'
 
139
        );
 
140
        foreach ($data as $element) {
 
141
            $song = $element['media'];
 
142
            $client = $element['user'];
 
143
            $title = $format;
 
144
            $description = $format;
 
145
            foreach($string_map as $search => $replace) {
 
146
                $trep = 'f_' . $replace;
 
147
                $drep = 'f_' . $replace . '_full';
 
148
                $title = str_replace($search, $song->$trep, $title);
 
149
                $description = str_replace($search, $song->$drep, $description);
 
150
            }
 
151
            $xml_array = array(
 
152
                    'title' => $title,
 
153
                    'link' => $song->link,
 
154
                    'description' => $description,
 
155
                    'comments' => $client->fullname . ' - ' . $element['agent'],
 
156
                    'pubDate' => date('r', $element['expire'])
 
157
                    );
 
158
            $results[] = $xml_array;
 
159
        } // end foreach
 
160
 
 
161
        return $results;
 
162
 
 
163
    } // load_now_playing
 
164
 
 
165
    /**
 
166
     * pubdate_now_playing
 
167
     * this is the pub date we should use for the now playing information,
 
168
     * this is a little specific as it uses the 'newest' expire we can find
 
169
     */
 
170
    public static function pubdate_now_playing() {
 
171
 
 
172
        // Little redundent, should be fixed by an improvement in the get_now_playing stuff
 
173
        $data = Stream::get_now_playing();
 
174
 
 
175
        $element = array_shift($data);
 
176
 
 
177
        return $element['expire'];
 
178
 
 
179
    } // pubdate_now_playing
 
180
 
 
181
    /**
 
182
     * load_recently_played
 
183
     * This loads in the recently played information and formats it up real nice like
 
184
     */
 
185
    public static function load_recently_played() {
 
186
 
 
187
        //FIXME: The time stuff should be centralized, it's currently in two places, lame
 
188
 
 
189
        $time_unit = array('', T_('seconds ago'), T_('minutes ago'), T_('hours ago'), T_('days ago'), T_('weeks ago'), T_('months ago'), T_('years ago'));
 
190
        $data = Song::get_recently_played();
 
191
 
 
192
        $results = array();
 
193
 
 
194
        foreach ($data as $item) {
 
195
            $client = new User($item['user']);
 
196
            $song = new Song($item['object_id']);
 
197
            $song->format();
 
198
            $amount = intval(time() - $item['date']+2);
 
199
            $time_place = '0';
 
200
            while ($amount >= 1) {
 
201
                $final = $amount;
 
202
                $time_place++;
 
203
                if ($time_place <= 2) {
 
204
                    $amount = floor($amount/60);
 
205
                }
 
206
                if ($time_place == '3') {
 
207
                    $amount = floor($amount/24);
 
208
                }
 
209
                if ($time_place == '4') {
 
210
                    $amount = floor($amount/7);
 
211
                }
 
212
                if ($time_place == '5') {
 
213
                    $amount = floor($amount/4);
 
214
                }
 
215
                if ($time_place == '6') {
 
216
                    $amount = floor ($amount/12);
 
217
                }
 
218
                if ($time_place > '6') {
 
219
                    $final = $amount . '+';
 
220
                    break;
 
221
                }
 
222
            } // end while
 
223
 
 
224
            $time_string = $final . ' ' . $time_unit[$time_place];
 
225
 
 
226
            $xml_array = array('title'=>$song->f_title . ' - ' . $song->f_artist . ' - ' . $song->f_album,
 
227
                        'link'=>str_replace('&amp;', '&', $song->link),
 
228
                        'description'=>$song->title . ' - ' . $song->f_artist_full . ' - ' . $song->f_album_full . ' - ' . $time_string,
 
229
                        'comments'=>$client->username,
 
230
                        'pubDate'=>date("r",$item['date']));
 
231
            $results[] = $xml_array;
 
232
 
 
233
        } // end foreach
 
234
 
 
235
        return $results;
 
236
 
 
237
    } // load_recently_played
 
238
 
 
239
    /**
 
240
     * pubdate_recently_played
 
241
     * This just returns the 'newest' recently played entry
 
242
     */
 
243
    public static function pubdate_recently_played() {
 
244
 
 
245
        $data = Song::get_recently_played();
 
246
 
 
247
        $element = array_shift($data);
 
248
 
 
249
        return $element['date'];
 
250
 
 
251
    } // pubdate_recently_played
 
252
 
 
253
} // end Ampache_RSS class