/** * * @param unknown $where array colname == value all pairs are treated as AND conditions * @param unknown $tablename * @param unknown $colnames String for only one column oder array of strings * @return array[] of all data rows matching where if colnames is a string, array[][$colnames] if $colnames is an array */ function load($where, $tablename, $colnames) { if (is_array($colnames)) { $colarray = $colnames; } else { $colarray[0] = $colnames; } $colnames_ = implode(', ', $colarray); $fromDB = $this->connection->load($where, $tablename, $colnames_); $ret = array(); // this is necessary because sometimes $fromDB contains an empty array foreach ($colarray as $answColNum => $cname) { $colnum = find_in_subarray($this->connection->evtables[$tablename], 'name', $cname); if ($fromDB === false) { return false; } foreach ($fromDB as $rownum => $gotrow) { // json decode if column is json if ($this->connection->evtables[$tablename][$colnum]['json']) { $value = json_decode($gotrow[$answColNum], true); } else { $value = $gotrow[$answColNum]; } if (is_array($colnames)) { $ret[$rownum][$cname] = $value; } else { $ret[$rownum] = $value; } } } return $ret; }
function find_in_subarray($r, $d) { foreach ($r as $k => $v) { if ($k == $d) { $ret = $v; } if (is_array($v) && !$ret) { $ret = find_in_subarray($v, $d); } } if ($ret) { return $ret; } }
/** * * @param unknown $configId * @return unknown|boolean false if 'configId' is not set, array otherwise */ function getExternalTokenConfig() { global $externalTokenConfig; if (isset($this->authConfig['configId'])) { $i = find_in_subarray($externalTokenConfig, 'configId', $this->authConfig['configId']); if ($i === false) { WrongRequestException::throwException(38573, 'externalTokenAuth: configId not found in server config', print_r($this->authConfig['configId'], true)); } return $externalTokenConfig[$i]; } else { return false; } }
function verifyBallots($voterReq, $blindedHashesFromDB) { // $voterReq: .ballots[] .electionId .VoteId //.blindedHash[] .serversAlreadySigned[] //contains an array saying the x-th server has to sign y ballots // verify if user is allowed to vote and status of communication (done: pickBallots, next: signBallots) is correct // verify content of the ballot // verify if the correct number of ballots was sent: if (count($voterReq["ballots"]) != $this->numVerifyBallots) { WrongRequestException::throwException('not the correct number of ballots sent'); } $tmpret = array(); if ($voterReq['electionId'] != $this->electionId) { WrongRequestException::throwException(210, 'Error: electionID is wrong', "expected electionID: {$this->electionId}, received electionID in ballot {$i}: " . $voterReq['ballots'][$i]['electionId']); } if (!isset($voterReq['questions']) || !is_array($voterReq['questions'])) { WrongRequestException::throwException(223, "Error: questions must be set and an array", print_r($voterReq, true)); } for ($q = 0; $q < count($voterReq['questions']); $q++) { if (!isset($voterReq["questions"][$q]['questionID']) || !(is_int($voterReq["questions"][$q]['questionID']) || is_string($voterReq["questions"][$q]['questionID']))) { WrongRequestException::throwException(227, 'Error: verifyBallots(): missing questionID or it is not of type int', $q); } $qno = find_in_subarray($voterReq["questions"], 'questionID', $blindedHashesFromDB['blindedHashes']['questions'][$q]['questionID']); if ($qno === false) { WrongRequestException::throwException(237, 'Error: All questions must be disclosed', 'Number in question array for which no questionID was found in voterrequest: ' . $q); } if (!isset($voterReq['questions'][$q]['ballots']) || !is_array($voterReq['questions'][$q]['ballots'])) { WrongRequestException::throwException(224, "/questions[].['ballots']/ must be set and of type array", print_r($voterReq, true)); } foreach ($blindedHashesFromDB['requestedBallots']['questions'][$q]['picked'] as $p) { $i = find_in_subarray($voterReq['questions'][$q]['ballots'], 'ballotno', $p); if ($i === false) { WrongRequestException::throwException(211, "A picked Ballot was not sent", "verifyBallots: not sent ballot: {$p}"); } $curVoterBallot = $voterReq["questions"][$qno]["ballots"][$i]; if (!isset($curVoterBallot['unblindf']) || !is_string($curVoterBallot['unblindf'])) { WrongRequestException::throwException(225, 'Error: /unblindf/ must be set and of type /string/', print_r($curVoterBallot, true)); } // verify if the sent ballot was requested // $kw = array_search($curVoterBallot['ballotno'], $blindedHashesFromDB['requestedBallots']['questions'][$q]['picked']); // if ($kw === false) WrongRequestException::throwException(211, "A Ballot was sent for verification purpose that was not requested", "verifyBallots: not requested ballot: " . $voterReq['ballots'][$i]['ballotno'] . "requested ballots: " . print_r($blindedHashesFromDB['requestedBallots'], true)); // $requestedballots['sent'][$kw] = true; // } // verify hash $str = $this->ballot2strForSig($curVoterBallot, makeCompleteElectionId($this->electionId, $blindedHashesFromDB['blindedHashes']['questions'][$q]['questionID'])); $blindedHashFromDatabase = $blindedHashesFromDB['blindedHashes']['questions'][$q]['ballots'][$curVoterBallot['ballotno']]['blindedHash']; $unblindf = $curVoterBallot['unblindf']; $hashOk = $this->crypt->verifyBlindedHash($str, $unblindf, $blindedHashFromDatabase); if (!($hashOk === true)) { WrongRequestException::throwException(212, "Error: hash wrong", "hash from signature: " . $verifyHash->toHex() . "calculated hash: {$hashByMe}"); } // verify sigs from previous servers .ballots.sigs: .sig(encryptet previous sig or hash if first sig) .sigBy (name of the signing server in order to identify the correct public key) $sigsOk = false; if (isset($curVoterBallot['sigs'])) { $sigsOk = $this->crypt->verifySigs($str, $curVoterBallot['sigs']); } else { $sigsOk = true; } // no sigs there $tmpret[$i] = $hashOk === true && $sigsOk === true; if ($i == 0) { $ret = $tmpret[$i] === true; } else { $ret = $ret === true && $tmpret[$i] === true; } // TODO verify if votingId is unique // load $allvotingno from database // if (array_search($raw['votingno'], $allvotingno) == false) {$e = WrongRequestException::throwException... error("Voting number allocated"); return $e;} // } // verify if all requested ballots were sent /* for ($i=0; $i<count($requestedballots["num"]); $i++) { if (! $requestedballots['sent']) { $e = throwExeption...("Not all requested ballots were sent for verification. Ballots $requestedballots[num][$i] is missing."); } } */ // array($tmpret, $hashByMe, $unblindethashFromDatabaseStr); if ($q == 0) { $retQ = $ret; } else { $retQ = $retQ === true && $ret === true; } } return $retQ; }
function rub_taxo($p, $t) { $id = ses('read'); if ($p == 1) { $p = $_SESSION['frm']; } elseif ($p == 'art') { $p = ib_of_id($id); } if ($p) { $taxcat = supertriad_dig($p); } //permanent//$_SESSION['superline'][$p];//cache if ($p > 1) { $t = lka(urlread($p), suj_of_id($p)) . br(); $hie = collect_hierarchie_c(0, ''); $taxcat = find_in_subarray($hie, $p); } $t = build_titl($taxcat, $t, 1); if (is_array($taxcat)) { return $t . divc('taxonomy', make_menus_r($taxcat)); } }
/** * * @param unknown $allVotes = [{"optionOrder": [1, 2, 3, 4], "options": [{"name": "yesNo", "value": 1}] * @param unknown $validOptionIDs = [1, 2, 3, 4] * @return [$optionID]['numYes' / 'numNo' / 'numAbstention'] */ function GetResultStat($allVotes, $validOptionIDs, $schemeConfig) { // TODO check signatures in each vote $GetResultStatcontentValid = function ($voteStr, $validOptionIDs, $schemeConfig) { try { $vote = json_decode($voteStr, true); if ($vote == null || !is_array($vote) || !isset($vote['optionOrder']) || !isset($vote['options']) || !is_array($vote['optionOrder']) || !is_array($vote['options'])) { throw new VoteInvalidException(); } $optionOrder = $vote['optionOrder']; $voteForStat = array(); foreach ($vote['options'] as $optionIndex => $curOption) { if (!in_array($optionOrder[$optionIndex], $validOptionIDs)) { throw new InvalidVoteException(); } $voteScheme = array(); foreach ($curOption as $ShemeIndex => $curSheme) { if (!isset($curSheme['name']) || !is_string($curSheme['name'])) { throw new VoteInvalidException(); } switch ($curSheme['name']) { case 'yesNo': if (!is_int($curSheme['value'])) { throw new InvalidVoteException(); } switch ($curSheme['value']) { case 1: $selectedOpt = 'numYes'; break; case 0: $selectedOpt = 'numNo'; break; case -1: $selectedOpt = 'numAbstention'; break; default: throw new InvalidVoteException(); break; } break; case 'score': $scoreSchemeConfig = $schemeConfig[find_in_subarray($schemeConfig, 'name', 'score')]; if ($curSheme['value'] > $scoreSchemeConfig['maxScore'] || $curSheme['value'] < $scoreSchemeConfig['minScore']) { throw new VoteInvalidException(); } $selectedOpt = $curSheme['value']; break; default: throw new InvalidVoteException(); break; } $voteScheme[$curSheme['name']] = $selectedOpt; } $voteForStat[$optionOrder[$optionIndex]] = $voteScheme; } } catch (VoteInvalidException $e) { $voteForStat = array('invalid' => 'invalid'); } return $voteForStat; }; /** * adds a vote to the statistics * @param $vote: [1: [ {'yesNo': 'numYes'}, {'score': 3}]] or ['invalid': 'invalid'] * @param $votestat old $voteStat * @return updated $voteStat * */ $GetResultStataddToStat = function ($voteForStat, $votestat) { foreach ($voteForStat as $optionIndex => $option) { if ($option === 'invalid') { if (!isset($votestat['invalid'])) { $votestat['invalid'] = 0; } $votestat['invalid']++; } else { if (!isset($votestat[$optionIndex])) { $votestat[$optionIndex] = array(); } foreach ($option as $name => $value) { if (!isset($votestat[$optionIndex][$name])) { $votestat[$optionIndex][$name] = array(); } if (!isset($votestat[$optionIndex][$name][$value])) { $votestat[$optionIndex][$name][$value] = 0; } $votestat[$optionIndex][$name][$value]++; } } } return $votestat; }; //$optionStat = array(); // foreach ($validOptionIDs as $optionID) { // $optionStat[$optionID] = array('numYes' => 0, 'numNo' => 0, 'numAbstention' => 0, 'numInvalid' => 0); // } $optionsStat = array(); foreach ($allVotes as $vote) { $voteForStat = $GetResultStatcontentValid($vote['vote']['vote'], $validOptionIDs, $schemeConfig); $optionsStat = $GetResultStataddToStat($voteForStat, $optionsStat); } // make sure that each option has "yes" and "no" set to zero if no one selected this. // and also calculate the sum of scores foreach ($validOptionIDs as $curOptID) { foreach ($schemeConfig as $i => $curSchemeConfig) { switch ($curSchemeConfig['name']) { case 'yesNo': if (!isset($optionsStat[$curOptID])) { $optionsStat[$curOptID] = array('yesNo' => array()); } if (!isset($optionsStat[$curOptID]['yesNo']['numNo'])) { $optionsStat[$curOptID]['yesNo']['numNo'] = 0; } if (!isset($optionsStat[$curOptID]['yesNo']['numYes'])) { $optionsStat[$curOptID]['yesNo']['numYes'] = 0; } if (!isset($optionsStat[$curOptID]['yesNo']['numAbstention'])) { $optionsStat[$curOptID]['yesNo']['numAbstention'] = 0; } break; case 'score': if (!isset($optionsStat[$curOptID])) { $optionsStat[$curOptID] = array('score' => array('sum' => 0)); } if (!isset($optionsStat[$curOptID]['score'])) { $optionsStat[$curOptID]['score'] = array('sum' => 0); } if (!isset($optionsStat[$curOptID]['score']['sum'])) { $optionsStat[$curOptID]['score']['sum'] = 0; } for ($score = $curSchemeConfig['minScore']; $score <= $curSchemeConfig['maxScore']; $score++) { if (!isset($optionsStat[$curOptID]['score'][$score])) { $optionsStat[$curOptID]['score'][$score] = 0; } $optionsStat[$curOptID]['score']['sum'] = $optionsStat[$curOptID]['score']['sum'] + $optionsStat[$curOptID]['score'][$score] * $score; } break; } } } return $optionsStat; }