Is it possible to post to forum from own website outside smf forum

Started by james78, May 13, 2010, 12:39:48 PM

Previous topic - Next topic

james78

Hi I was searching around for an answer but did'nt seem to get the answer.
Is it possible to have say a form on my main website with a few text fields and additional info box, which has a submit button at the bottom of the form.
When the user clicks submit it will create a new topic in a paticular board on our forum.
The text field at top of form would be used as the topic title.

At present we use rc3 2.0 forum and include ssi into our website pages to retrieve data from forum.

any example code would be greatly appreciated
many thanks in advance


james78

hi followed the link but it gives an error
Function not found or not part of the current version in middle of page with a back text link below.

I see it was going to the functions page is this a simple task i am asking or will i need a degree in php to pull it off.
any working scripts are greatly welcome

many thanks in advance

Kays

Hi, the function database hasn't been updated for the latest version yet. So just select a lower version number.

That's the createPost() function which requires three sets of parameters.


$msgOptions = array(
'id' => 0,
'subject' => $subject,
'body' => $body,
'icon' => 'xx',
'time' => time(),
'smileys_enabled' => true,
);
$topicOptions = array(
'id' => 0,
'board' => $board, // Id # for the board the topic is going into
'mark_as_read' => true,
);
$posterOptions = array(
'id' => $id_member,
'name' => add_slashes($context['username']),
'email' => $context['user']['email'],
'ip' => $user_info['['ip'],
'update_post_count' => empty($id_member) ? false : true,
);

// create the new topic in the appropriate board.
include_once($sourcedir . '/Subs-Post.php');
createPost($msgOptions, $topicOptions, $posterOptions);


You do need to build a form. And all data should be sanitised before going to the database.

$_POST['interger'] = (int) $_POST['interger'];
$_POST['string'] = htmlspecialchars(stripslashes($_POST['string']), ENT_QUOTES);


If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

Arantor

Unfortunately sanitising for createPost is actually surprisingly difficult because multiple things need to happen, not least a strtr on the subject, and a round trip to preparsecode() on the body content - at a minimum.

Then it gets more complex if WYSIWYG was enabled too.

More details please.

james78

hi I know how to create a html form for a normal website, but unsure how it changes for a php one,
The idea is for a site which is a gaming site and i wanted to have the form on main website outside smf forum which has check boxes and text fields for extra info to select game types and a text field at top which would be used as topic title on forum.
I looked on forum and under posts and topics section the box which says disable WYSIWYG editor is unchecked so i believe it is enabled.
How can i proceed with the script above , I really do appreciate the help
any extra info needed i will try n find out

many thanks in advance

Arantor

It might help if you start by listing what items are going to go in this form for the user to fill in and what type of information that is.

james78

<form id="cmatch_form" name="cmatch_form" action="send_mail.php" method="post">
<table width="530" border="0" cellpadding="5">
  <tr>
    <td><strong>General Details</strong></td>
    <td align="right">&nbsp;</td>
    </tr>
  <tr>
    <td>Title of topic: (eg. Your clan vs Our Clan)</td>
    <td align="right"><input type="text" name="post_title" id="post_title" /></td>
    </tr>
  <tr>
    <td>Gamer Tag:</td>
    <td align="right"><input type="text" name="cgtag" id="cgtag" /></td>
    </tr>
  <tr>
    <td>Clan Name:</td>
    <td align="right"><input type="text" name="cname" id="cname" /></td>
    </tr>
  <tr>
    <td>Clan Website:</td>
    <td align="right">http://<input type="text" name="cweb" id="cweb" /></td>
    </tr>
  <tr>
    <td>Date Of Proposed Can Match:<br />
(dd/mm/yyyy)</td>
    <td align="right"><input type="text" name="cdate" id="cdate" /></td>
    </tr>
  <tr>
    <td>Time Of Proposed Clan Match</td>
    <td align="right"><input type="text" name="ctime" id="ctime" />
    <select name="Am or Pm" size="1" id="ampm">
    <option value="AM/PM">AM/PM</option>
    <option value="AM">AM</option>
    <option value="PM">PM</option>
  </select></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right">&nbsp;</td>
    </tr>
  <tr>
    <td><strong>Clan Match Details</strong></td>
    <td align="right">&nbsp;</td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right">&nbsp;</td>
    </tr>
  <tr>
    <td>Game Type: (Hardcore/Noral, etc.)</td>
    <td align="right"><input type="text" name="ctype" id="ctype" /></td>
  </tr>
  <tr>
    <td>Game Mode: (S&amp;D, Headquarters, etc.)</td>
    <td align="right"><input type="text" name="cmode" id="cmode" /></td>
    </tr>
  <tr>
    <td>Team Size: (6vs6, 5vs5, etc)</td>
    <td align="right"><input type="text" name="csize" id="csize" /></td>
    </tr>
  <tr>
    <td>Exclusions</td>
    <td align="right"><input type="text" name="cexcl" id="cexcl" /></td>
    </tr>
  <tr>
    <td>Additional Info</td>
    <td align="right"><textarea name="cadd_info" id="cadd_info" cols="30" rows="5"></textarea></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right">&nbsp;</td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td align="right"><input type="submit" value="Submit challenge" id="submit_post">
</td>
    </tr>
</table>
</form>


This is the form basis I am planning on using , a few text fields and a larger text area, once filled out when user clicks submit I would like it to post to forim into a certain board on forum with the Title of topic being used as the new topic title, and it appearing just like a normal post, with users info on left side etc like normal and info from textfields in main message, I hope that made sense please bear with me.
The user posting this would alreadt be registered on forum and we include ssi in top of page if that helps at all.
Many thanks in advance, your help is really appreciated.


james78

The title of the first text field will be the new topic, which will be hopefully posted into a board which would already be on forum called challenges.
The person who starts the topic will be whoever fills out this form as they would already be registered on forum, or be a guest.
I hope that was the answer

many thanks in advance

Arantor

Still not sure how the other fields are supposed to tie up with going into a post.

Also with guest posting, should they be required to fill in a CAPTCHA?

james78

there was a captcha random image script at end of form but took it off as wasnt sure if was needed when showing form structure.
I thought that the text fields apart from the first could be gathered together when form is submitted so they would be submitted together to make a message body.
if there was only a subject field and main message text area like a normal post on forum could that be posted to a forum board.

really do appreciate everyones input and help, ideas seem so much easier than reality
many thanks in advance

Kays

Since it's only available to registered members, CAPTCHA shouldn't be necessary. Do get the member info from the SMF member data ($context['user'] or $user_info) rather than from the form. Also pass a session id and check it.

If at first you don't succeed, use a bigger hammer. If that fails, read the manual.
My Mods

james78

hi , thankyou for all your inputs and advice but to be honest i am getting lost as is the form able to be posted to a board as a new topic or can only a form with a subject and message body be posted.
when u say check user using ($context['user']  do you mean like this   if ($context['user']['is_logged']) or similar

like i said i am lost sorry
could anyone provide a working script which would post even only 2 textfields or something from our main site so i can better understand how things are writtten in script

many thanks in advance

Arantor

Unfortunately such a script is really not that simple. You tell us precisely what you want it to do, since it seems to have changed over time, and we'll help.

The backend code goes something like this (note this doesn't do any error checking or anything, not even session checking, and simply handles posting it to the forum)

This assumes:
1. You don't care about errors, session checks, that the post button is posted more than once or anything fancy like that.
2. That the subject is something like <input type="text" name="subject" />
3. That the main body is <textarea name="message"></textarea>

<?php
require('SSI.php');
require(
$sourcedir '/Subs-Post.php');

// 0. If the user isn't logged in, boot them.
if (empty($context['user']['id']))
die('Sorry, you must be logged in.');

// 1. Get the subject. Sanitise it.
$_POST['subject'] = empty($_POST['subject']) ? 'No subject' $_POST['subject'];
$_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => ''"\n" => ''"\t" => ''));

// 2. Get the main message. I don't know if you're using the bbc editor or not, presuming you are/want to at some stage.
if (!empty($_REQUEST['message_mode']) && isset($_REQUEST['message']))
{
// If we came from WYSIWYG, we need to convert from HTML to bbc, then unhtml it; then push to $_POST for everything else
$_REQUEST['message'] = un_htmlspecialchars(html_to_bbc($_REQUEST['message']));
$_POST['message'] = $_REQUEST['message'];
}

if (!isset(
$_POST['message']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message']), ENT_QUOTES) === '')
$_POST['message'] = 'No message'// this prevents errors in a minute

// 3. Set up the arrays we need for createPost()
$board 1// I don't actually know what board this should be.

$msgOptions = array(
'id' => 0,
'subject' => $_POST['subject'],
'body' => $_POST['body'],
'icon' => 'xx',
'smileys_enabled' => true,
);
$topicOptions = array(
'id' => 0,
'board' => $board// Id # for the board the topic is going into
'mark_as_read' => true,
);
$posterOptions = array(
'id' => $user_info['id'], // everything else can be set from this.
'ip' => $user_info['ip'],
'update_post_count' => true,
);

createPost($msgOptions$topicOptions$posterOptions);

// 4. Tell the user we're done.
template_header();

echo 
'This has been posted.';

template_footer();
?>


That's really the simplest it can possibly be on the backend side. To add security measures and sanity checks it requires making the form on the other side PHP based, from SSI.php in the first place. It's doable but it soon mounts up (and is why Post.php is a couple of thousand lines of code)

james78

Hi Arantor, thankyou for the quick response.
I am sorry if things have changed since 1st post, this was not ment, I was trying to cut things down to a bare minimum as thought what i first asked was well quite impossible to do after reading the first few replies.

The form I posted which is only html form i know i understand i will have to make it into a php form, correct me if i am wrong.

To clear things up.

I would still like to gather all of the textfield data from the form  10 text fields, 1 textarea

Title of topic.       This field i would like to be the topic title on forum (subject)

and the text that is entered into each textfield the ones listed below

Gamer Tag:
Clan name
clan website
Date Of Proposed Can Match:
Time Of Proposed Clan Match
Game Type
Game Mode
Team Size
Exclusions
Additional Info

so when it appears on forum it would appear as, please see pic attachment

If you can help code this for me i would be forever gratefull
i am also off to learn about php forms but if u could include this in script that would be great

many thanks in advance

Arantor

Pfft, very little is impossible. There's just impractical and infeasible - then there's everything else :)

Most of the problem is that I don't think you were overly clear in what you wanted, and I don't think we followed that clearly as a result :(

The only problem is the size of code is going to grow, hugely, with that. Doubly so if you want to have the ability to actually check for mistakes and tell the user about it.

I know it is possible to do, in fact we even have a page not a million miles away on SimpleDesk - http://www.simpledesk.net/development/security/ is a form that reposts to a private board. The entire code for that page is 353 lines, 250 of which is just figuring out what the user posted, whether it's correct or not, then setting it up to be reposted.

I'll see about doing this tomorrow but I really can't promise I'll get to look at it because it is a complex task to achieve, to get it all right.

(FWIW, the single most complex part of SimpleDesk, the mod we publish on simpledesk.net, is the file called SimpleDesk-Post.php, which handles posting of tickets and replies, because it really is that complicated to do to get it right, taking in to account dealing with errors)

james78

thankyou Arantor
just so things stay complety clear just added another picture to say that the text on right which are the textfield names don't have to appear in the post that is posted from main website just the entered text.
see pic below.
also checking data entered etc just needs to be letters and numbers allowed in fields apart from web address box which would allow a / and a . so address shows correctly.
If that cuts down on security checks at all just trying to help  :)
again i cannot thankyou enough for taking your time to help
many thanks in advance

james78

Hi I tried the script that was provided above just to see how it worked etc and it all posts to forum etc.
but when the message is echoed out to say that it was succesful I get an error but it still posts to forum , so just wondered if someone could point out what was causing the error.
the error

Warning: Invalid argument supplied for foreach() in /home/******/public_html/***/forum/Sources/Subs-Post.php on line 2992
This has been posted.

this is the code I post to from the form which is the same as above but changed this line from

'body' => $_POST['body'],
to
'body' => $_POST['message'],

as it kept saying body didnt exist

this is the code i post to to create forum topic from form on my main website


<?php
require('SSI.php');
require(
$sourcedir '/Subs-Post.php');

// 0. If the user isn't logged in, boot them.
if (empty($context['user']['id']))
die('Sorry, you must be logged in.');

// 1. Get the subject. Sanitise it.
$_POST['subject'] = empty($_POST['subject']) ? 'No subject' $_POST['subject'];
$_POST['subject'] = strtr($smcFunc['htmlspecialchars']($_POST['subject']), array("\r" => ''"\n" => ''"\t" => ''));

// 2. Get the main message. I don't know if you're using the bbc editor or not, presuming you are/want to at some stage.
if (!empty($_REQUEST['message_mode']) && isset($_REQUEST['message']))
{
// If we came from WYSIWYG, we need to convert from HTML to bbc, then unhtml it; then push to $_POST for everything else
$_REQUEST['message'] = un_htmlspecialchars(html_to_bbc($_REQUEST['message']));
$_POST['message'] = $_REQUEST['message'];
}

if (!isset(
$_POST['message']) || $smcFunc['htmltrim']($smcFunc['htmlspecialchars']($_POST['message']), ENT_QUOTES) === '')
$_POST['message'] = 'No message'// this prevents errors in a minute

// 3. Set up the arrays we need for createPost()
$board 2// I don't actually know what board this should be.

$msgOptions = array(
'id' => 0,
'subject' => $_POST['subject'],
'body' => $_POST['message'],
'icon' => 'xx',
'smileys_enabled' => true,
);
$topicOptions = array(
'id' => 0,
'board' => $board// Id # for the board the topic is going into
'mark_as_read' => true,
);
$posterOptions = array(
'id' => $user_info['id'], // everything else can be set from this.
'ip' => $user_info['ip'],
'update_post_count' => true,
);

createPost($msgOptions$topicOptions$posterOptions);

// 4. Tell the user we're done.


echo 'This has been posted.';


?>


am i missing something or complety off track.

Also I am very grateful to you Arantor if u do find the time to write the script for the original request i understand you do this off your own back just grateful there is such a great community where we can all ask for help, even if it does make me feel silly  :)

james78

could anyone help with the above error

many thanks in advance

Advertisement: