Lifestream Script for Etomite CMS

Lifestream is a chronological aggregated view of your life activities online. It is only limited by the content and sources that you use to define it.

This snippet generates a lifestream with the Etomite CMS. You must have a working SimplePie “installation” for this snippet to work.

If you not use Etomite CMS, it will be easy to convert to a regular PHP script.

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
/*
 lifestream v1.0
 A Etomite script by bogge, bogge.com
 I take no responsibility for any thing, even if I'm to blame.
 
 "Installing"
 1. Download SimplePie (http://simplepie.org/downloads/), it´s tested with 1.1.3.
 2. Extract and Upload to webserver, upload everything. (http://simplepie.org/wiki/setup/setup)
 
 Usage
 [ [lifestream]] Shows lifestream
 [ [lifestream?limit=20m&cachelen=3600]] Shows lifestream fetching 20 posts from each feed and with 1h cache.
  I think you get it...
 
 Settings
*/
$cachelen = isset($cachelen)? $cachelen: "5400"; //Set the minimum time (in seconds) for which a feed will be cached.
$limit = isset($limit)? $limit: "10"; //Set the maximum number of items to return per feed
$path = isset($path)? $path: "/external/simplepie"; //path to simplepie folder, must not end with /, You may need to change this to match the location of simplepie.
$iscontentking = isset($iscontentking)? $iscontentking: "2"; //0=No (no content) 1=Yes (show content), 2=Yes (show descriptions)
$showfav = isset($showfav)? $showfav: "1"; //Show favicon? 1=Yes, 0=No
$addto = isset($addto)? $addto: "1"; //Show "add to" section? 1=Yes, 0=No
$subscribeto = isset($subscribeto)? $subscribeto: "1"; //Show "subscribe" section? 1=Yes, 0=No
$oldpost = mktime(0, 0, 0, date("n")-1, date("j"), date("Y")); // This sets that no post in lifestream is older than today -1 month.
 
/*
Feeds, add all feed that you would like to show in lifestream.
This sets an array of URLs that you want to parse.
If there is not a feed at this location, auto-discovery is used.
Tested with: Delicious, Wordpress blog, Twitter, Flickr, Last.fm, YouTube
Twitter needs more work read more http://simplepie.org/wiki/faq/problematic_feeds
*/
$feedurls = array(
	'http://feeds.delicious.com/v2/rss/bogge',
	'http://feedproxy.google.com/bogge/blog'
);
 
//No editing below
 
require_once($_SERVER['DOCUMENT_ROOT'].$path.'/simplepie.inc'); // Make sure SimplePie is included.
 
$feed = new SimplePie(); // Create a new SimplePie object
$feed->set_feed_url($feedurls); // Passing feeds
$feed->set_item_limit($limit); //Set the maximum number of items to return per feed with Multifeeds.
$feed->set_output_encoding('UTF-8'); //Allows you to override SimplePie's output to match that of your webpage.
$feed->set_cache_duration($cachelen); //Set the minimum time (in seconds) for which a feed will be cached.
$feed->init(); // Initialize the feed object. This is what makes everything happen.
$feed->handle_content_type(); // This method ensures that the SimplePie-enabled page is being served with the correct mime-type and character encoding HTTP headers (character encoding determined by the set_output_encoding() config option.
 
// If a SimplePie error was thrown, it will display it here.
if ($feed->error) {
	$output = '
 
'.$feed->error().'
 
';
}
 
function getClass($url)
{
	//This is a modified version that was orginaly written by http://github.com/trey.
	preg_match('/https{0,1}:\\/\\/([^\\/]*)\\/*.*/i', $url, $matches);
	$class = $matches[1];
	$class = preg_replace("/www\./", "", $class); // Remove `www.`.
	$class = preg_replace("/\.(com|org|net)/", "", $class); // Remove top level domains. Add more as you see fit.
	$class = preg_replace("/\./", "_", $class); // Replace `.`s with `_`s.
	return $class;
}
 
function doRelativeDate($posted_date) {
    /**
        This function returns either a relative date or a formatted date depending
        on the difference between the current datetime and the datetime passed.
            $posted_date should be in the following format: YYYYMMDDHHMMSS
 
        Relative dates look something like this:
            3 weeks, 4 days ago
        Formatted dates look like this:
            on 02/18/2004
 
        The function includes 'ago' or 'on' and assumes you'll properly add a word
        like 'Posted ' before the function output.
 
        By Garrett Murray, http://graveyard.maniacalrage.net/etc/relative/
    **/
    $in_seconds = strtotime(substr($posted_date,0,8).' '.
                  substr($posted_date,8,2).':'.
                  substr($posted_date,10,2).':'.
                  substr($posted_date,12,2));
    $diff = time()-$in_seconds;
    $months = floor($diff/2592000);
    $diff -= $months*2419200;
    $weeks = floor($diff/604800);
    $diff -= $weeks*604800;
    $days = floor($diff/86400);
    $diff -= $days*86400;
    $hours = floor($diff/3600);
    $diff -= $hours*3600;
    $minutes = floor($diff/60);
    $diff -= $minutes*60;
    $seconds = $diff;
 
    if ($months>0) {
        // over a month old, just show date (yyyy-mm-dd format)
        return 'on '.substr($posted_date,0,4).'-'.substr($posted_date,4,2).'-'.substr($posted_date,6,2);
    } else {
        if ($weeks>0) {
            // weeks and days
            $relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks>1?'s':'');
            $relative_date .= $days>0?($relative_date?', ':'').$days.' day'.($days>1?'s':''):'';
        } elseif ($days>0) {
            // days and hours
            $relative_date .= ($relative_date?', ':'').$days.' day'.($days>1?'s':'');
            $relative_date .= $hours>0?($relative_date?', ':'').$hours.' hour'.($hours>1?'s':''):'';
        } elseif ($hours>0) {
            // hours and minutes
            $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours>1?'s':'');
            $relative_date .= $minutes>0?($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':''):'';
        } elseif ($minutes>0) {
            // minutes only
            $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes>1?'s':'');
        } else {
            // seconds only
            $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds>1?'s':'');
        }
    }
    // show relative date and add proper verbiage
    return $relative_date.' ago';
}
 
$output .= '
<div id="lifestream">
<ul>'; //Start outputing
$stored_date = ''; //Make sure variable is empty
 
// Here, we'll loop through all of the items in the feed, and $item represents the current item in the loop.
foreach($feed-&gt;get_items() as $item) {
 
	//Check if item is older than the $oldpost, if older break and stop outputing.
	$currentdate = mktime(0, 0, 0, $item-&gt;get_date('n'), $item-&gt;get_date('j'), $item-&gt;get_date('Y'));
	if ($currentdate &lt; $oldpost) { 		break; 	}   	//Creates the date headers that is used to group items by date. 	$item_date = $item-&gt;get_date('F jS');
	if ($stored_date != $item_date) {
		$stored_date = $item_date;
		$output .= '</ul>
<h2>'.$stored_date.'</h2>
<ul>';
	}
 
	//Get information about the feed (not the item) and create a CSS class name. ex a feed from flickr.com gets class name "flickr" and the CSS sould me ".flickr".
	$feed = $item-&gt;get_feed();
	$class = getClass($feed-&gt;get_permalink());
	$output .= '
	<li class="'.$class.'">';
 
	if ($showfav == 1) {
		//Returns the favicon image for the feed's website. If there is no favicon an alternate is shown.
		if (!$favicon = $feed-&gt;get_favicon()) {
			$favicon = $path.'/demo/for_the_demo/favicons/alternate.png';
		}
		//The favicon is linked to the website and the item is linked to the item.
		$output .= '<a href="'.$feed-&gt;get_permalink().'"><img title="'.$feed-&gt;get_title().'" src="'.$favicon.'" alt="'.$feed-&gt;get_title().'" width="16" height="16" /></a>';
	}
 
	//Link entire item
	$output .= '<a href="'.$item-&gt;get_permalink().'">';
 
	//Format and output the item title
	if ($title = $item-&gt;get_title()) {
		$output .= '<strong>'.html_entity_decode($title, ENT_QUOTES, "UTF-8").'</strong>';
	}
 
	//check if Content Is King or should we output description.
	if ($iscontentking == 1) {
		$output .= ' — '.$item-&gt;get_content();
	}elseif ($iscontentking == 2) {
		$output .= ' — '.$item-&gt;get_description();
	}else {}
 
	//Create and output the relative date for the item. instead of showing "12/01/2009" it shows "2 weeks, 6 days ago". If date over a month old, just show date (yyyy-mm-dd format).
	$reldate = doRelativeDate($item-&gt;get_date("YmdHis"));
	$output .= '<span class="small"> — '.$reldate.'</span></a>';
 
	//All the Add to code.
	if ($addto == 1) {
		$output .= '
 
<strong>Add to:</strong> ';
		$output .= '<a href="' . $item-&gt;add_to_delicious() . '"><img title="Add to del.icio.us" src="'.$path.'/demo/for_the_demo/favicons/delicious.png" alt="Add to del.icio.us" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_digg() . '"><img title="Digg It!" src="'.$path.'/demo/for_the_demo/favicons/digg.png" alt="Digg It!" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_blinklist() . '"><img title="Add to Blinklist" src="'.$path.'/demo/for_the_demo/favicons/blinklist.png" alt="Add to Blinklist" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_blogmarks() . '"><img title="Add to Blogmarks" src="'.$path.'/demo/for_the_demo/favicons/blogmarks.png" alt="Add to Blogmarks" width="16" height="16" /></a>';
//		$output .= '<a href="' . $item-&gt;add_to_furl() . '"><img title="Add to Furl" src="'.$path.'/demo/for_the_demo/favicons/furl.png" alt="Add to Furl" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_magnolia() . '"><img title="Add to Ma.gnolia" src="'.$path.'/demo/for_the_demo/favicons/magnolia.png" alt="Add to Ma.gnolia" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_myweb20() . '"><img title="Add to My Web 2.0" src="'.$path.'/demo/for_the_demo/favicons/myweb2.png" alt="Add to My Web 2.0" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_newsvine() . '"><img title="Add to Newsvine" src="'.$path.'/demo/for_the_demo/favicons/newsvine.png" alt="Add to Newsvine" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_reddit() . '"><img title="Add to Reddit" src="'.$path.'/demo/for_the_demo/favicons/reddit.png" alt="Add to Reddit" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_simpy() . '"><img title="Add to Simpy" src="'.$path.'/demo/for_the_demo/favicons/simpy.png" alt="Add to Simpy" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_spurl() . '"><img title="Add to Spurl" src="'.$path.'/demo/for_the_demo/favicons/spurl.png" alt="Add to Spurl" width="16" height="16" /></a>';
		$output .= '<a href="' . $item-&gt;add_to_wists() . '"><img title="Add to Wists" src="'.$path.'/demo/for_the_demo/favicons/wists.png" alt="Add to Wists" width="16" height="16" /></a>';
		$output .= '
 
';
	}
 
	//All the Subscribe code and One-Click Subscriptions.
	if ($subscribeto == 1) {
		$output .= '
 
<strong>Subscribe to this feed:</strong> <a href="' . $feed-&gt;subscribe_url() . '"><img title="Subscribe to this feed" src="'.$path.'/demo/for_the_demo/feed.png" alt="Subscribe to this feed" width="16" height="16" /></a>';
		$output .= ' | <a href="'.$feed-&gt;subscribe_google().'">Subscribe in Google Reader</a>';
		$output .= '
 
';
	}
 
	$output .= '</li>
';
}
$output .= '</ul>
<span style="float:right; font-size:9px; color:#888;"><a title="Lifestream script coded by bogge" href="http://www.bogge.com/info/scripting-language/etomitecms/lifestream-script">Script</a> by <a title="You can find tv series information, scripts, services and torrents at Bogge.com" href="http://www.bogge.com/">Bogge</a></span></div>
';
 
return $output;
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
#lifestream {
font-size:12px;
width:620px;
margin:15px auto auto;
padding:5px;
}
 
#lifestream ul {
list-style-position:inside;
list-style-type:none;
margin-left:0;
padding-left:0;
}
 
#lifestream li {
margin-bottom:3px;
margin-left:0;
padding:5px;
}
 
#lifestream li a {
text-decoration:none;
color:#111;
}
 
#lifestream li img {
margin-right:5px;
border: 0;
vertical-align: middle;
}
 
#lifestream h2 {
font-family:Arial,'Trebuchet MS',Sans-Serif;
font-weight:700;
font-size:120%;
margin:10px 0;
color:#fff;
background-color:#000;
letter-spacing:0.5px;
padding:4px 0 4px 0;
text-align: center
}
 
.twitter {
background-color:#FF8000;
}
 
.flickr {
background-color:#f8d8e9;
}
 
.delicious {
background-color:#CCCCFF;
}
 
.last_fm {
background-color:#FFCC99;
}
 
.bogge {
background-color:#ECFDCE;
}
 
.youtube {
background-color:#FFAFAF;
}
 
.small {
font-size:10px;
color:#888;
}

0 Responses