basercms /baser/plugins/feed/models/rss.php

Language PHP Lines 321
MD5 Hash f1f318dc63211d84eb053a62109ecc3c
Repository https://github.com/hashing/basercms.git View Raw File
  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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
 * Created: Wed Sep 06 18:03:26 CEST 2006
 * 
 * This Model allows you to parse a given RSS 2.0 feed and have it returned in a big 
 * array.
 * 
 * PHP versions 5
 *
 * Copyright (c) Felix Geisendrfer <info@fg-webdesign.de>
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright		Copyright (c) 2006, Felix Geisendrfer. 
 * @link			http://www.fg-webdesign.de/
 * @link			http://www.thinkingphp.org/ 
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */
/**
 * Include files
 */
App::import("Model","Feed.WebModel",true,array(BASER_PLUGINS));
/**
 * rss
 * 
 * @package baser.plugins.feed.models
 */
class Rss extends WebModel {
/**
 * name
 * 
 * @var string 
 * @access public
 */
    var $name = 'Rss';
/**
 * cacheExpires
 * 
 * @var string 
 * @access public
 */
    var $cacheExpires = '+2 hours';
/**
 * cacheFolder
 * 
 * @var string 
 * @access public
 */
    var $cacheFolder = 'web/rss';
/**
 * useDbConfig
 * 
 * @var string
 * @access public
 */
    var $useDbConfig = null;
/**
 * findAll
 * 
 * @param string $feedUrl
 * @param int $limit
 * @param string $cacheExpires
 * @return array
 * @access public
 */  
	function findAll($feedUrl, $limit = 10, $cacheExpires = null)
    {
		
        if (empty($feedUrl))
            return array();

        $feed = $this->__parseRSS($this->__getRawRSS($feedUrl, null, $cacheExpires));       
        
        if (isset($feed['Error']))
            return $feed;
        
        if (count($feed['Items']>$limit))
        {                                
            $feed['Items'] = array_slice($feed['Items'], 0, $limit);
        }

        return $feed;
    }
/**
 * getRawRSS
 * 
 * @param string $feedUrl
 * @param array $vars
 * @param string $cacheExpires
 * @return string
 * @access private
 */    
	function __getRawRSS($feedUrl, $vars = array(), $cacheExpires = null)
    {
		
        $url = $feedUrl;
        
        $cachePath = $this->cacheFolder.$this->__createCacheHash('.rss', $url, $vars);

        if (empty($cacheExpires))
            $cacheExpires = $this->cacheExpires;
            
        if (empty($vars))
            $vars = array();
                    
        $rssData = cache($cachePath, null, $cacheExpires);
        
        if (empty($rssData))
        {
            $rssData = cache($cachePath, $this->httpGet($url, $vars));
        }
        
        return $rssData;
		
    }
    
 /**
 * A simple function for parsing RSS data. Only returns Items for now.
 *
 * @param string $data
 * @return array 
 * @access private
 */
    function __parseRSS($data)
    {
		
        if (empty($data))
            return array();
    
        $regex = '/\<rss.+version="(.+)".*\>/iUs';
        
        preg_match($regex, $data, $match);
        if (empty($match))
            return array('Error' => 'No valid feed (no feed version found).');
        
        list($raw, $version) = $match;
                
        if (empty($version))
            $version = '2.0';

        // Check if we have a valid version number
        if (!preg_match('/^[0-9.]+$/iUs', $version))
        {
            return array('Error' => '"'.$version.'" is no valid RSS version.');
        }
                    
        $rssFunction = '__parseRSS_'.str_replace('.', '_', $version);
                   
        if (method_exists($this, $rssFunction))
        {
            return call_user_func(array(&$this, $rssFunction), $data);
        }
        else 
        {
            return array('Error' => 'No function for parsing RSS feeds of version "'.$version.'" available.');
        }
		
    }
/**
 * parseRSS_2_0
 * 
 * @param string $data
 * @return array 
 * @access private
 */    
    function __parseRSS_2_0($data)
    {

        // First thing we need to do, is to identify all html/otherwise formated contents
        preg_match_all('/\<\!\[CDATA\[(.+)\]\]\>/iUs', $data, $cdata, PREG_SET_ORDER);


        // Create the md5 hash of the data to parse
        $dataHash = md5($data);
        
        // Now we have to replace them with something that won't confuse our parser, but still keep the array containing their original content
        // [[CDATA:$dataHash:$cdataNum]] should be pretty unique, so we don't have to deal with errors in an rss feed that talks about this replacment
        // method.
        foreach ($cdata as $cdataNum => $cdataItem)
        {
            $data = str_replace($cdataItem[0], '[[CDATA:'.$dataHash.':'.$cdataNum.']]', $data);
        }            
        
        
        // Let's get the information about the channel
        $regex = '/\<channel\>(.+)\<item\>/iUs';
        preg_match($regex, $data, $match);
        if (!empty($match))
        {
            list($raw, $channel) = $match;
            
            $channel = $this->__getNodeFields($channel, $cdata, $dataHash, 'channel');
        }
        else 
            $channel = array();
        
        // This will get us a list with all Items contained in the feed
        $regex = '/\<item\>(.+)\<\/item\>/iUs';
        
        $matchCount = preg_match_all($regex, $data, $matches, PREG_SET_ORDER);                
        if (empty($matchCount))
        {
            // No items? Nothing to parse.
            $matches = array();        
        }
        else 
        {                            
            $items = array();
            
            // Loop through all Item Matches
            foreach ($matches as $itemNr => $item)
            {   
                // Find all fields in our Item           
                
                $items[$itemNr] = $this->__getNodeFields($item[1], $cdata, $dataHash);
            }
        }
    
        // Return everything
        return array('Channel' => $channel,
                     'Items' => @$items);
		
    }    
/**
 * getXMLNodeAttributes
 * 
 * @param string $rawFields
 * @param string $cdata
 * @param string $dataHash
 * @param string $type
 * @return string 
 * @access private
 */    
    function __getNodeFields($rawFields, $cdata = null, $dataHash = null, $type = null)
    {
		
        // Don't ask - it works. No seriously, I spent a lot of time and thought on this regex
        // if you are interested in how it works feel free to contact me. In case you wonder about
        // the \x00's, that's an optimization trick to generate a character set that matches new lines
        // but doesn't require the /s modifier.
        $fieldRegex = '/\<(.+)( [^\x00]*)?\>([^\x00]*)\<\/\\1\>|\<(.+)( [^\x00]*)?\/\>/U';
            
        preg_match_all($fieldRegex, $rawFields, $fieldMatches, PREG_SET_ORDER);
        
        // Loop through those fields
        foreach ($fieldMatches as $fieldMatch)
        {
            // Assign the preg_match_all contents to a couple of variables
            if (count($fieldMatch)==4)
                list($raw, $field, $attributes, $value) = $fieldMatch;
            else 
            {
                // This is for <nodes ... /> that don't have enclosed content
                list($raw, , , ,$field, $attributes) = $fieldMatch;
                $value = null;
            }
             
            // The child image in channel has child elements in RSS, so let's make sure we parse them too
            if ($type=='channel' && $field=='image')
            {
                $value = $this->__getNodeFields($value, $cdata, $dataHash);
            }
            else 
            {
                // Find CDATA replaced stuff and but it back in.
                preg_match_all('/\[\[CDATA:'.$dataHash.':([0-9]+)\]\]/iUs', $rawFields, $cdataDummies, PREG_SET_ORDER);
                foreach ($cdataDummies as $cdataDummy)
                {
                    // Replace CDATA dummies with the actual contents of the cdata field
                    $value = str_replace($cdataDummy[0], $cdata[($cdataDummy[1])][1], $value);
                }
            }
            
            // Parse the attributes contained in our Node / ItemField
            $attributes = $this->__getXMLNodeAttributes($attributes);
        
            // Add our news Node to the list of Items.
            $fields[$field] = array('value' => $value,
                                    'attributes' => $attributes);
        }    
        
        if (!isset($fields))
            $fields = $rawFields;
        
        return $fields;
		
    }
/**
 * getXMLNodeAttributes
 * 
 * @param string $attributesData
 * @return array
 * @access private 
 */    
    function __getXMLNodeAttributes($attributesData)
    {
		
        if (empty($attributesData))
            return array();
    
        preg_match_all('/ ([^ \r\n]+)=(["\'])(.+)\\2/iUs', $attributesData, $attributeMatches, PREG_SET_ORDER);
        
        if (empty($attributeMatches))
            return array();
            
        $attributes = array();
        
        foreach ($attributeMatches as $attribute)
        {
            list($raw, $attributeKey, $enclosure, $attributeValue) = $attribute;
            $attributes[] = array($attributeKey => $attributeValue);
        }
        
        return $attributes;
		
    }
	
}

?>
Back to Top