Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gnu-social
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
192
Issues
192
List
Board
Labels
Milestones
Merge Requests
12
Merge Requests
12
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
gnu.io
gnu-social
Commits
eefbfe74
Commit
eefbfe74
authored
Apr 22, 2017
by
mmn
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Split up OStatusPlugin preg functions so they can be reused
cherry-pick-merge
parent
45dfa9f2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
62 deletions
+89
-62
OStatusPlugin.php
plugins/OStatus/OStatusPlugin.php
+89
-62
No files found.
plugins/OStatus/OStatusPlugin.php
View file @
eefbfe74
...
@@ -272,6 +272,46 @@ class OStatusPlugin extends Plugin
...
@@ -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,76 +325,63 @@ class OStatusPlugin extends Plugin
...
@@ -285,76 +325,63 @@ 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
list
(
$target
,
$pos
)
=
$wmatch
;
if
(
preg_match_all
(
'/(?:^|\s+)@((?:\w+[\w\-\_\.]?)*(?:[\w\-\_\.]*\w+)@(?:(?!-)[A-Za-z0-9\-]{1,63}(?<!-)\.)+[A-Za-z]{2,10})/'
,
$this
->
log
(
LOG_INFO
,
"Checking webfinger '
$target
'"
);
$text
,
$profile
=
null
;
$wmatches
,
try
{
PREG_OFFSET_CAPTURE
))
{
$oprofile
=
Ostatus_profile
::
ensureWebfinger
(
$target
);
foreach
(
$wmatches
[
1
]
as
$wmatch
)
{
if
(
!
$oprofile
instanceof
Ostatus_profile
||
!
$oprofile
->
isPerson
())
{
list
(
$target
,
$pos
)
=
$wmatch
;
$this
->
log
(
LOG_INFO
,
"Checking webfinger '
$target
'"
);
$profile
=
null
;
try
{
$oprofile
=
Ostatus_profile
::
ensureWebfinger
(
$target
);
if
(
!
$oprofile
instanceof
Ostatus_profile
||
!
$oprofile
->
isPerson
())
{
continue
;
}
$profile
=
$oprofile
->
localProfile
();
}
catch
(
OStatusShadowException
$e
)
{
// This means we got a local user in the webfinger lookup
$profile
=
$e
->
profile
;
}
catch
(
Exception
$e
)
{
$this
->
log
(
LOG_ERR
,
"Webfinger check failed: "
.
$e
->
getMessage
());
continue
;
continue
;
}
}
$profile
=
$oprofile
->
localProfile
();
}
catch
(
OStatusShadowException
$e
)
{
// This means we got a local user in the webfinger lookup
$profile
=
$e
->
profile
;
}
catch
(
Exception
$e
)
{
$this
->
log
(
LOG_ERR
,
"Webfinger check failed: "
.
$e
->
getMessage
());
continue
;
}
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
))
{
$url
=
$profile
->
getUrl
();
$url
=
$profile
->
getUrl
();
}
$matches
[
$pos
]
=
array
(
'mentioned'
=>
array
(
$profile
),
'type'
=>
'mention'
,
'text'
=>
$text
,
'position'
=>
$pos
,
'length'
=>
mb_strlen
(
$target
),
'url'
=>
$url
);
}
}
$matches
[
$pos
]
=
array
(
'mentioned'
=>
array
(
$profile
),
'type'
=>
'mention'
,
'text'
=>
$text
,
'position'
=>
$pos
,
'length'
=>
mb_strlen
(
$target
),
'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+)*)!'
,
list
(
$target
,
$pos
)
=
$wmatch
;
$text
,
$schemes
=
array
(
'http'
,
'https'
);
$wmatches
,
foreach
(
$schemes
as
$scheme
)
{
PREG_OFFSET_CAPTURE
))
{
$url
=
"
$scheme
://
$target
"
;
foreach
(
$wmatches
[
1
]
as
$wmatch
)
{
$this
->
log
(
LOG_INFO
,
"Checking profile address '
$url
'"
);
list
(
$target
,
$pos
)
=
$wmatch
;
try
{
$schemes
=
array
(
'http'
,
'https'
);
$oprofile
=
Ostatus_profile
::
ensureProfileURL
(
$url
);
foreach
(
$schemes
as
$scheme
)
{
if
(
$oprofile
instanceof
Ostatus_profile
&&
!
$oprofile
->
isGroup
())
{
$url
=
"
$scheme
://
$target
"
;
$profile
=
$oprofile
->
localProfile
();
$this
->
log
(
LOG_INFO
,
"Checking profile address '
$url
'"
);
$text
=
!
empty
(
$profile
->
nickname
)
&&
mb_strlen
(
$profile
->
nickname
)
<
mb_strlen
(
$target
)
?
try
{
$profile
->
nickname
:
$target
;
$oprofile
=
Ostatus_profile
::
ensureProfileURL
(
$url
);
$matches
[
$pos
]
=
array
(
'mentioned'
=>
array
(
$profile
),
if
(
$oprofile
instanceof
Ostatus_profile
&&
!
$oprofile
->
isGroup
())
{
'type'
=>
'mention'
,
$profile
=
$oprofile
->
localProfile
();
'text'
=>
$text
,
$text
=
!
empty
(
$profile
->
nickname
)
&&
mb_strlen
(
$profile
->
nickname
)
<
mb_strlen
(
$target
)
?
'position'
=>
$pos
,
$profile
->
nickname
:
$target
;
'length'
=>
mb_strlen
(
$target
),
$matches
[
$pos
]
=
array
(
'mentioned'
=>
array
(
$profile
),
'url'
=>
$profile
->
getUrl
());
'type'
=>
'mention'
,
break
;
'text'
=>
$text
,
'position'
=>
$pos
,
'length'
=>
mb_strlen
(
$target
),
'url'
=>
$profile
->
getUrl
());
break
;
}
}
catch
(
Exception
$e
)
{
$this
->
log
(
LOG_ERR
,
"Profile check failed: "
.
$e
->
getMessage
());
}
}
}
catch
(
Exception
$e
)
{
$this
->
log
(
LOG_ERR
,
"Profile check failed: "
.
$e
->
getMessage
());
}
}
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment