git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

Commit eefbfe74 authored by mmn's avatar mmn

Split up OStatusPlugin preg functions so they can be reused

cherry-pick-merge
parent 45dfa9f2
......@@ -272,6 +272,46 @@ class OStatusPlugin extends Plugin
return true;
}
/**
* Webfinger matches: @user@example.com or even @user--one.george_orwell@1984.biz
*
* @return array The matching IDs (without @ or acct:) and each respective position in the given string.
*/
static function extractWebfingerIds($text)
{
$wmatches = array();
$result = preg_match_all('/(?:^|\s+)@((?:\w+[\w\-\_\.]?)*(?:[\w\-\_\.]*\w+)@(?:(?!-)[A-Za-z0-9\-]{1,63}(?<!-)\.)+[A-Za-z]{2,10})/',
$text,
$wmatches,
PREG_OFFSET_CAPTURE);
if ($result === false) {
common_log(LOG_ERR, __METHOD__ . ': Error parsing webfinger IDs from text (preg_last_error=='.preg_last_error().').');
} else {
common_debug(sprintf('Found %i matches for WebFinger IDs: %s', count($wmatches), _ve($wmatches)));
}
return $wmatches[1];
}
/**
* Profile URL matches: @example.com/mublog/user
*
* @return array The matching URLs (without @ or acct:) and each respective position in the given string.
*/
static function extractUrlMentions($text)
{
$wmatches = array();
$result = preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)*)!',
$text,
$wmatches,
PREG_OFFSET_CAPTURE);
if ($result === false) {
common_log(LOG_ERR, __METHOD__ . ': Error parsing profile URL mentions from text (preg_last_error=='.preg_last_error().').');
} else {
common_debug(sprintf('Found %i matches for profile URL mentions: %s', count($wmatches), _ve($wmatches)));
}
return $wmatches[1];
}
/**
* Find any explicit remote mentions. Accepted forms:
* Webfinger: @user@example.com
......@@ -285,13 +325,7 @@ class OStatusPlugin extends Plugin
{
$matches = array();
$wmatches = array();
// Webfinger matches: @user@example.com or even @user--one.george_orwell@1984.biz
if (preg_match_all('/(?:^|\s+)@((?:\w+[\w\-\_\.]?)*(?:[\w\-\_\.]*\w+)@(?:(?!-)[A-Za-z0-9\-]{1,63}(?<!-)\.)+[A-Za-z]{2,10})/',
$text,
$wmatches,
PREG_OFFSET_CAPTURE)) {
foreach ($wmatches[1] as $wmatch) {
foreach (self::extractWebfingerIds($text) as $wmatch) {
list($target, $pos) = $wmatch;
$this->log(LOG_INFO, "Checking webfinger '$target'");
$profile = null;
......@@ -312,7 +346,7 @@ class OStatusPlugin extends Plugin
assert($profile instanceof Profile);
$text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target)
? $profile->getNickname() // TODO: we could do getFancyName() or getFullname() here
? $profile->getNickname() // TODO: we could do getBestName() or getFullname() here
: $target;
$url = $profile->getUri();
if (!common_valid_http_url($url)) {
......@@ -325,14 +359,8 @@ class OStatusPlugin extends Plugin
'length' => mb_strlen($target),
'url' => $url);
}
}
// Profile matches: @example.com/mublog/user
if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)*)!',
$text,
$wmatches,
PREG_OFFSET_CAPTURE)) {
foreach ($wmatches[1] as $wmatch) {
foreach (self::extractUrlMentions($text) as $wmatch) {
list($target, $pos) = $wmatch;
$schemes = array('http', 'https');
foreach ($schemes as $scheme) {
......@@ -357,7 +385,6 @@ class OStatusPlugin extends Plugin
}
}
}
}
foreach ($mentions as $i => $other) {
// If we share a common prefix with a local user, override it!
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment