<?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>Terrell Russell: This Old Network &#187; Code</title>
	<atom:link href="http://weblog.terrellrussell.com/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://weblog.terrellrussell.com</link>
	<description>Ideas on interconnections, identity, and information from all sides.</description>
	<lastBuildDate>Thu, 22 Dec 2011 15:31:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>User Merge plugin for PunBB</title>
		<link>http://weblog.terrellrussell.com/2008/01/user-merge-plugin-for-punbb/</link>
		<comments>http://weblog.terrellrussell.com/2008/01/user-merge-plugin-for-punbb/#comments</comments>
		<pubDate>Fri, 18 Jan 2008 05:49:00 +0000</pubDate>
		<dc:creator>Terrell Russell</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[punbb]]></category>
		<category><![CDATA[usermerge]]></category>

		<guid isPermaLink="false">http://weblog.terrellrussell.com/2008/01/user-merge-plugin-for-punbb/</guid>
		<description><![CDATA[To close out a week of very geeky Rails and PHP posts&#8230; I released an administrator plugin for PunBB earlier this week and Rickard has posted it on the PunBB Downloads page (about 3/4ths of the way down). User Merge. Created by Terrell Russell. The User Merge plugin allows administrators to merge two user accounts. [...]]]></description>
			<content:encoded><![CDATA[<p>To close out a week of <a href="http://weblog.terrellrussell.com/2008/01/running-php-within-rails/">very geeky Rails</a> and <a href="http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/">PHP posts</a>&#8230;</p>
<p>I released an administrator plugin for <a href="http://punbb.org/">PunBB</a> earlier this week and Rickard has posted it on the <a href="http://punbb.org/downloads.php">PunBB Downloads page</a> (about 3/4ths of the way down).</p>
<blockquote><p><a href="http://punbb.org/download/plugins/AP_User_Merge.zip">User Merge</a>. Created by <a href="http://www.terrellrussell.com/">Terrell Russell</a>. The User Merge plugin allows administrators to merge two user accounts.</p></blockquote>
<p>This follows the Broadcast Email plugin from a couple years ago&#8230;</p>
<blockquote><p><a href="http://punbb.org/download/plugins/AP_Broadcast_Email.zip">Broadcast Email</a>. Created by <a href="http://punbb.org/forums/profile.php?id=2330">trel1023</a>. The Broadcast Email plugin allows administrators to broadcast e-mail to all registered users via the administration interface.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://weblog.terrellrussell.com/2008/01/user-merge-plugin-for-punbb/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Generating a Rails and PunBB (and DokuWiki) shared cookie</title>
		<link>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/</link>
		<comments>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 03:24:31 +0000</pubDate>
		<dc:creator>Terrell Russell</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[dokuwiki]]></category>
		<category><![CDATA[punbb]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sso]]></category>

		<guid isPermaLink="false">http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/</guid>
		<description><![CDATA[This is another post for how I got something to work in the past week that&#8217;s been bugging me for a while. I recently wrote about how to get PHP to render correctly within a Rails app. This post is about getting single-sign-on to work with a PunBB forum inside your Ruby on Rails application. [...]]]></description>
			<content:encoded><![CDATA[<p>This is another post for how I got something to work in the past week that&#8217;s been bugging me for a while.  I recently wrote about <a href="http://weblog.terrellrussell.com/2008/01/running-php-within-rails/">how to get PHP to render correctly within a Rails app</a>.</p>
<p>This post is about getting single-sign-on to work with a PunBB forum inside your Ruby on Rails application.  I wanted to have a user who signs into the Rails app be &#8216;logged into&#8217; the forum as well.  This requires setting the cookie in the same way the PunBB code does it.</p>
<p>I have put this code in the bottom of my application.rb, so that it can be called from anywhere in the Rails app.  I would suggest setting the cookie on login and clearing it on logout.</p>
<p>The forum config regex is based on the configuration parser in the <a href="http://www.ahgsoftware.com/pages/punbb_sdk">PunBB SDK for Rails</a>.  It parses your existing PunBB cookie_name and cookie_seed from the PunBB install so you only need to keep that information in one place (PunBB).</p>
<p>The necessary <a href="http://www.aagh.net/files/ruby/php_serialize.rb">php_serialize.rb</a> file also comes from the /lib directory of the PunBB SDK plugin and is courtesy of <a href="http://www.aagh.net/files/ruby/php_serialize.rb">Thomas Hurst (also available directly)</a>.  It should be copied/placed into your own Rails app&#8217;s /lib directory and &#8216;require&#8217;d accordingly.  The line below ending in \\ indicates a forced linewrap &#8211; and should be pulled back into a single line if you copy and paste this code.</p>
<blockquote><pre>
  def set_shared_cookie
    # this is the punbb cookie
    # should be called on login from main site
    # the wiki uses the same cookie
    # setting it here allows unified login
    require 'digest/md5'
    forumconfig = get_forum_config_data() # private method at bottom
    # get forumuser info and set cookie
    forumuser = Forumaccount.find_by_username(@current_user.login)
    cookies[forumconfig[:cookie_name]] = {
      :value =&gt; PHP.serialize([forumuser.id, \\
         Digest::MD5.hexdigest("#{forumconfig[:cookie_seed]}#{forumuser.password}")]),
      :expires =&gt; 1.year.from_now
    }
  end

  def clear_shared_cookie
    # should be called on logout from main site
    require 'digest/md5'
    forumconfig = get_forum_config_data() # private method at bottom
    # set cookie for Guest
    cookies[forumconfig[:cookie_name]] = {
      :value =&gt; PHP.serialize([1,Digest::MD5.hexdigest("#{forumconfig[:cookie_seed]}Guest")]),
      :expires =&gt; 1.year.from_now
    }
  end

  # Uses regex to parse the php punbb config file
    # ahgsoftware.com/punbb_sdk/
    # make sure the config file exists
    # make sure 'RewriteEngine Off' is in /forum/.htaccess and wiki/.htaccess
  def get_forum_config_data
    config_hash = Hash.new
    c = File.read(File.join(RAILS_ROOT,'public/forum/config.php'))
    c.scan(/\$(\w*)\s*=\s*['"](.*)['"];/).each do |pair|
      config_hash[pair[0].to_sym] = pair[1]
    end
    return config_hash
  end
</pre>
</blockquote>
<p>Of course, keeping your users in sync across the main Rails app and the Forum install is its own trick &#8211; and necessary before the above cookie injection will work.  I&#8217;ve got Theforum and Forumaccount models that are wired to the PunBB database.  I keep the usernames and passwords synced whenever users/passwords are created/deleted/updated.</p>
<p>database.yml</p>
<blockquote><pre>theforum_production:
  adapter: mysql
  database: punbb_production
  host: localhost
  username: xxxxxxxxx
  password: xxxxxxxxx
</pre>
</blockquote>
<p>theforum.rb</p>
<blockquote><pre>class Theforum &lt; ActiveRecord::Base
  self.abstract_class = true
  establish_connection "theforum_#{RAILS_ENV}"
end
</pre>
</blockquote>
<p>forumaccount.rb</p>
<blockquote><pre>class Forumaccount < Theforum
  set_table_name :users

  def encrypt_and_save_new_password(password)
    write_attribute("password", self.sha1hashed(password))
    save
  end

  def sync_from_account(account)
    write_attribute("email", account.email)
    forumname = account.prefix+" "+account.first_name+" "+account.last_name
    write_attribute("realname", forumname)
    save
  end

  protected

  def sha1hashed(str)
    Digest::SHA1.hexdigest("#{str}")[0..39]
  end

end
</pre>
</blockquote>
<p>A separate trick was to make DokuWiki look for and pay attention to the PunBB cookie we created at the beginning of the post.  I got that for free with the <a href="http://wiki.splitbrain.org/wiki:auth:backends">shipping auth options in Dokuwiki</a>.  I simply pointed my Dokuwiki install at the PunBB install and the magic was complete.</p>
<p><strong>Success: A login to the Rails app also sets a cookie for PunBB which is fully honored by Dokuwiki.</strong></p>
<p>An additional benefit is now the entire site is under one codebase and can be installed/developed without moving as many pieces around.</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Running PHP within Rails</title>
		<link>http://weblog.terrellrussell.com/2008/01/running-php-within-rails/</link>
		<comments>http://weblog.terrellrussell.com/2008/01/running-php-within-rails/#comments</comments>
		<pubDate>Tue, 15 Jan 2008 01:28:05 +0000</pubDate>
		<dc:creator>Terrell Russell</dc:creator>
				<category><![CDATA[Default]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[dokuwiki]]></category>
		<category><![CDATA[integration]]></category>
		<category><![CDATA[phpinsiderails]]></category>
		<category><![CDATA[phpwithinrails]]></category>
		<category><![CDATA[punbb]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://weblog.terrellrussell.com/2008/01/running-php-within-rails/</guid>
		<description><![CDATA[So this has been something I&#8217;ve been putting off because it wasn&#8217;t readily apparent how to make it happen when I first tried over a year and a half ago. I wanted to manage a group of users and passwords for a community with an existing Ruby on Rails application &#8211; and then have both [...]]]></description>
			<content:encoded><![CDATA[<p>So this has been something I&#8217;ve been putting off because it wasn&#8217;t readily apparent how to make it happen when I first tried over a year and a half ago.</p>
<p>I wanted to manage a group of users and passwords for a community with an existing Ruby on Rails application &#8211; and then have both a PHP-based PunBB forum and PHP-based wiki running inside the Rails app.  Of course, more interestingly, I wanted to make the sessions and cookies all align and share a single sign-on.</p>
<p>And this past week, I poked enough at the right pieces to make it happen.</p>
<p>I first found <a href="http://macdiggs.com/index.php/2007/06/29/using-php-inside-rails-structure-on-apache/">this post at macdiggs.com</a> but found the solution to lack directory mapping in addition to allowing the PHP to execute inside the Rails /public/ directory.  I wanted http://example.com/forum to render and not throw a Rails error.  As it stood, only http://example.com/forum/index.php and URLs like it would come up correctly.</p>
<p>Then, I started seeing references to turning off Apache&#8217;s RewriteEngine&#8230;</p>
<p><strong>The solution I&#8217;ve now got in place has two parts:</strong></p>
<p>1) Edit the Apache configuration file for your virtualhost.  This tells Apache not to push any requests that start with /forum or /wiki to the Rails app (here, a mongrel cluster) &#8211; and to just handle them by itself (via the PHP processor).</p>
<blockquote><pre>RewriteEngine On
# send all /forum traffic to php
RewriteCond %{REQUEST_URI} ^/forum.*
RewriteRule .* - [L]
# send all /wiki traffic to php
RewriteCond %{REQUEST_URI} ^/wiki.*
RewriteRule .* - [L]
# Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]
&lt;Proxy balancer://mongrel_cluster&gt;
BalancerMember http://127.0.0.1:6000
BalancerMember http://127.0.0.1:6001
&lt;/Proxy&gt;
</pre>
</blockquote>
<p>2) Add/Edit the .htaccess file in both the forum and the wiki directories.  This tells any requests that were sent the way of either the forum or the wiki to halt the rewriting engine and just process the requests themselves.</p>
<blockquote><pre>RewriteEngine Off</pre>
</blockquote>
<p>Now, both http://example.com/forum and http://example.com/wiki render correctly.</p>
<p><a href="http://weblog.terrellrussell.com/2008/01/generating-a-rails-and-punbb-and-dokuwiki-shared-cookie/">As for getting the cookie to sync &#8211; please see my next post&#8230;</a></p>
<p>P.S. And now for all the phrases that I couldn&#8217;t find anywhere when I was looking for how to make all this magic happen &#8211; maybe someone else will find this stuff here: PHP within Rails, PHP inside Rails, PunBB in Rails app, Ruby on Rails, DokuWiki, PHP/Rails integration, PHP integration with Rails</p>
]]></content:encoded>
			<wfw:commentRss>http://weblog.terrellrussell.com/2008/01/running-php-within-rails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

