function mp_get_activity_points_by_userid($userid, $categoryid) { return qa_db_read_one_value(qa_db_query_sub('SELECT COALESCE(SUM(U.points), 0) points FROM `mp_userpoints` U RIGHT JOIN mp_user_category_map M ON U.userid = M.userid AND U.categoryid = M.categoryid, qa_users QU WHERE M.userid = QU.userid AND M.categoryid = # AND M.userid = # GROUP BY M.userid ORDER BY points DESC, QU.handle ASC', $categoryid, $userid), true); }
function check_privileges($userid, $event_points) { $user = qa_db_select_with_pending(qa_db_user_points_selectspec($userid, true)); $upoints = (int) $user['points']; $before_points = (int) $user['points'] - $event_points; $permr = qa_db_read_one_value(qa_db_query_sub('SELECT meta_value FROM ^usermeta WHERE user_id=# AND meta_key=$ ', $userid, 'priv_notify'), true); // stale perms $stale = array(); if ($permr) { $perms = explode('^', $permr); $stale = explode(',', $perms[0]); } $p_options = qa_get_permit_options(); $notices = ''; foreach ($p_options as $option) { if (qa_opt($option) == QA_PERMIT_POINTS) { $opoints = (int) qa_opt($option . '_points'); if ($opoints < $upoints && $opoints > $before_points && !in_array($option, $stale)) { $notices = ($notices ? $notices . ',' : '') . $option; } } } if ($notices) { qa_db_query_sub('INSERT INTO ^usermeta (user_id,meta_key,meta_value) VALUES (#,$,$) ON DUPLICATE KEY UPDATE meta_value=$', $userid, 'priv_notify', '^' . $notices, $permr . ($perms[1] ? ',' : '') . $notices); if (qa_opt('priv_email_notify_on')) { $this->notify($userid, $notices); } } }
function qa_share_buttons($q_view) { if (qa_opt('expert_question_enable')) { $qid = $q_view['raw']['postid']; $expert = qa_db_read_one_value(qa_db_query_sub("SELECT meta_value FROM ^postmeta WHERE meta_key='is_expert_question' AND post_id=#", $qid), true); if ($expert) { return; } } $url = qa_path_html(qa_q_request($q_view['raw']['postid'], $q_view['raw']['title']), null, qa_opt('site_url')); $code = array('facebook' => '<iframe src="//www.facebook.com/plugins/like.php?href=' . qa_path_html(qa_q_request($this->content['q_view']['raw']['postid'], $this->content['q_view']['raw']['title']), null, qa_opt('site_url')) . '&send=false&layout=standard&width=53&layout=button_count&show_faces=false&action=like&colorscheme=light&font&height=20&appId=170382616390886" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:53px; height:20px;" allowTransparency="true"></iframe>', 'twitter' => '<a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a>', 'google' => '<g:plusone size="medium" count="false"></g:plusone>', 'linkedin' => '<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share"></script>', 'email' => '<a title="Share this question via email" id="share-button-email" href="mailto:?subject=' . rawurlencode('[' . qa_opt('site_title') . '] ' . $q_view['raw']['title']) . '&body=' . rawurlencode($url) . '"><img height="24" src="' . QA_HTML_THEME_LAYER_URLTOROOT . 'qa-share-mail.png' . '"/></a>'); // sort by weight $weight = array('facebook' => qa_opt('share_plugin_facebook_weight'), 'twitter' => qa_opt('share_plugin_twitter_weight'), 'google' => qa_opt('share_plugin_google_weight'), 'linkedin' => qa_opt('share_plugin_linkedin_weight'), 'email' => qa_opt('share_plugin_email_weight')); asort($weight); // output foreach ($weight as $key => $val) { if (qa_opt('share_plugin_' . $key)) { $shares[] = $code[$key]; } } if (empty($shares)) { return null; } $output = implode(' ', $shares); return $output; }
function qa_db_blob_exists($blobid) { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } return qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^blobs WHERE blobid=#', $blobid)) > 0; }
function qa_db_cache_get($type, $cacheid) { $content = qa_db_read_one_value(qa_db_query_sub('SELECT content FROM ^cache WHERE type=$ AND cacheid=#', $type, $cacheid), true); if (isset($content)) { qa_db_query_sub('UPDATE ^cache SET lastread=NOW() WHERE type=$ AND cacheid=#', $type, $cacheid); } return $content; }
function ra_db_builder($name, $value = null) { if (isset($value)) { ra_set_builder_content($name, $value); } else { $content = qa_db_read_one_value(qa_db_query_sub('SELECT BINARY content as content FROM ^builder WHERE name=#', $name), true); return str_replace('\\', '', $content); } }
function getuserfromhandle($handle) { require_once QA_INCLUDE_DIR . 'qa-app-users.php'; if (QA_FINAL_EXTERNAL_USERS) { $publictouserid = qa_get_userids_from_public(array($handle)); $userid = @$publictouserid[$handle]; } else { $userid = qa_db_read_one_value(qa_db_query_sub('SELECT userid FROM ^users WHERE handle = $', $handle), true); } return $userid; }
function qa_db_cache_get($type, $cacheid) { if (qa_to_override(__FUNCTION__)) { $args = func_get_args(); return qa_call_override(__FUNCTION__, $args); } $content = qa_db_read_one_value(qa_db_query_sub('SELECT content FROM ^cache WHERE type=$ AND cacheid=#', $type, $cacheid), true); if (isset($content)) { qa_db_query_sub('UPDATE ^cache SET lastread=NOW() WHERE type=$ AND cacheid=#', $type, $cacheid); } return $content; }
function a_count($post) { if (qa_opt('ccount') && isset($post['answers'])) { $this->output('<div class="qa-ac-count">'); qa_html_theme_base::a_count($post); $comments = qa_db_read_one_value(qa_db_query_sub("SELECT COUNT(postid) FROM ^posts WHERE (parentid=# OR parentid IN (SELECT postid FROM ^posts WHERE parentid=#)) AND type='C'", $post['raw']['postid'], $post['raw']['postid'])); $this->output_split(array('prefix' => '', 'data' => $comments, 'suffix' => ' comments'), 'qa-c-count', 'SPAN', 'SPAN'); $this->output('</div>'); } else { qa_html_theme_base::a_count($post); } }
function q_view_clear() { // call default method output qa_html_theme_base::q_view_clear(); // return if not admin! if (qa_get_logged_in_level() < QA_USER_LEVEL_ADMIN) { return; } // check if question is duplicate $closed = @$this->content['q_view']['raw']['closedbyid'] !== null; if ($closed) { // check if duplicate $duplicate = qa_db_read_one_value(qa_db_query_sub('SELECT postid FROM `^posts` WHERE `postid` = # AND `type` = "Q" ;', $this->content['q_view']['raw']['closedbyid']), true); if ($duplicate) { $this->output('<div id="mergeDup" style="margin:10px 0 0 120px;padding:5px 10px;background:#FCC;border:1px solid #AAA;"><h3>Merge Duplicate:</h3>'); // form output $this->output(' <FORM METHOD="POST"> <TABLE> <TR> <TD CLASS="qa-form-tall-label"> From: <INPUT NAME="merge_from" id="merge_from" TYPE="text" VALUE="' . $this->content['q_view']['raw']['postid'] . '" CLASS="qa-form-tall-number"> To: <INPUT NAME="merge_to" id="merge_to" TYPE="text" VALUE="' . $this->content['q_view']['raw']['closedbyid'] . '" CLASS="qa-form-tall-number"> </TD> </TR> <TR> <TD CLASS="qa-form-tall-label"> Text to show when redirecting from merged question: </TD> </TR> <TR> <TD CLASS="qa-form-tall-label"> <INPUT NAME="merge_question_merged" id="merge_question_merged" TYPE="text" VALUE="' . qa_opt('merge_question_merged') . '" CLASS="qa-form-tall-text"> </TD> </TR> <TR> <TD style="text-align:right;"> <INPUT NAME="merge_question_process" VALUE="Merge" TITLE="" TYPE="submit" CLASS="qa-form-tall-button qa-form-tall-button-0"> </TD> </TR> </TABLE> </FORM> '); $this->output('</div>'); } } }
function qa_page_q_post_rules($post, $parentpost = null, $siblingposts = null, $childposts = null) { $rules = qa_page_q_post_rules_base($post, $parentpost, $siblingposts, $childposts); qa_db_query_sub('CREATE TABLE IF NOT EXISTS ^postmeta ( meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, post_id bigint(20) unsigned NOT NULL, meta_key varchar(255) DEFAULT \'\', meta_value longtext, PRIMARY KEY (meta_id), KEY post_id (post_id), KEY meta_key (meta_key) ) ENGINE=MyISAM DEFAULT CHARSET=utf8'); $expert = qa_db_read_one_value(qa_db_query_sub("SELECT meta_value FROM ^postmeta WHERE meta_key='is_expert_question' AND post_id=#", $post['postid']), true); if ($expert) { if (!qa_permit_value_error(qa_opt('expert_question_roles'), qa_get_logged_in_userid(), qa_get_logged_in_level(), qa_get_logged_in_flags())) { $is_expert = true; } $users = qa_opt('expert_question_users'); $users = explode("\n", $users); $handle = qa_get_logged_in_handle(); foreach ($users as $idx => $user) { if ($user == $handle) { $is_expert = true; break; } if (strpos($user, '=')) { $user = explode('=', $user); if ($user[0] == $handle) { $catnames = explode(',', $user[1]); $cats = qa_db_read_all_values(qa_db_query_sub('SELECT categoryid FROM ^categories WHERE title IN ($)', $catnames)); $is_expert = $cats; } } } if (isset($is_expert) && !$rules['viewable']) { // experts that aren't allowed to change hidden questions if (is_array($is_expert)) { $in_cats = qa_db_read_one_value(qa_db_query_sub("SELECT COUNT(postid) FROM ^posts WHERE categoryid IN (#) AND postid=#", $is_expert, $post['postid']), true); if ($in_cats) { $rules['viewable'] = true; } } else { $rules['viewable'] = true; } } $rules['reshowable'] = false; $rules['answerbutton'] = true; $rules['commentbutton'] = true; $rules['commentable'] = true; } return $rules; }
function process_event($event, $userid, $handle, $cookieid, $params) { if (!qa_opt('close_auto_close')) { return; } $special = array('a_post', 'c_post'); if ($event == 'a_post') { $count = qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(postid) FROM ^posts WHERE type=$ AND parentid=#', 'A', $params['parentid']), true); if ($count >= qa_opt('close_auto_close')) { qa_db_query_sub('INSERT INTO ^postmeta (post_id,meta_key,meta_value) VALUES (#,$,$) ON DUPLICATE KEY UPDATE meta_value=meta_value', $params['parentid'], 'is_closed', $userid . '^' . qa_opt('close_auto_close_reason')); } } }
function process_event($event, $userid, $handle, $cookieid, $params) { if (qa_opt('buddypress_integration_enable') && $userid) { switch ($event) { // when a new question, answer or comment is created. The $params array contains full information about the new post, including its ID in $params['postid'] and textual content in $params['text']. case 'q_post': if (qa_post_text('is_expert_question') == 'yes' || !qa_opt('buddypress_integration_post_q')) { return; } // don't broadcast expert questions $this->post($event, $userid, $handle, $params, 'Q'); break; case 'a_post': if (qa_post_text('is_expert_question') == 'yes' || !qa_opt('buddypress_integration_post_a')) { return; } if (qa_opt('expert_question_enable')) { $pid = $params['parentid']; $parent = qa_db_read_one_assoc(qa_db_query_sub("SELECT type, parentid FROM ^posts WHERE postid=#", $pid), true); if (strpos($parent['type'], 'A') === 0) { $pid = $parent['parentid']; } $expert = qa_db_read_one_value(qa_db_query_sub("SELECT meta_value FROM ^postmeta WHERE post_id=# AND meta_key='is_expert_question'", $pid), true); if ($expert) { return; } } $this->post($event, $userid, $handle, $params, 'A'); break; case 'c_post': if (!qa_opt('buddypress_integration_post_c')) { return; } if (qa_opt('expert_question_enable')) { $pid = $params['parentid']; $parent = qa_db_read_one_assoc(qa_db_query_sub("SELECT type, parentid FROM ^posts WHERE postid=#", $pid), true); if (strpos($parent['type'], 'A') === 0) { $pid = $parent['parentid']; } $expert = qa_db_read_one_value(qa_db_query_sub("SELECT meta_value FROM ^postmeta WHERE post_id=# AND meta_key='is_expert_question'", $pid), true); if ($expert) { return; } } $this->post($event, $userid, $handle, $params, 'C'); break; default: break; } } }
function admin_form(&$qa_content) { // Process form input $ok = null; if (qa_clicked('priv_save_settings')) { $table_exists = qa_db_read_one_value(qa_db_query_sub("SHOW TABLES LIKE '^usermeta'"), true); if (!$table_exists) { 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'); } // options qa_opt('priv_active', (bool) qa_post_text('priv_active')); qa_opt('priv_user_field', (bool) qa_post_text('priv_user_field')); qa_opt('priv_title', qa_post_text('priv_title')); qa_opt('priv_hover', qa_post_text('priv_hover')); qa_opt('priv_shading', (int) qa_post_text('priv_shading')); qa_opt('priv_hover_earned', qa_post_text('priv_hover_earned')); qa_opt('priv_css', qa_post_text('priv_css')); qa_opt('priv_check', (bool) qa_post_text('priv_check')); qa_opt('priv_notify_text', qa_post_text('priv_notify_text')); qa_opt('priv_notify_text_multi', qa_post_text('priv_notify_text_multi')); qa_opt('priv_email_notify_on', (bool) qa_post_text('priv_email_notify_on')); qa_opt('priv_email_subject', qa_post_text('priv_email_subject')); qa_opt('priv_email_body', qa_post_text('priv_email_body')); $ok = qa_lang_html('admin/options_saved'); } // Create the form for display $fields = array(); $fields[] = array('label' => 'Activate Privilege Management', 'tags' => 'NAME="priv_active"', 'value' => qa_opt('priv_active'), 'type' => 'checkbox'); $fields[] = array('label' => 'Show privileges in user profile', 'tags' => 'NAME="priv_user_field"', 'value' => qa_opt('priv_user_field'), 'type' => 'checkbox'); $shading = array('color', 'grey'); $fields[] = array('label' => 'Privilege shading style', 'tags' => 'NAME="priv_shading"', 'type' => 'select', 'options' => $shading, 'value' => @$shading[qa_opt('priv_shading')]); $fields[] = array('label' => 'Title of user privilege box', 'tags' => 'NAME="priv_title"', 'value' => qa_opt('priv_title')); $fields[] = array('label' => 'Hover text on unearned privilege name', 'note' => '# is replaced by required points, % by percentage user already has', 'tags' => 'NAME="priv_hover"', 'value' => qa_opt('priv_hover')); $fields[] = array('label' => 'Hover text on earned privilege name', 'note' => '# is replaced by required points', 'tags' => 'NAME="priv_hover_earned"', 'value' => qa_opt('priv_hover_earned')); $fields[] = array('type' => 'blank'); $fields[] = array('label' => 'enable new privilege notification popup', 'tags' => 'NAME="priv_check"', 'value' => (bool) qa_opt('priv_check'), 'type' => 'checkbox'); $fields[] = array('label' => 'privilege popup text (single privilege)', 'tags' => 'NAME="priv_notify_text"', 'value' => qa_html(qa_opt('priv_notify_text')), 'note' => 'substitutes ^profile for profile url, ^privilege for privilege name'); $fields[] = array('label' => 'privilege popup text (multiple privileges)', 'tags' => 'NAME="priv_notify_text_multi"', 'value' => qa_html(qa_opt('priv_notify_text_multi')), 'note' => 'substitutes ^profile for profile url, # for number of privileges'); $fields[] = array('label' => 'enable email notification', 'tags' => 'NAME="priv_email_notify_on"', 'value' => (bool) qa_opt('priv_email_notify_on'), 'type' => 'checkbox'); $fields[] = array('label' => 'email subject', 'tags' => 'NAME="priv_email_subject"', 'value' => qa_html(qa_opt('priv_email_subject'))); $fields[] = array('label' => 'email body', 'tags' => 'name="priv_email_body"', 'value' => qa_html(qa_opt('priv_email_body')), 'type' => 'textarea', 'rows' => 20, 'note' => 'Available replacement text:<br/><br/><i>^site_title<br/>^handle<br/>^email<br/>^open<br/>^close<br/>^profile_url<br/>^site_url<br/>^single=`text`^multi=`text`</i>'); return array('ok' => $ok && !isset($error) ? $ok : null, 'fields' => $fields, 'buttons' => array(array('label' => 'Save', 'tags' => 'NAME="priv_save_settings"'))); }
function qa_theme_chooser() { $userid = qa_get_logged_in_userid(); if (function_exists('qa_register_plugin_overrides')) { // 1.5 return qa_db_read_one_value(qa_db_query_sub('SELECT meta_value FROM ^usermeta WHERE user_id=# AND meta_key=$', $userid, 'custom_theme'), true); } require_once QA_INCLUDE_DIR . 'qa-app-users.php'; if (!$userid) { $theme = @$_COOKIE['qa_theme_switch']; if (qa_opt('theme_switch_enable_mobile')) { $theme_choice_mobile = qa_theme_chooser_detect_mobile() ? qa_opt('theme_switch_mobile') : false; } if ($theme) { global $qa_theme_switch_is_mobile; $qa_theme_switch_is_mobile = $theme_choice_mobile; // it's mobile, but they have a preferred theme return $theme; } return $theme_choice_mobile; } if (qa_opt('theme_switch_enable_mobile')) { $theme_choice_mobile = @$_COOKIE['qa_theme_switch'] == qa_opt('theme_switch_mobile') ? qa_opt('theme_switch_mobile') : false; $theme_mobile = qa_theme_chooser_detect_mobile() ? qa_opt('theme_switch_mobile') : false; global $qa_theme_switch_is_mobile; $qa_theme_switch_is_mobile = $theme_mobile; // it's mobile, but they have a preferred theme if ($theme_choice && (!$theme_choice_mobile || !$theme_mobile)) { // have theme choice, and 1) it's not mobile, 2) it's mobile and they don't have a cookie, 3) they have a cookie but it's not mobile, 4) they have a cookie, it's mobile, but the cookie isn't mobile // do nothing } else { if (!$theme_choice_mobile && $theme_mobile) { // no cookie, is mobile, give mobile return $theme_mobile; } else { if ($theme_choice_mobile && $theme_mobile) { // cookie, is mobile, give cookie return $theme_choice_mobile; } } } } if ($theme_choice) { global $qa_theme_switch_is_mobile; return $theme_choice; } return $theme_choice_mobile; }
function process_event($event, $userid, $handle, $cookieid, $params) { if (!qa_opt('event_logger_to_database')) { return; } $twoway = array('a_select', 'a_unselect', 'q_vote_up', 'a_vote_up', 'q_vote_down', 'a_vote_down', 'q_vote_nil', 'a_vote_nil', 'q_flag', 'a_flag', 'c_flag', 'q_unflag', 'a_unflag', 'c_unflag', 'u_edit', 'u_level', 'u_block', 'u_unblock'); $special = array('a_post', 'c_post'); if (in_array($event, $twoway)) { if (strpos($event, 'u_') === 0) { $uid = $params['userid']; } else { $uid = qa_db_read_one_value(qa_db_query_sub('SELECT userid FROM ^posts WHERE postid=#', $params['postid']), true); } if ($uid != $userid) { $ohandle = $this->getHandleFromId($uid); $oevent = 'in_' . $event; $paramstring = ''; foreach ($params as $key => $value) { $paramstring .= (strlen($paramstring) ? "\t" : '') . $key . '=' . $this->value_to_text($value); } qa_db_query_sub('INSERT INTO ^eventlog (datetime, ipaddress, userid, handle, cookieid, event, params) ' . 'VALUES (NOW(), $, $, $, #, $, $)', qa_remote_ip_address(), $uid, $ohandle, $cookieid, $oevent, $paramstring); } } // comments and answers if (in_array($event, $special)) { $pid = qa_db_read_one_value(qa_db_query_sub('SELECT userid FROM ^posts WHERE postid=#', $params['parentid']), true); if ($pid != $userid) { $ohandle = $this->getHandleFromId($pid); switch ($event) { case 'a_post': $oevent = 'in_a_question'; break; case 'c_post': if ($params['parenttype'] == 'Q') { $oevent = 'in_c_question'; } else { $oevent = 'in_c_answer'; } break; } $paramstring = ''; foreach ($params as $key => $value) { $paramstring .= (strlen($paramstring) ? "\t" : '') . $key . '=' . $this->value_to_text($value); } qa_db_query_sub('INSERT INTO ^eventlog (datetime, ipaddress, userid, handle, cookieid, event, params) ' . 'VALUES (NOW(), $, $, $, #, $, $)', qa_remote_ip_address(), $pid, $ohandle, $cookieid, $oevent, $paramstring); } } }
function qa_get_blob_directory($blobid) { // get created from table blobs $blobcreated = qa_db_read_one_value(qa_db_query_sub('SELECT created FROM `^blobs` WHERE `blobid` = # ', $blobid), true); // error_log('blobid: '.$blobid); if (is_null($blobcreated)) { // fall back to default q2a core behavior // return rtrim(QA_BLOBS_DIRECTORY, '/').'/'.substr(str_pad($blobid, 20, '0', STR_PAD_LEFT), 0, 3); // file does not exist, makes no sense to allow it return null; } else { // create folders Y/M/D, e.g. 2016/01/15 $blobpath = substr($blobcreated, 0, 4) . '/' . substr($blobcreated, 5, 2) . '/' . substr($blobcreated, 8, 2); // error_log('path: '.rtrim(QA_BLOBS_DIRECTORY, '/').'/'.$blobpath); return rtrim(QA_BLOBS_DIRECTORY, '/') . '/' . $blobpath; } }
function qa_db_user_events_truncate($userid, $questionid = null) { // First try truncating based on there being too many events for this question $questiontruncated = false; if (isset($questionid)) { $truncate = qa_db_read_one_value(qa_db_query_sub('SELECT updated FROM ^userevents WHERE userid=$ AND questionid=# ORDER BY updated DESC LIMIT #,1', $userid, $questionid, QA_DB_MAX_EVENTS_PER_Q), true); if (isset($truncate)) { qa_db_query_sub('DELETE FROM ^userevents WHERE userid=$ AND questionid=# AND updated<=$', $userid, $questionid, $truncate); $questiontruncated = true; } } // If that didn't happen, try truncating the stream in general based on its total length if (!$questiontruncated) { $truncate = qa_db_read_one_value(qa_db_query_sub('SELECT updated FROM ^userevents WHERE userid=$ ORDER BY updated DESC LIMIT #,1', $userid, (int) qa_opt('max_store_user_updates')), true); if (isset($truncate)) { qa_db_query_sub('DELETE FROM ^userevents WHERE userid=$ AND updated<=$', $userid, $truncate); } } }
private function get_cache_db() { if (!$this->is_mobile()) { $device = ''; } else { $device = 'mobile'; } $content = qa_db_read_one_value(qa_db_query_sub('SELECT content' . ' FROM ^caching' . ' WHERE sign=0 AND format=$ AND type=\'page\' AND device=$ AND filename=$ AND (created > NOW() - INTERVAL # SECOND)', $this->get_cache_file_extension(), $device, basename($this->cache_file), QA_CACHING_EXPIRATION_TIME)); return $content; }
function qw_get_total_messages($uid) { return qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^ra_userevent WHERE `read` = 0 AND effecteduserid=# AND event IN ("u_wall_post", "u_message")', $uid), true); }
function GetUseridFromPost($postid) { return qa_db_read_one_value(qa_db_query_sub('SELECT userid FROM ^posts WHERE postid=#', $postid), true); }
function qa_db_uservote_get($postid, $userid) { return qa_db_read_one_value(qa_db_query_sub('SELECT vote FROM ^uservotes WHERE postid=# AND userid=#', $postid, $userid), true); }
function qa_db_favorite_create($userid, $entitytype, $entityid) { $threshold = qa_opt('max_copy_user_updates'); // if this many users subscribe to it, create a shared stream // Add in the favorite for this user, unshared events at first (will be switched later if appropriate) qa_db_query_sub('INSERT IGNORE INTO ^userfavorites (userid, entitytype, entityid, nouserevents) VALUES ($, $, #, 0)', $userid, $entitytype, $entityid); // See whether this entity already has another favoriter who uses its shared event stream $useshared = qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# AND nouserevents>0 LIMIT 1', $entitytype, $entityid)); // If not, check whether it's time to switch it over to a shared stream if (!$useshared) { $favoriters = qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^userfavorites WHERE entitytype=$ AND entityid=# LIMIT #', $entitytype, $entityid, $threshold)); $useshared = $favoriters >= $threshold; } // If we're going to use the shared stream... if ($useshared) { // ... for all the people for whom we're switching this to a shared stream, find the highest number of other shared streams they have $maxshared = qa_db_read_one_value(qa_db_query_sub('SELECT MAX(c) FROM (SELECT COUNT(*) AS c FROM ^userfavorites AS shared JOIN ^userfavorites AS unshared ' . 'WHERE shared.userid=unshared.userid AND shared.nouserevents>0 AND unshared.entitytype=$ AND unshared.entityid=# AND unshared.nouserevents=0 GROUP BY shared.userid) y', $entitytype, $entityid)); // ... if this number is greater than our current 'max_copy_user_updates' threshold, increase that threshold (see long comment above) if ($maxshared + 1 > $threshold) { qa_opt('max_copy_user_updates', $maxshared + 1); } // ... now switch all unshared favoriters (including this new one) over to be shared qa_db_query_sub('UPDATE ^userfavorites SET nouserevents=1 WHERE entitytype=$ AND entityid=# AND nouserevents=0', $entitytype, $entityid); // Otherwise if we're going to record this in user-specific streams ... } else { require_once QA_INCLUDE_DIR . 'qa-db-events.php'; // ... copy across recent events from the shared stream qa_db_query_sub('INSERT INTO ^userevents (userid, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated) ' . 'SELECT #, entitytype, entityid, questionid, lastpostid, updatetype, lastuserid, updated FROM ' . '^sharedevents WHERE entitytype=$ AND entityid=#', $userid, $entitytype, $entityid); // ... and truncate the user's stream as appropriate qa_db_user_events_truncate($userid); } }
function cs_get_excerpt($id) { $result = qa_db_read_one_value(qa_db_query_sub('SELECT content FROM ^posts WHERE postid=#', $id), true); return strip_tags($result); }
function qa_db_meta_get($metatable, $idcolumn, $idvalue, $title) { if (is_array($title)) { if (count($title)) { return qa_db_read_all_assoc(qa_db_query_sub('SELECT title, content FROM ^' . $metatable . ' WHERE ' . $idcolumn . '=$ AND title IN($)', $idvalue, $title), 'title', 'content'); } else { return array(); } } else { return qa_db_read_one_value(qa_db_query_sub('SELECT content FROM ^' . $metatable . ' WHERE ' . $idcolumn . '=$ AND title=$', $idvalue, $title), true); } }
function check_badges($uid) { $medals = qa_db_read_all_values(qa_db_query_sub('SELECT user_id FROM ^userbadges WHERE user_id=#', $uid)); $badges = array('medalist', 'champion', 'olympian'); foreach ($badges as $badge_slug) { if (count($medals) >= (int) qa_opt('badge_' . $badge_slug . '_var') && qa_opt('badge_' . $badge_slug . '_enabled') !== '0') { $result = qa_db_read_one_value(qa_db_query_sub('SELECT badge_slug FROM ^userbadges WHERE user_id=# AND badge_slug=$', $uid, $badge_slug), true); if ($result == null) { // not already awarded this badge $this->award_badge(null, $uid, $badge_slug, true); // this is a "badge badge" } } } }
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> <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 ' . $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); }
function qa_db_check_tables() { qa_db_query_raw('UNLOCK TABLES'); // we could be inside a lock tables block $version = qa_db_read_one_value(qa_db_query_raw('SELECT VERSION()')); if ((double) $version < 4.1) { qa_fatal_error('MySQL version 4.1 or later is required - you appear to be running MySQL ' . $version); } $definitions = qa_db_table_definitions(); $missing = qa_db_missing_tables($definitions); if (count($missing) == count($definitions)) { return 'none'; } else { if (!isset($missing['options'])) { $version = qa_db_get_db_version(); if (isset($version) && $version < QA_DB_VERSION_CURRENT) { return 'old-version'; } } if (count($missing)) { if (defined('QA_MYSQL_USERS_PREFIX')) { // special case if two installations sharing users $datacount = 0; $datamissing = 0; foreach ($definitions as $rawname => $definition) { if (qa_db_add_table_prefix($rawname) == QA_MYSQL_TABLE_PREFIX . $rawname) { $datacount++; if (isset($missing[$rawname])) { $datamissing++; } } } if ($datacount == $datamissing && $datamissing == count($missing)) { return 'non-users-missing'; } } return 'table-missing'; } else { foreach ($definitions as $table => $definition) { if (count(qa_db_missing_columns($table, $definition))) { return 'column-missing'; } } } } return false; }
function qa_check_all_users_badges() { $awarded = 0; $users; $temp = qa_db_query_sub('SELECT * FROM ^posts'); while (($post = qa_db_read_one_assoc($temp, true)) !== null) { if (!$post['userid']) { continue; } $user = '******' . $post['userid']; $pid = $post['postid']; $pt = $post['type']; // get post count if (isset($users[$user]) && isset($users[$user][$pt])) { $users[$user][$pt]++; } else { $users[$user][$pt] = 1; } // get post votes if ($post['netvotes'] != 0) { $users[$user][$pt . 'votes'][] = array('id' => $pid, 'votes' => (int) $post['netvotes'], 'parentid' => $post['parentid'], 'created' => $post['created']); } // get post views if ($post['views']) { $users[$user]['views'][] = array('id' => $pid, 'views' => $post['views']); } } //votes received and given out $voter = qa_db_read_all_assoc(qa_db_query_sub('SELECT userid,qupvotes,qdownvotes,aupvotes,adownvotes,upvoteds FROM ^userpoints')); foreach ($voter as $idx => $votes) { $user = '******' . $votes['userid']; // votes $users[$user]['votes'] = (int) $votes['qupvotes'] + (int) $votes['qdownvotes'] + (int) $votes['aupvotes'] + (int) $votes['adownvotes']; // voteds $users[$user]['voted'] = (int) $votes['upvoteds']; unset($voter[$idx]); } // flags $flag_result = qa_db_read_all_values(qa_db_query_sub('SELECT userid FROM ^uservotes WHERE flag > 0')); foreach ($flag_result as $idx => $flag) { $user = '******' . $flag; // get flag count if (isset($users[$user]) && isset($users[$user]['flags'])) { $users[$user]['flags']++; } else { $users[$user]['flags'] = 1; } unset($flag_result[$idx]); } // per user loop foreach ($users as $user => $data) { $uid = (int) substr($user, 4); // bulk posts $badges = array('Q' => array('asker', 'questioner', 'inquisitor'), 'A' => array('answerer', 'lecturer', 'preacher'), 'C' => array('commenter', 'commentator', 'annotator')); foreach ($badges as $pt => $slugs) { if (!isset($data[$pt])) { continue; } $awarded += count(qa_badge_award_check($slugs, $data[$pt], $uid, null, 0)); } // nice Q&A $badges = array('nice_question', 'good_question', 'great_question', 'nice_answer', 'good_answer', 'great_answer'); if ($this->badge_activated($badges)) { $badges = array('Q' => array('nice_question', 'good_question', 'great_question'), 'A' => array('nice_answer', 'good_answer', 'great_answer')); foreach ($badges as $pt => $slugs) { foreach ($slugs as $badge_slug) { if (!isset($data[$pt . 'votes'])) { continue; } foreach ($data[$pt . 'votes'] as $idv) { // poll plugin integration if ($pt == 'A' && qa_opt('poll_enable')) { $poll = qa_db_read_one_value(qa_db_query_sub('SELECT meta_value FROM ^postmeta WHERE post_id=# AND meta_key=$', $idv['id'], 'is_poll'), true); if ($poll) { continue; } } if ((int) $idv['votes'] >= (int) qa_opt('badge_' . $badge_slug . '_var') && qa_opt('badge_' . $badge_slug . '_enabled') !== '0') { $result = qa_db_read_one_value(qa_db_query_sub('SELECT badge_slug FROM ^userbadges WHERE user_id=# AND object_id=# AND badge_slug=$', $uid, $idv['id'], $badge_slug), true); if ($result == null) { // not already awarded this badge $this->award_badge($idv['id'], $uid, $badge_slug, false, true); $awarded++; } // old question answer vote checks if ($pt == 'A') { $qid = $idv['parentid']; $create = strtotime($idv['created']); $parent = $this->get_post_data($qid); $pcreate = strtotime($parent['created']); $diff = round(abs($pcreate - $create) / 60 / 60 / 24); $badge_slug2 = $badge_slug . '_old'; if ($diff >= (int) qa_opt('badge_' . $badge_slug2 . '_var') && qa_opt('badge_' . $badge_slug2 . '_enabled') !== '0') { $result = qa_db_read_one_value(qa_db_query_sub('SELECT badge_slug FROM ^userbadges WHERE user_id=# AND object_id=# AND badge_slug=$', $uid, $idv['id'], $badge_slug2), true); if ($result == null) { // not already awarded for this answer $this->award_badge($idv['id'], $uid, $badge_slug2); $awarded++; } } } } } } } } // votes per user badges if (isset($data['votes'])) { $votes = $data['votes']; $badges = array('voter', 'avid_voter', 'devoted_voter'); $awarded += count(qa_badge_award_check($badges, $votes, $uid, null, 0)); } // voted per user badges if (isset($data['voted'])) { $votes = $data['voted']; $badges = array('liked', 'loved', 'revered'); $awarded += count(qa_badge_award_check($badges, $votes, $uid, null, 0)); } // views per post badges if (isset($data['views'])) { $badges = array('notable_question', 'popular_question', 'famous_question'); foreach ($data['views'] as $idv) { $awarded += count(qa_badge_award_check($badges, $idv['views'], $uid, $idv['id'], 0)); } } // flags per user if (isset($data['flags'])) { $flags = $data['flags']; $badges = array('watchdog', 'bloodhound', 'pitbull'); $awarded += count(qa_badge_award_check($badges, $flags, $uid, null, 0)); } unset($users[$user]); } // selects, selecteds $badges = array('gifted', 'wise', 'enlightened', 'grateful', 'respectful', 'reverential'); if ($this->badge_activated($badges)) { $selects = qa_db_read_all_assoc(qa_db_query_sub('SELECT aselects, aselecteds, userid FROM ^userpoints')); foreach ($selects as $idx => $s) { $uid = $s['userid']; if (isset($s['aselecteds'])) { $count = $s['aselecteds']; $badges = array('gifted', 'wise', 'enlightened'); $awarded += count(qa_badge_award_check($badges, $count, $uid, null, 0)); } if (isset($s['aselects'])) { $count = $s['aselects']; $badges = array('grateful', 'respectful', 'reverential'); $awarded += count(qa_badge_award_check($badges, $count, $uid, null, 0)); } unset($selects[$idx]); } } // achievements $badges = array('dedicated', 'devoted', 'zealous', 'visitor', 'trouper', 'veteran', 'regular', 'old_timer', 'ancestor', 'reader', 'avid_reader', 'devoted_reader'); if ($this->badge_activated($badges)) { $userq = qa_db_query_sub('SELECT user_id AS uid,questions_read AS qr,oldest_consec_visit AS ocv,longest_consec_visit AS lcv,total_days_visited AS tdv,last_visit AS lv,first_visit AS fv,posts_edited AS pe FROM ^achievements'); while (($user = qa_db_read_one_assoc($userq, true)) !== null) { $uid = $user['uid']; // edits $count = $user['pe']; $badges = array('editor', 'copy_editor', 'senior_editor'); $awarded += count(qa_badge_award_check($badges, $count, $uid, null, 0)); // on-sign-in badges // check lapse in days since last visit // using julian days $todayj = GregorianToJD(date('n'), date('j'), date('Y')); $last_visit = strtotime($user['lv']); $lastj = GregorianToJD(date('n', $last_visit), date('j', $last_visit), date('Y', $last_visit)); $last_diff = $todayj - $lastj; $first_visit = strtotime($user['fv']); $first_visitj = GregorianToJD(date('n', $first_visit), date('j', $first_visit), date('Y', $first_visit)); $first_visit_diff = $todayj - $first_visitj; $badges = array('dedicated', 'devoted', 'zealous'); $awarded += count(qa_badge_award_check($badges, $user['lcv'], $uid, null, 0)); $badges = array('visitor', 'trouper', 'veteran'); $awarded += count(qa_badge_award_check($badges, $user['tdv'], $uid, null, 0)); $badges = array('regular', 'old_timer', 'ancestor'); $awarded += count(qa_badge_award_check($badges, $first_visit_diff, $uid, null, 0)); // views $badges = array('reader', 'avid_reader', 'devoted_reader'); $awarded += count(qa_badge_award_check($badges, $user['qr'], $uid, null, 0)); } } // points $badges = array('100_club', '1000_club', '10000_club'); if ($this->badge_activated($badges)) { $userq = qa_db_query_sub('SELECT userid, points FROM ^userpoints'); while (($user = qa_db_read_one_assoc($userq, true)) !== null) { $awarded += count(qa_badge_award_check($badges, $user['points'], $user['userid'], null, 0)); } } if (!QA_FINAL_EXTERNAL_USERS) { // verified $badges = array('verified'); if ($this->badge_activated($badges)) { $userq = qa_db_query_sub('SELECT userid, flags FROM ^users WHERE flags&#', QA_USER_FLAGS_EMAIL_CONFIRMED); while (($user = qa_db_read_one_assoc($userq, true)) !== null) { $awarded += count(qa_badge_award_check($badges, false, $user['userid'], null, 0)); } } // profile stuff $badges = array('avatar', 'profiler'); if ($this->badge_activated($badges)) { $userq = qa_db_query_sub('SELECT userid FROM ^users'); while (($userid = qa_db_read_one_value($userq, true)) !== null) { list($useraccount, $userprofile, $userfields) = qa_db_select_with_pending(qa_db_user_account_selectspec($userid, true), qa_db_user_profile_selectspec($userid, true), qa_db_userfields_selectspec()); // avatar badge if (qa_opt('avatar_allow_upload') && isset($useraccount['avatarblobid'])) { $badges = array('avatar'); $awarded += count(qa_badge_award_check($badges, false, $userid, null, 0)); } // profile completion $missing = false; foreach ($userfields as $userfield) { if (!isset($userprofile[$userfield['title']]) || @$userprofile[$userfield['title']] === '') { $missing = true; break; } } if (!$missing) { $badges = array('profiler'); $awarded += count(qa_badge_award_check($badges, false, $userid, null, 0)); } } } } // rebuild badges from other plugins - experimental! - $module->custom_badges_rebuild() returns number of badges awarded. $moduletypes = qa_list_module_types(); foreach ($moduletypes as $moduletype) { $modulenames = qa_list_modules($moduletype); foreach ($modulenames as $modulename) { $module = qa_load_module($moduletype, $modulename); if (method_exists($module, 'custom_badges_rebuild')) { $awarded += $module->custom_badges_rebuild(); } } } // badges $badges = array('medalist', 'champion', 'olympian'); if ($this->badge_activated($badges)) { $badgelist = qa_db_read_all_values(qa_db_query_sub('SELECT user_id FROM ^userbadges')); $users = array(); foreach ($badgelist as $idx => $medal) { $user = '******' . $medal; // get badge count if (isset($users[$user]) && isset($users[$user]['medals'])) { $users[$user]['medals']++; } else { $users[$user]['medals'] = 1; } unset($badgelist[$idx]); } foreach ($users as $user => $data) { $uid = (int) substr($user, 4); // check badges if (isset($data['medals'])) { $uid = (int) substr($user, 4); $count = $data['medals']; $awarded += count(qa_badge_award_check($badges, $count, $uid, null, 0)); } unset($users[$user]); } } // return ok text return $awarded . ' badge' . ($awarded != 1 ? 's' : '') . ' awarded.'; }
function qa_db_cookie_exists($cookieid) { return qa_db_read_one_value(qa_db_query_sub('SELECT COUNT(*) FROM ^cookies WHERE cookieid=#', $cookieid)) > 0; }