Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
Register FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-17-2011, 06:52 AM
Eliasen Eliasen is offline
 
Join Date: Feb 2011
Location: Denmark
Posts: 19
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default How to send a PM from outside vbulletin

-Solved-
EDIT 30/07-2011: Sorry everyone! I completely forgot to show how I solved this issue, so here it is!

I cannot remember where I found this, and I take no credit for this code.

First you need a php file called "pm-script.php" (or whatever you feel like).
Then past this code into this newly created file:
PHP Code:
<?php
function vbulletin_send_pm(
    $site, $sender_username, $sender_password, 
    $recipient, $title = '', $message = '', $iconid = '0'
){
    $cookiefile = tempnam("/tmp", "cookies"); 
    /* create a temporary file to store cookies.
   this should work on most systems and is more
   flexible than specifying path explicitly */
 
    $ch = curl_init();
    //General options
    //curl_setopt($ch, CURLOPT_HEADER, true); //For debugging
    //Masquerade as Firefox
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //Follow redirects
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
    //PHP >= 5.1.0
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
 
    /** *************************************************
             Log In
    ****************************************************/
 
    curl_setopt($ch, CURLOPT_URL, $site.'login.php?do=login');
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_REFERER, $site.'index.php'); 
 
    /* The fields of the login form.*/
    $postfields = array(
        'vb_login_username'    => $sender_username,
        'vb_login_password' => '', //Interestingly enough, this doesn't need to be set.
        //'cookieuser' => '1', //"Remember me"
        's' => '',
        'securitytoken' => 'guest',
        'do' => 'login',
        'vb_login_md5password' => md5($sender_password), //hash the password
        'vb_login_md5password_utf' => md5($sender_password), //UTF? Looks the same to me.
     );
 
    $postfields = http_build_query($postfields, '', '&'); //PHP >= 5.1.2
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
 
    $result = curl_exec ($ch);
    //There will be a redirection link if the login was successful
    if (preg_match('/index.php\?s=(\w{32})/', $result, $matches)){
        $s_key = $matches[1]; //not used 
    } else {
        return false;
    }
 
    /** *************************************************
         Retrieve the PM page to get the security token 
    ****************************************************/
    curl_setopt($ch, CURLOPT_URL, $site.'private.php?do=newpm');
    curl_setopt($ch, CURLOPT_POST, false);
    $html = curl_exec($ch);
    if (preg_match('/<input\s+type="hidden"\s+name="securitytoken"\s+value="([\w\-]+)"\s*\/?>/i', $html, $matches)){
        $security_token = $matches[1];
    } else {
        return false;
    }
 
    /** *************************************************
             Send the PM
    ****************************************************/
    curl_setopt($ch, CURLOPT_URL, $site.'private.php?do=insertpm');
    curl_setopt($ch, CURLOPT_POST, true);
    //curl_setopt($ch, CURLOPT_REFERER, $site.'private.php?do=newpm');
 
    /* PM form fields */
    $postfields = array(
        'recipients' => $recipient,
        'bccrecipients' => '',
        'title' => $title,
        'message' => $message,
        'wysiwyg' => '0',
        'iconid' => $iconid,
        's' => '', //might need to use $s_key here
        'securitytoken' => $security_token,
        'do' => 'insertpm',
        'pmid' => '',
        'forward' => '',
        'button' => 'Submit Message',
        'savecopy' => '0', //save the message in "Sent" folder
        'parseurl' => '1', //automatically parse URLs in the message
     );
 
    $postfields = http_build_query($postfields, '', '&');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
 
    $result = curl_exec ($ch);
    //Note : I don't verify if the message was actually sent.  
    //If everything went well so far it probably was.
 
    /** ****************************************************
       Done. Close connections and kill the cookie-file.
    ********************************************************/
    curl_close ($ch); 
    unlink($cookiefile);
    return true;
}
?>
To send a PM from a non-VB page, include this php file in your non-VB page, and use the following function to send a pm:

PHP Code:
vbulletin_send_pm("http://url-to-your-forum.com/forum/", "from-username", "user-password", "Recipient-user-ID", "PM Title!", "PM Content"); 
Reply With Quote
Reply


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:54 AM.


Powered by vBulletin® Version 3.9.0
Copyright ©2000 - 2017, vBulletin Solutions Inc.