// /polls/index.php require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/functions.inc.php"); require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/functions.start_session.php"); require_once($_SERVER['DOCUMENT_ROOT'] . "/includes/functions.polls.php"); if (!ksw_feature_enabled('polls')){ die_missing_page(); } $xPagePolls = ksw_page_feature_row_required('polls'); ksw_dbtable_require_fields($xPagePolls, array('domain','is_visible','url','section_title','page_title','header_title','meta_title','intro_html','leader_html','meta_keywords','meta_description','date_updated'), 'Polls settings'); $pollsBaseTitle = ksw_required_nonempty_text_value($xPagePolls, 'page_title', 'Polls settings'); if (trim((string)($xPagePolls->meta_description ?? '')) !== '') $meta_description = trim((string)$xPagePolls->meta_description); if (trim((string)($xPagePolls->meta_keywords ?? '')) !== '') $meta_keywords = trim((string)$xPagePolls->meta_keywords); $page_title = $pollsBaseTitle; $major_header = "polls"; $page_requires_login = false; $flash_ok = $_SESSION['polls_front_ok'] ?? ''; $flash_err = $_SESSION['polls_front_err'] ?? ''; unset($_SESSION['polls_front_ok'], $_SESSION['polls_front_err']); $poll_id = intval($_GET['id'] ?? 0); $poll = null; $is_logged_in = (isset($xThisAccount) && $xThisAccount->valid); $account_id = $is_logged_in ? intval($xThisAccount->ID) : 0; $voter_token = poll_get_voter_token(); if ($poll_id > 0) { $poll = poll_load_poll($poll_id); if (!$poll->valid || ($poll->is_visible ?? 'n') !== 'y') $poll = null; } // Handle vote POST if (($_POST['action'] ?? '') === 'vote' && $poll_id > 0) { if (!ksw_request_is_same_origin() || !ksw_verify_csrf_token()) { $_SESSION['polls_front_err'] = 'Your secure voting form expired. Please try again.'; header("Location: /polls/?id=" . intval($poll_id)); exit; } $poll = poll_load_poll($poll_id); if (!$poll->valid || ($poll->is_visible ?? 'n') !== 'y') { $_SESSION['polls_front_err'] = "Poll not found."; header("Location: /polls/"); exit; } if (!poll_is_open($poll)) { $_SESSION['polls_front_err'] = "This poll is not accepting votes."; header("Location: /polls/?id=" . intval($poll_id)); exit; } $require_login = (($poll->require_login ?? 'n') === 'y'); if ($require_login && !$is_logged_in) { $_SESSION['polls_front_err'] = "Please log in to vote on this poll."; $ref = "/polls/?id=" . intval($poll_id); header("Location: /account/login.php?ref_url=" . urlencode($ref)); exit; } $choice = $_POST['option_id'] ?? []; $res = poll_vote($poll, $account_id, $voter_token, $choice); if (($res['ok'] ?? false) === true) $_SESSION['polls_front_ok'] = "Thanks for your vote!"; else $_SESSION['polls_front_err'] = ($res['error'] ?? "Failed to vote."); header("Location: /polls/?id=" . intval($poll_id)); exit; } function polls_h($s){ return mb_htmlentities((string)$s); } function polls_render_results($results){ $total = intval($results['total'] ?? 0); $rows = $results['rows'] ?? []; echo '