-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
266 lines (222 loc) · 10.4 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\EventListener\RouterListener;
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Router;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader as DIYamlFileLoader;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Http\AccessMap;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\Security\Http\Firewall\AccessListener;
use Symfony\Component\Security\Http\FirewallMap;
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Component\Security\Http\Firewall;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;
use Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener;
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler;
use Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint;
use Symfony\Component\Security\Http\Firewall\LogoutListener;
use Symfony\Component\Security\Http\Logout\DefaultLogoutSuccessHandler;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use ebid\RouterExceptionListener;
use ebid\Auth\AuthenticationSuccessHandler;
use ebid\Auth\AuthenticationFailureHandler;
use ebid\Auth\LogoutSuccessHandler;
use ebid\Event\RegisterEvent;
use ebid\Event\EmailListener;
use ebid\Event\BidResultEvent;
error_reporting(E_ERROR);
ini_set('display_errors','On');
$isDebug = true;
$sendEmail = true;
$root = __DIR__;
$file = __DIR__ .'/cache/container.php';
date_default_timezone_set("America/New_York");
$containerConfigCache = new ConfigCache($file, $isDebug);
if (!$isDebug && file_exists($file) && $containerConfigCache->isFresh()) {
require_once $file;
$container = new CachedContainer();
} else {
//Dependency Injection
$container = new ContainerBuilder();
$loader = new DIYamlFileLoader($container, new FileLocator(__DIR__));
$loader->load('config/services.yml');
$container->compile();
if (!$isDebug) {
$dumper = new PhpDumper($container);
file_put_contents(
$file,
$dumper->dump(array('class' => 'CachedContainer'))
);
}
}
$request = Request::createFromGlobals();
//$session = new Session();
$session = $container->get('Session');
$session->start();
$request->setSession($session);
//$dispatcher = new EventDispatcher();
$dispatcher = $container->get('EventDispatcher');
//$resolver = new ControllerResolver();
$resolver = $container->get('ControllerResolver');
// instantiate the kernel
//$kernel = new HttpKernel($dispatcher, $resolver);
$kernel = $container->get('HttpKernel');
$SmtpTransport = $container->get('SmtpTransport');
$SmtpTransport->setUsername($container->getParameter('mail_username'))
->setPassword($container->getParameter('mail_password'));
//routing
$requestContext = new RequestContext();
$requestContext->fromRequest($request);
$router = new Router(
new YamlFileLoader(new FileLocator(__DIR__)),
'config/routes.yml',
array('cache_dir' => __DIR__.'/cache'),
$requestContext
);
$routeCollection = $router->getRouteCollection();
$matcher = new UrlMatcher($routeCollection, $requestContext);
$dispatcher->addSubscriber(new RouterListener($matcher));
$dispatcher->addSubscriber(new ResponseListener('UTF-8'));
//security
$map = $session->get("firewall_context");
$defaultEncoder = $container->get('MessageDigestPasswordEncoder');
$encoders = array(
'ebid\\Entity\\User' => $defaultEncoder
);
$encoderFactory = new EncoderFactory($encoders);
if($map == null){
$urlgenerator = new UrlGenerator($routeCollection, $requestContext);
$map = setupFireWall($kernel, $dispatcher, $urlgenerator);
$session->set("firewall_context", $map);
}
$firewall = new Firewall($map, $dispatcher);
$dispatcher->addListener(
KernelEvents::REQUEST,
array($firewall, 'onKernelRequest')
);
if($sendEmail){
$dispatcher->addListener(
BidResultEvent::BIDRESULT, array(new EmailListener(), 'sendEmailOnBidFinish'),0
);
$dispatcher->addListener(
RegisterEvent::REGISTER, array(new EmailListener(), 'sendEmailOnRegistration'),0
);
}
$dispatcher->addListener(
KernelEvents::EXCEPTION, array(new RouterExceptionListener(), 'onKernelException'), 0
);
$mysql = $container->get('db');
$mysql->connect();
// actually execute the kernel, which turns the request into a response
// by dispatching events, calling a controller, and returning the response
$response = $kernel->handle($request);
// send the headers and echo the content
$response->send();
// triggers the kernel.terminate event
$kernel->terminate($request, $response);
function _table($table) {
global $container;
$db = $container->get('db');
return $db->getPrefix() . $table;
}
function setupFireWall($kernel, $dispatcher, $urlgenerator){
global $session, $container, $encoderFactory;
$userProvider = $container->get('UserProvider');
/*
$userProvider = new InMemoryUserProvider(
array(
'admin' => array(
// password is "foo"
'password' => '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==',
'roles' => array('ROLE_ADMIN','ROLE_USER'),
),
)
);*/
// for some extra checks: is account enabled, locked, expired, etc.?
$userChecker = new UserChecker();
$provider = new DaoAuthenticationProvider(
$userProvider,
$userChecker,
'ebid.security',
$encoderFactory,
false
);
$authenticationManager = new AuthenticationProviderManager(array($provider));
$anonymousClass = 'Symfony\Component\Security\Core\Authentication\Token\AnonymousToken';
$rememberMeClass = 'Symfony\Component\Security\Core\Authentication\Token\RememberMeToken';
$trustResolver = new AuthenticationTrustResolver($anonymousClass, $rememberMeClass);
$authenticatedVoter = new AuthenticatedVoter($trustResolver);
$roleVoter = new RoleVoter('ROLE_');
$voters = array($authenticatedVoter, $roleVoter);
$accessDecisionManager = new AccessDecisionManager($voters);
$securityContext = new SecurityContext(
$authenticationManager,
$accessDecisionManager
);
$session->set("security_context", $securityContext);
$httpUtils = new HttpUtils($urlgenerator);
//$basicentrypoint = new BasicAuthenticationEntryPoint('Secured Demo Area');
//$basiclistener = new BasicAuthenticationListener($securityContext, $authenticationManager, "ebid.security",$basicentrypoint);
//$userlistener = new UserAuthenticationListener($securityContext, $authenticationManager, "ebid.security", new SessionAuthenticationStrategy(SessionAuthenticationStrategy::INVALIDATE));
/*$userlistener = new UsernamePasswordFormAuthenticationListener($securityContext, $authenticationManager,
new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE), $httpUtils, "ebid.security",
new DefaultAuthenticationSuccessHandler($httpUtils, array('default_target_path'=>'/admin/success')),
new DefaultAuthenticationFailureHandler($kernel, $httpUtils, array('failure_path'=>'/foo/error')),
array('check_path' => '/auth/check'));*/
$userlistener = new UsernamePasswordFormAuthenticationListener($securityContext, $authenticationManager,
new SessionAuthenticationStrategy(SessionAuthenticationStrategy::NONE), $httpUtils, "ebid.security",
new AuthenticationSuccessHandler(),
new AuthenticationFailureHandler(),
array('check_path' => '/auth/login', 'require_previous_session'=> false));
$anonymouslistener = new AnonymousAuthenticationListener($securityContext, "ebid.security");
$accessMap = new AccessMap();
$requestMatcher = new RequestMatcher('^/admin');
$accessMap->add($requestMatcher, array('ROLE_ADMIN'));
$accessListener = new AccessListener(
$securityContext,
$accessDecisionManager,
$accessMap,
$authenticationManager
);
$map = new FirewallMap();
$requestMatcher = new RequestMatcher('^/auth/logout');
$logoutlistener = new LogoutListener($securityContext, $httpUtils, new LogoutSuccessHandler(),
array('logout_path'=> '/auth/logout'));
$listeners = array($accessListener, $logoutlistener);
$exceptionListener = new ExceptionListener($securityContext, $trustResolver, $httpUtils, "ebid.security",
new FormAuthenticationEntryPoint($kernel, $httpUtils, '/auth/login', false));
$map->add($requestMatcher, $listeners, $exceptionListener);
$requestMatcher = new RequestMatcher('^/');
$listeners = array($anonymouslistener, $userlistener, $accessListener);
$map->add($requestMatcher, $listeners, $exceptionListener);
return $map;
//return new Firewall($map, $dispatcher);
}