1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* Feed helper class. |
4
|
|
|
* |
5
|
|
|
* $Id: feed.php 4152 2009-04-03 23:26:23Z ixmatus $ |
6
|
|
|
* |
7
|
|
|
* @package Core |
8
|
|
|
* @author Kohana Team |
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
10
|
|
|
* @license http://kohanaphp.com/license.html |
11
|
|
|
*/ |
12
|
|
|
class feed_Core |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Parses a remote feed into an array. |
17
|
|
|
* |
18
|
|
|
* @param string remote feed URL |
19
|
|
|
* @param integer item limit to fetch |
20
|
|
|
* @return array |
21
|
|
|
*/ |
22
|
|
|
public static function parse($feed, $limit = 0) |
23
|
|
|
{ |
24
|
|
|
// Check if SimpleXML is installed |
25
|
|
|
if (! function_exists('simplexml_load_file')) { |
26
|
|
|
throw new Kohana_User_Exception('Feed Error', 'SimpleXML must be installed!'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// Make limit an integer |
30
|
|
|
$limit = (int) $limit; |
31
|
|
|
|
32
|
|
|
// Disable error reporting while opening the feed |
33
|
|
|
$ER = error_reporting(0); |
|
|
|
|
34
|
|
|
|
35
|
|
|
// Allow loading by filename or raw XML string |
36
|
|
|
$load = (is_file($feed) or valid::url($feed)) ? 'simplexml_load_file' : 'simplexml_load_string'; |
37
|
|
|
|
38
|
|
|
// Load the feed |
39
|
|
|
$feed = $load($feed, 'SimpleXMLElement', LIBXML_NOCDATA); |
40
|
|
|
|
41
|
|
|
// Restore error reporting |
42
|
|
|
error_reporting($ER); |
43
|
|
|
|
44
|
|
|
// Feed could not be loaded |
45
|
|
|
if ($feed === false) { |
46
|
|
|
return array(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// Detect the feed type. RSS 1.0/2.0 and Atom 1.0 are supported. |
50
|
|
|
$feed = isset($feed->channel) ? $feed->xpath('//item') : $feed->entry; |
51
|
|
|
|
52
|
|
|
$i = 0; |
53
|
|
|
$items = array(); |
54
|
|
|
|
55
|
|
|
foreach ($feed as $item) { |
56
|
|
|
if ($limit > 0 and $i++ === $limit) { |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$items[] = (array) $item; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $items; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a feed from the given parameters. |
68
|
|
|
* |
69
|
|
|
* @param array feed information |
70
|
|
|
* @param array items to add to the feed |
71
|
|
|
* @param string define which format to use |
72
|
|
|
* @param string define which encoding to use |
73
|
|
|
* @return string |
|
|
|
|
74
|
|
|
*/ |
75
|
|
|
public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8') |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP'); |
78
|
|
|
|
79
|
|
|
$feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"><channel></channel></rss>'; |
80
|
|
|
$feed = simplexml_load_string($feed); |
81
|
|
|
|
82
|
|
View Code Duplication |
foreach ($info as $name => $value) { |
|
|
|
|
83
|
|
|
if (($name === 'pubDate' or $name === 'lastBuildDate') and (is_int($value) or ctype_digit($value))) { |
84
|
|
|
// Convert timestamps to RFC 822 formatted dates |
85
|
|
|
$value = date(DATE_RFC822, $value); |
86
|
|
|
} elseif (($name === 'link' or $name === 'docs') and strpos($value, '://') === false) { |
87
|
|
|
// Convert URIs to URLs |
88
|
|
|
$value = url::site($value, 'http'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Add the info to the channel |
92
|
|
|
$feed->channel->addChild($name, $value); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
foreach ($items as $item) { |
96
|
|
|
// Add the item to the channel |
97
|
|
|
$row = $feed->channel->addChild('item'); |
98
|
|
|
|
99
|
|
View Code Duplication |
foreach ($item as $name => $value) { |
|
|
|
|
100
|
|
|
if ($name === 'pubDate' and (is_int($value) or ctype_digit($value))) { |
101
|
|
|
// Convert timestamps to RFC 822 formatted dates |
102
|
|
|
$value = date(DATE_RFC822, $value); |
103
|
|
|
} elseif (($name === 'link' or $name === 'guid') and strpos($value, '://') === false) { |
104
|
|
|
// Convert URIs to URLs |
105
|
|
|
$value = url::site($value, 'http'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Add the info to the row |
109
|
|
|
$row->addChild($name, $value); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $feed->asXML(); |
114
|
|
|
} |
115
|
|
|
} // End feed |
116
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.