Example #1
0
function execute($request)
{
    if ($request['user_id'] == 0) {
        return build_response_forbidden('You must be <a href="/login">logged in</a> to post.');
    }
    $category_key = $request['path_parts'][1];
    $category_info = api_forum_get_category_info($request['user_id'], $request['is_admin'], $category_key, true);
    if ($category_info['ERROR']) {
        return build_response_not_found('Forum category not found.');
    }
    $path_parts = $request['path_parts'];
    if ($path_parts[3] == 'reply') {
        $type = 'reply';
        $thread_id = intval($path_parts[2]);
        $thread_info = api_forum_get_thread_info($request['user_id'], $request['is_admin'], $thread_id);
        if ($thread_info['is_locked']) {
            return build_response_forbidden("Cannot post in a locked thread.");
        }
    } else {
        if ($path_parts[2] == 'post') {
            $type = 'create';
        } else {
            throw new Exception("Invalid post type.");
        }
    }
    $thread_title = '';
    $post_body = '';
    $error_message = null;
    if ($request['method'] == "POST") {
        $thread_title = trim($request['form']['thread_title']);
        $post_body = trim($request['form']['post_body']);
        if ($type == 'create') {
            $result = api_forum_create_post($request['user_id'], $request['is_admin'], $category_info['category_id'], $thread_title, 0, null, $post_body);
        } else {
            if ($type == 'reply') {
                $result = api_forum_create_post($request['user_id'], $request['is_admin'], $category_info['category_id'], '', $thread_id, null, $post_body);
            } else {
                throw new Exception("Invalid post type.");
            }
        }
        if ($result['OK']) {
            return build_response_moved_temporarily('/forum/' . $category_key . '/' . $result['thread_id'] . '/new');
        } else {
            switch ($result['message']) {
                case 'BLANK_POST':
                    $error_message = "Post cannot be blank.";
                    break;
                case 'THREAD_TITLE_BLANK':
                    $error_message = "Thread title cannot be blank.";
                    break;
                default:
                    $error_message = "Server returned error: " . $result['message'];
                    break;
            }
        }
    }
    $html = array($error_message != null ? '<div style="color:#f00;">' . htmlspecialchars($error_message) . '</div>' : '', '<form action="/' . implode('/', $request['path_parts']) . '" method="post">', '<div>', $thread_id == 0 ? 'Title: <input type="text" name="thread_title" value="' . htmlspecialchars($thread_title) . '" />' : '', '</div>', '<div>', '<textarea name="post_body" rows="12" style="width:900px">' . htmlspecialchars($post_body) . '</textarea>', '</div>', '<div>', '<input type="submit" name="submit" value="Be Nice" />', '</div>', '</form>');
    return build_response_ok("New Post", implode("\n", $html));
}
Example #2
0
function execute($request)
{
    $path_parts = $request['path_parts'];
    $category_key = $path_parts[1];
    $thread_id = intval($path_parts[2]);
    $page_id = 0;
    // returns null for admin-only threads if not an admin
    $thread_info = api_forum_get_thread_info($request['user_id'], $request['is_admin'], $thread_id, true);
    if ($thread_info == null || $thread_info['category_info']['key'] != $category_key) {
        return build_response_not_found("Thread not found.");
    }
    $total_posts = $thread_info['post_count'];
    if (count($path_parts) > 3 && substr($path_parts[3], 0, strlen('page')) == 'page') {
        $page_id = intval(substr($path_parts[3], strlen('page'))) - 1;
        if ($page_id < 0) {
            $page_id = 0;
        }
    } else {
        if ($path_parts[3] == 'new') {
            // TODO: per-user new post tracking
        }
    }
    $current_page = $page_id + 1;
    $total_pages = intval(($total_posts - 1) / 25) + 1;
    // List of integers including 1-indexed page numbers or -1 for ellipses.
    // Links to first 3 pages and last 3 pages should always be available, along with pages within 2 of the current page.
    $paginator_links = array();
    $first_range = 3;
    $end_range = $total_pages - 2;
    $mid_begin_range = $current_page - 2;
    $mid_end_range = $current_page + 2;
    $last_item_is_ellipsis = false;
    for ($i = 1; $i <= $total_pages; ++$i) {
        if ($i <= $first_range || $i >= $end_range || $i >= $mid_begin_range && $i <= $mid_end_range) {
            array_push($paginator_links, $i);
            $last_item_is_ellipses = false;
        } else {
            if (!$last_item_is_ellipses) {
                array_push($paginator_links, -1);
                $last_item_is_ellipses = true;
            }
        }
    }
    $starting_post_index = $page_id * 25;
    if ($starting_post_index >= $total_posts) {
    }
    $forum_posts = api_forum_fetch_posts_for_thread($request['user_id'], $category_key, $thread_id, $page_id);
    $post_ids = $forum_posts['ordered_post_ids'];
    $thread_info = $forum_posts['thread_' . $thread_id];
    $category_info = $forum_posts['category_' . $thread_info['category_id']];
    if ($category_info == null) {
        return build_response_not_found("Thread not found.");
    }
    $output = array();
    array_push($output, '<h1 style="font-size:16px;">', '<a href="/forum">Forum</a> &gt; ', '<a href="/forum/' . $category_key . '">' . htmlspecialchars($category_info['name']) . '</a> &gt; ', htmlspecialchars($thread_info['title']), '</h1>');
    $paginator_html = array();
    if ($total_pages > 1) {
        array_push($paginator_html, '<div style="text-align:right;">');
        if ($current_page > 1) {
            array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . ($current_page - 1) . '">Prev</a> ');
        }
        foreach ($paginator_links as $page) {
            if ($page === -1) {
                array_push($paginator_html, ' ... ');
            } else {
                if ($page == $current_page) {
                    array_push($paginator_html, '[' . $current_page . ']');
                } else {
                    array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . $page . '">' . $page . '</a>');
                }
            }
        }
        if ($current_page < $total_pages) {
            array_push($paginator_html, '<a href="/forum/' . $category_key . '/' . $thread_id . '/page' . ($current_page + 1) . '">Next</a> ');
        }
        array_push($paginator_html, '</div>');
    }
    $paginator_html = implode("\n", $paginator_html);
    array_push($output, $paginator_html);
    if (count($post_ids) == 0) {
        return build_response_not_found("No posts found.");
    }
    array_push($output, '</div>');
    //array_push($output, '<div style="margin-bottom:20px;">');
    foreach ($post_ids as $post_id) {
        $post = $forum_posts['post_' . $post_id];
        $user = $forum_posts['user_' . $post['user_id']];
        array_push($output, '<div style="clear:both; padding-top:20px;">', '<div class="block" style="float:left; width:120px; margin-right:20px;">', '<div style="text-align:center;">', strlen($user['image_id']) > 0 ? '<img src="/uploads/avatars/' . $user['image_id'] . '" />' : ":'(", '</div>', '</div>', '<div style="float:left; width:780px; background-color:#fff;">', '<div style="background-color:#ddd;font-weight:bold; padding:8px; font-size:12px;">', '<div title="' . date("M j, Y g:i:s A", $post['time']) . '" style="float:right;width:300px;text-align:right;font-weight:normal;color:#555;">', unix_to_scaling_time($post['time']), '</div>', '<a href="/profiles/' . $user['login_id'] . '">' . htmlspecialchars($user['name']) . '</a>', '</div>', '<div style="clear:right;padding:20px;">', nl2br(htmlspecialchars($post['content_raw'])), '</div>', '</div>', '</div>');
    }
    //array_push($output, '</div>');
    array_push($output, '<div class="fullblock" style="clear:both;margin-top:20px;">');
    array_push($output, $paginator_html);
    array_push($output, '<div><a href="/forum/' . $category_key . '/' . $thread_id . '/reply">Reply</a></div>');
    return build_response_ok("Forum thread", implode("\n", $output));
}
Example #3
0
function api_forum_create_post($user_id, $is_admin, $category_id, $thread_title_if_new, $thread_id, $message_user_ids, $content)
{
    if ($user_id == 0) {
        return api_error("NOT_LOGGED_IN");
    }
    $content = trim($content);
    if (strlen($content) == 0) {
        return api_error("BLANK_POST");
    }
    $thread_info = null;
    $post = null;
    if ($message_user_ids != null) {
        array_push($message_user_ids, $user_id);
        $message_user_ids = sort_and_remove_duplicates($message_user_ids);
        if (count($message_user_ids) < 2) {
            return api_error("NO_TARGET");
        }
        // TODO: check to see if you've been blocked by a user
        // message a user
        $thread_info = api_forum_get_user_message_thread($message_user_ids);
        if ($thread_info == null) {
            // create a new thread between/among these users
            $post = api_forum_create_post_impl($user_id, 0, 0, $content);
            if ($post['ERROR']) {
                return $post;
            }
        } else {
            // append to existing thread
            $post = api_forum_create_post_impl($user_id, 0, $thread_info['thread_id'], $content);
        }
    } else {
        if ($category_id > 0) {
            // if the user is not allowed to post in a category, that will be reflected in the error message of create_post_impl
            if ($thread_id == 0) {
                // new thread
                $thread_title = trim($thread_title_if_new);
                if (strlen($thread_title) == 0) {
                    return api_error("THREAD_TITLE_BLANK");
                }
                $post = api_forum_create_post_impl($user_id, $is_admin, $category_id, 0, $content);
            } else {
                // reply to thread
                $thread_info = api_forum_get_thread_info($user_id, $is_admin, $thread_id, false);
                $post = api_forum_create_post_impl($user_id, $is_admin, $category_id, $thread_id, $content);
            }
        } else {
            if ($category_id == 0) {
                return api_error("NO_CATEGORY_DEFINED");
            }
        }
    }
    if ($post == null) {
        return api_error('POST_FAILED');
    }
    if ($post['ERROR']) {
        return $post;
    }
    if ($post['thread_id'] == 0) {
        if ($thread_info != null) {
            return api_error('THREAD_STATE_INCONSISTENCY');
        }
        if ($message_user_ids != null) {
            $thread_info = api_forum_create_new_thread(0, '', $post['post_id'], 1);
            if ($thread_info['ERROR']) {
                api_forum_delete_posts(array($post['post_id']));
                return $thread_info;
            }
            sql_insert('forum_message_user_to_thread_id', array('user_id_cluster' => api_forum_encode_user_id_cluster($message_user_ids), 'thread_id' => $thread_info['thread_id']));
        } else {
            $thread_info = api_forum_create_new_thread($user_id, $is_admin, $category_id, $thread_title_if_new, $post['post_id'], 1);
            if ($thread_info['ERROR']) {
                api_forum_delete_posts(array($post['post_id']));
                return $thread_info;
            }
        }
        sql_query("UPDATE `forum_posts` SET `thread_id` = " . $thread_info['thread_id'] . " WHERE `post_id` = " . $post['post_id'] . " LIMIT 1");
    }
    return api_success(array('thread_id' => $thread_info['thread_id'], 'post_id' => $post['post_id'], 'category_id' => $thread_info['category_id']));
}