Extension: Voting
[vanilla-miry.git] / extensions / FeedPublisher / default.php
blob1074743555b4b79dbc172479dbabf10e51a584a5
1 <?php
2 /*
3 Extension Name: Feed Publisher
4 Extension Url: http://tools.assembla.com/vanilla_feed_publisher/
5 Description: Handles feed publishing. Based on CrudeRSS, RSS2 and ATOM.
6 Version: 0.3.2
7 Author: Christophe Gragnic, Dan Richman (CrudeRSS), Mark O'Sullivan (RSS2, ATOM)
8 Author Url: http://www.fasterdisco.com/photo/44_78_b.jpg
9 */
10 if (!defined('IN_VANILLA')) exit();
12 // language strings, see the 'translations' directory
13 $Context->SetDefinition(
14 "Feeds",
15 "Feeds");
16 $Context->SetDefinition(
17 "FP_feedLinkToolTip_discussion",
18 "Subscribe to the feed of this discussion");
19 $Context->SetDefinition(
20 "FP_feedLinkToolTip_search",
21 "Subscribe to the feed of this search");
22 $Context->SetDefinition(
23 "RSS2Feed",
24 "RSS2");
25 $Context->SetDefinition(
26 "ATOMFeed",
27 "ATOM");
28 $Context->SetDefinition(
29 "FailedFeedAuthenticationTitle",
30 "Failed Authentication");
31 $Context->SetDefinition(
32 "FailedFeedAuthenticationText",
33 "Feeds for this forum require user authentication.");
34 // end language strings
36 ///// now the code...
37 ////////////////////////
39 // grab the config items
40 include( $Configuration[ 'EXTENSIONS_PATH' ] . 'FeedPublisher/config.php' );
41 // incorporate them in the Vanilla ones with our 'fp_' namespace
42 if ( is_array( $fp_conf ) AND !empty( $fp_conf ) )
44 foreach( $fp_conf as $k => $v )
45 $Context->Configuration[ 'fp_'.$k ] = $v;
47 unset( $fp_conf );
49 // this will tell us if we browse the forum or if a feed has to be rendered
50 // ( $FeedType == '' ) means normal browsing
51 $FeedType = ForceIncomingString( 'Feed', '' );
53 // sanitize the feed type: only let the published ones through
54 if ( ! in_array( $FeedType, $Context->Configuration[ 'fp_PublishedTypes' ] ) )
55 $FeedType = '';
57 // only process Feed Publisher on some pages and if the feed type is correct
58 // quick note: list of php files in Vanilla root is
59 // account.php categories.php comments.php extension.php index.php
60 // people.php post.php search.php settings.php termsofservice.php
61 $processed_pages = array( 'index.php', 'search.php', 'comments.php' );
62 $GoodPage = in_array( $Context->SelfUrl, $processed_pages );
63 $GoodFeedType = (
64 in_array( $FeedType, $Context->Configuration[ 'fp_PublishedTypes' ] )
65 OR $FeedType == '' );
66 if ( $GoodPage AND $GoodFeedType )
68 // first some common stuff, then we'll act according to the requested page
69 if ( $FeedType == '' )
71 include( $Configuration[ 'EXTENSIONS_PATH' ] . 'FeedPublisher/global_functions.php' );
73 else
75 include( $Configuration[ "EXTENSIONS_PATH" ] . "FeedPublisher/functions.php" );
77 // Make sure that page is not redirected if the user is not signed in
78 // and this is not a public forum
79 if (
80 $Context->Session->UserID == 0
81 && ! $Configuration[ "PUBLIC_BROWSING" ]
84 // Temporarily make the PUBLIC_BROWSING enabled,
85 // but make sure to validate this user
86 $Configuration[ "PUBLIC_BROWSING" ] = 1;
87 $Context->Configuration[ 'AUTHENTICATE_USER_FOR_FEED_PUBLISHER' ] = 1;
89 else
91 $Context->Configuration[ 'AUTHENTICATE_USER_FOR_FEED_PUBLISHER' ] = 0;
94 // if we don't want to exit after the display of the feed,
95 // we display it after cleaning the buffer, after PageEnd's Render
96 if ( ! $Context->Configuration[ 'fp_ExitAfterFeed' ] )
98 $Context->AddToDelegate(
99 'PageEnd',
100 'PostRender',
101 'CleanBufferAndDisplayFeed' );
105 // act here according to the requested page
106 if ( $Context->SelfUrl == "index.php" )
108 // see if we browse the forum or if a feed must be rendered
109 // ( $FeedType == '' ) means normal browsing
110 if ( $FeedType == '' )
112 // include user configuration about categories, that's all
113 $cat_conf = 'FeedPublisher/categories_config.php';
114 include( $Configuration[ 'EXTENSIONS_PATH' ] . $cat_conf );
116 else
118 // REMOVED DUE TO PROBLEMS
119 // Use manual generic search feed without keywords instead.
122 elseif ( $Context->SelfUrl == "search.php" )
124 // grab the search params in the url
125 $SearchType = ForceIncomingString( "Type", "" );
126 $SearchID = ForceIncomingInt( "SearchID", 0);
128 // if no search type in the url but we found an 'id'
129 // grab the search type from the 'id'
130 if ( $SearchType == "" && $SearchID > 0 )
132 $SearchManager
133 = $Context->ObjectFactory->NewContextObject( $Context, "SearchManager" );
134 $Search = $SearchManager->GetSearchById( $SearchID );
135 if ( $Search ) $SearchType = $Search->Type;
138 // now see if we browse the forum or if a feed must be rendered
139 // ( $FeedType == '' ) means normal browsing
140 if ( $FeedType == '' )
142 // add feed links in the Vanilla panel and in the html head
143 // no links are set for User searches
144 if ( $SearchType == 'Topics' || $SearchType == 'Comments' )
146 foreach ( $Context->Configuration[ 'fp_PublishedTypes' ] as $PublishedType )
148 $LinksProperties[ 'FeedType' ] = $PublishedType ;
149 $LinksProperties[ 'PageType' ] = 'search';
150 AddLinks( $Context, $Head, $Panel, $LinksProperties );
154 else
156 if ( $SearchType == "Topics" )
158 // Make sure that the first comment is also grabbed from the search
159 $Context->AddToDelegate(
160 "DiscussionManager",
161 "PostGetDiscussionBuilder",
162 "DiscussionManager_GetFirstCommentForFeeds" );
164 // Attach to the PostLoadData Delegate of the SearchForm control
165 function SearchForm_InjectFeedToTopicSearch( $SearchForm )
167 global $FeedType;
169 if ( $SearchForm->Context->WarningCollector->Count() == 0 )
171 AuthenticateUserForFP( $SearchForm->Context );
173 // Make sure that some channel properties are defined
174 $SearchForm->Context->PageTitle
175 = ForceIncomingString( "Categories", "" );
176 $SearchForm->Context->GetKeywords
177 = ForceIncomingString( "Keywords", "" );
178 $SearchForm->Context->GetAuthUserName
179 = ForceIncomingString( "AuthUsername", "" );
181 // Loop through the data
182 $Counter = 0;
183 $Feed = "";
184 $Properties = array();
185 while ( $DataSet = $SearchForm->Context->Database->GetRow( $SearchForm->Data ) )
187 if ( $Counter < $SearchForm->Context->Configuration[ "SEARCH_RESULTS_PER_PAGE" ] )
189 $Properties[ "Title" ]
190 = FormatHtmlStringInline( ForceString( $DataSet[ "Name" ], "" ) );
191 $Properties[ "Link" ]
192 = GetUrl(
193 $SearchForm->Context->Configuration,
194 "comments.php",
196 "DiscussionID",
197 ForceInt( $DataSet[ "DiscussionID" ], 0 ) );
198 $Properties[ "Published" ]
199 = FixDateForFP( $FeedType, @$DataSet[ "DateCreated" ] );
200 $Properties[ "Updated" ]
201 = FixDateForFP( $FeedType, @$DataSet[ "DateLastActive" ]);
202 $Properties[ "AuthorName" ]
203 = FormatHtmlStringInline(
204 ForceString($DataSet[ "AuthUsername" ], ""));
205 $Properties[ "AuthorUrl" ]
206 = GetUrl(
207 $SearchForm->Context->Configuration,
208 "account.php",
210 "u",
211 ForceInt($DataSet[ "AuthUserID" ],
212 0));
214 // Format the comment according to the defined
215 // formatter for that comment
216 $FormatType
217 = ForceString(
218 @$DataSet[ "FormatType" ],
219 $SearchForm->Context->Configuration[ "DEFAULT_FORMAT_TYPE" ] );
220 $FirstCommentBody
221 = ForceString(
222 @$DataSet[ "Body" ], '...' );
223 $Properties[ "Content" ]
224 = $SearchForm->Context->FormatString(
225 $FirstCommentBody,
226 $SearchForm,
227 $FormatType,
228 FORMAT_STRING_FOR_DISPLAY);
229 $Properties[ "Summary" ]
230 = FormatStringForFeedSummary( @$DataSet[ "Body" ], $FeedType);
232 $Feed .= ReturnFeedItem( $Properties, $FeedType );
234 $Counter++;
237 $Feed = ReturnWrappedFeedForSearch( $SearchForm->Context, $Feed, $FeedType );
239 if ( $SearchForm->Context->Configuration[ 'fp_ExitAfterFeed' ] )
241 // Set the content type to xml and dump the feed
242 header("Content-type: text/xml\n");
243 echo( $Feed );
245 // When all finished, unload the context object and stop
246 $SearchForm->Context->Unload();
247 exit;
249 else
251 // store the feed in the context object
252 $SearchForm->Context->Feed = $Feed;
257 $Context->AddToDelegate(
258 "SearchForm",
259 "PostLoadData",
260 "SearchForm_InjectFeedToTopicSearch" );
262 elseif ( $SearchType == "Comments" )
264 // Attach to the PostLoadData Delegate of the SearchForm control
265 function SearchForm_InjectFeedToCommentSearch( $SearchForm )
267 global $FeedType;
269 if ($SearchForm->Context->WarningCollector->Count() == 0)
271 AuthenticateUserForFP( $SearchForm->Context );
273 // Make sure that some channel properties are defined
274 $SearchForm->Context->PageTitle
275 = ForceIncomingString( "Categories", "" );
276 $SearchForm->Context->GetKeywords
277 = ForceIncomingString( "Keywords", "" );
278 $SearchForm->Context->GetAuthUserName
279 = ForceIncomingString( "AuthUsername", "" );
281 // Loop through the data
282 $Counter = 0;
283 $Feed = "";
284 $Properties = array();
285 $Comment = $SearchForm->Context->ObjectFactory->NewContextObject( $SearchForm->Context, "Comment" );
286 while ( $Row = $SearchForm->Context->Database->GetRow( $SearchForm->Data ) )
288 $Comment->Clear();
289 $Comment->GetPropertiesFromDataSet( $Row, $SearchForm->Context->Session->UserID );
291 if ( $Counter < $SearchForm->Context->Configuration["SEARCH_RESULTS_PER_PAGE" ] )
293 $Properties[ "Title" ]
294 = FormatHtmlStringInline( ForceString( $Comment->Discussion, "" ) );
295 $Properties[ "Link" ]
296 = GetUrl(
297 $SearchForm->Context->Configuration,
298 "comments.php",
300 "DiscussionID",
301 $Comment->DiscussionID,
303 "Focus=" . $Comment->CommentID
304 ."#Comment_" . $Comment->CommentID );
305 $Properties[ "Published" ]
306 = FixDateForFP( $FeedType, @$Row[ "DateCreated" ] );
307 $Properties[ "Updated" ]
308 = FixDateForFP( $FeedType, @$Row[ "DateEdited" ] );
309 $Properties[ "AuthorName" ]
310 = $Comment->AuthUsername;
311 $Properties[ "AuthorUrl" ]
312 = GetUrl(
313 $SearchForm->Context->Configuration,
314 "account.php",
316 "u",
317 $Comment->AuthUserID );
319 // Format the comment according to the defined
320 // formatter for that comment
321 $Properties[ "Content" ]
322 = $SearchForm->Context->FormatString(
323 $Comment->Body,
324 $Comment,
325 $Comment->FormatType,
326 FORMAT_STRING_FOR_DISPLAY);
327 $Properties[ "Summary" ]
328 = FormatStringForFeedSummary( @$Row[ "Body" ], $FeedType );
330 $Feed .= ReturnFeedItem( $Properties, $FeedType );
332 $Counter++;
335 $Feed = ReturnWrappedFeedForSearch( $SearchForm->Context, $Feed, $FeedType );
337 if ( $SearchForm->Context->Configuration[ 'fp_ExitAfterFeed' ] )
339 // Set the content type to xml and dump the feed
340 header("Content-type: text/xml\n");
341 echo( $Feed );
343 // When all finished, unload the context object and stop
344 $SearchForm->Context->Unload();
345 exit;
347 else
349 // store the feed in the context object
350 $SearchForm->Context->Feed = $Feed;
355 $Context->AddToDelegate(
356 "SearchForm",
357 "PostLoadData",
358 "SearchForm_InjectFeedToCommentSearch");
362 elseif ( $Context->SelfUrl == "comments.php" )
364 // see if we browse the forum or if a feed must be rendered
365 // ( $FeedType == '' ) means normal browsing
366 if ( $FeedType == '' )
368 // add feed links in the Vanilla panel and in the html head
369 foreach ( $Context->Configuration[ 'fp_PublishedTypes' ] as $PublishedType )
371 $LinksProperties[ 'FeedType' ] = $PublishedType ;
372 $LinksProperties[ 'PageType' ] = 'discussion';
373 AddLinks( $Context, $Head, $Panel, $LinksProperties );
376 else
378 // Attach to the Constructor Delegate of the CommentGrid control
379 function CommentGrid_InjectFeed( $CommentGrid )
381 global $FeedType;
383 if ( $CommentGrid->Context->WarningCollector->Count() == 0 )
385 AuthenticateUserForFP( $CommentGrid->Context );
387 // Make sure that some channel properties are defined
388 $CommentGrid->Context->PageTitle
389 = $CommentGrid->Discussion->Name;
391 // Loop through the data
392 $Feed = "";
393 $Properties = array();
394 $PageCount
395 = CalculateNumberOfPages(
396 $CommentGrid->CommentDataCount,
397 $CommentGrid->Context->Configuration[ 'COMMENTS_PER_PAGE' ] );
398 $CommentManager
399 = $CommentGrid->Context->ObjectFactory->NewContextObject(
400 $CommentGrid->Context,
401 'CommentManager' );
402 $CurrentPage = 1;
403 while ( $CurrentPage <= $PageCount )
405 $CommentGrid->CommentData = $CommentManager->GetCommentList(
406 $CommentGrid->Context->Configuration[ 'COMMENTS_PER_PAGE' ],
407 $CurrentPage,
408 $CommentGrid->Discussion->DiscussionID );
409 $Comment
410 = $CommentGrid->Context->ObjectFactory->NewContextObject(
411 $CommentGrid->Context,
412 "Comment" );
413 while ( $Row = $CommentGrid->Context->Database->GetRow( $CommentGrid->CommentData ) )
415 $Comment->Clear();
416 $Comment->GetPropertiesFromDataSet( $Row, $CommentGrid->Context->Session->UserID );
418 $Properties[ "Title" ]
419 = FormatHtmlStringInline( ForceString( $CommentGrid->Discussion->Name, "" ) );
420 $Properties[ "Link" ]
421 = GetUrl(
422 $CommentGrid->Context->Configuration,
423 "comments.php",
425 "DiscussionID",
426 $Comment->DiscussionID,
428 "Focus=" . $Comment->CommentID
429 ."#Comment_" . $Comment->CommentID );
430 $Properties[ "Published" ]
431 = FixDateForFP( $FeedType, @$Row[ "DateCreated" ] );
432 $Properties[ "Updated" ]
433 = FixDateForFP( $FeedType, @$Row[ "DateEdited" ] );
434 $Properties[ "AuthorName" ]
435 = $Comment->AuthUsername;
436 $Properties[ "AuthorUrl" ]
437 = GetUrl(
438 $CommentGrid->Context->Configuration,
439 "account.php",
441 "u",
442 $Comment->AuthUserID );
444 // Format the comment according to the defined
445 // formatter for that comment
446 $Properties[ "Content" ]
447 = $CommentGrid->Context->FormatString(
448 $Comment->Body,
449 $Comment,
450 $Comment->FormatType,
451 FORMAT_STRING_FOR_DISPLAY);
452 $Properties[ "Summary" ]
453 = FormatStringForFeedSummary( @$Row[ "Body" ] );
455 $Feed .= ReturnFeedItem( $Properties, $FeedType );
457 $CurrentPage++;
460 $Feed = ReturnWrappedFeed( $CommentGrid->Context, $Feed, $FeedType );
462 if ( $CommentGrid->Context->Configuration[ 'fp_ExitAfterFeed' ] )
464 // Set the content type to xml and dump the feed
465 header("Content-type: text/xml\n");
466 echo( $Feed );
468 // When all finished, unload the context object and stop
469 $CommentGrid->Context->Unload();
470 exit;
472 else
474 // store the feed in the context object
475 $CommentGrid->Context->Feed = $Feed;
480 $Context->AddToDelegate(
481 "CommentGrid",
482 "Constructor",
483 "CommentGrid_InjectFeed" );