| [ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 3 /* Reminder: always indent with 4 spaces (no tabs). */ 4 // +---------------------------------------------------------------------------+ 5 // | Geeklog 1.4 | 6 // +---------------------------------------------------------------------------+ 7 // | lib-syndication.php | 8 // | | 9 // | Geeklog syndication library. | 10 // +---------------------------------------------------------------------------+ 11 // | Copyright (C) 2003-2006 by the following authors: | 12 // | | 13 // | Authors: Dirk Haun - dirk AT haun-online DOT de | 14 // | Michael Jervis - mike AT fuckingbrit DOT com | 15 // +---------------------------------------------------------------------------+ 16 // | | 17 // | This program is free software; you can redistribute it and/or | 18 // | modify it under the terms of the GNU General Public License | 19 // | as published by the Free Software Foundation; either version 2 | 20 // | of the License, or (at your option) any later version. | 21 // | | 22 // | This program is distributed in the hope that it will be useful, | 23 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 24 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 25 // | GNU General Public License for more details. | 26 // | | 27 // | You should have received a copy of the GNU General Public License | 28 // | along with this program; if not, write to the Free Software Foundation, | 29 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 30 // | | 31 // +---------------------------------------------------------------------------+ 32 // 33 // $Id: lib-syndication.php,v 1.38 2006/11/22 04:44:57 blaine Exp $ 34 35 // set to true to enable debug output in error.log 36 $_SYND_DEBUG = false; 37 38 if (strpos ($_SERVER['PHP_SELF'], 'lib-syndication.php') !== false) { 39 die ('This file can not be used on its own!'); 40 } 41 42 if ($_CONF['trackback_enabled']) { 43 require_once ($_CONF['path_system'] . 'lib-trackback.php'); 44 } 45 46 /** 47 * Check if a feed for all stories needs to be updated. 48 * 49 * @param string $update_info list of story ids 50 * @param string $limit number of entries or number of hours 51 * @param string $updated_topic (optional) topic to be updated 52 * @param string $updated_id (optional) entry id to be updated 53 * @return bool false = feed needs to be updated 54 * 55 */ 56 function SYND_feedUpdateCheckAll( $update_info, $limit, $updated_topic = '', $updated_id = '' ) 57 { 58 global $_CONF, $_TABLES, $_SYND_DEBUG; 59 60 $where = ''; 61 if( !empty( $limit )) 62 { 63 if( substr( $limit, -1 ) == 'h' ) // last xx hours 64 { 65 $limitsql = ''; 66 $hours = substr( $limit, 0, -1 ); 67 $where = " AND date >= DATE_SUB(NOW(),INTERVAL $hours HOUR)"; 68 } 69 else 70 { 71 $limitsql = ' LIMIT ' . $limit; 72 } 73 } 74 else 75 { 76 $limitsql = ' LIMIT 10'; 77 } 78 79 // get list of topics that anonymous users have access to 80 $tresult = DB_query( "SELECT tid FROM {$_TABLES['topics']}" 81 . COM_getPermSQL( 'WHERE', 1 )); 82 $tnumrows = DB_numRows( $tresult ); 83 $topiclist = array(); 84 for( $i = 0; $i < $tnumrows; $i++ ) 85 { 86 $T = DB_fetchArray( $tresult ); 87 $topiclist[] = $T['tid']; 88 } 89 if( count( $topiclist ) > 0 ) 90 { 91 $tlist = "'" . implode( "','", $topiclist ) . "'"; 92 $where .= " AND (tid IN ($tlist))"; 93 } 94 95 $result = DB_query( "SELECT sid FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW() $where AND perm_anon > 0 ORDER BY date DESC $limitsql" ); 96 $nrows = DB_numRows( $result ); 97 98 $sids = array (); 99 for( $i = 0; $i < $nrows; $i++ ) 100 { 101 $A = DB_fetchArray( $result ); 102 103 if( $A['sid'] == $updated_id ) 104 { 105 // no need to look any further - this feed has to be updated 106 return false; 107 } 108 109 $sids[] = $A['sid']; 110 } 111 $current = implode( ',', $sids ); 112 113 if ($_SYND_DEBUG) { 114 COM_errorLog ("Update check for all stories: comparing new list ($current) with old list ($update_info)", 1); 115 } 116 117 return ( $current != $update_info ) ? false : true; 118 } 119 120 /** 121 * Check if a feed for stories from a topic needs to be updated. 122 * 123 * @param string $tid topic id 124 * @param string $update_info list of story ids 125 * @param string $limit number of entries or number of hours 126 * @param string $updated_topic (optional) topic to be updated 127 * @param string $updated_id (optional) entry id to be updated 128 * @return bool false = feed needs to be updated 129 * 130 */ 131 function SYND_feedUpdateCheckTopic( $tid, $update_info, $limit, $updated_topic = '', $updated_id = '' ) 132 { 133 global $_CONF, $_TABLES, $_SYND_DEBUG; 134 135 $where = ''; 136 if( !empty( $limit )) 137 { 138 if( substr( $limit, -1 ) == 'h' ) // last xx hours 139 { 140 $limitsql = ''; 141 $hours = substr( $limit, 0, -1 ); 142 $where = " AND date >= DATE_SUB(NOW(),INTERVAL $hours HOUR)"; 143 } 144 else 145 { 146 $limitsql = ' LIMIT ' . $limit; 147 } 148 } 149 else 150 { 151 $limitsql = ' LIMIT 10'; 152 } 153 154 $result = DB_query( "SELECT sid FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW() AND tid = '$tid' AND perm_anon > 0 ORDER BY date DESC $limitsql" ); 155 $nrows = DB_numRows( $result ); 156 157 $sids = array (); 158 for( $i = 0; $i < $nrows; $i++ ) 159 { 160 $A = DB_fetchArray( $result ); 161 162 if( $A['sid'] == $updated_id ) 163 { 164 // no need to look any further - this feed has to be updated 165 return false; 166 } 167 168 $sids[] = $A['sid']; 169 } 170 $current = implode( ',', $sids ); 171 172 if ($_SYND_DEBUG) { 173 COM_errorLog ("Update check for topic $tid: comparing new list ($current) with old list ($update_info)", 1); 174 } 175 176 return ( $current != $update_info ) ? false : true; 177 } 178 179 /** 180 * Check if the contents of Geeklog's built-in feeds need to be updated. 181 * 182 * @param string topic indicator of the feed's "topic" 183 * @param string limit number of entries or number of hours 184 * @param string updated_topic (optional) specific topic to update 185 * @param string updated_id (optional) specific id to update 186 * @return bool false = feed has to be updated, true = ok 187 * 188 */ 189 function SYND_feedUpdateCheck( $topic, $update_data, $limit, $updated_topic = '', $updated_id = '' ) 190 { 191 $is_current = true; 192 193 switch( $topic ) 194 { 195 case '::all': 196 { 197 $is_current = SYND_feedUpdateCheckAll( $update_data, $limit, 198 $updated_topic, $updated_id ); 199 } 200 break; 201 default: 202 { 203 $is_current = SYND_feedUpdateCheckTopic( $topic, $update_data, 204 $limit, $updated_topic, $updated_id ); 205 } 206 break; 207 } 208 209 return $is_current; 210 } 211 212 /** 213 * Get content for a feed that holds stories from one topic. 214 * 215 * @param string $tid topic id 216 * @param string $limit number of entries or number of stories 217 * @param string $link link to topic 218 * @param string $update list of story ids 219 * @return array content of the feed 220 * 221 */ 222 function SYND_getFeedContentPerTopic( $tid, $limit, &$link, &$update, $contentLength, $feedType, $feedVersion, $fid ) 223 { 224 global $_TABLES, $_CONF, $LANG01; 225 226 $content = array (); 227 $sids = array(); 228 229 if( DB_getItem( $_TABLES['topics'], 'perm_anon', "tid = '$tid'") >= 2) 230 { 231 $where = ''; 232 if( !empty( $limit )) 233 { 234 if( substr( $limit, -1 ) == 'h' ) // last xx hours 235 { 236 $limitsql = ''; 237 $hours = substr( $limit, 0, -1 ); 238 $where = " AND date >= DATE_SUB(NOW(),INTERVAL $hours HOUR)"; 239 } 240 else 241 { 242 $limitsql = ' LIMIT ' . $limit; 243 } 244 } 245 else 246 { 247 $limitsql = ' LIMIT 10'; 248 } 249 250 $topic = stripslashes( DB_getItem( $_TABLES['topics'], 'topic', 251 "tid = '$tid'" )); 252 253 $result = DB_query( "SELECT sid,uid,title,introtext,bodytext,postmode,UNIX_TIMESTAMP(date) AS modified,commentcode,trackbackcode FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW() AND tid = '$tid' AND perm_anon > 0 ORDER BY date DESC $limitsql" ); 254 255 $nrows = DB_numRows( $result ); 256 257 for( $i = 1; $i <= $nrows; $i++ ) 258 { 259 $row = DB_fetchArray( $result ); 260 $sids[] = $row['sid']; 261 262 $storytitle = stripslashes( $row['title'] ); 263 $fulltext = stripslashes( $row['introtext']."\n".$row['bodytext'] ); 264 $fulltext = PLG_replaceTags( $fulltext ); 265 $storytext = SYND_truncateSummary( $fulltext, $contentLength ); 266 267 $fulltext = trim( $fulltext ); 268 $fulltext = preg_replace( "/(\015)/", "", $fulltext ); 269 270 $storylink = COM_buildUrl( $_CONF['site_url'] 271 . '/article.php?story=' . $row['sid'] ); 272 $extensionTags = PLG_getFeedElementExtensions('article', $row['sid'], $feedType, $feedVersion, $tid, $fid); 273 if( $_CONF['trackback_enabled'] && ($feedType == 'RSS') && ($row['trackbackcode'] >= 0)) 274 { 275 $trbUrl = TRB_makeTrackbackUrl( $row['sid'] ); 276 $extensionTags['trackbacktag'] = '<trackback:ping>'.htmlspecialchars($trbUrl).'</trackback:ping>'; 277 } 278 $article = array( 'title' => $storytitle, 279 'summary' => $storytext, 280 'text' => $fulltext, 281 'link' => $storylink, 282 'uid' => $row['uid'], 283 'author' => COM_getDisplayName( $row['uid'] ), 284 'date' => $row['modified'], 285 'format' => $row['postmode'], 286 'topic' => $topic, 287 'extensions' => $extensionTags 288 ); 289 if($row['commentcode'] >= 0) 290 { 291 $article['commenturl'] = $storylink . '#comments'; 292 } 293 $content[] = $article; 294 } 295 } 296 297 $link = $_CONF['site_url'] . '/index.php?topic=' . $tid; 298 $update = implode( ',', $sids ); 299 300 return $content; 301 } 302 303 /** 304 * Get content for a feed that holds all stories. 305 * 306 * @param string $limit number of entries or number of stories 307 * @param string $link link to homepage 308 * @param string $update list of story ids 309 * @param int $contentLength Length of summary to allow. 310 * @param int $fid the id of the feed being fetched 311 * @return array content of the feed 312 * 313 */ 314 function SYND_getFeedContentAll( $limit, &$link, &$update, $contentLength, $feedType, $feedVersion, $fid) 315 { 316 global $_TABLES, $_CONF, $LANG01; 317 318 $where = ''; 319 if( !empty( $limit )) 320 { 321 if( substr( $limit, -1 ) == 'h' ) // last xx hours 322 { 323 $limitsql = ''; 324 $hours = substr( $limit, 0, -1 ); 325 $where = " AND date >= DATE_SUB(NOW(),INTERVAL $hours HOUR)"; 326 } 327 else 328 { 329 $limitsql = ' LIMIT ' . $limit; 330 } 331 } 332 else 333 { 334 $limitsql = ' LIMIT 10'; 335 } 336 337 // get list of topics that anonymous users have access to 338 $topics = array(); 339 $tresult = DB_query( "SELECT tid,topic FROM {$_TABLES['topics']}" 340 . COM_getPermSQL( 'WHERE', 1 )); 341 $tnumrows = DB_numRows( $tresult ); 342 $tlist = ''; 343 for( $i = 1; $i <= $tnumrows; $i++ ) 344 { 345 $T = DB_fetchArray( $tresult ); 346 $tlist .= "'" . $T['tid'] . "'"; 347 if( $i < $tnumrows ) 348 { 349 $tlist .= ','; 350 } 351 $topics[$T['tid']] = stripslashes( $T['topic'] ); 352 } 353 if( !empty( $tlist )) 354 { 355 $where .= " AND (tid IN ($tlist))"; 356 } 357 358 $result = DB_query( "SELECT sid,tid,uid,title,introtext,bodytext,postmode,UNIX_TIMESTAMP(date) AS modified,commentcode,trackbackcode FROM {$_TABLES['stories']} WHERE draft_flag = 0 AND date <= NOW() $where AND perm_anon > 0 ORDER BY date DESC $limitsql" ); 359 360 $content = array(); 361 $sids = array(); 362 $nrows = DB_numRows( $result ); 363 364 for( $i = 1; $i <= $nrows; $i++ ) 365 { 366 $row = DB_fetchArray( $result ); 367 $sids[] = $row['sid']; 368 369 $storytitle = stripslashes( $row['title'] ); 370 371 $fulltext = stripslashes( $row['introtext']."\n".$row['bodytext'] ); 372 $fulltext = PLG_replaceTags( $fulltext ); 373 $storytext = SYND_truncateSummary( $fulltext, $contentLength ); 374 $fulltext = trim( $fulltext ); 375 $fulltext = preg_replace( "/(\015)/", "", $fulltext ); 376 377 $storylink = COM_buildUrl( $_CONF['site_url'] . '/article.php?story=' 378 . $row['sid'] ); 379 $extensionTags = PLG_getFeedElementExtensions('article', $row['sid'], $feedType, $feedVersion, $fid, '::all'); 380 if( $_CONF['trackback_enabled'] && ($feedType == 'RSS') && ($row['trackbackcode'] >= 0)) 381 { 382 $trbUrl = TRB_makeTrackbackUrl( $row['sid'] ); 383 $extensionTags['trackbacktag'] = '<trackback:ping>'.htmlspecialchars($trbUrl).'</trackback:ping>'; 384 } 385 $article = array( 'title' => $storytitle, 386 'summary' => $storytext, 387 'text' => $fulltext, 388 'link' => $storylink, 389 'uid' => $row['uid'], 390 'author' => COM_getDisplayName( $row['uid'] ), 391 'date' => $row['modified'], 392 'format' => $row['postmode'], 393 'topic' => $topics[$row['tid']], 394 'extensions' => $extensionTags 395 ); 396 if($row['commentcode'] >= 0) 397 { 398 $article['commenturl'] = $storylink . '#comments'; 399 } 400 $content[] = $article; 401 402 403 } 404 405 $link = $_CONF['site_url']; 406 $update = implode( ',', $sids ); 407 408 return $content; 409 } 410 411 /** 412 * Update a feed. 413 * Re-written by Michael Jervis (mike AT fuckingbrit DOT com) 414 * to use the new architecture 415 * 416 * @param int $fid feed id 417 * 418 */ 419 function SYND_updateFeed( $fid ) 420 { 421 global $_CONF, $_TABLES, $_SYND_DEBUG; 422 423 $result = DB_query( "SELECT * FROM {$_TABLES['syndication']} WHERE fid = $fid"); 424 $A = DB_fetchArray( $result ); 425 if( $A['is_enabled'] == 1 ) 426 { 427 // Import the feed handling classes: 428 require_once( $_CONF['path_system'] 429 . '/classes/syndication/parserfactory.class.php' ); 430 require_once( $_CONF['path_system'] 431 . '/classes/syndication/feedparserbase.class.php' ); 432 433 // Load the actual feed handlers: 434 $factory = new FeedParserFactory( $_CONF['path_system'] 435 . '/classes/syndication/' ); 436 $format = explode( '-', $A['format'] ); 437 $feed = $factory->writer( $format[0], $format[1] ); 438 439 if( $feed ) 440 { 441 $feed->encoding = $A['charset']; 442 $feed->lang = $A['language']; 443 444 if( $A['type'] == 'geeklog' ) 445 { 446 if( $A['topic'] == '::all') 447 { 448 $content = SYND_getFeedContentAll( $A['limits'], $link, 449 $data, $A['content_length'], 450 $format[0], $format[1], $fid ); 451 } 452 else // feed for a single topic only 453 { 454 $content = SYND_getFeedContentPerTopic( $A['topic'], 455 $A['limits'], $link, $data, $A['content_length'], 456 $format[0], $format[1], $fid ); 457 } 458 } 459 else 460 { 461 $content = PLG_getFeedContent( $A['type'], $fid, $link, $data, $format[0], $format[1] ); 462 // can't randomly change the api to send a max length, so 463 // fix it here: 464 if ($A['content_length'] != 1) 465 { 466 $count = count( $content ); 467 for( $i = 0; $i < $count; $i++ ) 468 { 469 $content[$i]['summary'] = SYND_truncateSummary( 470 $content[$i]['text'], $A['content_length'] ); 471 } 472 } 473 } 474 if( empty( $link )) 475 { 476 $link = $_CONF['site_url']; 477 } 478 479 $feed->title = $A['title']; 480 $feed->description = $A['description']; 481 if( empty( $A['feedlogo'] )) 482 { 483 $feed->feedlogo = ''; 484 } 485 else 486 { 487 $feed->feedlogo = $_CONF['site_url'] . $A['feedlogo']; 488 } 489 $feed->sitelink = $link; 490 $feed->copyright = 'Copyright ' . strftime( '%Y' ) . ' ' 491 . $_CONF['site_name']; 492 $feed->sitecontact = $_CONF['site_mail']; 493 $feed->system = 'GeekLog'; 494 495 /* Gather any other stuff */ 496 $feed->namespaces = PLG_getFeedNSExtensions($A['type'], $format[0], $format[1], $A['topic'], $fid); 497 /* If the feed is RSS, and trackback is enabled */ 498 if( $_CONF['trackback_enabled'] && ($format[0] == 'RSS') ) 499 { 500 /* Check to see if an article has trackbacks enabled, and if 501 * at least one does, then include the trackback namespace: 502 */ 503 $trackbackenabled = false; 504 foreach($content as $item) 505 { 506 if( array_key_exists('extensions', $item) && 507 array_key_exists('trackbacktag', $item['extensions']) 508 ) 509 { 510 // Found at least one article, with a trackbacktag 511 // in it's extensions tag. 512 $trackbackenabled = true; 513 break; 514 } 515 } 516 if( $trackbackenabled ) 517 { 518 $feed->namespaces[] = 'xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/"'; 519 } 520 } 521 $feed->extensions = PLG_getFeedExtensionTags($A['type'], $format[0], $format[1], $A['topic'], $fid); 522 $feed->articles = $content; 523 524 if( !empty( $A['filename'] )) 525 { 526 $filename = $A['filename']; 527 } 528 else 529 { 530 $pos = strrpos( $_CONF['rdf_file'], '/' ); 531 $filename = substr( $_CONF['rdf_file'], $pos + 1 ); 532 } 533 $feed->url = SYND_getFeedUrl( $filename ); 534 $feed->createFeed( SYND_getFeedPath( $filename )); 535 } 536 else 537 { 538 COM_errorLog( "Unable to get a feed writer for {$format[0]} version {$format[1]}.", 1); 539 } 540 541 if( empty( $data )) 542 { 543 $data = 'NULL'; 544 } 545 else 546 { 547 $data = "'" . $data . "'"; 548 } 549 550 if ($_SYND_DEBUG) 551 { 552 COM_errorLog ("update_info for feed $fid is $data", 1); 553 } 554 555 DB_query( "UPDATE {$_TABLES['syndication']} SET updated = NOW(), update_info = $data WHERE fid = $fid"); 556 } 557 } 558 559 /** 560 * Truncate a feed item's text to a given max. length of characters 561 * 562 * @param string $text the item's text 563 * @param int $length max. length 564 * @return string truncated text 565 * 566 */ 567 function SYND_truncateSummary( $text, $length ) 568 { 569 if( $length == 0 ) 570 { 571 return ''; 572 } 573 else 574 { 575 $text = stripslashes( $text ); 576 $text = trim( $text ); 577 $text = preg_replace( "/(\015)/", "", $text ); 578 if(( $length > 3 ) && ( MBYTE_strlen( $text ) > $length )) 579 { 580 $text = substr( $text, 0, $length - 3 ) . '...'; 581 } 582 583 // Check if we broke html tag and storytext is now something 584 // like "blah blah <a href= ...". Delete "<*" if so. 585 if( strrpos( $text, '<' ) > strrpos( $text, '>' )) 586 { 587 $text = substr( $text, 0, strrpos( $text, '<' ) - 1 ) 588 . ' ...'; 589 } 590 591 return $text; 592 } 593 } 594 595 596 /** 597 * Get the path of the feed directory or a specific feed file 598 * 599 * @param string $feedfile (option) feed file name 600 * @return string path of feed directory or file 601 * 602 */ 603 function SYND_getFeedPath( $feedfile = '' ) 604 { 605 global $_CONF; 606 607 $feedpath = $_CONF['rdf_file']; 608 $pos = strrpos( $feedpath, '/' ); 609 $feed = substr( $feedpath, 0, $pos + 1 ); 610 $feed .= $feedfile; 611 612 return $feed; 613 } 614 615 /** 616 * Get the URL of the feed directory or a specific feed file 617 * 618 * @param string $feedfile (option) feed file name 619 * @return string URL of feed directory or file 620 * 621 */ 622 function SYND_getFeedUrl( $feedfile = '' ) 623 { 624 global $_CONF; 625 626 $feedpath = SYND_getFeedPath(); 627 $url = substr_replace ($feedpath, $_CONF['site_url'], 0, 628 strlen ($_CONF['path_html']) - 1); 629 $url .= $feedfile; 630 631 return $url; 632 } 633 634 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
|