Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F115354831
PhutilRemarkupRuleDocumentLink.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, May 31, 09:22
Size
1 KB
Mime Type
text/x-php
Expires
Mon, Jun 2, 09:22 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
26473870
Attached To
rPHU libphutil
PhutilRemarkupRuleDocumentLink.php
View Options
<?php
/*
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @group markup
*/
final
class
PhutilRemarkupRuleDocumentLink
extends
PhutilRemarkupRule
{
public
function
apply
(
$text
)
{
$text
=
preg_replace_callback
(
'@
\B\\
[
\\
[([^|
\\
]]+)(?:
\\
|([^
\\
]]+))?
\\
]
\\
]
\B
@U'
,
array
(
$this
,
'markupDocumentLink'
),
$text
);
return
$text
;
}
protected
function
renderHyperlink
(
$link
,
$name
)
{
return
phutil_render_tag
(
'a'
,
array
(
'href'
=>
$link
,
'target'
=>
'_blank'
,
),
phutil_escape_html
(
$name
));
}
public
function
markupDocumentLink
(
$matches
)
{
$uri
=
trim
(
$matches
[
1
]);
$name
=
trim
(
idx
(
$matches
,
2
,
$uri
));
// If whatever is being linked to begins with "/" or has "://", treat it
// as a URI instead of a wiki page.
$is_uri
=
preg_match
(
'@(^/)|(://)@'
,
$uri
);
if
(
$is_uri
&&
strncmp
(
'/'
,
$uri
,
1
))
{
$protocols
=
$this
->
getEngine
()->
getConfig
(
'uri.allowed-protocols'
,
array
());
$protocol
=
id
(
new
PhutilURI
(
$uri
))->
getProtocol
();
if
(!
idx
(
$protocols
,
$protocol
))
{
// Don't treat this as a URI if it's not an allowed protocol.
$is_uri
=
false
;
}
}
if
(!
$is_uri
)
{
return
$matches
[
0
];
}
return
$this
->
getEngine
()->
storeText
(
$this
->
renderHyperlink
(
$uri
,
$name
));
}
}
Event Timeline
Log In to Comment