/
newswire.class.php
346 lines (315 loc) · 19.7 KB
/
newswire.class.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
class newswire {
var $db;
var $templateObj;
function newswire(&$db=NULL) {
if (is_null($db)) {
require_once('db.class.php');
$this->db=new cloudDatabase();
} else
$this->db=$db;
}
function getWireStory($id=0) {
$result=$this->db->query("SELECT * FROM Newswire WHERE id=$id;");
$info=$this->db->readQ($result);
return $info;
}
function getWebpage($id=0) {
$result=$this->db->query("SELECT url FROM Newswire WHERE id=$id;");
$info=$this->db->readQ($result);
return $info->url;
}
function add($wire) {
// check for duplicate
$chkDup=$this->db->queryC("SELECT id FROM Newswire WHERE url='$wire->url' AND feedType='$wire->feedType';");
if (!$chkDup) {
// insert the story into the table
$this_query=$this->db->insert("Newswire","title,caption,source,url,date,wireid,feedType,feedid","'$wire->title','$wire->caption','$wire->source','$wire->url','$wire->date',$wire->wireid,'$wire->feedType',$wire->feedid");
$newId=$this->db->getId();
return $newId;
} else
return false;
}
function createTempContent($userinfo=NULL,$wireid=0) {
require_once(PATH_CORE.'/classes/content.class.php');
$cObj=new content($this->db);
$info=$this->getWireStory($wireid);
require_once(PATH_CORE.'/classes/parseStory.class.php');
$psObj = new parseStory();
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$info->title=stripslashes($info->title);
$info->caption=stripslashes($this->utilObj->shorten($info->caption));
// to do - replace proxy feed urls with final redirect
$info->title=$psObj->cleanTitle($info->title);
// create permalink
$info->permalink=$cObj->buildPermalink($info->title);
// serialize the content
$info->title=mysql_real_escape_string($info->title);
$info->caption=mysql_real_escape_string($info->caption);
$story=$cObj->serialize(0,$info->title,$info->caption,$info->source,$info->url,$info->permalink,$userinfo->ncUid,$userinfo->name,$userinfo->userid,'',$userinfo->votePower,0,0);
// post wire story to content
$siteContentId=$cObj->add($story);
return $siteContentId;
}
function serialize($title='',$caption='',$source='',$url='',$date='',$wireid=0,$feedType='wire',$feedid=0) {
// creates an object for an action
$data= new stdClass;
$data->title=mysql_real_escape_string($title);
$data->caption=mysql_real_escape_string($caption);
$data->source=$source;
$data->url=mysql_real_escape_string($url);
$data->date=$date;
$data->wireid=$wireid; // deprecated
$data->feedType=$feedType;
$data->feedid=$feedid;
return $data;
}
function fetchBreakingNewswire($limit=7) {
$storyList=$this->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Newswire WHERE feedType='wire' AND date<now() AND date > date_sub(NOW(), INTERVAL 14 DAY) ORDER BY date DESC LIMIT $limit;");
return $storyList;
}
function cleanup($numberDays=15) {
if ($numberDays>0) {
// delete stories in FeedStories older than numberDays days
$deleteQuery=$this->db->query("DELETE FROM Newswire WHERE date < date_sub(NOW(), INTERVAL $numberDays DAY);");
} else {
// delete all feed stories
$deleteQuery=$this->db->query("DELETE FROM Newswire;");
}
}
function buildStoriesTabs($current='all') {
$tabs='<div id="subNav" class="tabs clearfix"><div class="left_tabs"><ul class="toggle_tabs clearfix" id="toggle_tabs_unused">';
$tabs.='<li class="first"><a id="tabAllStories" href="?p=stories&o=all" class="'.($current=='all'?'selected':'').'" onclick="setNewswireTab(\'all\');return false;">All Stories</a></li>';
$tabs.='<li ><a id="tabRawFeeds" href="?p=stories&o=raw" class="'.($current=='raw'?'selected':'').'" onclick="setNewswireTab(\'raw\');return false;">Raw Feeds</a></li>';
$tabs.='</ul><!-- end left_tabs --></div><!-- end subNav --></div>';
return $tabs;
}
function fetchNewswire($option='all',$filter='all',$memberFriends='',$currentPage=1) {
$code.='<input type="hidden" id="option" value="'.$option.'">';
$code.='<div id="navFilter"><input type="hidden" id="filter" value="'.$filter.'">';
if ($option=='all') {
$code.=$this->fetchStoryFilter($filter);
}
$code.='<!-- end navFilter --></div>';
$code.='<div id="storyList">';
$code.=$this->fetchNewswirePage($option,$filter,$memberFriends,$currentPage);
$code.='<!-- end storyList --></div>';
$code.='<input type="hidden" id="pagingFunction" value="fetchNewswirePage">';
return $code;
}
function fetchStoryFilter($filter='all') {
$code='<div class="subFilter">Filter by: <a id="storyFilterAll" class="feedFilterButton '.(($filter=='all')?'selected':'').'" href="#" onclick="setNewswireFilter(\'all\');return false;">All stories</a>';
$code.='<a id="storyFilterSponsor" class="feedFilterButton '.(($filter=='sponsor')?'selected':'').'" href="#" onclick="setNewswireFilter(\'sponsor\');return false;">'.SITE_SPONSOR.'</a>';
$code.='<a id="storyFilterFriends" class="feedFilterButton '.(($filter=='friends')?'selected':'').'" href="#" onclick="setNewswireFilter(\'friends\');return false;">Friends</a>';
$code.='</div><!--end "subfilter"-->';
return $code;
}
function fetchRawStories() {
require_once(PATH_CORE.'/classes/template.class.php');
$this->templateObj=new template($this->db);
$this->templateObj->registerTemplates(MODULE_ACTIVE,'newswire');
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$this->templateObj->db->result=$this->templateObj->db->query("select id,title,caption,source,url,wireid from Newswire where (
select count(*) from Newswire as f where f.wireid= Newswire.wireid and f.id > Newswire.id
) < 5 ORDER BY id DESC;");
$rowTotal=$this->templateObj->db->countFoundRows();
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), array('caption', 500));
$this->templateObj->db->setTemplateCallback('safeCaption', array($this, 'encodeCleanString'), array('caption', 500));
$this->templateObj->db->setTemplateCallback('safeUrl', array($this, 'encodeUrl'), 'url');
$code=$this->templateObj->mergeTemplate($this->templateObj->templates['rawList'],$this->templateObj->templates['autoItem']);
return $code;
}
function encodeCleanString($str,$cnt) {
return urlencode(substr(strip_tags($str), 0, ($cnt - 1)));
}
function encodeUrl($url) {
return urlencode($url);
}
function fetchNewswirePage($option='all',$filter='all',$memberFriends='',$currentPage=1) {
// filter = show posted stories or rss feeds
// sort for posted stories: sort by votes, sort by date
// sort for rss feeds: (none)
require_once(PATH_CORE.'/classes/template.class.php');
$this->templateObj=new template($this->db);
$this->templateObj->registerTemplates(MODULE_ACTIVE,'newswire');
// load stories from cache or create a new one, unless its the friends' stories
if ($filter=='friends') $neverCache=true; else $neverCache=false;
$cacheName='nw_'.$option.'_'.$filter.'_'.$currentPage;
if ($this->templateObj->checkCache($cacheName,15) AND !$neverCache) {
// still current, get from cache
$code=$this->templateObj->fetchCache($cacheName);
} else {
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$startRow=($currentPage-1)*ROWS_PER_PAGE;
switch ($option) {
case 'raw':
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Newswire WHERE date<now() AND date > date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$rowTotal=$this->templateObj->db->countFoundRows();
$pagingHTML=$this->templateObj->paging($currentPage,$rowTotal,ROWS_PER_PAGE,'?p=stories&o=raw¤tPage=');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), array('caption', 130));
//$this->templateObj->db->setTemplateCallback('cmdAdd', array($this->templateObj, 'commandAdd'), 'siteContentId');
//$this->templateObj->db->setTemplateCallback('cmdRead', array($this->templateObj, 'commandRead'), 'permalink');
$code.=$this->templateObj->mergeTemplate($this->templateObj->templates['rawList'],$this->templateObj->templates['rawItem']);
break;
default:
switch ($filter) {
case 'sponsor':
$moderatorList=$this->db->buildIdList("SELECT userid AS id FROM User WHERE isSponsor=1;");
if ($moderatorList<>'') {
// to do - remove broken postedbyid = userid join
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS Content.*,UserInfo.fbId FROM Content LEFT JOIN UserInfo ON (Content.userid = UserInfo.userid) WHERE FIND_IN_SET(Content.userid,'".$moderatorList."') AND date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$rowTotal=$this->templateObj->db->countFoundRows();
$pagingHTML=$this->templateObj->paging($currentPage,$rowTotal,ROWS_PER_PAGE,'?p=stories&o=all&filter=all¤tPage=');
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('mbrLink', array($this->templateObj, 'memberLink'), 'postedById');
$this->templateObj->db->setTemplateCallback('mbrImage', array($this->templateObj, 'memberImage'), 'postedById');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), array('caption', 130));
$this->templateObj->db->setTemplateCallback('cmdVote', array($this->templateObj, 'commandVote'), 'siteContentId');
$this->templateObj->db->setTemplateCallback('cmdComment', array($this->templateObj, 'commandComment'), 'siteContentId');
$code.=$this->templateObj->mergeTemplate($this->templateObj->templates['storyList'],$this->templateObj->templates['storyItem']);
} else {
$code.='There are no sponsors identified for this site.';
}
break;
case 'friends':
if ($memberFriends<>'') {
// to do - remove broken postedbyid = userid join
//$this->templateObj->db->log("SELECT SQL_CALC_FOUND_ROWS Content.*,UserInfo.fbId FROM Content LEFT JOIN UserInfo ON (Content.userid = UserInfo.userid) WHERE FIND_IN_SET(Content.userid,".$memberFriends.") AND date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Content WHERE FIND_IN_SET(userid,'".$memberFriends."') AND date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$rowTotal=$this->templateObj->db->countFoundRows();
if ($rowTotal>0) {
$pagingHTML=$this->templateObj->paging($currentPage,$rowTotal,ROWS_PER_PAGE,'?p=stories&o=all&filter=all¤tPage=');
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('mbrLink', array($this->templateObj, 'memberLink'), 'postedById');
$this->templateObj->db->setTemplateCallback('mbrImage', array($this->templateObj, 'memberImage'), 'postedById');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), array('caption', 130));
$this->templateObj->db->setTemplateCallback('cmdVote', array($this->templateObj, 'commandVote'), 'siteContentId');
$this->templateObj->db->setTemplateCallback('cmdComment', array($this->templateObj, 'commandComment'), 'siteContentId');
$code.=$this->templateObj->mergeTemplate($this->templateObj->templates['storyList'],$this->templateObj->templates['storyItem']);
} else {
$code.='Your friends have not posted a story yet!';
}
} else {
$code.='None of your friends have joined '.SITE_TITLE;
}
break;
default:
// to do - remove broken postedbyid = userid join
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS Content.*,UserInfo.fbId FROM Content LEFT JOIN UserInfo ON (Content.userid = UserInfo.userid) WHERE date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) AND isBlocked=0 ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$rowTotal=$this->templateObj->db->countFoundRows();
$pagingHTML=$this->templateObj->paging($currentPage,$rowTotal,ROWS_PER_PAGE,'?p=stories&o=all&filter=all¤tPage=');
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('mbrLink', array($this->templateObj, 'memberLink'), 'postedById');
$this->templateObj->db->setTemplateCallback('mbrImage', array($this->templateObj, 'memberImage'), 'postedById');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), array('caption', 130));
$this->templateObj->db->setTemplateCallback('cmdVote', array($this->templateObj, 'commandVote'), 'siteContentId');
$this->templateObj->db->setTemplateCallback('cmdComment', array($this->templateObj, 'commandComment'), 'siteContentId');
//$this->templateObj->db->setTemplateCallback('cmdAdd', array($this->templateObj, 'commandAdd'), 'siteContentId');
// $this->templateObj->db->setTemplateCallback('cmdRead', array($this->templateObj, 'commandRead'), 'permalink');
$code.=$this->templateObj->mergeTemplate($this->templateObj->templates['storyList'],$this->templateObj->templates['storyItem']);
break;
}
break;
}
$code.=$pagingHTML;
if (!$neverCache) $this->templateObj->cacheContent($cacheName,$code); // note: conditionally write the file, if not always needed
}
return $code;
}
function fetchPublisherStories($option='all',$filter='default',$currentPage=1) { /*for publisher/email attachment*/
// to do: build filter and sort controls - outside of ajax...see sample code in newscloud facebook app or ask Jeff
// filter = show posted stories or rss feeds
// sort for posted stories: sort by votes, sort by date
// sort for rss feeds: (none)
require_once(PATH_CORE.'/classes/template.class.php');
$this->templateObj=new template($this->db);
$this->templateObj->registerTemplates(MODULE_ACTIVE,'publisher');
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$startRow=($currentPage-1)*ROWS_PER_PAGE;
$makeList=1;
switch ($filter){
case 'user':
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Content WHERE userid=(SELECT userid FROM UserInfo WHERE fbId=".$_POST['fb_sig_user'].") AND date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
$rowTotal=$this->templateObj->db->countFoundRows();
if ($rowTotal<1) {
$makeList=0;
$code.='You have not posted a story yet!';
}
break;
default:
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Content WHERE date>date_sub(NOW(), INTERVAL ".AGE_STORY_MAX_DAYS." DAY) ORDER BY date DESC LIMIT $startRow,".ROWS_PER_PAGE.";");
break;
}
if ($makeList){
$rowTotal=$this->templateObj->db->countFoundRows();
$pagingHTML=$this->templateObj->paging($currentPage,$rowTotal,ROWS_PER_PAGE,'?p=stories&o=all&filter=default¤tPage=');
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getAbsoluteStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('submitBy', array($this->templateObj, 'submitBy'), 'postedByName');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), 'caption');
$this->templateObj->db->setTemplateCallback('storyLink', array($this->templateObj, 'getAbsoluteStoryLink'), 'siteContentId');
//
$code.=$this->templateObj->mergeTemplate($this->templateObj->templates['storyList'],$this->templateObj->templates['storyItem']);
}
return $code;
}
function fetchPostedStoryInfo($cid){ /*for publisher/email attachment*/
require_once(PATH_CORE.'/classes/template.class.php');
$this->templateObj=new template($this->db);
$this->templateObj->registerTemplates(MODULE_ACTIVE,'publisher');
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS * FROM Content WHERE siteContentId=".$cid." LIMIT 1");
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('timeSince', array($this->utilObj, 'time_since'), 'date');
$this->templateObj->db->setTemplateCallback('caption', array($this->templateObj, 'cleanString'), 'caption');
$code=$this->templateObj->mergeTemplate($this->templateObj->templates['postedList'],$this->templateObj->templates['postedItem']);
$this->templateObj->db->result=$this->templateObj->db->query("SELECT SQL_CALC_FOUND_ROWS Content.*,ContentImages.url FROM Content LEFT JOIN ContentImages ON (Content.siteContentId=ContentImages.siteContentId) WHERE Content.siteContentId=".$cid." LIMIT 1");
$storyInfo=$this->templateObj->db->read();
$retArray=array('title'=>trim($storyInfo->title),
'storyLink'=>URL_CANVAS.'?p=read&o=comments&cid='.$cid.'&record',
'image'=>$storyInfo->imageUrl,
'story'=>$code
);
return $retArray;
}
/*profile box stuff*/
function fetchFeaturedStoriesForProfileBox(){
//echo 'module active is '.MODULE_ACTIVE;
require_once(PATH_CORE.'/classes/template.class.php');
$this->templateObj=new template($this->db);
$this->templateObj->registerTemplates(MODULE_ACTIVE,'profile');
require_once(PATH_CORE.'/classes/utilities.class.php');
$this->utilObj=new utilities($this->db);
$this->templateObj->db->result = $this->templateObj->db->query("SELECT * FROM FeaturedTemplate WHERE id = 1");
$this->templateObj->db->setTemplateCallback('storyImage', array($this->templateObj, 'getAbsoluteStoryImage'), 'imageid');
$this->templateObj->db->setTemplateCallback('storyLink', array($this->templateObj, 'getAbsoluteStoryLink'), 'siteContentId');
$featured = mysql_fetch_assoc($this->templateObj->db->result);
$code = '';
$story_ids = array();
foreach ($featured as $field => $value)
if (preg_match('/^story_([0-9]+)_id$/', $field))
$story_ids[] = $value;
$story_ids = array_slice($story_ids, 0, 3);
$this->templateObj->db->result=$this->templateObj->db->query("SELECT * FROM Content WHERE siteContentId IN (".join(',', $story_ids).")");
$code=$this->templateObj->mergeTemplate($this->templateObj->templates['storyList'],$this->templateObj->templates['storyItem']);
$code.='end of fetch featured stories';
//echo 'templat eload is : '; var_dump($this->templateObj->templates);
return $code;
}
function makeStoryLink($story){
$cid = $story['siteContentId'];
$title = $story['title'];
return "<a href=\"".URL_CANVAS."/?p=read&cid=$cid\">$title</a>";
}
}
?>