This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
170 lines (130 loc) · 4.8 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
require 'vendor/autoload.php';
require_once 'is_email.php';
require_once 'config.php';
use Silex\Application\TranslationTrait;
use Symfony\Component\HttpFoundation\Request;
use Guzzle\Http\Client;
use Silex\Application;
class RegistrationApplication extends Application {
use Application\TranslationTrait;
use Application\TwigTrait;
}
$app = new RegistrationApplication();
$app['debug'] = true;
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/templates',
));
$app->register(new Silex\Provider\TranslationServiceProvider(), array(
'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');
return $translator;
}));
$app->before(function (Request $request) use ($app) {
$lang = $request->getPreferredLanguage(array('en', 'de'));
$app['translator']->setLocale($lang);
});
$app->get('/', function (Request $request) use ($app, $config) {
return $app['twig']->render('registration_form.twig', array(
'hosts' => $config['hosts'],
'errors' => array(),
));
});
$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);
// 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.');
}
if (count($errors) == 0) {
$client = new Client($config['prosody']['http_base']);
$request = $client
->get($config['prosody']['url_prefix'] . 'user/' . $user, array('Host' => $host))
->setAuth($config['prosody']['user'], $config['prosody']['password']);
try {
$response = $request->send();
if ($response->getCode() != 404) {
$errors[] = $app->trans('Der Benutzername ist bereits vergeben.');
}
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
}
}
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());
}
});
$app->get('/{verifycode}', function ($verifycode) use ($app, $config) {
if (file_exists('validations/' . $verifycode)) {
$data = json_decode(file_get_contents('validations/' . $verifycode));
$jid = $data->username . '@' . $data->server;
$client = new Client($config['prosody']['http_base']);
$request = $client
->post($config['prosody']['url_prefix'] . 'user/' . $data->username, 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');
}
});
$app->run();