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 |