Contact Me or check out the Producerism Blog

smf2amfphp

03/29/2009


I have been bouncing between wordpress, joomla and expression engine for the past couple years trying to figure out a decent way to integrate a streaming audio player into my forums, without altering the database, or intruding on SMF at all.

Anyways, long story short, I ditched all the “bridges” and just made a non-intrusive package (well, started to make it at least) that feeds data into flash using a series of php functions, that connect directly to the smf database.

I’ve put together a practical example of using this class here:

HipHopProduction.com Beat Meet Player

It will load a given topic (in this case, each drop-down item is associated with a topic that has lots of replies with attachments), search that topic for mp3 attachments, and add them to the list. Then by clicking on any of the names in the list, that user’s mp3 will begin to stream.

The functions that I’ve got working so far are getNews, getMembers, getMemberGroups, getPollEntries, getPoll, getBoards, getBoardCategories, getBoardModerators, getMemberInfo, validateMember, and getTopicAttachments.

Read on for the source code and a functional example.

amfphp service file:

// +----------------------------------------------------------------------+
// | Filname: smf2amfphp.php                                           |
// +----------------------------------------------------------------------+
// | Copyright (c) http://www.producerism.com                              |
// +----------------------------------------------------------------------+
class smf2amfphp
{
    function smf2amfphp()
    {
        $conn = mysql_pconnect("localhost","username","password");
        mysql_select_db("smf_database");
    }

    /** getPoll
    * This method returns poll information
    * @param      pollID
    */
    function getPoll($pollID)
    {
        $query = "SELECT * FROM smf_polls WHERE ID_POLL = $pollID";
        $result = mysql_query($query);

        if(!mysql_num_rows($result)){
            return "Poll ID: ".$pollID." not found!";
        }else{
            $record = mysql_fetch_array($result);
            return $record;
        }

    } // end func

    /** getPollEntries
    * This method returns poll entries
    * @param      pollID
    */
    function getPollEntries($pollID)
    {
        $query = "SELECT * FROM smf_poll_choices WHERE ID_POLL = $pollID";
        $result = mysql_query($query);

        if(!mysql_num_rows($result)) {
        return "Poll ID: ".$pollID." not found!";
        }else{
        $tmp = array();
        while($record = mysql_fetch_array($result)){
        array_push($tmp,$record);
        }
        return $tmp;
        }
    } // end func

    /** getTopicAttachments
    * This method returns attachment filenames within a topic
    * @param      topicID
    */
    function getTopicAttachments($topicID)
    {
        $query = "SELECT att.filename, msg.posterName FROM smf_messages msg, smf_attachments att WHERE msg.ID_MSG = att.ID_MSG AND (msg.ID_BOARD = 8 OR msg.ID_BOARD = 11) AND msg.ID_TOPIC = $topicID";
        $result = mysql_query($query);

        if(!mysql_num_rows($result)) {
            return false;
        }else{
            $tmp = array();
            while($record = mysql_fetch_array($result)){
                array_push($tmp,$record);
            }
            return $tmp;
        }
    } // end func

    /** getMemberInfo
    * This method returns member information
    * @param      memberID
    */
    function getMemberInfo($memberID)
    {
        $query = "SELECT * FROM smf_members WHERE ID_MEMBER = $memberID";
        $result = mysql_query($query);

        if(!mysql_num_rows($result)) {
            return "Member ID: ".$memberID." not found!";
        }else{
            $record = mysql_fetch_array($result);
            return $record;
        }
    } // end func

    /** getMemberInfo
    * This method returns poll information
    * @param      memberName    - smf member name
    * @param      passwd      - smf password (this should be a HASH value, unless sha1 is true)
    * @param      sha1         - set to true if passing the actual password (NOT RECOMMENENDED!)
    */
    function validateMember($memberName, $passwd, $sha1=false)
    {
        if($sha1) $passwd = sha1($memberName.$passwd);

        $query = "SELECT * FROM smf_members WHERE memberName = '$memberName' AND passwd = '$passwd'";
        $result = mysql_query($query);

        if(!mysql_num_rows($result)) {
            return false;
        }else{
            $record = mysql_fetch_array($result);
            return $record;
        }
    } // end func
} // end class

flash as3 function that connects to amfphp, and calls one of the functions listed above:

function onLoginClick(evt)
{
    var memberName, passwd;

    memberName = StringUtil.replace(login_txt.text, "\r", "");
    passwd = SHA1.hash(StringUtil.replace(login_txt.text + password_txt.text, "\r", ""));

    _amfphpConnection = new amfphp(GATEWAY_URL, "smf2amfphp.validateMember", new Array(memberName, passwd), { onResponse:onLogin } );
}

as you can see by the SHA1.hash function, I am using the corelib encryption functions by Adobe in my flash file, so the password never leaves the client.

in keeping with everything else smf, i decided to simplify the class API i’m working on. For example, this is now the code for validating a login: (assuming there are 3 objects: login_txt, password_txt and the login button being clicked that triggers onLoginClick)

function onLoginClick(evt)
{
    smf2amfphp.validateMember(login_txt.text, login_txt.text+password_txt.text, onLogin);
}

function onLogin(evt)
{
    if (evt)
    {
        // login successful!
    }
}

Leave a Reply