/
rssblocks.plugin.php
160 lines (132 loc) · 3.95 KB
/
rssblocks.plugin.php
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
<?php
/**
*
*
* @version $Id$
* @copyright 2009
*/
class RSSBlock extends Plugin
{
public function action_plugin_activation( $file )
{
if(Plugins::id_from_file($file) == Plugins::id_from_file(__FILE__)) {
CronTab::add_hourly_cron('update_rrs_blocks', 'rssblocks_update');
}
}
public function filter_plugin_config( $actions )
{
$actions['update'] = _t('Update All Now');
return $actions;
}
public function action_plugin_ui( $plugin_id, $action ) {
switch($action) {
case 'update':
$result = $this->filter_rssblocks_update(false, true);
if($result) {
Session::notice('RSS Feeds Successfully Updated');
}
else {
Session::error('RSS Feeds Did Not Successfully Update');
}
break;
}
}
public function filter_rssblocks_update($success, $force = false)
{
EventLog::log('Running rrsblocks update');
$blocks = DB::get_results('SELECT b.* FROM {blocks} b WHERE b.type = ?', array('rssblock'), 'Block');
Plugins::act('get_blocks', $blocks);
$success = true;
foreach($blocks as $block) {
$cachename = array('rssblock', md5($block->feed_url));
if($force || Cache::expired($cachename)) {
$r = new RemoteRequest( $block->feed_url );
$r->set_timeout( 10 );
$r->execute();
$feed = $r->get_response_body();
try{
if(is_string($feed)) {
new SimpleXMLElement($feed); // This throws an exception if the feed isn't valid
Cache::set($cachename, $feed, 3600, true);
}
}
catch(Exception $e) {
$success = false;
}
}
}
Session::notice('ran rssblocks update');
return $success;
}
public function filter_block_list($block_list)
{
$block_list['rssblock'] = _t('RSS Block');
return $block_list;
}
public function action_block_content_rssblock($block)
{
$items = array();
$cachename = array('rssblock', md5($block->feed_url));
if(Cache::expired($cachename)) {
// CronTab::add_single_cron('single_update_rrs_blocks', 'rrsblocks_update', HabariDateTime::date_create());
}
$feed = Cache::get($cachename);
try {
$xml = new SimpleXMLElement($feed);
$dns = $xml->getDocNamespaces();
$itemcount = 0;
foreach($xml->channel->item as $xitem) {
$item = new StdClass();
foreach($xitem->children() as $child) {
$item->{$child->getName()} = (string) $child;
}
foreach($dns as $ns => $nsurl) {
foreach($xitem->children($nsurl) as $child) {
$item->{$ns . '__' . $child->getName()} = (string) $child;
foreach($child->attributes() as $name => $value) {
$item->{$ns . '__' . $child->getName() . '__' . $name} = (string) $value;
}
}
}
$items[] = $item;
$itemcount++;
if($block->item_limit > 0 && $itemcount >= $block->item_limit) {
break;
}
}
foreach($xml->item as $xitem) {
$item = new StdClass();
foreach($xitem->children() as $child) {
$item->{$child->getName()} = (string) $child;
}
foreach($dns as $ns => $nsurl) {
foreach($xitem->children($nsurl) as $child) {
$item->{$ns . '__' . $child->getName()} = (string) $child;
foreach($child->attributes() as $name => $value) {
$item->{$ns . '__' . $child->getName() . '__' . $name} = (string) $value;
}
}
}
$items[] = $item;
$itemcount++;
if($block->item_limit > 0 && $itemcount >= $block->item_limit) {
break;
}
}
}
catch(Exception $e) {
}
$block->items = $items;
$block->markup_id = Utils::slugify($block->title);
}
public function action_block_form_rssblock($form, $block)
{
$form->append( 'text', 'feed_url', $block, _t( 'RSS Feed URL:', 'rssblock' ) );
$form->append( 'text', 'item_limit', $block, _t( 'Item Limit:', 'rssblock' ) );
}
public function action_init()
{
$this->add_template('block.rssblock', dirname(__FILE__) . '/block.rssblock.php');
}
}
?>