<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Colateck</title>
	<atom:link href="http://poef.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://poef.wordpress.com</link>
	<description>Cafe&#239;nated Coding</description>
	<lastBuildDate>Fri, 09 Oct 2009 11:45:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='poef.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/fe0504b81dc9558761995532e33ae6b0?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Colateck</title>
		<link>http://poef.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://poef.wordpress.com/osd.xml" title="Colateck" />
	<atom:link rel='hub' href='http://poef.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Sandboxing PHP</title>
		<link>http://poef.wordpress.com/2009/10/09/sandboxing-php/</link>
		<comments>http://poef.wordpress.com/2009/10/09/sandboxing-php/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 11:33:16 +0000</pubDate>
		<dc:creator>poef</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://poef.wordpress.com/?p=3</guid>
		<description><![CDATA[Eli White posted a response to a posting by Fabien Potencier about using PHP as a template language versus custom template languages, like Smarty or in this case Twig. In it he counters almost all the arguments of Fabien to use a custom template language instead of just PHP, except one: When you need a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poef.wordpress.com&amp;blog=9858422&amp;post=3&amp;subd=poef&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://eliw.wordpress.com/2009/10/07/in-response-to-fabien-potencier-twig-php-templating/">Eli White posted a response</a> to <a href="http://fabien.potencier.org/article/34/templating-engines-in-php">a posting by Fabien Potencier</a> about using PHP as a template language versus custom template languages, like Smarty or in this case <a href="http://www.twig-project.org/">Twig</a>. In it he counters almost all the arguments of Fabien to use a custom template language instead of just PHP, except one: When you need a sandboxed environment where designers can create templates, but aren&#8217;t allowed further access to things like the file system, database, etc.</p>
<p>While difficult, sandboxing PHP for use as a template language is not impossible. We&#8217;ve been doing it since 1999 in <a href="http://www.ariadne-cms.org/">Ariadne</a>. Initially using a flawed approach based on parsing the PHP code with regular expressions, we&#8217;ve updated the process to use a full scanner/parser setup that understands the full PHP syntax.</p>
<p>We&#8217;ve called this contraption <a href="ftp://ftp.muze.nl/pub/pinp/pinp_compiler.php">PINP</a> (for PINP Is Not PHP). It allows you to specify a white list of functions that are ok to use. All other functions are rewritten with a configurable prefix. The same thing is done with variables, so the template writer doesn&#8217;t have access to any variable or function, except what you provide.</p>
<p>To make it extra clear that you are not in a full PHP environment when editing a PINP template, we&#8217;ve changed the start and end strings for code blocks from <code>&lt;?php</code> and <code>?&gt;</code> to <code>&lt;pinp&gt;</code> and <code>&lt;/pinp&gt;</code> respectively. But this is easy to change back.</p>
<p>The current version disallows &#8216;new&#8217;, so you need to provide factory methods if you want template writers to create new objects. It also rewrites method calls and property names with a &#8216;_&#8217; prefix. This is because it was designed for PHP3 and PHP4 where you had no private or protected methods or properties. Static method calls are also rewritten, the class name is prefixed with &#8216;pinp_&#8217;.</p>
<p>PINP compiles the template to PHP, you are responsible for saving the PHP code somewhere and running it as a template. E.g.:</p>
<pre>  $whitelist = 'header|myAppFunction|..';
  $pinp = new pinp($whitelist, 'this-&gt;', '$this-&gt;');
  $compiled = $pinp-&gt;compile($template);
 </pre>
<p>This will prefix all variable access with &#8216;<code>this-&gt;</code>&#8216;, so <code>$foo</code> will become <code>$this-&gt;foo</code>. All function calls are prefixed with &#8216;<code>$this-&gt;</code>&#8216;, so <code>bar()</code> becomes <code>$this-&gt;bar()</code>, unless <code>bar</code> is listed in the function whitelist.</p>
<p>Running the compiled template can be done in a number of ways, depending on how you save it. The simplest way is to include the compiled file in the correct spot, e.g.:</p>
<pre>  class sandbox() {
    function runme($compiledTemplatePath) {
      include($compiledTemplatePath);
    }
  }

  $sandbox = new sandbox();
  $sandbox-&gt;runme($aTemplatePath);
 </pre>
<p>This does require that your web application has write access somewhere to store these compiled templates. You could also store the compiled templates in a database, but then you&#8217;ll need <code>eval()</code> or <code>create_function()</code> to run the code, or you could use a custom stream wrapper in PHP5.</p>
<p>The current version doesn&#8217;t support exceptions or namespaces. It will return an error if it finds such code. On the plus side, the pinp compiler will happily run in a PHP4 environment, and actually allows templates there to use method chaining and even array comprehension. It also allows you to check for syntax errors, so you can generate a warning when saving an incorrect template. The compiled template doesn&#8217;t change the line numbers of the code, so any error PHP generates later can be traced back to the correct line in the uncompiled template.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/poef.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/poef.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/poef.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=poef.wordpress.com&amp;blog=9858422&amp;post=3&amp;subd=poef&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://poef.wordpress.com/2009/10/09/sandboxing-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/21a5015283ba90bd1e5c2d41d84ca1f8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">poef</media:title>
		</media:content>
	</item>
	</channel>
</rss>
