Sep
28
2008

smf2amfphp

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.

Please overlook the hardcoded ID_BOARD values in the topic attachment query, as this is for a custom purpose, although I will most likely be making this more flexible.

amfphp service file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?
// +----------------------------------------------------------------------+
// | 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:

1
2
3
4
5
6
7
8
9
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)

1
2
3
4
5
6
7
8
9
10
11
12
function onLoginClick(evt)
{
    smf2amfphp.validateMember(login_txt.text, login_txt.text+password_txt.text, onLogin);
}

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

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.

Here’s a complete list of functions that I’ve got working so far:

getNews, getMembers, getMemberGroups, getPollEntries, getPoll, getBoards, getBoardCategories, getBoardModerators, getMemberInfo, validateMember, getTopicAttachments


2 Comments »

  • handoyo

    Hi..Nice work there.I found out your site from google.Do you have an example of the working list of functions?Thanks…

    Comment | December 11, 2009
  • admin

    I’ll put together a new updated example and post it soon.

    Comment | February 26, 2010

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress Theme based on TBM.