| @@ -2,12 +2,13 @@ | ||
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: FeedWordPress |
| 4 | 4 | Plugin URI: http://projects.radgeek.com/feedwordpress |
| 5 | 5 | Description: simple and flexible Atom/RSS syndication for WordPress |
| 6 | -Version: 2009.0618 | |
| 6 | +Version: 2008.1214 | |
| 7 | 7 | Author: Charles Johnson |
| 8 | 8 | Author URI: http://radgeek.com/ |
| 9 | 9 | License: GPL |
| 10 | +Last modified: 2008-12-14 4:29pm PST | |
| 10 | 11 | */ |
| 11 | 12 | |
| 12 | 13 | # This uses code derived from: |
| 13 | 14 | # - wp-rss-aggregate.php by Kellan Elliot-McCrea <kellan@protest.net> |
| @@ -27,17 +28,13 @@ | ||
| 27 | 28 | # or use a cron job. |
| 28 | 29 | |
| 29 | 30 | # -- Don't change these unless you know what you're doing... |
| 30 | 31 | |
| 31 | -define ('FEEDWORDPRESS_VERSION', '2009.0612'); | |
| 32 | +define ('FEEDWORDPRESS_VERSION', '2008.1214'); | |
| 32 | 33 | define ('FEEDWORDPRESS_AUTHOR_CONTACT', 'http://radgeek.com/contact'); |
| 33 | 34 | define ('DEFAULT_SYNDICATION_CATEGORY', 'Contributors'); |
| 34 | 35 | |
| 35 | -$feedwordpress_debug = get_option('feedwordpress_debug'); | |
| 36 | -if (is_string($feedwordpress_debug)) : | |
| 37 | - $feedwordpress_debug = ($feedwordpress_debug == 'yes'); | |
| 38 | -endif; | |
| 39 | -define ('FEEDWORDPRESS_DEBUG', $feedwordpress_debug); | |
| 36 | +define ('FEEDWORDPRESS_DEBUG', false); | |
| 40 | 37 | |
| 41 | 38 | define ('FEEDWORDPRESS_CAT_SEPARATOR_PATTERN', '/[:\n]/'); |
| 42 | 39 | define ('FEEDWORDPRESS_CAT_SEPARATOR', "\n"); |
| 43 | 40 | |
| @@ -91,9 +88,8 @@ | ||
| 91 | 88 | endif; |
| 92 | 89 | endif; |
| 93 | 90 | |
| 94 | 91 | require_once(dirname(__FILE__) . '/compatability.php'); // LEGACY API: Replicate or mock up functions for legacy support purposes |
| 95 | -require_once(dirname(__FILE__) . '/feedwordpresshtml.class.php'); | |
| 96 | 92 | |
| 97 | 93 | // Magic quotes are just about the stupidest thing ever. |
| 98 | 94 | if (is_array($_POST)) : |
| 99 | 95 | $fwp_post = stripslashes_deep($_POST); |
| @@ -167,11 +163,9 @@ | ||
| 167 | 163 | remove_filter('pre_link_url', 'wp_filter_kses'); |
| 168 | 164 | |
| 169 | 165 | # Admin menu |
| 170 | 166 | add_action('admin_menu', 'fwp_add_pages'); |
| 171 | - add_action('admin_notices', 'fwp_check_debug'); | |
| 172 | - add_action('admin_notices', 'fwp_check_magpie'); | |
| 173 | - | |
| 167 | + | |
| 174 | 168 | # Inbound XML-RPC update methods |
| 175 | 169 | add_filter('xmlrpc_methods', 'feedwordpress_xmlrpc_hook'); |
| 176 | 170 | |
| 177 | 171 | # Outbound XML-RPC ping reform |
| @@ -195,12 +189,10 @@ | ||
| 195 | 189 | endif; |
| 196 | 190 | |
| 197 | 191 | # Cron-less auto-update. Hooray! |
| 198 | 192 | add_action('init', 'feedwordpress_auto_update'); |
| 199 | - add_action('init', 'feedwordpress_check_for_magpie_fix'); | |
| 200 | 193 | |
| 201 | 194 | # Default sanitizers |
| 202 | - add_filter('syndicated_item_content', array('SyndicatedPost', 'resolve_relative_uris'), 0, 2); | |
| 203 | 195 | add_filter('syndicated_item_content', array('SyndicatedPost', 'sanitize_content'), 0, 2); |
| 204 | 196 | |
| 205 | 197 | else : |
| 206 | 198 | # Hook in the menus, which will just point to the upgrade interface |
| @@ -206,33 +198,8 @@ | ||
| 206 | 198 | # Hook in the menus, which will just point to the upgrade interface |
| 207 | 199 | add_action('admin_menu', 'fwp_add_pages'); |
| 208 | 200 | endif; // if (!FeedWordPress::needs_upgrade()) |
| 209 | 201 | |
| 210 | -function feedwordpress_check_for_magpie_fix () { | |
| 211 | - if (isset($_POST['action']) and $_POST['action']=='fix_magpie_version') : | |
| 212 | - FeedWordPressCompatibility::validate_http_request(/*action=*/ 'feedwordpress_fix_magpie', /*capability=*/ 'edit_files'); | |
| 213 | - | |
| 214 | - $back_to = $_SERVER['REQUEST_URI']; | |
| 215 | - if (isset($_POST['ignore'])) : | |
| 216 | - // kill error message by telling it we ignored the upgrade request for this version | |
| 217 | - update_option('feedwordpress_magpie_ignored_upgrade_to', EXPECTED_MAGPIE_VERSION); | |
| 218 | - $ret = 'ignored'; | |
| 219 | - elseif (isset($_POST['upgrade'])) : | |
| 220 | - $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss.php"; | |
| 221 | - $destination = ABSPATH . WPINC . '/rss.php'; | |
| 222 | - $success = @copy($source, $destination); | |
| 223 | - $ret = (int) $success; | |
| 224 | - endif; | |
| 225 | - | |
| 226 | - if (strpos($back_to, '?')===false) : $sep = '?'; | |
| 227 | - else : $sep = '&'; | |
| 228 | - endif; | |
| 229 | - | |
| 230 | - header("Location: {$back_to}{$sep}feedwordpress_magpie_fix=".$ret); | |
| 231 | - exit; | |
| 232 | - endif; | |
| 233 | -} /* feedwordpress_check_for_magpie_fix() */ | |
| 234 | - | |
| 235 | 202 | function feedwordpress_auto_update () { |
| 236 | 203 | if (FeedWordPress::stale()) : |
| 237 | 204 | $feedwordpress =& new FeedWordPress; |
| 238 | 205 | $feedwordpress->update(); |
| @@ -396,13 +363,9 @@ | ||
| 396 | 363 | |
| 397 | 364 | function feedwordpress_preserve_syndicated_content ($text) { |
| 398 | 365 | global $feedwordpress_the_syndicated_content; |
| 399 | 366 | |
| 400 | - $globalExpose = (get_option('feedwordpress_formatting_filters') == 'yes'); | |
| 401 | - $localExpose = get_post_custom_values('_feedwordpress_formatting_filters'); | |
| 402 | - $expose = ($globalExpose or ((count($localExpose) > 0) and $localExpose[0])); | |
| 403 | - | |
| 404 | - if ( is_syndicated() and !$expose ) : | |
| 367 | + if ( is_syndicated() and get_option('feedwordpress_formatting_filters') != 'yes' ) : | |
| 405 | 368 | $feedwordpress_the_syndicated_content = $text; |
| 406 | 369 | else : |
| 407 | 370 | $feedwordpress_the_syndicated_content = NULL; |
| 408 | 371 | endif; |
| @@ -476,127 +439,14 @@ | ||
| 476 | 439 | $longoptions = __('Syndication Options'); |
| 477 | 440 | endif; |
| 478 | 441 | |
| 479 | 442 | call_user_func_array('add_menu_page', $menu); |
| 480 | - add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Posts', 'Posts', $fwp_capability['manage_options'], $fwp_path.'/posts-page.php'); | |
| 481 | - add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Authors', 'Authors', $fwp_capability['manage_options'], $fwp_path.'/authors-page.php'); | |
| 482 | - add_submenu_page($fwp_path.'/syndication.php', 'Categories & Tags', 'Categories & Tags', $fwp_capability['manage_options'], $fwp_path.'/categories-page.php'); | |
| 443 | + add_submenu_page($fwp_path.'/syndication.php', 'Syndicated Authors', 'Authors', $fwp_capability['manage_options'], $fwp_path.'/authors.php'); | |
| 483 | 444 | add_submenu_page($fwp_path.'/syndication.php', $longoptions, $options, $fwp_capability['manage_options'], $fwp_path.'/syndication-options.php'); |
| 484 | 445 | |
| 485 | 446 | add_options_page($longoptions, 'Syndication', $fwp_capability['manage_options'], $fwp_path.'/syndication-options.php'); |
| 486 | -} /* function fwp_add_pages () */ | |
| 447 | +} // function fwp_add_pages () */ | |
| 487 | 448 | |
| 488 | -function fwp_check_debug () { | |
| 489 | - // This is a horrible fucking kludge that I have to do because the | |
| 490 | - // admin notice code is triggered before the code that updates the | |
| 491 | - // setting. | |
| 492 | - if (isset($_POST['feedwordpress_debug'])) : | |
| 493 | - $feedwordpress_debug = $_POST['feedwordpress_debug']; | |
| 494 | - else : | |
| 495 | - $feedwordpress_debug = get_option('feedwordpress_debug'); | |
| 496 | - endif; | |
| 497 | - if ($feedwordpress_debug==='yes') : | |
| 498 | -?> | |
| 499 | - <div class="error"> | |
| 500 | -<p><strong>FeedWordPress warning.</strong> Debugging mode is <strong>ON</strong>. | |
| 501 | -While it remains on, FeedWordPress displays many diagnostic error messages, | |
| 502 | -warnings, and notices that are ordinarily suppressed, and also turns off all | |
| 503 | -caching of feeds. Use with caution: this setting is absolutely inappropriate | |
| 504 | -for a production server.</p> | |
| 505 | - </div> | |
| 506 | -<?php | |
| 507 | - endif; | |
| 508 | -} /* function fwp_check_debug () */ | |
| 509 | - | |
| 510 | -define('EXPECTED_MAGPIE_VERSION', '2009.0618'); | |
| 511 | -function fwp_check_magpie () { | |
| 512 | - if (isset($_REQUEST['feedwordpress_magpie_fix'])) : | |
| 513 | - if ($_REQUEST['feedwordpress_magpie_fix']=='ignored') : | |
| 514 | -?> | |
| 515 | -<div class="updated fade"> | |
| 516 | -<p>O.K., we'll ignore the problem for now. FeedWordPress will not display any | |
| 517 | -more error messages.</p> | |
| 518 | -</div> | |
| 519 | -<?php | |
| 520 | - elseif ((bool) $_REQUEST['feedwordpress_magpie_fix']) : | |
| 521 | -?> | |
| 522 | -<div class="updated fade"> | |
| 523 | -<p>Congratulations! Your MagpieRSS has been successfully upgraded to the version | |
| 524 | -shipped with FeedWordPress.</p> | |
| 525 | -</div> | |
| 526 | -<?php | |
| 527 | - else : | |
| 528 | - $source = dirname(__FILE__)."/MagpieRSS-upgrade/rss.php"; | |
| 529 | - $destination = ABSPATH . WPINC . '/rss.php'; | |
| 530 | - $cmd = "cp '".htmlspecialchars(addslashes($source))."' '".htmlspecialchars(addslashes($destination))."'"; | |
| 531 | - $cmd = wordwrap($cmd, /*width=*/ 75, /*break=*/ " \\\n\t"); | |
| 532 | - | |
| 533 | -?> | |
| 534 | -<div class="error"> | |
| 535 | -<p><strong>FeedWordPress was unable to automatically upgrade your copy of MagpieRSS.</strong></p> | |
| 536 | -<p>It's likely that you need to change the file permissions on <code><?php print htmlspecialchars($source); ?></code> | |
| 537 | -to allow FeedWordPress to overwrite it.</p> | |
| 538 | -<p><strong>To perform the upgrade manually,</strong> you can | |
| 539 | -use a SFTP or FTP client to upload a copy of <code>rss.php</code> from the | |
| 540 | -<code>MagpieRSS-upgrades/</code> directory of your FeedWordPress archive so that | |
| 541 | -it overwrites <code><?php print htmlspecialchars($destination); ?></code>. Or, | |
| 542 | -if your web host provides shell access, you can issue the following command from | |
| 543 | -a command prompt to perform the upgrade:</p> | |
| 544 | -<pre> | |
| 545 | -<samp>$</samp> <kbd><?php print $cmd; ?></kbd> | |
| 546 | -</pre> | |
| 547 | -<p><strong>If you've fixed the file permissions,</strong> you can try the | |
| 548 | -automatic upgrade again.</p> | |
| 549 | - | |
| 550 | -<?php feedwordpress_upgrade_old_and_busted_buttons(); ?> | |
| 551 | -</div> | |
| 552 | -<?php | |
| 553 | - endif; | |
| 554 | - else : | |
| 555 | - $magpie_version = FeedWordPress::magpie_version(); | |
| 556 | - | |
| 557 | - $ignored = get_option('feedwordpress_magpie_ignored_upgrade_to'); | |
| 558 | - if (EXPECTED_MAGPIE_VERSION != $magpie_version and EXPECTED_MAGPIE_VERSION != $ignored) : | |
| 559 | - if (current_user_can('edit_files')) : | |
| 560 | - $youAre = 'you are'; | |
| 561 | - $itIsRecommendedThatYou = 'It is <strong>strongly recommended</strong> that you'; | |
| 562 | - else : | |
| 563 | - $youAre = 'this site is'; | |
| 564 | - $itIsRecommendedThatYou = 'You may want to contact the administrator of the site; it is <strong>strongly recommended</strong> that they'; | |
| 565 | - endif; | |
| 566 | - print '<div class="error">'; | |
| 567 | -?> | |
| 568 | -<p style="font-style: italic"><strong>FeedWordPress has detected that <?php print $youAre; ?> currently using a version of | |
| 569 | -MagpieRSS other than the upgraded version that ships with this version of FeedWordPress.</strong></p> | |
| 570 | -<ul> | |
| 571 | -<li><strong>Currently running:</strong> MagpieRSS <?php print $magpie_version; ?></li> | |
| 572 | -<li><strong>Version included with FeedWordPress <?php print FEEDWORDPRESS_VERSION; ?>:</strong> MagpieRSS <?php print EXPECTED_MAGPIE_VERSION; ?></li> | |
| 573 | -</ul> | |
| 574 | -<p><?php print $itIsRecommendedThatYou; ?> install the upgraded | |
| 575 | -version of MagpieRSS supplied with FeedWordPress. The version of | |
| 576 | -MagpieRSS that ships with WordPress is very old and buggy, and | |
| 577 | -encounters a number of errors when trying to parse modern Atom | |
| 578 | -and RSS feeds.</p> | |
| 579 | -<?php | |
| 580 | - feedwordpress_upgrade_old_and_busted_buttons(); | |
| 581 | - print '</div>'; | |
| 582 | - endif; | |
| 583 | - endif; | |
| 584 | -} | |
| 585 | - | |
| 586 | -function feedwordpress_upgrade_old_and_busted_buttons() { | |
| 587 | - if (current_user_can('edit_files')) : | |
| 588 | -?> | |
| 589 | -<form action="" method="post"><div> | |
| 590 | -<?php FeedWordPressCompatibility::stamp_nonce('feedwordpress_fix_magpie'); ?> | |
| 591 | -<input type="hidden" name="action" value="fix_magpie_version" /> | |
| 592 | -<input class="button-secondary" type="submit" name="ignore" value="<?php _e('Ignore this problem'); ?>" /> | |
| 593 | -<input class="button-primary" type="submit" name="upgrade" value="<?php _e('Upgrade'); ?>" /> | |
| 594 | -</div></form> | |
| 595 | -<?php | |
| 596 | - endif; | |
| 597 | -} | |
| 598 | - | |
| 599 | 449 | ################################################################################ |
| 600 | 450 | ## fwp_hold_pings() and fwp_release_pings(): Outbound XML-RPC ping reform #### |
| 601 | 451 | ## ... 'coz it's rude to send 500 pings the first time your aggregator runs #### |
| 602 | 452 | ################################################################################ |
| @@ -950,12 +800,12 @@ | ||
| 950 | 800 | // WordPress 2.3 introduces a new taxonomy/term API |
| 951 | 801 | if (function_exists('is_term')) : |
| 952 | 802 | $cat_id = is_term($cat, 'link_category'); |
| 953 | 803 | // WordPress 2.1 and 2.2 use a common table for both link and post categories |
| 954 | - elseif (fwp_test_wp_version(FWP_SCHEMA_21, FWP_SCHEMA_23)) : | |
| 804 | + elseif (isset($wp_db_version) and ($wp_db_version >= FWP_SCHEMA_21 and $wp_db_version < FWP_SCHEMA_23) ) : | |
| 955 | 805 | $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->categories} WHERE cat_name='$cat'"); |
| 956 | 806 | // WordPress 1.5 and 2.0.x have a separate table for link categories |
| 957 | - elseif (fwp_test_wp_version(0, FWP_SCHEMA_21)): | |
| 807 | + elseif (!isset($wp_db_version) or $wp_db_version < FWP_SCHEMA_21) : | |
| 958 | 808 | $cat_id = $wpdb->get_var("SELECT cat_id FROM {$wpdb->linkcategories} WHERE cat_name='$cat'"); |
| 959 | 809 | // This should never happen. |
| 960 | 810 | else : |
| 961 | 811 | FeedWordPress::critical_bug('FeedWordPress::link_category_id::wp_db_version', $wp_db_version, __LINE__); |
| @@ -1091,15 +941,8 @@ | ||
| 1091 | 941 | CREATE INDEX {$wpdb->posts}_guid_idx ON {$wpdb->posts}(guid) |
| 1092 | 942 | "); |
| 1093 | 943 | } /* FeedWordPress::create_guid_index () */ |
| 1094 | 944 | |
| 1095 | - function magpie_version () { | |
| 1096 | - if (!defined('MAGPIE_VERSION')) : $magpie_version = $GLOBALS['wp_version'].'-default'; | |
| 1097 | - else : $magpie_version = MAGPIE_VERSION; | |
| 1098 | - endif; | |
| 1099 | - return $magpie_version; | |
| 1100 | - } | |
| 1101 | - | |
| 1102 | 945 | # Utility functions for handling text settings |
| 1103 | 946 | function negative ($f, $setting) { |
| 1104 | 947 | $nego = array ('n', 'no', 'f', 'false'); |
| 1105 | 948 | return (isset($f[$setting]) and in_array(strtolower($f[$setting]), $nego)); |
| @@ -1114,18 +957,13 @@ | ||
| 1114 | 957 | # Internal debugging functions |
| 1115 | 958 | function critical_bug ($varname, $var, $line) { |
| 1116 | 959 | global $wp_version; |
| 1117 | 960 | |
| 1118 | - if (defined('MAGPIE_VERSION')) : $mv = MAGPIE_VERSION; | |
| 1119 | - else : $mv = 'WordPress '.$wp_version.' default.'; | |
| 1120 | - endif; | |
| 1121 | - | |
| 1122 | 961 | echo '<p>There may be a bug in FeedWordPress. Please <a href="'.FEEDWORDPRESS_AUTHOR_CONTACT.'">contact the author</a> and paste the following information into your e-mail:</p>'; |
| 1123 | 962 | echo "\n<plaintext>"; |
| 1124 | 963 | echo "Triggered at line # ".$line."\n"; |
| 1125 | 964 | echo "FeedWordPress version: ".FEEDWORDPRESS_VERSION."\n"; |
| 1126 | - echo "MagpieRSS version: {$mv}\n"; | |
| 1127 | - echo "WordPress version: {$wp_version}\n"; | |
| 965 | + echo "WordPress version: $wp_version\n"; | |
| 1128 | 966 | echo "PHP version: ".phpversion()."\n"; |
| 1129 | 967 | echo "\n"; |
| 1130 | 968 | echo $varname.": "; var_dump($var); echo "\n"; |
| 1131 | 969 | die; |
| @@ -1145,8 +983,9 @@ | ||
| 1145 | 983 | var $feed = null; |
| 1146 | 984 | var $feedmeta = null; |
| 1147 | 985 | |
| 1148 | 986 | var $post = array (); |
| 987 | + var $_base = null; | |
| 1149 | 988 | |
| 1150 | 989 | var $_freshness = null; |
| 1151 | 990 | var $_wp_id = null; |
| 1152 | 991 | |
| @@ -1190,11 +1029,9 @@ | ||
| 1190 | 1029 | $this->post['named']['author'] = apply_filters('syndicated_item_author', $this->author(), $this); |
| 1191 | 1030 | |
| 1192 | 1031 | # Identify content and sanitize it. |
| 1193 | 1032 | # --------------------------------- |
| 1194 | - if (isset($this->item['atom_content'])) : | |
| 1195 | - $content = $this->item['atom_content']; | |
| 1196 | - elseif (isset($this->item['xhtml']['body'])) : | |
| 1033 | + if (isset($this->item['xhtml']['body'])) : | |
| 1197 | 1034 | $content = $this->item['xhtml']['body']; |
| 1198 | 1035 | elseif (isset($this->item['xhtml']['div'])) : |
| 1199 | 1036 | $content = $this->item['xhtml']['div']; |
| 1200 | 1037 | elseif (isset($this->item['content']['encoded']) and $this->item['content']['encoded']): |
| @@ -1243,26 +1080,8 @@ | ||
| 1243 | 1080 | |
| 1244 | 1081 | // Unique ID (hopefully a unique tag: URI); failing that, the permalink |
| 1245 | 1082 | $this->post['guid'] = apply_filters('syndicated_item_guid', $this->guid(), $this); |
| 1246 | 1083 | |
| 1247 | - // User-supplied custom settings to apply to each post. Do first so that FWP-generated custom settings will overwrite if necessary; thus preventing any munging | |
| 1248 | - $default_custom_settings = get_option('feedwordpress_custom_settings'); | |
| 1249 | - if ($default_custom_settings) : | |
| 1250 | - $default_custom_settings = unserialize($default_custom_settings); | |
| 1251 | - endif; | |
| 1252 | - if (!is_array($default_custom_settings)) : | |
| 1253 | - $default_custom_settings = array(); | |
| 1254 | - endif; | |
| 1255 | - | |
| 1256 | - $custom_settings = (isset($this->link->settings['postmeta']) ? $this->link->settings['postmeta'] : null); | |
| 1257 | - if ($custom_settings) : | |
| 1258 | - $custom_settings = unserialize($custom_settings); | |
| 1259 | - endif; | |
| 1260 | - if (!is_array($custom_settings)) : | |
| 1261 | - $custom_settings = array(); | |
| 1262 | - endif; | |
| 1263 | - $this->post['meta'] = array_merge($default_custom_settings, $custom_settings); | |
| 1264 | - | |
| 1265 | 1084 | // RSS 2.0 / Atom 1.0 enclosure support |
| 1266 | 1085 | if ( isset($this->item['enclosure#']) ) : |
| 1267 | 1086 | for ($i = 1; $i <= $this->item['enclosure#']; $i++) : |
| 1268 | 1087 | $eid = (($i > 1) ? "#{$id}" : ""); |
| @@ -1317,10 +1136,10 @@ | ||
| 1317 | 1136 | // Store a hash of the post content for checking whether something needs to be updated |
| 1318 | 1137 | $this->post['meta']['syndication_item_hash'] = $this->update_hash(); |
| 1319 | 1138 | |
| 1320 | 1139 | // Feed-by-feed options for author and category creation |
| 1321 | - $this->post['named']['unfamiliar']['author'] = (isset($this->feedmeta['unfamiliar author']) ? $this->feedmeta['unfamiliar author'] : null); | |
| 1322 | - $this->post['named']['unfamiliar']['category'] = (isset($this->feedmeta['unfamiliar category']) ? $this->feedmeta['unfamiliar category'] : null); | |
| 1140 | + $this->post['named']['unfamiliar']['author'] = $this->feedmeta['unfamiliar author']; | |
| 1141 | + $this->post['named']['unfamiliar']['category'] = $this->feedmeta['unfamiliar category']; | |
| 1323 | 1142 | |
| 1324 | 1143 | // Categories: start with default categories, if any |
| 1325 | 1144 | $fc = get_option("feedwordpress_syndication_cats"); |
| 1326 | 1145 | if ($fc) : |
| @@ -1328,9 +1147,9 @@ | ||
| 1328 | 1147 | else : |
| 1329 | 1148 | $this->post['named']['preset/category'] = array(); |
| 1330 | 1149 | endif; |
| 1331 | 1150 | |
| 1332 | - if (isset($this->feedmeta['cats']) and is_array($this->feedmeta['cats'])) : | |
| 1151 | + if (is_array($this->feedmeta['cats'])) : | |
| 1333 | 1152 | $this->post['named']['preset/category'] = array_merge($this->post['named']['preset/category'], $this->feedmeta['cats']); |
| 1334 | 1153 | endif; |
| 1335 | 1154 | |
| 1336 | 1155 | // Now add categories from the post, if we have 'em |
| @@ -1357,9 +1176,9 @@ | ||
| 1357 | 1176 | else : |
| 1358 | 1177 | $this->post['tags_input'] = array(); |
| 1359 | 1178 | endif; |
| 1360 | 1179 | |
| 1361 | - if (isset($this->feedmeta['tags']) and is_array($this->feedmeta['tags'])) : | |
| 1180 | + if (is_array($this->feedmeta['tags'])) : | |
| 1362 | 1181 | $this->post['tags_input'] = array_merge($this->post['tags_input'], $this->feedmeta['tags']); |
| 1363 | 1182 | endif; |
| 1364 | 1183 | |
| 1365 | 1184 | endif; |
| @@ -1503,223 +1322,187 @@ | ||
| 1503 | 1322 | } // function SyndicatedPost::store () |
| 1504 | 1323 | |
| 1505 | 1324 | function insert_new () { |
| 1506 | 1325 | global $wpdb, $wp_db_version; |
| 1507 | - | |
| 1508 | - $dbpost = $this->normalize_post(/*new=*/ true); | |
| 1509 | - if (!is_null($dbpost)) : | |
| 1510 | - if ($this->use_api('wp_insert_post')) : | |
| 1511 | - $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 1512 | - | |
| 1513 | - // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 1514 | - add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1515 | - | |
| 1516 | - // Kludge to prevent kses filters from stripping the | |
| 1517 | - // content of posts when updating without a logged in | |
| 1518 | - // user who has `unfiltered_html` capability. | |
| 1519 | - add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1520 | - | |
| 1521 | - $this->_wp_id = wp_insert_post($dbpost); | |
| 1522 | - | |
| 1523 | - // Turn off ridiculous fucking kludges #1 and #2 | |
| 1524 | - remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1525 | - remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1526 | - | |
| 1527 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 1528 | - | |
| 1529 | - // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 1530 | - // *still* offers no way to use a guid of your choice, | |
| 1531 | - // and munges your post modified timestamp, too. | |
| 1532 | - $result = $wpdb->query(" | |
| 1533 | - UPDATE $wpdb->posts | |
| 1534 | - SET | |
| 1535 | - guid='{$dbpost['guid']}', | |
| 1536 | - post_modified='{$dbpost['post_modified']}', | |
| 1537 | - post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 1538 | - WHERE ID='{$this->_wp_id}' | |
| 1539 | - "); | |
| 1326 | + | |
| 1327 | + // Why the fuck doesn't wp_insert_post already do this? | |
| 1328 | + foreach ($this->post as $key => $value) : | |
| 1329 | + if (is_string($value)) : | |
| 1330 | + $dbpost[$key] = $wpdb->escape($value); | |
| 1540 | 1331 | else : |
| 1541 | - # The right way to do this is the above. But, alas, | |
| 1542 | - # in earlier versions of WordPress, wp_insert_post has | |
| 1543 | - # too much behavior (mainly related to pings) that can't | |
| 1544 | - # be overridden. In WordPress 1.5, it's enough of a | |
| 1545 | - # resource hog to make PHP segfault after inserting | |
| 1546 | - # 50-100 posts. This can get pretty annoying, especially | |
| 1547 | - # if you are trying to update your feeds for the first | |
| 1548 | - # time. | |
| 1549 | - | |
| 1550 | - $result = $wpdb->query(" | |
| 1551 | - INSERT INTO $wpdb->posts | |
| 1552 | - SET | |
| 1553 | - guid = '{$dbpost['guid']}', | |
| 1554 | - post_author = '{$dbpost['post_author']}', | |
| 1555 | - post_date = '{$dbpost['post_date']}', | |
| 1556 | - post_date_gmt = '{$dbpost['post_date_gmt']}', | |
| 1557 | - post_content = '{$dbpost['post_content']}'," | |
| 1558 | - .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 1559 | - post_title = '{$dbpost['post_title']}', | |
| 1560 | - post_name = '{$dbpost['post_name']}', | |
| 1561 | - post_modified = '{$dbpost['post_modified']}', | |
| 1562 | - post_modified_gmt = '{$dbpost['post_modified_gmt']}', | |
| 1563 | - comment_status = '{$dbpost['comment_status']}', | |
| 1564 | - ping_status = '{$dbpost['ping_status']}', | |
| 1565 | - post_status = '{$dbpost['post_status']}' | |
| 1566 | - "); | |
| 1567 | - $this->_wp_id = $wpdb->insert_id; | |
| 1568 | - | |
| 1569 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 1570 | - | |
| 1571 | - // WordPress 1.5.x - 2.0.x | |
| 1572 | - wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 1573 | - | |
| 1574 | - // Since we are not going through official channels, we need to | |
| 1575 | - // manually tell WordPress that we've published a new post. | |
| 1576 | - // We need to make sure to do this in order for FeedWordPress | |
| 1577 | - // to play well with the staticize-reloaded plugin (something | |
| 1578 | - // that a large aggregator website is going to *want* to be | |
| 1579 | - // able to use). | |
| 1580 | - do_action('publish_post', $this->_wp_id); | |
| 1332 | + $dbpost[$key] = $value; | |
| 1581 | 1333 | endif; |
| 1334 | + endforeach; | |
| 1335 | + | |
| 1336 | + if (strlen($dbpost['post_title'].$dbpost['post_content'].$dbpost['post_excerpt']) == 0) : | |
| 1337 | + // FIXME: Option for filtering out empty posts | |
| 1582 | 1338 | endif; |
| 1583 | - } /* SyndicatedPost::insert_new() */ | |
| 1339 | + if (strlen($dbpost['post_title'])==0) : | |
| 1340 | + $dbpost['post_title'] = $this->post['meta']['syndication_source'] | |
| 1341 | + .' '.gmdate('Y-m-d H:i:s', $this->published() + $offset); | |
| 1342 | + // FIXME: Option for what to fill a blank title with... | |
| 1343 | + endif; | |
| 1584 | 1344 | |
| 1585 | - function update_existing () { | |
| 1586 | - global $wpdb; | |
| 1345 | + if ($this->use_api('wp_insert_post')) : | |
| 1346 | + $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 1587 | 1347 | |
| 1588 | - // Why the fuck doesn't wp_insert_post already do this? | |
| 1589 | - $dbpost = $this->normalize_post(/*new=*/ false); | |
| 1590 | - if (!is_null($dbpost)) : | |
| 1591 | - if ($this->use_api('wp_insert_post')) : | |
| 1592 | - $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 1593 | - | |
| 1594 | - // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 1595 | - add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1596 | - | |
| 1597 | - // Kludge to prevent kses filters from stripping the | |
| 1598 | - // content of posts when updating without a logged in | |
| 1599 | - // user who has `unfiltered_html` capability. | |
| 1600 | - add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1348 | + // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 1349 | + add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1350 | + | |
| 1351 | + // Kludge to prevent kses filters from stripping the | |
| 1352 | + // content of posts when updating without a logged in | |
| 1353 | + // user who has `unfiltered_html` capability. | |
| 1354 | + add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1355 | + | |
| 1356 | + $this->_wp_id = wp_insert_post($dbpost); | |
| 1601 | 1357 | |
| 1602 | - // Don't munge status fields that the user may have reset manually | |
| 1603 | - if (function_exists('get_post_field')) : | |
| 1604 | - $doNotMunge = array('post_status', 'comment_status', 'ping_status'); | |
| 1605 | - foreach ($doNotMunge as $field) : | |
| 1606 | - $dbpost[$field] = get_post_field($field, $this->wp_id()); | |
| 1607 | - endforeach; | |
| 1608 | - endif; | |
| 1358 | + // Turn off ridiculous fucking kludges #1 and #2 | |
| 1359 | + remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1360 | + remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1609 | 1361 | |
| 1610 | - $this->_wp_id = wp_insert_post($dbpost); | |
| 1362 | + // This should never happen. | |
| 1363 | + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) : | |
| 1364 | + FeedWordPress::critical_bug('SyndicatedPost (_wp_id problem)', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1365 | + endif; | |
| 1611 | 1366 | |
| 1612 | - // Turn off ridiculous fucking kludges #1 and #2 | |
| 1613 | - remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1614 | - remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1615 | - | |
| 1616 | - $this->validate_post_id($dbpost, array(__CLASS__, __FUNCTION__)); | |
| 1617 | - | |
| 1618 | - // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 1619 | - // munges your post modified timestamp. | |
| 1620 | - $result = $wpdb->query(" | |
| 1621 | - UPDATE $wpdb->posts | |
| 1622 | - SET | |
| 1623 | - post_modified='{$dbpost['post_modified']}', | |
| 1624 | - post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 1625 | - WHERE ID='{$this->_wp_id}' | |
| 1626 | - "); | |
| 1627 | - else : | |
| 1628 | - | |
| 1629 | - $result = $wpdb->query(" | |
| 1367 | + // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 1368 | + // *still* offers no way to use a guid of your choice, | |
| 1369 | + // and munges your post modified timestamp, too. | |
| 1370 | + $result = $wpdb->query(" | |
| 1630 | 1371 | UPDATE $wpdb->posts |
| 1631 | 1372 | SET |
| 1632 | - post_author = '{$dbpost['post_author']}', | |
| 1633 | - post_content = '{$dbpost['post_content']}'," | |
| 1634 | - .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 1635 | - post_title = '{$dbpost['post_title']}', | |
| 1636 | - post_name = '{$dbpost['post_name']}', | |
| 1637 | - post_modified = '{$dbpost['post_modified']}', | |
| 1638 | - post_modified_gmt = '{$dbpost['post_modified_gmt']}' | |
| 1639 | - WHERE guid='{$dbpost['guid']}' | |
| 1640 | - "); | |
| 1641 | - | |
| 1642 | - // WordPress 2.1.x and up | |
| 1643 | - if (function_exists('wp_set_post_categories')) : | |
| 1644 | - wp_set_post_categories($this->wp_id(), $this->post['post_category']); | |
| 1645 | - // WordPress 1.5.x - 2.0.x | |
| 1646 | - elseif (function_exists('wp_set_post_cats')) : | |
| 1647 | - wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 1648 | - // This should never happen. | |
| 1649 | - else : | |
| 1650 | - FeedWordPress::critical_bug(__CLASS__.'::'.__FUNCTION.'(): no post categorizing function', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1651 | - endif; | |
| 1652 | - | |
| 1653 | - // Since we are not going through official channels, we need to | |
| 1654 | - // manually tell WordPress that we've published a new post. | |
| 1655 | - // We need to make sure to do this in order for FeedWordPress | |
| 1656 | - // to play well with the staticize-reloaded plugin (something | |
| 1657 | - // that a large aggregator website is going to *want* to be | |
| 1658 | - // able to use). | |
| 1659 | - do_action('edit_post', $this->post['ID']); | |
| 1373 | + guid='{$dbpost['guid']}', | |
| 1374 | + post_modified='{$dbpost['post_modified']}', | |
| 1375 | + post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 1376 | + WHERE ID='{$this->_wp_id}' | |
| 1377 | + "); | |
| 1378 | + else : | |
| 1379 | + # The right way to do this is the above. But, alas, | |
| 1380 | + # in earlier versions of WordPress, wp_insert_post has | |
| 1381 | + # too much behavior (mainly related to pings) that can't | |
| 1382 | + # be overridden. In WordPress 1.5, it's enough of a | |
| 1383 | + # resource hog to make PHP segfault after inserting | |
| 1384 | + # 50-100 posts. This can get pretty annoying, especially | |
| 1385 | + # if you are trying to update your feeds for the first | |
| 1386 | + # time. | |
| 1387 | + | |
| 1388 | + $result = $wpdb->query(" | |
| 1389 | + INSERT INTO $wpdb->posts | |
| 1390 | + SET | |
| 1391 | + guid = '{$dbpost['guid']}', | |
| 1392 | + post_author = '{$dbpost['post_author']}', | |
| 1393 | + post_date = '{$dbpost['post_date']}', | |
| 1394 | + post_date_gmt = '{$dbpost['post_date_gmt']}', | |
| 1395 | + post_content = '{$dbpost['post_content']}'," | |
| 1396 | + .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 1397 | + post_title = '{$dbpost['post_title']}', | |
| 1398 | + post_name = '{$dbpost['post_name']}', | |
| 1399 | + post_modified = '{$dbpost['post_modified']}', | |
| 1400 | + post_modified_gmt = '{$dbpost['post_modified_gmt']}', | |
| 1401 | + comment_status = '{$dbpost['comment_status']}', | |
| 1402 | + ping_status = '{$dbpost['ping_status']}', | |
| 1403 | + post_status = '{$dbpost['post_status']}' | |
| 1404 | + "); | |
| 1405 | + $this->_wp_id = $wpdb->insert_id; | |
| 1406 | + | |
| 1407 | + // This should never happen. | |
| 1408 | + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) : | |
| 1409 | + FeedWordPress::critical_bug('SyndicatedPost (_wp_id problem)', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1660 | 1410 | endif; |
| 1411 | + | |
| 1412 | + // WordPress 1.5.x - 2.0.x | |
| 1413 | + wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 1414 | + | |
| 1415 | + // Since we are not going through official channels, we need to | |
| 1416 | + // manually tell WordPress that we've published a new post. | |
| 1417 | + // We need to make sure to do this in order for FeedWordPress | |
| 1418 | + // to play well with the staticize-reloaded plugin (something | |
| 1419 | + // that a large aggregator website is going to *want* to be | |
| 1420 | + // able to use). | |
| 1421 | + do_action('publish_post', $this->_wp_id); | |
| 1661 | 1422 | endif; |
| 1662 | - } /* SyndicatedPost::update_existing() */ | |
| 1423 | + } /* SyndicatedPost::insert_new() */ | |
| 1663 | 1424 | |
| 1664 | - /** | |
| 1665 | - * SyndicatedPost::normalize_post() | |
| 1666 | - * | |
| 1667 | - * @param bool $new If true, this post is to be inserted anew. If false, it is an update of an existing post. | |
| 1668 | - * @return array A normalized representation of the post ready to be inserted into the database or sent to the WordPress API functions | |
| 1669 | - */ | |
| 1670 | - function normalize_post ($new = true) { | |
| 1425 | + function update_existing () { | |
| 1671 | 1426 | global $wpdb; |
| 1672 | 1427 | |
| 1673 | - $out = array(); | |
| 1674 | - | |
| 1675 | 1428 | // Why the fuck doesn't wp_insert_post already do this? |
| 1429 | + $dbpost = array(); | |
| 1676 | 1430 | foreach ($this->post as $key => $value) : |
| 1677 | 1431 | if (is_string($value)) : |
| 1678 | - $out[$key] = $wpdb->escape($value); | |
| 1432 | + $dbpost[$key] = $wpdb->escape($value); | |
| 1679 | 1433 | else : |
| 1680 | - $out[$key] = $value; | |
| 1434 | + $dbpost[$key] = $value; | |
| 1681 | 1435 | endif; |
| 1682 | 1436 | endforeach; |
| 1683 | 1437 | |
| 1684 | - if (strlen($out['post_title'].$out['post_content'].$out['post_excerpt']) == 0) : | |
| 1685 | - // FIXME: Option for filtering out empty posts | |
| 1686 | - endif; | |
| 1687 | - if (strlen($out['post_title'])==0) : | |
| 1688 | - $offset = (int) get_option('gmt_offset') * 60 * 60; | |
| 1689 | - $out['post_title'] = | |
| 1690 | - $this->post['meta']['syndication_source'] | |
| 1691 | - .' '.gmdate('Y-m-d H:i:s', $this->published() + $offset); | |
| 1692 | - // FIXME: Option for what to fill a blank title with... | |
| 1693 | - endif; | |
| 1438 | + if ($this->use_api('wp_insert_post')) : | |
| 1439 | + $dbpost['post_pingback'] = false; // Tell WP 2.1 and 2.2 not to process for pingbacks | |
| 1694 | 1440 | |
| 1695 | - return $out; | |
| 1696 | - } | |
| 1441 | + // This is a ridiculous fucking kludge necessitated by WordPress 2.6 munging authorship meta-data | |
| 1442 | + add_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1697 | 1443 | |
| 1698 | - /** | |
| 1699 | - * SyndicatedPost::validate_post_id() | |
| 1700 | - * | |
| 1701 | - * @param array $dbpost An array representing the post we attempted to insert or update | |
| 1702 | - * @param mixed $ns A string or array representing the namespace (class, method) whence this method was called. | |
| 1703 | - */ | |
| 1704 | - function validate_post_id ($dbpost, $ns) { | |
| 1705 | - if (is_array($ns)) : $ns = implode('::', $ns); | |
| 1706 | - else : $ns = (string) $ns; endif; | |
| 1707 | - | |
| 1708 | - // This should never happen. | |
| 1709 | - if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) : | |
| 1710 | - FeedWordPress::critical_bug( | |
| 1711 | - /*name=*/ $ns.'::_wp_id', | |
| 1712 | - /*var =*/ array( | |
| 1713 | - "\$this->_wp_id" => $this->_wp_id, | |
| 1714 | - "\$dbpost" => $dbpost, | |
| 1715 | - "\$this" => $this | |
| 1716 | - ), | |
| 1717 | - /*line # =*/ __LINE__ | |
| 1718 | - ); | |
| 1444 | + // Kludge to prevent kses filters from stripping the | |
| 1445 | + // content of posts when updating without a logged in | |
| 1446 | + // user who has `unfiltered_html` capability. | |
| 1447 | + add_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1448 | + | |
| 1449 | + $this->_wp_id = wp_insert_post($dbpost); | |
| 1450 | + | |
| 1451 | + // Turn off ridiculous fucking kludges #1 and #2 | |
| 1452 | + remove_action('_wp_put_post_revision', array($this, 'fix_revision_meta')); | |
| 1453 | + remove_filter('content_save_pre', array($this, 'avoid_kses_munge'), 11); | |
| 1454 | + | |
| 1455 | + // This should never happen. | |
| 1456 | + if (!is_numeric($this->_wp_id) or ($this->_wp_id == 0)) : | |
| 1457 | + FeedWordPress::critical_bug('SyndicatedPost (_wp_id problem)', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1458 | + endif; | |
| 1459 | + | |
| 1460 | + // Unfortunately, as of WordPress 2.3, wp_insert_post() | |
| 1461 | + // munges your post modified timestamp. | |
| 1462 | + $result = $wpdb->query(" | |
| 1463 | + UPDATE $wpdb->posts | |
| 1464 | + SET | |
| 1465 | + post_modified='{$dbpost['post_modified']}', | |
| 1466 | + post_modified_gmt='{$dbpost['post_modified_gmt']}' | |
| 1467 | + WHERE ID='{$this->_wp_id}' | |
| 1468 | + "); | |
| 1469 | + else : | |
| 1470 | + | |
| 1471 | + $result = $wpdb->query(" | |
| 1472 | + UPDATE $wpdb->posts | |
| 1473 | + SET | |
| 1474 | + post_author = '{$dbpost['post_author']}', | |
| 1475 | + post_content = '{$dbpost['post_content']}'," | |
| 1476 | + .(isset($dbpost['post_excerpt']) ? "post_excerpt = '{$dbpost['post_excerpt']}'," : "")." | |
| 1477 | + post_title = '{$dbpost['post_title']}', | |
| 1478 | + post_name = '{$dbpost['post_name']}', | |
| 1479 | + post_modified = '{$dbpost['post_modified']}', | |
| 1480 | + post_modified_gmt = '{$dbpost['post_modified_gmt']}' | |
| 1481 | + WHERE guid='{$dbpost['guid']}' | |
| 1482 | + "); | |
| 1483 | + | |
| 1484 | + // WordPress 2.1.x and up | |
| 1485 | + if (function_exists('wp_set_post_categories')) : | |
| 1486 | + wp_set_post_categories($this->wp_id(), $this->post['post_category']); | |
| 1487 | + // WordPress 1.5.x - 2.0.x | |
| 1488 | + elseif (function_exists('wp_set_post_cats')) : | |
| 1489 | + wp_set_post_cats('1', $this->wp_id(), $this->post['post_category']); | |
| 1490 | + // This should never happen. | |
| 1491 | + else : | |
| 1492 | + FeedWordPress::critical_bug('SyndicatedPost (_wp_id problem)', array("dbpost" => $dbpost, "this" => $this), __LINE__); | |
| 1493 | + endif; | |
| 1494 | + | |
| 1495 | + // Since we are not going through official channels, we need to | |
| 1496 | + // manually tell WordPress that we've published a new post. | |
| 1497 | + // We need to make sure to do this in order for FeedWordPress | |
| 1498 | + // to play well with the staticize-reloaded plugin (something | |
| 1499 | + // that a large aggregator website is going to *want* to be | |
| 1500 | + // able to use). | |
| 1501 | + do_action('edit_post', $this->post['ID']); | |
| 1719 | 1502 | endif; |
| 1720 | - } /* SyndicatedPost::validate_post_id() */ | |
| 1721 | - | |
| 1503 | + } /* SyndicatedPost::update_existing() */ | |
| 1504 | + | |
| 1722 | 1505 | /** |
| 1723 | 1506 | * SyndicatedPost::fix_revision_meta() - Fixes the way WP 2.6+ fucks up |
| 1724 | 1507 | * meta-data (authorship, etc.) when storing revisions of an updated |
| 1725 | 1508 | * syndicated post. |
| @@ -2244,87 +2027,25 @@ | ||
| 2244 | 2027 | |
| 2245 | 2028 | return $author; |
| 2246 | 2029 | } // SyndicatedPost::author() |
| 2247 | 2030 | |
| 2248 | - var $uri_attrs = array ( | |
| 2249 | - array('a', 'href'), | |
| 2250 | - array('applet', 'codebase'), | |
| 2251 | - array('area', 'href'), | |
| 2252 | - array('blockquote', 'cite'), | |
| 2253 | - array('body', 'background'), | |
| 2254 | - array('del', 'cite'), | |
| 2255 | - array('form', 'action'), | |
| 2256 | - array('frame', 'longdesc'), | |
| 2257 | - array('frame', 'src'), | |
| 2258 | - array('iframe', 'longdesc'), | |
| 2259 | - array('iframe', 'src'), | |
| 2260 | - array('head', 'profile'), | |
| 2261 | - array('img', 'longdesc'), | |
| 2262 | - array('img', 'src'), | |
| 2263 | - array('img', 'usemap'), | |
| 2264 | - array('input', 'src'), | |
| 2265 | - array('input', 'usemap'), | |
| 2266 | - array('ins', 'cite'), | |
| 2267 | - array('link', 'href'), | |
| 2268 | - array('object', 'classid'), | |
| 2269 | - array('object', 'codebase'), | |
| 2270 | - array('object', 'data'), | |
| 2271 | - array('object', 'usemap'), | |
| 2272 | - array('q', 'cite'), | |
| 2273 | - array('script', 'src') | |
| 2274 | - ); /* var SyndicatedPost::$uri_attrs */ | |
| 2275 | - | |
| 2276 | - var $_base = null; | |
| 2277 | - | |
| 2278 | - function resolve_single_relative_uri ($refs) { | |
| 2279 | - $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 2280 | - $url = Relative_URI::resolve($tag['value'], $this->_base); | |
| 2281 | - return $tag['prefix'] . $url . $tag['suffix']; | |
| 2282 | - } /* function SyndicatedPost::resolve_single_relative_uri() */ | |
| 2283 | - | |
| 2284 | - function resolve_relative_uris ($content, $obj) { | |
| 2285 | - # The MagpieRSS upgrade has some `xml:base` support baked in. | |
| 2286 | - # However, sometimes people do silly things, like putting | |
| 2287 | - # relative URIs out on a production RSS 2.0 feed or other feeds | |
| 2288 | - # with no good support for `xml:base`. So we'll do our best to | |
| 2289 | - # try to catch any remaining relative URIs and resolve them as | |
| 2290 | - # best we can. | |
| 2291 | - $obj->_base = $obj->item['link']; // Reset the base for resolving relative URIs | |
| 2292 | - | |
| 2293 | - foreach ($obj->uri_attrs as $pair) : | |
| 2294 | - list($tag, $attr) = $pair; | |
| 2295 | - $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 2296 | - $content = preg_replace_callback ( | |
| 2297 | - $pattern, | |
| 2298 | - array(&$obj, 'resolve_single_relative_uri'), | |
| 2299 | - $content | |
| 2300 | - ); | |
| 2301 | - endforeach; | |
| 2302 | - | |
| 2303 | - return $content; | |
| 2304 | - } /* function SyndicatedPost::resolve_relative_uris () */ | |
| 2305 | - | |
| 2306 | 2031 | var $strip_attrs = array ( |
| 2307 | - array('[a-z]+', 'target'), | |
| 2308 | -// array('[a-z]+', 'style'), | |
| 2309 | -// array('[a-z]+', 'on[a-z]+'), | |
| 2032 | + array('[a-z]+', 'style'), | |
| 2033 | + array('[a-z]+', 'target'), | |
| 2310 | 2034 | ); |
| 2311 | - | |
| 2312 | - function strip_attribute_from_tag ($refs) { | |
| 2313 | - $tag = FeedWordPressHTML::attributeMatch($refs); | |
| 2314 | - return $tag['before_attribute'].$tag['after_attribute']; | |
| 2315 | - } | |
| 2316 | - | |
| 2317 | 2035 | function sanitize_content ($content, $obj) { |
| 2036 | + # FeedWordPress used to resolve URIs relative to the | |
| 2037 | + # feed URI. It now relies on the xml:base support | |
| 2038 | + # baked in to the MagpieRSS upgrade. So all we do here | |
| 2039 | + # now is to sanitize problematic attributes. | |
| 2040 | + # | |
| 2318 | 2041 | # This kind of sucks. I intend to replace it with |
| 2319 | 2042 | # lib_filter sometime soon. |
| 2320 | 2043 | foreach ($obj->strip_attrs as $pair): |
| 2321 | 2044 | list($tag,$attr) = $pair; |
| 2322 | - $pattern = FeedWordPressHTML::attributeRegex($tag, $attr); | |
| 2323 | - | |
| 2324 | - $content = preg_replace_callback ( | |
| 2325 | - $pattern, | |
| 2326 | - array(&$obj, 'strip_attribute_from_tag'), | |
| 2045 | + $content = preg_replace ( | |
| 2046 | + ":(<$tag [^>]*)($attr=(\"[^\">]*\"|[^>\\s]+))([^>]*>):i", | |
| 2047 | + "\\1\\4", | |
| 2327 | 2048 | $content |
| 2328 | 2049 | ); |
| 2329 | 2050 | endforeach; |
| 2330 | 2051 | return $content; |
| @@ -2330,10 +2051,438 @@ | ||
| 2330 | 2051 | return $content; |
| 2331 | 2052 | } |
| 2332 | 2053 | } // class SyndicatedPost |
| 2333 | 2054 | |
| 2334 | -require_once(dirname(__FILE__) . '/syndicatedlink.class.php'); | |
| 2055 | +# class SyndicatedLink: represents a syndication feed stored within the | |
| 2056 | +# WordPress database | |
| 2057 | +# | |
| 2058 | +# To keep things compact and editable from within WordPress, we use all the | |
| 2059 | +# links under a particular category in the WordPress "Blogroll" for the list of | |
| 2060 | +# feeds to syndicate. "Contributors" is the category used by default; you can | |
| 2061 | +# configure that under Options --> Syndication. | |
| 2062 | +# | |
| 2063 | +# Fields used are: | |
| 2064 | +# | |
| 2065 | +# * link_rss: the URI of the Atom/RSS feed to syndicate | |
| 2066 | +# | |
| 2067 | +# * link_notes: user-configurable options, with keys and values | |
| 2068 | +# like so: | |
| 2069 | +# | |
| 2070 | +# key: value | |
| 2071 | +# cats: computers\nweb | |
| 2072 | +# feed/key: value | |
| 2073 | +# | |
| 2074 | +# Keys that start with "feed/" are gleaned from the data supplied | |
| 2075 | +# by the feed itself, and will be overwritten with each update. | |
| 2076 | +# | |
| 2077 | +# Values have linebreak characters escaped with C-style | |
| 2078 | +# backslashes (so, for example, a newline becomes "\n"). | |
| 2079 | +# | |
| 2080 | +# The value of `cats` is used as a newline-separated list of | |
| 2081 | +# default categories for any post coming from a particular feed. | |
| 2082 | +# (In the example above, any posts from this feed will be placed | |
| 2083 | +# in the "computers" and "web" categories--*in addition to* any | |
| 2084 | +# categories that may already be applied to the posts.) | |
| 2085 | +# | |
| 2086 | +# Values of keys in link_notes are accessible from templates using | |
| 2087 | +# the function `get_feed_meta($key)` if this plugin is activated. | |
| 2335 | 2088 | |
| 2089 | +class SyndicatedLink { | |
| 2090 | + var $id = null; | |
| 2091 | + var $link = null; | |
| 2092 | + var $settings = array (); | |
| 2093 | + var $magpie = null; | |
| 2094 | + | |
| 2095 | + function SyndicatedLink ($link) { | |
| 2096 | + global $wpdb; | |
| 2097 | + | |
| 2098 | + if (is_object($link)) : | |
| 2099 | + $this->link = $link; | |
| 2100 | + $this->id = $link->link_id; | |
| 2101 | + else : | |
| 2102 | + $this->id = $link; | |
| 2103 | + if (function_exists('get_bookmark')) : // WP 2.1+ | |
| 2104 | + $this->link = get_bookmark($link); | |
| 2105 | + else : | |
| 2106 | + $this->link = $wpdb->get_row(" | |
| 2107 | + SELECT * FROM $wpdb->links | |
| 2108 | + WHERE (link_id = '".$wpdb->escape($link)."')" | |
| 2109 | + ); | |
| 2110 | + endif; | |
| 2111 | + endif; | |
| 2112 | + | |
| 2113 | + if (strlen($this->link->link_rss) > 0) : | |
| 2114 | + // Read off feed settings from link_notes | |
| 2115 | + $notes = explode("\n", $this->link->link_notes); | |
| 2116 | + foreach ($notes as $note): | |
| 2117 | + list($key, $value) = explode(": ", $note, 2); | |
| 2118 | + | |
| 2119 | + if (strlen($key) > 0) : | |
| 2120 | + // Unescape and trim() off the whitespace. | |
| 2121 | + // Thanks to Ray Lischner for pointing out the | |
| 2122 | + // need to trim off whitespace. | |
| 2123 | + $this->settings[$key] = stripcslashes (trim($value)); | |
| 2124 | + endif; | |
| 2125 | + endforeach; | |
| 2126 | + | |
| 2127 | + // "Magic" feed settings | |
| 2128 | + $this->settings['link/uri'] = $this->link->link_rss; | |
| 2129 | + $this->settings['link/name'] = $this->link->link_name; | |
| 2130 | + $this->settings['link/id'] = $this->link->link_id; | |
| 2131 | + | |
| 2132 | + // `hardcode categories` and `unfamiliar categories` are deprecated in favor of `unfamiliar category` | |
| 2133 | + if ( | |
| 2134 | + isset($this->settings['unfamiliar categories']) | |
| 2135 | + and !isset($this->settings['unfamiliar category']) | |
| 2136 | + ) : | |
| 2137 | + $this->settings['unfamiliar category'] = $this->settings['unfamiliar categories']; | |
| 2138 | + endif; | |
| 2139 | + if ( | |
| 2140 | + FeedWordPress::affirmative($this->settings, 'hardcode categories') | |
| 2141 | + and !isset($this->settings['unfamiliar category']) | |
| 2142 | + ) : | |
| 2143 | + $this->settings['unfamiliar category'] = 'default'; | |
| 2144 | + endif; | |
| 2145 | + | |
| 2146 | + // Set this up automagically for del.icio.us | |
| 2147 | + $bits = parse_url($this->link->link_rss); | |
| 2148 | + $tagspacers = array('del.icio.us', 'feeds.delicious.com'); | |
| 2149 | + if (!isset($this->settings['cat_split']) and in_array($bits['host'], $tagspacers)) : | |
| 2150 | + $this->settings['cat_split'] = '\s'; // Whitespace separates multiple tags in del.icio.us RSS feeds | |
| 2151 | + endif; | |
| 2152 | + | |
| 2153 | + if (isset($this->settings['cats'])): | |
| 2154 | + $this->settings['cats'] = preg_split(FEEDWORDPRESS_CAT_SEPARATOR_PATTERN, $this->settings['cats']); | |
| 2155 | + endif; | |
| 2156 | + if (isset($this->settings['tags'])): | |
| 2157 | + $this->settings['tags'] = preg_split(FEEDWORDPRESS_CAT_SEPARATOR_PATTERN, $this->settings['tags']); | |
| 2158 | + endif; | |
| 2159 | + | |
| 2160 | + if (isset($this->settings['map authors'])) : | |
| 2161 | + $author_rules = explode("\n\n", $this->settings['map authors']); | |
| 2162 | + $ma = array(); | |
| 2163 | + foreach ($author_rules as $rule) : | |
| 2164 | + list($rule_type, $author_name, $author_action) = explode("\n", $rule); | |
| 2165 | + | |
| 2166 | + // Normalize for case and whitespace | |
| 2167 | + $rule_type = strtolower(trim($rule_type)); | |
| 2168 | + $author_name = strtolower(trim($author_name)); | |
| 2169 | + $author_action = strtolower(trim($author_action)); | |
| 2170 | + | |
| 2171 | + $ma[$rule_type][$author_name] = $author_action; | |
| 2172 | + endforeach; | |
| 2173 | + $this->settings['map authors'] = $ma; | |
| 2174 | + endif; | |
| 2175 | + endif; | |
| 2176 | + } /* SyndicatedLink::SyndicatedLink () */ | |
| 2177 | + | |
| 2178 | + function found () { | |
| 2179 | + return is_object($this->link); | |
| 2180 | + } /* SyndicatedLink::found () */ | |
| 2181 | + | |
| 2182 | + function stale () { | |
| 2183 | + $stale = true; | |
| 2184 | + if (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='ping')) : | |
| 2185 | + $stale = false; // don't update on any timed updates; pings only | |
| 2186 | + elseif (isset($this->settings['update/hold']) and ($this->settings['update/hold']=='next')) : | |
| 2187 | + $stale = true; // update on the next timed update | |
| 2188 | + elseif (!isset($this->settings['update/ttl']) or !isset($this->settings['update/last'])) : | |
| 2189 | + $stale = true; // initial update | |
| 2190 | + else : | |
| 2191 | + $after = ((int) $this->settings['update/last']) | |
| 2192 | + +((int) $this->settings['update/ttl'] * 60); | |
| 2193 | + $stale = (time() >= $after); | |
| 2194 | + endif; | |
| 2195 | + return $stale; | |
| 2196 | + } /* SyndicatedLink::stale () */ | |
| 2197 | + | |
| 2198 | + function poll ($crash_ts = NULL) { | |
| 2199 | + global $wpdb; | |
| 2200 | + | |
| 2201 | + $this->magpie = fetch_rss($this->link->link_rss); | |
| 2202 | + $new_count = NULL; | |
| 2203 | + | |
| 2204 | + $resume = FeedWordPress::affirmative($this->settings, 'update/unfinished'); | |
| 2205 | + if ($resume) : | |
| 2206 | + // pick up where we left off | |
| 2207 | + $processed = array_map('trim', explode("\n", $this->settings['update/processed'])); | |
| 2208 | + else : | |
| 2209 | + // begin at the beginning | |
| 2210 | + $processed = array(); | |
| 2211 | + endif; | |
| 2212 | + | |
| 2213 | + if (is_object($this->magpie)) : | |
| 2214 | + $new_count = array('new' => 0, 'updated' => 0); | |
| 2215 | + | |
| 2216 | + # -- Update Link metadata live from feed | |
| 2217 | + $channel = $this->magpie->channel; | |
| 2218 | + | |
| 2219 | + if (!isset($channel['id'])) : | |
| 2220 | + $channel['id'] = $this->link->link_rss; | |
| 2221 | + endif; | |
| 2222 | + | |
| 2223 | + $update = array(); | |
| 2224 | + if (!$this->hardcode('url') and isset($channel['link'])) : | |
| 2225 | + $update[] = "link_url = '".$wpdb->escape($channel['link'])."'"; | |
| 2226 | + endif; | |
| 2227 | + | |
| 2228 | + if (!$this->hardcode('name') and isset($channel['title'])) : | |
| 2229 | + $update[] = "link_name = '".$wpdb->escape($channel['title'])."'"; | |
| 2230 | + endif; | |
| 2231 | + | |
| 2232 | + if (!$this->hardcode('description')) : | |
| 2233 | + if (isset($channel['tagline'])) : | |
| 2234 | + $update[] = "link_description = '".$wpdb->escape($channel['tagline'])."'"; | |
| 2235 | + elseif (isset($channel['description'])) : | |
| 2236 | + $update[] = "link_description = '".$wpdb->escape($channel['description'])."'"; | |
| 2237 | + endif; | |
| 2238 | + endif; | |
| 2239 | + | |
| 2240 | + $this->settings = array_merge($this->settings, $this->flatten_array($channel)); | |
| 2241 | + | |
| 2242 | + $this->settings['update/last'] = time(); $ttl = $this->ttl(); | |
| 2243 | + if (!is_null($ttl)) : | |
| 2244 | + $this->settings['update/ttl'] = $ttl; | |
| 2245 | + $this->settings['update/timed'] = 'feed'; | |
| 2246 | + else : | |
| 2247 | + $this->settings['update/ttl'] = rand(30, 120); // spread over time interval for staggered updates | |
| 2248 | + $this->settings['update/timed'] = 'automatically'; | |
| 2249 | + endif; | |
| 2250 | + | |
| 2251 | + if (!isset($this->settings['update/hold']) or $this->settings['update/hold']!='ping') : | |
| 2252 | + $this->settings['update/hold'] = 'scheduled'; | |
| 2253 | + endif; | |
| 2254 | + | |
| 2255 | + $this->settings['update/unfinished'] = 'yes'; | |
| 2256 | + | |
| 2257 | + $update[] = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'"; | |
| 2258 | + | |
| 2259 | + $update_set = implode(',', $update); | |
| 2260 | + | |
| 2261 | + // Update the properties of the link from the feed information | |
| 2262 | + $result = $wpdb->query(" | |
| 2263 | + UPDATE $wpdb->links | |
| 2264 | + SET $update_set | |
| 2265 | + WHERE link_id='$this->id' | |
| 2266 | + "); | |
| 2267 | + | |
| 2268 | + # -- Add new posts from feed and update any updated posts | |
| 2269 | + $crashed = false; | |
| 2270 | + | |
| 2271 | + if (is_array($this->magpie->items)) : | |
| 2272 | + foreach ($this->magpie->items as $item) : | |
| 2273 | + $post =& new SyndicatedPost($item, $this); | |
| 2274 | + if (!$resume or !in_array(trim($post->guid()), $processed)) : | |
| 2275 | + $processed[] = $post->guid(); | |
| 2276 | + if (!$post->filtered()) : | |
| 2277 | + $new = $post->store(); | |
| 2278 | + if ( $new !== false ) $new_count[$new]++; | |
| 2279 | + endif; | |
| 2280 | + | |
| 2281 | + if (!is_null($crash_ts) and (time() > $crash_ts)) : | |
| 2282 | + $crashed = true; | |
| 2283 | + break; | |
| 2284 | + endif; | |
| 2285 | + endif; | |
| 2286 | + endforeach; | |
| 2287 | + endif; | |
| 2288 | + | |
| 2289 | + // Copy back any changes to feed settings made in the course of updating (e.g. new author rules) | |
| 2290 | + $to_notes = $this->settings; | |
| 2291 | + | |
| 2292 | + $this->settings['update/processed'] = $processed; | |
| 2293 | + if (!$crashed) : | |
| 2294 | + $this->settings['update/unfinished'] = 'no'; | |
| 2295 | + endif; | |
| 2296 | + | |
| 2297 | + $update_set = "link_notes = '".$wpdb->escape($this->settings_to_notes())."'"; | |
| 2298 | + | |
| 2299 | + // Update the properties of the link from the feed information | |
| 2300 | + $result = $wpdb->query(" | |
| 2301 | + UPDATE $wpdb->links | |
| 2302 | + SET $update_set | |
| 2303 | + WHERE link_id='$this->id' | |
| 2304 | + "); | |
| 2305 | + endif; | |
| 2306 | + | |
| 2307 | + return $new_count; | |
| 2308 | + } /* SyndicatedLink::poll() */ | |
| 2309 | + | |
| 2310 | + function map_name_to_new_user ($name, $newuser_name) { | |
| 2311 | + global $wpdb; | |
| 2312 | + | |
| 2313 | + if (strlen($newuser_name) > 0) : | |
| 2314 | + $userdata = array(); | |
| 2315 | + $userdata['ID'] = NULL; | |
| 2316 | + | |
| 2317 | + $userdata['user_login'] = sanitize_user($newuser_name); | |
| 2318 | + $userdata['user_login'] = apply_filters('pre_user_login', $userdata['user_login']); | |
| 2319 | + | |
| 2320 | + $userdata['user_nicename'] = sanitize_title($newuser_name); | |
| 2321 | + $userdata['user_nicename'] = apply_filters('pre_user_nicename', $userdata['user_nicename']); | |
| 2322 | + | |
| 2323 | + $userdata['display_name'] = $wpdb->escape($newuser_name); | |
| 2324 | + | |
| 2325 | + $newuser_id = wp_insert_user($userdata); | |
| 2326 | + if (is_numeric($newuser_id)) : | |
| 2327 | + if (is_null($name)) : // Unfamiliar author | |
| 2328 | + $this->settings['unfamiliar author'] = $newuser_id; | |
| 2329 | + else : | |
| 2330 | + $this->settings['map authors']['name'][$name] = $newuser_id; | |
| 2331 | + endif; | |
| 2332 | + else : | |
| 2333 | + // TODO: Add some error detection and reporting | |
| 2334 | + endif; | |
| 2335 | + else : | |
| 2336 | + // TODO: Add some error reporting | |
| 2337 | + endif; | |
| 2338 | + } /* SyndicatedLink::map_name_to_new_user () */ | |
| 2339 | + | |
| 2340 | + function settings_to_notes () { | |
| 2341 | + $to_notes = $this->settings; | |
| 2342 | + | |
| 2343 | + unset($to_notes['link/id']); // Magic setting; don't save | |
| 2344 | + unset($to_notes['link/uri']); // Magic setting; don't save | |
| 2345 | + unset($to_notes['link/name']); // Magic setting; don't save | |
| 2346 | + unset($to_notes['hardcode categories']); // Deprecated | |
| 2347 | + unset($to_notes['unfamiliar categories']); // Deprecated | |
| 2348 | + | |
| 2349 | + // Collapse array settings | |
| 2350 | + if (isset($to_notes['update/processed']) and (is_array($to_notes['update/processed']))) : | |
| 2351 | + $to_notes['update/processed'] = implode("\n", $to_notes['update/processed']); | |
| 2352 | + endif; | |
| 2353 | + | |
| 2354 | + if (is_array($to_notes['cats'])) : | |
| 2355 | + $to_notes['cats'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['cats']); | |
| 2356 | + endif; | |
| 2357 | + if (is_array($to_notes['tags'])) : | |
| 2358 | + $to_notes['tags'] = implode(FEEDWORDPRESS_CAT_SEPARATOR, $to_notes['tags']); | |
| 2359 | + endif; | |
| 2360 | + | |
| 2361 | + // Collapse the author mapping rule structure back into a flat string | |
| 2362 | + if (isset($to_notes['map authors'])) : | |
| 2363 | + $ma = array(); | |
| 2364 | + foreach ($to_notes['map authors'] as $rule_type => $author_rules) : | |
| 2365 | + foreach ($author_rules as $author_name => $author_action) : | |
| 2366 | + $ma[] = $rule_type."\n".$author_name."\n".$author_action; | |
| 2367 | + endforeach; | |
| 2368 | + endforeach; | |
| 2369 | + $to_notes['map authors'] = implode("\n\n", $ma); | |
| 2370 | + endif; | |
| 2371 | + | |
| 2372 | + $notes = ''; | |
| 2373 | + foreach ($to_notes as $key => $value) : | |
| 2374 | + $notes .= $key . ": ". addcslashes($value, "\0..\37".'\\') . "\n"; | |
| 2375 | + endforeach; | |
| 2376 | + return $notes; | |
| 2377 | + } /* SyndicatedLink::settings_to_notes () */ | |
| 2378 | + | |
| 2379 | + function uri () { | |
| 2380 | + return (is_object($this->link) ? $this->link->link_rss : NULL); | |
| 2381 | + } /* SyndicatedLink::uri () */ | |
| 2382 | + | |
| 2383 | + function homepage () { | |
| 2384 | + return (isset($this->settings['feed/link']) ? $this->settings['feed/link'] : NULL); | |
| 2385 | + } /* SyndicatedLink::homepage () */ | |
| 2386 | + | |
| 2387 | + function ttl () { | |
| 2388 | + if (is_object($this->magpie)) : | |
| 2389 | + $channel = $this->magpie->channel; | |
| 2390 | + else : | |
| 2391 | + $channel = array(); | |
| 2392 | + endif; | |
| 2393 | + | |
| 2394 | + if (isset($channel['ttl'])) : | |
| 2395 | + // "ttl stands for time to live. It's a number of | |
| 2396 | + // minutes that indicates how long a channel can be | |
| 2397 | + // cached before refreshing from the source." | |
| 2398 | + // <http://blogs.law.harvard.edu/tech/rss#ltttlgtSubelementOfLtchannelgt> | |
| 2399 | + $ret = $channel['ttl']; | |
| 2400 | + elseif (isset($channel['sy']['updatefrequency']) or isset($channel['sy']['updateperiod'])) : | |
| 2401 | + $period_minutes = array ( | |
| 2402 | + 'hourly' => 60, /* minutes in an hour */ | |
| 2403 | + 'daily' => 1440, /* minutes in a day */ | |
| 2404 | + 'weekly' => 10080, /* minutes in a week */ | |
| 2405 | + 'monthly' => 43200, /* minutes in a month */ | |
| 2406 | + 'yearly' => 525600, /* minutes in a year */ | |
| 2407 | + ); | |
| 2408 | + | |
| 2409 | + // "sy:updatePeriod: Describes the period over which the | |
| 2410 | + // channel format is updated. Acceptable values are: | |
| 2411 | + // hourly, daily, weekly, monthly, yearly. If omitted, | |
| 2412 | + // daily is assumed." <http://web.resource.org/rss/1.0/modules/syndication/> | |
| 2413 | + if (isset($channel['sy']['updateperiod'])) : $period = $channel['sy']['updateperiod']; | |
| 2414 | + else : $period = 'daily'; | |
| 2415 | + endif; | |
| 2416 | + | |
| 2417 | + // "sy:updateFrequency: Used to describe the frequency | |
| 2418 | + // of updates in relation to the update period. A | |
| 2419 | + // positive integer indicates how many times in that | |
| 2420 | + // period the channel is updated. ... If omitted a value | |
| 2421 | + // of 1 is assumed." <http://web.resource.org/rss/1.0/modules/syndication/> | |
| 2422 | + if (isset($channel['sy']['updatefrequency'])) : $freq = (int) $channel['sy']['updatefrequency']; | |
| 2423 | + else : $freq = 1; | |
| 2424 | + endif; | |
| 2425 | + | |
| 2426 | + $ret = (int) ($period_minutes[$period] / $freq); | |
| 2427 | + else : | |
| 2428 | + $ret = NULL; | |
| 2429 | + endif; | |
| 2430 | + return $ret; | |
| 2431 | + } /* SyndicatedLink::ttl() */ | |
| 2432 | + | |
| 2433 | + // SyndicatedLink::flatten_array (): flatten an array. Useful for | |
| 2434 | + // hierarchical and namespaced elements. | |
| 2435 | + // | |
| 2436 | + // Given an array which may contain array or object elements in it, | |
| 2437 | + // return a "flattened" array: a one-dimensional array of scalars | |
| 2438 | + // containing each of the scalar elements contained within the array | |
| 2439 | + // structure. Thus, for example, if $a['b']['c']['d'] == 'e', then the | |
| 2440 | + // returned array for FeedWordPress::flatten_array($a) will contain a key | |
| 2441 | + // $a['feed/b/c/d'] with value 'e'. | |
| 2442 | + function flatten_array ($arr, $prefix = 'feed/', $separator = '/') { | |
| 2443 | + $ret = array (); | |
| 2444 | + if (is_array($arr)) : | |
| 2445 | + foreach ($arr as $key => $value) : | |
| 2446 | + if (is_scalar($value)) : | |
| 2447 | + $ret[$prefix.$key] = $value; | |
| 2448 | + else : | |
| 2449 | + $ret = array_merge($ret, $this->flatten_array($value, $prefix.$key.$separator, $separator)); | |
| 2450 | + endif; | |
| 2451 | + endforeach; | |
| 2452 | + endif; | |
| 2453 | + return $ret; | |
| 2454 | + } /* SyndicatedLink::flatten_array () */ | |
| 2455 | + | |
| 2456 | + function hardcode ($what) { | |
| 2457 | + $default = get_option("feedwordpress_hardcode_$what"); | |
| 2458 | + if ( $default === 'yes' ) : | |
| 2459 | + // If the default is to hardcode, then we want the | |
| 2460 | + // negation of negative(): TRUE by default and FALSE if | |
| 2461 | + // the setting is explicitly "no" | |
| 2462 | + $ret = !FeedWordPress::negative($this->settings, "hardcode $what"); | |
| 2463 | + else : | |
| 2464 | + // If the default is NOT to hardcode, then we want | |
| 2465 | + // affirmative(): FALSE by default and TRUE if the | |
| 2466 | + // setting is explicitly "yes" | |
| 2467 | + $ret = FeedWordPress::affirmative($this->settings, "hardcode $what"); | |
| 2468 | + endif; | |
| 2469 | + return $ret; | |
| 2470 | + } /* SyndicatedLink::hardcode () */ | |
| 2471 | + | |
| 2472 | + function syndicated_status ($what, $default) { | |
| 2473 | + global $wpdb; | |
| 2474 | + | |
| 2475 | + $ret = get_option("feedwordpress_syndicated_{$what}_status"); | |
| 2476 | + if ( isset($this->settings["$what status"]) ) : | |
| 2477 | + $ret = $this->settings["$what status"]; | |
| 2478 | + elseif (!$ret) : | |
| 2479 | + $ret = $default; | |
| 2480 | + endif; | |
| 2481 | + return $wpdb->escape(trim(strtolower($ret))); | |
| 2482 | + } /* SyndicatedLink:syndicated_status () */ | |
| 2483 | +} // class SyndicatedLink | |
| 2484 | + | |
| 2336 | 2485 | ################################################################################ |
| 2337 | 2486 | ## XML-RPC HOOKS: accept XML-RPC update pings from Contributors ################ |
| 2338 | 2487 | ################################################################################ |
| 2339 | 2488 | |
| @@ -2354,8 +2503,174 @@ | ||
| 2354 | 2503 | |
| 2355 | 2504 | return array('flerror' => false, 'message' => "Thanks for the ping.".implode(' and', $mesg)); |
| 2356 | 2505 | endif; |
| 2357 | 2506 | } |
| 2507 | + | |
| 2508 | +################################################################################ | |
| 2509 | +## class FeedFinder: find likely feeds using autodetection and/or guesswork #### | |
| 2510 | +################################################################################ | |
| 2511 | + | |
| 2512 | +class FeedFinder { | |
| 2513 | + var $uri = NULL; | |
| 2514 | + var $_cache_uri = NULL; | |
| 2515 | + | |
| 2516 | + var $verify = FALSE; | |
| 2517 | + | |
| 2518 | + var $_data = NULL; | |
| 2519 | + var $_error = NULL; | |
| 2520 | + var $_head = NULL; | |
| 2521 | + | |
| 2522 | + # -- Recognition patterns | |
| 2523 | + var $_feed_types = array( | |
| 2524 | + 'application/rss+xml', | |
| 2525 | + 'text/xml', | |
| 2526 | + 'application/atom+xml', | |
| 2527 | + 'application/x.atom+xml', | |
| 2528 | + 'application/x-atom+xml' | |
| 2529 | + ); | |
| 2530 | + var $_feed_markers = array('\\<feed', '\\<rss', 'xmlns="http://purl.org/rss/1.0'); | |
| 2531 | + var $_html_markers = array('\\<html'); | |
| 2532 | + var $_obvious_feed_url = array('[./]rss', '[./]rdf', '[./]atom', '[./]feed', '\.xml'); | |
| 2533 | + var $_maybe_feed_url = array ('rss', 'rdf', 'atom', 'feed', 'xml'); | |
| 2534 | + | |
| 2535 | + function FeedFinder ($uri = NULL, $verify = TRUE) { | |
| 2536 | + $this->uri = $uri; $this->verify = $verify; | |
| 2537 | + } /* FeedFinder::FeedFinder () */ | |
| 2538 | + | |
| 2539 | + function find ($uri = NULL) { | |
| 2540 | + $ret = array (); | |
| 2541 | + if (!is_null($this->data($uri))) { | |
| 2542 | + if ($this->is_feed($uri)) { | |
| 2543 | + $ret = array($this->uri); | |
| 2544 | + } else { | |
| 2545 | + // Assume that we have HTML or XHTML (even if we don't, who's it gonna hurt?) | |
| 2546 | + // Autodiscovery is the preferred method | |
| 2547 | + $href = $this->_link_rel_feeds(); | |
| 2548 | + | |
| 2549 | + // ... but we'll also take the little orange buttons | |
| 2550 | + $href = array_merge($href, $this->_a_href_feeds(TRUE)); | |
| 2551 | + | |
| 2552 | + // If all that failed, look harder | |
| 2553 | + if (count($href) == 0) $href = $this->_a_href_feeds(FALSE); | |
| 2554 | + | |
| 2555 | + // Verify feeds and resolve relative URIs | |
| 2556 | + foreach ($href as $u) { | |
| 2557 | + $the_uri = Relative_URI::resolve($u, $this->uri); | |
| 2558 | + if ($this->verify) { | |
| 2559 | + $feed =& new FeedFinder($the_uri); | |
| 2560 | + if ($feed->is_feed()) $ret[] = $the_uri; | |
| 2561 | + $feed = NULL; | |
| 2562 | + } else { | |
| 2563 | + $ret[] = $the_uri; | |
| 2564 | + } | |
| 2565 | + } /* foreach */ | |
| 2566 | + } /* if */ | |
| 2567 | + } /* if */ | |
| 2568 | + return array_unique($ret); | |
| 2569 | + } /* FeedFinder::find () */ | |
| 2570 | + | |
| 2571 | + function data ($uri = NULL) { | |
| 2572 | + $this->_get($uri); | |
| 2573 | + return $this->_data; | |
| 2574 | + } | |
| 2575 | + | |
| 2576 | + function error () { | |
| 2577 | + return $this->_error; | |
| 2578 | + } | |
| 2579 | + | |
| 2580 | + function is_feed ($uri = NULL) { | |
| 2581 | + $data = $this->data($uri); | |
| 2582 | + | |
| 2583 | + return ( | |
| 2584 | + preg_match ( | |
| 2585 | + "\007(".implode('|',$this->_feed_markers).")\007i", | |
| 2586 | + $data | |
| 2587 | + ) and !preg_match ( | |
| 2588 | + "\007(".implode('|',$this->_html_markers).")\007i", | |
| 2589 | + $data | |
| 2590 | + ) | |
| 2591 | + ); | |
| 2592 | + } /* FeedFinder::is_feed () */ | |
| 2593 | + | |
| 2594 | + # --- Private methods --- | |
| 2595 | + function _get ($uri = NULL) { | |
| 2596 | + if ($uri) $this->uri = $uri; | |
| 2597 | + | |
| 2598 | + // Is the result not yet cached? | |
| 2599 | + if ($this->_cache_uri !== $this->uri) : | |
| 2600 | + $headers['Connection'] = 'close'; | |
| 2601 | + $headers['Accept'] = 'application/atom+xml application/rdf+xml application/rss+xml application/xml text/html */*'; | |
| 2602 | + $headers['User-Agent'] = 'feedfinder/1.2 (compatible; PHP FeedFinder) +http://projects.radgeek.com/feedwordpress'; | |
| 2603 | + | |
| 2604 | + // Use function provided by MagpieRSS package | |
| 2605 | + $client = _fetch_remote_file($this->uri, $headers); | |
| 2606 | + if (isset($client->error)) : | |
| 2607 | + $this->_error = $client->error; | |
| 2608 | + else : | |
| 2609 | + $this->_error = NULL; | |
| 2610 | + endif; | |
| 2611 | + $this->_data = $client->results; | |
| 2612 | + | |
| 2613 | + // Kilroy was here | |
| 2614 | + $this->_cache_uri = $this->uri; | |
| 2615 | + endif; | |
| 2616 | + } /* FeedFinder::_get () */ | |
| 2617 | + | |
| 2618 | + function _link_rel_feeds () { | |
| 2619 | + $links = $this->_tags('link'); | |
| 2620 | + $link_count = count($links); | |
| 2621 | + | |
| 2622 | + // now figure out which one points to the RSS file | |
| 2623 | + $href = array (); | |
| 2624 | + for ($n=0; $n<$link_count; $n++) { | |
| 2625 | + if (strtolower($links[$n]['rel']) == 'alternate') { | |
| 2626 | + if (in_array(strtolower($links[$n]['type']), $this->_feed_types)) { | |
| 2627 | + $href[] = $links[$n]['href']; | |
| 2628 | + } /* if */ | |
| 2629 | + } /* if */ | |
| 2630 | + } /* for */ | |
| 2631 | + return $href; | |
| 2632 | + } | |
| 2633 | + | |
| 2634 | + function _a_href_feeds ($obvious = TRUE) { | |
| 2635 | + $pattern = ($obvious ? $this->_obvious_feed_url : $this->_maybe_feed_url); | |
| 2636 | + | |
| 2637 | + $links = $this->_tags('a'); | |
| 2638 | + $link_count = count($links); | |
| 2639 | + | |
| 2640 | + // now figure out which one points to the RSS file | |
| 2641 | + $href = array (); | |
| 2642 | + for ($n=0; $n<$link_count; $n++) { | |
| 2643 | + if (preg_match("\007(".implode('|',$pattern).")\007i", $links[$n]['href'])) { | |
| 2644 | + $href[] = $links[$n]['href']; | |
| 2645 | + } /* if */ | |
| 2646 | + } /* for */ | |
| 2647 | + return $href; | |
| 2648 | + } | |
| 2649 | + | |
| 2650 | + function _tags ($tag) { | |
| 2651 | + $html = $this->data(); | |
| 2652 | + | |
| 2653 | + // search through the HTML, save all <link> tags | |
| 2654 | + // and store each link's attributes in an associative array | |
| 2655 | + preg_match_all('/<'.$tag.'\s+(.*?)\s*\/?>/si', $html, $matches); | |
| 2656 | + $links = $matches[1]; | |
| 2657 | + $ret = array(); | |
| 2658 | + $link_count = count($links); | |
| 2659 | + for ($n=0; $n<$link_count; $n++) { | |
| 2660 | + $attributes = preg_split('/\s+/s', $links[$n]); | |
| 2661 | + foreach($attributes as $attribute) { | |
| 2662 | + $att = preg_split('/\s*=\s*/s', $attribute, 2); | |
| 2663 | + if (isset($att[1])) { | |
| 2664 | + $att[1] = preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]); | |
| 2665 | + $final_link[strtolower($att[0])] = $att[1]; | |
| 2666 | + } /* if */ | |
| 2667 | + } /* foreach */ | |
| 2668 | + $ret[$n] = $final_link; | |
| 2669 | + } /* for */ | |
| 2670 | + return $ret; | |
| 2671 | + } | |
| 2672 | +} /* class FeedFinder */ | |
| 2358 | 2673 | |
| 2359 | 2674 | # Relative URI static class: PHP class for resolving relative URLs |
| 2360 | 2675 | # |
| 2361 | 2676 | # This class is derived (under the terms of the GPL) from URL Class 0.3 by |