3 * Generic RSS Widget Class
5 * Copyright (c) Xerox Corporation, Codendi Team, 2001-2009. All rights reserved
6 * Copyright 2012,2014, Franck Villaume - TrivialDev
7 * http://fusionforge.org
9 * This file is a part of Fusionforge.
11 * Fusionforge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Fusionforge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with Fusionforge. If not, see <http://www.gnu.org/licenses/>.
25 require_once 'Widget.class.php';
32 /* abstract */ class Widget_Rss extends Widget {
35 function __construct($id, $owner_id, $owner_type) {
36 parent::__construct($id);
37 $this->setOwner($owner_id, $owner_type);
40 $hp = Codendi_HTMLPurifier::instance();
41 return $this->rss_title ? $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) : _('RSS Reader');
43 function getContent() {
45 $hp = Codendi_HTMLPurifier::instance();
48 if (function_exists('idn_to_utf8()')) {
49 function idn_to_utf8($param) {
50 return idn_to_unicode($param);
53 if (!(include_once 'simplepie/simplepie.inc')) // vendor, debian
54 if (!(include_once 'php-simplepie/autoloader.php')) // fedora
55 exit_error(_('Could not load the SimplePie PHP library.'));
56 if (!is_dir(forge_get_config('data_path') .'/rss')) {
57 if (!mkdir(forge_get_config('data_path') .'/rss')) {
58 $content .= $HTML->error_msg(_('Cannot create backend directory. Contact forge administrator.'));
61 $rss = new SimplePie($this->rss_url, forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
63 $items = array_slice($rss->get_items(), 0, $max_items);
65 $content .= $HTML->listTableTop();
67 foreach($items as $key => $item){
68 $content .= '<tr '.$HTML->boxGetAltRowStyle($key).'><td width="99%">';
69 if ($image = $item->get_link(0, 'image')) {
70 //hack to display twitter avatar
71 $content .= '<img src="'. $hp->purify($image, CODENDI_PURIFIER_CONVERT_HTML) .'" style="float:left; margin-right:1em;" />';
73 /* Do not trust SimplePie for purifying. */
74 $content .= html_e('a', array(
75 'href' => util_unconvert_htmlspecialchars($item->get_link()),
76 ), util_html_secure($item->get_title()));
77 if ($item->get_date()) {
78 $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>';
80 $content .= '</td></tr>';
82 $content .= $HTML->listTableBottom();
84 $content = $HTML->information(_('No element to display'));
92 function hasPreferences() {
95 function getPreferences() {
96 $hp = Codendi_HTMLPurifier::instance();
100 $prefs .= _('Title')._(':');
103 $prefs .= '<input type="text" class="textfield_medium" name="rss[title]" value="'. $hp->purify($this->rss_title, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
106 $prefs .= 'URL'._(':');
109 $prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="'. $hp->purify($this->rss_url, CODENDI_PURIFIER_CONVERT_HTML) .'" /></td></tr>';
110 $prefs .= '</table>';
113 function getInstallPreferences() {
117 $prefs .= 'URL'._(':');
120 $prefs .= '<input type="url" class="textfield_medium" name="rss[url]" value="" placeholder="'._('Set your RSS URL here.').'" />';
123 $prefs .= '</table>';
126 function cloneContent($id, $owner_id, $owner_type) {
127 $sql = "INSERT INTO widget_rss (owner_id, owner_type, title, url)
128 SELECT $1, $2, title, url
130 WHERE owner_id = $3 AND owner_type = $4";
131 $res = db_query_params($sql,array($owner_id,$owner_type,$this->owner_id,$this->owner_type));
132 return db_insertid($res,'widget_rss','id');
134 function loadContent($id) {
135 $sql = "SELECT * FROM widget_rss WHERE owner_id = $1 AND owner_type = $2 AND id = $3";
136 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$id));
137 if ($res && db_numrows($res)) {
138 $data = db_fetch_array($res);
139 $this->rss_title = $data['title'];
140 $this->rss_url = $data['url'];
141 $this->content_id = $id;
144 function create(&$request) {
146 $vUrl = new Valid_String('url');
147 $vUrl->setErrorMessage(_("Cannot add empty RSS URL"));
149 if($request->validInArray('rss', $vUrl)) {
150 $rss = $request->get('rss');
151 $vTitle = new Valid_String('title');
153 if (!$request->validInArray('rss', $vTitle)) {
154 if (!(include_once 'simplepie/simplepie.inc')) // vendor, debian
155 if (!(include_once 'php-simplepie/autoloader.php')) // fedora
156 exit_error(_('Could not load the SimplePie PHP library.'));
157 if (!is_dir(forge_get_config('data_path') .'/rss')) {
158 mkdir(forge_get_config('data_path') .'/rss');
160 $rss_reader = new SimplePie($rss['url'], forge_get_config('data_path') .'/rss', null, forge_get_config('sys_proxy'));
162 $rss['title'] = $rss_reader->get_title();
167 $sql = 'INSERT INTO widget_rss (owner_id, owner_type, title, url) VALUES ($1,$2,$3,$4)';
168 $res = db_query_params($sql,array($this->owner_id,$this->owner_type,$rss['title'],$rss['url']));
169 $content_id = db_insertid($res, 'widget_rss', 'id');
173 function updatePreferences(&$request) {
175 $vContentId = new Valid_UInt('content_id');
176 $vContentId->required();
177 if (($rss = $request->get('rss')) && $request->valid($vContentId)) {
178 $vUrl = new Valid_String('url');
179 if($request->validInArray('rss', $vUrl)) {
185 $vTitle = new Valid_String('title');
186 if($request->validInArray('rss', $vTitle)) {
187 $title = $rss['title'] ;
192 if ($url || $title) {
193 $sql = "UPDATE widget_rss SET title=$1 , url=$2 WHERE owner_id =$3 AND owner_type = $4 AND id = $5";
194 db_query_params($sql,array($title,$url,$this->owner_id,$this->owner_type,(int)$request->get('content_id')));
200 function destroy($id) {
201 $sql = 'DELETE FROM widget_rss WHERE id = $1 AND owner_id = $2 AND owner_type = $3';
202 db_query_params($sql,array($id,$this->owner_id,$this->owner_type));
204 function isUnique() {
207 function _date_ago($from_time, $to_time) {
208 $distance_in_minutes = round((abs($to_time - $from_time))/60);
210 if ($distance_in_minutes <= 1) {
211 return ($distance_in_minutes == 0) ? _('less than 1 minute') : _('1 minute');
212 } elseif ($distance_in_minutes <= 44) {
213 return sprintf(_('%d minutes ago'), $distance_in_minutes);
214 } elseif ($distance_in_minutes <= 89) {
215 return _('About one hour') ;
216 } elseif ($distance_in_minutes <= 1439) {
217 return sprintf(_('about %s hours'), round($distance_in_minutes/60));
218 } elseif ($distance_in_minutes <= 2879) {
219 return _('About one day') ;
220 } elseif ($distance_in_minutes <= 43199) {
221 return sprintf(_('%s days ago'), round($distance_in_minutes/1440));
222 } elseif ($distance_in_minutes <= 86399) {
223 return _('About one month') ;
224 } elseif ($distance_in_minutes <= 525959) {
225 return sprintf(_('%s months ago'), round($distance_in_minutes/43200));
226 } elseif ($distance_in_minutes <= 1051919) {
227 return _('About one year') ;
229 return sprintf(_('over %s years'), round($distance_in_minutes/525960));