/** * Checks if the current user has voted from the provided votes * * @param array $votes * @return bool|int */ protected function hasUserVoted(array $votes) { foreach ($votes as $oid) { foreach ($oid as $vote) { if (zula_ip2long(zula_get_client_ip()) == $vote['ip'] || $vote['uid'] != Ugmanager::_GUEST_ID && $vote['uid'] == $this->_session->getUserId()) { return (int) $vote['option_id']; } } } return false; }
/** * Main method that is called on all loggers from * the Log class. * * @param string $message * @param int $level * @param string $file * @param int $line * @return bool */ public function logMessage($msg, $level, $file = 'unknown', $line = 0) { $fileName = $this->makeFileName($level); $filePath = $this->logDir . '/' . $fileName; if (!zula_is_writable($this->logDir)) { return false; } $uid = Registry::has('session') ? $this->_session->getUserId() : 'unknown'; $msgFormat = '[%1$s] [%2$s | uid %3$s] [%4$s] -- (%5$s:%6$d) %7$s' . "\r\n"; $entry = sprintf($msgFormat, date('c'), zula_get_client_ip(), $uid, $this->levelName($level), basename($file), $line, $msg); return error_log($entry, 3, $filePath); }
/** * Gets the number of login attempts for the current remote addr * * @return int */ public function getLoginAttempts() { $remoteAddr = (int) zula_ip2long(zula_get_client_ip()); $query = $this->_sql->query('SELECT attempts, blocked FROM {PREFIX}mod_session WHERE ip = ' . $remoteAddr); $results = $query->fetch(PDO::FETCH_ASSOC); $query->closeCursor(); if ($results) { $blockedUntil = $this->_date->getDateTime($results['blocked'])->modify('+10 minutes'); if ($blockedUntil < new DateTime()) { // Remove the entry as it has now expired $this->_sql->exec('DELETE FROM {PREFIX}mod_session WHERE ip = ' . $remoteAddr); $results['attempts'] = 0; } return $results['attempts']; } return 0; }
/** * Check if the Antispam method has passed * * @return bool */ public function check() { if (!trim($this->privateKey)) { throw new Antispam_Exception('reCAPTCHA API private key needed'); } $clientIp = zula_get_client_ip(); if ($clientIp == '127.0.0.1') { throw new Antispam_Exception('unable to gather remote address for reCAPTCHA'); } try { $challenge = $this->_input->post('recaptcha_challenge_field'); $response = $this->_input->post('recaptcha_response_field'); if (trim($challenge) && trim($response)) { // Create an HTTP POST request to the reCAPTCHA server if (($sock = fsockopen('api-verify.recaptcha.net', 80, $errno, $errstr, 10)) == false) { throw new Antispam_Exception('unable to create socket'); } $data = http_build_query(array('privatekey' => $this->privateKey, 'remoteip' => $clientIp, 'challenge' => $challenge, 'response' => $response)); $httpRequest = "POST /verify HTTP/1.0\r\n" . "Host: api-verify.recaptcha.net\r\n" . "Content-Type: application/x-www-form-urlencoded;\r\n" . "Content-Length: " . strlen($data) . "\r\n" . "User-Agent: reCAPTCHA/PHP\r\n" . "\r\n{$data}"; fwrite($sock, $httpRequest); $response = ''; while (!feof($sock)) { $response .= fgets($sock, 1160); # One TCP-IP packet } fclose($sock); $response = explode("\r\n\r\n", $response, 2); // Check what the answer was $answers = explode("\n", $response[1]); return trim($answers[0]) === 'true'; } return false; } catch (Input_KeyNoExist $e) { throw new Antispam_Exception('missing recaptcha input data, strange.'); } }
/** * Adds a new vote to a poll option * * @param int $oid * @return bool */ public function vote($oid) { $option = $this->getOption($oid); $pdoSt = $this->_sql->prepare('INSERT INTO {PREFIX}mod_poll_votes (option_id, ip, uid) VALUES(?, ?, ?)'); return $pdoSt->execute(array($option['id'], zula_ip2long(zula_get_client_ip()), $this->_session->getUserId())); }