/** * Generates a JSON from an associative array. * * If the array contains one non integer key, an object literal needs to be * created. Otherwise an array literal does the job. * * @param keyValuePairs An associative array with keys and values for the * JSON object. * @return String, the generated JSON. */ function generateJSONAnswer($keyValuePairs) { // Check for non integer keys. $object = FALSE; foreach ($keyValuePairs as $key => $value) { if (!is_int($key)) { $object = TRUE; break; } } // Generate the JSON. Only print the keys, if in an object literal. $json = $object ? '{' : '['; $first = TRUE; foreach ($keyValuePairs as $key => $value) { // Separate different properties by commas. if (!$first) { $json .= ','; } $first = FALSE; // Only print the keys, if the array is converted to an object literal. // Omit the keys in array literals. if ($object) { $json .= '"' . (string) $key . '":'; } // See what type the data is. if (is_bool($value)) { $json .= $value ? 'true' : 'false'; } else { if (is_int($value) || is_float($value) || is_double($value)) { $json .= $value; } else { if (is_string($value)) { $json .= '"' . $value . '"'; } else { if (is_array($value)) { $json .= generateJSONAnswer($value); } else { if (is_null($value)) { $json .= 'null'; } else { throw new Exception('JSON Type not yet supported.'); } } } } } } $json .= $object ? '}' : ']'; return $json; }
if (isset($_POST['name']) && isset($_POST['oldName']) && isset($_POST['type']) && isset($_POST['privateId'])) { $name = strip_tags($_POST['name']); $oldName = strip_tags($_POST['oldName']); $type = strip_tags($_POST['type']); $privateId = strip_tags($_POST['privateId']); $accountId = getAccountId('', $privateId); if ($accountId !== FALSE) { $pdos = $GLOBALS['pdo']->prepare(' UPDATE `Category` SET `Name` = :name WHERE `Name` = :oldName AND `Type` = :type AND `Account` = :account '); $pdos->bindValue(':name', $name); $pdos->bindValue('type', $type); $pdos->bindValue(':oldName', $oldName); $pdos->bindValue(':account', $accountId); $pdos->execute(); $success = TRUE; } else { $errorMessage = 'Nicht angemeldet.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage));
if ($accountId !== FALSE) { $voteCodeId = checkVoteCode($voteCode, $accountId); if ($voteCodeId) { $votes = getVotes($voteCodeId); $students = getNames('student', $accountId); $studentCategories = getCategories('student', $accountId, $votes); $teachers = getNames('teacher', $accountId); $teacherCategories = getCategories('teacher', $accountId, $votes); $pdos = $GLOBALS['pdo']->prepare(' SELECT `Couple1`, `Couple2` FROM `CoupleVote` WHERE `VoteCode` = :voteCode '); $pdos->bindValue(':voteCode', $voteCodeId); $pdos->execute(); $result = $pdos->fetch(); $studentCouple1 = $result['Couple1']; $studentCouple2 = $result['Couple2']; $success = TRUE; } else { $errorMessage = 'Ungültiger Code.'; } } else { $errorMessage = 'Unbekannte Umfrage.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage, 'students' => $students, 'studentCategories' => $studentCategories, 'teachers' => $teachers, 'teacherCategories' => $teacherCategories, 'studentCouple1' => $studentCouple1, 'studentCouple2' => $studentCouple2));
$candidates = array(); if (isset($_POST['privateId']) && isset($_POST['type'])) { $privateId = strip_tags($_POST['privateId']); $accountId = getAccountId('', $privateId); $type = strip_tags($_POST['type']); if ($accountId !== FALSE) { $pdos = $GLOBALS['pdo']->prepare(' SELECT `Name`, `Gender` FROM `Candidate` WHERE `Account` = :account AND `Type` = :type ORDER BY `Name` '); $pdos->bindValue(':account', $accountId); $pdos->bindValue(':type', $type); $pdos->execute(); while (($candidate = $pdos->fetch()) !== FALSE) { $candidates[] = array('name' => $candidate['Name'], 'gender' => $candidate['Gender']); } $success = TRUE; } else { $errorMessage = 'Nicht angemeldet.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage, 'candidates' => $candidates));
`ShowStudentVotings`, `ShowTeacherVotings`, `ShowCoupleVotings` FROM `Account` WHERE UPPER(`Email`) = UPPER(:email) '); $pdos->bindValue(':email', $email); $pdos->execute(); $result = $pdos->fetch(); if ($result !== FALSE) { $hash = sha1($password . $result['Salt']); if ($hash == $result['Password']) { $success = TRUE; $publicId = $result['PublicId']; $privateId = $result['PrivateId']; $school = $result['School']; $separateStudentsBySex = $result['SeparateStudentsBySex'] === '1'; $separateTeachersBySex = $result['SeparateTeachersBySex'] === '1'; $votingMade = $result['VotingMade'] === '1'; $showStudentVotings = $result['ShowStudentVotings'] == '1'; $showTeacherVotings = $result['ShowTeacherVotings'] == '1'; $showCoupleVotings = $result['ShowCoupleVotings'] == '1'; } else { $errorMessage = 'Login fehlgeschlagen.'; } } else { $errorMessage = 'Login fehlgeschlagen.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage, 'school' => $school, 'publicId' => $publicId, 'privateId' => $privateId, 'separateStudentsBySex' => $separateStudentsBySex, 'separateTeachersBySex' => $separateTeachersBySex, 'votingMade' => $votingMade, 'showStudentVotings' => $showStudentVotings, 'showTeacherVotings' => $showTeacherVotings, 'showCoupleVotings' => $showCoupleVotings));
$pdos->bindValue(':account', $accountId); $pdos->bindValue(':type', $type); $pdos->execute(); $result = array(); while (($ds = $pdos->fetch()) !== FALSE) { $result[] = array('name' => $ds['Name'], 'boys' => getResultsForCategory($accountId, $ds['Id'], $type, 'Boy'), 'girls' => getResultsForCategory($accountId, $ds['Id'], $type, 'Girl'), 'unisex' => getResultsForCategory($accountId, $ds['Id'], $type, 'Unisex')); } return $result; } // To generate the JSON answer. $success = FALSE; $errorMessage = ''; $voteCount = 0; $studentResults = array(); $teacherResults = array(); $coupleResults = array(); if (isset($_POST['privateId'])) { $privateId = strip_tags($_POST['privateId']); $accountId = getAccountId('', $privateId); if ($accountId !== FALSE) { $voteCount = countVotes($accountId); $studentResults = getCategoryResults($accountId, 'student'); $teacherResults = getCategoryResults($accountId, 'teacher'); $coupleResults = getCoupleResults($accountId, 'student'); $success = TRUE; } else { $errorMessage = 'Nicht angemeldet.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage, 'voteCount' => $voteCount, 'studentResults' => $studentResults, 'teacherResults' => $teacherResults, 'coupleResults' => $coupleResults));
include_once '../utils.php'; // To generate the JSON answer. $success = FALSE; $errorMessage = ''; $codes = array(); if (isset($_POST['privateId'])) { $privateId = strip_tags($_POST['privateId']); $accountId = getAccountId('', $privateId); if ($accountId !== FALSE) { $pdos = $GLOBALS['pdo']->prepare(' SELECT `Code`, `Used`, `Date` FROM `VoteCode` WHERE `Account` = :account ORDER BY `Used`, `Code` '); $pdos->bindValue(':account', $accountId); $pdos->execute(); while (($row = $pdos->fetch()) !== FALSE) { $codes[] = array('code' => (string) $row['Code'], 'used' => (bool) $row['Used'], 'date' => $row['Date']); } $success = TRUE; } else { $errorMessage = 'Nicht angemeldet.'; } } echo generateJSONAnswer(array('success' => $success, 'errorMessage' => $errorMessage, 'voteCodes' => $codes));