TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-24-2008, 10:15 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Basic PM Class

Here's a basic PM Class that I whopped up today.

php Code:
<?php

class PrivateMessage
{
   
    /**
     * Private Message class, a simple PM System
     * By Tanax
     *
     * @var $aDbArgs, $iUser, $aUserPMs
     */

    private $aDbArgs = array();
    private $iUser;
    private $aUserPMs = array();
   
    /**
     * Function __construct
     * Sets the database table and column names
     *
     * @param array $aArgs
     * @return true or false
     */

    public function __construct(array $aArgs)
    {
       
        if(is_array($aArgs))
        {
           
            $this->aDbArgs = $aArgs;
            return true;
           
        }
       
        else return false;
       
    }

    /**
     * Loads the user based on the userid. Gets all the PM's for that user.
     * Uses the functions loadPMInbox and loadPMOutbox.
     *
     * @param integer $iUser
     * @return true or false
     */

    public function loadUser($iUser)
    {
       
        if(is_numeric($iUser))
        {
           
            $this->iUser = $iUser;
            $this->loadPMInbox();
            $this->loadPMOutbox();
            return true;
           
        }
       
        else return false;
       
    }
   
    /**
     * Loads the inbox
     *
     * @return true or false
     */

    private function loadPMInbox()
    {
       
        if(isset($this->iUserId))
        {
           
            $szSql = sprintf("  SELECT
                                    `%s`, %s`, `%s`, `%s`
                                FROM
                                    `%s`
                                WHERE
                                    `%s` = %d"
,
                               
                                $this->aDbArgs['col_pmID'],
                                $this->aDbArgs['col_fromUser'],
                                $this->aDbArgs['col_pmTitle'],
                                $this->aDbArgs['col_pmDate'],
                                $this->aDbArgs['tab_pms'],
                                $this->aDbArgs['col_toUser'],
                                $this->iUserId);
                               
            $bQuery = mysql_query($szSql) or die(mysql_error());
           
            $mdaResults = mysql_fetch_array($bQuery);
           
            if($mdaResults)
            {
               
                empty($this->aUserPMs['user_inbox']);
                $this->aUserPMs['user_inbox'] = $mdaResults;
                return true;
               
            }
           
            else return false;
           
        }
       
        else return false;
       
    }
   
    /**
     * Loads the outbox
     *
     * @return true or false
     */

    private function loadPMOutbox()
    {
       
        if(isset($this->iUserId))
        {
           
            $szSql = sprintf("  SELECT
                                    `%s`, `%s`, `%s`, `%s`
                                FROM
                                    `%s`
                                WHERE
                                    `%s` = %d"
,
           
                                $this->aDbArgs['col_pmID'],
                                $this->aDbArgs['col_toUser'],
                                $this->aDbArgs['col_pmTitle'],
                                $this->aDbArgs['col_pmDate'],
                                $this->aDbArgs['tab_pms'],
                                $this->aDbArgs['col_fromUser'],
                                $this->iUserId);
                               
            $bQuery = mysql_query($szSql) or die(mysql_error());
           
            $mdaResults = mysql_fetch_array($bQuery);
           
            if($mdaResults)
            {
               
                empty($this->aUserPMs['user_outbox']);
                $this->aUserPMs['user_outbox'] = $mdaResults;
                return true;
               
            }
           
            else return false;
           
        }
       
        else return false;
       
    }
   
    /**
     * Gets the inbox
     *
     * @return array $aUserPMs['user_inbox']
     */

    public function getPMInbox()
    {
       
        if(isset($this->aUserPMs['user_inbox']))
        {
           
            return $this->aUserPMs['user_inbox'];
           
        }
       
        else return false;
       
    }

    /**
     * Gets the outbox
     *
     * @return array $aUserPMs['user_outbox']
     */

    public function getPMOutbox()
    {
       
        if(isset($this->aUserPMs['user_outbox']))
        {
           
            return $this->aUserPMs['user_outbox'];
           
        }
       
        else return false;
       
    }

    /**
     * Sends a PM
     *
     * @param array $aArgs
     * @return true or false
     */

    public function sendPM(array $aArgs)
    {
       
        if(is_array($aArgs))
        {
           
            $szSql = sprintf("  INSERT INTO
                                    `%s`
                                SET
                                    `%s` = '%s',
                                    `%s` = '%s',
                                    `%s` = '%s',
                                    `%s` = '%s',
                                    `%s` = NOW()"
,
                               
                                $this->aDbArgs['tab_pms'],
                                $this->aDbArgs['col_toUser'],
                                mysql_real_escape_string($aArgs['to_user']),
                                $this->aDbArgs['col_fromUser'],
                                mysql_real_escape_string($aArgs['from_user']),
                                $this->aDbArgs['col_pmTitle'],
                                mysql_real_escape_string($aArgs['pm_title']),
                                $this->aDbArgs['col_pmBody'],
                                mysql_real_escape_string($aArgs['pm_body']),
                                $this->aDbArgs['col_pmDate']);
                               
            $bQuery = mysql_query($szSql) or die(mysql_error());
           
            if($bQuery)
            {
               
                return true;
               
            }
           
            else return false;
                               
        }
       
        else return false;
       
    }

    /**
     * Deletes a PM
     *
     * @param integer $iPmID
     * @return true or false
     */

    public function deletePM($iPmID)
    {
       
        if(is_numeric($iPmID))
        {
           
            $szSql = printf("   DELETE FROM
                                    `%s`
                                WHERE
                                    `%s` = `%d`
                                LIMIT 1"
,
           
                                $this->aDbArgs['tab_pms'],
                                $this->aDbArgs['col_pmID'],
                                $iPmID);
                               
            $bQuery = mysql_query($szSql) or die(mysql_error());
           
            if($bQuery)
            {
               
                return true;
               
            }
           
            else return false;
           
        }
       
        else return false;
       
    }

    /**
     * Gets a specific PM
     *
     * @param integer $iPmID
     * @return array $aResults or false
     */

    public function getPM($iPmID)
    {
       
        if(is_numeric($iPmID))
        {
           
            $szSql = sprintf("  SELECT
                                    *
                                FROM
                                    `%s`
                                WHERE
                                    `%s` = '%s'
                                LIMIT 1"
,
           
                                $this->aDbArgs['tab_pms'],
                                $this->aDbArgs['col_pmID'],
                                $iPmID);
                               
            $bQuery = mysql_query($szSql) or die(mysql_error());
           
            if($bQuery)
            {
               
                $aResults = mysql_fetch_array($bQuery);
                if($aResults)
                {
                   
                    return $aResults;
                   
                }
               
                else return false;
               
            }
           
            else return false;
           
        }
       
        else return false;
       
    }
   
}

?>

Haven't tested it tho, but thought I'd release it!

Here's an example usage. It's not perfect, cause when you view a specific PM, it doesn't know if it's an outgoing PM or an ingoing PM. But anyways, this is just an illustrating example..

php Code:
<?php
   
    // Include the class
    include('PrivateMessage.php');
   
    // The database table, and columns, names
    $config = array(
   
        'tab_pms' => 'privateMessages',
        'col_pmID' => 'pm_id',
        'col_pmTitle' => 'pm_title',
        'col_pmBody' => 'pm_body',
        'col_pmDate' => 'pm_date',
        'col_toUser' => 'to_user',
        'col_fromUser' => 'from_user'
   
    );
   
    /**
     * Instanciate the object based on our configuration
     * Set the userid to 3, and load the user's PMs
     */

    $pm = new PrivateMessage($config)
    $user = 3
    $pm->loadUser($user);
   
    // Get the $_GET values
    $id = $_GET['id'];
    $action = mysql_real_escape_string($_GET['action']);
   
    // If the id is set, and the action isn't set
    if(isset($id) && !isset($action))
    {
           
        // Check if the id is numeric
        if(!is_numeric($id))
        {
               
            die('Do not try to inject my website :(');
               
        }
       
        // Get the message based on the id
        $message = $pm->getPM($id);
       
        // If the message is not false
        if($message)
        {
           
            // Echo out the message details
            echo $message['pm_title'];
            echo '<br />';
               
            echo 'Sent ' . $message['pm_date'] . ' from ' . $message['from_user'];
            echo '<br />';
               
            echo $message['pm_body'];
               
        }
       
        // If the message is false
        else
        {
               
            echo 'Invalid PM id';
               
        }
           
    }
   
    // If the id is not set, the action is set, and the action is set to inbox
    elseif(!isset($id) && isset($action) && $action == 'inbox')
    {
       
        // Get all the pms in the inbox
        $inbox = $pm->getPMInbox();
       
        // If there are any pms
        if($inbox)
        {
           
            // Echo out total amount of messages in inbox
            echo 'Inbox contains ' . count($inbox) . '<br />';
           
            // Foreach the messages and echo out the details
            foreach($inbox as $message)
            {
                   
                echo '<a href="messages.php?id=' . $message['pm_id'] . '">' . $message['pm_title'] . '</a>';
                echo '<br />';
                   
                echo $message['from_user'];
                echo '<br />';
                   
                echo $message['pm_date'];
                   
            }
               
        }
       
        // If there are no pms
        else
        {
               
            echo 'You have no messages in your inbox';
               
        }
           
    }
   
    // If the id is not set, the action is set, and the action is set to outbox
    elseif(!isset($id) && isset($action) && $action == 'outbox')
    {
       
        // Get all the pms in outbox
        $outbox = $pm->getPMOutbox();
       
        // If there are any pms
        if($outbox)
        {
           
            // If there are any pms in outbox
            echo 'Outbox contains ' . count($outbox) . '<br />';
           
            // Foreach the messages and echo out the details
            foreach($outbox as $message)
            {
                   
                echo '<a href="messages.php?id=' . $message['pm_id'] . '">' . $message['pm_title'] . '</a>';
                echo '<br />';
                   
                echo $message['to_user'];
                echo '<br />';
                   
                echo $message['pm_date'];
                   
            }
               
        }
       
        // If there are no pms
        else
        {
               
            echo 'You have no messages in your outbox';
               
        }
           
    }
   
    // If the id is not set, the action is set, and the action is set to sendnew
    elseif(!isset($id) && isset($action) && $action == 'sendnew')
    {
           
        /**
         * Well just write out a form, and use $pm->sendPM($arguments) to send it
         * $arguments is ofcourse an array of the pm details that you get from the form.
         */


    }
   
    // If the id is set, the action is set, and the action is set to deletes
    elseif(isset($id) && isset($action) && $action == 'delete')
    {
           
        /**
         * Well just write out a confirmation, and use $pm->deletePM($id) to delete it
         */


    }
   
    // If it's any other than any of the above options
    else
    {
           
        echo 'Invalid path';
           
    }
   
   
   
   
   
?>

As I said, I haven't tested.. but feel free to test it, and check it through in learning purposes(or if you wanna use it, feel free..don't forget credits tho)
__________________
Tanax is offline  
Reply With Quote
Old 10-28-2008, 09:46 AM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

61 views and not a single comment? :-O
__________________
Tanax is offline  
Reply With Quote
Old 10-28-2008, 09:57 AM   #3 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

nice work ;) keep it up :D
codefreek is offline  
Reply With Quote
Old 10-28-2008, 07:31 PM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Thanks!
Though I noticed something else xD You can view other ppls PM's in this by just changing the id nr in the url :-O
__________________
Tanax is offline  
Reply With Quote
Old 12-02-2008, 02:52 PM   #5 (permalink)
The Wanderer
 
kokjj87's Avatar
 
Join Date: Oct 2008
Posts: 18
Thanks: 0
kokjj87 is on a distinguished road
Default

very neat indeed
kokjj87 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design