<?php if (!defined('PmWiki')) exit();

/*---------------------------------------------------------------

  * Copyright / Warranty *

  This PmWiki addon was written on March 20, 2004 by Steven Leite
  (steven_leite@kitimat.net).  The same terms and conditions that
  apply to PmWiki also apply to this script.  There's no warranty
  what-so-ever with this script.  Use at your own risk.

/*---------------------------------------------------------------
  
  * Special Thanks *

  Thanks to  Pm (Patrick Michaud, www.pmichaud.com), for creating
  PmWiki, and for sharing his knowledge  and insightfulness which
  is what made this script possible.

/*---------------------------------------------------------------

  * Description *

  This add-on allows a PmWiki user to include the contents of any
  external webpage into their WikiPage. By default, inclusion of
  www.pmwiki.org, and www.pmichaud.com/wiki is enabled. All other
  URLs will be rejected with an (optional) error message.

/*---------------------------------------------------------------

  * Features *

  X-Include variables are packed away in functions which are
  only called when the [[x-include:]] directive is executed.
  
  Ability to restrict which websites are allowed to be included. 
  This is achieved by one of two methods:  1) an "allowed_sites"
  array contained in this script, or 2) an external list of urls
  contained on a (secured) WikiPage.

/*---------------------------------------------------------------

  * Installation *

  1.  Create a new folder: /local/scripts.
  2.  Save this file (x-include.php) in the new directory.
  3.  Add this one line to your config.php file:
      include_once("local/scripts/x-include.php");

/*---------------------------------------------------------------

  * Usage Instructions *

  Using this script is easy  and intuitive, it uses a slightly
  modified version of the existing PmWiki [[include:]] directive:

  Example:  [[x-include: url=http://www.yahoo.com border=3 scroll=yes]]
  
  The script also includes a show=approved_sites parameter that
  can be used to list all allowed sites.
  
  Supported (optional) fields are:

    height = default/pixels/%
    width = default/pixels/%
    border = default/pixels
    scroll = default/yes/no
    align = default/left/right/center

  If the fields  are not specified, the script  defaults  will be 
  used.  You can set these defaults in the script (see below).

  To enable SafeSite management via a WikiPage, simply uncomment
  the 'datapage' variable.  Although disabled by default, the
  'datapage' is set to check "Admin/X-Include" for a list of<br>
  "allowed_sites".
  
  It's strongly recommended to set a password on the Admin group.  

/*---------------------------------------------------------------

  * History *

  May 17, 2004 - Added support for admins to allow all, or some
                 websites. These "safe sites" are maintained in
				 "list",  stored either directly in this sciprt,
				 or in a protected WikiPage.
  Apr 6, 2004  - Improved cpu and memory usage by putting all
                 code and variables in to functions.
  Mar 30, 2004 - Slight enhancement to the way the script
                 preserves the url in the <iframe> tag so that
				 PmWiki doesn't fiddle with it (using the Keep()
				 function).  Thanks to Pm for pointing that out
				 to me.
  Mar 22, 2004 - Added support for user-specified DefaultValues.
  Mar 20, 2004 - First released.
  
/*-------------------------------------------------------------*/

$DoubleBrackets["/\\[\\[x-include:(.*?)\\]\\]/e"] = 'xInclude("$1");';

//---------------------------------------------------------------
function xIncludeInit($user_input, & $xIncludeParams, & $xIncludeVars) {
//---------------------------------------------------------------

  $xIncludeVars = array (
    // 'datapage'       => 'Admin.X-Include',
    'show_errors'    => 'true',
	'site_filtering' => 'true');

  $pieces = explode(" ", $user_input);
  foreach($pieces as $apart){
    list($key,$value) = explode("=", $apart);
    $xIncludeParams["$key"] = $value;
  }

  SDVA($xIncludeParams, array(
    'width'          => '600',        // pixels or % (% not supported on all browsers)
    'height'         => '400',        // pixels or %
    'align'          => 'default',    // default, left, right, center
    'scroll'         => 'default',    // default, yes, no
    'border'         => '1'));  // pixels

  // do not include trailing slash for url's
  $ApprovedSites = array(   
	"http://www.pmwiki.org",
	"http://www.pmichaud.com/wiki",
	// "http://www.google.com",
	// "http://localhost/pmwiki",
	// "http://www.yahoo.com"
	);

  if (PageExists($xIncludeVars['datapage'])) {
      $Page = ReadPage($xIncludeVars['datapage']);
	  $PageText = $Page['text'];
      $lines = explode("\n", $PageText);
      foreach($lines as $line)  {
	    if (eregi("^http://(.*)$", $line, $match)) 
	      {$ApprovedSites[] = $line;}}
    } 
  $xIncludeVars['approved_sites'] = $ApprovedSites;
  return;
 }
 
 
//---------------------------------------------------------------
function xInclude($user_input) {
//---------------------------------------------------------------
  xIncludeInit($user_input, $xIncludeParams, $xIncludeVars);
  $html .= "\n\n<!-- X-include -->\n";


  if ($xIncludeParams['action'] == 'list') {
    $allowed = $xIncludeVars['approved_sites'];
	$html .= "\n<ul>";
    foreach($allowed as $allowed_item) {
	  $html .= "\n<li>$allowed_item<br>";
	}
    $html .= "\n</ul>";
  }

  else 
    {
      $approved = 'false';
      if ($xIncludeVars['site_filtering'] == 'true') 
	    {
          $allowed = $xIncludeVars['approved_sites'];
          foreach($allowed as $url) 
		    {
              if (eregi("^$url", $xIncludeParams['url'])) {$approved = 'true';}
	        }
        }
      if (($approved == 'true') || ($xIncludeVars['site_filtering']) == 'false' ) 
	    {
          $html .= ' <iframe width=' . $xIncludeParams['width'] .
            ' height='        . $xIncludeParams['height'] .
            ' align='         . $xIncludeParams['align'] . 
            ' frameborder='   . $xIncludeParams['border'] .
			' border='        . $xIncludeParams['border'] .
            ' scroll='        . $xIncludeParams['scroll'] .
            ' src='           . $xIncludeParams['url'] . '>\n</iframe>';
		}

      else 
	    {
          if ($xIncludeVars['show_errors'] == 'true') 
	        {
              $html .= "<span style='background-color:yellow; color:black; font-weight:bold;'>"; 
	          $html .= "ERROR (Invalid or restricted URL from modulllle [[x-include:]])</span>";
	        }
		}
    }

  $html .= "\n<!--/ X-include -->\n\n";
  return Keep($html);  // use Keep() function to preserve url
}

?>