<?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/"
> <channel><title>stefano &#187; php</title> <atom:link href="http://www.stefanoforenza.com/category/php/feed/" rel="self" type="application/rss+xml" /><link>http://www.stefanoforenza.com</link> <description>Abbattuta l&#039;Accademia della Crusca gli SMS vinceranno</description> <lastBuildDate>Sun, 05 May 2013 00:47:22 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.5.1</generator> <item><title>Let&#8217;s countdown together</title><link>http://www.stefanoforenza.com/lets-countdown-together/</link> <comments>http://www.stefanoforenza.com/lets-countdown-together/#comments</comments> <pubDate>Fri, 27 Mar 2009 13:15:08 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[php]]></category> <category><![CDATA[ubuntu]]></category> <category><![CDATA[2012 will be the year of Linux. Ubuntu Pink Pony will pwn Windows]]></category> <guid
isPermaLink="false">http://www.stefanoforenza.com/?p=1007</guid> <description><![CDATA[This post will display bad in RSS readers and planet. Click here to read it on my site. Update: Better degradation for no-JS-users using a PHP script added. See below. So, the latest and greatest (and badly named ) Ubuntu is approaching and you want to help the community spreading out the word and build expectance ? Jaunty Jackalope, while being apparently not a big step forward in dekstop features, brings upgrade to all installed ...]]></description> <content:encoded><![CDATA[<p><a
href="http://www.stefanoforenza.com/lets-countdown-together/">This post will display bad in RSS readers and planet. Click here to read it on my site</a>.</p><p><strong>Update</strong>: Better degradation for no-JS-users using a PHP script added. See below.</p><p>So, the <em>latest and greatest</em> (and <a
href="http://www.stefanoforenza.com/ubuntu-new-codename-announced-sic/">badly named <img
src='http://www.stefanoforenza.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </a>) Ubuntu is approaching and you want to help the community spreading out the word and build expectance ?</p><p>Jaunty Jackalope, while being apparently not a big step forward in dekstop features, brings upgrade to all installed software, and <em>btw</em> we don&#8217;t want people to stay on older versions like it happens with Windows, right ?</p><p>The community has made an &#8211; let me tell you &#8211; awesome work with <a
href="http://www.ubuntu.com/getubuntu/countdown">countdown banners</a>, and <em>if you&#8217;re that type of guy</em> (that has a site or blog),  you may want to help out by embedding one of those beauties in your pages.</p><p>Here&#8217;s the banners and the code to embed them on your site. <strong>Note</strong>: before rushing with your copy/paste I suggest you to read the entire post, as in the end of it I provide code to allow graceful degradation for those who don&#8217;t have javascript enabled.</p><p><strong>The ring</strong></p><p><script src="http://www.ubuntu.com/files/countdown/display.js" type="text/javascript"></script></p><pre>&lt;script src="http://www.ubuntu.com/files/countdown/display.js" type="text/javascript"&gt;&lt;/script&gt;</pre><p><strong>The calendar</strong></p><p><script type="text/javascript" src="http://www.ubuntu.com/files/countdown/display2.js"></script></p><pre>&lt;script type="text/javascript" src="http://www.ubuntu.com/files/countdown/display2.js"&gt;&lt;/script&gt;</pre><p><strong>Slideshow</strong></p><p><iframe
src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/countdown.html" width="180" height="150" frameborder="0" scrolling="no" name="ubuntucountdown"><br
/> <a
href="http://www.ubuntu.com/"><br
/> <img
src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/00.png" alt="Ubuntu 9.04 - on desktops, netbooks, servers and in the cloud" width="180" height="150" border="0" /><br
/> </a><br
/> </iframe></p><pre>&lt;iframe src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/countdown.html" width="180" height="150" frameborder="0" scrolling="no" name="ubuntucountdown"&gt;
&lt;a href="http://www.ubuntu.com/"&gt;
&lt;img src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/00.png" alt="Ubuntu 9.04 - on desktops, netbooks, servers and in the cloud" width="180" height="150" border="0" /&gt;
&lt;/a&gt;
&lt;/iframe&gt;</pre><p><strong>Static image (no javascript)</strong></p><p>Note: this doesn&#8217;t change day per day (except, perhaps, on the release day)</p><p><a
href="http://www.ubuntu.com/"><img
src="http://www.ubuntu.com/files/countdown/static.png" width="180" height="150" alt="Ubuntu: For Desktops, Servers, Netbooks and in the cloud" border="0" /></a></p><pre>&lt;a href="http://www.ubuntu.com/"&gt;&lt;img src="http://www.ubuntu.com/files/countdown/static.png" width="180" height="150" alt="Ubuntu: For Desktops, Servers, Netbooks and in the cloud" border="0" /&gt;&lt;/a&gt;</pre><p><strong>How to mix javascript banners with graceful degradation to the static image if no JS is available:<br
/> </strong></p><p>The following codes will help you display the <em>Javascript</em> banners to those who have <em>Javascript</em> enabled while still degrade gracefully to the static image for those who have <em>Javascript</em> disabled. It&#8217;s not anything from the official instructions, just my suggestion.<br
/> Also useful for embedding the <em>Ubuntu Countdown</em> in your posts, since RSS feed reader usually strip all javascript.</p><p><strong>The ring:</strong></p><pre>&lt;script src="http://www.ubuntu.com/files/countdown/display.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://www.ubuntu.com/"&gt;&lt;img src="http://www.ubuntu.com/files/countdown/static.png" width="180" height="150" alt="Ubuntu: For Desktops, Servers, Netbooks and in the cloud" border="0" /&gt;&lt;/a&gt;&lt;/noscript&gt;</pre><p><strong>Update</strong>: Steve Dodier just shared a php-based alternative to the basic javascript degradation. This version will display a proper countdown even with javascript disabled. <del
datetime="2009-04-01T09:34:33+00:00">It may be a little slower in loading</del> (fixed: this new code is fast), but will surely look better.</p><pre>&lt;img src="http://www.2nt.free.fr/host/jaunty/countdown2.php" /&gt;</pre><p>Also, you can mess with PHP, you can <a
href="http://www.2nt.free.fr/host/jaunty/source.php">find the script source here</a>.</p><p><strong>The calendar:</strong></p><pre>&lt;script type="text/javascript" src="http://www.ubuntu.com/files/countdown/display2.js"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://www.ubuntu.com/"&gt;&lt;img src="http://www.ubuntu.com/files/countdown/static.png" width="180" height="150" alt="Ubuntu: For Desktops, Servers, Netbooks and in the cloud" border="0" /&gt;&lt;/a&gt;&lt;/noscript&gt;</pre><p><strong>Slideshow:</strong></p><p>The coolest and <em>trickies</em>t of them all. (may give little problem in the 0.01% of site setup in case you make of <strong>heavy</strong> use of ajax/javascript in the rest of your site).</p><pre>&lt;script type="text/javascript"&gt;
document.write('&lt;iframe src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/countdown.html" width="180" height="150" frameborder="0" scrolling="no" name="ubuntucountdown"&gt;&lt;a href="http://www.ubuntu.com/"&gt;&lt;img src="http://www.ubuntu.com/files/countdown/904/countdown-9.04-1/00.png" alt="Ubuntu 9.04 - on desktops, netbooks, servers and in the cloud" width="180" height="150" border="0" /&gt;&lt;/a&gt;&lt;/iframe&gt;');
&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://www.ubuntu.com/"&gt;&lt;img src="http://www.ubuntu.com/files/countdown/static.png" width="180" height="150" alt="Ubuntu: For Desktops, Servers, Netbooks and in the cloud" border="0" /&gt;&lt;/a&gt;&lt;/noscript&gt;</pre><p><strong>Authors</strong></p><p>The great people who did them:</p><ul><li><a
href="http://thorwil.wordpress.com/">Thorsten Wilms (thorwil)</a> for the ring and animation artwork</li><li><a
href="http://markus.alshain.fi">Markus Grönholm</a> for the animation javascript</li><li><a
href="http://anotherubuntu.blogspot.com/">Mads Rosendahl (MadsRH)</a> for the calendar artwork</li><li><a
href="http://mongolito404.tumblr.com/">Pierre Buyle</a> for initial animation and concept</li></ul><p><a
href="http://www.ubuntu.com/getubuntu/countdown">Official instructions</a></p><p>Enjoy !</p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/lets-countdown-together/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>How to create Private (and moderated) Forums BuddyPress and BBPress</title><link>http://www.stefanoforenza.com/how-to-create-private-and-moderated-forums-buddypress-and-bbpress/</link> <comments>http://www.stefanoforenza.com/how-to-create-private-and-moderated-forums-buddypress-and-bbpress/#comments</comments> <pubDate>Wed, 04 Feb 2009 04:57:16 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[php]]></category> <category><![CDATA[bbpress]]></category> <category><![CDATA[buddypress]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress mu]]></category> <category><![CDATA[[HOW TO] get the damn thing working]]></category> <guid
isPermaLink="false">http://www.stefanoforenza.com/?p=290</guid> <description><![CDATA[If you develop on BuddyPress, you&#8217;re likely to get stuck while trying to get per-forum permissions. Indeed, it seems there&#8217;s not yet a way to make BBPress read the BuddyPress groups and hide the froums belonging to private  groups I had to develop a way myself. Furthermore, I needed to give forum moderation rights to the moderators of each group in BuddyPress on a per-forum basis. This is a quick and dirty tutorial for future ...]]></description> <content:encoded><![CDATA[<p>If you develop on BuddyPress, you&#8217;re likely to get stuck while trying to get per-forum permissions.</p><p>Indeed, it seems there&#8217;s not yet a way to make BBPress read the BuddyPress groups and hide the froums belonging to private  groups I had to develop a way myself.</p><p>Furthermore, I needed to give forum moderation rights to the moderators of each group in BuddyPress on a per-forum basis.</p><p>This is a quick and dirty tutorial for future reference. Keep in mind I am using the bleeding edge (svn) versions of WordPress Mu, BuddyPress and BBPress.</p><p>At the end of this tutorial <strong>your BBpress will hide any forum associated with a non-public user group, and group moderators will have the needed  privileges in BBPress as well</strong> (without manual intervention).</p><p><strong>Note: </strong>I assume you <strong>already work</strong> with those three softwares and <strong>you know how</strong> to install them, install plugins, etc.</p><p><strong>First</strong>: install WordPress Mu 2.7 (it&#8217;s not stable yet, so you&#8217;ll have to get it somewhere from svn)</p><p><strong>Second:</strong> Install BuddyPress bleeding edge version on top of WordPress.</p><p><strong>Third:</strong> Download the svn version of BBPress and decompress it in a sub folder.</p><p><a
href="http://bbpress.org/forums/topic/wordpress-and-bbpress-integration-101">Be sure to properly integrate BBPress with WordPress</a>. (<a
href="http://jimgroom.org/bavawiki/index.php?title=Integrating_WPMu%2C_BuddyPress%2C_and_bbPress">further reading here</a>)</p><p>When you get everything working, download the <a
href="http://bbpress.org/plugins/topic/hidden-forums/">Hidden Forums plugin</a> and install it in BBPress.</p><p>Now, open the <em>hidden-forums/hidden-forums.php</em> file and replace everything from the top to the &#8220;<em>/*    stop  editing  here    */</em>&#8221; mark with:</p><pre>require_once( 'bp_integration.php' );</pre><p>When you&#8217;re done, create a new file named &#8220;<em>bp_integration.php</em>&#8221; in the same folder and copy paste the following code. Save everything, activate, and you&#8217;re done <img
src='http://www.stefanoforenza.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p><pre>&lt;?php
//settings
global $bp;
$bp['groups']['table_name_groupmeta'] = 'wp_bp_groups_groupmeta';
$bp['groups']['table_name'] = 'wp_bp_groups';
$bp['groups']['table_name_members'] = 'wp_bp_groups_members';
$bp_integration_privileges = array(); //ids of forums the logged user can moderate
function bp_integration_apply_user_permissions()
{
    global $hidden_forums, $bb_current_user;
    global $bbdb, $bp;
    global $bp_integration_privileges;
    $query = "
    SELECT group.id as id, group.enable_forum as enable_forum, meta.meta_value as forum_id FROM `{$bp['groups']['table_name']}` as `group`
    INNER JOIN `{$bp['groups']['table_name_groupmeta']}` as meta
        ON group.id = meta.group_id AND meta.meta_key = 'forum_id' AND group.status != 'public'
    ";
    $private_groups = $bbdb-&gt;get_results( $query );
    $user_id=(!empty($bb_current_user)) ? intval($bb_current_user-&gt;ID) : 0;
    // hide these forums, list by comma seperated number
    $hidden_forums['hidden_forums']=array();
    foreach ( $private_groups as $group )
    {
        $hidden_forums['hidden_forums'][] = $group-&gt;forum_id;
    }
    $allowed_group = bp_integration_allowed_forums($user_id);
    $moderator_in = bp_integration_user_privileges($user_id);
    foreach ( $allowed_group as $allowed )
    {
        foreach ( $private_groups as $group )
        {
            if ( $group-&gt;id == $allowed-&gt;group_id &amp;&amp; $group-&gt;enable_forum )
            {
                $hidden_forums['allow_users'][$group-&gt;forum_id][] = $user_id;
            }
        }
    }
    foreach ( $moderator_in as $moderated )
    {
        foreach ( $private_groups as $group )
        {
            if ( $group-&gt;id == $moderated-&gt;group_id &amp;&amp; $group-&gt;enable_forum )
            {
                $bp_integration_privileges[] = $group-&gt;forum_id;
            }
        }
    }
    $hidden_forums['allow_roles']['all_forums']=array('keymaster');
    $hidden_forums['allow_users']['all_forums'] = array();
}
add_action('bb_init','bp_integration_apply_user_permissions',199);
function bp_integration_user_privileges ( $user_id, $limit = false, $page = false, $get_total = true, $as_col = false )
{
    global $bbdb, $bp;
    if ( $limit &amp;&amp; $page )
        $pag_sql = $bbdb-&gt;prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
			$group_sql = $bbdb-&gt;prepare( "
SELECT DISTINCT group_id
FROM " . $bp['groups']['table_name_members'] . "
WHERE user_id = %d AND inviter_id = 0 AND is_banned = 0
AND ( is_admin = 1 OR is_mod = 1 )
{$pag_sql}
", $user_id );
    return $paged_groups = $bbdb-&gt;get_results( $group_sql );
}
function bp_integration_allowed_forums( $user_id, $limit = false, $page = false, $get_total = true, $as_col = false ) {
		global $bbdb, $bp;
		if ( $limit &amp;&amp; $page )
			$pag_sql = $bbdb-&gt;prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
			$group_sql = $bbdb-&gt;prepare( "SELECT DISTINCT group_id FROM " . $bp['groups']['table_name_members'] . " WHERE user_id = %d AND inviter_id = 0 AND is_banned = 0{$pag_sql}", $user_id );
		return $paged_groups = $bbdb-&gt;get_results( $group_sql );
	}
function bp_integration_moderators_privileges ()
{
    global $bb_current_user, $forum,$topic_id;
    global $bp_integration_privileges;
    $args = func_get_args();
     $moderator_skills = array(
		'moderate' =&gt; true,
		'participate' =&gt; true,
		'manage_tags' =&gt; true,			//+
        /* ??!?! seems not work
		'delete_topics' =&gt; true,		//+
		'close_topics' =&gt; true,			//+
		'stick_topics' =&gt; true,			//+
		'move_topics' =&gt; false,			//+
         */
        /* ??!?! seems to work */
        'delete_topic' =&gt; true,		//+
		'close_topic' =&gt; true,			//+
		'stick_topic' =&gt; true,			//+
		'move_topic' =&gt; false,
		'view_by_ip' =&gt; false,			//+
		'edit_closed' =&gt; true,			//+
		'edit_deleted' =&gt; true,			//+
		'browse_deleted' =&gt; true,		//+
		'edit_others_tags' =&gt; true,		//+
		'edit_others_topics' =&gt; true,	//+
		'delete_posts' =&gt; true,			//+
		'throttle' =&gt; true,				//+
		'ignore_edit_lock' =&gt; true,		//+
		'edit_others_posts' =&gt; true,	//+
		'edit_favorites' =&gt; true,
		'edit_tags' =&gt; true,
		'edit_topics' =&gt; true,
		'edit_posts' =&gt; true,
		'edit_profile' =&gt; true,
		'write_topics' =&gt; true,
		'write_posts' =&gt; true,
		'change_password' =&gt; true,
		'read' =&gt; true
	);
    if ( ! in_array($args[1], array_keys($moderator_skills)) )
    {
        //echo '&lt;pre&gt;';var_dump( $args[1] );echo '&lt;/pre&gt;';
        return $args[0];
    }
    if (!$bb_current_user || in_array('keymaster',$bb_current_user-&gt;roles) || in_array('administrator',$bb_current_user-&gt;roles))
    {
		return $args[0];
    }
    //--------&gt; OK, LET'S GO !
    // prevents forum moderators to stick a topic to the forum's front page
    if ( $args[1] == 'stick_topic' )
    {
        global $super;
        $super = 0;
    }
	if (empty($forum)) {
		$topic = get_topic($topic_id);
		$forum_id = $topic-&gt;forum_id;
	} else {
        $forum_id = $forum-&gt;forum_id;
    }
    if ( !$forum_id ) return $args[0];
    if ( in_array( $forum_id, $bp_integration_privileges ))
    {
        $args[0] = $moderator_skills[ $args[1] ];
    }
    return $args[0];
}
add_filter('bb_current_user_can','bp_integration_moderators_privileges',10,25);
?&gt;</pre>]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/how-to-create-private-and-moderated-forums-buddypress-and-bbpress/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>5 tips to make your PHP code beautiful (3)</title><link>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-3/</link> <comments>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-3/#comments</comments> <pubDate>Sun, 20 Apr 2008 15:31:34 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://www.stefanoforenza.com/?p=35</guid> <description><![CDATA[Avoid unnecessary control structures. Brackets everywhere. Try to get rid of those as much as you can. Unnecessary brackets add visual complexity and make your code much longer. In real world situation, when you need to quickly browse through your code, they just add clutter //prints out the structure of a table function describe( $table ) { $query = "SHOW COLUMNS FROM `$table `"; $result =mysql_query( $query ); if ( ! $result ) { return ...]]></description> <content:encoded><![CDATA[<p><strong>Avoid unnecessary control structures.</strong></p><p>Brackets everywhere. Try to get rid of those as much as you can. Unnecessary brackets add visual complexity and make your code much longer. In real world situation, when you need to quickly browse through your code, they just add clutter</p><pre>
//prints out the structure of a table
function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result )
	{
		return $result;
	} else {
		$data = array();
		while ( $record = mysql_fetch_assoc( $result ) )
		{
			$data[] =  $record;
		}
		return $data;
	}
}
</pre><p>In the function above the if-else structure is not needed, just because when $result is false I can just return it, breaking the flow. Also while ( $record = ..) uses brackets for looping over just one statement.</p><p>We can make describe() look much more tidy:</p><pre>function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result ) return $result;
	$data = array();
	while ( $record = mysql_fetch_assoc( $result ) ) $data[] =  $record;
	return $data;
}
</pre><p>Seems nicer, uh ? While one could think that&#8217;s just a detail, when code complexity grows, this kind of tricks can help you cutting it down.</p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-3/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>5 tips to make your PHP code beautiful (2)</title><link>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-2/</link> <comments>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-2/#comments</comments> <pubDate>Sun, 20 Apr 2008 14:58:36 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://www.stefanoforenza.com/?p=33</guid> <description><![CDATA[Thoughtful minimalism Think about the most concise, clear and readable way to write the code your writing. Assume you were writing a function to get a file extension (.gif, .doc, etc.). You could be tempted to use string manipulation functions. $filename = 'file.doc'; $dot_position = strpos( $filename, '.' ); if ( $dot_position !== false ) { $extension = substr( $filename, $dot_position + 1 ); } var_dump($extension) It&#8217;s a lot of code for such a simple ...]]></description> <content:encoded><![CDATA[<p><strong>Thoughtful minimalism</strong></p><p>Think about the most concise, clear and readable way to write the code your writing. Assume you were writing a function to get a file extension (.gif, .doc, etc.). You could be tempted to use string manipulation functions.</p><pre>
$filename = 'file.doc';
$dot_position = strpos( $filename, '.' );
if ( $dot_position !== false )
{
	$extension = substr( $filename, $dot_position + 1  );
}
var_dump($extension)
</pre><p>It&#8217;s a lot of code for such a simple task. Beside that, files with multiple dots ( <em>filename.with.dots.html</em> ) won&#8217;t be handled properly.</p><pre>
$filename = 'file.doc';
$extension = array_pop ( explode( '.', $filename ) );
var_dump($extension)
</pre><p>The lesson here is to restlessy try to find the best solutions , even when you thought you knew how to accomplish the task. The one-liner above, is more easy to read and much less error-prone. It will work with file with multiple dots or with no extension without any problem. (note: you may want to filter $filename with basename() to avoid directory names dot to interfere).</p><p>I am gonna give another example &#8211; imagine you&#8217;re trying to print a different string for a bunch of pages on you&#8217;re site.</p><pre>if ( $page_id == 32 || $page_id == 10 || $page_id == null )
{
	echo 'inactive';
} else {
	echo 'active';
}
</pre><p>While this code is pretty ok, you could note that $page_id is repeated one time for every page you want to set as inactive.</p><pre>$inactive_pages = array( 32, 10, null );
if ( in_array( $page_id, $inactive_pages ) )
{
	echo 'inactive';
} else {
	echo 'active';
}
</pre><p>This way you can add exceptions just inserting a new element in $inactive_pages array. This will make easier refactor the code to read $inactive_pages from a configuration file or from a database.<br
/> In situations where you need to keep your code even smaller you could even write a one-liner (but don&#8217;t exceed with them, as many people find them less readable &#8211; and often they&#8217;re right):</p><pre>echo in_array( $page_id, array( 32, 10, null )  )  ? 'inactive' : 'active';</pre>]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful-2/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>5 tips to make your PHP code beautiful (1)</title><link>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful/</link> <comments>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful/#comments</comments> <pubDate>Sun, 20 Apr 2008 14:18:56 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://www.stefanoforenza.com/?p=31</guid> <description><![CDATA[Get your brackets right ..and indent your code properly, always. Always give to each of your curly brackets their very own line. I am taking a snippet from Kohana to demonstrate this. (bad) What do you think of this code ? if ( ! is_array($data)){ if (strpos($data, '[') !== FALSE) {$data = preg_replace('/\[.*\]/', '', $data); }$data = empty($data) ? array() : array('for' =&#62; $data);} (not nice) This is much better if ( ! is_array($data)){ if ...]]></description> <content:encoded><![CDATA[<p><strong>Get your brackets right</strong></p><p>..and indent your code properly, always. Always give to each of your curly brackets their very own line.<br
/> I am taking a snippet from <a
href="http://www.kohanaphp.com/">Kohana</a> to demonstrate this.</p><p><em>(bad)</em> What do you think of this code ?</p><pre>
if ( ! is_array($data)){
if (strpos($data, '[') !== FALSE)
{$data = preg_replace('/\[.*\]/', '', $data);
}$data = empty($data) ? array() : array('for' =&gt; $data);}
</pre><p><em>(not nice)</em> This is much better</p><pre>
if ( ! is_array($data)){
	if (strpos($data, '[') !== FALSE){
		$data = preg_replace('/\[.*\]/', '', $data);
	}
	$data = empty($data) ? array() : array('for' =&gt; $data);
}
</pre><p>(sweet) A nicely formatted code should show like this. You can tell at a glance where your every <strong>if</strong> begins and ends.</p><pre>
if ( ! is_array( $data) )
{
	if ( strpos( $data, '[') !== FALSE )
	{
		$data = preg_replace('/\[.*\]/', '', $data );
	}
	$data = empty( $data ) ? array() : array( 'for' =&gt; $data );
}
</pre>]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/5-tips-to-make-your-php-code-beautiful/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>How to build an absolute url in php</title><link>http://www.stefanoforenza.com/how-to-build-an-absolute-url-in-php/</link> <comments>http://www.stefanoforenza.com/how-to-build-an-absolute-url-in-php/#comments</comments> <pubDate>Mon, 30 Apr 2007 16:50:06 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://devnull.dreamhosters.com/2007/04/30/how-to-build-an-absolute-url-in-php/</guid> <description><![CDATA[Today, I was looking for a way to make an url absolute. Ok, it’s easy. Guess I should think some good excuse for not being to able to do it in 5 minutes. The sad reality is I ended digging on google (again). I am posting what I found, for (main, and) your reference. A working one Update: the function I found didn’t pass my testcases. I had to craft one myself. A raw one, ...]]></description> <content:encoded><![CDATA[<p><a
title="how-to-build-an-absolute-url-in-php" name="how-to-build-an-absolute-url-in-php" id="how-to-build-an-absolute-url-in-php"></a> Today, I was looking for a <strong>way to make an url absolute</strong>.</p><p
class="level1"> Ok, it’s easy. Guess I should think some good excuse for not being to able to do it in 5 minutes. The sad reality is I ended digging on google (again).</p><p>I am posting what I found, for (main, and) your reference.<span
id="more-11"></span></p><h2><a
title="a-working-one" name="a-working-one" id="a-working-one"></a>A working one</h2><p
class="level2"> <strong>Update</strong>: the function I found didn’t pass my testcases. I had to craft one myself. A raw one, but this should work (really).</p><p>I apologize for ugly code, I just wanted to get something fast.</p><pre class="code php"><span class="kw2">function</span> absolutizeUrl <span class="br0">(</span> <span class="re0">$u</span>, <span class="re0">$p</span> <span class="br0">)</span>
<span class="br0">{</span>
	<span class="re0">$url</span> = <a href="http://www.php.net/parse_url"><span class="kw3">parse_url</span></a><span class="br0">(</span> <span class="re0">$u</span> <span class="br0">)</span>;
	<span class="re0">$page</span> = <a href="http://www.php.net/parse_url"><span class="kw3">parse_url</span></a><span class="br0">(</span> <span class="re0">$p</span> <span class="br0">)</span>;
	<span class="kw1">if</span> <span class="br0">(</span> <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">(</span> <span class="re0">$u</span> , <span class="st0">'/'</span> <span class="br0">)</span> === <span class="nu0">0</span> <span class="br0">)</span>
	<span class="br0">{</span>
		<span class="co1">//already absolute		</span>
	<span class="br0">}</span> <span class="kw1">else</span> <span class="br0">{</span>
		<span class="re0">$basePath</span> = <span class="st0">''</span>;
		<span class="kw1">if</span> <span class="br0">(</span>
			<a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">(</span> <span class="re0">$page</span><span class="br0">[</span> <span class="st0">'path'</span> <span class="br0">]</span> <span class="br0">)</span>
			&amp;&amp; <a href="http://www.php.net/strpos"><span class="kw3">strpos</span></a><span class="br0">(</span> <a href="http://www.php.net/ltrim"><span class="kw3">ltrim</span></a><span class="br0">(</span> <span class="re0">$page</span><span class="br0">[</span> <span class="st0">'path'</span> <span class="br0">]</span>, <span class="st0">'/'</span> <span class="br0">)</span>, <span class="st0">'/'</span> <span class="br0">)</span>
		<span class="br0">)</span>
		<span class="br0">{</span>
			<span class="re0">$baseTokens</span> = <a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">(</span> <span class="st0">'/'</span>, <span class="re0">$page</span><span class="br0">[</span> <span class="st0">'path'</span> <span class="br0">]</span> <span class="br0">)</span>;
			<a href="http://www.php.net/array_pop"><span class="kw3">array_pop</span></a><span class="br0">(</span> <span class="re0">$baseTokens</span> <span class="br0">)</span>; <span class="co1">// strip basename			</span>
			<span class="re0">$baseTokens</span><span class="br0">[</span><span class="br0">]</span> = <span class="re0">$u</span>;
			<span class="re0">$u</span> = <a href="http://www.php.net/join"><span class="kw3">join</span></a><span class="br0">(</span> <span class="st0">'/'</span>, <span class="re0">$baseTokens</span> <span class="br0">)</span>;
		<span class="br0">}</span>
	<span class="br0">}</span>
	<span class="kw1">if</span> <span class="br0">(</span> ! <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">(</span> <span class="re0">$url</span><span class="br0">[</span> <span class="st0">'host'</span> <span class="br0">]</span><span class="br0">)</span><span class="br0">)</span>
	<span class="br0">{</span>
		<span class="re0">$u</span> = <span class="st0">'http://'</span>.<span class="re0">$page</span><span class="br0">[</span> <span class="st0">'host'</span> <span class="br0">]</span>.<span class="st0">'/'</span>.<a href="http://www.php.net/ltrim"><span class="kw3">ltrim</span></a><span class="br0">(</span> <span class="re0">$u</span>, <span class="st0">'/'</span> <span class="br0">)</span>;
	<span class="br0">}</span>
	<span class="kw1">return</span> <span class="re0">$u</span>;
<span class="br0">}</span></pre><h2><a
title="the-bad-one-i-found" name="the-bad-one-i-found" id="the-bad-one-i-found"></a>The bad one I found</h2><p><strong>Note:</strong> this is <strong>not</strong> <em>error_reporting( E_ALL )</em> safe. You may have to edit (I had) to avoid stupid notices on the screen.</p><pre class="code php"><span class="kw2">function</span> absolutizeUrl<span class="br0">(</span> <span class="re0">$relative</span>, <span class="re0">$absolute</span> <span class="br0">)</span>
<span class="br0">{</span>
	<a href="http://www.php.net/extract"><span class="kw3">extract</span></a><span class="br0">(</span><a href="http://www.php.net/parse_url"><span class="kw3">parse_url</span></a><span class="br0">(</span><span class="re0">$absolute</span><span class="br0">)</span><span class="br0">)</span>;
	<span class="kw1">if</span><span class="br0">(</span><span class="re0">$relative</span><span class="br0">{</span><span class="nu0">0</span><span class="br0">}</span> == <span class="st0">'/'</span><span class="br0">)</span> <span class="br0">{</span>
		<span class="re0">$cparts</span> = <a href="http://www.php.net/array_filter"><span class="kw3">array_filter</span></a><span class="br0">(</span><a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">(</span><span class="st0">"/"</span>, <span class="re0">$relative</span><span class="br0">)</span><span class="br0">)</span>;
	<span class="br0">}</span>
	<span class="kw1">else</span>
	<span class="br0">{</span>
		<span class="re0">$aparts</span> = <a href="http://www.php.net/array_filter"><span class="kw3">array_filter</span></a><span class="br0">(</span><a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">(</span><span class="st0">"/"</span>, <span class="re0">$path</span><span class="br0">)</span><span class="br0">)</span>;
		<span class="re0">$rparts</span> = <a href="http://www.php.net/array_filter"><span class="kw3">array_filter</span></a><span class="br0">(</span><a href="http://www.php.net/explode"><span class="kw3">explode</span></a><span class="br0">(</span><span class="st0">"/"</span>, <span class="re0">$relative</span><span class="br0">)</span><span class="br0">)</span>;
		<span class="re0">$cparts</span> = <a href="http://www.php.net/array_merge"><span class="kw3">array_merge</span></a><span class="br0">(</span><span class="re0">$aparts</span>, <span class="re0">$rparts</span><span class="br0">)</span>;
		<span class="kw1">foreach</span><span class="br0">(</span><span class="re0">$cparts</span> <span class="kw1">as</span> <span class="re0">$i</span> =&gt; <span class="re0">$part</span><span class="br0">)</span>
		<span class="br0">{</span>
			<span class="kw1">if</span><span class="br0">(</span><span class="re0">$part</span> == <span class="st0">'.'</span><span class="br0">)</span>
			<span class="br0">{</span>
				<span class="re0">$cparts</span><span class="br0">[</span><span class="re0">$i</span><span class="br0">]</span> = <span class="kw2">null</span>;
			<span class="br0">}</span>
			<span class="kw1">if</span><span class="br0">(</span><span class="re0">$part</span> == <span class="st0">'..'</span><span class="br0">)</span>
			<span class="br0">{</span>
				<span class="re0">$cparts</span><span class="br0">[</span><span class="re0">$i</span> - <span class="nu0">1</span><span class="br0">]</span> = <span class="kw2">null</span>;
				<span class="re0">$cparts</span><span class="br0">[</span><span class="re0">$i</span><span class="br0">]</span> = <span class="kw2">null</span>;
			<span class="br0">}</span>
		<span class="br0">}</span>
		<span class="re0">$cparts</span> = <a href="http://www.php.net/array_filter"><span class="kw3">array_filter</span></a><span class="br0">(</span><span class="re0">$cparts</span><span class="br0">)</span>;
	<span class="br0">}</span>
	<span class="re0">$path</span> = <a href="http://www.php.net/implode"><span class="kw3">implode</span></a><span class="br0">(</span><span class="st0">"/"</span>, <span class="re0">$cparts</span><span class="br0">)</span>;
	<span class="re0">$url</span> = <span class="st0">""</span>;
	<span class="kw1">if</span><span class="br0">(</span><span class="re0">$scheme</span><span class="br0">)</span>
	<span class="br0">{</span>
		<span class="re0">$url</span> = <span class="st0">"$scheme://"</span>;
	<span class="br0">}</span>
	<span class="kw1">if</span><span class="br0">(</span><span class="re0">$user</span><span class="br0">)</span>
	<span class="br0">{</span>
		<span class="re0">$url</span> .= <span class="st0">"$user"</span>;
		<span class="kw1">if</span><span class="br0">(</span><span class="re0">$pass</span><span class="br0">)</span>
		<span class="br0">{</span>
		<span class="re0">$url</span> .= <span class="st0">":$pass"</span>;
		<span class="br0">}</span>
		<span class="re0">$url</span> .= <span class="st0">"@"</span>;
	<span class="br0">}</span>
	<span class="kw1">if</span><span class="br0">(</span><span class="re0">$host</span><span class="br0">)</span>
	<span class="br0">{</span>
		<span class="re0">$url</span> .= <span class="st0">"$host/"</span>;
	<span class="br0">}</span>
	<span class="re0">$url</span> .= <span class="re0">$path</span>;
	<span class="kw1">return</span> <span class="re0">$url</span>;
<span class="br0">}</span></pre><p><a
href="http://www.thescripts.com/forum/thread3648.html" class="urlextern" title="http://www.thescripts.com/forum/thread3648.html">Found it here</a>.</p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/how-to-build-an-absolute-url-in-php/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>A piece of story</title><link>http://www.stefanoforenza.com/a-piece-of-story/</link> <comments>http://www.stefanoforenza.com/a-piece-of-story/#comments</comments> <pubDate>Fri, 02 Mar 2007 17:21:48 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[php]]></category> <guid
isPermaLink="false">http://devnull.dreamhosters.com/2007/03/02/a-piece-of-story/</guid> <description><![CDATA[What I found. Oh! Look, look ! As old-schoolers know Deja News, an historical Usenet repository was lately acquired by Google some year ago for Google Groups service, still conserves intact pieces of story of the web. Searching in google for google groups php at the first place what I found is (suspance): Rasmus Lerdorf&#8217;s first PHP stable release announce!!]]></description> <content:encoded><![CDATA[<p
class="include hentry"><a
name="a-piece-of-story" id="a-piece-of-story"></a> What I found. Oh! Look, look !</p><p
class="entry-content"><p
class="level1"> As old-schoolers know <a
href="http://en.wikipedia.org/wiki/DejaNews" class="urlextern" title="http://en.wikipedia.org/wiki/DejaNews">Deja News</a>, an historical <a
href="http://en.wikipedia.org/wiki/Usenet" class="urlextern" title="http://en.wikipedia.org/wiki/Usenet">Usenet</a> repository was lately acquired by Google some year ago for <a
href="http://groups.google.com/" class="urlextern" title="http://groups.google.com/">Google Groups</a> service, still conserves intact pieces of story of the web.</p><p><a
href="http://www.google.it/search?q=google+groups+php&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:official&amp;client=firefox-a" class="urlextern" title="http://www.google.it/search?q=google+groups+php&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=com.ubuntu:en-US:official&amp;client=firefox-a">Searching in google for google groups php</a> at the first place what I found is (suspance):</p><p><em><a
href="http://groups.google.ch/group/comp.infosystems.www.authoring.cgi/msg/cc7d43454d64d133?oe=UTF-8&amp;output=gplain" class="urlextern" title="http://groups.google.ch/group/comp.infosystems.www.authoring.cgi/msg/cc7d43454d64d133?oe=UTF-8&amp;output=gplain">Rasmus Lerdorf&#8217;s first PHP stable release announce</a>!!</em></p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/a-piece-of-story/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Xdump: a little piece of my story as developer</title><link>http://www.stefanoforenza.com/xdump-a-little-piece-of-my-story-as-developer/</link> <comments>http://www.stefanoforenza.com/xdump-a-little-piece-of-my-story-as-developer/#comments</comments> <pubDate>Mon, 19 Feb 2007 16:56:37 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[php]]></category> <category><![CDATA[projects]]></category> <guid
isPermaLink="false">http://devnull.dreamhosters.com/2007/02/19/xdump-a-little-piece-of-my-story-as-developer/</guid> <description><![CDATA[I am slowly beginning to pull online docs and samples for Xdump. Xdump was one debugging tool I had to develop for the reason that in some situations var_dump() simply is not enough. Here’s the overview of xdump. Of course one could say that as good programmer you should never ever findy yourself in such a situation which requires something more powerful than var_dump() or print_r(). I can understand this kind of point-of-view, but I ...]]></description> <content:encoded><![CDATA[<p><a
title="xdumpa-little-piece-of-my-story-as-developer" name="xdumpa-little-piece-of-my-story-as-developer" id="xdumpa-little-piece-of-my-story-as-developer"></a> I am slowly beginning to pull online docs and samples for <strong>Xdump</strong>.</p><p
class="level1"> Xdump was one debugging tool I had to develop for the reason that in some situations <a
href="http://php.net/var_dump" class="urlextern" title="http://php.net/var_dump">var_dump()</a> simply is not enough.</p><h2><a
title="section" name="section" id="section"></a></h2><p
class="level2"> Here’s the <a
href="/2007/01/23/xdump/">overview of xdump</a>.</p><p>Of course one could say that as good programmer you should never ever findy yourself in such a situation which requires something more powerful than <a
href="http://php.net/var_dump" class="urlextern" title="http://php.net/var_dump">var_dump()</a> or <a
href="http://php.net/print_r" class="urlextern" title="http://php.net/print_r">print_r()</a>. I can understand this kind of point-of-view, but I found that real world not always is like that.</p><p>Today at work, for example, I had to add some functionality to a <a
href="http://www.zen-cart.com/" class="urlextern" title="http://www.zen-cart.com/">zen-cart</a> installation. <a
href="http://www.stefanoforenza.com/xdump/zen-cart-example" class="wikilink1" title="xdump:zen-cart-example">This is something var_dump simply cannot manage</a> (without driving you crazy, I mean).<span
id="more-12"></span></p><h2><a
title="download" name="download" id="download"></a>Download</h2><p><del>I will try to make download available as soon as I can.</del></p><p><a
href="http://xdump.googlecode.com/files/xdump%2020070220.zip" class="urlextern" title="http://xdump.googlecode.com/files/xdump%2020070220.zip">Download available on google code</a></p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/xdump-a-little-piece-of-my-story-as-developer/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Need for Cache</title><link>http://www.stefanoforenza.com/need-for-cache/</link> <comments>http://www.stefanoforenza.com/need-for-cache/#comments</comments> <pubDate>Fri, 16 Feb 2007 10:04:36 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[php]]></category> <category><![CDATA[projects]]></category> <guid
isPermaLink="false">http://devnull.dreamhosters.com/2007/06/23/need-for-cache/</guid> <description><![CDATA[After going through PhpThumb hell, I decided to develop one serious decent caching library for caching dynamically generated images. Download &#160; Download zipped source from Google Code Or browse svn repository Download source from svn using: svn checkout http://xdump.googlecode.com/svn/trunk/ xdump Brief story One of our previous sites (about 50k page-views per day) was using a little php script for resizing dinamically images to thumbnail size. Since we didn’t knew in advance the size we was ...]]></description> <content:encoded><![CDATA[<p>After going through <a
href="http://phpthumb.sourceforge.net/" class="urlextern" title="http://phpthumb.sourceforge.net/">PhpThumb</a> hell, I decided to develop one <del>serious</del> decent <a
href="http://code.google.com/p/cache-on-request/" class="urlextern" title="http://code.google.com/p/cache-on-request/">caching library</a> for caching dynamically generated images.<span
id="more-6"></span></p><h2><a
title="download" name="download" id="download"></a>Download</h2><p
class="level2">&nbsp;</p><blockquote><p
class="no"> Download zipped source from <a
href="http://cache-on-request.googlecode.com/files/cache-on-request-veryalpha.zip" class="urlextern" title="http://cache-on-request.googlecode.com/files/cache-on-request-veryalpha.zip">Google Code</a></p><p>Or <a
href="http://cache-on-request.googlecode.com/svn/trunk/" class="urlextern" title="http://cache-on-request.googlecode.com/svn/trunk/">browse svn repository</a></p><p>Download source from svn using: <em>svn checkout <a
href="http://xdump.googlecode.com/svn/trunk/" class="urlextern" title="http://xdump.googlecode.com/svn/trunk/">http://xdump.googlecode.com/svn/trunk/</a> xdump</em></p></blockquote><h2><a
title="brief-story" name="brief-story" id="brief-story"></a>Brief story</h2><p
class="level2"> One of our previous sites (about 50k page-views per day) was using a little php script for resizing dinamically images to thumbnail size. Since we didn’t knew in advance the size we was gonna need, resize was made at run-time.</p><p>With site’s restyling the number of resized images on every page increased sensibly. As it’s easy to guess server load went sky-high. We tried to replace the package with <a
href="http://phpthumb.sourceforge.net/" class="urlextern" title="http://phpthumb.sourceforge.net/">PhpThumb</a> but the situation got even worse (yes, worse was possible).</p><h2><a
title="what-s-wrong-with-phpthumb" name="what-s-wrong-with-phpthumb" id="what-s-wrong-with-phpthumb"></a>What&#8217;s wrong with PhpThumb</h2><p
class="level2"> I have some problems with <a
href="http://phpthumb.sourceforge.net/" class="urlextern" title="http://phpthumb.sourceforge.net/">PhpThumb</a>. Oh yes, I do.</p><ul><li
class="level1"><p
class="li"> <strong>It’s bloated</strong>. Even if PhpThumb is a class, the caching routines are written in a procedural file who reads $_GET superglobal to decide what to do. This means I had to simulate <acronym
title="Hyper Text Transfer Protocol">HTTP</acronym> request by emptying manually $_GET and putting there only values PhpThumb needs. You don’t pass through that file.. you don’t cache.</p></li></ul><ul><li
class="level1"><p
class="li"> <strong>Cache working</strong>.</p><ul><li
class="level2"><p
class="li"> Does it work? I couldn’t verify, the only thing I know is that the cache directory kept becoming bigger, with apparently no limit. Actually, it became so big to cause server load by itself.</p></li><li
class="level2"><p
class="li"> I tried disabling cache in the config. Guess what? It kept caching, yes it did! I had to <em>physically delete</em> the cache path to block caching.</p></li><li
class="level2"><p
class="li"> Cached file retrieving. It’s made using <a
href="http://php.net/readfile" class="urlextern" title="http://php.net/readfile">readfile()</a> or <a
href="http://php.net/header" class="urlextern" title="http://php.net/header">header</a> redirects. While that’s better than nothing I think we can <a
href="http://meta.wikimedia.org/wiki/404_handler_caching" class="urlextern" title="http://meta.wikimedia.org/wiki/404_handler_caching">do better</a>, much better.</p></li></ul></li></ul><h2><a
title="strategy" name="strategy" id="strategy"></a>Strategy</h2><p
class="level2"> The solution I choose is what some call <strong>generate on request</strong>. This trick consists in creating a dynamic script, making it the default <strong>Apache’s ErrorDocument</strong> for the current directory and let it handle the content generation.</p><p>The flow:</p><ol><li
class="level1"><p
class="li"> Assume we store all downloadable wallpapers on our site are stored into /image/wallpapers directory.</p></li><li
class="level1"><p
class="li"> The website’s page will contain a non existant image path. Say something like: <em> <a
href="http://example.com/dynamic/40x30/images/wallpapers/britney.jpg" class="urlextern" title="http://example.com/dynamic/40x30/images/wallpapers/britney.jpg" rel="image_group">http://example.com/dynamic/40&#215;30/images/wallpapers/britney.jpg</a> </em></p></li><li
class="level1"><p
class="li"> At the time of the first visit to the page the resized image will not be there yet.</p></li><li
class="level1"><p
class="li"> When the image requests hits Apache, it will be redirected to our custom ErrorDocument (<em>front_controller.php</em>).</p></li><li
class="level1"><p
class="li"> <em>front_controller.php</em> will take care to read <em>/images/wallpapers/britney.jpg</em> generate our 40×30 nice thumbnail at <em>/dynamic/40×30/images/wallpapers/britney.jpg</em> path.</p></li><li
class="level1"><p
class="li"> Afterwars the user browser will be redirected to the generated image,  making it appear on the page.</p></li><li
class="level1"><p
class="li"> Every other request to the page will find the image already at the path requested, without needing <acronym
title="Hypertext Preprocessor">PHP</acronym> to do anything.</p></li></ol><h2><a
title="the-code" name="the-code" id="the-code"></a>The Code</h2><p
class="level2"> Here’s some code boxes to let you know how code looks like.</p><h3><a
title="htaccess" name="htaccess" id="htaccess"></a>.htaccess</h3><p
class="level3"> The only purpose of .htaccess here is to instruct to consider our <strong>Front Controller</strong> the default error page for the generated images path.</p><p>The first time an image is requested the Front Controller manages to create it at the requested path, and to redirect the user to it.</p><p>The following requests will find the image already waiting for them. This means the file will be served by Apache itself with no php intermediation. This can lead to performance improvement around the order of magnitude.</p><pre class="code apache"><span class="kw1">ErrorDocument</span> <span class="nu0">404</span> /dynamic/front_controller.php</pre><h3><a
title="front-controller" name="front-controller" id="front-controller"></a>Front Controller</h3><p
class="level3"> The purpouse of this file is to include libs, instantiate main class, decide which drivers are to be used and make thing run.</p><pre class="code php"><span class="kw1">include</span> <span class="br0">(</span> <span class="st0">'_libs/logger.php'</span> <span class="br0">)</span>;
<span class="kw1">include</span> <span class="br0">(</span> <span class="st0">'_libs/cache_on_request.php'</span> <span class="br0">)</span>;
<span class="re0">$FrontController</span> = <span class="kw2">new</span> GenerateOnRequest<span class="br0">(</span><span class="br0">)</span>;
<span class="co1">//decomment the following 2 lines only for debug purpouse</span>
<span class="re0">$FrontController</span>-&gt;<span class="me1">configuration</span><span class="br0">[</span><span class="st0">'debug'</span><span class="br0">]</span>=<span class="kw2">true</span>; <span class="co1">//prints logs (and hinibits header final)</span>
<span class="co1">//$FrontController-&gt;configuration['stopGeneration']=true; //checked by drivers, hinibits concrete file generation</span>
<span class="co1">//attacching drivers</span>
<span class="re0">$FrontController</span>-&gt;<span class="me1">attachDriver</span><span class="br0">(</span> <span class="kw2">new</span> ResetController <span class="br0">)</span>;
<span class="re0">$FrontController</span>-&gt;<span class="me1">attachDriver</span><span class="br0">(</span> <span class="kw2">new</span> GdImageResizer <span class="br0">)</span>;
<span class="re0">$FrontController</span>-&gt;<span class="me1">run</span><span class="br0">(</span> <span class="re0">$_SERVER</span><span class="br0">[</span> <span class="st0">'REQUEST_URI'</span> <span class="br0">]</span>, <a href="http://www.php.net/dirname"><span class="kw3">dirname</span></a><span class="br0">(</span><span class="kw2">__FILE__</span> <span class="br0">)</span> <span class="br0">)</span>;</pre><p
class="tags"><span> <a
href="http://www.stefanoforenza.com/tags/php" class="wikilink2" title="tags:php" rel="tag">php</a>, <a
href="http://www.stefanoforenza.com/tags/lib-dev" class="wikilink2" title="tags:lib-dev" rel="tag">lib-dev</a> </span></p> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/need-for-cache/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Xdump</title><link>http://www.stefanoforenza.com/xdump/</link> <comments>http://www.stefanoforenza.com/xdump/#comments</comments> <pubDate>Tue, 23 Jan 2007 16:58:38 +0000</pubDate> <dc:creator>Stefano Forenza</dc:creator> <category><![CDATA[development]]></category> <category><![CDATA[php]]></category> <category><![CDATA[projects]]></category> <guid
isPermaLink="false">http://devnull.dreamhosters.com/2007/01/23/xdump/</guid> <description><![CDATA[When working with large arrays/objects,with complex references beetween variables or (again) php’s built-in var_dump() e debug_backtrace() shows their limits. Have you ever tried to var_dump $GLOBALS ? Even if when I started this lib there were already some classes which provided nice html formatted dump, in the moment I needed one, I couldn’t (my fault) find one. Xdump started as a bunch of procedural lines of code and slowly evolved in a object-oriented library which ...]]></description> <content:encoded><![CDATA[<p><a
title="x-dump" name="x-dump" id="x-dump"></a> When working with large arrays/objects,with complex references beetween variables or (again) php’s built-in <a
href="http://php.net/var_dump" class="urlextern" title="http://php.net/var_dump">var_dump()</a> e <a
href="http://php.net/debug_backtrace" class="urlextern" title="http://php.net/debug_backtrace">debug_backtrace()</a> shows their limits.</p><p
class="level1"> Have you ever tried to var_dump $GLOBALS ?</p><p>Even if when I started this lib there were already some classes which provided nice html formatted dump, in the moment I needed one, I couldn’t (my fault) find one.<span
id="more-13"></span></p><p><strong>Xdump</strong> started as a bunch of procedural lines of code and slowly evolved in a object-oriented library which can provide <em>nice collapsable html dumps and backtraces</em>, <em>spoof references</em> (to avoid dump bloat and recursions) and <em>show source code lines</em> where dump was invoked as well as display on screen <em>source code lines for every backtrace jump</em>.</p><h2><a
title="download" name="download" id="download"></a>Download</h2><p
class="level2">&nbsp;</p><blockquote><p
class="no"> <a
href="http://code.google.com/p/xdump/downloads/list" class="urlextern" title="http://code.google.com/p/xdump/downloads/list">Xdump Download Page</a></p></blockquote><h2><a
title="overview" name="overview" id="overview"></a>Overview</h2><p
class="level2"> Xdump let’s you display php vars nicely on the screen.</p><ul><li
class="level1"><p
class="li"> Jump to examples</p></li><li
class="level2"><p
class="li"> Test cases are provided (requires SimpleTest)</p></li></ul><h2><a
title="usage" name="usage" id="usage"></a>Usage</h2><p
class="level2">&nbsp;</p><ol><li
class="level1"><p
class="li"> Include “xdump/xdump.php” in your main include file.</p></li><li
class="level1"><p
class="li"> Use following code:</p></li></ol><pre class="code php"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> xdump::<span class="me2">dump</span><span class="br0">(</span><span class="re0">$var</span><span class="br0">)</span>;</pre><ol><li
class="level1"><p
class="li"> To show a backtrace instead type:</p></li></ol><pre class="code php"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> xdump::<span class="me2">backtrace</span><span class="br0">(</span><span class="br0">)</span>;</pre><h2><a
title="samples" name="samples" id="samples"></a>Samples</h2><p
class="level2"> <a
href="http://www.stefanoforenza.com/xdump/zen-cart-example" class="wikilink1" title="xdump:zen-cart-example">Dumping get_defined_vars() inside a Zen-Cart template</a></p><h2><a
title="licence-and-contact" name="licence-and-contact" id="licence-and-contact"></a>Licence and contact</h2><p
class="level2">&nbsp;</p><ul><li
class="level1"><p
class="li"> Xdump is <acronym
title="Massachusetts Institute of Technology">MIT</acronym> Licensed.</p></li><li
class="level2"><p
class="li"> Info, suggestions and stuff to: stefano@stefanoforenza.com</p></li></ul><h2><a
title="features" name="features" id="features"></a>Features</h2><p
class="level2"> <strong>General</strong></p><ul><li
class="level1"><p
class="li"> Displays php dumps more nicely</p></li><li
class="level2"><p
class="li"> Dumps are javascript contractible. They occupy little space on the page and open when clicked.</p></li><li
class="level2"><p
class="li"> Display nicely without styles and javascript as well (if you work with them disabled)</p></li><li
class="level2">Shows file, line number and source lines around the dump invocation (no more digging around to remove debugging code). Shows source lines for every backtrace jump as well (neat! try it).</li><li
class="level2"><p
class="li"> (almost) Plug &amp; Play</p></li></ul><p><strong>Technical</strong></p><ul><li
class="level1"><p
class="li"> Handles reference recursions inside objects and arrays (no endless loop). Will signal visually nested references (the parent referenced element will blink orange).</li><li
class="level2"><p
class="li"> PHP5 Overloading aware (doesn’t touch <em
class="u">get, </em>set etc)</p></li><li
class="level2"><p
class="li"> PHP4 Compatible</p></li><li
class="level2"><p
class="li"> Shows you backtrace info and the chunk of the code that invoked the dump</p></li><li
class="level2">Some little effort to limit the size of markup inside the dump. Keep in mind that the dump will anyway be bigger than a vardump</li></ul> ]]></content:encoded> <wfw:commentRss>http://www.stefanoforenza.com/xdump/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> </channel> </rss>