4webby BLOG

01 11 2007

SMAKE: SMF forum - CakePHP integration

by Daniel | hits(3434)

TAGS: cakephp smf integration forum

Today I'll present you a convinient way of integrating the Simple Machine Forum (SMF) into your CakePHP applications.

Well the problem is that the SMF DB table used to store sessions does not follow the CakePHP conventions, or said the other way around, CakePHP doen't understand SMF session table structure.

SMF stores its sessions in a table called forumprefix_sessions. Let's assume in the rest of our article that the forumprefix is smf_ (you can set the forum prefix table while installing SMF). This table is missing a couple of fields that CakPHP needs to write its sessions:

  • the 'id' field (primaryKey)
  • the 'data' field

Right!

CakePHP DB sessions are handled by the Session library (ROOT/cake/libs/Session.php). We don't want to hack the Session library directly, but simply find a convenient way to extend it, so that if we update our CakePHP installation, we won't overwrite the core Session library. Furthermore going the way I'm presenting here, our code will be more tidy, and we won't get crazy on every upgrade.

For this tutorial I'm using:

Let's start then!

1) Put the SMF forum in app/webroot/forums/

2) Set CAKE_SESSION_COOKIE to be the default 'PHPSESSID'.

3) Modify the session handling functions in Cake to use the MySQL table used by SMF (forumprefix_sessions)

4) in my app/bootstrap.php I added this line at the end

require_Once('forum/SSI.php');

NOW WE NEED TO TELL CAKE TO USE SMF session table (here I'll assume that your SMF DB tables prefix is *smf_*

THE SMF sessin table doesn't have an id column, but rather a *session_id* one

5) rename cake/libs/session.php in *session_cake.php*

6) rename the class in your new *session_cake.php* into

class CakeOriginalSession extends Object {  

7) create a file in cake/libs/ called *session_smf.php* (this extends the original Cake Session lib that we moved in *session_cake.php*).
go in the bin and copy and past what you will find in here:
http://bin.cakephp.org/view/1933866502

into the newly created file.

8) create a file in cake/libs/ called *session.php* with the following in it.

require_once('session_smf.php'); class CakeSession extends SmfSession { } 

9) hack the original smf_sessions table as follows:

  • add a column called 'expires' and set it to NULL (only CakePHP writes there)
  • set the `last_update` field to NULL (only SMF writes there)
CREATE TABLE `smf_sessions` (
`session_id` char(32) NOT NULL default '',
`last_update` int(10) unsigned default NULL,
`data` text,
`expires` int(11) default NULL,
PRIMARY KEY (`session_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

10) somewhere in you template write the following

  <?php echo ssi_welcome()?>

11) how can you get the SMF session data then?

try this:

$user = (unserialize($this->Session->read('THE_LOGIN_COOKIE_NAME_YOU_SET_IN_SMF_CONFIG')));
echo $user[0];

This will give you the id of the currently logged in user in SMF.

What's missing yet?
An helper or component that behaves as a wrapper for the functions in SSI.php

Dan

PS: a different approach is presented here: CakePHP & SMF

view/hide comments | add comment

4webby.com

Tags

powered by 4webby.com