add ip on stack of vote

This commit is contained in:
Tykayn 2021-04-27 10:41:21 +02:00 committed by tykayn
parent eaee01838d
commit 6831d8d1ae
5 changed files with 503 additions and 471 deletions

View File

@ -67,10 +67,11 @@ class VoteController extends EmailsController {
} }
// TODO anti flood // TODO anti flood
$foundOwner $foundOwner
->setModifierToken( $poll->generateAdminKey() ); ->setModifierToken( $poll->generateRandomKey() );
$stack = new StackOfVotes(); $stack = new StackOfVotes();
$stack $stack
->setOwner( $foundOwner ) ->setOwner( $foundOwner )
->setIp($_SERVER['REMOTE_ADDR'])
->setPseudo( $data[ 'pseudo' ] ) ->setPseudo( $data[ 'pseudo' ] )
->setPoll( $poll ); ->setPoll( $poll );
foreach ( $data[ 'votes' ] as $voteInfo ) { foreach ( $data[ 'votes' ] as $voteInfo ) {

View File

@ -2,6 +2,7 @@
namespace App\Entity; namespace App\Entity;
use App\Traits\RandomTrait;
use DateTime; use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@ -16,6 +17,9 @@ use RangeException;
* @Serializer\ExclusionPolicy("all") * @Serializer\ExclusionPolicy("all")
*/ */
class Poll { class Poll {
use RandomTrait;
/** /**
* @ORM\Id() * @ORM\Id()
* @ORM\GeneratedValue() * @ORM\GeneratedValue()
@ -193,6 +197,10 @@ class Poll {
public function __construct() { public function __construct() {
$this->initiate(); $this->initiate();
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection();
$this->comments = new ArrayCollection();
} }
private function initiate() { private function initiate() {
@ -200,7 +208,7 @@ class Poll {
$this->stacksOfVotes = new ArrayCollection(); $this->stacksOfVotes = new ArrayCollection();
$this->choices = new ArrayCollection(); $this->choices = new ArrayCollection();
$this->comments = new ArrayCollection(); $this->comments = new ArrayCollection();
$this->setAdminKey( $this->generateAdminKey() ); $this->setAdminKey( $this->generateRandomKey() );
$this->setCreationDate( new DateTime() ); $this->setCreationDate( new DateTime() );
$this->setExpiracyDate( $this->addDaysToDate( $this->setExpiracyDate( $this->addDaysToDate(
new DateTime(), new DateTime(),
@ -209,49 +217,9 @@ class Poll {
$this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] ); $this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] );
} }
public function generateAdminKey() {
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
return str_shuffle( md5( $rand ) . $rand . $this->random_str() );
}
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* This function uses type hints now (PHP 7+ only), but it was originally
* written for PHP 5 as well.
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
*
* @return string
*/
public function random_str(
int $length = 64,
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
if ( $length < 1 ) {
throw new RangeException( "Length must be a positive integer" );
}
$pieces = [];
$max = mb_strlen( $keyspace, '8bit' ) - 1;
for ( $i = 0 ; $i < $length ; ++ $i ) {
$pieces [] = $keyspace[ random_int( 0, $max ) ];
}
return implode( '', $pieces );
}
public function addDaysToDate( DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() );
return new DateTime( $st );
}
public function displayForAdmin() { public function displayForAdmin() {
$content = $this->display(); $content = $this->display();

View File

@ -46,6 +46,11 @@ class StackOfVotes {
*/ */
private $owner; private $owner;
/**
* @ORM\Column(type="string", length=255)
*/
private $ip;
public function __construct() { public function __construct() {
$this->votes = new ArrayCollection(); $this->votes = new ArrayCollection();
} }
@ -143,6 +148,18 @@ class StackOfVotes {
} }
} }
return $this;
}
public function getIp(): ?string
{
return $this->ip;
}
public function setIp(string $ip): self
{
$this->ip = $ip;
return $this; return $this;
} }
} }

View File

@ -3,6 +3,43 @@
namespace App\Traits; namespace App\Traits;
trait RandomTrait { trait RandomTrait {
public function generateRandomKey() {
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
return str_shuffle( md5( $rand ) . $rand . $this->random_str() );
}
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* This function uses type hints now (PHP 7+ only), but it was originally
* written for PHP 5 as well.
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
*
* @return string
*/
public function random_str(
int $length = 64,
string $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
): string {
if ( $length < 1 ) {
throw new RangeException( "Length must be a positive integer" );
}
$pieces = [];
$max = mb_strlen( $keyspace, '8bit' ) - 1;
for ( $i = 0 ; $i < $length ; ++ $i ) {
$pieces [] = $keyspace[ random_int( 0, $max ) ];
}
return implode( '', $pieces );
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Traits; namespace App\Traits;
use DateTime;
use DateTimeInterface; use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
@ -25,4 +26,12 @@ trait TimeStampableTrait {
return $this->createdAt->format( 'c' ); return $this->createdAt->format( 'c' );
} }
public function addDaysToDate( DateTime $date, int $days ) {
$st = strtotime( $date->getTimestamp() );
return new DateTime( $st );
}
} }