Detects all links to file.php encoded via $@FILEPHP@$ and returns the files to migrate

@see self::migrate_referenced_files()
@param string $text
@return array
169115
public static function find_referenced_files($text) {

        $files = array();

        if (empty($text) or is_numeric($text)) {
            return $files;
        }

        $matches = array();
        $pattern = '|(["\'])(\$@FILEPHP@\$.+?)\1|';
        $result = preg_match_all($pattern, $text, $matches);
        if ($result === false) {
            throw new moodle1_convert_exception('error_while_searching_for_referenced_files');
        }
        if ($result == 0) {
            return $files;
        }
        foreach ($matches[2] as $match) {
            $file = str_replace(array('$@FILEPHP@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$'), array('', '/', ''), $match);
            if ($file === clean_param($file, PARAM_PATH)) {
                $files[] = rawurldecode($file);
            }
        }

        return array_unique($files);
    }
862704
public function extractMentions($text)
    {
        $matches = array();

        if (preg_match_all(ComPeopleDomainEntityPerson::PATTERN_MENTION, $text, $matches)) {
            return array_unique($matches[1]);
        } else {
            return array();
        }
    }
879997
static private function leetify( $text )
    {
        $searchMap = array( '/to/i', '/for/i', '/ate/i', '/your/i', '/you/i', '/l/i', '/e/i', '/o/i', '/a/i', '/t/i' );
        $replaceMap = array( '2', '4', '8', 'ur', 'u', '1', '3', '0', '4', '7' );

        $textBlocks = preg_split( '/(%[^ ]+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
        $newTextBlocks = array();
        foreach ( $textBlocks as $text )
        {
            if ( strlen( $text ) && $text[0] == '%' )
            {
                $newTextBlocks[] = (string) $text;
                continue;
            }
            $text = preg_replace( $searchMap, $replaceMap, $text );

            $newTextBlocks[] = (string) $text;
        }
        $text = implode( '', $newTextBlocks );
        return $text;
    }
925327
public static function makeUrls(string $text) : string
    {
        $urlPattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,6}(\/\S*)?/";
        preg_match_all($urlPattern, $text, $matches);
        $usedPatterns = array();
        foreach($matches[0] as $pattern){
            if(!array_key_exists($pattern, $usedPatterns)){
                $usedPatterns[$pattern]=true;
                // now try to catch last thing in text
                $text = str_replace($pattern, '<a href="'.$pattern.'" target="_blank">'.$pattern.'</a>', $text);
            }
        }
        return $text;
    }
1338929
public static function urlize($text)
    {
        $text = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\">\\2</a>", $text);
        $text = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\">\\2</a>", $text);
        $text = preg_replace("/@(\w+)/", "<a href=\"http://twitter.com/\\1\">@\\1</a>", $text);
        $text = preg_replace("/([^&]|^)#(\w+)/", "\\1<a href=\"http://twitter.com/search/\\2\">#\\2</a>", $text);

        return $text;
    }
802874
public function getMentions($text) {
    $mentions = [];
    $config_names = $this->mentionTypes;

    foreach ($config_names as $config_name) {
      $settings = $this->config->get('mentions.mentions_type.' . $config_name);
      $input_settings = [
        'prefix' => $settings->get('input.prefix'),
        'suffix' => $settings->get('input.suffix'),
        'entity_type' => $settings->get('input.entity_type'),
        'value' => $settings->get('input.inputvalue'),
      ];
      $this->inputSettings[$config_name] = $input_settings;

      if (!isset($input_settings['entity_type']) || empty($this->settings['mentions_filter'][$config_name])) {
        continue;
      }

      $output_settings = [
        'value' => $settings->get('output.outputvalue'),
        'renderlink' => (bool) $settings->get('output.renderlink'),
        'rendertextbox' => $settings->get('output.renderlinktextbox'),
      ];
      $this->outputSettings[$config_name] = $output_settings;
      $mention_type = $settings->get('mention_type');
      $mention = $this->mentionsManager->createInstance($mention_type);

      if ($mention instanceof MentionsPluginInterface) {
        $pattern = '/(?:' . preg_quote($input_settings['prefix']) . ')([a-zA-Z0-9_]+)' . preg_quote($input_settings['suffix']) . '/';
        preg_match_all($pattern, $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match) {
          $target = $mention->targetCallback($match[1], $input_settings);

          if ($target !== FALSE) {
            $mentions[$match[0]] = [
              'type' => $mention_type,
              'source' => [
                'string' => $match[0],
                'match' => $match[1],
              ],
              'target' => $target,
              'config_name' => $config_name,
            ];
          }
        }
      }
    }

    return $mentions;
  }
1355747
private function getTags(string $text): array
    {
        $tags    = [];
        $matches = [];

        preg_match_all('/#([^#]+)#/', $text, $matches, PREG_SET_ORDER);

        foreach ($matches as $match) {
            $params = explode(':', $match[1]);
            $method = array_shift($params);

            if (method_exists($this, $method)) {
                $tags[$match[0]] = $this->$method(...$params);
            }
        }

        return $tags;
    }
1319244
public function linkTo($text, $urlOptions, array $attrs = [])
    {
        $url = $this->parseUrlOptions($urlOptions);
        return $this->contentTag('a', $text, array_merge(['href' => $url], $attrs));
    }
41038
private function _decode($text)
    {
        $marc = new $this->record_class($this);

        // Store leader
        $marc->setLeader($text->leader);

        // go through all the control fields
        foreach ($text->controlfield as $controlfield) {
            $controlfieldattributes = $controlfield->attributes();
            $marc->appendField(new File_MARC_Control_Field((string)$controlfieldattributes['tag'], $controlfield));
        }

        // go through all the data fields
        foreach ($text->datafield as $datafield) {
            $datafieldattributes = $datafield->attributes();
            $subfield_data = array();
            foreach ($datafield->subfield as $subfield) {
                $subfieldattributes = $subfield->attributes();
                $subfield_data[] = new File_MARC_Subfield((string)$subfieldattributes['code'], $subfield);
            }
            
            // If the data is invalid, let's just ignore the one field
            try {
                $new_field = new File_MARC_Data_Field((string)$datafieldattributes['tag'], $subfield_data, $datafieldattributes['ind1'], $datafieldattributes['ind2']);
                $marc->appendField($new_field);
            } catch (Exception $e) {
                $marc->addWarning($e->getMessage());
            }
        }

        return $marc;
    }
1276882
public static function cleanText(&$text)
    {
        $text = preg_replace("'<script[^>]*>.*?</script>'si", '', $text);
        $text = preg_replace('/<a\s+.*?href="([^"]+)"[^>]*>([^<]+)<\/a>/is', '\2 (\1)', $text);
        $text = preg_replace('/<!--.+?-->/', '', $text);
        $text = preg_replace('/{.+?}/', '', $text);
        $text = preg_replace('/&nbsp;/', ' ', $text);
        $text = preg_replace('/&amp;/', ' ', $text);
        $text = preg_replace('/&quot;/', ' ', $text);
        $text = strip_tags($text);
        $text = htmlspecialchars($text, ENT_COMPAT, 'UTF-8');

        return $text;
    }
866405
static function strtolower( $text )
    {
        //We need to detect our internal charset
        if ( self::$charset === null )
        {
            self::$charset = eZTextCodec::internalCharset();
        }

        //First try to use mbstring
        if ( extension_loaded( 'mbstring' ) )
        {
            return mb_strtolower( $text, self::$charset );
        }
        else
        {
            // Fall back if mbstring is not available
            $char = eZCharTransform::instance();
            return $char->transformByGroup( $text, 'lowercase' );
        }
    }
506923
public function url($text)
    {
        return preg_replace_callback($this->pattern['url'], function ($matches) {
            return $this->Html->link($matches[2], $matches[1]);
        }, $text);
    }
274900
public static function fromText($text, $mode = null)
    {
        $text = nl2br(htmlentities($text, null, 'utf-8'));

        $text = self::createLinks($text);

        return $text;
    }
411097
function &_getInlineTags($text)
    {
        $return = NULL;
        $tagStrings = preg_split('/{(@.+)}/sU', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
        if ($tagStrings) {
            $inlineTags = NULL;
            $phpdoctor =& $this->_root->phpdoctor();
            foreach ($tagStrings as $tag) {
                if (substr($tag, 0, 1) == '@') {
                    $pos = strpos($tag, ' ');
                    if ($pos !== FALSE) {
                        $name = trim(substr($tag, 0, $pos));
                        $text = trim(substr($tag, $pos + 1));
                    } else {
                        $name = $tag;
                        $text = NULL;
                    }
                } else {
                    $name = '@text';
                    $text = $tag;
                }
                $data = NULL;
                $inlineTag =& $phpdoctor->createTag($name, $text, $data, $this->_root);
                $inlineTag->setParent($this->_parent);
                $inlineTags[] = $inlineTag;
            }
            $return =& $inlineTags;
        }

        return $return;
    }
97168
public static function parseAttributes($text)
    {
        $text = preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text);
        if (!preg_match_all(self::$attrPattern, $text, $matches, PREG_SET_ORDER)) {
            return array(ltrim($text));
        }
        $attr = array();
        foreach ($matches as $match) {
            if (!empty($match[1])) {
                $attr[strtolower($match[1])] = stripcslashes($match[2]);
            } elseif (!empty($match[3])) {
                $attr[strtolower($match[3])] = stripcslashes($match[4]);
            } elseif (!empty($match[5])) {
                $attr[strtolower($match[5])] = stripcslashes($match[6]);
            } elseif (isset($match[7]) && strlen($match[7])) {
                $attr[] = stripcslashes($match[7]);
            } elseif (isset($match[8])) {
                $attr[] = stripcslashes($match[8]);
            }
        }
        return (array)$attr;
    }
409952
public static function parseHeaders($text)
    {
        $curr = null;
        $headers = new Horde_Mime_Headers();
        $hdr_list = array();

        if ($text instanceof Horde_Stream) {
            $stream = $text;
            $stream->rewind();
        } else {
            $stream = new Horde_Stream_Temp();
            $stream->add($text, true);
        }

        while (!$stream->eof()) {
            if (!($val = rtrim($stream->getToChar("\n", false), "\r"))) {
                break;
            }

            if ($curr && (($val[0] == ' ') || ($val[0] == "\t"))) {
                $curr->text .= ' ' . ltrim($val);
            } else {
                $pos = strpos($val, ':');

                $curr = new stdClass;
                $curr->header = substr($val, 0, $pos);
                $curr->text = ltrim(substr($val, $pos + 1));

                $hdr_list[] = $curr;
            }
        }

        foreach ($hdr_list as $val) {
            /* When parsing, only keep the FIRST header seen for single value
             * text-only headers, since newer headers generally are appended
             * to the top of the message. */
            if (!($ob = $headers[$val->header]) ||
                !($ob instanceof Horde_Mime_Headers_Element_Single) ||
                ($ob instanceof Horde_Mime_Headers_Addresses)) {
                $headers->addHeader($val->header, rtrim($val->text));
            }
        }

        if (!($text instanceof Horde_Stream)) {
            $stream->close();
        }

        return $headers;
    }
720694
public function linkUrlsAndEscapeHtml($text)
	{
		// We can abort if there is no . in $text
		if ( strpos($text, '.') === false )
		{
			return $this->escapeHtml($text);
		}

		$html = '';

		$position = 0;

		$match = array();

		while (preg_match($this->buildRegex(), $text, $match, PREG_OFFSET_CAPTURE, $position))
		{
			list($url, $urlPosition) = $match[0];

			// Add the text leading up to the URL.
			$html .= $this->escapeHtml(substr($text, $position, $urlPosition - $position));

			$scheme      = $match[1][0];
			$username    = $match[2][0];
			$password    = $match[3][0];
			$domain      = $match[4][0];
			$afterDomain = $match[5][0]; // everything following the domain
			$port        = $match[6][0];
			$path        = $match[7][0];

			// Check that the TLD is valid or that $domain is an IP address.
			$tld = strtolower(strrchr($domain, '.'));

			$validTlds = $this->validTlds;

			if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld]))
			{
				// Do not permit implicit scheme if a password is specified, as
				// this causes too many errors (e.g. "my email:foo@example.org").
				if ( ! $scheme && $password )
				{
					$html .= $this->escapeHtml($username);

					// Continue text parsing at the ':' following the "username".
					$position = $urlPosition + strlen($username);

					continue;
				}

				if ( ! $scheme && $username && ! $password && ! $afterDomain )
				{
					// Looks like an email address.
					$emailLinkCreator = $this->emailLinkCreator;

					// Add the hyperlink.
					$html .= $emailLinkCreator($url, $url);
				}
				else
				{
					// Prepend http:// if no scheme is specified
					$completeUrl = $scheme ? $url : "http://$url";
					$linkText = "$domain$port$path";

					$htmlLinkCreator = $this->htmlLinkCreator;

					// Add the hyperlink.
					$html .= $htmlLinkCreator($completeUrl, $linkText);
				}
			}
			else
			{
				// Not a valid URL.
				$html .= $this->escapeHtml($url);
			}

			// Continue text parsing from after the URL.
			$position = $urlPosition + strlen($url);
		}

		// Add the remainder of the text.
		$html .= $this->escapeHtml(substr($text, $position));

		return $html;
	}