PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 2.0.3
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v2.0.3
4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / ostraining / embera / Lib / Embera / Embera.php
embedpress / vendor / ostraining / embera / Lib / Embera Last commit date
Adapters 8 years ago Providers 8 years ago Autoload.php 8 years ago Embera.php 8 years ago FakeResponse.php 8 years ago Formatter.php 8 years ago HtmlProcessor.php 8 years ago HttpRequest.php 8 years ago Oembed.php 8 years ago Providers.php 8 years ago Url.php 8 years ago
Embera.php
289 lines
1 <?php
2 /**
3 * Embera.php
4 *
5 * @package Embera
6 * @author Michael Pratt <pratt@hablarmierda.net>
7 * @link http://www.michael-pratt.com/
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace Embera;
14
15 /**
16 * The Main Class of this library
17 */
18 class Embera
19 {
20 /** @var int Class constant with the current Version of this library */
21 const VERSION = '1.9.3';
22
23 /** @var object Instance of \Embera\Oembed */
24 protected $oembed;
25
26 /** @var object Instance of \Embera\Providers */
27 protected $providers;
28
29 /** @var array Configuration Settings */
30 protected $config = array();
31
32 /** @var array Fetched errors */
33 protected $errors = array();
34
35 /** @var string The pattern used to extract urls from a text, ignoring text inside quotes */
36 protected $urlRegex = '~(?<!"|\')\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_))(?!"|\')~i';
37
38 /** @var string The pattern used to extract urls from a text when the embed:// prefix option is enabled,
39 ignoring text inside quotes */
40 protected $urlEmbedRegex = '~(?<!"|\')\bembed://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/|_))(?!"|\')~i';
41
42 /**
43 * Constructs the object and also instantiates the \Embera\Oembed Object
44 * and stores it into the $oembed properoty
45 *
46 * @param array $config
47 */
48 public function __construct(array $config = array())
49 {
50 $this->config = self::_recursiveMergeOverwrite(array(
51 /**
52 * The oembed setting represents the behaviour of the library.
53 * The default value (since version 1.8.0) is null, which means that the
54 * library will first try to get the data from the oembed endpoint and if that fails
55 * it tries to use a fake response.
56 *
57 * When true is given it ONLY tries to get the info from the endpoint directly... It DOES NOT fallback
58 * to fake responses.
59 *
60 * Finally, when false is given it ONLY uses fake responses.
61 */
62 'oembed' => null,
63 'use_embed_prefix' => false,
64 'params' => array(
65 'width' => 0,
66 'maxwidth' => 0,
67 'height' => 0,
68 'maxheight' => 0,
69 ),
70 'custom_params' => array(),
71 'http' => array(),
72 'fake' => array(),
73 'ignore_tags' => array('pre', 'code', 'a', 'img'),
74 ), $config);
75
76
77 $this->config['params']['maxwidth'] = max(
78 $this->config['params']['width'],
79 $this->config['params']['maxwidth']
80 );
81
82 $this->config['params']['maxheight'] = max(
83 $this->config['params']['height'],
84 $this->config['params']['maxheight']
85 );
86
87 unset($this->config['params']['height'], $this->config['params']['width']);
88
89 $this->oembed = new \Embera\Oembed(new \Embera\HttpRequest($this->config['http']));
90 $this->providers = new \Embera\Providers($this->config, $this->oembed);
91 }
92
93 /**
94 * Merges deep associative arrays.
95 *
96 * @access protected
97 * @author Oliver Lillie
98 * @param array $array1
99 * @param array $array2
100 * @return array
101 */
102 protected static function _recursiveMergeOverwrite($array1, $array2)
103 {
104 foreach($array2 as $key => $val)
105 {
106 if(array_key_exists($key, $array1) === true && is_array($val) === true)
107 {
108 $array1[$key] = self::_recursiveMergeOverwrite($array1[$key], $val);
109 }
110 else
111 {
112 $array1[$key] = $val;
113 }
114 }
115
116 return $array1;
117 }
118
119 /**
120 * Embeds known/available services into the
121 * given text.
122 *
123 * @param string $body
124 * @return string
125 */
126 public function autoEmbed($body = null)
127 {
128 if (!is_string($body)) {
129 $this->errors[] = 'For auto-embedding purposes, the input must be a string';
130 } elseif ($data = $this->getUrlInfo($body)) {
131 $table = array();
132 foreach ($data as $url => $service) {
133 if (!empty($service['html'])) {
134 $table[$url] = $service['html'];
135 }
136 }
137
138 // Determine wether the body looks like HTML or just plain text.
139 if (strpos($body, '>') !== false) {
140 $processor = new \Embera\HtmlProcessor($this->config['ignore_tags'], $table);
141 return $processor->process($body);
142 }
143
144 return strtr($body, $table);
145 }
146
147 return $body;
148 }
149
150 /**
151 * Finds all the information about a url (or a collection of urls)
152 *
153 * @param string|array $body An array or string with urls
154 * @return array
155 */
156 public function getUrlInfo($body = null)
157 {
158 $results = array();
159 if ($providers = $this->getProviders($body)) {
160 foreach ($providers as $url => $service) {
161 $info = $service->getInfo();
162
163 // Check if we don't have a provider_name set, and set it based on the class name
164 if (!isset($info['provider_name'])) {
165 $reflect = new \ReflectionClass($service);
166 $info['provider_name'] = $reflect->getShortName();
167 unset($reflect);
168 }
169
170 // Add the provider_alias if not exists
171 if (!isset($info['provider_alias'])) {
172 $info['provider_alias'] = preg_replace('/[^a-z0-9\-]/i', '-', $info['provider_name']);
173 $info['provider_alias'] = strtolower(str_replace('--', '-', $info['provider_alias']));
174 }
175
176 // Add the wrapper_class if not exists
177 if (!isset($info['wrapper_class'])) {
178 $info['wrapper_class'] = '';
179 }
180
181 $results[$url] = $info;
182 $this->errors = array_merge($this->errors, $service->getErrors());
183 }
184 }
185
186 return array_filter($results);
187 }
188
189 /**
190 * Finds all the valid urls inside the given text,
191 * compares which are allowed and returns an array
192 * with the detected providers
193 *
194 * @param null|string|array $body An array or string with Urls
195 * @return array
196 */
197 protected function getProviders($body = '')
198 {
199 $regex = ($this->config['use_embed_prefix'] === true ? $this->urlEmbedRegex : $this->urlRegex);
200 if (is_array($body)) {
201
202 $body = array_filter($body, function ($arr) use ($regex) {
203 return preg_match($regex, $arr);
204 });
205
206 $services = $this->providers->getAll($body);
207 } elseif (preg_match_all($regex, $body, $matches)) {
208 $services = $this->providers->getAll($matches['0']);
209 } else {
210 return array();
211 }
212
213 return $this->clean($services);
214 }
215
216 /**
217 * Adds a new Provider into the service map
218 *
219 * @param string $host The host for the map
220 * @param string|object $class The class or object that should manage the provider
221 * @param array $params Custom parameters that should be sent in the url for this Provider
222 */
223 public function addProvider($host, $class, array $params = array())
224 {
225 $this->providers->addProvider($host, $class, $params);
226 }
227
228 /**
229 * Strips invalid providers from the list
230 *
231 * @param array $services
232 * @return array
233 */
234 protected function clean(array $services = array())
235 {
236 if (empty($services)) {
237 return array();
238 }
239
240 if (!empty($this->config['allow'])) {
241 $allow = array_map('strtolower', (array) $this->config['allow']);
242 $services = array_filter($services, function ($arr) use ($allow) {
243 $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
244 return (in_array($serviceName, $allow));
245 });
246 }
247
248 if (!empty($services) && !empty($this->config['deny'])) {
249 $deny = array_map('strtolower', (array) $this->config['deny']);
250 $services = array_filter($services, function ($arr) use ($deny) {
251 $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
252 return (!in_array($serviceName, $deny));
253 });
254 }
255
256 return (array) $services;
257 }
258
259 /**
260 * Gets the last error found
261 *
262 * @return string
263 */
264 public function getLastError()
265 {
266 return end($this->errors);
267 }
268
269 /**
270 * Returns an array with all the errors
271 *
272 * @return array
273 */
274 public function getErrors()
275 {
276 return $this->errors;
277 }
278
279 /**
280 * Checks if there were errors
281 *
282 * @return bool
283 */
284 public function hasErrors()
285 {
286 return (!empty($this->errors));
287 }
288 }
289