ACC SHELL
<?php
/**
* Homepage presenter.
*/
class ProblemSPrihlasenimPresenter extends BasePresenter
{
public function renderNovyPristup()
{
$this->flashMessage('Nejdřív se zkus přihlásit svým emailem a heslem \'duklavysco\'!', 'error');
}
protected function createComponentNewAccessForm()
{
$form = new Nette\Application\UI\Form;
$form->addText('name', 'Jméno:')
->setRequired('Musíš vyplnit jméno!');
$form->addText('email', 'Email:', 30)
->setRequired('Musíš vyplnit email!')
->addRule(Nette\Application\UI\Form::EMAIL, 'Zadej platný email!');
$form->addSubmit('submit', 'Požádat o přístup')
->onClick[] = callback($this, 'newAccessFormSubmitted');
return $form;
}
public function newAccessFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
{
$values = $submitButton->getForm()->getValues();
$mail = new Nette\Mail\Message();
$mail->setFrom("$values->name <$values->email>")
->setSubject('Dukla Vyščo - Žádost o nový přístup')
->setBody("Uživatel $values->name žádá o přístup na stránky Dukly Vyščo!");
try {
$admins = $this->db->table('uzivatele')->where('role', 'administrator');
foreach($admins as $admin)
{
$mail->addTo($admin->email);
}
$mail->send();
$this->flashMessage('Administrátorům webu byl odeslán email s žádostí.', 'success');
$this->redirect('Homepage:');
} catch(Nette\InvalidStateException $e) {
$this->flashMessage("Email nemohl být odeslán! " . $e->getMessage(), 'error');
}
}
protected function createComponentForgottenPasswordForm()
{
$form = new Nette\Application\UI\Form;
$form->addText('email', 'Email:')
->setRequired('Musíš vyplnit email!')
->addRule(Nette\Application\UI\Form::EMAIL, 'Zadej platný email!');
$form->addSubmit('submit', 'Odeslat nové heslo')
->onClick[] = callback($this, 'forgottenPasswordFormSubmitted');
return $form;
}
public function forgottenPasswordFormSubmitted(Nette\Forms\Controls\SubmitButton $submitButton)
{
$values = $submitButton->getForm()->getValues();
$user = $this->db->table('uzivatele')->where('email', $values->email)->fetch();
if ($user)
{
$newPassword = $this->get_random_string('duklavysco', 6);
$this->db->table('uzivatele')->get($user->id)->update(array('heslo' => md5($newPassword)));
try {
$mail = new Nette\Mail\Message();
$mail->setFrom("Dukla Vyščo <do-not-reply@duklavysco.cz>")
->setSubject('Dukla Vyščo - Nové heslo')
->setBody("Tvoje nové heslo je: $newPassword")
->addTo($user->email);
$mail->send();
$this->flashMessage("Nové heslo bylo odesláno na Tvůj email!", 'success');
$this->redirect('Homepage:');
} catch(Nette\InvalidStateException $e) {
$this->flashMessage("Nové heslo se nepodařilo odeslat na mail! " . $e->getMessage(), 'error');
$this->flashMessage("Nové heslo bylo změněno na '$newPassword' (bez uvozovek)!", 'success');
}
}
else
{
$this->template->emailNotFound = true;
}
}
private function get_random_string($valid_chars, $length)
{
// start with an empty random string
$random_string = "";
// count the number of chars in the valid chars string so we know how many choices we have
$num_valid_chars = strlen($valid_chars);
// repeat the steps until we've created a string of the right length
for ($i = 0; $i < $length; $i++)
{
// pick a random number from 1 up to the number of valid chars
$random_pick = mt_rand(1, $num_valid_chars);
// take the random character out of the string of valid chars
// subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
$random_char = $valid_chars[$random_pick-1];
// add the randomly-chosen char onto the end of our string so far
$random_string .= $random_char;
}
// return our finished random string
return $random_string;
}
}
ACC SHELL 2018