use random hash strings for Station URLs

This commit is contained in:
oliver 2016-02-27 19:59:59 +01:00
commit 676c2386fd
3 changed files with 58 additions and 11 deletions

View file

@ -35,12 +35,12 @@
* *
* Redirect to a Character groups Quest Station. * Redirect to a Character groups Quest Station.
* *
* @param int $stationId ID of Character groups Quest Station * @param string $stationHash Hash of Character groups Quest Station
*/ */
public function cgqs($stationId) public function cgqs($stationHash)
{ {
// Get station // Get station
$station = $this->Charactergroupsqueststations->getStationById($stationId); $station = $this->Charactergroupsqueststations->getStationByHash($stationHash);
// Get Character groups Quests // Get Character groups Quests
$quest = $this->Charactergroupsquests->getQuestById($station['charactergroupsquest_id']); $quest = $this->Charactergroupsquests->getQuestById($station['charactergroupsquest_id']);

View file

@ -72,7 +72,7 @@
$station = $this->Charactergroupsqueststations->getStationByUrl($quest['id'], $stationUrl); $station = $this->Charactergroupsqueststations->getStationByUrl($quest['id'], $stationUrl);
// Generate QR-code // Generate QR-code
$url = $this->linker->link(array('qr', 'cgqs', $station['id']), 0, true, null, true, null, true); $url = $this->linker->link(array('qr', 'cgqs', $station['url']), 0, true, null, true, null, true);
$file = $this->generateQRcode($url, $size); $file = $this->generateQRcode($url, $size);
if(is_null($file)) { if(is_null($file)) {
return; return;

View file

@ -38,6 +38,12 @@
* @var int; * @var int;
*/ */
const STATUS_SOLVED = 3; const STATUS_SOLVED = 3;
/**
* String length for Station URLs
*
* @var int
*/
const URL_LENGTH = 10;
/** /**
* Required models * Required models
@ -86,10 +92,11 @@
/** /**
* Get a Station by its ID. * Get a Station by its URL.
* *
* @throws \nre\exceptions\IdNotFoundException * @throws \nre\exceptions\IdNotFoundException
* @param int $stationId ID of Station to get * @param int $groupsquestId ID of Character groups Quest
* @param string $stationUrl URL-title of Station to get
* @return array Station data * @return array Station data
*/ */
public function getStationByUrl($groupsquestId, $stationUrl) public function getStationByUrl($groupsquestId, $stationUrl)
@ -111,6 +118,31 @@
} }
/**
* Get a Station by its Hash.
*
* @throws \nre\exceptions\IdNotFoundException
* @param string $stationUrl Hash of Station to get
* @return array Station data
*/
public function getStationByHash($stationHash)
{
$data = $this->db->query(
'SELECT id, charactergroupsquest_id, stationtype_id, title, url, stationpicture_id, prolog, task, latitude, longitude, righttext, wrongtext, rightimage_id, rightav_id, wrongimage_id, wrongav_id '.
'FROM charactergroupsqueststations '.
'WHERE url = ?',
's',
$stationHash
);
if(empty($data)) {
throw new \nre\exceptions\IdNotFoundException($stationHash);
}
return $data[0];
}
/** /**
* Get all Stations for a Character groups Quest. * Get all Stations for a Character groups Quest.
* *
@ -455,7 +487,7 @@
'(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
'iississddss', 'iississddss',
$questId, $stationtypeId, $title, $questId, $stationtypeId, $title,
\nre\core\Linker::createLinkParam($title), $pos, $this->createStationUrl(), $pos,
$prolog, $task, $latitude, $longitude, $righttext, $wrongtext $prolog, $task, $latitude, $longitude, $righttext, $wrongtext
); );
@ -482,11 +514,10 @@
{ {
$this->db->query( $this->db->query(
'UPDATE charactergroupsqueststations '. 'UPDATE charactergroupsqueststations '.
'SET stationtype_id = ?, title = ?, url = ?, prolog = ?, task = ?, latitude = ?, longitude = ?, righttext = ?, wrongtext = ? '. 'SET stationtype_id = ?, title = ?, prolog = ?, task = ?, latitude = ?, longitude = ?, righttext = ?, wrongtext = ? '.
'WHERE id = ?', 'WHERE id = ?',
'issssddssi', 'isssddssi',
$stationtypeId, $title, $stationtypeId, $title,
\nre\core\Linker::createLinkParam($title),
$prolog, $task, $latitude, $longitude, $righttext, $wrongtext, $prolog, $task, $latitude, $longitude, $righttext, $wrongtext,
$stationId $stationId
); );
@ -599,6 +630,22 @@
); );
} }
/**
* Generate a URL for Stations with random characters.
*
* @return string A string with random characters
*/
private function createStationUrl()
{
// Length of URL
$length = max(0, min(32, self::URL_LENGTH));
// Create and return random string
return substr(md5(microtime()), rand(0, 32-$length), $length);
}
} }
?> ?>