Esempio n. 1
0
 public static function KeepVoting()
 {
     global $DB, $VOTING_LAMP, $USER_ALREADY_VOTE, $USER_GROUP_PERMISSION, $USER;
     $err_mess = CAllVote::err_mess() . "<br>Function: KeepVoting<br>Line: ";
     $VOTING_LAMP = "green";
     $USER_ALREADY_VOTE = "N";
     $PUBLIC_VOTE_ID = intval($_REQUEST["PUBLIC_VOTE_ID"]);
     $aMsg = array();
     $VOTE_ID = 0;
     $arVote = array();
     $arQuestions = array();
     if (!(!empty($_REQUEST["vote"]) && $PUBLIC_VOTE_ID > 0 && check_bitrix_sessid())) {
         $aMsg[] = array("id" => "bad_params", "text" => GetMessage("VOTE_NOT_FOUND"));
     } elseif (($VOTE_ID = intVal(GetVoteDataByID($PUBLIC_VOTE_ID, $arChannel, $arVote, $arQuestions, $arAnswers, $arDropDown, $arMultiSelect, $arGroupAnswers, "N"))) && ($VOTE_ID <= 0 || $arVote["LAMP"] != "green")) {
         $VOTING_LAMP = "red";
         if ($VOTE_ID <= 0) {
             $aMsg[] = array("id" => "VOTE_ID", "text" => GetMessage("VOTE_NOT_FOUND"));
         } else {
             $aMsg[] = array("id" => "LAMP", "text" => GetMessage("VOTE_RED_LAMP"));
         }
     } elseif ($arChannel["USE_CAPTCHA"] == "Y" && !$USER->IsAuthorized()) {
         include_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/classes/general/captcha.php";
         $cpt = new CCaptcha();
         if (!empty($_REQUEST["captcha_word"])) {
             $captchaPass = COption::GetOptionString("main", "captcha_password", "");
             if (!$cpt->CheckCodeCrypt($_REQUEST["captcha_word"], $_REQUEST["captcha_code"], $captchaPass)) {
                 $GLOBALS["BAD_CAPTCHA"] = "Y";
                 $aMsg[] = array("id" => "CAPTCHA", "text" => GetMessage("VOTE_BAD_CAPTCHA"));
             }
         } else {
             if (!$cpt->CheckCode($_REQUEST["captcha_word"], 0)) {
                 $GLOBALS["NO_CAPTCHA"] = "Y";
                 $aMsg[] = array("id" => "CAPTCHA", "text" => GetMessage("VOTE_BAD_CAPTCHA"));
             }
         }
     }
     if (empty($aMsg)) {
         // get user id
         $_SESSION["VOTE_USER_ID"] = CVote::SetVoteUserID();
         $GLOBALS["VOTING_ID"] = $VOTE_ID;
         // check: can user vote
         $UNIQUE_TYPE = $arVote["UNIQUE_TYPE"];
         $KEEP_IP_SEC = $arVote["KEEP_IP_SEC"];
         $CHANNEL_ID = $arVote["CHANNEL_ID"];
         $StatusVote = CVote::UserAlreadyVote($VOTE_ID, $_SESSION["VOTE_USER_ID"], $UNIQUE_TYPE, $KEEP_IP_SEC, $USER->GetID());
         $USER_ALREADY_VOTE = $StatusVote != false ? "Y" : "N";
         $USER_GROUP_PERMISSION = CVote::UserGroupPermission($CHANNEL_ID);
         // if user can vote that
         if (($USER_ALREADY_VOTE == "N" || $StatusVote == 8 && $_REQUEST["REVOTE_ID"] == $VOTE_ID) && $USER_GROUP_PERMISSION >= 2) {
             $arSqlAnswers = array();
             // check answers
             foreach ($arQuestions as $qID => $arQuestion) {
                 $arSqlAnswers[$arQuestion["ID"]] = array();
                 $bIndicators = array();
                 foreach ($arQuestion["ANSWERS"] as $arAnswer) {
                     switch ($arAnswer["FIELD_TYPE"]) {
                         case 0:
                             // radio
                         // radio
                         case 2:
                             // dropdown list
                             $fieldName = ($arAnswer["FIELD_TYPE"] == 0 ? "vote_radio_" : "vote_dropdown_") . $qID;
                             $aID = intval($GLOBALS[$fieldName]);
                             if (!isset($bIndicators[$fieldName]) && array_key_exists($aID, $arQuestion["ANSWERS"])) {
                                 if (!empty($arAnswer['MESSAGE'])) {
                                     $arSqlAnswers[$qID][$aID] = array("ANSWER_ID" => $aID);
                                 }
                                 $bIndicators[$fieldName] = "Y";
                             }
                             break;
                         case 1:
                             // checkbox
                         // checkbox
                         case 3:
                             // multiselect list
                             $fieldName = ($arAnswer["FIELD_TYPE"] == 1 ? "vote_checkbox_" : "vote_multiselect_") . $qID;
                             $res = $GLOBALS[$fieldName];
                             if (!isset($bIndicators[$fieldName]) && is_array($res) && !empty($res)) {
                                 reset($res);
                                 foreach ($res as $aID) {
                                     if (array_key_exists($aID, $arQuestion["ANSWERS"])) {
                                         $arSqlAnswers[$qID][$aID] = array("ANSWER_ID" => $aID);
                                     }
                                 }
                                 $bIndicators[$fieldName] = "Y";
                             }
                             break;
                         case 4:
                             // field
                         // field
                         case 5:
                             // text
                             $aID = $arAnswer["ID"];
                             $fieldName = ($arAnswer["FIELD_TYPE"] == 4 ? "vote_field_" : "vote_memo_") . $aID;
                             $MESSAGE = trim($GLOBALS[$fieldName]);
                             if ($MESSAGE != "") {
                                 $arSqlAnswers[$qID][$aID] = array("ANSWER_ID" => $aID, "MESSAGE" => "'" . $DB->ForSql(trim($MESSAGE), 2000) . "'");
                             }
                             break;
                     }
                 }
                 if (empty($arSqlAnswers[$qID])) {
                     unset($arSqlAnswers[$qID]);
                     if ($arQuestion['REQUIRED'] == 'Y') {
                         $aMsg[] = array("id" => "QUESTION_" . $qID, "text" => GetMessage("VOTE_REQUIRED_MISSING"));
                         $GLOBALS["VOTE_REQUIRED_MISSING"] = "Y";
                         break;
                     }
                 }
             }
             if (!empty($aMsg)) {
                 /* do nothing; */
             } elseif (!empty($arSqlAnswers)) {
                 // vote event
                 $arFields = array("VOTE_ID" => $VOTE_ID, "VOTE_USER_ID" => intval($_SESSION["VOTE_USER_ID"]), "DATE_VOTE" => $DB->GetNowFunction(), "STAT_SESSION_ID" => intval($_SESSION["SESS_SESSION_ID"]), "IP" => "'" . $DB->ForSql($_SERVER["REMOTE_ADDR"], 15) . "'", "VALID" => "'Y'");
                 /***************** Event onBeforeVoting ****************************/
                 foreach (GetModuleEvents("vote", "onBeforeVoting", true) as $arEvent) {
                     if (ExecuteModuleEventEx($arEvent, array(&$arFields, &$arSqlAnswers)) === false) {
                         return false;
                     }
                 }
                 /***************** /Event ******************************************/
                 if ($StatusVote == 8 && $_REQUEST["REVOTE_ID"] == $VOTE_ID) {
                     $strSql = "SELECT VE.ID, VEQ.QUESTION_ID, VEA.ANSWER_ID" . " FROM b_vote_event VE " . "\tLEFT JOIN b_vote_event_question VEQ ON (VEQ.EVENT_ID=VE.ID)" . "\tLEFT JOIN b_vote_event_answer VEA ON (VEA.EVENT_QUESTION_ID=VEQ.ID)" . "\tLEFT JOIN b_vote_user VU ON (VE.VOTE_USER_ID = VU.ID)" . " WHERE VE.VOTE_ID=" . $VOTE_ID . " AND VU.AUTH_USER_ID=" . intval($USER->GetID()) . " ORDER BY VE.ID ASC, VEQ.QUESTION_ID ASC";
                     $db_res = $DB->Query($strSql, false, $err_mess . __LINE__);
                     if ($db_res && ($res = $db_res->Fetch())) {
                         if ($USER->GetID() > 0 && CModule::IncludeModule("im")) {
                             CIMNotify::DeleteByTag("VOTING|" . $VOTE_ID, $USER->GetID());
                         }
                         $del = false;
                         $delQ = false;
                         do {
                             if ($delQ !== $res["QUESTION_ID"]) {
                                 if ($del !== $res["ID"]) {
                                     CVoteEvent::Delete($res["ID"]);
                                     $del = $res["ID"];
                                     $arVote["COUNTER"] = intval($arVote["COUNTER"]) - 1;
                                 }
                                 $delQ = $res["QUESTION_ID"];
                                 $arQuestions[$res["QUESTION_ID"]]["COUNTER"] = intval($arQuestions[$res["QUESTION_ID"]]["COUNTER"]) - 1;
                             }
                             $arQuestions[$res["QUESTION_ID"]]["ANSWERS"][$res["ANSWER_ID"]]["COUNTER"] = intval($arQuestions[$res["QUESTION_ID"]]["ANSWERS"][$res["ANSWER_ID"]]["COUNTER"]) - 1;
                         } while ($res = $db_res->Fetch());
                     }
                     $USER_ALREADY_VOTE = "N";
                 }
                 unset($GLOBALS["VOTE_CACHE_VOTING"][$VOTE_ID]);
                 unset($GLOBALS["VOTE_CACHE"]["VOTE"][$VOTE_ID]);
                 $EVENT_ID = intval($DB->Insert("b_vote_event", $arFields, $err_mess . __LINE__));
                 if ($EVENT_ID > 0) {
                     $arSqlQuestionsID = array();
                     $arSqlAnswersID = array();
                     foreach ($arSqlAnswers as $qID => $arSqlAnswer) {
                         $arFields = array("EVENT_ID" => $EVENT_ID, "QUESTION_ID" => $qID);
                         $EVENT_QUESTION_ID = intval($DB->Insert("b_vote_event_question", $arFields, $err_mess . __LINE__));
                         if ($EVENT_QUESTION_ID > 0) {
                             $arSqlQuestionsID[] = $qID;
                             $arQuestions[$qID]["COUNTER"] = intval($arQuestions[$qID]["COUNTER"]) + 1;
                             foreach ($arSqlAnswer as $aID => $res) {
                                 $res["EVENT_QUESTION_ID"] = $EVENT_QUESTION_ID;
                                 if ($DB->Insert("b_vote_event_answer", $res, $err_mess . __LINE__)) {
                                     $arSqlAnswersID[$aID] = $qID;
                                     $arQuestions[$qID]["ANSWERS"][$aID]["COUNTER"] = intval($arQuestions[$qID]["ANSWERS"][$aID]["COUNTER"]) + 1;
                                 }
                             }
                         }
                     }
                     if (empty($arSqlQuestionsID) || empty($arSqlAnswersID)) {
                         $DB->Query("DELETE FROM b_vote_event WHERE ID=" . $EVENT_ID, $arFields, $err_mess . __LINE__);
                     } else {
                         $arFields = array("COUNTER" => "COUNTER+1");
                         $DB->Update("b_vote", $arFields, "WHERE ID='" . $VOTE_ID . "'", $err_mess . __LINE__);
                         $arVote["COUNTER"] = intval($arVote["COUNTER"]) + 1;
                         $DB->Update("b_vote_question", $arFields, "WHERE ID in (" . implode(", ", $arSqlQuestionsID) . ")", $err_mess . __LINE__);
                         $DB->Update("b_vote_answer", $arFields, "WHERE ID in (" . implode(", ", array_keys($arSqlAnswersID)) . ")", $err_mess . __LINE__);
                         // increment user counter
                         CVote::UpdateVoteUserID($_SESSION["VOTE_USER_ID"]);
                         $GLOBALS["VOTING_OK"] = "Y";
                         $_SESSION["VOTE_ARRAY"][] = $VOTE_ID;
                         if ($UNIQUE_TYPE & 8) {
                             $_SESSION["VOTE"]["VOTES"][$VOTE_ID] = $EVENT_ID;
                         }
                         // statistic module
                         if (CModule::IncludeModule("statistic")) {
                             $event3 = $arVote["EVENT3"];
                             if (!empty($event3)) {
                                 $event3 = "http://" . $_SERVER["HTTP_HOST"] . "/bitrix/admin/vote_user_results.php?EVENT_ID=" . $EVENT_ID . "&lang=" . LANGUAGE_ID;
                             }
                             CStatEvent::AddCurrent($arVote["EVENT1"], $arVote["EVENT2"], $event3);
                         }
                         // notification
                         if (!!$arVote["AUTHOR_ID"] && $arVote["AUTHOR_ID"] != $USER->GetID()) {
                             if (empty($arVote["TITLE"])) {
                                 $arQuestion = reset($arQuestions);
                                 $arVote["TITLE"] = $arQuestion["QUESTION"];
                             }
                             if ($arVote["NOTIFY"] == "I" && CModule::IncludeModule("im")) {
                                 $arVote["TOTAL_URL"] = "";
                                 if (!empty($arVote["URL"])) {
                                     if (defined('SITE_SERVER_NAME')) {
                                         $arVote["TOTAL_URL"] = SITE_SERVER_NAME;
                                     }
                                     $arVote["TOTAL_URL"] = !empty($arVote["TOTAL_URL"]) ? $arVote["TOTAL_URL"] : COption::GetOptionString("main", "server_name", $GLOBALS["SERVER_NAME"]);
                                     if (!empty($arVote["TOTAL_URL"])) {
                                         $arVote["TOTAL_URL"] = (CMain::IsHTTPS() ? "https" : "http") . "://" . $arVote["TOTAL_URL"] . $arVote["URL"];
                                     }
                                 }
                                 // send notification
                                 $gender = $USER->getParam("PERSONAL_GENDER") == "F" ? "_F" : "";
                                 $arMessageFields = array("MESSAGE_TYPE" => IM_MESSAGE_SYSTEM, "TO_USER_ID" => $arVote["AUTHOR_ID"], "FROM_USER_ID" => $USER->GetID(), "NOTIFY_TYPE" => IM_NOTIFY_FROM, "NOTIFY_MODULE" => "vote", "NOTIFY_EVENT" => "voting", "NOTIFY_TAG" => "VOTING|" . $VOTE_ID, "NOTIFY_MESSAGE" => !empty($arVote["URL"]) ? GetMessage("V_NOTIFY_MESSAGE_HREF" . $gender, array("#VOTE_TITLE#" => $arVote["TITLE"], "#VOTE_URL#" => $arVote["URL"])) : GetMessage("V_NOTIFY_MESSAGE" . $gender, array("#VOTE_TITLE#" => $arVote["TITLE"])), "NOTIFY_MESSAGE_OUT" => !empty($arVote["TOTAL_URL"]) ? GetMessage("V_NOTIFY_MESSAGE_OUT_HREF" . $gender, array("#VOTE_TITLE#" => $arVote["TITLE"], "#VOTE_URL#" => $arVote["TOTAL_URL"])) : GetMessage("V_NOTIFY_MESSAGE" . $gender, array("#VOTE_TITLE#" => $arVote["TITLE"])));
                                 CIMNotify::Add($arMessageFields);
                             } else {
                                 if ($arVote["NOTIFY"] == "Y") {
                                     // send e-mail
                                     $db_user = CUser::GetById($arVote["AUTHOR_ID"]);
                                     if ($db_user && ($arUser = $db_user->Fetch()) && !empty($arUser["EMAIL"])) {
                                         $arEventFields = array("EMAIL_TO" => $arUser["EMAIL"], "VOTE_STATISTIC" => "", "ID" => $EVENT_ID, "TIME" => GetTime(time(), "FULL"), "VOTE_TITLE" => $arVote["TITLE"], "VOTE_DESCRIPTION" => $arVote["DESCRIPTION"], "VOTE_ID" => $arVote["ID"], "VOTE_COUNTER" => $arVote["COUNTER"], "URL" => $arVote["URL"], "CHANNEL" => $arChannel["TITLE"], "CHANNEL_ID" => $arChannel["ID"], "VOTER_ID" => $_SESSION["VOTE_USER_ID"], "USER_NAME" => $USER->GetFullName(), "LOGIN" => $USER->GetLogin(), "USER_ID" => $USER->GetID(), "STAT_GUEST_ID" => intval($_SESSION["SESS_GUEST_ID"]), "SESSION_ID" => intval($_SESSION["SESS_SESSION_ID"]), "IP" => $_SERVER["REMOTE_ADDR"]);
                                         $arEventFields["USER_NAME"] = !!$arEventFields["USER_NAME"] ? $arEventFields["USER_NAME"] : $arEventFields["LOGIN"];
                                         // VOTE_STATISTIC
                                         $text = array();
                                         foreach ($arSqlAnswersID as $aID => $qID) {
                                             $text[$qID] = is_array($text[$qID]) ? $text[$qID] : array();
                                             if ($arQuestions[$qID]["ANSWERS"][$aID]["FIELD_TYPE"] == 4 || $arQuestions[$qID]["ANSWERS"][$aID]["FIELD_TYPE"] == 5) {
                                                 if (!empty($arSqlAnswers[$qID][$aID]["MESSAGE"])) {
                                                     $text[$qID][] = $arSqlAnswers[$qID][$aID]["MESSAGE"];
                                                 }
                                             } else {
                                                 $text[$qID][] = $arQuestions[$qID]["ANSWERS"][$aID]["MESSAGE"];
                                             }
                                         }
                                         foreach ($text as $qID => $txt) {
                                             $text[$qID] = " - " . $arQuestions[$qID]["QUESTION"] . "\n - " . implode(", ", $text[$qID]);
                                         }
                                         $arEventFields["VOTE_STATISTIC"] = "\n" . implode("\n\n", $text);
                                         $arrSites = CVoteChannel::GetSiteArray($arChannel["ID"]);
                                         CEvent::Send("VOTE_FOR", $arrSites, $arEventFields, "N");
                                     }
                                 }
                             }
                         }
                     }
                     /***************** Event onAfterVoting *****************************/
                     foreach (GetModuleEvents("vote", "onAfterVoting", true) as $arEvent) {
                         ExecuteModuleEventEx($arEvent, array($VOTE_ID, $EVENT_ID));
                     }
                     /***************** /Event ******************************************/
                 }
             } else {
                 $GLOBALS["USER_VOTE_EMPTY"] = "Y";
                 $aMsg[] = array("id" => "VOTE_ID", "text" => GetMessage("USER_VOTE_EMPTY"));
             }
         } else {
             $aMsg[] = array("id" => "VOTE_ID", "text" => $USER_GROUP_PERMISSION >= 2 ? GetMessage("VOTE_ALREADY_VOTE") : GetMessage("VOTE_ACCESS_DENIED"));
         }
     }
     if (!empty($aMsg)) {
         $e = new CAdminException($aMsg, "CVote::KeepVoting");
         $GLOBALS["APPLICATION"]->ThrowException($e);
         $GLOBALS["VOTING_OK"] = "N";
         return false;
     }
     return true;
 }