示例#1
0
function httpGet($url)
{
    $client = new Guzzle\Http\Client(null, array('ssl.certificate_authority' => __DIR__ . '/../mozilla-ca-certs.pem'));
    ob_start();
    $url = web_address_to_uri($url, true);
    ob_end_clean();
    try {
        $response = $client->get($url)->send();
        return array($response, null);
    } catch (Guzzle\Common\Exception\GuzzleException $e) {
        return array(null, $e);
    }
}
/**
 *	Returns an array of any URLs in the given text
 */
function truncator_find_urls($text, $tidy = true)
{
    // Pattern is from 1 cassis.js (thanks Tantek!), slightly modified to not look for twitter names
    // E.G. beforehand it would return @tantek for @tantek.com. This function is just interested in addresses, not twitter names
    $pattern = '/(?:(?:(?:(?:http|https|irc)?:\\/\\/(?:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+(?:\\:(?:[!$&-.0-9;=?A-Z_a-z]|(?:\\%[a-fA-F0-9]{2}))+)?\\@)?)?(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*\\.)+(?:(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])|(?:biz|b[abdefghijmnorstvwyz])|(?:cat|com|coop|c[acdfghiklmnoruvxyz])|d[ejkmoz]|(?:edu|e[cegrstu])|f[ijkmor]|(?:gov|g[abdefghilmnpqrstuwy])|h[kmnrtu]|(?:info|int|i[delmnoqrst])|j[emop]|k[eghimnrwyz]|l[abcikrstuvy]|(?:mil|museum|m[acdeghklmnopqrstuvwxyz])|(?:name|net|n[acefgilopruz])|(?:org|om)|(?:pro|p[aefghklmnrstwy])|qa|r[eouw]|s[abcdeghijklmnortuvyz]|(?:tel|travel|t[cdfghjklmnoprtvwz])|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw]))|(?:(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])))(?:\\:\\d{1,5})?)(?:\\/(?:(?:[!#&-;=?-Z_a-z~])|(?:\\%[a-fA-F0-9]{2}))*)?)(?=\\b|\\s|$)/';
    $c = preg_match_all($pattern, $text, $m);
    if ($c) {
        // Normalise
        $links = array_values($m[0]);
        ob_start();
        $links = array_map(function ($value) use($tidy) {
            return $tidy ? web_address_to_uri($value, true) : $value;
        }, $links);
        ob_end_clean();
        // $links = ['http://someurl.tld', •••]
        return $links;
    }
    return array();
}
示例#3
0
文件: cassis.php 项目: tantek/cassis
function auto_link()
{
    $isjs = js();
    $args = $isjs ? arguments : func_get_args();
    if (count($args) === 0) {
        return '';
    }
    $t = $args[0];
    $do_embed = count($args) > 1 && $args[1] !== false;
    $do_link = count($args) < 3 || $args[2] !== false;
    $re = auto_link_re();
    $ms = preg_matches($re, $t);
    if (!$ms) {
        return $t;
    }
    $mlen = count($ms);
    $sp = preg_split($re, $t);
    $t = "";
    $sp[0] = string($sp[0]);
    for ($i = 0; $i < $mlen; $i += 1) {
        $mi = $ms[$i];
        $spliti = $sp[$i];
        $t = strcat($t, $spliti);
        $sp[$i + 1] = string($sp[$i + 1]);
        if (substr($sp[$i + 1], 0, 1) === '/') {
            $sp[$i + 1] = substr($sp[$i + 1], 1, strlen($sp[$i + 1]) - 1);
            $mi = strcat($mi, '/');
        }
        $spe = substr($spliti, -2, 2);
        if ((!$spe || !preg_match('/(?:\\=[\\"\\\']?|t;)/', $spe)) && substr(trim($sp[$i + 1]), 0, 3) !== '</a' && !contains('@charset@font@font-face@import@media@namespace@page@ABCDEFGHIJKLMNOPQ@', strcat($mi, '@'))) {
            $afterlink = '';
            $afterchar = substr($mi, -1, 1);
            while (contains('.!?,;"\')]}', $afterchar) && ($afterchar !== ')' || !contains($mi, '('))) {
                $afterlink = strcat($afterchar, $afterlink);
                $mi = substr($mi, 0, -1);
                $afterchar = substr($mi, -1, 1);
            }
            $fe = 0;
            if ($do_embed) {
                $fe = strtolower(substr($mi, -4, 1) === '.' ? substr($mi, -4, 4) : substr($mi, -5, 5));
            }
            $wmi = web_address_to_uri($mi, true);
            $prot = protocol_of_uri($wmi);
            $hn = hostname_of_uri($wmi);
            $pa = path_of_uri($wmi);
            $ih = is_http_uri($wmi);
            $ahref = '<span class="figure" style="text-align:left">';
            $enda = '</span>';
            if ($do_link) {
                $ahref = strcat('<a class="auto-link figure" href="', $wmi, '">');
                $enda = '</a>';
            }
            if ($fe && ($fe === '.jpeg' || $fe === '.jpg' || $fe === '.png' || $fe === '.gif' || $fe === '.svg')) {
                $alt = strcat('a ', offset('photo', $mi) != 0 ? 'photo' : substr($fe, 1));
                $t = strcat($t, $ahref, '<img class="auto-embed" alt="', $alt, '" src="', $wmi, '"/>', $enda, $afterlink);
            } else {
                if ($fe && ($fe === '.mp4' || $fe === '.mov' || $fe === '.ogv' || $fe === '.webm')) {
                    $t = strcat($t, $ahref, '<video class="auto-embed" ', 'controls="controls" src="', $wmi, '"></video>', $enda, $afterlink);
                } else {
                    if ($hn === 'vimeo.com' && ctype_digit(substr($pa, 1))) {
                        if ($do_link) {
                            $t = strcat($t, '<a class="auto-link" href="', 'https:', relative_uri_hash($wmi), '">', $mi, '</a> ');
                        }
                        $t = strcat($t, '<iframe class="vimeo-player auto-embed figure" width="480" height="385" style="border:0" src="', 'https://player.vimeo.com/video/', substr($pa, 1), '"></iframe>', $afterlink);
                    } else {
                        if ($hn === 'youtu.be' || ($hn === 'youtube.com' || $hn === 'www.youtube.com') && ($yvid = offset('watch?v=', $mi)) !== 0) {
                            if ($hn === 'youtu.be') {
                                $yvid = substr($pa, 1);
                            } else {
                                $yvid = explode('&', substr($mi, $yvid + 7));
                                $yvid = $yvid[0];
                            }
                            if ($do_link) {
                                $t = strcat($t, '<a class="auto-link" href="', 'https:', relative_uri_hash($wmi), '">', $mi, '</a> ');
                            }
                            $t = strcat($t, '<iframe class="youtube-player auto-embed figure" width="480" height="385" style="border:0"  src="', 'https://www.youtube.com/embed/', $yvid, '"></iframe>', $afterlink);
                        } else {
                            if ($mi[0] === '@' && $do_link) {
                                if ($sp[$i + 1][0] === '.' && $spliti != '' && ctype_email_local(substr($spliti, -1, 1))) {
                                    $t = strcat($t, $mi, $afterlink);
                                } else {
                                    $t = strcat($t, '<a class="auto-link h-x-username" href="', $wmi, '">', $mi, '</a>', $afterlink);
                                }
                            } else {
                                if ($do_link) {
                                    $t = strcat($t, '<a class="auto-link" href="', $wmi, '">', $mi, '</a>', $afterlink);
                                } else {
                                    $t = strcat($t, $mi, $afterlink);
                                }
                            }
                        }
                    }
                }
            }
        } else {
            $t = strcat($t, $mi);
        }
    }
    return strcat($t, $sp[$mlen]);
}