예제 #1
0
    /**
     * @param $string
     * @return array
     */
    function Internal_AutoDetectURLs($string)
    {
        $search = preg_split('/(?xi)
		\\b
		(
			(?:
				(?:https?|ftp):\\/\\/
				|
				www\\d{0,3}\\.
				|
				mailto:
				|
				(?:[a-zA-Z0-9._-]{2,}@)
			)
			(?:
				[^\\s()<>]+
				|
				\\((?:[^\\s()<>]+|(\\(?:[^\\s()<>]+\\)))*\\)
			)+
			(?:
				\\((?:[^\\s()<>]+|(\\(?:[^\\s()<>]+\\)))*\\)
				|
				[^\\s`!()\\[\\]{};:\'"\\.,<>?«»“”‘’]
			)
		)/u', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
        $output = array();
        foreach ($search as $index => $token) {
            if ($index & 1) {
                if (preg_match("/^(https?|ftp|mailto):/ui", $token)) {
                    // Protocol has been provided, so just use it as-is.
                    $url = $token;
                } else {
                    // Add scheme to emails and raw domain URLs.
                    $url = (strpos($token, '@') ? 'mailto:' : 'http://') . $token;
                }
                // Never start URL from the middle of text (except for punctuation).
                $invalid = preg_match('#[^\\s`!()\\[\\]{};\'"\\.,<>?«»“”‘’]$#u', $search[$index - 1]);
                $invalid |= !$this->IsValidURL($url, true);
                // We have a full, complete, and properly-formatted URL, with protocol.
                // Now we need to apply the $this->url_pattern template to turn it into HTML.
                $params = Joomla\String\String::parse_url($url);
                if (!$invalid && substr($url, 0, 7) == 'mailto:') {
                    $email = Joomla\String\String::substr($url, 7);
                    if ($this->canCloakEmail($params)) {
                        $output[$index] = JHtml::_('email.cloak', $email, $this->IsValidEmail($email));
                    } else {
                        $output[$index] = $email;
                    }
                } elseif ($invalid || empty($params['host']) || !empty($params['pass'])) {
                    $output[$index - 1] .= $token;
                    $output[$index] = '';
                } else {
                    $params['url'] = $url;
                    $params['link'] = $url;
                    $params['text'] = $token;
                    $output[$index] = $this->FillTemplate($this->url_pattern, $params);
                }
            } else {
                $output[$index] = $token;
            }
        }
        return $output;
    }