scripting

General questions about CMSimple
Post Reply
stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

scripting

Post by stargazer96 » Sat Jan 02, 2016 6:07 pm

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

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: scripting

Post by cmb » Sun Jan 03, 2016 10:35 am

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);
    }
}
Christoph M. Becker – Plugins for CMSimple_XH

stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

Re: scripting

Post by stargazer96 » Sun Jan 03, 2016 11:19 am

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

stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

Re: scripting

Post by stargazer96 » Sun Jan 03, 2016 11:36 am

or -just a though-, would it be also possible to use an other function, maybe 'file_get_contents'?

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: scripting

Post by cmb » Sun Jan 03, 2016 11:47 am

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.
Christoph M. Becker – Plugins for CMSimple_XH

stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

Re: scripting

Post by stargazer96 » Sun Jan 03, 2016 12:30 pm

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.

stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

Re: scripting

Post by stargazer96 » Sun Jan 03, 2016 7:29 pm

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

stargazer96
Posts: 28
Joined: Sat Jul 07, 2012 6:16 pm
Location: Budapest, Hungary

Re: scripting

Post by stargazer96 » Wed Jan 06, 2016 9:20 am

With slight / small modifications it start working.
THANK YOU very much for the help !

Post Reply