function dob_set_remote_addr($user_login) { /*{{{*/ $user_obj = get_user_by('login', $user_login); $_SESSION['user_id'] = $user_obj->ID; $_SESSION['user_login'] = $user_login; $_SESSION['LOGIN_IP'] = dob_get_real_ip(); //wp_redirect("http://www.google.com"); //exit; }
function dob_elect_contents($post_id, $bEcho = false) { #echo '<pre>'; global $wpdb; $dob_elect_cmb_vote = get_post_meta($post_id, 'dob_elect_cmb_vote', true); $vm_type = empty($dob_elect_cmb_vote['type']) ? 'updown' : $dob_elect_cmb_vote['type']; $vm_data = empty($dob_elect_cmb_vote['data']) ? array() : $dob_elect_cmb_vote['data']; $vm_begin = empty($dob_elect_cmb_vote['begin']) ? '' : $dob_elect_cmb_vote['begin']; $vm_end = empty($dob_elect_cmb_vote['end']) ? '' : $dob_elect_cmb_vote['end']; $user_id = get_current_user_id(); $LOGIN_IP = empty($_SESSION['LOGIN_IP']) ? '' : $_SESSION['LOGIN_IP']; $bVote = false; if (is_single() && $user_id) { $debug = ''; if (!empty($_POST) && $LOGIN_IP == dob_get_real_ip()) { $bVote = true; if ((int) $_POST['dob_form_cart']) { $debug = dob_common_cart($user_id, $post_id, 'elect'); } else { $debug = dob_common_update($user_id, $post_id, 'elect'); } } if ($debug) { echo "<pre>{$debug}</pre>"; } } // load CACHE $ts_struct = $ts_all = $ts_stat = ''; $ts_post = get_the_modified_time('Y-m-d H:i:s'); $cached_struct = dob_common_cache(-1, 'all', false, $ts_struct); $cached_all = dob_common_cache($post_id, 'all', false, $ts_all); $cached_stat_json = dob_common_cache($post_id, 'stat', false, $ts_stat, false); $ttids = $elect_latest = $result_stat = $stat = null; if (is_array($cached_all)) { extract($cached_all); } $nTotal = null; $nDirect = null; if (!empty($stat)) { extract($stat); } // ttids: 신규, 포스트변경, 계층변경 $bTtids = false; if ($ttids === null || $ts_all < $ts_post || $ts_all < $ts_struct) { $ttids = dob_common_get_selected_hierarchy_leaf_ttids($post_id); $bTtids = true; } // nTotal: 신규, 포스트변경, 계층변경 if ($nTotal === null || $ts_all < $ts_post || $ts_all < $ts_struct) { $nTotal = dob_common_get_users_count($ttids); // get all user count } // elect_latest, stat_detail: 신규, 투표, 포스트변경, 계층변경 $bLatest = false; if ($elect_latest === null || $bVote || $ts_all < $ts_post || $ts_all < $ts_struct) { $elect_latest = dob_common_get_latest_by_ttids($post_id, $ttids, 'elect'); // user_id => rows // for login_name $nDirect = is_array($elect_latest) ? count($elect_latest) : 0; $bLatest = true; } // build final vote results. $myinfo = $user_id ? dob_common_get_user_info($user_id) : null; $myval = isset($elect_latest[$user_id]) ? (int) $elect_latest[$user_id]['value'] : ''; #print_r($elect_latest); // Cache STAT, 통계: 신규, 실제 통계값 변경 $bStat = false; $stat = ['nDirect' => $nDirect, 'nTotal' => $nTotal]; $stat_json = json_encode($stat, JSON_UNESCAPED_UNICODE); if (empty($cached_stat_json) || $cached_stat_json != $stat_json) { $ts_now = date('Y-m-d H:i:s'); dob_common_cache($post_id, 'stat', $stat_json, $ts_now, false); $bStat = true; } $bResult = false; if (strtotime($vm_end) < time() && ($result_stat === null || $bTtids || $bLatest || $bStat)) { // AFTER voting period $result_stat = array(); foreach ($elect_latest as $uid => $v) { dob_elect_accum_stat($result_stat, $vm_type, $v['value'], 1); } $bResult = true; } if ($bTtids || $bLatest || $bStat || $bResult) { // Cache Results, 결과: 신규, 포스트변경, 계층변경 $data = compact('ttids', 'elect_latest', 'result_stat', 'stat'); dob_common_cache($post_id, 'all', $data); } ## build HTML /*{{{*/ //$label_title = '균형 투표'; //__('Balance Voting', DOBslug); $label_result = '투표결과'; //__('My Vote', DOBslug); $label_before = '시작전'; //__('Statistics', DOBslug); $label_ing = '진행중'; //__('Statistics', DOBslug); $label_after = '종료됨'; //__('Statistics', DOBslug); $label_chart = '결과 차트'; //__('Direct voter', DOBslug); $label_my = '내 투표'; //__('My Vote', DOBslug); $label_history = '기록'; //__('My Vote', DOBslug); $label_login = '******'; //__('Please Login', DOBslug); /*}}}*/ $html_timer = $html_chart = $html_form = $html_history = ''; if (is_single()) { $vm_label = $vm_type == 'updown' ? array(-1 => '반대', 0 => '기권') : array(-1 => '모두반대', 0 => '기권'); if ($vm_type == 'updown') { $vm_label[1] = '찬성'; } else { // choice, plural foreach ($vm_data as $k => $v) { $vm_label[$k + 1] = $v; } } /*}}}*/ $ts = time(); $html_timer = dob_elect_html_timer($ts); if ($ts < strtotime($vm_begin)) { // BEFORE $label_result .= ' : ' . $label_before; } elseif (strtotime($vm_begin) < $ts && $ts < strtotime($vm_end)) { // VOTING /*{{{*/ $label_result .= ' : ' . $label_ing; $content_form = "<a href='" . wp_login_url($_SERVER['REQUEST_URI']) . "' style='color:red; font-weight:bold'>{$label_login}</a>"; if ($user_id) { if (is_null($ttids)) { $content_form = '선거대상 계층이 지정되지 않았습니다.'; //__('Election Hierarchy does not selected.', DOBslug); } elseif (empty($ttids) && empty($myinfo->term_taxonomy_id) || is_array($ttids) && !empty($ttids) && !in_array($myinfo->term_taxonomy_id, $ttids)) { $label_restrict = '선거대상 계층이 아닙니다.'; //__('Your hierarchy is not available in this voting.', DOBslug); $content_form = "<span style='color:red; font-weight:bold'>{$label_restrict}</span>"; } elseif (isset($_SESSION['LOGIN_IP']) && $_SESSION['LOGIN_IP'] == dob_get_real_ip()) { $content_form = dob_elect_display_mine($post_id, $vm_type, $vm_label, $myval, $user_id); } else { $content_form = '로그인 이후 1시간이 지났거나, 네트워크가 변경되었으니, 다시 로그인해 주세요<br>투표시에는 네트워크(WIFI,LTE,3G)를 변경하지 마세요.'; //__('You passed 1-hours after login, or Your network was Changed. Please Login AGAIN.', DOBslug); } } $html_form = <<<HTML <div class="panel panel-default"> <div class="panel-heading" data-toggle="collapse" data-target="#dob_elect_my"> <span class="panel-title">{$label_my}</span> </div> <div id="dob_elect_my" class="panel-collapse collapse in"> \t\t\t\t{$content_form} </div> </div> HTML; /*}}}*/ } else { // AFTER /*{{{*/ $label_result .= ' : ' . $label_after; $content_chart = dob_elect_html_chart($result_stat, $vm_label, $nTotal, $nDirect, $vm_type == 'plural'); $html_chart = <<<HTML <div class="panel panel-default"> <div class="panel-heading" data-toggle="collapse" data-target="#dob_elect_html_chart"> <span class="panel-title">{$label_chart}</span> </div> <div id="dob_elect_html_chart" class="panel-collapse collapse in"> {$content_chart} </div> </div> HTML; } /*}}}*/ } $html_stat = dob_elect_html_stat($nDirect, $nTotal, $vm_begin, $vm_end, false); $dob_elect = <<<HTML \t{$html_stat} \t{$html_chart} \t{$html_form} \t{$html_history} HTML; if ($bEcho) { echo $dob_elect; } else { return $dob_elect; } }
/** * Create site pages for this plugin */ add_action('wp', 'dob_wp_init'); function dob_wp_init() { /*{{{*/ // Load js file wp_enqueue_script('bdd-js', plugins_url('assets/js/bdd.js', __FILE__), array('jquery')); wp_localize_script('bdd-js', 'bddjs', array('ajaxurl' => admin_url('admin-ajax.php'))); // Load css file wp_enqueue_style('bdd-css', plugins_url('assets/css/bdd.css', __FILE__)); } /*}}}*/ $global_real_ip = dob_get_real_ip(); function dob_get_real_ip() { /*{{{*/ if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR'];
function dob_vote_display_mine($post_id, $vm_type, $vm_legend, $myval = '', $user_id) { /*{{{*/ ob_start(); //session_unset(); // $_SESSION = array(); $LOGIN_IP = empty($_SESSION['LOGIN_IP']) ? '' : $_SESSION['LOGIN_IP']; $_SESSION['user_id'] = $user_id; $_SESSION['post_id'] = $post_id; $_SESSION['secret'] = $secret = base64_encode(openssl_random_pseudo_bytes(20)); if ($vm_type == 'plural') { // normalize plural value if (is_null($myval) || $myval === '') { $myvals = array(); } else { if ($myval <= 1) { $myvals = array($myval); } else { $myvals = array(); $vals = str_split(strrev(base_convert($myval, 10, 2))); //$myval = array_keys(array_filter($vals, function($v){return $v=='1';})); foreach ($vals as $k => $v) { if ($v == '1') { $myvals[] = $k + 1; } } } } } $label_secret = '보안코드'; //__('Statistics', DOBslug); $label_remember = '암호화된 DB에서 직접 투표확인을 원하시면 이 값을 기억해 주세요.'; //__('Statistics', DOBslug); $html_plural_inputs = $vm_type == 'plural' ? '<input type="hidden" name="dob_form_val" value="' . $myval . '">' : ''; // display area echo <<<HTML \t\t<div class="panel"> \t\t<table> \t\t\t<form id="formDob" method="post"> \t\t\t<input type="hidden" name="dob_form_type" value="{$vm_type}"> \t\t\t<input type="hidden" name="dob_form_cart" value="0"> \t\t\t<input type="hidden" name="dob_form_old_val" value="{$myval}"> {$html_plural_inputs} \t\t\t<!--tr><td> \t\t\t\t{$label_secret} : <input type="text" name="dob_vote_secret" value="{$secret}" style="width:300px" READONLY> \t\t\t\t<br><b>{$label_remember}</b> \t\t\t</td></tr--> \t\t\t<tr><td id="tdVote"><div class="well"> HTML; wp_nonce_field('dob_form_nonce_' . $vm_type, 'dob_form_nonce'); foreach ($vm_legend as $val => $label) { $html_input = ''; if ($vm_type == 'plural') { $control = in_array($val, $myvals) ? 'CHECKED' : ($myval === 0 || $myval === -1 ? 'DISABLED' : ''); $exp = $val < 1 ? $val : 1 << $val - 1; $html_input = "<input type='checkbox' data-idx='{$val}' value='{$exp}' {$control}>"; } else { $checked = $val === $myval ? 'CHECKED' : ''; $html_input = "<input type='radio' name='dob_form_val' value='{$val}' {$checked}>"; } echo " <label class='radio-inline checkbox-inline'>{$html_input}{$label}</label> "; } $html_submit = empty($user_id) ? $label_login : dob_common_get_message($post_id, $user_id, 'offer'); // vote_post_latest timestamp if ($LOGIN_IP == dob_get_real_ip()) { $label_fast = '바로투표'; //__('Vote', DOBslug); $label_cart = '투표바구니'; //__('Vote', DOBslug); $html_submit .= " <input id='btn_fast' type='button' value='{$label_fast}' class='btn btn-success btn-sm' >"; $html_submit .= " <input id='btn_cart' type='button' value='{$label_cart}' class='btn btn-warning btn-sm' >"; } else { $label_iperr_relogin = '******'; //__('You passed 1-hours after login, or Your network was Changed. Please Login AGAIN.', DOBslug); $html_submit .= '<br>' . $label_iperr_relogin; } echo <<<HTML \t\t\t</div></td></tr> \t\t\t<tr><td nowrap style="text-align:right;">{$html_submit}</td></tr> \t\t\t</form> \t\t</table> \t\t</div> HTML; $ret = ob_get_contents(); ob_end_clean(); return $ret; }