Project

General

Profile

Actions

Feature #46314

closed

function http_makelinks does not find https links in Text (but could with some small changes)

Added by Jörg Velletti over 11 years ago. Updated about 6 years ago.

Status:
Closed
Priority:
Could have
Assignee:
-
Category:
Frontend
Target version:
-
Start date:
2013-03-15
Due date:
% Done:

0%

Estimated time:
PHP Version:
5.3
Tags:
Complexity:
easy
Sprint Focus:

Description

I would recommend that the function http_makelinks should also find "https://" in given text.

My solution to do this is like this:
(maybe there exists easier ways and maybe i am alone with this wish, but this works for me in 4.5.25)

1. rename "function http_makelinks" to "function http_makelinks_sub"

2. add 3. parameter scheme to renamede funktion
(so it looks like this: function http_makelinks_sub($data, $conf ,$scheme) {

3. add original function "function http_makelinks like this:

    function http_makelinks($data, $conf) {
        $datastep1 = $this->http_makelinks_sub($data, $conf ,"http://") ;
        return  $this->http_makelinks_sub( $datastep1, $conf ,"https://") ;

    }

4. replace "http://" or 'http://' with $scheme 6 times in "function http_makelinks_sub"

so the result looks like this in Version 4.5.25:

    /**
     * Finds URLS in text and makes it to a real link.
     * Will find all strings prefixed with "http://" OR "https://" in the $data string and make them into a link, linking to the URL we should have found.
     *
     * @param    string        The string in which to search for "http://" 
     * @param    array        Configuration for makeLinks, see link
     * @return    string        The processed input string, being returned.
     * @see _parseFunc()
     */
    function http_makelinks($data, $conf) {
        $datastep1 = $this->http_makelinks_sub($data, $conf ,"http://") ;
        return  $this->http_makelinks_sub( $datastep1, $conf ,"https://") ;

    }    

    /**
     * subfunction to http_makelinks: Finds URLS in text and makes it to a real link.
     * Will find all strings prefixed with given $scheme in the $data string and make them into a link, linking to the URL we should have found.
     *
     * @param    string        The string in which to search for $sheme
     * @param    array        Configuration for makeLinks, see link
     * @param    string        scheme: "http://" or "https://" 
     * @return    string        The processed input string, being returned.
     * @see _parseFunc()
     */
    function http_makelinks_sub($data, $conf ,$scheme) {
        $aTagParams = $this->getATagParams($conf);
        $textpieces = explode($scheme, $data);

        $pieces = count($textpieces);
        $textstr = $textpieces[0];
        $initP = '?id=' . $GLOBALS['TSFE']->id . '&type=' . $GLOBALS['TSFE']->type;
        for ($i = 1; $i < $pieces; $i++) {
            $len = strcspn($textpieces[$i], chr(32) . TAB . CRLF);
            if (trim(substr($textstr, -1)) == '' && $len) {

                $lastChar = substr($textpieces[$i], $len - 1, 1);
                if (!preg_match('/[A-Za-z0-9\/#_-]/', $lastChar)) {
                    $len--;
                } // Included '\/' 3/12

                $parts[0] = substr($textpieces[$i], 0, $len);
                $parts[1] = substr($textpieces[$i], $len);

                $keep = $conf['keep'];
                $linkParts = parse_url($scheme . $parts[0]);
                $linktxt = '';
                if (strstr($keep, 'scheme')) {
                    $linktxt = $scheme ;
                }
                $linktxt .= $linkParts['host'];
                if (strstr($keep, 'path')) {
                    $linktxt .= $linkParts['path'];
                    if (strstr($keep, 'query') && $linkParts['query']) { // added $linkParts['query'] 3/12
                        $linktxt .= '?' . $linkParts['query'];
                    } elseif ($linkParts['path'] == '/') { // If query is NOT added and the path is '/' then remove the slash ('/')   (added 3/12)
                        $linktxt = substr($linktxt, 0, -1);
                    }
                }
                $target = isset($conf['extTarget']) ? $conf['extTarget'] : $GLOBALS['TSFE']->extTarget;
                if ($GLOBALS['TSFE']->config['config']['jumpurl_enable']) {
                    $jumpurl = $scheme . $parts[0];
                    $juHash = t3lib_div::hmac($jumpurl, 'jumpurl');
                    $res = '<a' . ' href="' . htmlspecialchars(($GLOBALS['TSFE']->absRefPrefix . $GLOBALS['TSFE']->config['mainScript'] . $initP . '&jumpurl=' . rawurlencode($jumpurl))) . '&juHash=' . $juHash . $GLOBALS['TSFE']->getMethodUrlIdToken . '"' . ($target ? ' target="' . $target . '"' : '') . $aTagParams . $this->extLinkATagParams(('http://' . $parts[0]), 'url') . '>';
                } else {
                    $res = '<a' . ' href="'. $scheme . htmlspecialchars($parts[0]) . '"' .
                        ($target ? ' target="' . $target . '"' : '') .
                        $aTagParams . $this->extLinkATagParams( $sheme . $parts[0], 'url') .
                        '>';
                }
                $wrap = isset($conf['wrap.'])
                    ? $this->stdWrap($conf['wrap'], $conf['wrap.'])
                    : $conf['wrap'];
                if ($conf['ATagBeforeWrap']) {
                    $res = $res . $this->wrap($linktxt, $wrap) . '</a>';
                } else {
                    $res = $this->wrap($res . $linktxt . '</a>', $wrap);
                }
                $textstr .= $res . $parts[1];
            } else {
                $textstr .= $scheme . $textpieces[$i];
            }
        }
        return $textstr;
    }

PS: i added a DIFF file created with Eclipse on a a windows maschine after i upgraded to 4.5.25


Files

content.diff (3.71 KB) content.diff diff Version 4.5.25 /typo3/sysext/cms/tslib/class.tslib_content.php Jörg Velletti, 2013-03-15 10:51
Actions #1

Updated by Ernesto Baschny over 11 years ago

  • Status changed from New to Accepted
  • Priority changed from Should have to Could have
  • Target version deleted (4.5.26)

Hi Velletti,

nice idea and addition! Instead of making separate methods for "http" and "https" I would suggest to simply use preg_split instead of explode('http://', $data) and then do it all in one pass: It's faster.

Since this changes the behaviour of the frontend, this must be considered a new feature (it's unexpected to change this in released versions), so you must create a patch against the "master" branch (which will turn into 6.1 soon).

Check out http://wiki.typo3.org/Contribution_Walkthrough_Tutorials for a guided tour on how you can contribute your code and others can review it.

Actions #2

Updated by Ernesto Baschny over 11 years ago

Please note also that this method is now in typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php since TYPO3 6.0 (namespaces changes).

Also consider creating unit tests for this method and your change: This would greatly rise the chance for people to review it and getting it into the core faster.

Actions #3

Updated by Jörg Velletti over 11 years ago

Hello Ernesto

thanks for the hint to the new position in TYPO3 6.0 : typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php.

But please one additional Question:
can you also give me the filename for the best place for a unit test of this function? on my own extensions this is easy, but for the core i did not do it before.. and: I actually stuck in 4.5 as its LTS Support. Only the next NEW project will be done in 6.0 ..

Greetings
Jörg

ps: using preg Split it will make it a little bit mor complex, as i need to know, why the string was splitted, but yes, you are right .. it will be faster...

Actions #4

Updated by Gerrit Code Review almost 11 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27511

Actions #5

Updated by Gerrit Code Review almost 11 years ago

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27522

Actions #6

Updated by Gerrit Code Review almost 11 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27522

Actions #7

Updated by Jörg Velletti almost 11 years ago

for me the changed Function also worked in 4.5. LTS...

Actions #8

Updated by Wouter Wolters almost 10 years ago

  • Status changed from Under Review to Resolved
Actions #9

Updated by Benni Mack about 6 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF