2013-09-11 15:27:52 +02:00
|
|
|
<?php
|
|
|
|
require 'vendor/autoload.php';
|
2013-09-19 14:59:38 +02:00
|
|
|
require_once 'is_email.php';
|
2013-10-16 09:34:56 +02:00
|
|
|
require_once 'config.php';
|
2013-09-11 15:27:52 +02:00
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
use Silex\Application\TranslationTrait;
|
2013-09-19 14:58:27 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2013-10-16 09:34:56 +02:00
|
|
|
use Guzzle\Http\Client;
|
2013-09-19 14:58:27 +02:00
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
use Silex\Application;
|
|
|
|
|
|
|
|
class RegistrationApplication extends Application {
|
|
|
|
use Application\TranslationTrait;
|
|
|
|
use Application\TwigTrait;
|
|
|
|
}
|
|
|
|
|
|
|
|
$app = new RegistrationApplication();
|
2013-09-11 15:27:52 +02:00
|
|
|
|
|
|
|
$app['debug'] = true;
|
|
|
|
|
|
|
|
$app->register(new Silex\Provider\TwigServiceProvider(), array(
|
2013-10-16 09:34:56 +02:00
|
|
|
'twig.path' => __DIR__ . '/templates',
|
2013-09-11 15:27:52 +02:00
|
|
|
));
|
|
|
|
|
2013-09-19 14:58:27 +02:00
|
|
|
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
|
2013-10-16 09:34:56 +02:00
|
|
|
'locale_fallback' => 'de',
|
2013-09-19 14:58:27 +02:00
|
|
|
));
|
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
$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');
|
2013-09-19 14:58:27 +02:00
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
return $translator;
|
2013-09-19 14:58:27 +02:00
|
|
|
}));
|
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
$app->before(function (Request $request) use ($app) {
|
2013-09-19 14:58:27 +02:00
|
|
|
$lang = $request->getPreferredLanguage(array('en', 'de'));
|
|
|
|
$app['translator']->setLocale($lang);
|
2013-09-11 15:27:52 +02:00
|
|
|
});
|
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
$app->get('/', function (Request $request) use ($app, $config) {
|
2013-09-19 14:58:27 +02:00
|
|
|
return $app['twig']->render('registration_form.twig', array(
|
2013-10-16 09:34:56 +02:00
|
|
|
'hosts' => $config['hosts'],
|
|
|
|
'errors' => array(),
|
2013-09-11 15:27:52 +02:00
|
|
|
));
|
|
|
|
});
|
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
$app->post('/', function (Request $request) use ($app, $config) {
|
2013-09-19 14:59:38 +02:00
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
// collect the params
|
2013-10-16 09:34:56 +02:00
|
|
|
$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);
|
2013-09-19 14:59:38 +02:00
|
|
|
|
|
|
|
// check for errors
|
|
|
|
if (!$user) {
|
|
|
|
$errors[] = $app->trans('Kein Benutzername angegeben.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$host) {
|
|
|
|
$errors[] = $app->trans('Keinen Hostnamen angegeben.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$email) {
|
|
|
|
$errors[] = $app->trans('Keine E-Mail-Adresse angegeben.');
|
|
|
|
} else {
|
|
|
|
if (!is_email($email)) {
|
|
|
|
$errors[] = $app->trans('Keine gültige E-Mail-Adresse angegeben.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$password) {
|
|
|
|
$errors[] = $app->trans('Kein Passwort angegeben.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($password != $password_repeat) {
|
|
|
|
$errors[] = $app->trans('Bitte gebe in den Feldern Passwort und Passwortwiederholung identische Werte ein.');
|
|
|
|
}
|
2013-10-16 09:34:56 +02:00
|
|
|
|
|
|
|
if (count($errors) == 0) {
|
|
|
|
$client = new Client($config['prosody']['http_base']);
|
|
|
|
|
|
|
|
$request = $client
|
2013-10-16 11:13:54 +02:00
|
|
|
->get($config['prosody']['url_prefix'] . 'user/' . $user, array('Host' => $host))
|
2013-10-16 09:34:56 +02:00
|
|
|
->setAuth($config['prosody']['user'], $config['prosody']['password']);
|
|
|
|
|
2013-10-16 11:13:54 +02:00
|
|
|
try {
|
|
|
|
$response = $request->send();
|
2013-10-16 12:10:59 +02:00
|
|
|
if ($response->getCode() != 404) {
|
2013-10-16 11:13:54 +02:00
|
|
|
$errors[] = $app->trans('Der Benutzername ist bereits vergeben.');
|
|
|
|
}
|
2013-10-16 12:10:59 +02:00
|
|
|
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
|
|
|
|
|
2013-10-16 09:34:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 14:59:38 +02:00
|
|
|
if (count($errors) > 0) {
|
|
|
|
return $app['twig']->render('registration_form.twig', array(
|
2013-10-16 09:34:56 +02:00
|
|
|
'hosts' => $config['hosts'],
|
2013-09-19 14:59:38 +02:00
|
|
|
'errors' => $errors,
|
|
|
|
));
|
|
|
|
} else {
|
2013-10-16 09:34:56 +02:00
|
|
|
return $app['twig']->render('success.twig', array());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$app->get('/{verifycode}', function ($verifycode) use ($app, $config) {
|
2013-10-16 12:10:59 +02:00
|
|
|
if (file_exists('validations/' . $verifycode)) {
|
|
|
|
$data = json_decode(file_get_contents('validations/' . $verifycode));
|
2013-10-16 09:34:56 +02:00
|
|
|
|
2013-10-16 12:10:59 +02:00
|
|
|
$jid = $data->username . '@' . $data->server;
|
2013-10-16 09:34:56 +02:00
|
|
|
|
|
|
|
$client = new Client($config['prosody']['http_base']);
|
|
|
|
|
|
|
|
$request = $client
|
2013-10-16 12:10:59 +02:00
|
|
|
->post($config['prosody']['url_prefix'] . 'user/' . $data->username, array(
|
2013-10-16 09:34:56 +02:00
|
|
|
'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');
|
2013-09-19 14:59:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-09-11 15:27:52 +02:00
|
|
|
$app->run();
|