Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/rule/PhabricatorYoutubeRemarkupRule.php
12241 views
1
<?php
2
3
final class PhabricatorYoutubeRemarkupRule extends PhutilRemarkupRule {
4
5
public function getPriority() {
6
return 350.0;
7
}
8
9
public function apply($text) {
10
try {
11
$uri = new PhutilURI($text);
12
} catch (Exception $ex) {
13
return $text;
14
}
15
16
$domain = $uri->getDomain();
17
if (!preg_match('/(^|\.)youtube\.com\z/', $domain)) {
18
return $text;
19
}
20
21
$v_params = array();
22
23
$params = $uri->getQueryParamsAsPairList();
24
foreach ($params as $pair) {
25
list($k, $v) = $pair;
26
if ($k === 'v') {
27
$v_params[] = $v;
28
}
29
}
30
31
if (count($v_params) !== 1) {
32
return $text;
33
}
34
35
$v_param = head($v_params);
36
37
$text_mode = $this->getEngine()->isTextMode();
38
$mail_mode = $this->getEngine()->isHTMLMailMode();
39
40
if ($text_mode || $mail_mode) {
41
return $text;
42
}
43
44
$youtube_src = 'https://www.youtube.com/embed/'.$v_param;
45
46
$iframe = $this->newTag(
47
'div',
48
array(
49
'class' => 'embedded-youtube-video',
50
),
51
$this->newTag(
52
'iframe',
53
array(
54
'width' => '650',
55
'height' => '400',
56
'style' => 'margin: 1em auto; border: 0px;',
57
'src' => $youtube_src,
58
'frameborder' => 0,
59
),
60
''));
61
62
return $this->getEngine()->storeText($iframe);
63
}
64
65
public function didMarkupText() {
66
CelerityAPI::getStaticResourceResponse()
67
->addContentSecurityPolicyURI('frame-src', 'https://www.youtube.com/');
68
}
69
70
}
71
72