示例#1
0
function execute($request)
{
    $category_key = $request['path_parts'][1];
    $category_info = api_forum_get_category_info($request['user_id'], $request['is_admin'], $category_key, false);
    if ($category_info['ERROR']) {
        return not_found_impl($request);
    }
    $page = 0;
    if (substr($request['path_parts'][2], 0, strlen('page')) == 'page') {
        $page = intval(substr($request['path_parts'][2], strlen('page')));
        if ($page < 0) {
            $page = 0;
        }
    }
    $threads = api_forum_get_threads($category_info['category_id'], $page);
    $output = array('<h2>', '<a href="/forum">Forum</a>', ' &gt; ', htmlspecialchars($category_info['name']) . '</h2>', '<div>', '<a href="/forum/' . $category_key . '/post">Create new thread</a>', '</div>', '<div style="background-color:#999;">');
    $thread_ids = $threads['thread_order'];
    if (count($thread_ids) > 0) {
        $i = 0;
        foreach ($thread_ids as $thread_id) {
            $thread_info = $threads['thread_' . $thread_id];
            $last_post_info = $threads['post_' . $thread_info['last_post_id']];
            $last_post_user_info = $threads['user_' . $last_post_info['user_id']];
            $row = implode("", array('<div style="margin:1px; background-color:#' . ($i % 2 == 0 ? 'eee' : 'fff') . '">', '<div style="width:600px; float:left;">', '<a href="/forum/' . $category_key . '/' . $thread_info['thread_id'] . '">', htmlspecialchars($thread_info['title']), '</a>', '</div>', '<div style="width:80px; float:left; text-align:center; font-size:11px;">', '<div>' . ($thread_info['post_count'] - 1) . '</div>', '<div>replies</div>', '</div>', '<div style="width:80px; float:left; text-align:center; font-size:11px;">', '<div>' . $thread_info['view_count'] . '</div>', '<div>views</div>', '</div>', '<div style="width:150px; float:left;">', '<a href="/profiles/' . $last_post_user_info['login_id'] . '">', htmlspecialchars($last_post_user_info['name']), '</a>', '</div>', '<div style="clear:left;"></div>', '</div>'));
            array_push($output, $row);
            ++$i;
        }
    } else {
        array_push($output, '<tr><td>No posts</td></tr>');
    }
    array_push($output, '</div>');
    return build_response_ok("Forum category", implode("\n", $output));
}
示例#2
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));
}
示例#3
0
function api_forum_get_thread_info($user_id, $is_admin, $thread_id, $fetch_category_info_too = false)
{
    $thread_info = api_forum_canonicalize_thread_db_entry(sql_query_item("SELECT * FROM `forum_threads` WHERE `thread_id` = " . intval($thread_id) . " LIMIT 1"));
    if ($thread_info == null) {
        return null;
    }
    if ($fetch_category_info_too) {
        $category_id = $thread_info['category_id'];
        $category_info = api_forum_get_category_info($user_id, $is_admin, $category_id, false);
        if (!$is_admin && $category_info['is_admin_visible']) {
            return null;
        }
        $thread_info['category_info'] = $category_info;
    }
    return $thread_info;
}