Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Parser.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 4.82 KiB


001  <?php
002   
003  /*
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2016 The s9e Authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Plugins\MediaEmbed;
009  use s9e\TextFormatter\Parser as TagStack;
010  use s9e\TextFormatter\Parser\Tag;
011  use s9e\TextFormatter\Plugins\ParserBase;
012  use s9e\TextFormatter\Utils\Http;
013  class Parser extends ParserBase
014  {
015      protected static $client;
016      public function parse($text, array $matches)
017      {
018          foreach ($matches as $m)
019          {
020              $url = $m[0][0];
021              $pos = $m[0][1];
022              $len = \strlen($url);
023              $tag = $this->parser->addSelfClosingTag($this->config['tagName'], $pos, $len, -10);
024              $tag->setAttribute('url', $url);
025          }
026      }
027      public static function filterTag(Tag $tag, TagStack $tagStack, array $sites)
028      {
029          if ($tag->hasAttribute('site'))
030              self::addTagFromMediaId($tag, $tagStack, $sites);
031          elseif ($tag->hasAttribute('url'))
032              self::addTagFromMediaUrl($tag, $tagStack, $sites);
033          return \false;
034      }
035      public static function hasNonDefaultAttribute(Tag $tag)
036      {
037          foreach ($tag->getAttributes() as $attrName => $void)
038              if ($attrName !== 'url')
039                  return \true;
040          return \false;
041      }
042      public static function scrape(Tag $tag, array $scrapeConfig, $cacheDir = \null)
043      {
044          if ($tag->hasAttribute('url'))
045          {
046              $url = $tag->getAttribute('url');
047              if (\preg_match('#^https?://[^<>"\'\\s]+$#D', $url))
048                  foreach ($scrapeConfig as $scrape)
049                      self::scrapeEntry($url, $tag, $scrape, $cacheDir);
050          }
051          return \true;
052      }
053      protected static function addSiteTag(Tag $tag, TagStack $tagStack, $siteId)
054      {
055          $endTag = $tag->getEndTag() ?: $tag;
056          $lpos = $tag->getPos();
057          $rpos = $endTag->getPos() + $endTag->getLen();
058          $tagStack->addTagPair(\strtoupper($siteId), $lpos, 0, $rpos, 0, $tag->getSortPriority())->setAttributes($tag->getAttributes());
059      }
060      protected static function addTagFromMediaId(Tag $tag, TagStack $tagStack, array $sites)
061      {
062          $siteId = \strtolower($tag->getAttribute('site'));
063          if (\in_array($siteId, $sites, \true))
064              self::addSiteTag($tag, $tagStack, $siteId);
065      }
066      protected static function addTagFromMediaUrl(Tag $tag, TagStack $tagStack, array $sites)
067      {
068          $p = \parse_url($tag->getAttribute('url'));
069          if (isset($p['scheme']) && isset($sites[$p['scheme'] . ':']))
070              $siteId = $sites[$p['scheme'] . ':'];
071          elseif (isset($p['host']))
072              $siteId = self::findSiteIdByHost($p['host'], $sites);
073          if (!empty($siteId))
074              self::addSiteTag($tag, $tagStack, $siteId);
075      }
076      protected static function findSiteIdByHost($host, array $sites)
077      {
078          do
079          {
080              if (isset($sites[$host]))
081                  return $sites[$host];
082              $pos = \strpos($host, '.');
083              if ($pos === \false)
084                  break;
085              $host = \substr($host, 1 + $pos);
086          }
087          while ($host > '');
088          return \false;
089      }
090      protected static function getHttpClient()
091      {
092          if (!isset(self::$client))
093              self::$client = Http::getClient();
094          self::$client->timeout = 10;
095          return self::$client;
096      }
097      protected static function replaceTokens($url, array $vars)
098      {
099          return \preg_replace_callback(
100              '#\\{@(\\w+)\\}#',
101              function ($m) use ($vars)
102              {
103                  return (isset($vars[$m[1]])) ? $vars[$m[1]] : '';
104              },
105              $url
106          );
107      }
108      protected static function scrapeEntry($url, Tag $tag, array $scrape, $cacheDir)
109      {
110          list($matchRegexps, $extractRegexps, $attrNames) = $scrape;
111          if (!self::tagIsMissingAnyAttribute($tag, $attrNames))
112              return;
113          $vars    = array();
114          $matched = \false;
115          foreach ((array) $matchRegexps as $matchRegexp)
116              if (\preg_match($matchRegexp, $url, $m))
117              {
118                  $vars   += $m;
119                  $matched = \true;
120              }
121          if (!$matched)
122              return;
123          $vars += $tag->getAttributes();
124          $scrapeUrl = (isset($scrape[3])) ? self::replaceTokens($scrape[3], $vars) : $url;
125          self::scrapeUrl($scrapeUrl, $tag, (array) $extractRegexps, $cacheDir);
126      }
127      protected static function scrapeUrl($url, Tag $tag, array $regexps, $cacheDir)
128      {
129          $content = self::wget($url, $cacheDir);
130          foreach ($regexps as $regexp)
131              if (\preg_match($regexp, $content, $m))
132                  foreach ($m as $k => $v)
133                      if (!\is_numeric($k) && !$tag->hasAttribute($k))
134                          $tag->setAttribute($k, $v);
135      }
136      protected static function tagIsMissingAnyAttribute(Tag $tag, array $attrNames)
137      {
138          foreach ($attrNames as $attrName)
139              if (!$tag->hasAttribute($attrName))
140                  return \true;
141          return \false;
142      }
143      protected static function wget($url, $cacheDir = \null)
144      {
145          $prefix = '';
146          $url    = \preg_replace('(#.*)s', '', $url);
147          if (isset($cacheDir) && \file_exists($cacheDir))
148          {
149              $cacheFile = $cacheDir . '/http.' . \crc32($url);
150              if (\extension_loaded('zlib'))
151              {
152                  $prefix     = 'compress.zlib://';
153                  $cacheFile .= '.gz';
154              }
155              if (\file_exists($cacheFile))
156                  return \file_get_contents($prefix . $cacheFile);
157          }
158          $content = @self::getHttpClient()->get($url, array('User-Agent: PHP (not Mozilla)'));
159          if (isset($cacheFile) && !empty($content))
160              \file_put_contents($prefix . $cacheFile, $content);
161          return $content;
162      }
163  }