Skip to:
Content

bbPress.org

Ticket #1239: loginless_posting.1.1239.diff

File loginless_posting.1.1239.diff, 11.7 KB (added by filosofo, 12 years ago)
  • bb-templates/kakumei/style.css

     
    592592
    593593.postform label { display: block; }
    594594
     595.postform #author,
     596.postform #email,
    595597.postform #topic,
    596 .postform #tags-input {
     598.postform #tags-input,
     599.postform #url {
    597600        margin: 5px 0;
    598601        padding: 5px;
    599602        width: 724px;
     
    10081011
    10091012p.submit input:active {
    10101013        border-color: rgb(0, 100, 0);
    1011 }
    1012  No newline at end of file
     1014}
  • bb-templates/kakumei/post-form.php

     
    55        </label>
    66</p>
    77<?php endif; do_action( 'post_form_pre_post' ); ?>
     8<?php if ( ! bb_is_user_logged_in() && ! bb_is_login_required() )  :
     9        $current_commenter = bb_get_current_commenter();
     10?>
     11        <p id="post-form-author-container">
     12                <label for="author"><?php _e('Author (required)'); ?>
     13                        <input type="text" name="author" id="author" size="50" aria-required="true" value="<?php echo esc_attr($current_commenter['comment_author']); ?>" />
     14                </label>
     15        </p>
     16
     17        <p id="post-form-email-container">
     18                <label for="email"><?php _e('Email (required)'); ?>
     19                        <input type="text" name="email" id="email" size="50" aria-required="true" value="<?php echo esc_attr($current_commenter['comment_author_email']); ?>" />
     20                </label>
     21        </p>
     22
     23        <p id="post-form-url-container">
     24                <label for="url"><?php _e('URL'); ?>
     25                        <input type="text" name="url" id="url" size="50" aria-required="true" value="<?php echo esc_attr($current_commenter['comment_author_url']); ?>" />
     26                </label>
     27
     28        </p>
     29<?php endif; // end user not logged in but logins aren't required ?>
     30
    831<p id="post-form-post-container">
    932        <label for="post_content"><?php _e('Post'); ?>
    1033                <textarea name="post_content" cols="50" rows="8" id="post_content" tabindex="3"></textarea>
  • bb-includes/functions.bb-posts.php

     
    22
    33/* Posts */
    44
     5/**
     6 * Check to make sure that a user is not making too many posts in a short amount of time.
     7 *
     8 * @todo Add logic for users not logged in.
     9 *
     10 * @param string $ip Comment IP.
     11 * @param string $email Comment author email address.
     12 * @param string $date MySQL time string.
     13 */
     14function bb_check_comment_flood( $ip = '', $email = '', $date = '' ) {
     15        global $bbdb;
     16        $user_id = (int) $user_id;
     17        $throttle_time = bb_get_option( 'throttle_time' );
     18
     19        if ( bb_current_user_can('manage_options') || empty( $throttle_time ) ) {
     20                return;
     21        }
     22
     23        $hour_ago = gmdate( 'Y-m-d H:i:s', time() - 3600 );
     24
     25        if ( bb_is_user_logged_in() ) {
     26                $bb_current_user = bb_get_current_user();
     27               
     28                if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && ! bb_current_user_can('throttle') ) {
     29                        if ( defined('DOING_AJAX') && DOING_AJAX ) {
     30                                die(__('Slow down; you move too fast.'));
     31                        } else {
     32                                bb_die(__('Slow down; you move too fast.'));
     33                        }
     34                }
     35        } else {
     36                // todo: add logic for non-logged-in users
     37        }
     38}
     39
     40/**
     41 * Get the current, non-logged-in commenter data.
     42 * @return array The associative array of author, email, and url data.
     43 */
     44function bb_get_current_commenter() {
     45        // Cookies should already be sanitized.
     46        $comment_author = '';
     47        if ( isset($_COOKIE['comment_author_'.COOKIEHASH]) )
     48                $comment_author = $_COOKIE['comment_author_'.COOKIEHASH];
     49
     50        $comment_author_email = '';
     51        if ( isset($_COOKIE['comment_author_email_'.COOKIEHASH]) )
     52                $comment_author_email = $_COOKIE['comment_author_email_'.COOKIEHASH];
     53
     54        $comment_author_url = '';
     55        if ( isset($_COOKIE['comment_author_url_'.COOKIEHASH]) )
     56                $comment_author_url = $_COOKIE['comment_author_url_'.COOKIEHASH];
     57
     58        return compact('comment_author', 'comment_author_email', 'comment_author_url');
     59}
     60
    561function bb_get_post( $post_id ) {
    662        global $bbdb;
    763        $post_id = (int) $post_id;
     
    300356        $defaults['throttle'] = true;
    301357        extract( wp_parse_args( $args, $defaults ) );
    302358
     359        if ( isset( $post_author ) ) {
     360                $post_author = sanitize_user($post_author);
     361        }
     362
     363        if ( isset( $post_email ) ) {
     364                $post_email = sanitize_email($post_email);
     365        }
     366
     367        if ( isset( $post_url ) ) {
     368                $post_url = esc_url($post_url);
     369        }
     370
    303371        if ( !$topic = get_topic( $topic_id ) )
    304372                return false;
    305373
    306         if ( !$user = bb_get_user( $poster_id ) )
     374        if ( bb_is_login_required() && ! $user = bb_get_user( $poster_id ) )
    307375                return false;
    308376
    309377        $topic_id = (int) $topic->topic_id;
     
    330398                $bbdb->insert( $bbdb->posts, compact( $fields ) );
    331399                $post_id = $topic_last_post_id = (int) $bbdb->insert_id;
    332400
     401                // if user not logged in, save user data as meta data
     402                if ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) {
     403                        bb_update_meta($post_id, 'post_author', $post_author, 'post');
     404                        bb_update_meta($post_id, 'post_email', $post_email, 'post');
     405                        bb_update_meta($post_id, 'post_url', $post_url, 'post');
     406                }
     407
    333408                if ( 0 == $post_status ) {
    334409                        $topic_time = $post_time;
    335                         $topic_last_poster = $poster_id;
    336                         $topic_last_poster_name = $user->user_login;
     410                        $topic_last_poster = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? -1 : $poster_id;
     411                        $topic_last_poster_name = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? $post_author : $user->user_login;
    337412
    338413                        $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d;", $topic->forum_id ) );
    339414                        $bbdb->update(
  • bb-includes/functions.bb-template.php

     
    291291        do_action( 'pre_post_form' );
    292292
    293293        if (
     294                ( false === bb_is_login_required() ) ||
    294295                ( bb_is_topic() && bb_current_user_can( 'write_post', $topic->topic_id ) && ( $page == $last_page || !$last_page_only ) ) ||
    295296                ( !bb_is_topic() && bb_current_user_can( 'write_topic', isset( $forum->forum_id ) ? $forum->forum_id : 0 ) )
    296297        ) {
     
    427428        return 'forum-page' == bb_get_location();
    428429}
    429430
     431/**
     432 * Whether a user is required to log in in order to create posts and forums.
     433 * @return bool Whether a user must be logged in.
     434 */
     435function bb_is_login_required() {
     436        return ! (bool) bb_get_option('enable_loginless');
     437}
     438
    430439function bb_is_tags() {
    431440        return 'tag-page' == bb_get_location();
    432441}
     
    17351744                return apply_filters( 'get_post_author', $user->display_name, $user->ID, $post_id );
    17361745        elseif ( $title = bb_get_post_meta( 'pingback_title' ) )
    17371746                return apply_filters( 'bb_get_pingback_title', $title, $post_id );
     1747        elseif ( $title = bb_get_post_meta( 'post_author' ) )
     1748                return apply_filters( 'get_post_author', $title, 0, $post_id );
    17381749        else
    17391750                return apply_filters( 'get_post_author', __('Anonymous'), 0, $post_id );
    17401751}
     
    17441755                echo '<a href="' . esc_attr( $link ) . '">' . get_post_author( $post_id ) . '</a>';
    17451756        } elseif ( $link = bb_get_post_meta( 'pingback_uri' )) {
    17461757                echo '<a href="' . esc_attr( $link ) . '">' . get_post_author( $post_id ) . '</a>';
     1758        } elseif ( $link = bb_get_post_meta( 'post_url' ) ) {
     1759                echo '<a href="' . esc_attr( $link ) . '">' . get_post_author( $post_id ) . '</a>';
    17471760        } else {
    17481761                post_author( $post_id );
    17491762        }
  • bb-includes/functions.bb-meta.php

     
    465465                'wp_admin_cookie_path',
    466466                'wp_plugins_cookie_path',
    467467                'wordpress_mu_primary_blog_id',
     468                'enable_loginless',
    468469                'enable_xmlrpc',
    469470                'enable_pingback',
    470471                'throttle_time',
  • bb-post.php

     
    11<?php
    22require('./bb-load.php');
    33
    4 bb_auth('logged_in');
     4if ( bb_is_login_required() ) {
     5        bb_auth('logged_in');
     6}
    57
    6 if ( $throttle_time = bb_get_option( 'throttle_time' ) )
    7         if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && !bb_current_user_can('throttle') )
    8                 bb_die(__('Slow down; you move too fast.'));
     8bb_check_comment_flood();
    99
    1010if ( !$post_content = trim($_POST['post_content']) )
    1111        bb_die(__('You need to actually submit some content!'));
    1212
     13$post_author = $post_email = $post_url = '';
     14
     15if ( ! bb_is_user_logged_in() ) {
     16        if ( bb_is_login_required() ) {
     17                bb_die(__('You are not allowed to post.  Are you logged in?'));
     18        } else {
     19                if ( ! $post_author = trim($_POST['author']) ) {
     20                        bb_die(__('You need to submit your name!'));
     21                } elseif ( ! $post_email = trim($_POST['email']) ) {
     22                        bb_die(__('You need to submit your email!'));
     23                }
     24
     25                if ( ! empty( $_POST['url'] ) ) {
     26                        $post_url = trim($_POST['url']);
     27                }
     28        }
     29}
     30
     31
     32
     33
    1334if ( isset($_POST['topic']) && $forum_id = (int) $_POST['forum_id'] ) {
    14         if ( !bb_current_user_can('write_posts') )
     35        if ( bb_is_login_required() && ! bb_current_user_can('write_posts') )
    1536                bb_die(__('You are not allowed to post.  Are you logged in?'));
    1637
    17         if ( !bb_current_user_can( 'write_topic', $forum_id ) )
     38        if ( bb_is_login_required() && ! bb_current_user_can( 'write_topic', $forum_id ) )
    1839                bb_die(__('You are not allowed to write new topics.'));
    1940
    2041        bb_check_admin_referer( 'create-topic' );
     
    3253        bb_check_admin_referer( 'create-post_' . $topic_id );
    3354}
    3455
    35 if ( !bb_current_user_can( 'write_post', $topic_id ) )
     56if ( bb_is_login_required() && ! bb_current_user_can( 'write_post', $topic_id ) )
    3657        bb_die(__('You are not allowed to post.  Are you logged in?'));
    3758
    3859if ( !topic_is_open( $topic_id ) )
    3960        bb_die(__('This topic has been closed'));
    4061
    41 $post_id = bb_new_post( $topic_id, $_POST['post_content'] );
     62$post_data = array(
     63        'post_text' => stripslashes($_POST['post_content']),
     64        'topic_id' => $topic_id,
     65);
    4266
     67foreach( array('post_author', 'post_email', 'post_url') as $field ) {
     68        if ( ! empty( $$field ) ) {
     69                $post_data[$field] = $$field;
     70        }
     71}
     72
     73$post_id = bb_insert_post($post_data);
     74
    4375$tags  = trim( $_POST['tags']  );
    4476bb_add_topic_tags( $topic_id, $tags );
    4577
  • bb-admin/options-discussion.php

     
    1010        if (!isset($_POST['enable_pingback'])) {
    1111                $_POST['enable_pingback'] = false;
    1212        }
     13
     14        if (!isset($_POST['enable_loginless'])) {
     15                $_POST['enable_loginless'] = false;
     16        }
    1317       
    1418        // Deal with avatars checkbox when it isn't checked
    1519        if (!isset($_POST['avatars_show'])) {
     
    3842        bb_admin_notice( __( '<strong>Settings saved.</strong>' ) );
    3943}
    4044
    41 $remote_options = array(
     45$general_options = array(
    4246        'enable_pingback' => array(
    4347                'title' => __( 'Enable Pingbacks' ),
    4448                'type' => 'checkbox',
     
    4650                        1 => __( 'Allow link notifications from other sites.' )
    4751                )
    4852        ),
     53
     54        'enable_loginless' => array(
     55                'title' => __( 'Enable Login-less Posting' ),
     56                'type' => 'checkbox',
     57                'options' => array(
     58                        1 => __( 'Allow users to create topics and posts without logging in.' )
     59                ),
     60        ),
    4961);
    5062
    5163$bb_get_option_avatars_show = create_function( '$a', 'return 1;' );
     
    101113<form class="settings" method="post" action="<?php bb_uri( 'bb-admin/options-discussion.php', null, BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>">
    102114        <fieldset>
    103115<?php
    104 foreach ( $remote_options as $option => $args ) {
     116foreach ( $general_options as $option => $args ) {
    105117        bb_option_form_element( $option, $args );
    106118}
    107119?>