News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Tip: script to automate posting in 1.1

Started by pctwo, September 15, 2006, 01:00:47 PM

Previous topic - Next topic

pctwo

I searched for a way to automate posting to 1.1 and found some old answers and some hints for how to do it.  Here's a bare minimum working script that demonstrates how to automate posting.  Notes

1) it's  meant to be run from the web.  Running it from the shell gives some errors, but it seems to work nevertheless.  It uses very simple security checking (must be admin to run).

2) it doesn't handle polls or attachments.

3) it does do notification.and appears to keep unread/read stats correctly.

4) it comes with a simple routine to turn htmlized msg to smf compatible one.

Hopefully, this will give you a place a start.  Adjust to suit your needs.  If you figure out how to get it to run under shell w/o error, pls let me know :)


<?phpinclude_once('/aboslute-local-path-to-ssi-php/SSI.php');require_once($sourcedir . '/Post.php');require_once($sourcedir . '/Subs-Post.php'); // comment out below line to run in shellif (!isset($context['user']) || !$context['user']['is_admin']) die ('Unknown error 744 has occurred');$topic = 0;  // set to 0 for new topic, set to a topic id to reply$board = 2; // set to valid board id$ID_MEMBER = 5;$_POST['subject'] = 'you\'re okay';  // the notify function uses this$msgSubject = addslashes($_POST['subject']);$msgBody =  '[quote author=anon]<br>You can use bbc code here<p>This script assumes you have html-ized body which it will convert to smf compatible format[/quote]<p>it\'s all good';$msgBody = prepBody( $msgBody);$msgOptions = array( 'id' => 0, // set id to modify 'subject' => $msgSubject, 'body' => $msgBody, 'icon' => 'xx', // look at smf_message_icons table to see all possible values 'smileys_enabled' => 1, 'attachments' => empty($attachIDs) ? array() : $attachIDs, );$topicOptions = array( 'id' => empty($topic) ? 0 : $topic, 'board' => $board, 'poll' => null, 'lock_mode' => 0, 'sticky_mode' => null, 'mark_as_read' => true, ); $posterOptions = array( 'id' => $ID_MEMBER, 'name' => 'some name', 'email' => '[email protected]', 'update_post_count' => 1 ); $newTopic = empty($topic) || $topic == 0; createPost($msgOptions, $topicOptions, $posterOptions); if (isset($topicOptions['id'])) $topic = $topicOptions['id']; if ($newTopic) notifyMembersBoard(); else sendNotifications($topic, 'reply'); redirectexit('topic=' . $topic); // take an htmlized $txt and turn it into a smf compatible msg// no cr lf, no html tags, <br> turned into <br /> and <p>  made into <br /><br />function prepBody( $txt){ // cr lf to spaces $txt = str_replace( "\r", ' ', $txt); $txt = str_replace( "\n", ' ', $txt); // reduce blank spaces $txt = preg_replace( '/[ ][ ]+/', ' ', $txt); // turn <br> or <br /> into \n $txt = preg_replace( '!\s*<br(\s*/)?>\s*!i', "\n", $txt);

// turn <p ...> and <p> into \n\n
$txt = preg_replace( '/\s*<p([^>]+|)>\s*/i', "\n\n", $txt);

// mercilessly strip all other html
$txt = preg_replace( '/<[^>]+>/', '', $txt);
$txt = preg_replace( '!</[a-z]+>!i', '', $txt);

// turn \n back to <br />
$txt = str_replace( "\n", '<br />', trim( $txt));

return addslashes($txt);
}

?>




kangkenyot

would you like to let me know the complete script if on my form include file attachment (foto)  ???

pctwo

sorry I did not work out how to do attachment or poll.

addendum: I did not say so in my original post, but obviously you need to set $ID_MEMBER to a valid member's ID.  You can use 0 for a guest if guests are allowed to post.

a2h_

The end of the script turns black... there is a problem in your scripting. It appears to be the ?>, which is the last bit of blue.
With an underscore. Because the one without an underscore's taken.

pctwo


a2h_

Quote // turn <br> or <br /> into \n
$txt = preg_replace( '!\s*<br(\s*/)?>\s*!i', "\n", $txt);

// turn <p ...> and <p> into \n\n
$txt = preg_replace( '/\s*<p([^>]+|)>\s*/i', "\n\n", $txt);

// mercilessly strip all other html
$txt = preg_replace( '/<[^>]+>/', '', $txt);
$txt = preg_replace( '!</[a-z]+>!i', '', $txt);

// turn \n back to <br />
$txt = str_replace( "\n", '<br />', trim( $txt));

return addslashes($txt);
}

?>
With an underscore. Because the one without an underscore's taken.

pctwo

my regex threw the syntax highlighter off.  However, I ran it through php and it found no error.  I tested the script.  It works.

motumbo

I wanted the ability to do automated posts (I have informational posts stickied at the tops of forum categories and I didn't want to do it by hand) but couldn't find a way to do it.

So what I did is wrote an external PHP script to take the data I already had in a MySQL database and format it into INSERT queries that I imported into my SMF forum using phpMyAdmin.  That allowed me to "automate" my posts.


tattooedpierre

Can anyone help me extend this script? I'm looking to either; use a similair method to create a posting from a form (completed by an already logged in user), or to create a posting for a user but not submit .. ie: pre-filled fields but not actually posted. I know there is a mod kicking around to do this, but I hoped it could be done via SSI, similair to what happens here.

floppy01

Hi!

Many thanks for you input regarding how to create a posting functionality. But there is still one question left for me. Ive tried to implement it almost the same way you did, but when I try to submit the form, i get 1 error saying:

Notice: notifyMembersBoard(): Can't send a notification without a board id! in ../smf/Sources/Post.php on line 1896

But the board id is stated :/ as shown by you.

Could anyone help me to solve this? As I really need the notification feature.

Many thanks in advance!

floppy

twistedsymphony

WOW!!!

Thank you, I've been looking for exactly this functionality for some time now.

I have 1 question and 1 comment.

The comment I have is about your HMTLized clean up... if the user is admin you can use [ html ][ /html ] tags on either end of the content and it will render your HTML for you...  but this only works for admins (anytime) or if you allow HTML in your forums. AFAIK it's a newer feature of SMF.

I would think you'd rather use this because it allows me to use things like blockquotes iframes and object tags. since this script is posting from an HTML source one would assume you'd trust that same source to not to inject malicious code in it :) Not to mention it's all inclusive instead of trying to script the conversion of all kinds of HTML tags....

I can't wait to try this out and see if it works with the rest of my integration... this is the  last piece I needed in my puzzle.

The question I have is I was wondering if you could use the SMF API instead of SSI.php... the only reason I ask this is because the application I'm integrating with has some function names that are the same as SMF (the first one it crashes on is is_admin()) and it spews error messages whenever I try to include it... while I haven't tried the SMF API yet I've seen others use it with the same system as me to some success.

Thanks again!

yort124

This might be a stupid question...but where do I put this at?

twistedsymphony

@floppy01... I found a solution to your problem... I tested this script as-is and had some great success... then I added a bunch of code of my own and tried running it again only to hit the same errors you were... I was scratching my head but then looking at the source code for post.php I realized $board is a global variable.

you can't change the name of the variable $board... it's a bit screwy but you must leave that variable name as is... just to be on the save side I'd leave the other variables like $ID_MEMBER the same as well... set them to whatever you want just don't change them.

@yort124
you put this where ever you want to put the intent of this code is to auto-magically create a new topic in SMF from some other application running on the same server.

So for instance my use is I'm linking this up to my WordPress front end. I've created a WordPress plugin using this code so when I create a new topic in WordPress it will set the variables at the top for my message, subject, user, and board and then run the rest of the code to create a matching topic in SMF.

There are alot of other complex things going on in my code but that's the gist of it.

if you copy paste this code into a .php file on the same server... make sure the path to ssi.php is correct and then call the page in a browser it will create a new topic in board #2 called "you're ok"

it's really quite nifty.

floppy01

@twistedsymphony

Hi! Thank you for you comment. But I am not sure wether i got you right. What do you mean by  leaving the name of the variable $board the same. I didn't change it. I used almost the same code as stated but when it comes to the notifymembersboard() function an error occurs. Could you please explain to me how I could correctly call this function or what you have done differently then me.
You can read what i've done in this thread: http://www.simplemachines.org/community/index.php?topic=148074.0
Thank you very much in advance!
floppy

rockinaway

Have I helped you? Then please join AdminFuel - The forum for forum administrators.

If you need help managing your forum, or maybe launching it, then visit us and view the quality articles, downloads and posts.

Zeshin

Hey guys, I know this topic hasn't been posted in for a while, but it kind of pertains to what I need help with. I've followed all the advice given in this topic and several other topics and I'm still getting the same error.

This is sort of what I'm trying to put together: I have a Medieval Fantasy based RP Forum. It's somewhat like DnD. I'm trying to make a character sheet page and when the user is completely finished filling out their character sheet, it can be posted as a new topic into a board and is then reviewed. Anyway, I'm stuck on that part - the posting. I'm trying to do this all from Profile.php

Here's my code:

require_once($sourcedir . '/Subs-Post.php');
require_once($sourcedir . '/Post.php');

$topic = 0;
$subject = $_POST['fullname'];
$username = $context['user']['name'];
$board = 34;

$msgSubject = addslashes($subject); //the title is our topic

//here we prepare the message itself by adding the download link
$msgBody = $body;

$msgBody = prepBody($msgBody); //get rid of html tags in favor of smf tags

$msgOptions = array(
'id' => 0, // set id to modify
'subject' => $msgSubject,
'body' => $msgBody,
'icon' => 'xx', // look at smf_message_icons table to see all possible values
'smileys_enabled' => 1,
'attachments' => empty($attachIDs) ? array() : $attachIDs,
);

$topicOptions = array(
'id' => empty($topic) ? 0 : $topic,
'board' => $board,
'poll' => null,
'lock_mode' => 0,
'sticky_mode' => null,
'mark_as_read' => true,
);

$posterOptions = array(
'id' => $ID_MEMBER,
'name' => 'some name',
'email' => '[email protected]',
'update_post_count' => 1
);

$newTopic = empty($topic) || $topic == 0;

createPost($msgOptions, $topicOptions, $posterOptions);


And here is the syntax error that I'm getting:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SUBSTRING('This is the subject!', 1, 255), SUBSTRING('Human', 1, 65534), SUBSTR' at line 4
File: /home/hiddenre/public_html/development/Sources/Subs-Post.php
Line: 1535


Any help would be much appreciated. I'm not too great at PHP myself, just really getting tired of being stuck on this for a couple days now. >.<

elfishtroll

QuoteYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' SUBSTRING('This is the subject!', 1, 255), SUBSTRING('Human', 1, 65534), SUBSTR' at line 4
File: /home/hiddenre/public_html/development/Sources/Subs-Post.php
Line: 1535



You seem to have a post SUBJECT
Quote$topic = 0;
$subject = $_POST['fullname'];
$username = $context['user']['name'];

...but I'm not sure where you get the $msgbody from. If you havent initialized it somewhere then it will be null and may be the source of an error when you try to execute a query with it.

If your msgbody is being initialized properly (within code you did not include in your post) then perhaps you did not escape the data properly?

rockinaway

I took this script and enhanced it to work very well at http://www.wickettowicket.com/

Take a look, it works for both topics and posts...
Have I helped you? Then please join AdminFuel - The forum for forum administrators.

If you need help managing your forum, or maybe launching it, then visit us and view the quality articles, downloads and posts.

elfishtroll

Quote from: rockinaway on August 14, 2007, 01:11:51 AM
I took this script and enhanced it to work very well at http://www.whogivesaratsass.com/

Take a look, it works for both topics and posts...

^ did you really?

wow.

husmen73 (Gulhin)

#19
its working but I want to work as like form. Such us someone write yourselft on any form mail section. while creating new post, must be write his name on automatic creating post. (like form mail)

I have did a form, its posting to this code page but member name showing as Guest
-Kurumsal Kimlik Web Tasarım Hizmeti.
Gülhin Portal Sistemi hakkında yorumlarınızı bekliyorum.
SMF
Referanslar --> http://www.gulhin.com/referanslar-portfoy/
-Web Tasarımı ve Tema Tasarımı Sitesi www.gulhin.com
-TOPLU MAİL GÖNDERİMİ --> http://www.toplumailsms.com
E-Ticaret Sistemi yaptırmak mı istiyorsunuz?

Advertisement: