Page 1 of 1

scripting

Posted: Sat Jan 02, 2016 6:07 pm
by stargazer96
Hello everybody,

first of all, Happy New Year to all cmsimple fans!

Sometimes in content.htm file I need to use geturl function which is working fine with absolut url path, but not with relative ones.
- geturl('http://www.mydomain.com/folder1/page.php?&print') -- OK
- geturl('../folder1/page.php?&print') -- ng (not good) - give an empty page.

Can someone please help to make the geturl function also working with relative paths too?

Thank you in advance,
Alex

Re: scripting

Posted: Sun Jan 03, 2016 10:35 am
by cmb
stargazer96 wrote:Sometimes in content.htm file I need to use geturl function which is working fine with absolut url path, but not with relative ones.
Indeed, geturl() is designed to work with fully qualified absolute URLs only.
stargazer96 wrote:Can someone please help to make the geturl function also working with relative paths too?
You may try the following untested code (based on CMSimple 3.4 and CMSimple_XH's definition of CMSIMPLE_URL):

Code: Select all

function geturl($u) {
    global $sn;

    $u = 'http'
        . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
        . '://' . $_SERVER['HTTP_HOST'] . $sn . $u;
    $t = '';
    if ($fh = @fopen(preg_replace("/\&/is", "&", $u), "r")) {
        while (!feof($fh))$t .= fread($fh, 1024);
        fclose($fh);
        return preg_replace("/.*<body[^>]*>(.*)<\/body>.*/is", "\\1", $t);
    }
}

Re: scripting

Posted: Sun Jan 03, 2016 11:19 am
by stargazer96
Thank you very much for the answer, Christoph and Happy New Year for you!
It woks, but unfortunatelly work only if pages are in the root (and not in subfolders), but the main problem is that takes a while -a looooong time period- to load (and dispay) the page.
Maybe because I am using clear urls (with "_" uri seperator , and without the "?"), or have nothing to do with this aspect ?
Alex

Re: scripting

Posted: Sun Jan 03, 2016 11:36 am
by stargazer96
or -just a though-, would it be also possible to use an other function, maybe 'file_get_contents'?

Re: scripting

Posted: Sun Jan 03, 2016 11:47 am
by cmb
Happy new year to you, too!
stargazer96 wrote:It woks, but unfortunatelly work only if pages are in the root (and not in subfolders), […]
It might be useful to remove the $sn variable. Change:

Code: Select all

    $u = 'http'
        . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
        . '://' . $_SERVER['HTTP_HOST'] . $sn . $u; 
to

Code: Select all

    $u = 'http'
        . (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 's' : '')
        . '://' . $_SERVER['HTTP_HOST'] . $u; 
stargazer96 wrote:[…] but the main problem is that takes a while -a looooong time period- to load (and dispay) the page.
Some delay is to be expected. geturl() requests a document via HTTP(s) (what may take quite a while), and blocks further script execution while fetching the response.

I suggest you compare the loading time with the alternative approach of including the page in an IFrame, for instance:

Code: Select all

<iframe src="../folder1/page.php?&print"></iframe>
stargazer96 wrote:or -just a though-, would it be also possible to use an other function, maybe 'file_get_contents'?
file_get_contents() may be (slightly) faster than the fopen() used by geturl(), but you'd still need to remove everything outside of the <body> element (a preg_match() might be faster as the current preg_replace()).

Anyway, you may try GXGetUrl.

Re: scripting

Posted: Sun Jan 03, 2016 12:30 pm
by stargazer96
in this case (with removed $sn) gave -after half-, or one minute waiting- a "504 Gateway Time-out".
I will try also GXGetUrl, thank you, I already download it.

Re: scripting

Posted: Sun Jan 03, 2016 7:29 pm
by stargazer96
Thank you very much Christoph, seems that your solution is started to working well.
I will test it again also tomorrow and will reply soon.
Thanks a lot for your valuable help!
Alex

Re: scripting

Posted: Wed Jan 06, 2016 9:20 am
by stargazer96
With slight / small modifications it start working.
THANK YOU very much for the help !