| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Webfinger |
| 4 |
Plugin URI: http://wordpress.org/extend/plugins/webfinger/ |
| 5 |
Description: Webfinger for WordPress |
| 6 |
Version: 0.9 |
| 7 |
Author: Matthias Pfefferle |
| 8 |
Author URI: http://notizblog.org/ |
| 9 |
*/ |
| 10 |
|
| 11 |
add_filter('query_vars', array('WebfingerPlugin', 'queryVars')); |
| 12 |
add_action('parse_request', array('WebfingerPlugin', 'parseRequest')); |
| 13 |
add_action('wp_head', array('WebfingerPlugin', 'addHtmlHeader')); |
| 14 |
add_action('host_meta_xrd', array('WebfingerPlugin', 'addHostMeta')); |
| 15 |
add_filter('plugin_action_links', array('WebfingerPlugin', 'addSettingsLink'), 10, 2); |
| 16 |
|
| 17 |
// add profile parts |
| 18 |
add_action('show_user_profile', array('WebfingerPlugin', 'addUserProfileInfos')); |
| 19 |
add_action('edit_user_profile', array('WebfingerPlugin', 'addUserProfileInfos')); |
| 20 |
|
| 21 |
/** |
| 22 |
* returns a users default webfinger |
| 23 |
* |
| 24 |
* @param mixed $id_or_name_or_object |
| 25 |
* @param boolean $protocol |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
function get_webfinger($id_or_name_or_object = null, $protocol = false) { |
| 29 |
$user = WebfingerPlugin::getUserByVarious($id_or_name_or_object); |
| 30 |
|
| 31 |
if ($user) { |
| 32 |
$webfinger = $user->user_login."@".WebfingerPlugin::getHost(); |
| 33 |
if ($protocol) { |
| 34 |
$webfinger = "acct:".$webfinger; |
| 35 |
} |
| 36 |
return $webfinger; |
| 37 |
} else { |
| 38 |
return null; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* returns all webfingers |
| 44 |
* |
| 45 |
* @param mixed $id_or_name_or_object |
| 46 |
* @param boolean $protocol |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
function get_webfingers($id_or_name_or_object = null, $protocol = false) { |
| 51 |
$user = WebfingerPlugin::getUserByVarious($id_or_name_or_object); |
| 52 |
$webfingers = array(); |
| 53 |
|
| 54 |
if ($user) { |
| 55 |
$webfingers[] = ($protocol ? "acct:" : "").get_webfinger($user); |
| 56 |
$webfingers[] = get_author_posts_url($user->ID, $user->user_nicename); |
| 57 |
$webfingers = apply_filters('webfingers', $webfingers); |
| 58 |
|
| 59 |
return $webfingers; |
| 60 |
} else { |
| 61 |
return array(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* webfinger |
| 67 |
* |
| 68 |
* @author Matthias Pfefferle |
| 69 |
*/ |
| 70 |
class WebfingerPlugin { |
| 71 |
/** |
| 72 |
* adds some query vars |
| 73 |
* |
| 74 |
* @param array $vars |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
function queryVars($vars) { |
| 78 |
$vars[] = 'webfinger-uri'; |
| 79 |
//$vars[] = 'webfinger'; |
| 80 |
return $vars; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* parses the request |
| 85 |
*/ |
| 86 |
function parseRequest() { |
| 87 |
global $wp_query, $wp; |
| 88 |
|
| 89 |
$queryVars = $wp->query_vars; |
| 90 |
|
| 91 |
if( array_key_exists('webfinger-uri', $queryVars) ) { |
| 92 |
if (!$user = WebfingerPlugin::getUserByUri($queryVars['webfinger-uri'])) { |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
$url = get_author_posts_url($user->ID, $user->user_nicename); |
| 97 |
|
| 98 |
header('Content-Type: application/xrd+xml; charset=' . get_option('blog_charset'), true); |
| 99 |
|
| 100 |
echo "<?xml version='1.0' encoding='".get_option('blog_charset')."'?>\n"; |
| 101 |
echo "<XRD xmlns='http://docs.oasis-open.org/ns/xri/xrd-1.0'"; |
| 102 |
do_action('webfinger_ns'); |
| 103 |
echo ">\n"; |
| 104 |
echo " <Subject>".htmlentities($queryVars['webfinger-uri'])."</Subject>\n"; |
| 105 |
echo " <Alias>".$url."</Alias>\n"; |
| 106 |
echo " <Link rel='http://webfinger.net/rel/profile-page' type='text/html' href='$url' />\n"; |
| 107 |
echo " <Link rel='http://webfinger.net/rel/avatar' href='http://www.gravatar.com/avatar/".md5( $user->user_email ).".jpg' />\n"; |
| 108 |
do_action('webfinger_xrd', $user); |
| 109 |
// @deprecated please don't use |
| 110 |
do_action('webfinger_xrd_'.$user->user_login); |
| 111 |
echo "</XRD>"; |
| 112 |
|
| 113 |
exit; |
| 114 |
} else { |
| 115 |
return null; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* returns a Userobject |
| 121 |
* |
| 122 |
* @param string $uri |
| 123 |
* @return stdClass |
| 124 |
*/ |
| 125 |
function getUserByUri($uri) { |
| 126 |
global $wpdb; |
| 127 |
|
| 128 |
$uri = urldecode($uri); |
| 129 |
|
| 130 |
if (preg_match("~^https?://~i", $uri)) { |
| 131 |
// check if url matches with a |
| 132 |
// users profile url |
| 133 |
foreach (get_users_of_blog() as $user) { |
| 134 |
if (rtrim(str_replace("www.", "", get_author_posts_url($user->ID, $user->user_nicename)), "/") == |
| 135 |
rtrim(str_replace("www.", "", $uri), "/")) { |
| 136 |
return $user; |
| 137 |
} |
| 138 |
} |
| 139 |
} elseif (preg_match('/^([a-zA-Z]+:)?([^@]+)@([a-zA-Z0-9._-]+)$/i', $uri, $array)) { |
| 140 |
$username = $array[2]; |
| 141 |
$host = $array[3]; |
| 142 |
$email = $username."@".$host; |
| 143 |
|
| 144 |
if ($host == WebfingerPlugin::getHost()) { |
| 145 |
$sql = "SELECT * FROM $wpdb->users u INNER JOIN $wpdb->usermeta um ON u.id=um.user_id WHERE u.user_email = '$email' OR |
| 146 |
(um.meta_key = 'jabber' AND um.meta_value = '$email') OR |
| 147 |
u.user_login = '$username' LIMIT 1;"; |
| 148 |
$user = $wpdb->get_results($wpdb->prepare($sql)); |
| 149 |
if (!empty($user)) { |
| 150 |
return $user[0]; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* add the host meta information |
| 160 |
*/ |
| 161 |
function addHostMeta() { |
| 162 |
echo "<Link rel='lrdd' template='".get_option('siteurl')."/?webfinger-uri={uri}' type='application/xrd+xml' />"; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* adds lrdd-header-link to author profile pages |
| 167 |
*/ |
| 168 |
function addHtmlHeader() { |
| 169 |
global $wp_query; |
| 170 |
|
| 171 |
if (is_author() && !is_feed()) { |
| 172 |
$author = $wp_query->get_queried_object(); |
| 173 |
$url = get_author_posts_url($author->ID, $author->user_nicename); |
| 174 |
|
| 175 |
echo "\n"; |
| 176 |
echo "<link rel='lrdd' href='".get_option('siteurl')."/?webfinger-uri=".urlencode($url)."' type='application/xrd+xml' />"; |
| 177 |
echo "\n"; |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* prints webfinger part on the user-site |
| 183 |
*/ |
| 184 |
function addUserProfileInfos($user) { |
| 185 |
$webfingers = get_webfingers($user, true); |
| 186 |
?> |
| 187 |
<h3 id="webfinger">Webfinger</h3> |
| 188 |
|
| 189 |
<table class="form-table"> |
| 190 |
<tr> |
| 191 |
<th scope="row"><?php _e('Your Identifier')?></th> |
| 192 |
<td> |
| 193 |
<ul> |
| 194 |
<?php foreach ($webfingers as $webfinger) { ?> |
| 195 |
<li><?php echo $webfinger ?></li> |
| 196 |
<?php } ?> |
| 197 |
</ul> |
| 198 |
|
| 199 |
<a href="http://webfinger.org/lookup/<?php echo get_webfinger($user); ?>" target="_blank">Check your webfinger!</a> |
| 200 |
</td> |
| 201 |
</tr> |
| 202 |
</table> |
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* returns the blogs host |
| 208 |
* |
| 209 |
* @return string |
| 210 |
*/ |
| 211 |
function getHost() { |
| 212 |
$url = parse_url(get_bloginfo('url')); |
| 213 |
return $url['host']; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* add a settings link next to deactive / edit |
| 218 |
* |
| 219 |
* @param array $links |
| 220 |
* @param string $file |
| 221 |
* @return array |
| 222 |
*/ |
| 223 |
function addSettingsLink( $links, $file ) { |
| 224 |
if( preg_match("/webfinger/i", $file) && function_exists( "admin_url" ) ) { |
| 225 |
$settings_link = '<a href="' . admin_url( 'profile.php#webfinger' ) . '">' . __('Identifiers') . '</a>'; |
| 226 |
array_unshift( $links, $settings_link ); |
| 227 |
} |
| 228 |
return $links; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Convenience method to get user data by ID, username, object or from current user. |
| 233 |
* |
| 234 |
* @param mixed $id_or_name_or_object the username, ID or object. If not provided, the current user will be used. |
| 235 |
* @return bool|object False on failure, User DB row object |
| 236 |
* |
| 237 |
* @author Will Norris |
| 238 |
* @see get_userdata_by_various() # DiSo OpenID-Plugin |
| 239 |
*/ |
| 240 |
function getUserByVarious($id_or_name_or_object = null) { |
| 241 |
if ( $id_or_name_or_object === null ) { |
| 242 |
$user = wp_get_current_user(); |
| 243 |
if ($user == null) return false; |
| 244 |
return $user->data; |
| 245 |
} else if ( is_object($id_or_name_or_object) ) { |
| 246 |
return $id_or_name_or_object; |
| 247 |
} else if ( is_numeric($id_or_name_or_object) ) { |
| 248 |
return get_userdata($id_or_name_or_object); |
| 249 |
} else { |
| 250 |
return get_userdatabylogin($id_or_name_or_object); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
?> |