Split up OStatusPlugin preg functions so they can be reused

cherry-pick-merge
This commit is contained in:
Mikael Nordfeldth 2017-04-22 10:58:14 +02:00
parent 45dfa9f215
commit eefbfe746f

View File

@ -272,6 +272,46 @@ class OStatusPlugin extends Plugin
return true; 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: * Find any explicit remote mentions. Accepted forms:
* Webfinger: @user@example.com * Webfinger: @user@example.com
@ -285,13 +325,7 @@ class OStatusPlugin extends Plugin
{ {
$matches = array(); $matches = array();
$wmatches = array(); foreach (self::extractWebfingerIds($text) as $wmatch) {
// 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) {
list($target, $pos) = $wmatch; list($target, $pos) = $wmatch;
$this->log(LOG_INFO, "Checking webfinger '$target'"); $this->log(LOG_INFO, "Checking webfinger '$target'");
$profile = null; $profile = null;
@ -312,7 +346,7 @@ class OStatusPlugin extends Plugin
assert($profile instanceof Profile); assert($profile instanceof Profile);
$text = !empty($profile->nickname) && mb_strlen($profile->nickname) < mb_strlen($target) $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; : $target;
$url = $profile->getUri(); $url = $profile->getUri();
if (!common_valid_http_url($url)) { if (!common_valid_http_url($url)) {
@ -325,14 +359,8 @@ class OStatusPlugin extends Plugin
'length' => mb_strlen($target), 'length' => mb_strlen($target),
'url' => $url); 'url' => $url);
} }
}
// Profile matches: @example.com/mublog/user foreach (self::extractUrlMentions($text) as $wmatch) {
if (preg_match_all('!(?:^|\s+)@((?:\w+\.)*\w+(?:\w+\-\w+)*\.\w+(?:/\w+)*)!',
$text,
$wmatches,
PREG_OFFSET_CAPTURE)) {
foreach ($wmatches[1] as $wmatch) {
list($target, $pos) = $wmatch; list($target, $pos) = $wmatch;
$schemes = array('http', 'https'); $schemes = array('http', 'https');
foreach ($schemes as $scheme) { foreach ($schemes as $scheme) {
@ -357,7 +385,6 @@ class OStatusPlugin extends Plugin
} }
} }
} }
}
foreach ($mentions as $i => $other) { foreach ($mentions as $i => $other) {
// If we share a common prefix with a local user, override it! // If we share a common prefix with a local user, override it!