barl = (file_exists(THEME.'images/barl.png') ? THEME_ABS.'images/barl.png' : e_PLUGIN_ABS.'poll/images/barl.png'); $this->barr = (file_exists(THEME.'images/barr.png') ? THEME_ABS.'images/barr.png' : e_PLUGIN_ABS.'poll/images/barr.png'); $this->bar = (file_exists(THEME.'images/bar.png') ? THEME_ABS.'images/bar.png' : e_PLUGIN_ABS.'poll/images/bar.png'); } /* function remove_poll_cookies Remove unused poll cookies. See: http://krijnhoetmer.nl/stuff/javascript/maximum-cookies/ Thanks Fanat1k - bugtracker #4983 no parameters */ function remove_poll_cookies() { $arr_polls_cookies = array(); foreach($_COOKIE as $cookie_name => $cookie_val) { // Collect poll cookies list($str, $int) = explode('_', $cookie_name); if (($str == 'poll') && is_numeric($int)) { // Yes, its poll's cookie $arr_polls_cookies[] = $int; } } if (count($arr_polls_cookies) > 1) { // Remove all except first (assumption: there is always only one active poll) rsort($arr_polls_cookies); for($i = 1; $i < count($arr_polls_cookies); $i++) { cookie("poll_{$arr_polls_cookies[$i]}", "", (time() - 2592000)); } } } /* function delete_poll parameter in: $existing - existing poll id to be deleted parameter out: language text string on succesful delete, nothing on failed deletion */ function delete_poll($existing) { global $admin_log; $sql = e107::getDb(); if ($sql->delete("polls", " poll_id='".intval($existing)."' ")) { if (function_exists("admin_purge_related")) { admin_purge_related("poll", $existing); } e107::getLog()->add('POLL_01',LAN_AL_POLL_01.': '.$existing,''); //return POLL_ADLAN08; } } /* function clean_poll_array parameter in: original array with poll answers as entered in the forums parameter out: cleaned array which trims the poll answers (to avoid 'falsely' empty answers) but allows to have '0' as an option Note: Used instead of array_filter because array_filter($array, 'trim') would also ignore the value '0' (as that returns FALSE) http://www.bubjavier.com/common-problems-php-arrayfilter-no-callback */ function clean_poll_array($val) { $val = trim($val); // trims the array to remove poll answers which are (seemingly) empty but which may contain spaces $allowed_vals = array("0"); // Allows for '0' to be a poll answer option. Possible to add more allowed values. return in_array($val, $allowed_vals, true) ? true : ( $val ? true : false ); } /* function submit_poll $mode = 1 :: poll is main poll $mode = 2 :: poll is forum poll returns message */ function submit_poll($mode=1) { global $admin_log; $tp = e107::getParser(); $sql = e107::getDb(); $poll_title = $tp->toDB($_POST['poll_title']); $poll_comment = $tp->toDB($_POST['poll_comment']); $multipleChoice = intval($_POST['multipleChoice']); $showResults = intval($_POST['showResults']); $pollUserclass = intval($_POST['pollUserclass']); $storageMethod = intval($_POST['storageMethod']); $active_start = (!$_POST['startmonth'] || !$_POST['startday'] || !$_POST['startyear'] ? 0 : mktime (0, 0, 0, $_POST['startmonth'], $_POST['startday'], $_POST['startyear'])); $active_end = (!$_POST['endmonth'] || !$_POST['endday'] || !$_POST['endyear'] ? 0 : mktime (0, 0, 0, $_POST['endmonth'], $_POST['endday'], $_POST['endyear'])); $poll_options = ''; $_POST['poll_option'] = array_filter($_POST['poll_option'], 'poll::clean_poll_array'); foreach ($_POST['poll_option'] as $key => $value) { $poll_options .= $tp->toDB($value).chr(1); } if (POLLACTION == 'edit' || vartrue($_POST['poll_id'])) { $sql->update("polls", "poll_title='{$poll_title}', poll_options='{$poll_options}', poll_comment='{$poll_comment}', poll_type={$mode}, poll_allow_multiple={$multipleChoice}, poll_result_type={$showResults}, poll_vote_userclass={$pollUserclass}, poll_storage_method={$storageMethod} WHERE poll_id=".intval(POLLID)); /* update poll results - bugtracker #1124 .... */ $sql->select("polls", "poll_votes", "poll_id='".intval(POLLID)."' "); $foo = $sql->fetch(); $voteA = explode(chr(1), $foo['poll_votes']); $opt = count($poll_option) - count($voteA); if ($opt) { for($a=0; $a<=$opt; $a++) { $foo['poll_votes'] .= '0'.chr(1); } $sql->update("polls", "poll_votes='".$foo['poll_votes']."' WHERE poll_id='".intval(POLLID)."' "); } e107::getLog()->add('POLL_02','ID: '.POLLID.' - '.$poll_title,''); //$message = POLLAN_45; } else { $votes = ''; for($a=1; $a<=count($_POST['poll_option']); $a++) { $votes .= '0'.chr(1); } if ($mode == 1) { /* deactivate other polls */ if ($sql->select("polls", "*", "poll_type=1 AND poll_vote_userclass!=255")) { $deacArray = $sql->db_getList(); foreach ($deacArray as $deacpoll) { $sql->update("polls", "poll_end_datestamp='".time()."', poll_vote_userclass='255' WHERE poll_id=".$deacpoll['poll_id']); } } $ret = $sql->insert("polls", "'0', ".time().", ".intval($active_start).", ".intval($active_end).", ".ADMINID.", '{$poll_title}', '{$poll_options}', '{$votes}', '', '1', '".$tp->toDB($poll_comment)."', '".intval($multipleChoice)."', '".intval($showResults)."', '".intval($pollUserclass)."', '".intval($storageMethod)."'"); e107::getLog()->add('POLL_03','ID: '.$ret.' - '.$poll_title,''); // Intentionally only log admin-entered polls } else { $sql->insert("polls", "'0', ".intval($_POST['iid']).", '0', '0', ".USERID.", '$poll_title', '$poll_options', '$votes', '', '2', '0', '".intval($multipleChoice)."', '0', '0', '".intval($storageMethod)."'"); } } return $message; } function get_poll($query) { global $e107; $sql = e107::getDb(); if ($sql->gen($query)) { $pollArray = $sql->fetch(); if (!check_class($pollArray['poll_vote_userclass'])) { $POLLMODE = 'disallowed'; } else { switch($pollArray['poll_storage_method']) { case POLL_MODE_COOKIE: $userid = ''; $cookiename = 'poll_'.$pollArray['poll_id']; if (isset($_COOKIE[$cookiename])) { $POLLMODE = 'voted'; } else { $POLLMODE = 'notvoted'; } break; case POLL_MODE_IP: $userid = e107::getIPHandler()->getIP(FALSE); $voted_ids = explode('^', substr($pollArray['poll_ip'], 0, -1)); if (in_array($userid, $voted_ids)) { $POLLMODE = 'voted'; } else { $POLLMODE = 'notvoted'; } break; case POLL_MODE_USERID: if (!USER) { $POLLMODE = 'disallowed'; } else { $userid = USERID; $voted_ids = explode('^', substr($pollArray['poll_ip'], 0, -1)); if (in_array($userid, $voted_ids)) { $POLLMODE = 'voted'; } else { $POLLMODE = 'notvoted'; } } break; } } } else { return FALSE; } if (isset($_POST['pollvote']) && $POLLMODE == 'notvoted' && ($POLLMODE != 'disallowed')) { if ($_POST['votea']) { // $sql -> db_Select("polls", "*", "poll_vote_userclass!=255 AND poll_type=1 ORDER BY poll_datestamp DESC LIMIT 0,1"); $row = $pollArray; extract($row); $votes = explode(chr(1), $poll_votes); if (is_array($_POST['votea'])) { /* multiple choice vote */ foreach ($_POST['votea'] as $vote) { $vote = intval($vote); $votes[($vote-1)] ++; } } else { $votes[($_POST['votea']-1)] ++; } $optionArray = explode(chr(1), $pollArray['poll_options']); $optionArray = array_slice($optionArray, 0, -1); foreach ($optionArray as $k=>$v) { if (!$votes[$k]) { $votes[$k] = 0; } } $votep = implode(chr(1), $votes); $pollArray['poll_votes'] = $votep; $sql->update("polls", "poll_votes = '$votep'".($pollArray['poll_storage_method'] != POLL_MODE_COOKIE ? ", poll_ip='".$poll_ip.$userid."^'" : '')." WHERE poll_id=".$poll_id); /*echo " "; */ $poll_cookie_expire = time() + (3600 * 24 * 356 * 15); // FIXME cannot be used after 2023 (this year is the maxium unixstamp on 32 bit system) cookie('poll_'.$poll_id.'', $poll_id, $poll_cookie_expire); $POLLMODE = 'voted'; } } $this->pollRow = $pollArray; $this->pollmode = $POLLMODE; } function render_poll($pollArray = "", $type = "menu", $POLLMODE = "", $returnMethod=FALSE) { $ns = e107::getRender(); $tp = e107::getParser(); $sql = e107::getDb(); global $POLLSTYLE; switch ($POLLMODE) { case 'query' : // Show poll, register any vote if ($this->get_poll($pollArray) === FALSE) { return ''; // No display if no poll } $pollArray = $this->pollRow; $POLLMODE = $this->pollmode; break; case 'results' : if ($sql->gen($pollArray)) { $pollArray = $sql->fetch(); } break; } if ($type == 'preview') { $optionArray = array_filter($pollArray['poll_option']); $voteArray = array(); $voteArray = array_pad($voteArray, count($optionArray), 0); $pollArray['poll_allow_multiple'] = $pollArray['multipleChoice']; } else if ($type == 'forum') { if (isset($_POST['fpreview'])) { $pollArray['poll_allow_multiple'] = $pollArray['multipleChoice']; $optionArray = $pollArray['poll_option']; } else { $optionArray = explode(chr(1), $pollArray['poll_options']); $optionArray = array_slice($optionArray, 0, -1); } $voteArray = explode(chr(1), $pollArray['poll_votes']); // $voteArray = array_slice($voteArray, 0, -1); } else { // Get existing results $optionArray = explode(chr(1), $pollArray['poll_options']); $optionArray = array_slice($optionArray, 0, -1); $voteArray = explode(chr(1), $pollArray['poll_votes']); // $voteArray = array_slice($voteArray, 0, -1); } $voteTotal = array_sum($voteArray); $percentage = array(); if (count($voteArray)) { foreach ($voteArray as $votes) { if ($voteTotal > 0) { $percentage[] = round(($votes/$voteTotal) * 100, 2); } else { $percentage[] = 0; } } } /* get template */ if (file_exists(THEME.'poll_template.php')) { require(THEME.'poll_template.php'); } else if (!isset($POLL_NOTVOTED_START)) { require(e_PLUGIN.'poll/templates/poll_template.php'); } if(deftrue('BOOTSTRAP')) { if($type == 'forum') { require_once(e_PLUGIN."forum/templates/forum_poll_template.php"); $POLL_FORUM_NOTVOTED_START = $FORUM_POLL_TEMPLATE['form']['start']; $POLL_FORUM_NOTVOTED_LOOP = $FORUM_POLL_TEMPLATE['form']['item']; $POLL_FORUM_NOTVOTED_END = $FORUM_POLL_TEMPLATE['form']['end']; $POLL_FORUM_VOTED_START = $FORUM_POLL_TEMPLATE['results']['start']; $POLL_FORUM_VOTED_LOOP = $FORUM_POLL_TEMPLATE['results']['item']; $POLL_FORUM_VOTED_END = $FORUM_POLL_TEMPLATE['results']['end']; } $POLL_NOTVOTED_START = $POLL_TEMPLATE['form']['start']; $POLL_NOTVOTED_LOOP = $POLL_TEMPLATE['form']['item']; $POLL_NOTVOTED_END = $POLL_TEMPLATE['form']['end']; $POLL_VOTED_START = $POLL_TEMPLATE['results']['start']; $POLL_VOTED_LOOP = $POLL_TEMPLATE['results']['item']; $POLL_VOTED_END = $POLL_TEMPLATE['results']['end']; } $preview = FALSE; if ($type == 'preview') { $POLLMODE = 'notvoted'; } elseif ($type == 'forum') { $preview = TRUE; } $comment_total = 0; if ($pollArray['poll_comment']) { // Only get comments if they're allowed on poll. And we only need the count ATM $comment_total = $sql->count("comments", "(*)", "WHERE `comment_item_id`='".intval($pollArray['poll_id'])."' AND `comment_type`=4"); } $sc = e107::getScBatch('poll'); $sc->setVars($pollArray); $QUESTION = $tp->toHTML($pollArray['poll_title'], TRUE, "emotes_off, defs"); $VOTE_TOTAL = POLLAN_31.": ".$voteTotal; $COMMENTS = ($pollArray['poll_comment'] ? " ".LAN_COMMENTS.": ".$comment_total."" : ""); $poll_count = $sql->count("polls", "(*)", "WHERE poll_id <= '".$pollArray['poll_id']."'"); $OLDPOLLS = ''; if ($poll_count > 1) { $OLDPOLLS = ($type == 'menu' ? "".POLLAN_28."" : ""); } $AUTHOR = POLLAN_35." ".($type == 'preview' || $type == 'forum' ? USERNAME : "".$pollArray['user_name'].""); switch ($POLLMODE) { case 'notvoted': $text = "
"; break; case 'voted': case 'results' : if ($pollArray['poll_result_type'] && !strstr(e_SELF, "comment.php")) { $text = "