PHPBB3 中的自动发帖 +操纵/恢复用户会话

发布于 2024-08-04 10:11:58 字数 2760 浏览 2 评论 0原文

我遇到了一个问题,目前还没有找到完美的解决方案。首先让我概述一下场景。 我在服务器上有一个 PHPBB3 论坛,并且在同一台服务器上还有一个链接到该论坛的应用程序(由我开发)。仅当您登录论坛时才能访问该应用程序,并且仅当您属于特定论坛用户组时才能访问该应用程序的某些部分。 此外,当用户访问脚本/页面时,应用程序需要能够通过专用用户(我们称之为“机器人”)以编程方式发布论坛主题/帖子。 所以我需要的是: 1. 保存当前登录用户(我们称他为“测试用户”) 2. 使用用户“Bot”登录 3. 发布主题/帖子。 4. 销毁会话 5. 恢复登录用户

这种“某种程度”一开始是有效的,但是当重复调用/发帖时,前 1-2 个帖子将由“机器人”发布,但接下来的帖子将由“测试用户”发布。 当前代码:

位于 setup.php 中,它包含在所有相关脚本中。

    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './forum/';
 $phpEx = substr(strrchr(__FILE__, '.'), 1);
 // The common.php file is required.
 include($phpbb_root_path . 'common.' . $phpEx);
 // this is required for auto posting
 include($phpbb_root_path . 'config.' . $phpEx);
 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
 include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

这就是函数本身,在另一个脚本中,

require_once 'setup.php';
function create_forum_post($subject, $text, $forumid, $posting_userid, $topic_id=NULL) {

 if(!PHPBB_SESSION_INTEGRATION) return false;

 global $user, $auth;

 $username = BOT_USERNAME;
 $password = BOT_PASSWORD;

 $title = unhtmlentities( $subject );
 $text = unhtmlentities( $text );

 $forumid = $forumid;
 $topicid = $topic_id;

 $original_user_id = $user->data['user_id'];
 $user->session_begin();
 $login = $auth->login($username, $password, false);
 $auth->acl($user->data);
 $user->setup();

 $title = utf8_normalize_nfc($title);
 $text = utf8_normalize_nfc($text);

 $poll = $uid = $bitfield = $options = '';

 generate_text_for_storage($title, $uid, $bitfield, $options, false, false, false);
 generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);

 $data = array(
     'forum_id'      => $forumid, 
     'topic_id'      => $topicid,
     'icon_id'      => false,
     'post_approved' => true,

     'enable_bbcode'   => true,
     'enable_smilies'   => true,
     'enable_urls'      => true,
     'enable_sig'      => true,

     'message'      => $text,
     'message_md5'   => md5($text),

     'bbcode_bitfield'   => $bitfield,
     'bbcode_uid'      => $uid,

     'post_edit_locked'   => 0,
     'topic_title'      => $title,
     'notify_set'      => false,
     'notify'         => false,
     'post_time'       => 0,
     'forum_name'      => '',
     'enable_indexing'   => true,
 );

 if ($topicid == NULL)
    $post_url =  submit_post('post', $title, '', POST_NORMAL, $poll, $data);
 else
    $post_url = submit_post('reply', $title, '', POST_NORMAL, $poll, $data);

 $user->session_kill();
 $user->session_create($original_user_id, false, true);

 return $post_url;

}

我希望有任何有用的提示或替代方法。

I ran into an issue that I haven't found a perfect solution for yet. First let me outline the scenario.
I have a PHPBB3 forum on a server and also an application (developed by me) on the same server which is linked to the forum. The application is only accessible if you are logged in to the forum and also some parts of it are only accessible if you are in specific forum user groups.
Also the app needs to be able to post forum threads/posts programatically through a dedicated user (let's call it 'Bot'), when the user accesses a script/page.
So what I need is:
1. Save current logged in user (let's call him 'Test User')
2. Log in with user 'Bot'
3. Post a thread/post.
4. Destroy session
5. Restore logged in user

This 'sort of' works at first, but when called/posting repeatedly, the first 1-2 posts will be posted by 'Bot' but the following will be posted by 'Test User'.
Current code:

This is in setup.php, which is included in all relevant scripts.

    $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './forum/';
 $phpEx = substr(strrchr(__FILE__, '.'), 1);
 // The common.php file is required.
 include($phpbb_root_path . 'common.' . $phpEx);
 // this is required for auto posting
 include($phpbb_root_path . 'config.' . $phpEx);
 include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
 include($phpbb_root_path . 'includes/message_parser.' . $phpEx);

And this is the function itself, in another script

require_once 'setup.php';
function create_forum_post($subject, $text, $forumid, $posting_userid, $topic_id=NULL) {

 if(!PHPBB_SESSION_INTEGRATION) return false;

 global $user, $auth;

 $username = BOT_USERNAME;
 $password = BOT_PASSWORD;

 $title = unhtmlentities( $subject );
 $text = unhtmlentities( $text );

 $forumid = $forumid;
 $topicid = $topic_id;

 $original_user_id = $user->data['user_id'];
 $user->session_begin();
 $login = $auth->login($username, $password, false);
 $auth->acl($user->data);
 $user->setup();

 $title = utf8_normalize_nfc($title);
 $text = utf8_normalize_nfc($text);

 $poll = $uid = $bitfield = $options = '';

 generate_text_for_storage($title, $uid, $bitfield, $options, false, false, false);
 generate_text_for_storage($text, $uid, $bitfield, $options, true, true, true);

 $data = array(
     'forum_id'      => $forumid, 
     'topic_id'      => $topicid,
     'icon_id'      => false,
     'post_approved' => true,

     'enable_bbcode'   => true,
     'enable_smilies'   => true,
     'enable_urls'      => true,
     'enable_sig'      => true,

     'message'      => $text,
     'message_md5'   => md5($text),

     'bbcode_bitfield'   => $bitfield,
     'bbcode_uid'      => $uid,

     'post_edit_locked'   => 0,
     'topic_title'      => $title,
     'notify_set'      => false,
     'notify'         => false,
     'post_time'       => 0,
     'forum_name'      => '',
     'enable_indexing'   => true,
 );

 if ($topicid == NULL)
    $post_url =  submit_post('post', $title, '', POST_NORMAL, $poll, $data);
 else
    $post_url = submit_post('reply', $title, '', POST_NORMAL, $poll, $data);

 $user->session_kill();
 $user->session_create($original_user_id, false, true);

 return $post_url;

}

I'd appreciate any helpful tips or alternative methods.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅紫色的梦幻 2024-08-11 10:11:58

我建议你不要理会会议等。

如果您使用 CURL 启动“机器人”活动,那么它将在幕后进行交互,而不是在当前用户的上下文中进行交互。
例如,所发帖子的 IP 地址将是您的服务器的 IP 地址,而不是用户的 IP 地址。

I would suggest you leave sessions,etc alone.

If you initiate the 'bot' activity with CURL, then it would be interacting behind the scenes and not within the context of the current user.
The IP address of the posts made, for example, would be that of your server, not that of the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文