function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
 {
     require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
     global $qa_login_userid, $questionid, $relatedcount, $question, $relatedquestions, $qa_cookieid, $usershtml;
     list($question, $relatedquestions) = qa_db_select_with_pending(qa_db_full_post_selectspec($qa_login_userid, $questionid), qa_db_related_qs_selectspec($qa_login_userid, $questionid));
     if ($relatedcount > 1 && !$question['hidden']) {
         $minscore = qa_match_to_min_score(qa_opt('match_related_qs'));
         foreach ($relatedquestions as $key => $related) {
             if ($related['postid'] == $questionid || $related['score'] < $minscore) {
                 unset($relatedquestions[$key]);
             }
         }
         if (count($relatedquestions)) {
             $themeobject->output('<h2>' . qa_lang('main/related_qs_title') . '</h2>');
         } else {
             $themeobject->output('<h2>' . qa_lang('main/no_related_qs_title') . '</h2>');
         }
         $upper = qa_opt('related_qs_num') < count($relatedquestions) ? qa_opt('related_qs_num') : count($relatedquestions);
         foreach ($relatedquestions as $related) {
             if ($upper <= 0) {
                 break;
             }
             $themeobject->output('<p style="margin:0 0 10px 0; font-weight:bold;"><a href="' . qa_path_html(qa_q_request($related['postid'], $related['title'])) . '">' . $related['title'] . '</a></p>');
             $upper--;
         }
     }
 }
    function user_activity_form()
    {
        $handle = $this->_user_handle();
        if (!$handle) {
            return;
        }
        $userid = $this->getuserfromhandle($handle);
        if (!$userid) {
            return;
        }
        // update last visit
        if ($userid == qa_get_logged_in_userid() && qa_opt('user_act_list_new')) {
            qa_db_query_sub('CREATE TABLE IF NOT EXISTS ^usermeta (
				meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
				user_id bigint(20) unsigned NOT NULL,
				meta_key varchar(255) DEFAULT NULL,
				meta_value longtext,
				PRIMARY KEY (meta_id),
				UNIQUE (user_id,meta_key)
				) ENGINE=MyISAM  DEFAULT CHARSET=utf8');
            $last_visit = qa_db_read_one_value(qa_db_query_sub('SELECT UNIX_TIMESTAMP(meta_value) FROM ^usermeta WHERE user_id=# AND meta_key=$', qa_get_logged_in_userid(), 'visited_profile'), true);
            qa_db_query_sub('INSERT INTO ^usermeta (user_id,meta_key,meta_value) VALUES(#,$,NOW()) ON DUPLICATE KEY UPDATE meta_value=NOW()', $userid, 'visited_profile');
        } else {
            $last_visit = time();
        }
        $event_query = qa_db_query_sub("SELECT \n\t\t\t\te.event, \n\t\t\t\tBINARY e.params as params, \n\t\t\t\tUNIX_TIMESTAMP(e.datetime) AS datetime\n\t\t\tFROM \n\t\t\t\t^eventlog AS e\n\t\t\tWHERE\n\t\t\t\te.userid=#\n\t\t\t\tAND\n\t\t\t\tDATE_SUB(CURDATE(),INTERVAL # DAY) <= datetime\n\t\t\tORDER BY datetime DESC" . (qa_opt('user_act_list_max') ? " LIMIT " . (int) qa_opt('user_act_list_max') : ""), $userid, qa_opt('user_act_list_age'));
        // no post
        $nopost = array('u_password', 'u_reset', 'u_save', 'u_confirmed', 'u_edit', 'u_level', 'u_block', 'u_unblock', 'u_register', 'in_u_edit', 'in_u_level', 'in_u_block', 'in_u_unblock', 'feedback', 'search');
        // points
        require_once QA_INCLUDE_DIR . 'qa-db-points.php';
        $optionnames = qa_db_points_option_names();
        $options = qa_get_options($optionnames);
        $multi = (int) $options['points_multiple'];
        // compat fudge
        $upvote = '';
        $downvote = '';
        if (@$options['points_per_q_voted_up']) {
            $upvote = '_up';
            $downvote = '_down';
        }
        $option_events['in_q_vote_up'] = (int) $options['points_per_q_voted' . $upvote] * $multi;
        $option_events['in_q_vote_down'] = (int) $options['points_per_q_voted' . $downvote] * $multi * -1;
        $option_events['in_q_unvote_up'] = (int) $options['points_per_q_voted' . $upvote] * $multi * -1;
        $option_events['in_q_unvote_down'] = (int) $options['points_per_q_voted' . $downvote] * $multi;
        $option_events['in_a_vote_up'] = (int) $options['points_per_a_voted' . $upvote] * $multi;
        $option_events['in_a_vote_down'] = (int) $options['points_per_a_voted' . $downvote] * $multi * -1;
        $option_events['in_a_unvote_up'] = (int) $options['points_per_a_voted' . $upvote] * $multi * -1;
        $option_events['in_a_unvote_down'] = (int) $options['points_per_a_voted' . $downvote] * $multi;
        $option_events['in_a_select'] = (int) $options['points_a_selected'] * $multi;
        $option_events['in_a_unselect'] = (int) $options['points_a_selected'] * $multi * -1;
        $option_events['q_post'] = (int) $options['points_post_q'] * $multi;
        $option_events['a_post'] = (int) $options['points_post_a'] * $multi;
        $option_events['a_select'] = (int) $options['points_select_a'] * $multi;
        $option_events['q_vote_up'] = (int) $options['points_vote_up_q'] * $multi;
        $option_events['q_vote_down'] = (int) $options['points_vote_down_q'] * $multi;
        $option_events['a_vote_up'] = (int) $options['points_vote_up_a'] * $multi;
        $option_events['a_vote_down'] = (int) $options['points_vote_down_a'] * $multi;
        $fields = array();
        $events = array();
        $postids = array();
        $count = 0;
        while (($event = qa_db_read_one_assoc($event_query, true)) !== null) {
            if (preg_match('/postid=([0-9]+)/', $event['params'], $m) === 1) {
                $event['postid'] = (int) $m[1];
                $postids[] = (int) $m[1];
                $events[$m[1] . '_' . $count++] = $event;
            } else {
                $events['nopost_' . $count++] = $event;
            }
        }
        // get post info, also makes sure post exists
        $posts = null;
        if (!empty($postids)) {
            $post_query = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid, type, parentid, BINARY title as title FROM ^posts WHERE postid IN (' . implode(',', $postids) . ')'));
            foreach ($post_query as $post) {
                $posts[(string) $post['postid']] = $post;
            }
        }
        foreach ($events as $postid_string => $event) {
            $type = $event['event'];
            $postid = preg_replace('/_.*/', '', $postid_string);
            $post = null;
            $post = @$posts[$postid];
            // these calls allow you to deal with deleted events;
            // uncomment the first one to skip them
            // uncomment the second one to build your own routine based on whether they are deleted.
            if (!in_array($type, $nopost) && $post == null) {
                continue;
            }
            // $deleted = (!in_array($type, $nopost) && $post == null);
            // hide / show exceptions
            if (qa_get_logged_in_level() < QA_USER_LEVEL_ADMIN) {
                if ($userid != qa_get_logged_in_userid()) {
                    // show public
                    $types = explode("\n", qa_opt('user_act_list_show'));
                    if (!in_array($type, $types)) {
                        continue;
                    }
                } else {
                    // hide from owner
                    $types = explode("\n", qa_opt('user_act_list_hide'));
                    if (in_array($type, $types)) {
                        continue;
                    }
                }
            }
            if (!qa_opt('user_act_list_' . $type)) {
                continue;
            }
            $params = array();
            $paramsa = explode("\t", $event['params']);
            foreach ($paramsa as $param) {
                $parama = explode('=', $param);
                if (isset($parama[1])) {
                    $params[$parama[0]] = $parama[1];
                } else {
                    $params[$param] = $param;
                }
            }
            $link = '';
            if (in_array($type, $nopost)) {
                if ($type == 'search') {
                    if ((int) $params['start'] != 0) {
                        continue;
                    }
                    $link = '<a href="' . qa_path_html('search', array('q' => $params['query'])) . '">' . qa_html($params['query']) . '</a>';
                } else {
                    if (in_array($type, array('u_edit', 'u_level', 'u_block', 'u_unblock'))) {
                        $ohandle = $this->getHandleFromID($params['userid']);
                        $link = '<a href="' . qa_path_html('user/' . $ohandle, null, qa_opt('site_url')) . '">' . $ohandle . '</a>';
                    } else {
                        $link = '';
                    }
                }
            } else {
                if ($type == 'badge_awarded') {
                    if (!qa_opt('badge_active') || !function_exists('qa_get_badge_type')) {
                        continue;
                    }
                    if ($post != null) {
                        if (strpos($post['type'], 'Q') !== 0) {
                            $anchor = qa_anchor(strpos($post['type'], 'A') === 0 ? 'A' : 'C', $params['postid']);
                            $parent = qa_db_read_one_assoc(qa_db_query_sub('SELECT parentid,type,BINARY title as title,postid FROM ^posts WHERE postid=#', $post['parentid']), true);
                            if ($parent['type'] == 'A') {
                                $parent = qa_db_read_one_assoc(qa_db_query_sub('SELECT BINARY title as title,postid FROM ^posts WHERE postid=#', $parent['parentid']), true);
                            }
                            $activity_url = qa_path_html(qa_q_request($parent['postid'], $parent['title']), null, qa_opt('site_url'), null, $anchor);
                            $link = '<a href="' . $activity_url . '">' . $parent['title'] . '</a>';
                        } else {
                            $activity_url = qa_path_html(qa_q_request($params['postid'], $post['title']), null, qa_opt('site_url'), null, null);
                            $link = '<a href="' . $activity_url . '">' . $post['title'] . '</a>';
                        }
                    }
                } else {
                    if ($post != null && strpos($event['event'], 'q_') !== 0 && strpos($event['event'], 'in_q_') !== 0) {
                        // comment or answer
                        if (!isset($params['parentid'])) {
                            $params['parentid'] = $post['parentid'];
                        }
                        $parent = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $params['parentid']));
                        if ($parent['type'] == 'A') {
                            $parent = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $parent['parentid']));
                        }
                        $anchor = qa_anchor(strpos($event['event'], 'a_') === 0 || strpos($event['event'], 'in_a_') === 0 ? 'A' : 'C', $params['postid']);
                        $activity_url = qa_path_html(qa_q_request($parent['postid'], $parent['title']), null, qa_opt('site_url'), null, $anchor);
                        $link = '<a href="' . $activity_url . '">' . $parent['title'] . '</a>';
                    } else {
                        if ($post != null) {
                            // question
                            if (!isset($params['title'])) {
                                $params['title'] = $posts[$params['postid']]['title'];
                            }
                            if ($params['title'] !== null) {
                                $activity_url = qa_path_html(qa_q_request($params['postid'], $params['title']), null, qa_opt('site_url'));
                                $link = '<a href="' . $activity_url . '">' . $params['title'] . '</a>';
                            }
                        }
                    }
                }
            }
            $time = $event['datetime'];
            if (qa_opt('user_act_list_shading')) {
                $days = (qa_opt('db_time') - $time) / 60 / 60 / 24;
                $col = round($days / qa_opt('user_act_list_age') * 255 / 2);
                $bkg = 255 - round($days / qa_opt('user_act_list_age') * 255 / 8);
                $bkg = dechex($bkg);
                $col = dechex($col);
                if (strlen($col) == 1) {
                    $col = '0' . $col;
                }
                if (strlen($bkg) == 1) {
                    $bkg = '0' . $bkg;
                }
                $col = '#' . $col . $col . $col;
                $bkg = '#' . $bkg . $bkg . $bkg;
            }
            $whenhtml = qa_html(qa_time_to_string(qa_opt('db_time') - $time));
            $when = qa_lang_html_sub('main/x_ago', $whenhtml);
            $when = str_replace(' ', '<br/>', $when);
            $when = preg_replace('/([0-9]+)/', '<span class="qa-history-item-date-no">$1</span>', $when);
            if (strpos($type, '_vote_nil') == 4) {
                if ($params['oldvote'] == '1') {
                    // unvoting an upvote
                    $points = @$option_events[str_replace('_vote_nil', '_unvote_up', $type)];
                } else {
                    // unvoting a downvote
                    $points = @$option_events[str_replace('_vote_nil', '_unvote_down', $type)];
                }
            } else {
                $points = @$option_events[$type];
            }
            $string = qa_opt('user_act_list_' . $type);
            if ($type == 'badge_awarded') {
                $slug = $params['badge_slug'];
                $typea = qa_get_badge_type_by_slug($slug);
                if (!$typea) {
                    continue;
                }
                $types = $typea['slug'];
                $typed = $typea['name'];
                $badge_name = qa_badge_name($slug);
                if (!qa_opt('badge_' . $slug . '_name')) {
                    qa_opt('badge_' . $slug . '_name', $badge_name);
                }
                $var = qa_opt('badge_' . $slug . '_var');
                $name = qa_opt('badge_' . $slug . '_name');
                $desc = qa_badge_desc_replace($slug, $var, false);
                $string = str_replace('^badge', '<span class="badge-' . $types . '" title="' . $desc . ' (' . $typed . ')">' . qa_html($name) . '</span>', $string);
            }
            $fields[] = array('type' => 'static', 'label' => '<div class="qa-history-item-date' . ($time >= $last_visit && strpos($type, 'in_') === 0 ? ' qa-history-item-date-new' : '') . '"' . (qa_opt('user_act_list_shading') ? ' style="color:' . $col . ';background-color:' . $bkg . '"' : '') . '>' . $when . '</div>', 'value' => '<table class="qa-history-item-table"><tr><td class="qa-history-item-type-cell"><div class="qa-history-item-type qa-history-item-' . $type . '">' . $string . '</div></td><td class="qa-history-item-title-cell"><div class="qa-history-item-title">' . $link . '</div></td class="qa-history-item-points-cell"><td align="right">' . ($points ? '<div class="qa-history-item-points qa-history-item-points-' . ($points < 0 ? 'neg">' : 'pos">+') . $points . '</div>' : '&nbsp') . '</td></tr></table>');
        }
        if (empty($fields)) {
            return;
        }
        return array('style' => 'wide', 'title' => qa_opt('user_act_list_title'), 'fields' => $fields);
    }
Exemple #3
0
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'pages/question-view.php';
require_once QA_INCLUDE_DIR . 'pages/question-submit.php';
//	Load relevant information about this comment
$commentid = qa_post_text('commentid');
$questionid = qa_post_text('questionid');
$parentid = qa_post_text('parentid');
$userid = qa_get_logged_in_userid();
list($comment, $question, $parent, $children) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $commentid), qa_db_full_post_selectspec($userid, $questionid), qa_db_full_post_selectspec($userid, $parentid), qa_db_full_child_posts_selectspec($userid, $parentid));
//	Check if there was an operation that succeeded
if (@$comment['basetype'] == 'C' && @$question['basetype'] == 'Q' && (@$parent['basetype'] == 'Q' || @$parent['basetype'] == 'A')) {
    $comment = $comment + qa_page_q_post_rules($comment, $parent, $children, null);
    // array union
    if (qa_page_q_single_click_c($comment, $question, $parent, $error)) {
        $comment = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $commentid));
        //	If so, page content to be updated via Ajax
        echo "QA_AJAX_RESPONSE\n1";
        //	If the comment was not deleted...
        if (isset($comment)) {
            $parent = $parent + qa_page_q_post_rules($parent, $questionid == $parentid ? null : $question, null, $children);
            // in theory we should retrieve the parent's siblings for the above, but they're not going to be relevant
            $comment = $comment + qa_page_q_post_rules($comment, $parent, $children, null);
            $usershtml = qa_userids_handles_html(array($comment), true);
            $c_view = qa_page_q_comment_view($question, $parent, $comment, $usershtml, false);
            $themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-comment', null, null);
            //	... send back the HTML for it
            echo "\n";
            $themeclass->c_list_item($c_view);
        }
        return;
function qa_flag_set_tohide($post, $userid, $handle, $cookieid, $question)
{
    require_once QA_INCLUDE_DIR . 'qa-db-votes.php';
    require_once QA_INCLUDE_DIR . 'qa-app-limits.php';
    qa_db_userflag_set($post['postid'], $userid, true);
    qa_db_post_recount_flags($post['postid']);
    switch ($post['basetype']) {
        case 'Q':
            $action = 'q_flag';
            break;
        case 'A':
            $action = 'a_flag';
            break;
        case 'C':
            $action = 'c_flag';
            break;
    }
    qa_report_write_action($userid, null, $action, $post['basetype'] == 'Q' ? $post['postid'] : null, $post['basetype'] == 'A' ? $post['postid'] : null, $post['basetype'] == 'C' ? $post['postid'] : null);
    qa_report_event($action, $userid, $handle, $cookieid, array('postid' => $post['postid']));
    $post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $post['postid']));
    $flagcount = $post['flagcount'];
    $notifycount = $flagcount - qa_opt('flagging_notify_first');
    if ($notifycount >= 0 && $notifycount % qa_opt('flagging_notify_every') == 0) {
        require_once QA_INCLUDE_DIR . 'qa-app-emails.php';
        require_once QA_INCLUDE_DIR . 'qa-app-format.php';
        $anchor = $post['basetype'] == 'Q' ? null : qa_anchor($post['basetype'], $post['postid']);
        qa_send_notification(null, qa_opt('feedback_email'), null, qa_lang('emails/flagged_subject'), qa_lang('emails/flagged_body'), array('^p_handle' => isset($post['handle']) ? $post['handle'] : qa_lang('main/anonymous'), '^flags' => $flagcount == 1 ? qa_lang_html_sub('main/1_flag', '1', '1') : qa_lang_html_sub('main/x_flags', $flagcount), '^p_context' => trim(@$post['title'] . "\n\n" . qa_viewer_text($post['content'], $post['format'])), '^url' => qa_path(qa_q_request($question['postid'], $question['title']), null, qa_opt('site_url'), null, $anchor)));
    }
    if ($flagcount >= qa_opt('flagging_hide_after') && !$post['hidden']) {
        return true;
    }
    return false;
}
function qa_question_set_status($oldquestion, $status, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null)
{
    require_once QA_INCLUDE_DIR . 'qa-app-format.php';
    require_once QA_INCLUDE_DIR . 'qa-app-updates.php';
    $washidden = $oldquestion['type'] == 'Q_HIDDEN';
    $wasqueued = $oldquestion['type'] == 'Q_QUEUED';
    $wasrequeued = $wasqueued && isset($oldquestion['updated']);
    qa_post_unindex($oldquestion['postid']);
    foreach ($answers as $answer) {
        qa_post_unindex($answer['postid']);
    }
    foreach ($commentsfollows as $comment) {
        if ($comment['basetype'] == 'C') {
            qa_post_unindex($comment['postid']);
        }
    }
    if (@$closepost['parentid'] == $oldquestion['postid']) {
        qa_post_unindex($closepost['postid']);
    }
    $setupdated = false;
    $event = null;
    if ($status == QA_POST_STATUS_QUEUED) {
        $newtype = 'Q_QUEUED';
        if (!$wasqueued) {
            $event = 'q_requeue';
        }
        // same event whether it was hidden or shown before
    } elseif ($status == QA_POST_STATUS_HIDDEN) {
        $newtype = 'Q_HIDDEN';
        if (!$washidden) {
            $event = $wasqueued ? 'q_reject' : 'q_hide';
            if (!$wasqueued) {
                $setupdated = true;
            }
        }
    } elseif ($status == QA_POST_STATUS_NORMAL) {
        $newtype = 'Q';
        if ($wasqueued) {
            $event = 'q_approve';
        } elseif ($washidden) {
            $event = 'q_reshow';
            $setupdated = true;
        }
    } else {
        qa_fatal_error('Unknown status in qa_question_set_status(): ' . $status);
    }
    qa_db_post_set_type($oldquestion['postid'], $newtype, $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE);
    if ($wasqueued && $status == QA_POST_STATUS_NORMAL && qa_opt('moderate_update_time')) {
        // ... for approval of a post, can set time to now instead
        if ($wasrequeued) {
            // reset edit time to now if there was one, since we're approving the edit...
            qa_db_post_set_updated($oldquestion['postid'], null);
        } else {
            // ... otherwise we're approving original created post
            qa_db_post_set_created($oldquestion['postid'], null);
            qa_db_hotness_update($oldquestion['postid']);
        }
    }
    qa_update_counts_for_q($oldquestion['postid']);
    qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects'));
    if ($wasqueued || $status == QA_POST_STATUS_QUEUED) {
        qa_db_queuedcount_update();
    }
    if ($oldquestion['flagcount']) {
        qa_db_flaggedcount_update();
    }
    if ($status == QA_POST_STATUS_NORMAL) {
        qa_post_index($oldquestion['postid'], 'Q', $oldquestion['postid'], $oldquestion['parentid'], $oldquestion['title'], $oldquestion['content'], $oldquestion['format'], qa_viewer_text($oldquestion['content'], $oldquestion['format']), $oldquestion['tags'], $oldquestion['categoryid']);
        foreach ($answers as $answer) {
            if ($answer['type'] == 'A') {
                // even if question visible, don't index hidden or queued answers
                qa_post_index($answer['postid'], $answer['type'], $oldquestion['postid'], $answer['parentid'], null, $answer['content'], $answer['format'], qa_viewer_text($answer['content'], $answer['format']), null, $answer['categoryid']);
            }
        }
        foreach ($commentsfollows as $comment) {
            if ($comment['type'] == 'C') {
                $answer = @$answers[$comment['parentid']];
                if (!isset($answer) || $answer['type'] == 'A') {
                    // don't index comment if it or its parent is hidden
                    qa_post_index($comment['postid'], $comment['type'], $oldquestion['postid'], $comment['parentid'], null, $comment['content'], $comment['format'], qa_viewer_text($comment['content'], $comment['format']), null, $comment['categoryid']);
                }
            }
        }
        if ($closepost['parentid'] == $oldquestion['postid']) {
            qa_post_index($closepost['postid'], $closepost['type'], $oldquestion['postid'], $closepost['parentid'], null, $closepost['content'], $closepost['format'], qa_viewer_text($closepost['content'], $closepost['format']), null, $closepost['categoryid']);
        }
    }
    $eventparams = array('postid' => $oldquestion['postid'], 'parentid' => $oldquestion['parentid'], 'parent' => isset($oldquestion['parentid']) ? qa_db_single_select(qa_db_full_post_selectspec(null, $oldquestion['parentid'])) : null, 'title' => $oldquestion['title'], 'content' => $oldquestion['content'], 'format' => $oldquestion['format'], 'text' => qa_viewer_text($oldquestion['content'], $oldquestion['format']), 'tags' => $oldquestion['tags'], 'categoryid' => $oldquestion['categoryid'], 'name' => $oldquestion['name']);
    if (isset($event)) {
        qa_report_event($event, $userid, $handle, $cookieid, $eventparams + array('oldquestion' => $oldquestion));
    }
    if ($wasqueued && $status == QA_POST_STATUS_NORMAL && !$wasrequeued) {
        require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
        require_once QA_INCLUDE_DIR . 'qa-util-string.php';
        qa_report_event('q_post', $oldquestion['userid'], $oldquestion['handle'], $oldquestion['cookieid'], $eventparams + array('notify' => isset($oldquestion['notify']), 'email' => qa_email_validate($oldquestion['notify']) ? $oldquestion['notify'] : null, 'delayed' => $oldquestion['created']));
    }
}
Exemple #6
0
function qa_post_get_full($postid, $requiredbasetypes = null)
{
    $post = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
    if (!is_array($post)) {
        qa_fatal_error('Post ID could not be found: ' . $postid);
    }
    if (isset($requiredbasetypes) && !is_numeric(strpos($requiredbasetypes, $post['basetype']))) {
        qa_fatal_error('Post of wrong type: ' . $post['basetype']);
    }
    return $post;
}
Exemple #7
0
function qa_badge_plugin_user_form($userid)
{
    $handles = qa_userids_to_handles(array($userid));
    $handle = $handles[$userid];
    // displays badge list in user profile
    $result = qa_db_read_all_assoc(qa_db_query_sub('SELECT badge_slug as slug, object_id AS oid FROM ^userbadges WHERE user_id=#', $userid));
    $fields = array();
    if (count($result) > 0) {
        // count badges
        $bin = qa_get_badge_list();
        $badges = array();
        foreach ($result as $info) {
            $slug = $info['slug'];
            $type = $bin[$slug]['type'];
            if (isset($badges[$type][$slug])) {
                $badges[$type][$slug]['count']++;
            } else {
                $badges[$type][$slug]['count'] = 1;
            }
            if ($info['oid']) {
                $badges[$type][$slug]['oid'][] = $info['oid'];
            }
        }
        foreach ($badges as $type => $badge) {
            $typea = qa_get_badge_type($type);
            $types = $typea['slug'];
            $typed = $typea['name'];
            $output = '
							<table>
								<tr>
									<td class="qa-form-wide-label">
										<h3 class="badge-title" title="' . qa_lang('badges/' . $types . '_desc') . '">' . $typed . '</h3>
									</td>
								</tr>';
            foreach ($badge as $slug => $info) {
                $badge_name = qa_lang('badges/' . $slug);
                if (!qa_opt('badge_' . $slug . '_name')) {
                    qa_opt('badge_' . $slug . '_name', $badge_name);
                }
                $name = qa_opt('badge_' . $slug . '_name');
                $count = $info['count'];
                if (qa_opt('badge_show_source_posts')) {
                    $oids = @$info['oid'];
                } else {
                    $oids = null;
                }
                $var = qa_opt('badge_' . $slug . '_var');
                $desc = qa_badge_desc_replace($slug, $var, $name);
                // badge row
                $output .= '
								<tr>
									<td class="badge-container">
										<div class="badge-container-badge">
											<span class="badge-' . $types . '" title="' . $desc . ' (' . $typed . ')">' . qa_html($name) . '</span>&nbsp;<span onclick="jQuery(\'.badge-container-sources-' . $slug . '\').slideToggle()" class="badge-count' . (is_array($oids) ? ' badge-count-link" title="' . qa_lang('badges/badge_count_click') : '') . '">x&nbsp;' . $count . '</span>
										</div>';
                // source row(s) if any
                if (is_array($oids)) {
                    $output .= '
										<div class="badge-container-sources-' . $slug . '" style="display:none">';
                    foreach ($oids as $oid) {
                        $post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $oid));
                        $title = $post['title'];
                        $anchor = '';
                        if ($post['parentid']) {
                            $anchor = urlencode(qa_anchor($post['type'], $oid));
                            $oid = $post['parentid'];
                            $title = qa_db_read_one_value(qa_db_query_sub('SELECT BINARY title as title FROM ^posts WHERE postid=#', $oid), true);
                        }
                        $length = 30;
                        $text = qa_strlen($title) > $length ? qa_substr($title, 0, $length) . '...' : $title;
                        $output .= '
											<div class="badge-source"><a href="' . qa_path_html(qa_q_request($oid, $title), NULL, qa_opt('site_url')) . ($anchor ? '#' . $anchor : '') . '">' . qa_html($text) . '</a></div>';
                    }
                }
                $output .= '
									</td>
								</tr>';
            }
            $output .= '
							</table>';
            $outa[] = $output;
        }
        $fields[] = array('value' => '<table class="badge-user-tables"><tr><td class="badge-user-table">' . implode('</td><td class="badge-user-table">', $outa) . '</td></tr></table>', 'type' => 'static');
    }
    $ok = null;
    $tags = null;
    $buttons = array();
    if ((bool) qa_opt('badge_email_notify') && qa_get_logged_in_handle() == $handle) {
        // add badge notify checkbox
        if (qa_clicked('badge_email_notify_save')) {
            qa_opt('badge_email_notify_id_' . $userid, (bool) qa_post_text('badge_notify_email_me'));
            $ok = qa_lang('badges/badge_notified_email_me');
        }
        $select = (bool) qa_opt('badge_email_notify_id_' . $userid);
        $tags = 'id="badge-form" action="' . qa_self_html() . '#signature_text" method="POST"';
        $fields[] = array('type' => 'blank');
        $fields[] = array('label' => qa_lang('badges/badge_notify_email_me'), 'type' => 'checkbox', 'tags' => 'NAME="badge_notify_email_me"', 'value' => $select);
        $buttons[] = array('label' => qa_lang_html('main/save_button'), 'tags' => 'NAME="badge_email_notify_save"');
    }
    return array('ok' => $ok && !isset($error) ? $ok : null, 'style' => 'tall', 'tags' => $tags, 'title' => qa_lang('badges/badges'), 'fields' => $fields, 'buttons' => $buttons);
}
Exemple #8
0
function ra_ajax_add_answer()
{
    //	Load relevant information about this question
    $questionid = qa_post_text('a_questionid');
    $userid = qa_get_logged_in_userid();
    list($question, $childposts) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid), qa_db_full_child_posts_selectspec($userid, $questionid));
    //	Check if the question exists, is not closed, and whether the user has permission to do this
    if (@$question['basetype'] == 'Q' && !isset($question['closedbyid']) && !qa_user_post_permit_error('permit_post_a', $question, QA_LIMIT_ANSWERS)) {
        require_once QA_INCLUDE_DIR . 'qa-app-captcha.php';
        require_once QA_INCLUDE_DIR . 'qa-app-format.php';
        require_once QA_INCLUDE_DIR . 'qa-app-post-create.php';
        require_once QA_INCLUDE_DIR . 'qa-app-cookies.php';
        require_once QA_INCLUDE_DIR . 'qa-page-question-view.php';
        require_once QA_INCLUDE_DIR . 'qa-page-question-submit.php';
        //	Try to create the new answer
        $usecaptcha = qa_user_use_captcha(qa_user_level_for_post($question));
        $answers = qa_page_q_load_as($question, $childposts);
        $answerid = qa_page_q_add_a_submit($question, $answers, false, $in, $errors);
        if ($answerid) {
            return true;
        }
    }
    die;
}
 /**
  * Outputs cache to the user
  */
 private function get_cache()
 {
     global $qa_usage;
     qa_db_connect('qa_page_db_fail_handler');
     qa_page_queue_pending();
     qa_load_state();
     qa_check_login_modules();
     qa_check_page_clicks();
     $contents = @file_get_contents($this->cache_file);
     if (!$contents) {
         return;
     }
     //cache failure, graceful exit
     $qa_content = array();
     // Dummy contents
     $userid = qa_get_logged_in_userid();
     $questionid = qa_request_part(0);
     $cookieid = qa_cookie_get(true);
     if (is_numeric($questionid)) {
         $question = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid));
         if (is_numeric($questionid) && qa_opt('do_count_q_views') && !$this->post_method && !qa_is_http_post() && qa_is_human_probably() && (!$question['views'] || ($question['lastviewip'] != qa_remote_ip_address() || !isset($question['lastviewip'])) && ($question['createip'] != qa_remote_ip_address() || !isset($question['createip'])) && ($question['userid'] != $userid || !isset($question['userid'])) && ($question['cookieid'] != $cookieid || !isset($question['cookieid'])))) {
             $qa_content['inc_views_postid'] = $questionid;
         } else {
             $qa_content['inc_views_postid'] = null;
         }
         qa_do_content_stats($qa_content);
     }
     if (QA_DEBUG_PERFORMANCE) {
         ob_start();
         $qa_usage->output();
         $contents .= ob_get_contents();
         ob_end_clean();
     }
     qa_db_disconnect();
     exit($contents);
 }
 function bookmarks_plugin_form()
 {
     // displays bookmarks_plugin_form form in user profile
     global $qa_request;
     $handle = preg_replace('/^[^\\/]+\\/([^\\/]+).*/', "\$1", $qa_request);
     $uid = $this->getuserfromhandle($handle);
     if (!$uid) {
         return;
     }
     if (qa_get_logged_in_handle() && qa_get_logged_in_handle() == $handle) {
         $bookmarks = $this->get_bookmarks_for_user($uid);
         if (!$bookmarks) {
             return;
         }
         $output = '<div class="bookmarks_container">';
         $query = qa_db_query_sub('SELECT title,postid FROM ^posts WHERE type=$ AND postid in (' . $bookmarks . ')', 'Q');
         $idx = 1;
         $bms = explode(',', $bookmarks);
         foreach ($bms as $qid) {
             $post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $qid));
             $title = $post['title'];
             $length = 60;
             $text = strlen($title) > $length ? substr($title, 0, $length) . '...' : $title;
             $output .= '<div class="bookmark-row" id="bookmark-row-' . $idx . '"><div class="bookmark-row-image bookmark" title="' . qa_html(qa_opt('bookmarks_plugin_unbookmark')) . '" onclick="ajaxBookmark(' . $qid . ',' . $uid . ',true,' . $idx++ . ')"></div><a href="' . qa_path_html(qa_q_request($qid, $title), NULL, qa_opt('site_url')) . '">' . qa_html($text) . '</a></div>';
         }
         $output .= '</div>';
         $fields['bookmarks'] = array('type' => 'static', 'label' => $output);
         $form = array('style' => 'tall', 'tags' => 'id="bookmarks_form"', 'title' => '<a id="bookmark_title">' . qa_opt('bookmarks_plugin_title') . '</a>', 'fields' => $fields);
         return $form;
     }
 }
 function ajaxCommentVote($vote, $postid)
 {
     global $topage, $qa_cookieid;
     $post = qa_db_select_with_pending(qa_db_full_post_selectspec($this->logged_in_userid, $postid));
     $voteerror = $this->comment_vote_error_html($post, $this->logged_in_userid, $topage);
     if ($voteerror === false) {
         $this->comment_vote_set($post, $this->logged_in_userid, qa_get_logged_in_handle(), $qa_cookieid, $vote);
         $comment = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
         $votes = $comment['netvotes'];
         $up_text = qa_lang_html('main/vote' . ($vote == 1 ? 'd' : '') . '_up_popup');
         $down_text = qa_lang_html('main/vote' . ($vote == -1 ? 'd' : '') . '_down_popup');
         echo '{"status":"1","data":"' . $votes . '","up":"' . $up_text . '","down":"' . $down_text . '"}';
     } else {
         echo '{"status":"0","data":"' . $voteerror . '"}';
     }
 }
function qa_recalc_perform_step(&$state)
{
    $continue = false;
    @(list($operation, $length, $next, $done) = explode(',', $state));
    switch ($operation) {
        case 'doreindexposts':
            qa_recalc_transition($state, 'doreindexposts_postcount');
            break;
        case 'doreindexposts_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'doreindexposts_reindex');
            break;
        case 'doreindexposts_reindex':
            $posts = qa_db_posts_get_for_reindexing($next, 10);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'qa-app-format.php';
                $lastpostid = max(array_keys($posts));
                qa_db_prepare_for_reindexing($next, $lastpostid);
                foreach ($posts as $postid => $post) {
                    qa_post_index($postid, $post['type'], $post['questionid'], $post['title'], qa_viewer_text($post['content'], $post['format']), $post['tags'], true);
                }
                $next = 1 + $lastpostid;
                $done += count($posts);
                $continue = true;
            } else {
                qa_db_truncate_indexes($next);
                qa_recalc_transition($state, 'doreindexposts_wordcount');
            }
            break;
        case 'doreindexposts_wordcount':
            $wordids = qa_db_words_prepare_for_recounting($next, 1000);
            if (count($wordids)) {
                $lastwordid = max($wordids);
                qa_db_words_recount($next, $lastwordid);
                $next = 1 + $lastwordid;
                $done += count($wordids);
                $continue = true;
            } else {
                qa_db_tagcount_update();
                // this is quick so just do it here
                qa_recalc_transition($state, 'doreindexposts_complete');
            }
            break;
        case 'dorecountposts':
            qa_recalc_transition($state, 'dorecountposts_postcount');
            break;
        case 'dorecountposts_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_db_unaqcount_update();
            qa_recalc_transition($state, 'dorecountposts_recount');
            break;
        case 'dorecountposts_recount':
            $postids = qa_db_posts_get_for_recounting($next, 1000);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_recount($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecountposts_complete');
            }
            break;
        case 'dorecalcpoints':
            qa_recalc_transition($state, 'dorecalcpoints_usercount');
            break;
        case 'dorecalcpoints_usercount':
            qa_db_userpointscount_update();
            // for progress update - not necessarily accurate
            qa_recalc_transition($state, 'dorecalcpoints_recalc');
            break;
        case 'dorecalcpoints_recalc':
            $userids = qa_db_users_get_for_recalc_points($next, 10);
            if (count($userids)) {
                $lastuserid = max($userids);
                qa_db_users_recalc_points($next, $lastuserid);
                $next = 1 + $lastuserid;
                $done += count($userids);
                $continue = true;
            } else {
                qa_db_truncate_userpoints($next);
                qa_db_userpointscount_update();
                // quick so just do it here
                qa_recalc_transition($state, 'dorecalcpoints_complete');
            }
            break;
        case 'dorecalccategories':
            qa_recalc_transition($state, 'dorecalccategories_postcount');
            break;
        case 'dorecalccategories_postcount':
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'dorecalccategories_postupdate');
            break;
        case 'dorecalccategories_postupdate':
            $postids = qa_db_posts_get_for_recategorizing($next, 100);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_recalc_categoryid($next, $lastpostid);
                qa_db_posts_calc_category_path($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_recount');
            }
            break;
        case 'dorecalccategories_recount':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                foreach ($categoryids as $categoryid) {
                    qa_db_ifcategory_qcount_update($categoryid);
                }
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_backpaths');
            }
            break;
        case 'dorecalccategories_backpaths':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                qa_db_categories_recalc_backpaths($next, $lastcategoryid);
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_complete');
            }
            break;
        case 'dodeletehidden':
            qa_recalc_transition($state, 'dodeletehidden_comments');
            break;
        case 'dodeletehidden_comments':
            $posts = qa_db_posts_get_for_deleting('C', $next, 1);
            if (count($posts)) {
                $postid = $posts[0];
                $oldcomment = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
                $parent = qa_db_single_select(qa_db_full_post_selectspec(null, $oldcomment['parentid']));
                if ($parent['basetype'] == 'Q') {
                    $question = $parent;
                    $answer = null;
                } else {
                    $question = qa_db_single_select(qa_db_full_post_selectspec(null, $parent['parentid']));
                    $answer = $parent;
                }
                qa_comment_delete($oldcomment, $question, $answer, null, null, null);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_answers');
            }
            break;
        case 'dodeletehidden_answers':
            $posts = qa_db_posts_get_for_deleting('A', $next, 1);
            if (count($posts)) {
                $postid = $posts[0];
                $oldanswer = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
                $question = qa_db_single_select(qa_db_full_post_selectspec(null, $oldanswer['parentid']));
                qa_answer_delete($oldanswer, $question, null, null, null);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_questions');
            }
            break;
        case 'dodeletehidden_questions':
            $posts = qa_db_posts_get_for_deleting('Q', $next, 1);
            if (count($posts)) {
                $postid = $posts[0];
                $oldquestion = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
                qa_question_delete($oldquestion, null, null, null);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_complete');
            }
            break;
        default:
            $state = '';
            break;
    }
    if ($continue) {
        $state = $operation . ',' . $length . ',' . $next . ',' . $done;
    }
    return $continue && $done < $length;
}
Exemple #13
0
function qa_post_check_full($postid)
{
    $post = qa_db_single_select(qa_db_full_post_selectspec(null, $postid));
    return $post;
}
function qa_question_set_hidden($oldquestion, $hidden, $userid, $handle, $cookieid, $answers, $commentsfollows, $closepost = null)
{
    require_once QA_INCLUDE_DIR . 'qa-app-format.php';
    require_once QA_INCLUDE_DIR . 'qa-app-updates.php';
    $wasqueued = $oldquestion['type'] == 'Q_QUEUED';
    qa_post_unindex($oldquestion['postid']);
    foreach ($answers as $answer) {
        qa_post_unindex($answer['postid']);
    }
    foreach ($commentsfollows as $comment) {
        if ($comment['basetype'] == 'C') {
            qa_post_unindex($comment['postid']);
        }
    }
    if (@$closepost['parentid'] == $oldquestion['postid']) {
        qa_post_unindex($closepost['postid']);
    }
    $setupdated = $hidden || !$wasqueued;
    // don't record approval of a post as an update action...
    qa_db_post_set_type($oldquestion['postid'], $hidden ? 'Q_HIDDEN' : 'Q', $setupdated ? $userid : null, $setupdated ? qa_remote_ip_address() : null, QA_UPDATE_VISIBLE);
    if (!$setupdated) {
        // ... for approval of a post, set created time to now instead
        qa_db_post_set_created($oldquestion['postid'], null);
        qa_db_hotness_update($oldquestion['postid']);
    }
    qa_db_category_path_qcount_update(qa_db_post_get_category_path($oldquestion['postid']));
    qa_db_points_update_ifuser($oldquestion['userid'], array('qposts', 'aselects'));
    qa_db_qcount_update();
    qa_db_unaqcount_update();
    qa_db_unselqcount_update();
    qa_db_unupaqcount_update();
    if (!$hidden) {
        qa_post_index($oldquestion['postid'], 'Q', $oldquestion['postid'], $oldquestion['parentid'], $oldquestion['title'], $oldquestion['content'], $oldquestion['format'], qa_viewer_text($oldquestion['content'], $oldquestion['format']), $oldquestion['tags'], $oldquestion['categoryid']);
        foreach ($answers as $answer) {
            if ($answer['type'] == 'A') {
                // even if question visible, don't index hidden or queued answers
                qa_post_index($answer['postid'], $answer['type'], $oldquestion['postid'], $answer['parentid'], null, $answer['content'], $answer['format'], qa_viewer_text($answer['content'], $answer['format']), null, $answer['categoryid']);
            }
        }
        foreach ($commentsfollows as $comment) {
            if ($comment['type'] == 'C') {
                $answer = @$answers[$comment['parentid']];
                if (!isset($answer) || $answer['type'] == 'A') {
                    // don't index comment if it or its parent is hidden
                    qa_post_index($comment['postid'], $comment['type'], $oldquestion['postid'], $comment['parentid'], null, $comment['content'], $comment['format'], qa_viewer_text($comment['content'], $comment['format']), null, $comment['categoryid']);
                }
            }
        }
        if ($closepost['parentid'] == $oldquestion['postid']) {
            qa_post_index($closepost['postid'], $closepost['type'], $oldquestion['postid'], $closepost['parentid'], null, $closepost['content'], $closepost['format'], qa_viewer_text($closepost['content'], $closepost['format']), null, $closepost['categoryid']);
        }
    }
    qa_report_event($wasqueued ? $hidden ? 'q_reject' : 'q_approve' : ($hidden ? 'q_hide' : 'q_reshow'), $userid, $handle, $cookieid, array('postid' => $oldquestion['postid'], 'oldquestion' => $oldquestion));
    if ($wasqueued && !$hidden) {
        require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
        require_once QA_INCLUDE_DIR . 'qa-util-string.php';
        qa_report_event('q_post', $oldquestion['userid'], $oldquestion['handle'], $oldquestion['cookieid'], array('postid' => $oldquestion['postid'], 'parentid' => $oldquestion['parentid'], 'parent' => isset($oldquestion['parentid']) ? qa_db_single_select(qa_db_full_post_selectspec(null, $oldquestion['parentid'])) : null, 'title' => $oldquestion['title'], 'content' => $oldquestion['content'], 'format' => $oldquestion['format'], 'text' => qa_viewer_text($oldquestion['content'], $oldquestion['format']), 'tags' => $oldquestion['tags'], 'categoryid' => $oldquestion['categoryid'], 'notify' => isset($oldquestion['notify']), 'email' => qa_email_validate($oldquestion['notify']) ? $oldquestion['notify'] : null, 'delayed' => $oldquestion['created']));
    }
}
Exemple #15
0
function qa_check_page_clicks()
{
    if (qa_to_override(__FUNCTION__)) {
        $args = func_get_args();
        return qa_call_override(__FUNCTION__, $args);
    }
    global $qa_page_error_html;
    if (qa_is_http_post()) {
        foreach ($_POST as $field => $value) {
            if (strpos($field, 'vote_') === 0) {
                // voting...
                @(list($dummy, $postid, $vote, $anchor) = explode('_', $field));
                if (isset($postid) && isset($vote)) {
                    if (!qa_check_form_security_code('vote', qa_post_text('code'))) {
                        $qa_page_error_html = qa_lang_html('misc/form_security_again');
                    } else {
                        require_once QA_INCLUDE_DIR . 'app/votes.php';
                        require_once QA_INCLUDE_DIR . 'db/selects.php';
                        $userid = qa_get_logged_in_userid();
                        $post = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $postid));
                        $qa_page_error_html = qa_vote_error_html($post, $vote, $userid, qa_request());
                        if (!$qa_page_error_html) {
                            qa_vote_set($post, $userid, qa_get_logged_in_handle(), qa_cookie_get(), $vote);
                            qa_redirect(qa_request(), $_GET, null, null, $anchor);
                        }
                        break;
                    }
                }
            } elseif (strpos($field, 'favorite_') === 0) {
                // favorites...
                @(list($dummy, $entitytype, $entityid, $favorite) = explode('_', $field));
                if (isset($entitytype) && isset($entityid) && isset($favorite)) {
                    if (!qa_check_form_security_code('favorite-' . $entitytype . '-' . $entityid, qa_post_text('code'))) {
                        $qa_page_error_html = qa_lang_html('misc/form_security_again');
                    } else {
                        require_once QA_INCLUDE_DIR . 'app/favorites.php';
                        qa_user_favorite_set(qa_get_logged_in_userid(), qa_get_logged_in_handle(), qa_cookie_get(), $entitytype, $entityid, $favorite);
                        qa_redirect(qa_request(), $_GET);
                    }
                }
            } elseif (strpos($field, 'notice_') === 0) {
                // notices...
                @(list($dummy, $noticeid) = explode('_', $field));
                if (isset($noticeid)) {
                    if (!qa_check_form_security_code('notice-' . $noticeid, qa_post_text('code'))) {
                        $qa_page_error_html = qa_lang_html('misc/form_security_again');
                    } else {
                        if ($noticeid == 'visitor') {
                            setcookie('qa_noticed', 1, time() + 86400 * 3650, '/', QA_COOKIE_DOMAIN);
                        } elseif ($noticeid == 'welcome') {
                            require_once QA_INCLUDE_DIR . 'db/users.php';
                            qa_db_user_set_flag(qa_get_logged_in_userid(), QA_USER_FLAGS_WELCOME_NOTICE, false);
                        } else {
                            require_once QA_INCLUDE_DIR . 'db/notices.php';
                            qa_db_usernotice_delete(qa_get_logged_in_userid(), $noticeid);
                        }
                        qa_redirect(qa_request(), $_GET);
                    }
                }
            }
        }
    }
}
Exemple #16
0
function qa_recalc_perform_step(&$state)
{
    $continue = false;
    @(list($operation, $length, $next, $done) = explode(',', $state));
    switch ($operation) {
        case 'doreindexcontent':
            qa_recalc_transition($state, 'doreindexcontent_pagereindex');
            break;
        case 'doreindexcontent_pagereindex':
            $pages = qa_db_pages_get_for_reindexing($next, 10);
            if (count($pages)) {
                require_once QA_INCLUDE_DIR . 'qa-app-format.php';
                $lastpageid = max(array_keys($pages));
                foreach ($pages as $pageid => $page) {
                    if (!($page['flags'] & QA_PAGE_FLAGS_EXTERNAL)) {
                        $searchmodules = qa_load_modules_with('search', 'unindex_page');
                        foreach ($searchmodules as $searchmodule) {
                            $searchmodule->unindex_page($pageid);
                        }
                        $searchmodules = qa_load_modules_with('search', 'index_page');
                        if (count($searchmodules)) {
                            $indextext = qa_viewer_text($page['content'], 'html');
                            foreach ($searchmodules as $searchmodule) {
                                $searchmodule->index_page($pageid, $page['tags'], $page['heading'], $page['content'], 'html', $indextext);
                            }
                        }
                    }
                }
                $next = 1 + $lastpageid;
                $done += count($pages);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'doreindexcontent_postcount');
            }
            break;
        case 'doreindexcontent_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'doreindexcontent_postreindex');
            break;
        case 'doreindexcontent_postreindex':
            $posts = qa_db_posts_get_for_reindexing($next, 10);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'qa-app-format.php';
                $lastpostid = max(array_keys($posts));
                qa_db_prepare_for_reindexing($next, $lastpostid);
                qa_suspend_update_counts();
                foreach ($posts as $postid => $post) {
                    qa_post_unindex($postid);
                    qa_post_index($postid, $post['type'], $post['questionid'], $post['parentid'], $post['title'], $post['content'], $post['format'], qa_viewer_text($post['content'], $post['format']), $post['tags'], $post['categoryid']);
                }
                $next = 1 + $lastpostid;
                $done += count($posts);
                $continue = true;
            } else {
                qa_db_truncate_indexes($next);
                qa_recalc_transition($state, 'doreindexposts_wordcount');
            }
            break;
        case 'doreindexposts_wordcount':
            $wordids = qa_db_words_prepare_for_recounting($next, 1000);
            if (count($wordids)) {
                $lastwordid = max($wordids);
                qa_db_words_recount($next, $lastwordid);
                $next = 1 + $lastwordid;
                $done += count($wordids);
                $continue = true;
            } else {
                qa_db_tagcount_update();
                // this is quick so just do it here
                qa_recalc_transition($state, 'doreindexposts_complete');
            }
            break;
        case 'dorecountposts':
            qa_recalc_transition($state, 'dorecountposts_postcount');
            break;
        case 'dorecountposts_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_db_unaqcount_update();
            qa_db_unselqcount_update();
            qa_recalc_transition($state, 'dorecountposts_votecount');
            break;
        case 'dorecountposts_votecount':
            $postids = qa_db_posts_get_for_recounting($next, 1000);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_votes_recount($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecountposts_acount');
            }
            break;
        case 'dorecountposts_acount':
            $postids = qa_db_posts_get_for_recounting($next, 1000);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_answers_recount($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_db_unupaqcount_update();
                qa_recalc_transition($state, 'dorecountposts_complete');
            }
            break;
        case 'dorecalcpoints':
            qa_recalc_transition($state, 'dorecalcpoints_usercount');
            break;
        case 'dorecalcpoints_usercount':
            qa_db_userpointscount_update();
            // for progress update - not necessarily accurate
            qa_recalc_transition($state, 'dorecalcpoints_recalc');
            break;
        case 'dorecalcpoints_recalc':
            $userids = qa_db_users_get_for_recalc_points($next, 10);
            if (count($userids)) {
                $lastuserid = max($userids);
                qa_db_users_recalc_points($next, $lastuserid);
                $next = 1 + $lastuserid;
                $done += count($userids);
                $continue = true;
            } else {
                qa_db_truncate_userpoints($next);
                qa_db_userpointscount_update();
                // quick so just do it here
                qa_recalc_transition($state, 'dorecalcpoints_complete');
            }
            break;
        case 'dorefillevents':
            qa_recalc_transition($state, 'dorefillevents_qcount');
            break;
        case 'dorefillevents_qcount':
            qa_db_qcount_update();
            qa_recalc_transition($state, 'dorefillevents_refill');
            break;
        case 'dorefillevents_refill':
            $questionids = qa_db_qs_get_for_event_refilling($next, 1);
            if (count($questionids)) {
                require_once QA_INCLUDE_DIR . 'qa-app-events.php';
                require_once QA_INCLUDE_DIR . 'qa-app-updates.php';
                require_once QA_INCLUDE_DIR . 'qa-util-sort.php';
                $lastquestionid = max($questionids);
                foreach ($questionids as $questionid) {
                    //	Retrieve all posts relating to this question
                    list($question, $childposts, $achildposts) = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $questionid), qa_db_full_child_posts_selectspec(null, $questionid), qa_db_full_a_child_posts_selectspec(null, $questionid));
                    //	Merge all posts while preserving keys as postids
                    $posts = array($questionid => $question);
                    foreach ($childposts as $postid => $post) {
                        $posts[$postid] = $post;
                    }
                    foreach ($achildposts as $postid => $post) {
                        $posts[$postid] = $post;
                    }
                    //	Creation and editing of each post
                    foreach ($posts as $postid => $post) {
                        $followonq = $post['basetype'] == 'Q' && $postid != $questionid;
                        qa_create_event_for_q_user($questionid, $postid, $followonq ? QA_UPDATE_FOLLOWS : null, $post['userid'], @$posts[$post['parentid']]['userid'], $post['created']);
                        if (isset($post['updated']) && !$followonq) {
                            qa_create_event_for_q_user($questionid, $postid, $post['updatetype'], $post['lastuserid'], $post['userid'], $post['updated']);
                        }
                    }
                    //	Tags and categories of question
                    qa_create_event_for_tags($question['tags'], $questionid, null, $question['userid'], $question['created']);
                    qa_create_event_for_category($question['categoryid'], $questionid, null, $question['userid'], $question['created']);
                    //	Collect comment threads
                    $parentidcomments = array();
                    foreach ($posts as $postid => $post) {
                        if ($post['basetype'] == 'C') {
                            $parentidcomments[$post['parentid']][$postid] = $post;
                        }
                    }
                    //	For each comment thread, notify all previous comment authors of each comment in the thread (could get slow)
                    foreach ($parentidcomments as $parentid => $comments) {
                        $keyuserids = array();
                        qa_sort_by($comments, 'created');
                        foreach ($comments as $comment) {
                            foreach ($keyuserids as $keyuserid => $dummy) {
                                if ($keyuserid != $comment['userid'] && $keyuserid != @$posts[$parentid]['userid']) {
                                    qa_db_event_create_not_entity($keyuserid, $questionid, $comment['postid'], null, $comment['userid'], $comment['created']);
                                }
                            }
                            if (isset($comment['userid'])) {
                                $keyuserids[$comment['userid']] = true;
                            }
                        }
                    }
                }
                $next = 1 + $lastquestionid;
                $done += count($questionids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorefillevents_complete');
            }
            break;
        case 'dorecalccategories':
            qa_recalc_transition($state, 'dorecalccategories_postcount');
            break;
        case 'dorecalccategories_postcount':
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'dorecalccategories_postupdate');
            break;
        case 'dorecalccategories_postupdate':
            $postids = qa_db_posts_get_for_recategorizing($next, 100);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_recalc_categoryid($next, $lastpostid);
                qa_db_posts_calc_category_path($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_recount');
            }
            break;
        case 'dorecalccategories_recount':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                foreach ($categoryids as $categoryid) {
                    qa_db_ifcategory_qcount_update($categoryid);
                }
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_backpaths');
            }
            break;
        case 'dorecalccategories_backpaths':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                qa_db_categories_recalc_backpaths($next, $lastcategoryid);
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_complete');
            }
            break;
        case 'dodeletehidden':
            qa_recalc_transition($state, 'dodeletehidden_comments');
            break;
        case 'dodeletehidden_comments':
            $posts = qa_db_posts_get_for_deleting('C', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_answers');
            }
            break;
        case 'dodeletehidden_answers':
            $posts = qa_db_posts_get_for_deleting('A', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_questions');
            }
            break;
        case 'dodeletehidden_questions':
            $posts = qa_db_posts_get_for_deleting('Q', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_complete');
            }
            break;
        default:
            $state = '';
            break;
    }
    if ($continue) {
        $state = $operation . ',' . $length . ',' . $next . ',' . $done;
    }
    return $continue && $done < $length;
}
Exemple #17
0
function qa_flag_set_tohide($oldpost, $userid, $handle, $cookieid, $question)
{
    if (qa_to_override(__FUNCTION__)) {
        $args = func_get_args();
        return qa_call_override(__FUNCTION__, $args);
    }
    require_once QA_INCLUDE_DIR . 'db/votes.php';
    require_once QA_INCLUDE_DIR . 'app/limits.php';
    require_once QA_INCLUDE_DIR . 'db/post-update.php';
    qa_db_userflag_set($oldpost['postid'], $userid, true);
    qa_db_post_recount_flags($oldpost['postid']);
    qa_db_flaggedcount_update();
    switch ($oldpost['basetype']) {
        case 'Q':
            $event = 'q_flag';
            break;
        case 'A':
            $event = 'a_flag';
            break;
        case 'C':
            $event = 'c_flag';
            break;
    }
    $post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $oldpost['postid']));
    qa_report_event($event, $userid, $handle, $cookieid, array('postid' => $oldpost['postid'], 'oldpost' => $oldpost, 'flagcount' => $post['flagcount'], 'questionid' => $question['postid'], 'question' => $question));
    return $post['flagcount'] >= qa_opt('flagging_hide_after') && !$post['hidden'];
}
Exemple #18
0
function ra_user_badges_list($userid)
{
    if (!qa_opt('badge_active')) {
        return;
    }
    $handles = qa_userids_to_handles(array($userid));
    $handle = $handles[$userid];
    // displays badge list in user profile
    $result = qa_db_read_all_assoc(qa_db_query_sub('SELECT badge_slug as slug, object_id AS oid FROM ^userbadges WHERE user_id=#', $userid));
    if (count($result) > 0) {
        // count badges
        $bin = qa_get_badge_list();
        $badges = array();
        foreach ($result as $info) {
            $slug = $info['slug'];
            $type = $bin[$slug]['type'];
            if (isset($badges[$type][$slug])) {
                $badges[$type][$slug]['count']++;
            } else {
                $badges[$type][$slug]['count'] = 1;
            }
            if ($info['oid']) {
                $badges[$type][$slug]['oid'][] = $info['oid'];
            }
        }
        foreach ($badges as $type => $badge) {
            $typea = qa_get_badge_type($type);
            $types = $typea['slug'];
            $typed = $typea['name'];
            $output = '';
            //$output = '<h3 class="badge-title" title="'.qa_lang('badges/'.$types.'_desc').'">'.$typed.'</h3>';
            foreach ($badge as $slug => $info) {
                $badge_name = qa_badge_name($slug);
                if (!qa_opt('badge_' . $slug . '_name')) {
                    qa_opt('badge_' . $slug . '_name', $badge_name);
                }
                $name = qa_opt('badge_' . $slug . '_name');
                $count = $info['count'];
                if (qa_opt('badge_show_source_posts')) {
                    $oids = @$info['oid'];
                } else {
                    $oids = null;
                }
                $var = qa_opt('badge_' . $slug . '_var');
                $desc = qa_badge_desc_replace($slug, $var, false);
                // badge row
                $output .= '<div class="badge-container-badge">
									<span class="user-badge ' . $types . ' icon-badge" title="' . $desc . ' (' . $typed . ')">' . qa_html($name) . '</span>&nbsp;<span class="badge-count">x&nbsp;' . $count . '</span>' . (is_array($oids) ? '<i class="icon-chevron-down"></i>' : '');
                // source row(s) if any
                if (is_array($oids)) {
                    $output .= '
								<div class="badge-sources ' . $slug . '"><ul>';
                    foreach ($oids as $oid) {
                        $post = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $oid));
                        $title = $post['title'];
                        $anchor = '';
                        if ($post['parentid']) {
                            $anchor = urlencode(qa_anchor($post['type'], $oid));
                            $oid = $post['parentid'];
                            $title = qa_db_read_one_value(qa_db_query_sub('SELECT BINARY title as title FROM ^posts WHERE postid=#', $oid), true);
                        }
                        //$length = 30;
                        $text = $title;
                        $output .= '<li><a href="' . qa_path_html(qa_q_request($oid, $title), NULL, qa_opt('site_url')) . ($anchor ? '#' . $anchor : '') . '">' . qa_html($text) . '</a></li>';
                    }
                    $output .= '</ul></div>';
                }
                $output .= '</div>';
            }
            $outa[] = $output;
        }
        $fields = '<div class="badge-user-block widget"><h3 class="widget-title">' . _ra_lang('Badges') . '</h3><div class="widget-inner">' . implode('', $outa) . '</div></div>';
    }
    return $fields;
}
    header('Location: ../');
    exit;
}
require_once QA_INCLUDE_DIR . 'app/cookies.php';
require_once QA_INCLUDE_DIR . 'app/format.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
require_once QA_INCLUDE_DIR . 'util/sort.php';
require_once QA_INCLUDE_DIR . 'util/string.php';
require_once QA_INCLUDE_DIR . 'app/captcha.php';
require_once QA_INCLUDE_DIR . 'pages/question-view.php';
require_once QA_INCLUDE_DIR . 'app/updates.php';
$questionid = qa_request_part(0);
$userid = qa_get_logged_in_userid();
$cookieid = qa_cookie_get();
//	Get information about this question
list($question, $childposts, $achildposts, $parentquestion, $closepost, $extravalue, $categories, $favorite) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid), qa_db_full_child_posts_selectspec($userid, $questionid), qa_db_full_a_child_posts_selectspec($userid, $questionid), qa_db_post_parent_q_selectspec($questionid), qa_db_post_close_post_selectspec($questionid), qa_db_post_meta_selectspec($questionid, 'qa_q_extra'), qa_db_category_nav_selectspec($questionid, true, true, true), isset($userid) ? qa_db_is_favorite_selectspec($userid, QA_ENTITY_QUESTION, $questionid) : null);
if ($question['basetype'] != 'Q') {
    // don't allow direct viewing of other types of post
    $question = null;
}
if (isset($question)) {
    $question['extra'] = $extravalue;
    $answers = qa_page_q_load_as($question, $childposts);
    $commentsfollows = qa_page_q_load_c_follows($question, $childposts, $achildposts);
    $question = $question + qa_page_q_post_rules($question, null, null, $childposts);
    // array union
    if ($question['selchildid'] && @$answers[$question['selchildid']]['type'] != 'A') {
        $question['selchildid'] = null;
    }
    // if selected answer is hidden or somehow not there, consider it not selected
    foreach ($answers as $key => $answer) {
 function ajaxCommentCreate($parent, $cid)
 {
     global $qa_login_userid, $qa_cookieid, $usershtml, $formtype, $formpostid, $formrequested;
     $comment = qa_db_single_select(qa_db_full_post_selectspec(null, $cid));
     $htmloptions = qa_post_html_defaults('C', true);
     $htmloptions['avatarsize'] = qa_opt('avatar_q_page_c_size');
     $c_view = qa_post_html_fields($comment, $qa_login_userid, $qa_cookieid, $usershtml, null, $htmloptions);
     //	Buttons for operating on this comment
     $c_view['form'] = array('style' => 'light', 'buttons' => array());
     $c_view['form']['buttons']['edit'] = array('tags' => 'NAME="doeditc_' . qa_html($cid) . '"', 'label' => qa_lang_html('question/edit_button'), 'popup' => qa_lang_html('question/edit_c_popup'));
     $c_view['form']['buttons']['hide'] = array('tags' => 'NAME="dohidec_' . qa_html($cid) . '"', 'label' => qa_lang_html('question/hide_button'), 'popup' => qa_lang_html('question/hide_c_popup'));
     $comment['claimable'] = !isset($comment['userid']) && isset($qa_login_userid) && strcmp(@$comment['cookieid'], $qa_cookieid) == 0 && !$permiterror_post_c;
     if ($comment['claimable']) {
         $c_view['form']['buttons']['claim'] = array('tags' => 'NAME="doclaimc_' . qa_html($cid) . '"', 'label' => qa_lang_html('question/claim_button'));
     }
     $parent['commentbutton'] = qa_user_permit_error('permit_post_c') != 'level' && qa_opt($comment['type'] == 'Q' ? 'comment_on_qs' : 'comment_on_as');
     if ($parent['commentbutton'] && qa_opt('show_c_reply_buttons') && !$comment['hidden']) {
         $c_view['form']['buttons']['comment'] = array('tags' => 'NAME="' . ($parent['basetype'] == 'Q' ? 'docommentq' : 'docommenta_' . qa_html($parent['postid'])) . '"', 'label' => qa_lang_html('question/reply_button'), 'popup' => qa_lang_html('question/reply_c_popup'));
     }
     return @$c_view;
 }
require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
require_once QA_INCLUDE_DIR . 'qa-page-question-view.php';
require_once QA_INCLUDE_DIR . 'qa-page-question-submit.php';
//	Load relevant information about this answer
$answerid = qa_post_text('answerid');
$questionid = qa_post_text('questionid');
$userid = qa_get_logged_in_userid();
list($answer, $question, $qchildposts, $achildposts) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $answerid), qa_db_full_post_selectspec($userid, $questionid), qa_db_full_child_posts_selectspec($userid, $questionid), qa_db_full_child_posts_selectspec($userid, $answerid));
//	Check if there was an operation that succeeded
if (@$answer['basetype'] == 'A' && @$question['basetype'] == 'Q') {
    $answers = qa_page_q_load_as($question, $qchildposts);
    $question = $question + qa_page_q_post_rules($question, null, null, $qchildposts);
    // array union
    $answer = $answer + qa_page_q_post_rules($answer, $question, $qchildposts, $achildposts);
    if (qa_page_q_single_click_a($answer, $question, $answers, $achildposts, false, $error)) {
        list($answer, $question) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $answerid), qa_db_full_post_selectspec($userid, $questionid));
        //	If so, page content to be updated via Ajax
        echo "QA_AJAX_RESPONSE\n1\n";
        //	Send back new count of answers
        $countanswers = $question['acount'];
        if ($countanswers == 1) {
            echo qa_lang_html('question/1_answer_title');
        } else {
            echo qa_lang_html_sub('question/x_answers_title', $countanswers);
        }
        //	If the answer was not deleted....
        if (isset($answer)) {
            $question = $question + qa_page_q_post_rules($question, null, null, $qchildposts);
            // array union
            $answer = $answer + qa_page_q_post_rules($answer, $question, $qchildposts, $achildposts);
            foreach ($achildposts as $key => $achildpost) {
function qa_page_q_load_q()
{
    global $qa_login_userid, $questionid, $question, $parentquestion, $answers, $commentsfollows, $relatedcount, $relatedquestions, $question, $categories;
    list($question, $childposts, $achildposts, $parentquestion, $relatedquestions, $categories) = qa_db_select_with_pending(qa_db_full_post_selectspec($qa_login_userid, $questionid), qa_db_full_child_posts_selectspec($qa_login_userid, $questionid), qa_db_full_a_child_posts_selectspec($qa_login_userid, $questionid), qa_db_post_parent_q_selectspec($questionid), qa_db_related_qs_selectspec($qa_login_userid, $questionid), qa_db_category_nav_selectspec($questionid, true, true));
    if ($question['basetype'] != 'Q') {
        // don't allow direct viewing of other types of post
        $question = null;
    }
    $answers = array();
    $commentsfollows = array();
    foreach ($childposts as $postid => $post) {
        switch ($post['type']) {
            case 'Q':
                // never show follow-on Qs which have been hidden, even to admins
            // never show follow-on Qs which have been hidden, even to admins
            case 'C':
            case 'C_HIDDEN':
                $commentsfollows[$postid] = $post;
                break;
            case 'A':
            case 'A_HIDDEN':
                $answers[$postid] = $post;
                break;
        }
    }
    foreach ($achildposts as $postid => $post) {
        switch ($post['type']) {
            case 'Q':
            case 'Q_HIDDEN':
            case 'C':
            case 'C_HIDDEN':
                $commentsfollows[$postid] = $post;
                break;
        }
    }
    if (isset($question)) {
        $relatedcount = qa_opt('do_related_qs') ? 1 + qa_opt('page_size_related_qs') : 0;
        $relatedquestions = array_slice($relatedquestions, 0, $relatedcount);
        // includes question itself at this point
        qa_page_q_post_rules($question);
        if ($question['selchildid'] && @$answers[$question['selchildid']]['type'] != 'A') {
            $question['selchildid'] = null;
        }
        // if selected answer is hidden or somehow not there, consider it not selected
        foreach ($answers as $key => $answer) {
            $question['deleteable'] = false;
            qa_page_q_post_rules($answers[$key]);
            if ($answers[$key]['isbyuser'] && !qa_opt('allow_multi_answers')) {
                $question['answerbutton'] = false;
            }
            $answers[$key]['isselected'] = $answer['postid'] == $question['selchildid'];
        }
        foreach ($commentsfollows as $key => $commentfollow) {
            if ($commentfollow['parentid'] == $questionid) {
                $question['deleteable'] = false;
            }
            if (isset($answers[$commentfollow['parentid']])) {
                $answers[$commentfollow['parentid']]['deleteable'] = false;
            }
            qa_page_q_post_rules($commentsfollows[$key]);
        }
    }
}
 require_once QA_INCLUDE_DIR . 'qa-app-cookies.php';
 require_once QA_INCLUDE_DIR . 'qa-page-question-view.php';
 require_once QA_INCLUDE_DIR . 'qa-page-question-submit.php';
 //	Load relevant information about this question and check it exists
 $usecaptcha = qa_user_use_captcha();
 $questionid = qa_post_text('a_questionid');
 $userid = qa_get_logged_in_userid();
 list($question, $childposts) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid), qa_db_full_child_posts_selectspec($userid, $questionid));
 if (@$question['basetype'] == 'Q' && !isset($question['closedbyid'])) {
     $answers = qa_page_q_load_as($question, $childposts);
     //	Try to create the new answer
     $answerid = qa_page_q_add_a_submit($question, $answers, $usecaptcha, $in, $errors);
     $countanswers = $question['acount'] + 1;
     if (isset($answerid)) {
         //	If successful, page content will be updated via Ajax
         $answer = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $answerid));
         $question = $question + qa_page_q_post_rules($question, null, null, $childposts);
         // array union
         $answer = $answer + qa_page_q_post_rules($answer, $question, $answers, null);
         $usershtml = qa_userids_handles_html(array($answer), true);
         $a_view = qa_page_q_answer_view($question, $answer, false, $usershtml, false);
         $themeclass = qa_load_theme_class(qa_get_site_theme(), 'ajax-answer', null, null);
         echo "QA_AJAX_RESPONSE\n1\n";
         //	Send back whether the 'answer' button should still be visible
         echo (int) qa_opt('allow_multi_answers') . "\n";
         //	Send back the count of answers
         if ($countanswers == 1) {
             echo qa_lang_html('question/1_answer_title') . "\n";
         } else {
             echo qa_lang_html_sub('question/x_answers_title', $countanswers) . "\n";
         }
Exemple #24
0
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/
require_once QA_INCLUDE_DIR . 'app/users.php';
require_once QA_INCLUDE_DIR . 'app/limits.php';
require_once QA_INCLUDE_DIR . 'db/selects.php';
//	Load relevant information about this question and the comment parent
$questionid = qa_post_text('c_questionid');
$parentid = qa_post_text('c_parentid');
$userid = qa_get_logged_in_userid();
list($question, $parent, $children) = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid), qa_db_full_post_selectspec($userid, $parentid), qa_db_full_child_posts_selectspec($userid, $parentid));
//	Check if the question and parent exist, and whether the user has permission to do this
if (@$question['basetype'] == 'Q' && (@$parent['basetype'] == 'Q' || @$parent['basetype'] == 'A') && !qa_user_post_permit_error('permit_post_c', $parent, QA_LIMIT_COMMENTS)) {
    require_once QA_INCLUDE_DIR . 'app/captcha.php';
    require_once QA_INCLUDE_DIR . 'app/format.php';
    require_once QA_INCLUDE_DIR . 'app/post-create.php';
    require_once QA_INCLUDE_DIR . 'app/cookies.php';
    require_once QA_INCLUDE_DIR . 'pages/question-view.php';
    require_once QA_INCLUDE_DIR . 'pages/question-submit.php';
    require_once QA_INCLUDE_DIR . 'util/sort.php';
    //	Try to create the new comment
    $usecaptcha = qa_user_use_captcha(qa_user_level_for_post($question));
    $commentid = qa_page_q_add_c_submit($question, $parent, $children, $usecaptcha, $in, $errors);
    //	If successful, page content will be updated via Ajax
    if (isset($commentid)) {
        $children = qa_db_select_with_pending(qa_db_full_child_posts_selectspec($userid, $parentid));
 /**
  * Outputs cache to the user
  */
 private function get_cache()
 {
     qa_report_process_stage('init_page');
     qa_db_connect('qa_page_db_fail_handler');
     qa_page_queue_pending();
     qa_load_state();
     qa_check_login_modules();
     if (QA_DEBUG_PERFORMANCE) {
         if (qa_qa_version_below('1.7')) {
             qa_usage_mark('setup');
         } else {
             //global $qa_usage;
             //$qa_usage->mark('setup');
             null;
         }
     }
     qa_check_page_clicks();
     qa_set_form_security_key();
     if (!QA_CACHING_FILE) {
         $contents = $this->get_cache_db();
     } else {
         $contents = $this->get_cache_file();
     }
     $qa_content = array();
     // Dummy contents
     $userid = qa_get_logged_in_userid();
     $questionid = qa_request_part(0);
     $cookieid = qa_cookie_get(true);
     if (is_numeric($questionid)) {
         $question = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid));
         if (is_numeric($questionid) && qa_opt('do_count_q_views') && !preg_match("/^(?:POST|PUT)\$/i", $_SERVER["REQUEST_METHOD"]) && !qa_is_http_post() && qa_is_human_probably() && (!$question['views'] || ($question['lastviewip'] != qa_remote_ip_address() || !isset($question['lastviewip'])) && ($question['createip'] != qa_remote_ip_address() || !isset($question['createip'])) && ($question['userid'] != $userid || !isset($question['userid'])) && ($question['cookieid'] != $cookieid || !isset($question['cookieid'])))) {
             $qa_content['inc_views_postid'] = $questionid;
         } else {
             $qa_content['inc_views_postid'] = null;
         }
         qa_do_content_stats($qa_content);
     }
     if (QA_DEBUG_PERFORMANCE) {
         ob_start();
         if (qa_qa_version_below('1.7')) {
             qa_usage_output();
         } else {
             global $qa_usage;
             $qa_usage->output();
         }
         $contents .= ob_get_contents();
         ob_end_clean();
     }
     qa_db_disconnect();
     header('Content-type: ' . strtr('^type/^format; charset=utf-8', array('^type' => 'text', '^format' => $this->get_cache_file_extension())));
     exit($contents);
 }
require_once QA_INCLUDE_DIR . 'qa-app-users.php';
require_once QA_INCLUDE_DIR . 'qa-app-cookies.php';
require_once QA_INCLUDE_DIR . 'qa-app-votes.php';
require_once QA_INCLUDE_DIR . 'qa-app-format.php';
require_once QA_INCLUDE_DIR . 'qa-app-options.php';
require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
function qa_ajax_vote_db_fail_handler()
{
    echo "QA_AJAX_RESPONSE\n0\nA database error occurred.";
    exit;
}
qa_base_db_connect('qa_ajax_vote_db_fail_handler');
$postid = qa_post_text('postid');
$qa_login_userid = qa_get_logged_in_userid();
$qa_cookieid = qa_cookie_get();
$post = qa_db_select_with_pending(qa_db_full_post_selectspec($qa_login_userid, $postid));
$voteerror = qa_vote_error_html($post, $qa_login_userid, $qa_request);
if ($voteerror === false) {
    qa_vote_set($post, $qa_login_userid, qa_get_logged_in_handle(), $qa_cookieid, qa_post_text('vote'));
    $post = qa_db_select_with_pending(qa_db_full_post_selectspec($qa_login_userid, $postid));
    $fields = qa_post_html_fields($post, $qa_login_userid, $qa_cookieid, array(), null, array('voteview' => qa_opt('votes_separated') ? 'updown' : 'net'));
    $themeclass = qa_load_theme_class(qa_opt('site_theme'), 'voting', null, null);
    echo "QA_AJAX_RESPONSE\n1\n";
    $themeclass->voting_inner_html($fields);
} else {
    echo "QA_AJAX_RESPONSE\n0\n" . $voteerror;
}
qa_base_db_disconnect();
/*
	Omit PHP closing tag to help avoid accidental output
*/
function qa_recalc_perform_step(&$state)
{
    $continue = false;
    @(list($operation, $length, $next, $done) = explode("\t", $state));
    switch ($operation) {
        case 'doreindexcontent':
            qa_recalc_transition($state, 'doreindexcontent_pagereindex');
            break;
        case 'doreindexcontent_pagereindex':
            $pages = qa_db_pages_get_for_reindexing($next, 10);
            if (count($pages)) {
                require_once QA_INCLUDE_DIR . 'app/format.php';
                $lastpageid = max(array_keys($pages));
                foreach ($pages as $pageid => $page) {
                    if (!($page['flags'] & QA_PAGE_FLAGS_EXTERNAL)) {
                        $searchmodules = qa_load_modules_with('search', 'unindex_page');
                        foreach ($searchmodules as $searchmodule) {
                            $searchmodule->unindex_page($pageid);
                        }
                        $searchmodules = qa_load_modules_with('search', 'index_page');
                        if (count($searchmodules)) {
                            $indextext = qa_viewer_text($page['content'], 'html');
                            foreach ($searchmodules as $searchmodule) {
                                $searchmodule->index_page($pageid, $page['tags'], $page['heading'], $page['content'], 'html', $indextext);
                            }
                        }
                    }
                }
                $next = 1 + $lastpageid;
                $done += count($pages);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'doreindexcontent_postcount');
            }
            break;
        case 'doreindexcontent_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'doreindexcontent_postreindex');
            break;
        case 'doreindexcontent_postreindex':
            $posts = qa_db_posts_get_for_reindexing($next, 10);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'app/format.php';
                $lastpostid = max(array_keys($posts));
                qa_db_prepare_for_reindexing($next, $lastpostid);
                qa_suspend_update_counts();
                foreach ($posts as $postid => $post) {
                    qa_post_unindex($postid);
                    qa_post_index($postid, $post['type'], $post['questionid'], $post['parentid'], $post['title'], $post['content'], $post['format'], qa_viewer_text($post['content'], $post['format']), $post['tags'], $post['categoryid']);
                }
                $next = 1 + $lastpostid;
                $done += count($posts);
                $continue = true;
            } else {
                qa_db_truncate_indexes($next);
                qa_recalc_transition($state, 'doreindexposts_wordcount');
            }
            break;
        case 'doreindexposts_wordcount':
            $wordids = qa_db_words_prepare_for_recounting($next, 1000);
            if (count($wordids)) {
                $lastwordid = max($wordids);
                qa_db_words_recount($next, $lastwordid);
                $next = 1 + $lastwordid;
                $done += count($wordids);
                $continue = true;
            } else {
                qa_db_tagcount_update();
                // this is quick so just do it here
                qa_recalc_transition($state, 'doreindexposts_complete');
            }
            break;
        case 'dorecountposts':
            qa_recalc_transition($state, 'dorecountposts_postcount');
            break;
        case 'dorecountposts_postcount':
            qa_db_qcount_update();
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_db_unaqcount_update();
            qa_db_unselqcount_update();
            qa_recalc_transition($state, 'dorecountposts_votecount');
            break;
        case 'dorecountposts_votecount':
            $postids = qa_db_posts_get_for_recounting($next, 1000);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_votes_recount($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecountposts_acount');
            }
            break;
        case 'dorecountposts_acount':
            $postids = qa_db_posts_get_for_recounting($next, 1000);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_answers_recount($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_db_unupaqcount_update();
                qa_recalc_transition($state, 'dorecountposts_complete');
            }
            break;
        case 'dorecalcpoints':
            qa_recalc_transition($state, 'dorecalcpoints_usercount');
            break;
        case 'dorecalcpoints_usercount':
            qa_db_userpointscount_update();
            // for progress update - not necessarily accurate
            qa_db_uapprovecount_update();
            // needs to be somewhere and this is the most appropriate place
            qa_recalc_transition($state, 'dorecalcpoints_recalc');
            break;
        case 'dorecalcpoints_recalc':
            $recalccount = 10;
            $userids = qa_db_users_get_for_recalc_points($next, $recalccount + 1);
            // get one extra so we know where to start from next
            $gotcount = count($userids);
            $recalccount = min($recalccount, $gotcount);
            // can't recalc more than we got
            if ($recalccount > 0) {
                $lastuserid = $userids[$recalccount - 1];
                qa_db_users_recalc_points($next, $lastuserid);
                $done += $recalccount;
            } else {
                $lastuserid = $next;
            }
            // for truncation
            if ($gotcount > $recalccount) {
                // more left to do
                $next = $userids[$recalccount];
                // start next round at first one not recalculated
                $continue = true;
            } else {
                qa_db_truncate_userpoints($lastuserid);
                qa_db_userpointscount_update();
                // quick so just do it here
                qa_recalc_transition($state, 'dorecalcpoints_complete');
            }
            break;
        case 'dorefillevents':
            qa_recalc_transition($state, 'dorefillevents_qcount');
            break;
        case 'dorefillevents_qcount':
            qa_db_qcount_update();
            qa_recalc_transition($state, 'dorefillevents_refill');
            break;
        case 'dorefillevents_refill':
            $questionids = qa_db_qs_get_for_event_refilling($next, 1);
            if (count($questionids)) {
                require_once QA_INCLUDE_DIR . 'app/events.php';
                require_once QA_INCLUDE_DIR . 'app/updates.php';
                require_once QA_INCLUDE_DIR . 'util/sort.php';
                $lastquestionid = max($questionids);
                foreach ($questionids as $questionid) {
                    //	Retrieve all posts relating to this question
                    list($question, $childposts, $achildposts) = qa_db_select_with_pending(qa_db_full_post_selectspec(null, $questionid), qa_db_full_child_posts_selectspec(null, $questionid), qa_db_full_a_child_posts_selectspec(null, $questionid));
                    //	Merge all posts while preserving keys as postids
                    $posts = array($questionid => $question);
                    foreach ($childposts as $postid => $post) {
                        $posts[$postid] = $post;
                    }
                    foreach ($achildposts as $postid => $post) {
                        $posts[$postid] = $post;
                    }
                    //	Creation and editing of each post
                    foreach ($posts as $postid => $post) {
                        $followonq = $post['basetype'] == 'Q' && $postid != $questionid;
                        if ($followonq) {
                            $updatetype = QA_UPDATE_FOLLOWS;
                        } elseif ($post['basetype'] == 'C' && @$posts[$post['parentid']]['basetype'] == 'Q') {
                            $updatetype = QA_UPDATE_C_FOR_Q;
                        } elseif ($post['basetype'] == 'C' && @$posts[$post['parentid']]['basetype'] == 'A') {
                            $updatetype = QA_UPDATE_C_FOR_A;
                        } else {
                            $updatetype = null;
                        }
                        qa_create_event_for_q_user($questionid, $postid, $updatetype, $post['userid'], @$posts[$post['parentid']]['userid'], $post['created']);
                        if (isset($post['updated']) && !$followonq) {
                            qa_create_event_for_q_user($questionid, $postid, $post['updatetype'], $post['lastuserid'], $post['userid'], $post['updated']);
                        }
                    }
                    //	Tags and categories of question
                    qa_create_event_for_tags($question['tags'], $questionid, null, $question['userid'], $question['created']);
                    qa_create_event_for_category($question['categoryid'], $questionid, null, $question['userid'], $question['created']);
                    //	Collect comment threads
                    $parentidcomments = array();
                    foreach ($posts as $postid => $post) {
                        if ($post['basetype'] == 'C') {
                            $parentidcomments[$post['parentid']][$postid] = $post;
                        }
                    }
                    //	For each comment thread, notify all previous comment authors of each comment in the thread (could get slow)
                    foreach ($parentidcomments as $parentid => $comments) {
                        $keyuserids = array();
                        qa_sort_by($comments, 'created');
                        foreach ($comments as $comment) {
                            foreach ($keyuserids as $keyuserid => $dummy) {
                                if ($keyuserid != $comment['userid'] && $keyuserid != @$posts[$parentid]['userid']) {
                                    qa_db_event_create_not_entity($keyuserid, $questionid, $comment['postid'], QA_UPDATE_FOLLOWS, $comment['userid'], $comment['created']);
                                }
                            }
                            if (isset($comment['userid'])) {
                                $keyuserids[$comment['userid']] = true;
                            }
                        }
                    }
                }
                $next = 1 + $lastquestionid;
                $done += count($questionids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorefillevents_complete');
            }
            break;
        case 'dorecalccategories':
            qa_recalc_transition($state, 'dorecalccategories_postcount');
            break;
        case 'dorecalccategories_postcount':
            qa_db_acount_update();
            qa_db_ccount_update();
            qa_recalc_transition($state, 'dorecalccategories_postupdate');
            break;
        case 'dorecalccategories_postupdate':
            $postids = qa_db_posts_get_for_recategorizing($next, 100);
            if (count($postids)) {
                $lastpostid = max($postids);
                qa_db_posts_recalc_categoryid($next, $lastpostid);
                qa_db_posts_calc_category_path($next, $lastpostid);
                $next = 1 + $lastpostid;
                $done += count($postids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_recount');
            }
            break;
        case 'dorecalccategories_recount':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                foreach ($categoryids as $categoryid) {
                    qa_db_ifcategory_qcount_update($categoryid);
                }
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_backpaths');
            }
            break;
        case 'dorecalccategories_backpaths':
            $categoryids = qa_db_categories_get_for_recalcs($next, 10);
            if (count($categoryids)) {
                $lastcategoryid = max($categoryids);
                qa_db_categories_recalc_backpaths($next, $lastcategoryid);
                $next = 1 + $lastcategoryid;
                $done += count($categoryids);
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dorecalccategories_complete');
            }
            break;
        case 'dodeletehidden':
            qa_recalc_transition($state, 'dodeletehidden_comments');
            break;
        case 'dodeletehidden_comments':
            $posts = qa_db_posts_get_for_deleting('C', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'app/posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_answers');
            }
            break;
        case 'dodeletehidden_answers':
            $posts = qa_db_posts_get_for_deleting('A', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'app/posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_questions');
            }
            break;
        case 'dodeletehidden_questions':
            $posts = qa_db_posts_get_for_deleting('Q', $next, 1);
            if (count($posts)) {
                require_once QA_INCLUDE_DIR . 'app/posts.php';
                $postid = $posts[0];
                qa_post_delete($postid);
                $next = 1 + $postid;
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'dodeletehidden_complete');
            }
            break;
        case 'doblobstodisk':
            qa_recalc_transition($state, 'doblobstodisk_move');
            break;
        case 'doblobstodisk_move':
            $blob = qa_db_get_next_blob_in_db($next);
            if (isset($blob)) {
                require_once QA_INCLUDE_DIR . 'app/blobs.php';
                require_once QA_INCLUDE_DIR . 'db/blobs.php';
                if (qa_write_blob_file($blob['blobid'], $blob['content'], $blob['format'])) {
                    qa_db_blob_set_content($blob['blobid'], null);
                }
                $next = 1 + $blob['blobid'];
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'doblobstodisk_complete');
            }
            break;
        case 'doblobstodb':
            qa_recalc_transition($state, 'doblobstodb_move');
            break;
        case 'doblobstodb_move':
            $blob = qa_db_get_next_blob_on_disk($next);
            if (isset($blob)) {
                require_once QA_INCLUDE_DIR . 'app/blobs.php';
                require_once QA_INCLUDE_DIR . 'db/blobs.php';
                $content = qa_read_blob_file($blob['blobid'], $blob['format']);
                qa_db_blob_set_content($blob['blobid'], $content);
                qa_delete_blob_file($blob['blobid'], $blob['format']);
                $next = 1 + $blob['blobid'];
                $done++;
                $continue = true;
            } else {
                qa_recalc_transition($state, 'doblobstodb_complete');
            }
            break;
        default:
            $state = '';
            break;
    }
    if ($continue) {
        $state = $operation . "\t" . $length . "\t" . $next . "\t" . $done;
    }
    return $continue && $done < $length;
}
 function do_flag($data, $post)
 {
     $questionid = (int) @$data['action_data']['question_id'];
     $postid = (int) @$data['action_id'];
     $userid = qa_get_logged_in_userid();
     $handle = qa_get_logged_in_handle();
     $cookieid = isset($userid) ? qa_cookie_get() : qa_cookie_get_create();
     // create a new cookie if necessary
     if ($questionid === null || $postid === null) {
         return false;
     }
     if ($questionid != $postid) {
         $question = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $questionid));
     } else {
         $question = $post;
     }
     require_once QA_INCLUDE_DIR . 'qa-app-votes.php';
     $error = qa_flag_error_html($post, $userid, "");
     if (!$error) {
         // allowed
         if (qa_flag_set_tohide($post, $userid, $handle, $cookieid, $question)) {
             qa_post_set_hidden($postid, true);
         }
         return true;
     }
     return false;
 }
    header('Location: ../');
    exit;
}
require_once QA_INCLUDE_DIR . 'qa-app-format.php';
require_once QA_INCLUDE_DIR . 'qa-app-limits.php';
require_once QA_INCLUDE_DIR . 'qa-db-selects.php';
require_once QA_INCLUDE_DIR . 'qa-util-sort.php';
//	Check whether this is a follow-on question and get some info we need from the database
$in = array();
$followpostid = qa_get('follow');
$in['categoryid'] = qa_get_category_field_value('category');
if (!isset($in['categoryid'])) {
    $in['categoryid'] = qa_get('cat');
}
$userid = qa_get_logged_in_userid();
list($categories, $followanswer, $completetags) = qa_db_select_with_pending(qa_db_category_nav_selectspec($in['categoryid'], true), isset($followpostid) ? qa_db_full_post_selectspec($userid, $followpostid) : null, qa_db_popular_tags_selectspec(0, QA_DB_RETRIEVE_COMPLETE_TAGS));
if (!isset($categories[$in['categoryid']])) {
    $in['categoryid'] = null;
}
if (@$followanswer['basetype'] != 'A') {
    $followanswer = null;
}
//	Check for permission error
$permiterror = qa_user_maximum_permit_error('permit_post_q', QA_LIMIT_QUESTIONS);
if ($permiterror) {
    $qa_content = qa_content_prepare();
    // The 'approve', 'login', 'confirm', 'limit', 'userblock', 'ipblock' permission errors are reported to the user here
    // The other option ('level') prevents the menu option being shown, in qa_content_prepare(...)
    switch ($permiterror) {
        case 'login':
            $qa_content['error'] = qa_insert_login_links(qa_lang_html('question/ask_must_login'), qa_request(), isset($followpostid) ? array('follow' => $followpostid) : null);
    function process_request($request)
    {
        // we received post data, it is the ajax call!
        $transferString = qa_post_text('ajax');
        if ($transferString !== null) {
            // prevent empty userid
            $userid = qa_get_logged_in_userid();
            if (empty($userid)) {
                echo 'Userid is empty!';
                return;
            }
            // this is echoed via ajax success data
            $notifyBoxEvents = '';
            // ajax return all user events
            if (isset($userid) && $transferString == 'receiveNotify') {
                $last_visit = qa_db_read_one_value(qa_db_query_sub('SELECT UNIX_TIMESTAMP(meta_value) FROM ^usermeta WHERE user_id=# AND meta_key="visited_profile"', $userid), true);
                $maxEvents = qa_opt('q2apro_onsitenotifications_maxevshow');
                // maximal events to show
                // query all new events of user
                $event_query = qa_db_query_sub('SELECT 
							e.event, 
							e.userid, 
							BINARY e.params as params, 
							UNIX_TIMESTAMP(e.datetime) AS datetime
						FROM 
							^eventlog AS e
						WHERE
							FROM_UNIXTIME(#) <= datetime
							AND
							(e.userid=# AND e.event LIKE "in_%")
							OR ((e.event LIKE "u_message" OR e.event LIKE "u_wall_post") AND e.params LIKE "userid=#\\t%")
						ORDER BY datetime DESC
						LIMIT #', qa_opt('q2apro_onsitenotifications_maxage'), $userid, $userid, $maxEvents);
                $events = array();
                $postids = array();
                $count = 0;
                while (($event = qa_db_read_one_assoc($event_query, true)) !== null) {
                    if (preg_match('/postid=([0-9]+)/', $event['params'], $m) === 1) {
                        $event['postid'] = (int) $m[1];
                        $postids[] = (int) $m[1];
                        $events[$m[1] . '_' . $count++] = $event;
                    }
                    // private message
                    if ($event['event'] == 'u_message') {
                        // example of $event['params']: userid=1  handle=admin  messageid=4  message=hi admin, how are you?
                        $ustring = $event['params'];
                        // get messageid
                        if (preg_match('/messageid=([0-9]+)/', $ustring, $m) === 1) {
                            $event['messageid'] = (int) $m[1];
                        }
                        // needed for function qa_post_userid_to_handle()
                        require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                        // get handle from userid, memo: userid from receiver is saved in params (the acting userid is the sender)
                        $event['handle'] = qa_post_userid_to_handle($event['userid']);
                        // get message preview by cutting out the string
                        $event['message'] = substr($ustring, strpos($ustring, 'message=') + 8, strlen($ustring) - strpos($ustring, 'message=') + 8);
                        $events[$m[1] . '_' . $count++] = $event;
                    } else {
                        if ($event['event'] == 'u_wall_post') {
                            // example of $event['params']: userid=1	handle=admin	messageid=8	content=hi admin!	format=	text=hi admin!
                            $ustring = $event['params'];
                            // get messageid
                            if (preg_match('/messageid=([0-9]+)/', $ustring, $m) === 1) {
                                $event['messageid'] = (int) $m[1];
                            }
                            // needed for function qa_post_userid_to_handle()
                            require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                            // get handle from userid, memo: userid from receiver is saved in params (the acting userid is the sender)
                            $event['handle'] = qa_post_userid_to_handle($event['userid']);
                            // get message preview by cutting out the string
                            $event['message'] = substr($ustring, strpos($ustring, 'text=') + 5, strlen($ustring) - strpos($ustring, 'text=') + 5);
                            $events[$m[1] . '_' . $count++] = $event;
                        }
                    }
                }
                // get post info, also make sure that post exists
                $posts = null;
                if (!empty($postids)) {
                    $post_query = qa_db_read_all_assoc(qa_db_query_sub('SELECT postid, type, parentid, BINARY title as title FROM ^posts 
									WHERE postid IN (' . implode(',', $postids) . ')'));
                    foreach ($post_query as $post) {
                        // save postids as index in array $posts with the $post content
                        $posts[(string) $post['postid']] = $post;
                    }
                }
                // List all events
                $notifyBoxEvents = '<div id="nfyWrap" class="nfyWrap">
						<div class="nfyTop">' . qa_lang('q2apro_onsitenotifications_lang/my_notifications') . ' <a id="nfyReadClose">' . qa_lang('q2apro_onsitenotifications_lang/close') . ' | × |</a> </div>
						<div class="nfyContainer">
							<div id="nfyContainerInbox">
						';
                // BIG FOREACH
                foreach ($events as $postid_string => $event) {
                    // $postid_string, e.g. 32_1 (32 is postid, 1 is global event count)
                    $type = $event['event'];
                    if ($type == 'u_message') {
                        $eventName = qa_lang('q2apro_onsitenotifications_lang/you_received') . ' ';
                        $itemIcon = '<div class="nicon nmessage"></div>';
                        $activity_url = qa_path_absolute('message') . '/' . $event['handle'];
                        $linkTitle = qa_lang('q2apro_onsitenotifications_lang/message_from') . ' ' . $event['handle'];
                    } else {
                        if ($type == 'u_wall_post') {
                            $eventName = qa_lang('q2apro_onsitenotifications_lang/you_received') . ' ';
                            $itemIcon = '<div class="nicon nwallpost"></div>';
                            // create link to own wall, needs handle
                            require_once QA_INCLUDE_DIR . 'qa-app-posts.php';
                            $userhandle = qa_post_userid_to_handle($userid);
                            // from v1.7 require_once QA_INCLUDE_DIR.'qa-app-users.php'; and qa_userid_to_handle($userid);
                            $activity_url = qa_path_absolute('user') . '/' . $userhandle . '/wall';
                            $linkTitle = qa_lang('q2apro_onsitenotifications_lang/wallpost_from') . ' ' . $event['handle'];
                        } else {
                            // a_post, c_post, q_vote_up, a_vote_up, q_vote_down, a_vote_down
                            $postid = preg_replace('/_.*/', '', $postid_string);
                            $post = null;
                            // assign post content (postid,type,parentid,title) if available
                            $post = @$posts[$postid];
                            $params = array();
                            // explode string to array with values (memo: leave "\t", '\t' will cause errors)
                            $paramsa = explode("\t", $event['params']);
                            foreach ($paramsa as $param) {
                                $parama = explode('=', $param);
                                if (isset($parama[1])) {
                                    $params[$parama[0]] = $parama[1];
                                } else {
                                    $params[$param] = $param;
                                }
                            }
                            $link = '';
                            $linkTitle = '';
                            $activity_url = '';
                            // comment or answer
                            if (isset($post) && strpos($event['event'], 'q_') !== 0 && strpos($event['event'], 'in_q_') !== 0) {
                                if (!isset($params['parentid'])) {
                                    $params['parentid'] = $post['parentid'];
                                }
                                $parent = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $params['parentid']));
                                if ($parent['type'] == 'A') {
                                    $parent = qa_db_select_with_pending(qa_db_full_post_selectspec($userid, $parent['parentid']));
                                }
                                $anchor = qa_anchor(strpos($event['event'], 'a_') === 0 || strpos($event['event'], 'in_a_') === 0 ? 'A' : 'C', $params['postid']);
                                $activity_url = qa_path_absolute(qa_q_request($parent['postid'], $parent['title']), null, $anchor);
                                $linkTitle = $parent['title'];
                                $link = '<a target="_blank" href="' . $activity_url . '">' . $parent['title'] . '</a>';
                            } else {
                                if (isset($post)) {
                                    // question
                                    if (!isset($params['title'])) {
                                        $params['title'] = $posts[$params['postid']]['title'];
                                    }
                                    if ($params['title'] !== null) {
                                        $qTitle = qa_db_read_one_value(qa_db_query_sub("SELECT title FROM `^posts` WHERE `postid` = " . $params['postid'] . " LIMIT 1"), true);
                                        if (!isset($qTitle)) {
                                            $qTitle = '';
                                        }
                                        $activity_url = qa_path_absolute(qa_q_request($params['postid'], $qTitle), null, null);
                                        $linkTitle = $qTitle;
                                        $link = '<a target="_blank" href="' . $activity_url . '">' . $qTitle . '</a>';
                                    }
                                }
                            }
                            // event name
                            $eventName = '';
                            $itemIcon = '';
                            if ($type == 'in_c_question' || $type == 'in_c_answer' || $type == 'in_c_comment') {
                                // added in_c_comment
                                $eventName = qa_lang('q2apro_onsitenotifications_lang/in_comment');
                                $itemIcon = '<div class="nicon ncomment"></div>';
                            } else {
                                if ($type == 'in_q_vote_up' || $type == 'in_a_vote_up') {
                                    $eventName = qa_lang('q2apro_onsitenotifications_lang/in_upvote');
                                    $itemIcon = '<div class="nicon nvoteup"></div>';
                                } else {
                                    if ($type == 'in_q_vote_down' || $type == 'in_a_vote_down') {
                                        $eventName = qa_lang('q2apro_onsitenotifications_lang/in_downvote');
                                        $itemIcon = '<div class="nicon nvotedown"></div>';
                                    } else {
                                        if ($type == 'in_a_question') {
                                            $eventName = qa_lang('q2apro_onsitenotifications_lang/in_answer');
                                            $itemIcon = '<div class="nicon nanswer"></div>';
                                        } else {
                                            if ($type == 'in_a_select') {
                                                $eventName = qa_lang('q2apro_onsitenotifications_lang/in_bestanswer');
                                                $itemIcon = '<div class="nicon nbestanswer"></div>';
                                            } else {
                                                // ignore other events such as in_c_flag
                                                continue;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // end a_post, c_post, q_vote_up, a_vote_up, q_vote_down, a_vote_down
                    $eventtime = $event['datetime'];
                    $whenhtml = qa_html(qa_time_to_string(qa_opt('db_time') - $eventtime));
                    $when = qa_lang_html_sub('main/x_ago', $whenhtml);
                    // extra CSS for highlighting new events
                    $cssNewEv = '';
                    if ($eventtime > $last_visit) {
                        $cssNewEv = '-new';
                    }
                    // if post has been deleted there is no link, dont output
                    if ($activity_url == '') {
                        continue;
                    } else {
                        $notifyBoxEvents .= '<div class="itemBox' . $cssNewEv . '">
								' . $itemIcon . '
								<div class="nfyItemLine">
									<p class="nfyWhat">' . $eventName . ' 
										<a ' . ($type == 'u_message' || $type == 'u_wall_post' ? 'title="' . $event['message'] . '" ' : '') . 'href="' . $activity_url . '"' . (qa_opt('q2apro_onsitenotifications_newwindow') ? ' target="_blank"' : '') . '>' . $linkTitle . '</a>
									</p>
									<p class="nfyTime">' . $when . '</p>
								</div>
							</div>';
                    }
                }
                // END FOREACH
                $notifyBoxEvents .= '</div>
						</div>
						<div class="nfyFooter">
							<a href="http://www.q2apro.com/">by q2apro.com</a>
						</div>
					</div>
					';
                header('Access-Control-Allow-Origin: ' . qa_path(null));
                echo $notifyBoxEvents;
                // update database entry so that all user notifications are seen as read
                qa_db_query_sub('INSERT INTO ^usermeta (user_id,meta_key,meta_value) VALUES(#,$,NOW()) ON DUPLICATE KEY UPDATE meta_value=NOW()', $userid, 'visited_profile');
                exit;
            } else {
                echo 'Unexpected problem detected! No userid, no transfer string.';
                exit;
            }
        }
        /* start */
        $qa_content = qa_content_prepare();
        $qa_content['title'] = '';
        // page title
        // return if not admin!
        if (qa_get_logged_in_level() < QA_USER_LEVEL_ADMIN) {
            $qa_content['error'] = '<p>Access denied</p>';
            return $qa_content;
        } else {
            $qa_content['custom'] = '<p>Hi Admin, it actually makes no sense to call the Ajax URL directly.</p>';
        }
        return $qa_content;
    }