PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 1.4.2
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v1.4.2
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 / library / ostraining / embera / Lib / Embera / Embera.php
embedpress / library / ostraining / embera / Lib / Embera Last commit date
Adapters 9 years ago Providers 9 years ago Autoload.php 9 years ago Embera.php 9 years ago FakeResponse.php 9 years ago Formatter.php 9 years ago HttpRequest.php 9 years ago Oembed.php 9 years ago Providers.php 9 years ago Url.php 9 years ago
Embera.php
259 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.8.18';
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 */
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 protected $urlEmbedRegex = '~\bembed://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))~i';
40
41 /**
42 * Constructs the object and also instantiates the \Embera\Oembed Object
43 * and stores it into the $oembed properoty
44 *
45 * @param array $config
46 * @return void
47 */
48 public function __construct(array $config = array())
49 {
50 $this->config = array_replace_recursive(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 ), $config);
74
75
76 $this->config['params']['maxwidth'] = max(
77 $this->config['params']['width'],
78 $this->config['params']['maxwidth']
79 );
80
81 $this->config['params']['maxheight'] = max(
82 $this->config['params']['height'],
83 $this->config['params']['maxheight']
84 );
85
86 unset($this->config['params']['height'], $this->config['params']['width']);
87
88 $this->oembed = new \Embera\Oembed(new \Embera\HttpRequest($this->config['http']));
89 $this->providers = new \Embera\Providers($this->config, $this->oembed);
90 }
91
92 /**
93 * Embeds known/available services into the
94 * given text.
95 *
96 * @param string $body
97 * @return string
98 */
99 public function autoEmbed($body = null)
100 {
101 if (!is_string($body)) {
102 $this->errors[] = 'For auto-embedding purposes, the input must be a string';
103 } elseif ($data = $this->getUrlInfo($body)) {
104 $table = array();
105 foreach ($data as $url => $service) {
106 if (!empty($service['html'])) {
107 $table[$url] = $service['html'];
108 }
109 }
110
111 return strtr($body, $table);
112 }
113
114 return $body;
115 }
116
117 /**
118 * Finds all the information about a url (or a collection of urls)
119 *
120 * @param string|array $body An array or string with urls
121 * @return array
122 */
123 public function getUrlInfo($body = null)
124 {
125 $results = array();
126 if ($providers = $this->getProviders($body)) {
127 foreach ($providers as $url => $service) {
128 $info = $service->getInfo();
129
130 // Check if we don't have a provider_name set, and set it based on the class name
131 if (!isset($info['provider_name'])) {
132 $reflect = new \ReflectionClass($service);
133 $info['provider_name'] = $reflect->getShortName();
134 unset($reflect);
135 }
136
137 // Add the provider_alias if not exists
138 if (!isset($info['provider_alias'])) {
139 $info['provider_alias'] = preg_replace('/[^a-z0-9\-]/i', '-', $info['provider_name']);
140 $info['provider_alias'] = strtolower(str_replace('--', '-', $info['provider_alias']));
141 }
142
143 // Add the wrapper_class if not exists
144 if (!isset($info['wrapper_class'])) {
145 $info['wrapper_class'] = '';
146 }
147
148 $results[$url] = $info;
149 $this->errors = array_merge($this->errors, $service->getErrors());
150 }
151 }
152
153 return array_filter($results);
154 }
155
156 /**
157 * Finds all the valid urls inside the given text,
158 * compares which are allowed and returns an array
159 * with the detected providers
160 *
161 * @param null|string|array $body An array or string with Urls
162 * @return array
163 */
164 protected function getProviders($body = '')
165 {
166 $regex = ($this->config['use_embed_prefix'] === true ? $this->urlEmbedRegex : $this->urlRegex);
167 if (is_array($body)) {
168
169 $body = array_filter($body, function ($arr) use ($regex) {
170 return preg_match($regex, $arr);
171 });
172
173 $services = $this->providers->getAll($body);
174 } elseif (preg_match_all($regex, $body, $matches)) {
175 $services = $this->providers->getAll($matches['0']);
176 } else {
177 return array();
178 }
179
180 return $this->clean($services);
181 }
182
183 /**
184 * Adds a new Provider into the service map
185 *
186 * @param string $host The host for the map
187 * @param string|object $class The class or object that should manage the provider
188 * @param array $params Custom parameters that should be sent in the url for this Provider
189 * @return void
190 */
191 public function addProvider($host, $class, array $params = array())
192 {
193 $this->providers->addProvider($host, $class, $params);
194 }
195
196 /**
197 * Strips invalid providers from the list
198 *
199 * @param array $services
200 * @return array
201 */
202 protected function clean(array $services = array())
203 {
204 if (empty($services)) {
205 return array();
206 }
207
208 if (!empty($this->config['allow'])) {
209 $allow = array_map('strtolower', (array) $this->config['allow']);
210 $services = array_filter($services, function ($arr) use ($allow) {
211 $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
212 return (in_array($serviceName, $allow));
213 });
214 }
215
216 if (!empty($services) && !empty($this->config['deny'])) {
217 $deny = array_map('strtolower', (array) $this->config['deny']);
218 $services = array_filter($services, function ($arr) use ($deny) {
219 $serviceName = strtolower(basename(str_replace('\\', '/', get_class($arr))));
220 return (!in_array($serviceName, $deny));
221 });
222 }
223
224 return (array) $services;
225 }
226
227 /**
228 * Gets the last error found
229 *
230 * @return string
231 */
232 public function getLastError()
233 {
234 return end($this->errors);
235 }
236
237 /**
238 * Returns an array with all the errors
239 *
240 * @return array
241 */
242 public function getErrors()
243 {
244 return $this->errors;
245 }
246
247 /**
248 * Checks if there were errors
249 *
250 * @return bool
251 */
252 public function hasErrors()
253 {
254 return (!empty($this->errors));
255 }
256 }
257
258 ?>
259