Avatar Brit Gardner’s Site

all the code that’s fit to printf()

Integrating CakePHP with bbPress - Part 2

This is the second of a 3 part tutorial on how to integrate bbPress with CakePHP 1.2.x. When complete, these 3 tutorials will accomplish:

  1. Integrating bbPress in a sub-folder (not a subdomain) of your cake install (part 1 here)
  2. Integrating user registration - when a user registers through your cake application they are automatically registered in bbPress.
  3. Integrating the login - when a user is logged in via the cake application they are automatically logged in in bbPress.

Part 2 - Integrating User Registration

This part is a little more tricky, but not unmanageable.   The challenge here is that your Cake application probably uses the Auth component, or a custom user setup.  So you probably have a prebuilt users table and chances are it does not conform to what bbPress is looking for in it’s users table.  Also, the encryption scheme between the two is probably different.

With that in mind, I’ve made the following design decisions

  1. My Cake users schema will remain intact AND the bbPress users schema will remain intact.
  2. Users must register through my Cake application only
  3. CakePHP is using the built-in Auth component.
  4. When a user registers through my Cake application - I run the necessary logic from the Cake User model to update the bbPress users table.
  5. The bbPress tables are in the same database as my CakePHP tables
  6. I change the encryption scheme of bbPress to conform to my Cake app.

Step 1: changing the bbPress password encryption.

If you’ve followed Part 1 of this tutorial, you’ve successfully installed and ran bbPress in a subfolder of your cakephp install.  Navigate to that subfolder, then open following file: bb-includes/backpress/code.wp-pass.php

Notice two functions, hash_password(...) and check_password(...).  We will overwrite these two functions to match the hashing that takes place in our CakePHP install.  If, as I denoted above, you are using the built-in CakePHP Auth component, this is very simple - just comment out the existing functions and replace them with the following:

  1.   function hash_password($string) {
  2.     $string = ‘YOUR CONFIGURE::SALT VALUE’.$string;
  3.     return sha1($string);
  4.   }
  5.  
  6.   function check_password($password, $hash, $user_id = ) {
  7.     $string = ‘YOUR CONFIGURE::SALT VALUE’.$password;
  8.     if(sha1($string) != $hash) {
  9.       return false;
  10.     } else {
  11.       return true;
  12.     }
  13.   }

Replace the YOUR CONFIGURE::SALT VALUE with the appropriate value from your CakePHP application. This can be found in app/config/core.php.

With these two functions in place, your bbPress user system should be hashing and checking the passwords the same way.

Step 2: Saving a bbPress user from the CakePHP install.

Fortunately, CakePHP provides an easy hook into the ORM saving process with the afterSave() model callback.

To get your User model to save a new bbPress user when a new Cake user is created, add the following functions to your User model script:

  1.   function afterSave($created)
  2.   {
  3.     if($created)
  4.     {
  5.       $sql = "INSERT INTO `bb_users`
  6.       (`user_login`, `user_pass`, `user_nicename`,
  7.       `user_email`, `user_url`, `user_registered`,
  8.       `user_status`, `display_name`)
  9.       VALUES(
  10.       ‘".$this->data[‘User’][‘username’]."’,
  11.       ‘".$this->data[‘User’][‘password’]."’,
  12.       ‘".$this->data[‘User’][‘username’]."’,
  13.       ‘".$this->data[‘User’][‘email’]."’,
  14.       ”,
  15.       NOW(),
  16.       0, ‘".$user[‘User’][‘username’]."’)";
  17.       $this->query($sql);
  18.     }
  19.   }

Notice that we are just creating an SQL statement with all the necessary fields required to successfully insert the user into the bbPress system, and then executing that SQL. Also, if you are using the default functionality of the Auth component, $this->data['User']['password']; should automatically be hashed with the same scheme as Step 1.

Step 3 - Preventing registration from bbPress

This is the easy part. Simply add the following line to the top of your register.php script in you bbPress install and replace the path with the correct path to your cake registration page.

  1. die(header(‘Location: /path/to/your/cake/registration/page’));

So, that’s it! Now, when you register a user through your Cake application, they should also appear in the bbPress users table, and you should be able to successfully log into both with the exact same credentials.

But, the plot thickens - we don’t want our users to have to log in to both the cake app and bbpress. We want them to be logged into both automatigically. Stay tuned for part 3 of this tutorial and I’ll attempt to tackle this very conundrum.

Integrating CakePHP with bbPress - Part 1

I’ve been working on StatForge.com for the past few days and one of the community features I want to integrate is a forum.  Rather than go for something bloated like phpbb or vanilla, I decided to go with bbPress from the makers of Wordpress.

This is the first of a 3 part tutorial on how to integrate bbPress with CakePHP 1.2.x.  When complete, these 3 tutorials will accomplish:

  1. Integrating bbPress in a sub-folder (not a subdomain) of your cake install
  2. Integrating user registration - when a user registers through your cake application they are automatically registered in bbPress. (UPDATE: part 2 here)
  3. Integrating the login - when a user is logged in via the cake application they are automatically logged in in bbPress.

Part 1 - Integrating bbPress in a sub-folder (not a subdomain)

It may be personal preference, but I’m not a big fan of subdomaining parts of an application.  There are definitely legitamite technical reasons to do so, but when I can get away with it, I try to use subfolders, i.e. http://statforge.com and http://statforge.com/forum/.

There’s a small hurdle here since CakePHP wants to ReWrite all your paths (if you’ve installed it in your domain root), but it’s easy to overcome.  Find the .htaccess file in the root of your cake install.  It should look like this:

  1.   RewriteEngine on
  2.    RewriteRule    ^$ app/webroot/    [L]
  3.    RewriteRule    (.*) app/webroot/$1 [L]

This is rewriting all of your requests to the webroot folder. Assuming you want to install bbPress into a subfolder called forum (i.e. http://statforge.com/forum/) the change this .htaccess file to the following:

  1.   RewriteEngine on
  2.  
  3.    RewriteCond %{REQUEST_URI} ^/forum/(.*)$
  4.    RewriteRule ^.*$ - [L]
  5.  
  6.    RewriteRule    ^$ app/webroot/    [L]
  7.    RewriteRule    (.*) app/webroot/$1 [L]

This simply reads - if the Request starts with /forum/ pass it through normally. If not, let Cake handle it. The only limitation here is that you cannot have a controller named ‘forum’ in your cake application.

That’s it - you should be able to drop a fresh bbPress install into the /forum subfolder and access it normally without Cake interfering.

I’ve already integrated the user registration between my CakePHP install and my bbPress install, but I’ve gotta run for the time being. Hopefully tomorrow I’ll throw up part 2 - it’s a bit more complicated. (UPDATE: part 2 here)

Google Reader Shared Items in WP RSS Widget

I just added a default RSS widget to one of my sidebars and noticed that the links weren’t functioning correctly. The source of the RSS feed is my google reader shared items and I think the format in which google sends the information is different than a normal RSS feed? I don’t know enough about them to be sure. All I know is that for some reason the link to the articles and the link to the sources of the articles are being concatenated together in the widget links.

Here’s a small 2-line fix I came up with if you’re familiar with php and the wordpress framework (version 2.5+).

In your {wp home}/wp-includes/widgets.php find the function wp_widget_rss_output(…). The first foreach() loop in the function should be foreach ($rss->items as $item ) { … Directly after the open curly brace, place these two lines of code:

$parts = explode(’http’,$item['link']);
$item['link'] = ‘http’.$parts[1];

Voila - that fixed the problem for me. All it does is explode the link string by the delimeter ‘http’ and rebuild the link using the first part. It’s not a very extensible solution though, so I would only use it if you just want to use a google reader shared items feed for the source of an RSS widget.

,

Passion Projects