| 1 |
<?php |
| 2 |
# update-feeds.php: Instruct FeedWordPress to scan for fresh content |
| 3 |
# |
| 4 |
# Project: FeedWordPress |
| 5 |
# URI: <http://projects.radgeek.com/feedwordpress> |
| 6 |
# Author: Charles Johnson <technophilia@radgeek.com> |
| 7 |
# License: GPL |
| 8 |
# Version: 2005.11.06 |
| 9 |
# |
| 10 |
# USAGE |
| 11 |
# ----- |
| 12 |
# update-feeds.php is a useful script for instructing the FeedWordPress plugin |
| 13 |
# to scan for fresh content on the feeds that it syndicates. This is handy if |
| 14 |
# you want your syndication site to actually syndicate content, and you can't |
| 15 |
# rely on all your contributors to send you an XML-RPC ping when they update. |
| 16 |
# |
| 17 |
# 1. Install FeedWordPress and activate the FeedWordPress plugin. (See |
| 18 |
# <http://projects.radgeek.com/feedwordpress/install> if you need a guide |
| 19 |
# for the perplexed.) |
| 20 |
# |
| 21 |
# 2. If you want to manually update one or more of your feeds, you can do |
| 22 |
# so by pointing your web browser to the URI |
| 23 |
# <http://xyz.com/wp-content/update-feeds.php>, where `http://xyz.com/` is |
| 24 |
# replaced by the URI to your installation of WordPress. Log in as any |
| 25 |
# user in your WordPress database and use the form to update. |
| 26 |
# |
| 27 |
# 3. To keep content up-to-date automatically, set up a cron job to run |
| 28 |
# update-feeds.php locally: |
| 29 |
# |
| 30 |
# cd /path/to/wordpress/wp-content ; php update-feeds.php |
| 31 |
# |
| 32 |
# where `/path/to/wordpress` is replaced by the filesystem path to your |
| 33 |
# installation of WordPress; or, if you don't have, or don't want to use, |
| 34 |
# command-line PHP, you can send an HTTP POST request to the appropriate |
| 35 |
# URI: |
| 36 |
# |
| 37 |
# curl --user user:pass http://xyz.com/wp-content/update-feeds.php -d update=quiet |
| 38 |
# |
| 39 |
# `user` and `pass` should be replaced by the username and password of |
| 40 |
# a user in your WordPress database (you can create a dummy user for |
| 41 |
# updates if you want; that's what I do). `http://xyz.com/` should be |
| 42 |
# replaced by the URI to your installation of WordPress. |
| 43 |
# |
| 44 |
# Don't be afraid to run this cron job frequently. FeedWordPress staggers |
| 45 |
# updates over time rather than checking all of the feeds every time the |
| 46 |
# cron job runs, so even if the cron job runs every 10 minutes, each feed |
| 47 |
# will, on average only be polled for updates once an hour or so (or less |
| 48 |
# frequently if the feed author requests less frequent updates using |
| 49 |
# the RSS <ttl> element or the syndication module elements). |
| 50 |
# |
| 51 |
# 4. If you want to update *one* of the feeds rather than *all* of them, then |
| 52 |
# pass the URI and title as command-line arguments: |
| 53 |
# |
| 54 |
# $ php update-feeds.php http://radgeek.com |
| 55 |
# |
| 56 |
# or in the POST request: |
| 57 |
# |
| 58 |
# $ curl --user login:password http://xyz.com/wp-content/update-feeds.php -d uri=http://www.radgeek.com\&update=quiet |
| 59 |
# |
| 60 |
// Help us to pick out errors, if any. |
| 61 |
ini_set('error_reporting', E_ALL & ~E_NOTICE); |
| 62 |
ini_set('display_errors', true); |
| 63 |
define('MAGPIE_DEBUG', true); |
| 64 |
|
| 65 |
// Are we running from a web request or from the command line? |
| 66 |
if (!isset($_SERVER['REQUEST_URI'])) : |
| 67 |
$update_feeds_display = 'text/plain'; |
| 68 |
$update_feeds_invoke = 'cmd'; |
| 69 |
elseif (isset($_POST['update'])) : |
| 70 |
if ($_POST['update'] == 'quiet') : |
| 71 |
$update_feeds_display = 'text/plain'; |
| 72 |
$update_feeds_invoke = 'post'; |
| 73 |
$update_feeds_verbose = false; |
| 74 |
elseif ($_POST['update'] == 'verbose') : |
| 75 |
$update_feeds_display = 'text/plain'; |
| 76 |
$update_feeds_invoke = 'post'; |
| 77 |
$update_feeds_verbose = true; |
| 78 |
else : |
| 79 |
$update_feeds_display = 'text/html'; |
| 80 |
$update_feeds_invoke = 'post'; |
| 81 |
endif; |
| 82 |
else : |
| 83 |
$update_feeds_display = 'text/html'; |
| 84 |
$update_feeds_invoke = 'get'; |
| 85 |
endif; |
| 86 |
|
| 87 |
require_once ('../wp-blog-header.php'); |
| 88 |
|
| 89 |
function update_feeds_mention ($feed) { |
| 90 |
global $update_feeds_display; |
| 91 |
|
| 92 |
if ($update_feeds_display=='text/html') : |
| 93 |
echo "<li>Updating <cite>".$feed['link/name']."</cite> from <<a href=\"" |
| 94 |
.$feed['link/uri']."\">".$feed['link/uri']."</a>> ...</li>\n"; |
| 95 |
else : |
| 96 |
echo "* Updating ".$feed['link/name']." from <".$feed['link/uri']."> ...\n"; |
| 97 |
endif; |
| 98 |
flush(); |
| 99 |
} |
| 100 |
|
| 101 |
# -- Don't change these unless you know what you're doing... |
| 102 |
define ('RPC_MAGIC', 'tag:radgeek.com/projects/feedwordpress/'); // update all |
| 103 |
|
| 104 |
// Query secret word from database |
| 105 |
$rpc_secret = get_settings('feedwordpress_rpc_secret'); |
| 106 |
|
| 107 |
header("Content-Type: {$update_feeds_display}; charset=utf-8"); |
| 108 |
|
| 109 |
# -- Are we running from an HTTP GET, HTTP POST, or from the command line? |
| 110 |
if ($update_feeds_invoke != 'cmd') : // We're acessing this from HTTP GET or HTTP POST |
| 111 |
// Authenticate the user, if possible ... |
| 112 |
if (isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['PHP_AUTH_PW'])) : // try HTTP authentication |
| 113 |
$login = $_SERVER['PHP_AUTH_USER']; $pass = $_SERVER['PHP_AUTH_PW']; |
| 114 |
elseif (isset($_POST['log']) and isset($_POST['pwd'])) : // try POST data |
| 115 |
$login = $_REQUEST['log']; $pass = $_REQUEST['pwd']; |
| 116 |
endif; |
| 117 |
|
| 118 |
if (empty($login) or empty($pass) or !wp_login($login, $pass)) : |
| 119 |
if ($update_feeds_display=='text/html') : |
| 120 |
auth_redirect(); // try authentication cookies; if all else fails, redirect to wp-login.php |
| 121 |
else : |
| 122 |
echo "update-feeds (".date('Y-m-d H:i:s')."): ERROR: Could not log in as '$login' (password: '$pass')\n"; |
| 123 |
die; |
| 124 |
endif; |
| 125 |
endif; |
| 126 |
|
| 127 |
// Henceforward, we can proceed on the assumption that we have an authenticated user |
| 128 |
$uri = (isset($_REQUEST['uri']) ? $_REQUEST['uri'] : RPC_MAGIC.$rpc_secret); |
| 129 |
|
| 130 |
if ($update_feeds_display=='text/html') : |
| 131 |
echo <<<EOHTML |
| 132 |
<?xml version="1.0" encoding="utf-8"?> |
| 133 |
<html> |
| 134 |
<head> |
| 135 |
<title>update-feeds :: FeedWordPress</title> |
| 136 |
</head> |
| 137 |
|
| 138 |
<body> |
| 139 |
<h1>update-feeds: make FeedWordPress check for new syndicated content</h1> |
| 140 |
|
| 141 |
EOHTML; |
| 142 |
endif; |
| 143 |
else : |
| 144 |
// update-feeds has been invoked from the command line; no further |
| 145 |
// authentication is necessary. (If PAM hasn't already done the |
| 146 |
// necessary screening for you, you have bigger problems than their |
| 147 |
// access to FeedWordPress...) |
| 148 |
$uri = (isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : RPC_MAGIC.$rpc_secret); |
| 149 |
endif; |
| 150 |
|
| 151 |
$feedwordpress =& new FeedWordPress; |
| 152 |
|
| 153 |
if ($update_feeds_display=='text/html' or $update_feeds_verbose) : |
| 154 |
add_action('feedwordpress_check_feed', 'update_feeds_mention'); |
| 155 |
endif; |
| 156 |
|
| 157 |
if ($update_feeds_display=='text/html') : // HTTP GET or HTTP POST: add some web niceties |
| 158 |
|
| 159 |
echo "<form action=\"\" method=\"POST\">\n"; |
| 160 |
echo "<select name=\"uri\">\n"; |
| 161 |
echo "<option value=\"".RPC_MAGIC.$rpc_secret."\">All feeds</option>\n"; |
| 162 |
foreach ($feedwordpress->feeds as $feed) : |
| 163 |
echo "<option value=\"{$feed['link/uri']}\""; |
| 164 |
if ($feed['link/uri']==$_REQUEST['uri']) : echo ' selected="selected"'; endif; |
| 165 |
echo ">{$feed['link/name']}</option>\n"; |
| 166 |
endforeach; |
| 167 |
echo "</select> "; |
| 168 |
echo "<input type=\"submit\" name=\"update\" value=\"Update\" />\n"; |
| 169 |
echo "</form>\n"; |
| 170 |
endif; |
| 171 |
|
| 172 |
if ($update_feeds_invoke != 'get') : // Only do things with side-effects for HTTP POST or command line |
| 173 |
if ($update_feeds_display == 'text/html') : echo "<ul>\n"; endif; |
| 174 |
$delta = @$feedwordpress->update($uri); |
| 175 |
if ($update_feeds_display == 'text/html') : echo "</ul>\n"; endif; |
| 176 |
|
| 177 |
if (is_null($delta)) : |
| 178 |
if ($update_feeds_invoke == 'cmd') : |
| 179 |
$stderr = fopen('php://stderr', 'w'); |
| 180 |
fputs($stderr, "update-feeds (".date('Y-m-d H:i:s')."): ERROR: I don't syndicate <$uri>\n"); |
| 181 |
elseif ($update_feeds_display == 'text/plain') : |
| 182 |
echo "update-feeds (".date('Y-m-d H:i:s')."): ERROR: I don't syndicate <$uri>\n"; |
| 183 |
else : |
| 184 |
echo "<p><strong>Error:</strong> I don't syndicate <a href=\"$uri\">$uri</a></p>\n"; |
| 185 |
endif; |
| 186 |
elseif ($update_feeds_display=='text/html' or $update_feeds_verbose) : |
| 187 |
$mesg = array(); |
| 188 |
if (isset($delta['new'])) : $mesg[] = ' '.$delta['new'].' new posts were syndicated'; endif; |
| 189 |
if (isset($delta['updated'])) : $mesg[] = ' '.$delta['updated'].' existing posts were updated'; endif; |
| 190 |
if ($update_feeds_display=='text/html') : echo "<p>"; endif; |
| 191 |
echo "Update complete.".implode(' and', $mesg); |
| 192 |
if ($update_feeds_display=='text/html') : echo "</p>"; endif; |
| 193 |
echo "\n"; flush(); |
| 194 |
endif; |
| 195 |
endif; |
| 196 |
|
| 197 |
if ($update_feeds_display=='text/html') : // HTTP GET or HTTP POST: close off web niceties |
| 198 |
echo <<<EOHTML |
| 199 |
|
| 200 |
<p><a href="../wp-admin">← Return to WordPress Dashboard</a></p> |
| 201 |
</body> |
| 202 |
</html> |
| 203 |
EOHTML; |
| 204 |
endif; |
| 205 |
?> |
| 206 |
|