diff --git a/index.php b/index.php index 1adfb1f..7d84ed9 100644 --- a/index.php +++ b/index.php @@ -1,48 +1,59 @@ register(new Silex\Provider\TwigServiceProvider(), array( - 'twig.path' => __DIR__.'/templates', + 'twig.path' => __DIR__ . '/templates', )); $app->register(new Silex\Provider\TranslationServiceProvider(), array( - 'locale_fallback' => 'de', + 'locale_fallback' => 'de', )); -$app['translator'] = $app->share($app->extend('translator', function($translator, $app) { - $translator->addResource('xliff', __DIR__.'/locales/de.xml', 'de'); - $translator->addResource('xliff', __DIR__.'/locales/en.xml', 'en'); +$app['translator'] = $app->share($app->extend('translator', function ($translator, $app) { + $translator->addResource('xliff', __DIR__ . '/locales/de.xml', 'de'); + $translator->addResource('xliff', __DIR__ . '/locales/en.xml', 'en'); - return $translator; + return $translator; })); -$app->before(function(Request $request) use ($app){ +$app->before(function (Request $request) use ($app) { $lang = $request->getPreferredLanguage(array('en', 'de')); $app['translator']->setLocale($lang); }); -$app->get('/', function (Request $request) use ($app) { +$app->get('/', function (Request $request) use ($app, $config) { return $app['twig']->render('registration_form.twig', array( - 'errors' => array(), + 'hosts' => $config['hosts'], + 'errors' => array(), )); }); -$app->post('/', function (Request $request) use ($app) { +$app->post('/', function (Request $request) use ($app, $config) { $errors = array(); // collect the params - $user = $request->get('username',null); - $host = $request->get('host',null); - $email = $request->get('mail',null); - $password = $request->get('password',null); - $password_repeat = $request->get('password_repeat',null); + $user = $request->get('username', null); + $host = $request->get('host', null); + $email = $request->get('mail', null); + $password = $request->get('password', null); + $password_repeat = $request->get('password_repeat', null); // check for errors if (!$user) { @@ -68,14 +79,88 @@ $app->post('/', function (Request $request) use ($app) { if ($password != $password_repeat) { $errors[] = $app->trans('Bitte gebe in den Feldern Passwort und Passwortwiederholung identische Werte ein.'); } - + + if (count($errors) == 0) { + $client = new Client($config['prosody']['http_base']); + + $request = $client + ->get($config['prosody']['url_prefix'] . 'user/' . $user) + ->setAuth($config['prosody']['user'], $config['prosody']['password']); + + $response = $request->send(); + + if ($response->getStatusCode() != 404) { + $errors[] = $app->trans('Der Benutzername ist bereits vergeben.'); + } + } + + if (count($errors) == 0) { + + $client = new Client($config['prosody']['http_base']); + + $data = json_encode(array( + 'username' => $user, + 'password' => $password, + 'server' => $host, + 'mail' => $email, + )); + + $token = sha1($data); + + if (strlen($token) > 0) { + file_put_contents('validations/' . $token, $data); + $message = Swift_Message::newInstance() + ->setSubject($app->trans('Registrierung auf %server%', array('%server%' => $host))) + ->setFrom($config['from']) + ->setTo($email) + ->setBody($app['twig']->render(sprintf('email.%s.twig', $app['translator']->getLocale()), array('auth_token' => $token, 'url' => $config['url']))); + + $transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs'); + + $mailer = Swift_Mailer::newInstance($transport); + + $result = $mailer->send($message); + + if (!$result) { + $errors[] = $app->trans('Beim Mailversand ist ein Fehler aufgetreten.'); + } + } + } + if (count($errors) > 0) { return $app['twig']->render('registration_form.twig', array( + 'hosts' => $config['hosts'], 'errors' => $errors, )); } else { - return $app['twig']->render('success.twig', array( - )); + return $app['twig']->render('success.twig', array()); + } +}); + +$app->get('/{verifycode}', function ($verifycode) use ($app, $config) { + if (file_exists('templates/' . $verifycode)) { + $data = json_decode(file_get_contents('templates/' . $verifycode)); + + $jid = $data['user'] . '@' . $data['server']; + + $client = new Client($config['prosody']['http_base']); + + $request = $client + ->post($config['prosody']['url_prefix'] . 'user/' . $data->user, array( + 'Host' => $data->server, + ), json_encode(array('password' => $data->password))) + ->setAuth($config['prosody']['user'], $config['prosody']['password']); + + $response = $request->send(); + + if ($response->getStatusCode() == 201) { + return $app->render('welcome.twig', array('jid' => $jid)); + } else { + return $app->render('error.twig', array('url' => $config['url'])); + } + + } else { + return $app->render('tokennotfound.twig'); } }); diff --git a/templates/email.de.twig b/templates/email.de.twig new file mode 100644 index 0000000..addb681 --- /dev/null +++ b/templates/email.de.twig @@ -0,0 +1,9 @@ +Hallo Benutzer, + +bitte besuche folgende URL um dein Konto zu aktivieren: + +https://bandenkrieg.hacked.jp/~tim/xmpp/{{ auth_token }} + +mit freundlichen Grüßen + +das Registrierungsformular \ No newline at end of file diff --git a/templates/email.en.twig b/templates/email.en.twig new file mode 100644 index 0000000..c7c086f --- /dev/null +++ b/templates/email.en.twig @@ -0,0 +1,9 @@ +Hello user, + +please visit the following url to activate your account: + +https://bandenkrieg.hacked.jp/~tim/xmpp/{{ auth_token }} + +with kind regards + +the registration form \ No newline at end of file diff --git a/templates/error.twig b/templates/error.twig new file mode 100644 index 0000000..c2d65f0 --- /dev/null +++ b/templates/error.twig @@ -0,0 +1,5 @@ +{% extends "layout.twig" %} + +{% block content %} +

Bei der Registrierung ist etwas schief gelaufen. Bitte probiere es noch einmal.

+{% endblock %} \ No newline at end of file diff --git a/templates/layout.twig b/templates/layout.twig new file mode 100644 index 0000000..bebc708 --- /dev/null +++ b/templates/layout.twig @@ -0,0 +1,21 @@ + + + + + + + + + {% trans %}Ein XMPP-Konto registrieren{% endtrans %} + + + + + + + +
+ {% block content %}{% endblock %} +
+ + \ No newline at end of file diff --git a/templates/registration_form.twig b/templates/registration_form.twig index 60a57d4..3699fb1 100644 --- a/templates/registration_form.twig +++ b/templates/registration_form.twig @@ -1,20 +1,6 @@ - - - - - - - +{% extends "layout.twig" %} - {% trans %}Ein XMPP-Konto registrieren{% endtrans %} - - - - - - - -
+{% block content %}
@@ -47,7 +33,9 @@

{% trans %}Wähle hier einen Servernamen aus. Deine XMPP-ID ist dann Benutzername@Servername{% endtrans %}

@@ -91,6 +79,4 @@
-
- - +{% endblock %} diff --git a/templates/success.twig b/templates/success.twig new file mode 100644 index 0000000..e12e763 --- /dev/null +++ b/templates/success.twig @@ -0,0 +1,14 @@ +{% extends "layout.twig" %} + +{% block content %} +
+
+ + +{% trans %}Ein XMPP-Konto registrieren{% endtrans %} + +

{% trans %}Vielen Dank für deine Registrierung. Dir wurde eine E-Mail mit einem aktivierungs Link zugeschickt.{% endtrans %}

+ +
+
+{% endblock %} \ No newline at end of file diff --git a/templates/tokennotfound.twig b/templates/tokennotfound.twig new file mode 100644 index 0000000..3477387 --- /dev/null +++ b/templates/tokennotfound.twig @@ -0,0 +1,5 @@ +{% extends "layout.twig" %} + +{% block content %} +

{% trans %}Der angegebene Token wurde nicht gefunden{% endtrans %}

+{% endblock %} \ No newline at end of file diff --git a/templates/welcome.twig b/templates/welcome.twig new file mode 100644 index 0000000..7074197 --- /dev/null +++ b/templates/welcome.twig @@ -0,0 +1,5 @@ +{% extends "layout.twig" %} + +{% block content %} +

{% trans %}Herzlich Willkommen {{ jid }}! {% endtrans %}

+{% endblock %} \ No newline at end of file diff --git a/validations/.keep b/validations/.keep new file mode 100644 index 0000000..e69de29