News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Create a thread via script for automation

Started by HelAu007, May 11, 2022, 06:57:35 AM

Previous topic - Next topic

HelAu007

Hello
We are using our forum for tournaments.
For each tournament we have to create one Thread for each game.
To automate this I would like to have something like:
https://myurl.com/forum/createThread.php?title=My-Title&contents=My-contents [nofollow]
Is there anything similar available or can you give me some hints how to realize that ?
It must be done without login in
Regards
Helmut

Doug Heffernan

You must use the createPost() function to create automated posts. You can look it up at the Subs-Post.php file.

Dhayzon


  public function postMessages($data){
        global $scripturl, $context,$modSettings, $settings,$sourcedir,$smcFunc;
        $this->boardExist();
        $title = $data['subject'];
        $body = $data['body'];
        $board = $data['id_board'];

        $random_users = !empty($modSettings['quick_post_default_member']) ? explode(',',$modSettings['quick_post_default_member']) : [];
        $user = $random_users[array_rand($random_users)];
       
        $title = $smcFunc['htmlspecialchars']($title);
        $body = $body; 
        $board = (int)$board;
        $user = (int)$user;
     
        if(!in_array($board, $this->boards))
         return false;
               
        $title = preg_replace("/ {2,}/", " ", trim($title));
        $body = preg_replace("/ {2,}/", " ",trim($body));

       
        if(empty($title) || empty($body) || empty( $board ) )
            return false;

            require_once( $sourcedir.'/Subs-Post.php');
         
            $msgOptions = array(
            'subject' =>   substr($title, 0, 100),
            'body' => $body,
            );
            $topicOptions = array(
            'board' => (int)$board,// $boardid, // this is ID of board where message gets posted
            'mark_as_read' => true,
            );
            $posterOptions = array(
                    'id' => !empty($user) ? $user:0,
                    'name' => 'Anon',
                    'email' =>'[email protected]',
                    'update_post_count' => !empty($user) ? true: false,
                    );
           
           
            createPost($msgOptions, $topicOptions, $posterOptions); 
            $posts = array(
                        'board' => array(
                                'href' => $scripturl.'?board='.(int)$board,
                            ),
                        'topic' => $topicOptions,
                        'msg' => $msgOptions,
                        'poster' => $posterOptions['name'],
                        'href' => $scripturl . '?topic=' . $topicOptions['id'] . '',
            );
           
            return  $posts;
   }

nishuFun

Quote from: Dhayzon on May 12, 2022, 11:03:12 PM
  public function postMessages($data){
       ...
}


i am bit new to smf, may u please specify  where i should place this function?  in which file? and is there any way existing option to avoid misuse by others? :D
smf 2.1.2

Kira_

а нас
за що?

nishuFun

Quote from: Kira_ on May 15, 2022, 01:17:59 AM1. https://wiki.simplemachines.org/smf/External_page
It's enough only require_once('/path/to/SSI.php'); if you don't need an interface
2. https://support.simplemachines.org/function_db/index.php?action=view_function;id=323
(require Subs-Post.php)


thanks a lot, the script by Dhayzon working out of the box :)

honestly i didnt even know this wiki :D

now i m ready to struggle with attachments,
i might need u later :)

till then, lemme experiment :)
smf 2.1.2

nishuFun

how to escape the strings and insert below body?

Quoteमेरा 👉🏻Attitude तो मेरी #निशानी है,
    ➡️ 𝐃𝐨𝐧'𝐭 𝐒𝐡𝐨𝐰 𝐌𝐞 𝐘𝐨𝐮𝐫 𝐀𝐭𝐭𝐢𝐭𝐮𝐝𝐞 !!
    💯#𝑨𝒕𝒕𝒊𝒕𝒖𝒅𝒆𝑨𝒑𝒑 ✓

QuoteDatabase Error: Incorrect string value: '\xF0\x9F\x91\x89\xF0\x9F...' for column `smf`.`smf_messages`.`body` at row 1


i mean, in general how i prepare body before passing it?
smf 2.1.2

Kira_

global $smcFunc;
$var = $smcFunc['db_escape_string']($var);
а нас
за що?

nishuFun

Quote from: Kira_ on May 15, 2022, 02:05:48 AMglobal $smcFunc;
$var = $smcFunc['db_escape_string']($var);

i tried yours, but it is leaving \n\r .
so working function for body is

$body = $smcFunc['htmlspecialchars']($body, ENT_QUOTES);
but it is not working for title,
guide me😭
smf 2.1.2

Kira_

smf_db_escape_string can't flags.

Can be preliminary str_replace?

More about smcFunc.
https://wiki.simplemachines.org/smf/$smcFunc
а нас
за що?

nishuFun

$smcFunc['htmlspecialchars']($body, ENT_QUOTES)
vs
$var = $smcFunc['db_escape_string']($var);
which do u think is better? why?
as far i seen, $smcFunc['htmlspecialchars'] is used majorly where $smcFunc['db_escape_string'] for dbs purpose


for now, i m sticking to $smcFunc['htmlspecialchars'], cause it is working out of the box.
smf 2.1.2

Kira_

#11
I'm sorry, I read the question inattentively :)

These two functions are used for completely different purposes.

Htmlspecialchars converts HTML special characters into entities so that they can be output without problems. escape_string escapes sensitive SQL characters, so dynamic queries can be executed without the risk of SQL injection.

You might as well say that htmlspecialchars handles OUTPUT, and escape_string handles input.
а нас
за що?

Arantor

SMF's htmlspecialchars does more than that, you should use it in this situation.

You should also be sure to call preparsecode() from Subs-Post.PHP on the body of a post before inserting it.

You also should almost *never* be calling db_escape_string manually, it does not do what you think.

nishuFun

thanks you  to both..

i will try to play with preparsecode() later

i love this community ❤️
smf 2.1.2

Advertisement: