add ip on stack of vote
This commit is contained in:
parent
eaee01838d
commit
6831d8d1ae
|
@ -67,10 +67,11 @@ class VoteController extends EmailsController {
|
|||
}
|
||||
// TODO anti flood
|
||||
$foundOwner
|
||||
->setModifierToken( $poll->generateAdminKey() );
|
||||
->setModifierToken( $poll->generateRandomKey() );
|
||||
$stack = new StackOfVotes();
|
||||
$stack
|
||||
->setOwner( $foundOwner )
|
||||
->setIp($_SERVER['REMOTE_ADDR'])
|
||||
->setPseudo( $data[ 'pseudo' ] )
|
||||
->setPoll( $poll );
|
||||
foreach ( $data[ 'votes' ] as $voteInfo ) {
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Entity;
|
||||
|
||||
use App\Traits\RandomTrait;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
@ -16,6 +17,9 @@ use RangeException;
|
|||
* @Serializer\ExclusionPolicy("all")
|
||||
*/
|
||||
class Poll {
|
||||
|
||||
use RandomTrait;
|
||||
|
||||
/**
|
||||
* @ORM\Id()
|
||||
* @ORM\GeneratedValue()
|
||||
|
@ -193,6 +197,10 @@ class Poll {
|
|||
public function __construct() {
|
||||
|
||||
$this->initiate();
|
||||
$this->votes = new ArrayCollection();
|
||||
$this->stacksOfVotes = new ArrayCollection();
|
||||
$this->choices = new ArrayCollection();
|
||||
$this->comments = new ArrayCollection();
|
||||
}
|
||||
|
||||
private function initiate() {
|
||||
|
@ -200,7 +208,7 @@ class Poll {
|
|||
$this->stacksOfVotes = new ArrayCollection();
|
||||
$this->choices = new ArrayCollection();
|
||||
$this->comments = new ArrayCollection();
|
||||
$this->setAdminKey( $this->generateAdminKey() );
|
||||
$this->setAdminKey( $this->generateRandomKey() );
|
||||
$this->setCreationDate( new DateTime() );
|
||||
$this->setExpiracyDate( $this->addDaysToDate(
|
||||
new DateTime(),
|
||||
|
@ -209,49 +217,9 @@ class Poll {
|
|||
$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() {
|
||||
$content = $this->display();
|
||||
|
|
|
@ -46,6 +46,11 @@ class StackOfVotes {
|
|||
*/
|
||||
private $owner;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", length=255)
|
||||
*/
|
||||
private $ip;
|
||||
|
||||
public function __construct() {
|
||||
$this->votes = new ArrayCollection();
|
||||
}
|
||||
|
@ -54,19 +59,19 @@ class StackOfVotes {
|
|||
$votes = $this->getVotes();
|
||||
|
||||
$tab = [
|
||||
// 'id' => $this->getId(),
|
||||
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
||||
// 'id' => $this->getId(),
|
||||
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
||||
'pseudo' => $this->getPseudo(),
|
||||
'created_at' => $this->getCreatedAtAsString(),
|
||||
'votes' => [],
|
||||
];
|
||||
// prefill votes with all choices ids
|
||||
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
||||
// $tab[ 'votes' ][ $choice->getId() ] = [
|
||||
// 'choice_id' => $choice->getId(),
|
||||
// 'value' => null,
|
||||
// ];
|
||||
// }
|
||||
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
||||
// $tab[ 'votes' ][ $choice->getId() ] = [
|
||||
// 'choice_id' => $choice->getId(),
|
||||
// 'value' => null,
|
||||
// ];
|
||||
// }
|
||||
|
||||
foreach ( $votes as $vote ) {
|
||||
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,43 @@
|
|||
namespace App\Traits;
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
namespace App\Traits;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
|
@ -25,4 +26,12 @@ trait TimeStampableTrait {
|
|||
return $this->createdAt->format( 'c' );
|
||||
}
|
||||
|
||||
|
||||
public function addDaysToDate( DateTime $date, int $days ) {
|
||||
$st = strtotime( $date->getTimestamp() );
|
||||
|
||||
return new DateTime( $st );
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue