<commit_before><?php
namespace Core\Lib;

/**
 * Url shortener class
 * 
 * @author Michael "Tekkla" Zorn <<EMAIL>
 * @copyright 2014
 * @license MIT
 * @package TekFW
 * @subpackage Lib
 */
class ShortenURL
{

    public function factory($content, $type = 'text')
    {
        $obj = new ShortenURL();
        
        switch ($type) {
            case 'text':
                return $obj->shortenUrls($content);
            
            case 'url':
                return $obj->getTinyUrl($content);
        }
    }

    /**
     * Transforms given url into tiny url
     * 
     * @param string $url
     * @return string
     */
    private function getTinyUrl($url)
    {
        $ch = curl_init();
        
        $timeout = 5;
        
        curl_setopt($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        
        $url = curl_exec($ch);
        curl_close($ch);
        
        return $url;
    }

    /**
     * Transforms all urls in content into bbcode urls [url=THEURL]TitleOfPageBehindUrl[/url]
     * 
     * @todo This method currently does not ignore urls of img, css or already BBCed urls.
     *       => It's important to take care of this.
     * @param string $content
     * @return string
     */
    private function shortenUrls($content)
    {
        $url_pattern = '#\bhttps?://[^\s()]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
        
        preg_match_all($url_pattern, $content, $urls);
        
        if (Cfg::exists('web', 'url_shortener_exclude'))
            $exclude_pattern = '#' . implode('|', Cfg::get('Core', 'url_shortener_exclude')) . '#';
        
        foreach ($urls[0] as $url) {
            // modify not exluded urls
            if (isset($exclude_pattern) && preg_match($exclude_pattern, $url))
                continue;
            
            if (Cfg::get('Core', 'url_shortener_use') == 1) {
                // get pagetitle
                $doc = new \DOMDocument();
                @$doc->loadHTMLFile($url);
                
                $xpath = new \DOMXPath($doc);
                $inner = $xpath->query('//title')->item(0)->nodeValue . "";
            }
            
            if (! $inner) {
                $url_parts = parse_url($url);
                $inner = $url_parts['host'];
            }
            
            // shorten url
            if (Cfg::get('Core', 'url_shortener_tiny') == 1)
                $url = $this->getTinyUrl($url);
            
            $content = str_replace($url, '[url=' . $url . ']' . $inner . '[/url]', $content);
        }
        
        return $content;
    }
}<commit_msg>Changes comments<commit_after><?php
namespace Core\Lib;

/**
 * ShortenURL.php
 *
 * @author Michael "Tekkla" Zorn <<EMAIL>>
 * @copyright 2015
 * @license MIT
 */
class ShortenURL
{

    public function factory($content, $type = 'text')
    {
        $obj = new ShortenURL();

        switch ($type) {
            case 'text':
                return $obj->shortenUrls($content);

            case 'url':
                return $obj->getTinyUrl($content);
        }
    }

    /**
     * Transforms given url into tiny url
     *
     * @param string $url
     * @return string
     */
    private function getTinyUrl($url)
    {
        $ch = curl_init();

        $timeout = 5;

        curl_setopt($ch, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url=' . $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

        $url = curl_exec($ch);
        curl_close($ch);

        return $url;
    }

    /**
     * Transforms all urls in content into bbcode urls [url=THEURL]TitleOfPageBehindUrl[/url]
     *
     * @todo This method currently does not ignore urls of img, css or already BBCed urls.
     *       => It's important to take care of this.
     * @param string $content
     * @return string
     */
    private function shortenUrls($content)
    {
        $url_pattern = '#\bhttps?://[^\s()]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';

        preg_match_all($url_pattern, $content, $urls);

        if (Cfg::exists('web', 'url_shortener_exclude'))
            $exclude_pattern = '#' . implode('|', Cfg::get('Core', 'url_shortener_exclude')) . '#';

        foreach ($urls[0] as $url) {
            // modify not exluded urls
            if (isset($exclude_pattern) && preg_match($exclude_pattern, $url))
                continue;

            if (Cfg::get('Core', 'url_shortener_use') == 1) {
                // get pagetitle
                $doc = new \DOMDocument();
                @$doc->loadHTMLFile($url);

                $xpath = new \DOMXPath($doc);
                $inner = $xpath->query('//title')->item(0)->nodeValue . "";
            }

            if (! $inner) {
                $url_parts = parse_url($url);
                $inner = $url_parts['host'];
            }

            // shorten url
            if (Cfg::get('Core', 'url_shortener_tiny') == 1)
                $url = $this->getTinyUrl($url);

            $content = str_replace($url, '[url=' . $url . ']' . $inner . '[/url]', $content);
        }

        return $content;
    }
}
{'fraction_non_alphanumeric': 0.1581448393841475, 'fraction_numerical': 0.0038015586390420073, 'mean_word_length': 1.7320872274143302, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5695283', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1619, 'n_words': 343}