geturlx(): Extended geturl

This is a modified and extended version of the original function geturl() who allows to “grab” external webcontent and to include it in the CMSimple content page. The content from the other website would be “embedded”.

Interesting if you would like to use other scripts in CMSimple without rewriting it as a plugin.

Attention: It is illegal to grab content from other websites and show them embedded in yours!

Example on how to use it in content (including an external gallery):

#CMSimple $output.=geturlx("http://singapore.sourceforge.net/demo/","?gallery=Demo/Templates/MinimumBlue");#

CODE

To put in CMS.PHP (functions.php) or better include as an external file:

function geturlx($base,$default)
  /*
    Luc Saffre 2004-03-17...30:
    - replace relative links in content by absolute links
   
    note: considered as relative link is anything between the '"' of a
    href="" or src="" tag that does not contain a ":"
    character. If it contains a ":", then it is not modified.
 
    if geturlx fails there is not output at all. If you want to know
    why it failed, remove the "@" in front of fopen so that PHP will
    display a warning. Or uncomment the last two lines.
   
   */
{
  $t='';
  if(substr($base,-1,1) != '/index.html') $base .= '/index.html';
  $urlx = $_GET['urlx'];
  if(strlen($urlx) == 0)
    $u = $base . $default;
  else
    $u = $base . $urlx;
  $u = rawurldecode($u);
  $u = preg_replace("/\&/is","&",$u);
  // echo 'Original content from <a href="'.$u.'">here</a>';
  if($fh=@fopen($u,"r")){
    while(!feof($fh))
      $t.=fread($fh, 1024);
    fclose($fh);
   
    $t = preg_replace("/.*<body[^>]*>(.*)<\/body>.*/is","\\1",$t);
    $t = preg_replace("/src=\"([^\":]*)\"/is","src=\"$base\\1\"",$t);
    $t = preg_replace("/window.open\('([^':]*)'/is",
                      "window.open('$base\\1'",$t);
 
    $t = preg_replace_callback("/href=\"([^\":]*)\"/is",
                               "geturlx_replace",
                               $t);
    return $t;
//      } else {
//        echo "could not open $u";
  }
}
 
 
function geturlx_replace($matches) {
  /*
    callback function for preg_replace_callback used in geturlx
   
    $matches[0] is the complete match
   
    $matches[1] the match for the first subpattern enclosed in '(...)'
    and so on
   
   */
  $uri = $_SERVER['REQUEST_URI'];
  $pos = strpos($uri,'urlx=');
  $newurl = '';
  if ($pos) {
    $self = substr($uri,0,$pos-1);
    $urlx=rawurldecode(substr($uri,$pos+5));
    if(strpos($urlx,'&')==false) {
      $rpos = strrpos($urlx,'/index.html');
      if ($rpos)
        $newurl = substr($urlx,0,$rpos+1);
    }
  } else $self = $uri;
 
 
  $newurl .= $matches[1];
      
  if (strpos($self,"?") === false)
    $self .= "?urlx=";
  else
    $self .= "&urlx=";
 
  $self .= rawurlencode($newurl);
      
  return 'href="' . $self . '"';
}

This code is discontinued as it and should be replaced by the gxgeturl plugin!

back to addons

Discussion

Tillman Schuster, 2007/06/14 12:32:

How about adding it to function.php in CMSimple v.2.8 and up.

 
johnjdoe, 2007/06/22 05:55:

I’m busy to make it a plugin. Seems easier to me. First beta will be available soon.

 
johnjdoe, 2007/07/12 08:15:

First beta of GXGetUrl is now available...

 
addons/geturlx.txt · Last modified: 2008/04/22 09:02 by till