[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2019 The s9e Authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Plugins\MediaEmbed; 9 use s9e\TextFormatter\Parser as TagStack; 10 use s9e\TextFormatter\Parser\Tag; 11 use s9e\TextFormatter\Plugins\ParserBase; 12 use s9e\TextFormatter\Utils\Http; 13 class Parser extends ParserBase 14 { 15 protected static $client; 16 protected static $clientCacheDir; 17 public function parse($text, array $matches) 18 { 19 foreach ($matches as $m) 20 { 21 $tagName = $this->config['tagName']; 22 $url = $m[0][0]; 23 $pos = $m[0][1]; 24 $len = \strlen($url); 25 $this->parser->addSelfClosingTag($tagName, $pos, $len, -10)->setAttribute('url', $url); 26 } 27 } 28 public static function filterTag(Tag $tag, TagStack $tagStack, array $hosts, array $sites, $cacheDir) 29 { 30 $tag->invalidate(); 31 if ($tag->hasAttribute('url')) 32 { 33 $url = $tag->getAttribute('url'); 34 $siteId = self::getSiteIdFromUrl($url, $hosts); 35 if (isset($sites[$siteId])) 36 { 37 $attributes = self::getAttributes($url, $sites[$siteId], $cacheDir); 38 if (!empty($attributes)) 39 self::createTag(\strtoupper($siteId), $tagStack, $tag)->setAttributes($attributes); 40 } 41 } 42 } 43 protected static function addNamedCaptures(array &$attributes, $string, array $regexps) 44 { 45 $matched = 0; 46 foreach ($regexps as $_53d26d37) 47 { 48 list($regexp, $map) = $_53d26d37; 49 $matched += \preg_match($regexp, $string, $m); 50 foreach ($map as $i => $name) 51 if (isset($m[$i]) && $m[$i] !== '' && $name !== '') 52 $attributes[$name] = $m[$i]; 53 } 54 return (bool) $matched; 55 } 56 protected static function createTag($tagName, TagStack $tagStack, Tag $tag) 57 { 58 $startPos = $tag->getPos(); 59 $endTag = $tag->getEndTag(); 60 if ($endTag) 61 { 62 $startLen = $tag->getLen(); 63 $endPos = $endTag->getPos(); 64 $endLen = $endTag->getLen(); 65 } 66 else 67 { 68 $startLen = 0; 69 $endPos = $tag->getPos() + $tag->getLen(); 70 $endLen = 0; 71 } 72 return $tagStack->addTagPair($tagName, $startPos, $startLen, $endPos, $endLen, $tag->getSortPriority()); 73 } 74 protected static function getAttributes($url, array $config, $cacheDir) 75 { 76 $attributes = []; 77 self::addNamedCaptures($attributes, $url, $config[0]); 78 foreach ($config[1] as $scrapeConfig) 79 self::scrape($attributes, $url, $scrapeConfig, $cacheDir); 80 return $attributes; 81 } 82 protected static function getHttpClient($cacheDir) 83 { 84 if (!isset(self::$client) || self::$clientCacheDir !== $cacheDir) 85 { 86 self::$client = (isset($cacheDir)) ? Http::getCachingClient($cacheDir) : Http::getClient(); 87 self::$clientCacheDir = $cacheDir; 88 } 89 return self::$client; 90 } 91 protected static function getSiteIdFromUrl($url, array $hosts) 92 { 93 $host = (\preg_match('(^https?://([^/]+))', \strtolower($url), $m)) ? $m[1] : ''; 94 while ($host > '') 95 { 96 if (isset($hosts[$host])) 97 return $hosts[$host]; 98 $host = \preg_replace('(^[^.]*.)', '', $host); 99 } 100 return ''; 101 } 102 protected static function interpolateVars($str, array $vars) 103 { 104 return \preg_replace_callback( 105 '(\\{@(\\w+)\\})', 106 function ($m) use ($vars) 107 { 108 return (isset($vars[$m[1]])) ? $vars[$m[1]] : ''; 109 }, 110 $str 111 ); 112 } 113 protected static function scrape(array &$attributes, $url, array $config, $cacheDir) 114 { 115 $vars = []; 116 if (self::addNamedCaptures($vars, $url, $config['match'])) 117 { 118 if (isset($config['url'])) 119 $url = self::interpolateVars($config['url'], $vars + $attributes); 120 if (\preg_match('(^https?://[^#]+)i', $url, $m)) 121 { 122 $response = self::wget($m[0], $cacheDir, $config); 123 self::addNamedCaptures($attributes, $response, $config['extract']); 124 } 125 } 126 } 127 protected static function wget($url, $cacheDir, $config) 128 { 129 $headers = (isset($config['header'])) ? (array) $config['header'] : []; 130 return @self::getHttpClient($cacheDir)->get($url, $headers); 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |