-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrssWidget.php
executable file
·150 lines (142 loc) · 5.83 KB
/
rssWidget.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
<?php
if (!isset($rssFeedURI)) { $rssFeedURI=""; }
if (!isset($rssTimeZone)) { $rssTimeZone='America/New_York'; }
if (!isset($rssOddColor)) { $rssOddColor='#f2f2f2'; }
if (!isset($rssEvenColor)) { $rssEvenColor='lightgray'; }
function getHashtags($string) {
unset($hashtags);
preg_match_all("/(#\w+)/u", $string, $matches);
if ($matches) {
$hashtagsArray = array_count_values($matches[0]);
$hashtags = array_keys($hashtagsArray);
}
return $hashtags;
}
?>
<style>
.rss-widget-list {
border-collapse: collapse;
width: 100%;
}
.rss-widget-list tr:nth-child(odd) {background-color: <?php echo $rssOddColor ?>;}
.rss-widget-list tr:nth-child(even) {background-color: <?php echo $rssEvenColor ?>;}
.rss-widget-list a {
text-decoration: none;
}
.rss-widget-list a:hover {
text-decoration: underline;
}
.rss-widget-list small {
font-size: smaller;
color: dimgray;
}
.rss-widget-cell {
text-align: left;
padding: 18px 10px 18px 10px;
border-bottom: 1px dotted dimgray;
}
.rss-widget-content {
margin-left: 6px;
margin-right: 6px;
}
.rss-widget-footer {
margin-top: 12px;
}
.rss-widget-footer a,span {
font-size: small;
}
</style>
<table class='rss-widget-list'>
<?php
if (isset($rssFeedURI) && $rssFeedURI != "") {
$content = file_get_contents($rssFeedURI);
$feed = new SimpleXMLElement($content);
//detect RSS or ATOM
if (isset($feed->channel) && isset($feed->channel->item)) {
//RSS
foreach($feed->channel->item as $entry) {
$tzDate = new DateTimeZone($rssTimeZone);
date_default_timezone_set($rssTimeZone);
$date = new DateTime($entry->published);
$date->setTimeZone($tzDate);
echo "<tr><td class='rss-widget-cell'>\n";
echo " <p><b><a href='" . $feed->channel->link . "'>@" . $entry->author . "</a></b> <small> ". $date->format('Y-m-d g:i:sa') . " ET</small></p>\n";
echo " <p class='rss-widget-content'>";
//Entry media
foreach ($entry->children('http://search.yahoo.com/mrss/') as $media) {
echo " <a href=\"" . $media->attributes()['url'] . "\">\n";
echo " <img src='" . $media->attributes()['url'] . "' style='float:left; margin-right: 8px; max-height: 100px'/>\n";
echo " </a>";
}
//Main Entry with hashtag lists moved to footer
$description = $entry->description;
$hashtags = getHashtags($description);
if (isset($hashtags) && $hashtags != "") {
$searchString = implode(" ", $hashtags);
//$description = str_replace($searchString, "", $description);
}
echo $description . "\n";
echo " </p>\n";
//Entry Footer
echo " <div class='rss-widget-footer'>\n";
echo " <span style='float:right; margin-right: 10px;'><a href='$entry->link' title='$entry->title'>Permalink</a>";
if (isset($hashtags) && !empty($hashtags)) {
echo " | Tags: ";
foreach($hashtags as $hashtag) {
$linkTag = str_replace("#", "", $hashtag);
echo "<a href='https://twitter.com/hashtag/$linkTag'>" . $hashtag . "</a> ";
}
}
echo " </span>\n";
echo " </div>\n";
echo "</td></tr>\n";
}
} else {
//ATOM
foreach($feed->entry as $entry) {
$tzDate = new DateTimeZone($rssTimeZone);
date_default_timezone_set($rssTimeZone);
$date = new DateTime($entry->published);
$date->setTimeZone($tzDate);
$hashTagRoot = $feed->author->uri;
$hashTagRoot = explode('/users/', $hashTagRoot)[0];
$hashTagRoot = $hashTagRoot . "/tag/";
echo "<tr><td class='rss-widget-cell'>\n";
echo " <p><b><a href='" . $feed->author->uri . "'>@" . $feed->author->name . "</a></b> <small> ". $date->format('Y-m-d g:i:sa') . " ET</small></p>\n";
echo " <p class='rss-widget-content'>";
//Entry media
foreach ($entry->link as $link) {
if ($link->attributes()['rel'] == "enclosure") {
echo " <a href=\"" . $link->attributes()['href'] . "\">\n";
echo " <img src='" . $link->attributes()['href'] . "' style='float:left; margin-right: 8px; max-height: 100px'/>\n";
echo " </a>";
}
}
//Main Entry with hashtag lists moved to footer
$description = $entry->content;
$hashtags = array();
foreach($entry->category as $category) {
array_push($hashtags, $category->attributes()['term']);
}
echo $description . "\n";
echo " </p>\n";
//Entry Footer
echo " <div class='rss-widget-footer'>\n";
echo " <span style='float:right; margin-right: 10px;'><a href='$entry->id' title='$entry->title'>Permalink</a>";
if (isset($hashtags) && !empty($hashtags)) {
echo " | Tags: ";
foreach($hashtags as $hashtag) {
$linkTag = str_replace("#", "", $hashtag);
echo "<a href='" . $hashTagRoot . $linkTag . "'>" . $hashtag . "</a> ";
}
}
echo " </span>\n";
echo " </div>\n";
echo "</td></tr>\n";
}
}
} else {
echo "<!-- No feed URI provided for RSS Widget. Set the variable rssFeedURI to enable this widget. -->";
}
?>
</table>