Exemplo n.º 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));
}
Exemplo n.º 2
0
function execute($request)
{
    $language_info = api_autograder_get_language_info($request['path_parts'][1]);
    if ($language_info == null) {
        return build_response_not_found("Language not found.");
    }
    $language_key = $language_info['key'];
    $language_id = $language_info['language_id'];
    $problems = api_autograder_menu_get_problems($request['user_id'], 'practice', 0, $language_id);
    debug_print($problems);
    if ($problems['OK']) {
        $output = array('<h1>' . htmlspecialchars($language_info['name']) . ' Practice Problems</h1>');
        if (count($problems['ordered_problem_ids']) == 0) {
            array_push($output, "<div>Empty!</div>");
        }
        foreach ($problems['ordered_problem_ids'] as $problem_id) {
            $problem_info = $problems['problem_' . $problem_id];
            array_push($output, '<div><a href="/practice/' . $language_key . '/' . $problem_id . '">');
            array_push($output, htmlspecialchars($problem_info['title']));
            array_push($output, '</a></div>');
        }
        return build_response_ok($language_info['title'] . " Practice Problems", implode("\n", $output));
    } else {
        if ($problems['message'] == 'INVALID_LANGUAGE') {
            return build_response_not_found("Language not found.");
        }
    }
}
Exemplo n.º 3
0
function execute($request)
{
    if ($request['method'] == 'GET' && $request['path'] == '/autograder/graderpoll') {
        $tokens = api_autograder_get_work_queue();
        return build_response_ok('', 'OK,' . count($tokens) . ',' . implode(',', $tokens));
    }
    if ($request['method'] == 'POST' && $request['path_parts'][1] == 'graderpoll' && count($request['path_parts']) == 3) {
        $action = $request['path_parts'][2];
        $key = strtolower(trim($request['form']['key']));
        $token = trim($request['form']['token']);
        switch ($action) {
            case 'claim':
                $result = api_autograder_claim_by_grader($token, $key);
                if ($result['ERROR']) {
                    return build_response_ok('ERR,' . $result['message']);
                }
                return build_response_ok('', implode(',', array('OK', string_to_hex($result['language']), string_to_hex($result['code']), string_to_hex($result['callback']), string_to_hex($result['expected_function_name']), string_to_hex($result['expected_arg_count']), string_to_hex(!!$result['arg_types'] ? implode('|', $result['arg_types']) : ''), string_to_hex($result['return_type']), string_to_hex($result['test_json']), string_to_hex($result['feature']))));
            case 'setstatus':
                $result = api_autograder_set_status($token, $key, $request['form']['status']);
                if ($result['ERROR']) {
                    return build_response_ok('ERR,' . $result['message']);
                }
                return build_response_ok('', 'OK');
            case 'finish':
                $result = api_autograder_report_conclusion($token, $key, $request['form']['output'], $request['form']['callback']);
                if ($result['ERROR']) {
                    return build_response_ok('ERR,' . $result['message']);
                }
                return build_response_ok('', 'OK');
            default:
                return build_response_ok('', 'ERR,not found');
        }
    }
    return build_response_ok('', 'ERR,not found');
}
Exemplo n.º 4
0
function execute($request)
{
    $username = $request['form']['login_username'] . '';
    $password = $request['form']['login_password'] . '';
    $error = null;
    if (strlen($username) > 0) {
        $result = api_account_create_session($username, $password, 'web', $request['ip'], 14 * 24);
        // two weeks
        if ($result['OK']) {
            $expire = time() + 365 * 24 * 3600;
            setcookie('npclient', 'web', $expire);
            setcookie('nptoken', $result['token'], $expire);
            return build_response_moved_temporarily('/');
        } else {
            switch ($result['message']) {
                case 'WRONG_PASSWORD':
                    $error = "Bad password. Did you forget it?";
                    break;
                default:
                    $error = "Server returned error code: " . $result['message'];
                    break;
            }
        }
    }
    $output = array('<h1>Log in</h1>', $error == null ? '' : nl2br(htmlspecialchars($error)), '<form action="/login" method="post">', 'Username: <input type="text" name="login_username" value="' . htmlspecialchars($username) . '"/><br />', 'Password: <input type="password" name="login_password" /><br />', '<input type="submit" name="submit" value="Login" />', '</form>');
    return build_response_ok("Log In", implode("\n", $output));
}
Exemplo n.º 5
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));
}
Exemplo n.º 6
0
function execute($request)
{
    $problem_id = intval($request['path_parts'][1]);
    $problem = api_autograder_menu_get_problem($request['user_id'], 'golf', 0, $problem_id);
    if ($problem == null) {
        return build_response_not_found("Golf Problem not found.");
    }
    $output = array('<h1>', '<a href="/golf">Code Golf</a> &gt; ', htmlspecialchars($problem['title']), '</h1>', '<div>', nl2br(htmlspecialchars($problem['statement'])), '</div>', api_autograder_generate_client_html('golf', null, '', $problem_id));
    return build_response_ok("Code Golf: " . $problem['title'], implode("\n", $output), array('js' => 'autograder.js', 'onload' => "ag_init('golf')"));
}
Exemplo n.º 7
0
function execute($request)
{
    $output = array('<h1>', "Coding practice problems", '</h1>');
    $languages = sql_query("SELECT * FROM `languages` WHERE `auto_grader_supported` = 1 ORDER BY `name`");
    for ($i = 0; $i < $languages->num_rows; ++$i) {
        $language = $languages->fetch_assoc();
        if ($language['key'] == 'python2x') {
            continue;
        }
        array_push($output, '<div><a href="/practice/' . $language['key'] . '">' . htmlspecialchars($language['name']) . '</a></div>');
    }
    return build_response_ok("Practice Problems", implode("\n", $output));
}
Exemplo n.º 8
0
function execute($request)
{
    if ($request['method'] == 'POST') {
        switch (strtolower(trim($request['form']['action']))) {
            case 'create':
                $feature = strtolower(trim($request['form']['feature']));
                $code = $request['form']['code'];
                $language = $request['form']['language'];
                $problem_id = intval($request['form']['problem_id']);
                switch ($feature) {
                    case 'tinker':
                        $result = api_autograder_create_new_tinker_item($request['user_id'], $language, $code);
                        break;
                    case 'practice':
                        $result = api_autograder_create_new_practice_item($request['user_id'], $language, $code, $problem_id);
                        break;
                    case 'golf':
                        $result = api_autograder_create_new_golf_item($request['user_id'], $language, $code, $problem_id);
                        break;
                    default:
                        return build_response_ok('', client_poll_encode_response(array('type' => 'error', 'msg' => 'what just happened?')));
                }
                if ($result['ERROR']) {
                    return build_response_ok('', client_poll_encode_response(array('type' => 'error', 'msg' => $result['message'])));
                }
                return build_response_ok('', client_poll_encode_response(array('type' => 'ok', 'token' => $result['token'])));
            case 'poll':
                $token = $request['form']['token'];
                $result = api_autograder_get_item_status_for_client($token);
                if ($result['ERROR']) {
                    return build_response_ok('', client_poll_encode_response(array('type' => 'error', 'msg' => $result['message'])));
                }
                $type = 'info';
                $message = $result['state'];
                if ($result['state'] == 'DONE') {
                    $message = $result['output'];
                    $type = 'output';
                } else {
                    if (strpos($result['state'], 'ERROR_') === 0) {
                        $type = 'error';
                    } else {
                        $type = 'state';
                    }
                }
                return build_response_ok('', client_poll_encode_response(array('type' => $type, 'msg' => $message, 'token' => $token)));
            default:
                break;
        }
    }
    return build_response_ok('', 'wat?');
}
Exemplo n.º 9
0
function execute($request)
{
    $language_info = api_autograder_get_language_info($request['path_parts'][1]);
    $problem_id = intval($request['path_parts'][2]);
    if ($problem_id == 0 || $language_info == null) {
        return build_response_not_found("Problem not found.");
    }
    $problem = api_autograder_menu_get_problem($request['user_id'], $language_info['language_id'], 'practice', 0, $problem_id);
    if ($problem == null) {
        return build_response_not_found("Problem not found.");
    }
    $output = array('<h1>' . htmlspecialchars($problem['title']) . '</h1>', '<div>', nl2br(htmlspecialchars($problem['statement'])), '</div>', api_autograder_generate_client_html('practice', $language_info, $problem['template'], $problem_id));
    return build_response_ok('Problem', implode("\n", $output), array('js' => 'autograder.js', 'onload' => "ag_init('practice')"));
}
Exemplo n.º 10
0
function execute($request)
{
    $user_info = api_account_lookup_user_by_name($request['path_parts'][1]);
    if ($user_info == null) {
        return build_response_not_found("Not account by that name exists.");
    }
    $user_id = $user_info['user_id'];
    $output = array('<h1>' . htmlspecialchars($user_info['name']) . '</h1>');
    $profile = sql_query_item("SELECT * FROM `user_profiles` WHERE `user_id` = {$user_id} LIMIT 1");
    if ($profile == null) {
        $profile = array();
    }
    if (strlen($user_info['image_id']) > 0) {
        array_push($output, '<div>', '<img src="/uploads/avatars/' . $user_info['image_id'] . '" />', '</div>');
    }
    $blurb = trim($profile['blurb']);
    if (strlen($blurb) > 0) {
        array_push($output, '<div>', nl2br(htmlspecialchars($blurb)), '</div>');
    }
    array_push($output, '<div style="padding-top:100px; font-style:italic; color:#888;">', "More interesting stuff will be put here, I promise.", '</div>');
    return build_response_ok($user_info['name'], implode("\n", $output));
}
Exemplo n.º 11
0
function execute($request)
{
    $output = array('<h1>About</h1>');
    $items = array(array('What is this, exactly?', 'wat', "Nerd Paradise is a site for programmers to learn, play, and interact casually or competitively."), array("What are points?", 'points', "Various activities on the site will award you points. At the end of each season, the person with the most points wins. "), array("I have an idea for a golf problem!", 'problemidea', "Great. <a href=\"/contact\">Let me know</a>."), array("I want to contribute a tutorial", 'tutorailidea', "At this time I am only posting tutorials I have personally written for reasons."), array("I'm disatisfied. I want to take your source code and make my own NP. With blackjack. And hookers.", 'blackjack', "Neat. The source code for both the site and the auto-grader is available <a href=\"https://github.com/blakeohare/nerdparadise\">here</a> and ready for forking."), array("Wasn't NP a totally differenet site? Where did it go?", 'oldnp', "Yes. This is version 10 of the website. The content from the previous version (version 8) was mostly rolled into my <a href=\"http://blakeohare.com\">personal blog</a> since that's really all it was anyway. Yes, the version numbers are a Windows joke."), array("The autograder is down/unresponsive.", 'autograder', "The auto-grader is a sandboxed program that I am running on a dedicated machine in my apartment on my personal internet connection. It'll happen from time to time."), array("What the hell is Crayon and why is it everywhere?", 'crayon', "Crayon is a programming language I created primarily for creating games. If you're interested in learning it, feel free to wander over to <a href=\"http://crayonlang.org\">crayonlang.org</a>. The reason why it's everywhere is because I know how to sandbox it easily and so making an autograder for it was simple. I also like promoting it since I think it's a swell language."), array("Has anyone actually asked you to ban their parents like the ToS mention?", 'parents', "Yes. Twice."), array("Who are you?", 'who', "My name is Blake. I am a professional software engineer and enjoy writing programming languages, creating games, and teaching programming."), array("How is it that this site can both be free and not have any ads?", 'adfree', "The server costs for this and my other sites are currently miniscule. I do this as a hobby."));
    array_push($output, '<div style="padding:8px; margin-bottom:20px;">');
    foreach ($items as $item) {
        $heading = $item[0];
        $bookmark = $item[1];
        array_push($output, '<div><a href="#' . $bookmark . '">' . htmlspecialchars($heading) . '</a></div>');
    }
    array_push($output, '</div>');
    foreach ($items as $item) {
        $heading = $item[0];
        $bookmark = $item[1];
        $text = $item[2];
        array_push($output, '<h2><a name="' . $bookmark . '"></a>' . htmlspecialchars($heading) . '</h2>');
        array_push($output, '<div style="padding:8px; margin-bottom:20px;">');
        array_push($output, $text);
        array_push($output, '</div>');
    }
    return build_response_ok("About", implode("\n", $output));
}
Exemplo n.º 12
0
function execute($request)
{
    $output = array('<h1>Code Golf</h1>', "<p><a href=\"https://en.wikipedia.org/wiki/Code_golf\">Code Golf</a> is a competition to see who can solve a programming problem using the fewest [key] \"strokes\".</p>", '</div>');
    $now = time();
    // TODO: migrate to api layer
    $current_challenge = api_autograder_canonicalize_problem(sql_query_item("SELECT * FROM `code_problems` WHERE `type` = 'golf' AND `golf_start_time` <= {$now} AND `golf_end_time` > {$now} LIMIT 1"));
    array_push($output, '<div style="padding-top:20px; margin-bottom:20px;">', '<div class="block" style="float:left; width:460px;">', '<p>' . "A new problem is posted every <s>2 weeks</s> once in a while. " . "During that time you can submit solutions. " . "Once time is up, the highest ranking (shortest) solutions will be awarded points. " . "You may still submit solutions after time is up for practice, but they won't be recorded for scores." . '</p>', '<p>Points are granted as follows on a per-language basis:</p>', '<ul>', '<li>First place: 3 points</li>', '<li>Second place: 2 points</li>', '<li>Third through fifth: 1 point</li>', '</ul>', '<p>Preference is given to earlier solutions in the event of ties. The maximum points you can receive is 3 &times; {number of languages}.</p>', '<p>More about <a href="/about#points">NP points</a>.</p>', '<p>Want a reminder every 2 weeks? New Golf questions will be announced via <a href="https://twitter.com/nerdparadise">twitter</a>.</p>', '</div>', '<div class="block" style="float:left; margin-left:20px; width:400px;">');
    if ($current_challenge == null) {
        array_push($output, '<h2>Current Challenge: None</h2>', '<div>Check back soon or poke <a href="/profiles/blake">Blake</a></div>');
    } else {
        array_push($output, '<h2>Current Challenge: <a href="/golf/' . $current_challenge['problem_id'] . '">' . htmlspecialchars($current_challenge['title']) . '</a></h2>', '<div><span style="color:#048; font-weight:bold;">' . seconds_to_duration($current_challenge['golf_end_time'] - time()) . '</span> Remain.</div>', '');
        // TODO: migrate to api
        $ranked_entries = sql_query("\r\n\t\t\t\tSELECT\r\n\t\t\t\t\tr.`user_id`,\r\n\t\t\t\t\tr.`integer_rank`,\r\n\t\t\t\t\tr.`code_size`,\r\n\t\t\t\t\tr.`language_id`,\r\n\t\t\t\t\tlang.`name` AS 'lang_name',\r\n\t\t\t\t\tlang.`key` AS 'lang_key'\r\n\t\t\t\tFROM `code_solutions` r\r\n\t\t\t\tINNER JOIN `languages` lang ON (lang.`language_id` = r.`language_id`)\r\n\t\t\t\tWHERE\r\n\t\t\t\t\tr.`problem_id` = " . $current_challenge['problem_id'] . " AND\r\n\t\t\t\t\tr.`integer_rank` <= 3\r\n\t\t\t\tORDER BY r.`integer_rank`");
        if ($ranked_entries->num_rows == 0) {
            array_push($output, '<p>Currently there are no submissions.</p>', '<p><a href="/golf/' . $current_challenge['problem_id'] . '">Be the first!</a></p>');
        } else {
            array_push($output, '<h2 style="padding-top:20px; padding-bottom:10px;">Rankings</h2>');
            $user_ids = array();
            $languages = array();
            $language_keys = array();
            $language_names = array();
            for ($i = 0; $i < $ranked_entries->num_rows; ++$i) {
                $entry = $ranked_entries->fetch_assoc();
                array_push($user_ids, $entry['user_id']);
                $language_key = $entry['lang_key'];
                if (!isset($languages[$language_key])) {
                    $languages[$language_key] = array();
                    array_push($language_keys, $language_key);
                    $language_names[$language_key] = $entry['lang_name'];
                }
                array_push($languages[$language_key], $entry);
            }
            sort($language_keys);
            $user_infos = api_account_fetch_mini_profiles($user_ids);
            foreach ($language_keys as $language_key) {
                array_push($output, '<h3>', '<img src="/images/languages/' . $language_key . '_small.png" valign="middle" />', htmlspecialchars($language_names[$language_key]), '</h3>', '<table style="width:100%">');
                $rank = 1;
                foreach ($languages[$language_key] as $entry) {
                    $user_info = $user_infos['user_' . $entry['user_id']];
                    array_push($output, '<tr>', '<td>#' . $rank . '</td>', '<td><a href="/profiles/' . $user_info['login_id'] . '">' . htmlspecialchars($user_info['name']) . '</a></td>', '<td>' . $entry['code_size'] . ' byte' . ($entry['code_size'] == 1 ? '' : 's') . '</td>', '</tr>');
                    ++$rank;
                }
                array_push($output, '</table>');
            }
        }
    }
    array_push($output, '</div>', '</div>');
    array_push($output, '<div style="clear:left; padding-top:20px;">', '<div class="fullblock">', '<h2>All Challenges</h2>');
    $languages = api_autograder_get_language_infos(true);
    $problems_and_scores = api_autograder_menu_get_problems($request['user_id'], $request['is_admin'], 'golf', 0, true);
    $ordered_problem_ids = $problems_and_scores['ordered_problem_ids'];
    array_push($output, '<table cellspacing="0" cellpadding="4"><tr style="font-size:14px; font-weight:bold;"><td></td><td></td>');
    foreach ($languages as $language) {
        array_push($output, '<td style="padding-right:30px;">');
        array_push($output, '<img src="/images/languages/' . htmlspecialchars($language['key']) . '_small.png" valign="middle" />');
        array_push($output, htmlspecialchars($language['name']));
        array_push($output, '</td>');
    }
    array_push($output, '</tr>');
    $now = time();
    $alt = true;
    foreach ($ordered_problem_ids as $problem_id) {
        $problem_info = $problems_and_scores['problem_' . $problem_id];
        $is_active = $now < $problem_info['golf_end_time'];
        $alt = !$alt;
        $bg_color = $is_active ? 'cde' : ($alt ? 'fff' : 'eee');
        array_push($output, '<tr style="' . ($is_active ? 'font-weight:bold;' : '') . 'text-align:center;background-color:#' . $bg_color . ';">', '<td style="text-align:left;"><a href="/golf/' . $problem_id . '">', htmlspecialchars($problem_info['title']), '</a></td>', '<td>');
        if ($is_active) {
            array_push($output, "Ends: " . unix_to_scaling_time($problem_info['golf_end_time']));
        } else {
            array_push($output, "Ended: " . unix_to_scaling_time($problem_info['golf_start_time']));
        }
        array_push($output, '</td>');
        foreach ($languages as $language) {
            $score = $problems_and_scores['score_' . $problem_id . '_' . $language['language_id']];
            if (intval($score['code_size']) > 0) {
                array_push($output, '<td>');
                array_push($output, $score['code_size']);
                array_push($output, ' (#' . $score['integer_rank'] . ')');
                // TODO: little trophy images.
            } else {
                array_push($output, '<td style="color:#888;">');
                array_push($output, 'N/A');
            }
            array_push($output, '</td>');
        }
        array_push($output, '</tr>');
    }
    array_push($output, '</table>');
    array_push($output, '</div>');
    return build_response_ok("Code Golf", implode("\n", $output));
}
Exemplo n.º 13
0
function execute($request)
{
    $error = null;
    if ($request['method'] == 'POST') {
        $username = trim($request['form']['register_username']);
        $email = trim($request['form']['register_email']);
        $password1 = $request['form']['register_password1'];
        $password2 = $request['form']['register_password2'];
        $tuba = strtolower(string_alphanums($request['form']['register_tos']));
        if ($tuba != 'tuba') {
            $error = "Please read the terms of service.";
        } else {
            $result = api_account_register_user($username, $email, $password1, $password2, $request['ip']);
            if ($result['OK']) {
                $output = array('<h1>Account Registration</h1>', '<div>Registration successful!</div>', '<div>You may now <a href="/login">log in</a>.</div>');
                return build_response_ok("Registration Successful", implode("\n", $output));
            } else {
                switch ($result['message']) {
                    case 'NAME_BLANK':
                        $error = "Name is blank.";
                        break;
                    case 'NAME_INVALID_CHARS':
                        $error = "Name contains invalid characters.";
                        break;
                    case 'NAME_TOO_SHORT':
                        $error = "Name is too short.";
                        break;
                    case 'NAME_TOO_LONG':
                        $error = "Name is too long.";
                        break;
                    case 'NAME_NO_ALPHANUMS':
                        $error = "Name must contain at least 1 alphanumeric character.";
                        break;
                    case 'SIMILAR_NAME_EXISTS':
                        $error = "A similar username already exists.";
                        break;
                    case 'EMAIL_BLANK':
                        $error = "Email was left blank. Use a mailinator if you're paranoid.";
                        break;
                    case 'INVALID_EMAIL':
                        $error = "Email was invalid.";
                        break;
                    case 'PASSWORDS_DONT_MATCH':
                        $error = "Passwords did not match.";
                        break;
                    case 'PASSWORD_IS_BLANK':
                        $error = "Password was blank.";
                        break;
                    case 'PASSWORD_SAME_AS_USER':
                        $error = "Password was same as username.";
                        break;
                    case 'PASSWORD_EASY':
                        $error = "Your password is in the top 10 list of easy-to-guess passwords. Please pick something more creative.";
                        break;
                    default:
                        $error = "Server returned error code: " . $result['message'];
                        break;
                }
            }
        }
    }
    $output = array('<style type="text/css">', 'div#register_form h2 { margin:0px; margin-top:8px; font-size:16px; }', 'div#register_form { padding-left:30px; padding-bottom:30px; font-size:12px;}', '.register_aside { color:#888; }', 'div#register_error { color:#f00; font-weight:bold; }', '</style>', '<div id="register_form">', '<h1>Account Registration</h1>', '<form action="' . $request['path'] . '" method="post">', $error == null ? '' : '<div id="register_error">' . htmlspecialchars($error) . '</div>', '<h2>Username</h2>', '<div><input type="text" name="register_username" value="' . htmlspecialchars($username) . '" /></div>', '<h2>Email</h2>', '<div><input type="text" name="register_email" value="' . htmlspecialchars($email) . '" /></div>', '<h2>Password</h2>', '<div>', '<input type="password" name="register_password1" /><br />', '<input type="password" name="register_password2" /> <span class="register_aside">Again. This time with feeling.</span>', '</div>', '<div style="margin-top:30px;"><input type="text" name="register_tos" style="width:30px;" value="' . htmlspecialchars($tuba) . '" /> I read the <a href="/tos">Terms of Service</a>. As such, I know exactly what to type in this box.</div>', '<div style="margin-top:30px;">', '<input type="submit" name="register_submit" value="Register" />', '</div>', '</form>', '</div>');
    return build_response_ok("Register New Account", implode("\n", $output));
}
Exemplo n.º 14
0
function execute($request)
{
    $response = api_account_change_password('Blake', 'password2', 'blake', 'blake');
    debug_print($response);
    return build_response_ok("Test", "Ahoy. 2");
}
Exemplo n.º 15
0
function execute($request)
{
    $html = api_autograder_generate_client_html('tinker', null, '', 0);
    $output = array('<h1>Code Tinker</h1>', "<p>", "Write some code. Run it. See the output. ", "All code runs in a remote sandbox. ", "For more guided practice, see the <a href=\"/practice\">practice problems</a> or <a href=\"/tutorials\">tutorials</a> if you're just starting out. ", "</p>", $html);
    return build_response_ok("Code Tinker", implode("\n", $output), array('js' => 'autograder.js', 'onload' => "ag_init('tinker')"));
}
Exemplo n.º 16
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));
}
Exemplo n.º 17
0
function execute($request)
{
    $categories = api_forum_get_top_level_categories($request['user_id'], $request['is_admin']);
    $output = array('<h1>Forum</h1>', '<p>Please read the <a href="/TODO-write-forum-rules">forum rules</a> before posting.</p>', '</div>');
    $topics = array(array('header' => "General", 'keys' => array('announcements', 'general', 'touchytouchy', 'introductions', 'forumgames')), array('header' => "Software Discussion", 'keys' => array('projects', 'generalprogramming', 'gamedev', 'web', 'ux', 'interviewquestions')), array('header' => "Site Topics", 'keys' => array('codegolfing', 'competitions', 'gamejams', 'content')));
    array_push($output, '<div style="padding-top:20px; padding-bottom:20px;">');
    array_push($output, '<div style="float:left; width:600px; margin-right:20px;">');
    foreach ($topics as $topic) {
        array_push($output, '<div class="block" style="margin-bottom:20px;">');
        array_push($output, '<h2>' . htmlspecialchars($topic['header']) . '</h2>');
        foreach ($topic['keys'] as $category_key) {
            $category = $categories[$category_key];
            array_push($output, '<div style="margin-top:10px;">');
            $threads = $category['thread_count'];
            $posts = $category['post_count'];
            array_push($output, '<div style="width:100px; float:right; color:#aaa; font-size:11px;">');
            array_push($output, $threads . ' thread' . ($threads == 1 ? '' : 's') . '<br />');
            array_push($output, $posts . ' post' . ($posts == 1 ? '' : 's'));
            array_push($output, '</div>');
            array_push($output, '<h3><a href="/forum/' . $category_key . '">');
            array_push($output, htmlspecialchars($category['name']));
            array_push($output, '</a></h3>');
            array_push($output, '<div style="width:400px;">');
            array_push($output, htmlspecialchars($category['description']));
            array_push($output, '</div>');
            array_push($output, '</div>');
        }
        array_push($output, '</div>');
    }
    array_push($output, '</div>');
    array_push($output, '<div style="float:left; width:340px;">');
    array_push($output, '<div class="block">');
    array_push($output, '<h2>Recent Activity</h2>');
    $recent_threads = api_forum_get_recent_threads();
    $user_info = $recent_threads['user_info'];
    foreach ($recent_threads['threads'] as $thread_info) {
        array_push($output, '<div style="margin-bottom:10px;">');
        $user = $user_info['user_' . $thread_info['user_id']];
        array_push($output, '<h3>');
        array_push($output, '<a href="/forum/' . $thread_info['category_key'] . '/' . $thread_info['thread_id'] . '/new">');
        array_push($output, htmlspecialchars($thread_info['title']));
        array_push($output, '</a>');
        array_push($output, '</h3>');
        array_push($output, '<div style="color:#888; font-style:italic;">');
        array_push($output, "Lorem ipsum dolar sit amet...");
        array_push($output, '</div>');
        array_push($output, '<div>');
        array_push($output, 'by <a href="/profiles/' . $user['login_id'] . '">');
        array_push($output, htmlspecialchars($user['name']));
        array_push($output, '</a>');
        array_push($output, ' in <a href="/forum/' . $thread_info['category_key'] . '">');
        array_push($output, htmlspecialchars($thread_info['category_name']));
        array_push($output, '</a>');
        array_push($output, '</div>');
        array_push($output, '</div>');
    }
    array_push($output, '</div>');
    array_push($output, '</div>');
    array_push($output, '</div>');
    array_push($output, '<div class="fullblock" style="clear:both;">');
    array_push($output, '<div>Users Online: ');
    $users_online = api_forum_get_users_online();
    $first = true;
    foreach ($users_online['ordered_user_keys'] as $login_id) {
        $user_id = $users_online['keys_to_user_ids'][$login_id];
        $user_info = $users_online['user_' . $user_id];
        if ($first) {
            $first = false;
        } else {
            array_push($output, ', ');
        }
        array_push($output, '<a href="/profiles/' . $login_id . '">');
        array_push($output, htmlspecialchars($user_info['name']));
        array_push($output, '</a>');
    }
    if ($first) {
        array_push($output, "No one!");
    }
    array_push($output, '</div>');
    return build_response_ok("Forum", implode("\n", $output));
}
Exemplo n.º 18
0
function execute($request)
{
    $output = array('<div>', main_page_render_marquee($request), '</div>', '</div>', '<div style="padding-top:20px;">', '<div style="float:left; width:306px;">', '<div class="block">', $request['user_id'] == 0 ? main_page_about_widget($request) : main_page_achievements_widget($request), '</div>', '<div class="block" style="margin-top:20px;">', main_page_forum_widget($request), '</div>', '</div>', '<div style="float:left; width:307px; margin-left:20px;">', '<div class="block">', main_page_tutorials_widget($request), '</div>', '<div class="block" style="margin-top:20px;">', main_page_practice_widget($request), '</div>', '<div class="block" style="margin-top:20px;">', main_page_tinker_widget($request), '</div>', '</div>', '<div style="float:left; width:307px; margin-left:20px;">', '<div class="block">', main_page_code_golf_widget($request), '</div>', '<div class="block" style="margin-top:20px;">', main_page_competitions_widget($request), '</div>', '<div class="block" style="margin-top:20px;">', main_page_game_jam_widget($request), '</div>', '</div>', '<div style="clear:both;"></div>', '</div>', '<div class="fullblock" style="margin-top:20px;">', 'lorem ipsum');
    return build_response_ok("Nerd Paradise", implode("\n", $output));
}
Exemplo n.º 19
0
function execute($request)
{
    return build_response_ok("Log Out", "Log out.");
}
Exemplo n.º 20
0
function execute($request)
{
    return build_response_ok('.', '.');
}
Exemplo n.º 21
0
function execute($request)
{
    $user_id = $request['user_id'];
    if ($user_id == 0) {
        return build_response_forbidden("You must be logged in to see this page.");
    }
    $has_blurb = true;
    $user_info = api_account_canonicalize_user_db_entry(sql_query_item("SELECT * FROM `users` WHERE `user_id` = {$user_id} LIMIT 1"));
    $user_profile = sql_query_item("SELECT * FROM `user_profiles` WHERE `user_id` = {$user_id} LIMIT 1");
    if ($user_profile == null) {
        $user_profile = array('user_id' => $user_id, 'blurb' => '', 'contact' => '');
        $has_blurb = false;
    }
    $profile_image = $user_info['image_id'];
    $profile_email = $user_info['email_addr'];
    $profile_blurb = $user_profile['blurb'];
    $new_profile_image_path = null;
    $upload_success = false;
    $errors = array();
    if ($request['method'] == 'POST') {
        $profile_email = trim($request['form']['profile_email']);
        $profile_old_password = $request['form']['profile_old_password'];
        $profile_new_password1 = $request['form']['profile_new_password1'];
        $profile_new_password2 = $request['form']['profile_new_password2'];
        $profile_blurb = $request['form']['profile_blurb'];
        $password_change_attempt = strlen($profile_old_password) > 0 || strlen($profile_new_password1) > 0 || strlen($profile_new_password2) > 0;
        $upload_avatar = count($request['files']) == 1 && $request['files'][0]['size'] > 0;
        if ($upload_avatar) {
            $file = $request['files'][0];
            if (!$file['is_image']) {
                array_push($errors, "File was not an image.");
            } else {
                $width = intval($file['image_width']);
                $height = intval($file['image_height']);
                if ($width < 10 || $height < 10) {
                    array_push($errors, "Avatar width and height must be greater than 10 pixels.");
                } else {
                    if ($width > 100 || $height > 100) {
                        array_push($errors, "Avatar must be small enough to fit in a 100x100 pixel box.");
                    } else {
                        if ($file['size'] > 50 * 1024) {
                            array_push($errors, "Avatar filesize is too big (limit is 50KB)");
                        } else {
                            $image_key = generate_gibberish(10);
                            $extension = null;
                            switch ($file['type']) {
                                case 'PNG':
                                    $extension = '.png';
                                    break;
                                case 'JPG':
                                case 'JPEG':
                                    $extension = '.jpg';
                                    break;
                                case 'GIF':
                                    $extension = '.gif';
                                    break;
                                default:
                                    break;
                            }
                            if ($extension == null) {
                                array_push($errors, "Unknown image format.");
                            } else {
                                $new_profile_image_path = $image_key . $extension;
                                $destination = 'uploads/avatars/' . $new_profile_image_path;
                                $error = false;
                                @copy($file['path'], $destination) or $error = true;
                                if ($error) {
                                    array_push($errors, "An unknown error occurred while copying the image.");
                                } else {
                                    $upload_success = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        if ($upload_success) {
            sql_query("\r\n\t\t\t\t\tUPDATE `users`\r\n\t\t\t\t\tSET\r\n\t\t\t\t\t\t`image_id` = '" . sql_sanitize_string($new_profile_image_path) . "',\r\n\t\t\t\t\t\t`image_dim` = '" . intval($width) . "|" . intval($height) . "'\r\n\t\t\t\t\tWHERE `user_id` = {$user_id}\r\n\t\t\t\t\tLIMIT 1");
        }
        $password_updated = false;
        if ($password_change_attempt) {
            $old_pass_hash = api_account_hash_password($profile_old_password);
            $pass_hash = sql_query_item("SELECT `pass_hash` FROM `users` WHERE `user_id` = {$user_id} LIMIT 1");
            if ($pass_hash['pass_hash'] != $old_pass_hash) {
                array_push($errors, "Old password was incorrect.");
            } else {
                $result = api_account_validate_password($request['name'], $profile_new_password1, $profile_new_password2);
                if ($result['ERROR']) {
                    $error = '';
                    switch ($result['message']) {
                        case 'PASSWORDS_DONT_MATCH':
                            $error = "New passowrd fields didn't match.";
                            break;
                        case 'PASSWORD_IS_BLANK':
                            $error = "Password was blank.";
                            break;
                        case 'PASSWORD_SAME_AS_USER':
                            $error = "Password was same as username.";
                            break;
                        case 'PASSWORD_EASY':
                            $error = "Password is too easy to guess.";
                            break;
                        default:
                            $error = "Invalid password.";
                            break;
                    }
                    array_push($errors, $error);
                } else {
                    $password_updated = true;
                    sql_query("UPDATE `users` SET `pass_hash` = '" . sql_sanitize_string(api_account_hash_password($profile_new_password1)) . "' WHERE `user_id` = {$user_id} LIMIT 1");
                }
            }
        }
        $email_validate = api_account_validate_email($profile_email);
        if ($email_validate['ERROR']) {
            if ($email_validate['BLANK_EMAIL']) {
                array_push($errors, "Email is blank.");
            } else {
                array_push($errors, "Invalid email.");
            }
        }
        if (count($errors) == 0) {
            sql_query("UPDATE `users` SET `email_addr` = '" . sql_sanitize_string($profile_email) . "' WHERE `user_id` = {$user_id} LIMIT 1");
            if ($has_blurb) {
                sql_query("UPDATE `user_profiles` SET `blurb` = '" . sql_sanitize_string($profile_blurb) . "' WHERE `user_id` = {$user_id} LIMIT 1");
            } else {
                if (strlen(trim($profile_blurb)) > 0) {
                    sql_insert('user_profiles', array('user_id' => $user_id, 'blurb' => $profile_blurb));
                }
            }
        }
    }
    $output = array('<h1>Account Settings</h1>');
    if ($upload_success) {
        array_push($output, '<div>', "Profile Image Updated", '</div>');
    }
    if ($password_updated) {
        array_push($output, '<div>', "Password updated.", '</div>');
    }
    if (count($errors) > 0) {
        array_push($output, '<div style="color:#f00;"><div>', implode('</div><div>', $errors), '</div></div>');
    }
    array_push($output, '<form action="' . $request['path'] . '" method="post" enctype="multipart/form-data">');
    $has_image = strlen($user_info['image_id']) > 0;
    array_push($output, '<div style="padding-bottom:20px;">', '<h2>Profile Image</h2>', $has_image ? '<div><img src="/uploads/avatars/' . $user_info['image_id'] . '" /></div>' : '', '<div>', "Update: ", '<input type="file" name="avatar" />', '</div>', '<div>', '<input type="checkbox" name="profile_delete_image" value="1" /> Delete profile image', '</div>', '</div>');
    array_push($output, '<div style="padding-bottom:20px;">', '<h2>Profile Blurb</h2>', '<div>', '<textarea name="profile_blurb" rows="6" style="width:600px;">' . htmlspecialchars($profile_blurb) . '</textarea>', '</div>', '</div>');
    array_push($output, '<div style="padding-bottom:20px;">', '<h2>Email Address</h2>', '<div>', '<input type="text" name="profile_email" value="' . $profile_email . '" style="width:300px;"/>', '</div>', '</div>');
    array_push($output, '<div style="padding-bottom:20px;">', '<h2>Change Password</h2>', '<div>(leave blank to leave as is)</div>', '<table>', '<tr><td>Old Password:</td><td><input type="password" name="profile_old_password" /></td></tr>', '<tr><td>New Password:</td><td><input type="password" name="profile_new_password1" /></td></tr>', '<tr><td>New Password Confirm:</td><td><input type="password" name="profile_new_password2" /></td></tr>', '</table>', '</div>');
    array_push($output, '<div>', '<input type="submit" name="submit" value="Update" />', '</div>');
    array_push($output, '</form>');
    return build_response_ok('Account Settings', implode("\n", $output));
}
Exemplo n.º 22
0
function execute($request)
{
    return build_response_ok("Contact", "Contact.");
}
Exemplo n.º 23
0
function execute($request)
{
    $output = array('<h1 style="margin:0px;">Terms of Service</h1>', '<div style="font-size:12px; padding:20px;">', "<p>This site is provided just for fun. No quality of service is guaranteed. This site will break. Sometimes for long periods of time. You'll be okay. I promise.</p>", "<p>This site is privately owned and maintained. Freedom of speech does not apply. The moderators are your wardens. Any content (posts, comments, etc) can be removed for any reason whatsoever.</p>", "<p>Here are some possible reasons:</p>", '<ul>', "<li>The moderator deems your behavior as toxic to an inclusive and happy community.</li>", "<li>The moderator ate a really bad <a href=\"http://s3-media1.fl.yelpcdn.com/bphoto/06SlQIfYHJERTzzb3iM0lw/o.jpg\">breakfast burrito from a gas station</a> and now has a tummy ache and is taking it out on you.</li>", '</ul>', "<p>All moderator decisions are final. Even in the event of burrit'ocalypse '16. Please do not argue with them. Access to this site is not a right.</p>", "<p>Illegal content or discussion of illegal activities will be removed.</p>", "<p>Type tuba in the box on the registration form. But still, you should read the rest of this.</p>", "<p>If your parents join this site in order to keep tabs on you, please do not ask the moderator to ban them.</p>", "<p>Do not bash people for their choice of IDE or text editor.</p>", "<p>Be nice. That includes while on IRC as well.</p>", "<p>If you are banned, do not evade the ban. That makes us very mad. Your life will go on without us. I promise.</p>", "<h2>Stuff about Privacy</h2>", "<p>Your account information is not shared with anyone. Your account information isn't that interesting anyway.</p>", '</div>');
    return build_response_ok('Terms of Service', implode("\n", $output));
}