Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// vim: set expandtab tabstop=4 shiftwidth=4 fdm=marker:
3
 
4
/**
5
 * XML RSS parser
6
 *
7
 * PHP version 4
8
 *
9
 * @category XML
10
 * @package  XML_RSS
11
 * @author   Martin Jansen <mj@php.net>
12
 * @license  PHP License http://php.net/license
13
 * @version  SVN: $Id: RSS.php 310689 2011-05-01 17:54:48Z kguest $
14
 * @link     http://pear.php.net/package/XML_RSS
15
 */
16
 
17
require_once 'XML/Parser.php';
18
 
19
/**
20
 * RSS parser class.
21
 *
22
 * This class is a parser for Resource Description Framework (RDF) Site
23
 * Summary (RSS) documents. For more information on RSS see the
24
 * website of the RSS working group (http://www.purl.org/rss/).
25
 *
26
 * @category XML
27
 * @package  XML_RSS
28
 * @author   Martin Jansen <mj@php.net>
29
 * @license  PHP License http://php.net/license
30
 * @link     http://pear.php.net/package/XML_RSS
31
 */
32
class XML_RSS extends XML_Parser
33
{
34
    // {{{ properties
35
 
36
    /**
37
     * @var string
38
     */
39
    var $insideTag = '';
40
 
41
    /**
42
     * @var array
43
     */
44
    var $insideTagStack = array();
45
 
46
    /**
47
     * @var string
48
     */
49
    var $activeTag = '';
50
 
51
    /**
52
     * @var array
53
     */
54
    var $channel = array();
55
 
56
    /**
57
     * @var array
58
     */
59
    var $items = array();
60
 
61
    /**
62
     * @var array
63
     */
64
    var $item = array();
65
 
66
    /**
67
     * @var array
68
     */
69
    var $image = array();
70
 
71
    /**
72
     * @var array
73
     */
74
    var $textinput = array();
75
 
76
    /**
77
     * @var array
78
     */
79
    var $textinputs = array();
80
 
81
    /**
82
     * @var array
83
     */
84
    var $attribs;
85
 
86
    /**
87
     * @var array
88
     */
89
    var $parentTags = array('CHANNEL', 'ITEM', 'IMAGE', 'TEXTINPUT');
90
 
91
    /**
92
     * @var array
93
     */
94
    var $channelTags = array(
95
        'TITLE', 'LINK', 'DESCRIPTION', 'IMAGE',
96
        'ITEMS', 'TEXTINPUT', 'LANGUAGE', 'COPYRIGHT',
97
        'MANAGINGEditor', 'WEBMASTER', 'PUBDATE', 'LASTBUILDDATE',
98
        'CATEGORY', 'GENERATOR', 'DOCS', 'CLOUD', 'TTL',
99
        'RATING'
100
    );
101
 
102
    /**
103
     * @var array
104
     */
105
    var $itemTags = array(
106
        'TITLE', 'LINK', 'DESCRIPTION', 'PUBDATE', 'AUTHOR', 'CATEGORY',
107
        'COMMENTS', 'ENCLOSURE', 'GUID', 'SOURCE',
108
        'CONTENT:ENCODED'
109
    );
110
 
111
    /**
112
     * @var array
113
     */
114
    var $imageTags = array('TITLE', 'URL', 'LINK', 'WIDTH', 'HEIGHT');
115
 
116
 
117
    var $textinputTags = array('TITLE', 'DESCRIPTION', 'NAME', 'LINK');
118
 
119
    /**
120
     * List of allowed module tags
121
     *
122
     * Currently supported:
123
     *
124
     *   Dublin Core Metadata
125
     *   blogChannel RSS module
126
     *   CreativeCommons
127
     *   Content
128
     *   Syndication
129
     *   Trackback
130
     *   GeoCoding
131
     *   Media
132
     *   iTunes
133
     *
134
     * @var array
135
     */
136
    var $moduleTags = array(
137
        'DC:TITLE', 'DC:CREATOR', 'DC:SUBJECT', 'DC:DESCRIPTION',
138
        'DC:PUBLISHER', 'DC:CONTRIBUTOR', 'DC:DATE', 'DC:TYPE',
139
        'DC:FORMAT', 'DC:IDENTIFIER', 'DC:SOURCE', 'DC:LANGUAGE',
140
        'DC:RELATION', 'DC:COVERAGE', 'DC:RIGHTS',
141
        'BLOGCHANNEL:BLOGROLL', 'BLOGCHANNEL:MYSUBSCRIPTIONS',
142
        'BLOGCHANNEL:BLINK', 'BLOGCHANNEL:CHANGES',
143
        'CREATIVECOMMONS:LICENSE', 'CC:LICENSE', 'CONTENT:ENCODED',
144
        'SY:UPDATEPERIOD', 'SY:UPDATEFREQUENCY', 'SY:UPDATEBASE',
145
        'TRACKBACK:PING', 'GEO:LAT', 'GEO:LONG',
146
        'MEDIA:GROUP', 'MEDIA:CONTENT', 'MEDIA:ADULT',
147
        'MEDIA:RATING', 'MEDIA:TITLE', 'MEDIA:DESCRIPTION',
148
        'MEDIA:KEYWORDS', 'MEDIA:THUMBNAIL', 'MEDIA:CATEGORY',
149
        'MEDIA:HASH', 'MEDIA:PLAYER', 'MEDIA:CREDIT',
150
        'MEDIA:COPYRIGHT', 'MEDIA:TEXT', 'MEDIA:RESTRICTION',
151
        'ITUNES:AUTHOR', 'ITUNES:BLOCK', 'ITUNES:CATEGORY',
152
        'ITUNES:DURATION', 'ITUNES:EXPLICIT', 'ITUNES:IMAGE',
153
        'ITUNES:KEYWORDS', 'ITUNES:NEW-FEED-URL', 'ITUNES:OWNER',
154
        'ITUNES:PUBDATE', 'ITUNES:SUBTITLE', 'ITUNES:SUMMARY'
155
    );
156
 
157
    /**
158
     * @var array
159
     */
160
    var $last = array();
161
 
162
    // }}}
163
    // {{{ Constructor
164
 
165
    /**
166
     * Constructor
167
     *
168
     * @param mixed  $handle File pointer, name of the RSS file, or an RSS string.
169
     * @param string $srcenc Source charset encoding, use null (default)
170
     *                       to use default encoding (ISO-8859-1)
171
     * @param string $tgtenc Target charset encoding, use null (default)
172
     *                       to use default encoding (ISO-8859-1)
173
     *
174
     * @return void
175
     * @access public
176
     */
177
    function XML_RSS($handle = '', $srcenc = null, $tgtenc = null)
178
    {
179
        if ($srcenc === null && $tgtenc === null) {
180
            $this->XML_Parser();
181
        } else {
182
            $this->XML_Parser($srcenc, 'event', $tgtenc);
183
        }
184
 
185
        $this->setInput($handle);
186
 
187
        if ($handle == '') {
188
            $this->raiseError('No input passed.');
189
        }
190
    }
191
 
192
    // }}}
193
    // {{{ startHandler()
194
 
195
    /**
196
     * Start element handler for XML parser
197
     *
198
     * @param object $parser  XML parser object
199
     * @param string $element XML element
200
     * @param array  $attribs Attributes of XML tag
201
     *
202
     * @return void
203
     * @access private
204
     */
205
    function startHandler($parser, $element, $attribs)
206
    {
207
        if (substr($element, 0, 4) == "RSS:") {
208
            $element = substr($element, 4);
209
        }
210
 
211
        switch ($element) {
212
        case 'CHANNEL':
213
        case 'ITEM':
214
        case 'IMAGE':
215
        case 'TEXTINPUT':
216
            $this->insideTag = $element;
217
            array_push($this->insideTagStack, $element);
218
            break;
219
 
220
        case 'ENCLOSURE' :
221
            $this->attribs = $attribs;
222
            break;
223
 
224
        default:
225
            $this->activeTag = $element;
226
        }
227
    }
228
 
229
    // }}}
230
    // {{{ endHandler()
231
 
232
    /**
233
     * End element handler for XML parser
234
     *
235
     * If the end of <item>, <channel>, <image> or <textinput>
236
     * is reached, this method updates the structure array
237
     * $this->struct[] and adds the field "type" to this array,
238
     * that defines the type of the current field.
239
     *
240
     * @param object $parser  XML parser object
241
     * @param string $element Name of element that ends
242
     *
243
     * @return void
244
     * @access private
245
     */
246
    function endHandler($parser, $element)
247
    {
248
        if (substr($element, 0, 4) == "RSS:") {
249
            $element = substr($element, 4);
250
        }
251
 
252
        if ($element == $this->insideTag) {
253
            array_pop($this->insideTagStack);
254
            $this->insideTag = end($this->insideTagStack);
255
 
256
            $this->struct[] = array_merge(
257
                array('type' => strtolower($element)),
258
                $this->last
259
            );
260
        }
261
 
262
        if ($element == 'ITEM') {
263
            $this->items[] = $this->item;
264
            $this->item = '';
265
        }
266
 
267
        if ($element == 'IMAGE') {
268
            $this->images[] = $this->image;
269
            $this->image = '';
270
        }
271
 
272
        if ($element == 'TEXTINPUT') {
273
            $this->textinputs = $this->textinput;
274
            $this->textinput = '';
275
        }
276
 
277
        if ($element == 'ENCLOSURE') {
278
            if (!isset($this->item['enclosures'])) {
279
                $this->item['enclosures'] = array();
280
            }
281
 
282
            $this->item['enclosures'][] = array_change_key_case(
283
                $this->attribs, CASE_LOWER
284
            );
285
            $this->attribs = array();
286
        }
287
 
288
        $this->activeTag = '';
289
    }
290
 
291
    // }}}
292
    // {{{ cdataHandler()
293
 
294
    /**
295
     * Handler for character data
296
     *
297
     * @param object $parser XML parser object
298
     * @param string $cdata  CDATA
299
     *
300
     * @return void
301
     * @access private
302
     */
303
    function cdataHandler($parser, $cdata)
304
    {
305
        if (in_array($this->insideTag, $this->parentTags)) {
306
            $tagName = strtolower($this->insideTag);
307
            $var = $this->{$tagName . 'Tags'};
308
 
309
            if (in_array($this->activeTag, $var)
310
                || in_array($this->activeTag, $this->moduleTags)
311
            ) {
312
                $this->_add(
313
                    $tagName, strtolower($this->activeTag),
314
                    $cdata
315
                );
316
            }
317
 
318
        }
319
    }
320
 
321
    // }}}
322
    // {{{ defaultHandler()
323
 
324
    /**
325
     * Default handler for XML parser
326
     *
327
     * @param object $parser XML parser object
328
     * @param string $cdata  CDATA
329
     *
330
     * @return void
331
     * @access private
332
     */
333
    function defaultHandler($parser, $cdata)
334
    {
335
        return;
336
    }
337
 
338
    // }}}
339
    // {{{ _add()
340
 
341
    /**
342
     * Add element to internal result sets
343
     *
344
     * @param string $type  Name of the result set
345
     * @param string $field Fieldname
346
     * @param string $value Value
347
     *
348
     * @return void
349
     * @access private
350
     * @see    cdataHandler
351
     */
352
    function _add($type, $field, $value)
353
    {
354
        if ($field == 'category') {
355
            $this->{$type}[$field][] = $value;
356
        } else if (empty($this->{$type}) || empty($this->{$type}[$field])) {
357
            $this->{$type}[$field] = $value;
358
        } else {
359
            $this->{$type}[$field] .= $value;
360
        }
361
 
362
        $this->last = $this->{$type};
363
    }
364
 
365
    // }}}
366
    // {{{ getStructure()
367
 
368
    /**
369
     * Get complete structure of RSS file
370
     *
371
     * @return array
372
     * @access public
373
     */
374
    function getStructure()
375
    {
376
        return (array)$this->struct;
377
    }
378
 
379
    // }}}
380
    // {{{ getchannelInfo()
381
 
382
    /**
383
     * Get general information about current channel
384
     *
385
     * This method returns an array containing the information
386
     * that has been extracted from the <channel>-tag while parsing
387
     * the RSS file.
388
     *
389
     * @return array
390
     * @access public
391
     */
392
    function getChannelInfo()
393
    {
394
        return (array)$this->channel;
395
    }
396
 
397
    // }}}
398
    // {{{ getItems()
399
 
400
    /**
401
     * Get items from RSS file
402
     *
403
     * This method returns an array containing the set of items
404
     * that are provided by the RSS file.
405
     *
406
     * @return array
407
     * @access public
408
     */
409
    function getItems()
410
    {
411
        return (array)$this->items;
412
    }
413
 
414
    // }}}
415
    // {{{ getImages()
416
 
417
    /**
418
     * Get images from RSS file
419
     *
420
     * This method returns an array containing the set of images
421
     * that are provided by the RSS file.
422
     *
423
     * @return array
424
     * @access public
425
     */
426
    function getImages()
427
    {
428
        return (array)$this->images;
429
    }
430
 
431
    // }}}
432
    // {{{ getTextinputs()
433
 
434
    /**
435
     * Get text input fields from RSS file
436
     *
437
     * @return array
438
     * @access public
439
     */
440
    function getTextinputs()
441
    {
442
        return (array)$this->textinputs;
443
    }
444
 
445
    // }}}
446
 
447
}
448
?>