AbstractMethods.php
6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* @package OSYouTube
* @contact www.joomlashack.com, help@joomlashack.com
* @copyright 2017 Open Source Training, LLC. All rights reserved
* @license http://www.gnu.org/licenses/gpl.html GNU/GPL
*
* This file is part of OSYouTube.
*
* OSYouTube is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* OSYouTube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OSYouTube. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Alledia\OSYouTube;
use Alledia\Framework\Joomla\Extension\AbstractPlugin;
use JHtml;
use Joomla\Registry\Registry;
use Joomla\String\StringHelper;
use Joomla\Utilities\ArrayHelper;
defined('_JEXEC') or die();
abstract class AbstractMethods
{
const LINK = 'link';
const IGNORE = 'ignore';
/**
* @var string
*/
protected $tokenIgnore = '::ignore::';
/**
* @var Registry
*/
protected $params = null;
public function __construct(AbstractPlugin $parent)
{
$this->params = $parent->params;
}
/**
* @param string $context
* @param object $article
* @param object $params
* @param int $page
*
* @return bool
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
$search = $this->getSearches();
$ignoreHtmlLinks = $this->params->get('ignore_html_links', 0);
foreach ($search as $type => $regexes) {
foreach ($regexes as $i => $regex) {
if (preg_match_all($regex, $article->text, $matches)) {
foreach ($matches[0] as $k => $source) {
if ($type == static::LINK && $ignoreHtmlLinks) {
// Attach the token to ignore the URL
$this->addTokenToIgnoreURL($source, $article->text);
} else {
// Parse the URL
$urlHash = @$matches[2][$k];
$videoCode = $matches[1][$k];
$embedCode = $this->youtubeCodeEmbed($videoCode, $urlHash);
if ($ignoreHtmlLinks) {
// Must pay attention to ignored links here
$matchString = '#(?<!' . $this->tokenIgnore . ')' . preg_quote($source, '#') . '#';
$article->text = preg_replace($matchString, $embedCode, $article->text);
} else {
// Don't care, do the faster replace
$article->text = str_replace($source, $embedCode, $article->text);
}
}
}
}
}
}
// Remove all "ignore" tokens from the text
if ($ignoreHtmlLinks) {
$this->removeTokensToIgnoreURL($article->text);
}
return true;
}
/**
* Load the regular expressions to search for in the text
*
* @return array[]
*/
protected function getSearches()
{
$searches = array(
static::LINK => array(
'#(?:<a.*?href=["\'](?:https?://(?:www\.)?youtube.com/watch\?v=([^\'"\#]+)(\#[^\'"\#]*)?[\'"][^>]*>(.+)?(?:</a>)))#'
),
static::IGNORE => array(
'#(?<!' . $this->tokenIgnore . ')https?://(?:www\.)?youtube.com/watch\?v=([a-zA-Z0-9-_&;=]+)(\#[a-zA-Z0-9-_&;=]*)?#'
)
);
return $searches;
}
protected function addTokenToIgnoreURL($tag, &$text)
{
$newTag = preg_replace('#(https?://)#i', $this->tokenIgnore . '$1', $tag);
$text = str_replace($tag, $newTag, $text);
}
protected function removeTokensToIgnoreURL(&$text)
{
$text = str_replace($this->tokenIgnore, '', $text);
}
protected function youtubeCodeEmbed($videoCode, $urlHash = null)
{
$output = '';
$params = $this->params;
$width = $params->get('width', 425);
$height = $params->get('height', 344);
$responsive = $params->get('responsive', 1);
if ($responsive) {
JHtml::_('stylesheet', 'plugins/content/osyoutube/style.css');
$output .= '<div class="video-responsive">';
}
$query = explode('&', htmlspecialchars_decode($videoCode));
$videoCode = array_shift($query);
$attribs = array(
'id' => 'youtube_' . $videoCode,
'width' => $width,
'height' => $height,
'frameborder' => '0',
'src' => $this->getUrl($params, $videoCode, $query, $urlHash)
);
$output .= '<iframe ' . ArrayHelper::toString($attribs) . ' allowfullscreen></iframe>';
if ($responsive) {
$output .= '</div>';
}
return $output;
}
/**
* @param Registry $params
* @param array $query
* @param string $videoCode
*
* @return array
*/
protected static function buildUrlQuery($params, $query, $videoCode = null)
{
// Converts the query in an associative array
$queryAssoc = array();
if (!empty($query)) {
foreach ($query as $key => $value) {
if (is_numeric($key)) {
$value = explode('=', $value);
if (!isset($value[1])) {
$queryAssoc[$value[0]] = 'true';
} else {
$queryAssoc[$value[0]] = $value[1];
}
}
}
}
return $queryAssoc;
}
/**
* @param Registry $params
* @param string $videoCode
* @param array $query
* @param string $hash
*
* @return string
*/
protected static function getUrl($params, $videoCode, $query = array(), $hash = null)
{
$url = 'https://www.youtube.com/embed/' . $videoCode . '?wmode=transparent';
$query = static::buildUrlQuery($params, $query, $videoCode);
if (!empty($query)) {
$url .= '&' . http_build_query($query);
}
if (!empty($hash)) {
$url .= $hash;
}
return $url;
}
}