By using the API created in part 1, integration with CakePHP or any other site becomes really easy.
As said in part 1, we want functionality to add posts on the site and some user management.
For this I already had 2 models, ‘News’ and ‘Member’, these models do everything related to user management and news posts on the site itself.
What we’ll do here is add a couple of methods for the SMF integration into the models.
First, put the SMF API file in the vendors directory(app/vendors/smf_api.php).
The two models posted here will have to be modified to fit your application.
Members
class Member extends AppModel { /** * Log in to ams with the username and password */ function loginToSMFWithPassword($pUserName, $pPassword, $pExpiryTime = null, $pIsEncrypted = false) { if(is_null($pExpiryTime)) { $pExpiryTime = 0; } App::Import('Vendor', 'SmfApi'); if(smf_setLoginCookie($pExpiryTime, $pUserName, $pPassword, $pIsEncrypted)) { if(smf_authenticateUser()) { smf_logOnline(); } if(smf_isOnline($pUserName)) { global $smf_user_info; return $smf_user_info; } } return false; } /* * Log in to SMF without the password */ function loginToSMF($pUserName, $pExpiryTime = null) { if(is_null($pExpiryTime)) { $pExpiryTime = 0; } App::Import('Vendor', 'SmfApi'); //first get password $data = smf_getUserPassword($pUserName); $passwd = sha1($data['passwd'].$data['salt']); //now log on with the encrypted password return $this->loginToSMFWithPassword($pUserName, $passwd, $pExpiryTime, true); } function userExistsInSMF($pUserName) { App::Import('Vendor', 'SmfApi'); return smf_userExists($this->data['Member']['username']); } function registerInSMF($pUserName, $pEmail, $pPassword) { App::Import('Vendor', 'SmfApi'); return smf_registerMember($pUserName, $pEmail, $pPassword); } function logoutInSMF($pUserName){ App::Import('Vendor', 'SmfApi'); smf_loadSession(); global $smf_user_info; smf_destroyCookie(); smf_sessionDestroy($smf_user_info['session_id']); return; } function changePasswordInSMF($pUserName, $pNewPassword) { App::Import('Vendor', 'SmfApi'); smf_changePassword($pUserName, $pNewPassword); $this->loginToSMFWithPassword($pUserName, $pNewPassword); return; } }
As you can see, it’s not much work using the API.
After importing the vendor(SMF API), call the function and that’s it most of the time.
Now that we can log in on the forum using the site, it’s time to post something.
News Model
class News extends AppModel { public $belongsTo = 'Member'; public function GetActiveNewsItems($pNewsCount = 10, $pLineCount = 0, $pGetMessageCount = false) { $this->recursive = 2; $news = $this->find('all', array('order' => 'News.added DESC', 'limit' => $pNewsCount)); if($pLineCount > 0) { $currentcount = 0; $newsitems = array(); for($i=0;$i<count($news); $i++) { if($pGetMessageCount) { $news[$i]['News']['comment_count'] = ($this->getTopicMessageCountInSMF($news[$i]['News']['topic_id'])-1); } $lines = explode("\n", $news[$i]['News']['text']); $currentcount.= count($lines)-1; $newsitems[] = $news[$i]; if($currentcount > $pLineCount) { unset($news); break; } } } else { $newsitems = $news; } return $newsitems; } public function addNewsItemInSMF($pSMFUserInfo, $pTopic, $pMessage) { App::Import('Vendor', 'SmfApi'); $topic_nr = smf_postNewsTopic($pSMFUserInfo, $pTopic, $pMessage, '1'); return $topic_nr; } public function editNewsItemInSMF($pSMFUserInfo, $pTopicID, $pTopic, $pMessage) { App::Import('Vendor', 'SmfApi'); return smf_editNewsTopic($pSMFUserInfo, $pTopicID, $pTopic, $pMessage); } public function getTopicMessageCountInSMF($pTopicID) { App::Import('Vendor', 'SmfApi'); return smf_getTopicMessageCount($pTopicID); } }
Conclusion
Using the SMF API class this way is really easy.
If only it implemented more. most of the time you’re on your own.
So when you want something not in the API, go look it up how they handle it in the SMF code and do the same in the API.
Also, the methods I added do not take into account authorization, so be sure to watch out what you do!