<?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>Chris&#039; Blog &#187; code</title>
	<atom:link href="http://www.cs278.org/blog/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cs278.org/blog</link>
	<description>“A metal loving, web developing, open source contributor.”</description>
	<lastBuildDate>Sun, 03 Apr 2011 14:36:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>phpBB Hook to remove success messages</title>
		<link>http://www.cs278.org/blog/2010/02/16/phpbb-hook-to-remove-success-messages/</link>
		<comments>http://www.cs278.org/blog/2010/02/16/phpbb-hook-to-remove-success-messages/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 22:32:59 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[phpBB]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[modification]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.cs278.org/blog/?p=531</guid>
		<description><![CDATA[Time for another phpBB hook, this time the aim is to remove the messages which irritate some people when a post has been made and instead redirect the user immediately to the default choice. A few considerations had to be &#8230; <a href="http://www.cs278.org/blog/2010/02/16/phpbb-hook-to-remove-success-messages/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Time for another <a href="http://www.phpbb.com/" rel="external">phpBB</a> <a href="http://www.cs278.org/blog/tag/hook/">hook</a>, this time the aim is to remove the messages which irritate some people when a post has been made and instead redirect the user immediately to the default choice. A few considerations had to be made with this, firstly the administration panel is off limits as this uses messages for success and failure in some places. Secondly any page which goes back to itself by default is made confusing if there isn&#8217;t a confirmation, so these aren&#8217;t automatically redirected; a good example of this being the UCP preferences.<span id="more-531"></span></p>
<p>This change exploits phpBB&#8217;s <a href="http://code.phpbb.com/svn/phpbb/branches/phpBB-3_0_0/phpBB/docs/hook_system.html">hook system</a> which allows us to execute custom code in certain phpBB functions/methods without modifying any files. To do this we need to create a new file in our phpBB installation in the <code>includes/hooks/</code> directory, the file name must begin with <code>hook_</code> and end with your PHP extension (usually <code>.php</code>.) So, basically you need to drop the below file into your <code>includes/hooks/</code> directory and purge the board cache, job done!</p>
<p><script src="http://gist.github.com/305915.js?file=hook_disable-delayed-redirects.php"></script><noscript>
<p><a href="http://gist.github.com/305915" rel="external">includes/hooks/hook_disable-delayed-redirects.php</a></p>
<p></noscript></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cs278.org/blog/2010/02/16/phpbb-hook-to-remove-success-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP function: is_serialized()</title>
		<link>http://www.cs278.org/blog/2009/10/23/php-function-is_serialized/</link>
		<comments>http://www.cs278.org/blog/2009/10/23/php-function-is_serialized/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 19:12:32 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.cs278.org/blog/?p=502</guid>
		<description><![CDATA[I needed a way to detect if some values were serialized or not, annoyingly PHP does not provide a method for this. I&#8217;d also rather not perform unserialize() on everything to check for a serialized string in the name of &#8230; <a href="http://www.cs278.org/blog/2009/10/23/php-function-is_serialized/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I needed a way to detect if some values were serialized or not, annoyingly PHP does not provide a method for this. I&#8217;d also rather not perform unserialize() on everything to check for a serialized string in the name of efficiency, so instead I came up with a simple function to detect serialized data. The result of my endeavours is a function that checks a string for basic makers of the PHP serialization form, it only checks the variables type and not any deeper values inside arrays or objects, if this preliminary testing is successful then unserialize() is invoked to provide the final proof. The unserialized form is available as an option argument passed by reference and the return value is a boolean, true for boolean or false otherwise. You can find the function on <a href="http://gist.github.com/217091" rel="external">Gist</a> or below. <span id="more-502"></span></p>
<p><script src="http://gist.github.com/217091.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cs278.org/blog/2009/10/23/php-function-is_serialized/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Another phpBB Hook</title>
		<link>http://www.cs278.org/blog/2009/04/05/another-phpbb-hook/</link>
		<comments>http://www.cs278.org/blog/2009/04/05/another-phpbb-hook/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 14:56:11 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[phpBB]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[modification]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.cs278.org/blog/?p=475</guid>
		<description><![CDATA[Further to my entry last month about a phpBB Hook to disable the post queue for certain users, I&#8217;ve written a really quick one to enable phpBB&#8217;s debugging mode for founder users. Debug mode is usually enabled by using the &#8230; <a href="http://www.cs278.org/blog/2009/04/05/another-phpbb-hook/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Further to my <a href="http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/">entry</a> last month about a phpBB Hook to disable the post queue for certain users, I&#8217;ve written a really quick one to enable phpBB&#8217;s debugging mode for founder users. Debug mode is usually enabled by using the constants <code>DEBUG</code> and <code>DEBUG_EXTRA</code> in the configuration file but these can be defined anywhere. The only downside debugging information will only be available after the hook is included, however this is sufficient to get the page generation time, memory usage etc. in the footer which is what I was after. <span id="more-475"></span></p>
<p><a href="http://gist.github.com/90448" rel="external">includes/hooks/hook_enable-debug-for-founders.php </a></p>
<pre>&lt;?php

/**
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details.
 */

if (!defined('IN_PHPBB'))
{
	exit;
}

/**
 * This hook enables the debug mode for founders
 */
function hook_enable_debug_for_founders(&#038;$hook)
{
	global $user;

	if ($user-&gt;data['user_type'] == USER_FOUNDER)
	{
		// Be careful when defining the constants

		if (!defined('DEBUG'))
		{
			define('DEBUG', true);
		}

		if (!defined('DEBUG_EXTRA'))
		{
			define('DEBUG_EXTRA', true);
		}
	}
}

$phpbb_hook-&gt;register('phpbb_user_session_handler', 'hook_enable_debug_for_founders');</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cs278.org/blog/2009/04/05/another-phpbb-hook/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>phpBB Post Queue</title>
		<link>http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/</link>
		<comments>http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 17:58:06 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[phpBB]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[hook]]></category>
		<category><![CDATA[modification]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[xmedia]]></category>

		<guid isPermaLink="false">http://www.cs278.org/blog/?p=463</guid>
		<description><![CDATA[In phpBB 3.0.3 we added the possibility to queue posts of users who had a post count lower than an administrator defined threshold, this works very well at keeping spam out of the public eye on your board. But on &#8230; <a href="http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.phpbb.com/community/viewtopic.php?f=14&#038;t=1292765" rel="external">phpBB 3.0.3</a> we added the possibility to queue posts of users who had a post count lower than an administrator defined threshold, this works very well at keeping spam out of the public eye on your board. But on the <a href="http://xmedia.ex.ac.uk/forums/" rel="external">board</a> I administer which is primarily for <a href="http://www.exeter.ac.uk/" rel="external">university</a> students who all have their own university e-mail address it penalises new users who I as the board administrator trust. To get around this, I will be applying a small modification to our board during the next maintenance period which I thought I will share with you. <span id="more-463"></span></p>
<p>This change exploits phpBB&#8217;s <a href="http://code.phpbb.com/svn/phpbb/branches/phpBB-3_0_0/phpBB/docs/hook_system.html">hook system</a> which allows us to execute custom code in certain phpBB functions/methods without modifying any files. To do this we need to create a new file in our phpBB installation in the <code>includes/hooks/</code> directory, the file name must begin with <code>hook_</code> and end with your PHP extension (usually <code>.php</code>.)</p>
<p><a href="http://gist.github.com/79491">includes/hooks/hook_trusted-users.php</a>:</p>
<pre>&lt;?php

/**
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details.
 */

if (!defined('IN_PHPBB'))
{
	exit;
}

/**
 * This hook disables the post queue for accounts registered with a trusted
 * e-mail address.
 */
function hook_trusted_users(&#038;$hook)
{
	global $config, $user;

	/**
	 * Skip when the queue is disabled, or this is an administration
	 * session so we don't break the ACP, chances are that administrators
	 * have moderation permissions or more posts than required to post any way.
	 */
	if ($config['enable_queue_trigger'] &#038;&#038; !$user-&gt;data['session_admin'] &#038;&#038; preg_match('#@example\.com$#i', $user-&gt;data['user_email']))
	{
		$config['enable_queue_trigger'] = 0; // Disable the post queue for our trusted users
	}
}

$phpbb_hook-&gt;register('phpbb_user_session_handler', 'hook_trusted_users');</pre>
<p>Once you have dropped the file in place you will need to purge your cache, or just manually delete <code>cache/data_hooks.php</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cs278.org/blog/2009/03/15/phpbb-post-queue/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WordPress archives</title>
		<link>http://www.cs278.org/blog/2009/03/03/wordpress-archives/</link>
		<comments>http://www.cs278.org/blog/2009/03/03/wordpress-archives/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 05:11:32 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Site]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[mods]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://www.cs278.org/blog/?p=451</guid>
		<description><![CDATA[I&#8217;ve been running my blog since late 2005 as such my archives had been getting a little bit unwieldy in the sidebar and stretching the page to extraordinary heights. I&#8217;ve reduced this clutter with some very small changes. First I &#8230; <a href="http://www.cs278.org/blog/2009/03/03/wordpress-archives/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been running my blog since <a href="http://www.cs278.org/blog/2005/12/07/welcome/">late 2005</a> as such my archives had been getting a little bit unwieldy in the sidebar and stretching the page to extraordinary heights. I&#8217;ve reduced this clutter with some very small changes. First I dug around only to find out that WordPress doesn&#8217;t support fetching the monthly archives for one year, so I had to apply some filter foomongery. I discovered somebody else has done <a href="http://www.misthaven.org.uk/blog/2009/01/11/filtering-archives/" rel="external">something similar</a>, not quite as nice personally but it works.<br />
<span id="more-451"></span><br />
First I created a filter in my WordPress installation and added it.</p>
<pre>function getarchives_filter($where, $args)
{
	if (isset($args['year']))
	{
		$where .= ' AND YEAR(post_date) = ' . intval($args['year']);
	}
	return $where;
}
add_filter('getarchives_where', 'getarchives_filter', 10, 2);
</pre>
<p>Now the <a href="http://codex.wordpress.org/Template_Tags/wp_get_archives" rel="external"><code>wp_get_archives()</code></a> function supports a year parameter which restricts results to one particular year. Next we modify the sidebar to make use of this additional argument. Here is an example of the correct call to make:</p>
<pre>&lt;?php wp_get_archives('type=monthly&amp;year=' . get_the_time('Y', reset($GLOBALS['posts'])); ?&gt;</pre>
<p>This code also works properly when viewing the archives of a particular year, the months outputted change to the currently viewed year.</p>
<p>I have changed my own sidebar code to use this:</p>
<pre>
&lt;li&gt;&lt;h2&gt;Archives&lt;/h2&gt;
        &lt;ul&gt;
            &lt;li&gt;&lt;strong&gt;Months&lt;/strong&gt;
            &lt;ul&gt;
                    &lt;?php
                    // Use the current date when viewing a page, not the pages year
                    $year = (is_page()) ? date('Y') : get_the_time('Y', reset($GLOBALS['posts']));

                    wp_get_archives('type=monthly&amp;limit=12&amp;year[]=' . $year . (!is_archive() ? '&amp;year[]=' . ($year - 1) : ''));
                    ?&gt;
            &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li&gt;&lt;strong&gt;Years&lt;/strong&gt;
            &lt;ul&gt;
                    &lt;?php wp_get_archives('type=yearly'); ?&gt;
            &lt;/ul&gt;
            &lt;/li&gt;
        &lt;/ul&gt;
&lt;/li&gt;
</pre>
<p class="note"><strong>Updated 31/01/2010:</strong> Paul spotted a bug, the code used the date of the last post displayed on the page rather than the first.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cs278.org/blog/2009/03/03/wordpress-archives/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

