File: Widget_Rss.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (227 lines) | stat: -rw-r--r-- 8,126 bytes parent folder | download | duplicates (3)
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
<?php
/**
 * Generic RSS Widget Class
 *
 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
 * Copyright 2012, Franck Villaume - TrivialDev
 * http://fusionforge.org
 *
 * This file is a part of Fusionforge.
 *
 * Fusionforge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Fusionforge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
 */

require_once 'Widget.class.php';

/**
 * Widget_Rss
 *
 * Rss reader
 */
/* abstract */ class Widget_Rss extends Widget {
	var $rss_title;
	var $rss_url;
	function Widget_Rss($id, $owner_id, $owner_type) {
		$this->Widget($id);
		$this->setOwner($owner_id, $owner_type);
	}
	function getTitle() {
		$hp = Codendi_HTMLPurifier::instance();
		return $this->rss_title ?  $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML)  : _('RSS Reader');
	}
	function getContent() {
		$hp = Codendi_HTMLPurifier::instance();
		$content = '';
		if ($this->rss_url) {
			if (function_exists('idn_to_utf8()')) {
				function idn_to_utf8($param) {
					return idn_to_unicode($param);
				}
			}
			require_once 'common/rss/simplepie.inc';
			if (!is_dir(forge_get_config('data_path') .'/rss')) {
				if (!mkdir(forge_get_config('data_path') .'/rss')) {
					$content .= '<p class="error_msg" >'._('Cannot create backend directory. Contact forge administrator.').'</p>';
				}
			}
			$rss = new SimplePie($this->rss_url, forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
			$max_items = 10;
			$items = array_slice($rss->get_items(), 0, $max_items);
			$content .= '<table class="fullwidth">';
			$i = 0;
			foreach($items as $item){
				$i=$i+1;

				$content .= '<tr '.$GLOBALS['HTML']->boxGetAltRowStyle($i).'><td WIDTH="99%">';
				if ($image = $item->get_link(0, 'image')) {
					//hack to display twitter avatar
					$content .= '<img src="'.  $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML)  .'" style="float:left; margin-right:1em;" />';
				}
				/* Do not trust SimplePie for purifying. */
				$content .= html_e('a', array(
					'href' => util_unconvert_htmlspecialchars($item->get_link()),
				    ), util_html_secure($item->get_title()));
				if ($item->get_date()) {
					$content .= '<span style="color:#999;" title="'. date(_("Y-m-d H:i"), $item->get_date('U')) .'"> - '. $this->_date_ago($item->get_date('U'),time()) .'</span>';
				}
				$content .= '</td></tr>';
			}
			$content .= '</table>';
		}
		return $content;
	}
	function isAjax() {
		return true;
	}
	function hasPreferences() {
		return true;
	}
	function getPreferences() {
		$hp = Codendi_HTMLPurifier::instance();
		$prefs = '<table>';
		$prefs .= '<tr>';
		$prefs .= '<td>';
		$prefs .= _('Title')._(':');
		$prefs .= '</td>';
		$prefs .= '<td>';
		$prefs .= '<input type="text" class="textfield_medium" name="rss[title]" value="'. $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
		$prefs .= '<tr>';
		$prefs .= '<td>';
		$prefs .= 'URL'._(':');
		$prefs .= '</td>';
		$prefs .= '<td>';
		$prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="'. $hp->purify($this->rss_url, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
		$prefs .= '</table>';
		return $prefs;
	}
	function getInstallPreferences() {
		$prefs = '<table>';
		$prefs .= '<tr>';
		$prefs .= '<td>';
		$prefs .= 'URL'._(':');
		$prefs .= '</td>';
		$prefs .= '<td>';
		$prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="http://search.twitter.com/search.atom?q=alcatel-lucent&amp;show_user=1" />';
		$prefs .= '</td>';
		$prefs .= '</tr>';
		$prefs .= '</table>';
		return $prefs;
	}
	function cloneContent($id, $owner_id, $owner_type) {
		$sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
			SELECT $1, $2, title, url
			FROM widget_rss
			WHERE owner_id = $3 AND owner_type = $4";
		$res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
		return db_insertid($res,'widget_rss','id');
	}
	function loadContent($id) {
		$sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
		$res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
		if ($res && db_numrows($res)) {
			$data = db_fetch_array($res);
			$this->rss_title = $data['title'];
			$this->rss_url   = $data['url'];
			$this->content_id = $id;
		}
	}
	function create(&$request) {
		$content_id = false;
		$vUrl = new Valid_String('url');
		$vUrl->setErrorMessage(_("Cannot add empty RSS URL"));
		$vUrl->required();
		if($request->validInArray('rss', $vUrl)) {
			$rss = $request->get('rss');
			$vTitle = new Valid_String('title');
			$vTitle->required();
			if (!$request->validInArray('rss', $vTitle)) {
				if (function_exists('idn_to_utf8()')) {
					require_once 'simplepie/simplepie.inc';
				}
				else {
					require_once 'common/rss/simplepie.inc';
				}
				if (!is_dir(forge_get_config('data_path') .'/rss')) {
					mkdir(forge_get_config('data_path') .'/rss');
				}
				$rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
				$rss['title'] = $rss_reader->get_title();
			}
			$sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
			$res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
			$content_id = db_insertid($res, 'widget_rss', 'id');
		}
		return $content_id;
	}
	function updatePreferences(&$request) {
		$done = false;
		$vContentId = new Valid_UInt('content_id');
		$vContentId->required();
		if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
			$vUrl = new Valid_String('url');
			if($request->validInArray('rss', $vUrl)) {
				$url =  $rss['url'] ;
			} else {
				$url = '';
			}

			$vTitle = new Valid_String('title');
			if($request->validInArray('rss', $vTitle)) {
				$title =  $rss['title'] ;
			} else {
				$title = '';
			}

			if ($url || $title) {
				$sql = "UPDATE widget_rss SET title=$1 , url=$2  WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
				$res = db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
				$done = true;
			}
		}
		return $done;
	}
	function destroy($id) {
		$sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
		db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
	}
	function isUnique() {
		return false;
	}
	function _date_ago($from_time, $to_time, $include_seconds = false) {
		$distance_in_minutes = round((abs($to_time - $from_time))/60);
		$distance_in_seconds = round(abs($to_time - $from_time));

		if ($distance_in_minutes <= 1) {
			return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
		} elseif ($distance_in_minutes <= 44) {
			return vsprintf(_('%s minutes ago'), $distance_in_minutes);
		} elseif ($distance_in_minutes <= 89) {
			return _('About one hour') ;
		} elseif ($distance_in_minutes <= 1439) {
			return vsprintf(_('about %s hours'), round($distance_in_minutes/60));
		} elseif ($distance_in_minutes <= 2879) {
			return _('About one day') ;
		} elseif ($distance_in_minutes <= 43199) {
			return vsprintf(_('%s days ago'), round($distance_in_minutes/1440));
		} elseif ($distance_in_minutes <= 86399) {
			return _('About one month') ;
		} elseif ($distance_in_minutes <= 525959) {
			return vsprintf(_('%s months ago'), round($distance_in_minutes/43200));
		} elseif ($distance_in_minutes <= 1051919) {
			return _('About one year') ;
		} else {
			return vsprintf(_('over %s years'), round($distance_in_minutes/525960));
		}
	}
}