[pmwiki-users] How to embed php code

Patrick R. Michaud pmichaud at pobox.com
Wed Dec 13 15:09:01 CST 2006


On Wed, Dec 13, 2006 at 03:27:22PM -0500, Tim Boland wrote:
>    I see that it is possible to write set php code functions using custom
>    markup.  Is it possible to write a custom markup tag that will let me
>    write any inline php code similar to the HTML markup stuff I have seen?
>    Something like this but for php tags.

First, I have to point out that enabling inline PHP in markup is
REALLY DANGEROUS.  Essentially you're allowing any author to execute
any PHP program on your system -- this includes being able to erase files,
or even execute PHP programs downloaded from remote servers.

Still, if you want inline PHP and are willing to take the risks, the 
code below will add a <?php ... ?> markup, which will execute the PHP 
code and insert any output (e.g., from print or echo statements) into 
the final page output.  See http://www.pmwiki.org/wiki/Test/InlinePHP 
for a demonstration.

    Markup('php', '[=',
        '/&lt;\\?php\\s(.*?)\\?&gt;/es',
        "Keep(InlinePHP(PSS('$1')))");
    
    function InlinePHP($x) {
      $x = str_replace(array('&lt;', '&gt;', '&amp;'), array('<', '>', '&'), $x);
      ob_start();
      eval($x);
      $y = ob_get_contents();
      ob_end_clean();
      return $y;
    }


Again, this is really dangerous.  Personally I would never use it,
with the exception of the single Test.InlinePHP demonstration page
above (and even there I'm likely to cripple it or disable it because
it's way too risky for my tastes, even with the page being locked to
editing).

Pm




More information about the pmwiki-users mailing list