1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\HttpDI;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class HttpExtension extends Nette\DI\CompilerExtension
17: {
18: public $defaults = array(
19: 'proxy' => array(),
20: 'headers' => array(
21: 'X-Powered-By' => 'Nette Framework',
22: 'Content-Type' => 'text/html; charset=utf-8',
23: ),
24: 'frames' => 'SAMEORIGIN',
25: );
26:
27:
28: public function loadConfiguration()
29: {
30: $container = $this->getContainerBuilder();
31: $config = $this->validateConfig($this->defaults);
32:
33: $container->addDefinition($this->prefix('requestFactory'))
34: ->setClass('Nette\Http\RequestFactory')
35: ->addSetup('setProxy', array($config['proxy']));
36:
37: $container->addDefinition($this->prefix('request'))
38: ->setClass('Nette\Http\Request')
39: ->setFactory('@Nette\Http\RequestFactory::createHttpRequest');
40:
41: $container->addDefinition($this->prefix('response'))
42: ->setClass('Nette\Http\Response');
43:
44: $container->addDefinition($this->prefix('context'))
45: ->setClass('Nette\Http\Context');
46:
47: if ($this->name === 'http') {
48: $container->addAlias('nette.httpRequestFactory', $this->prefix('requestFactory'));
49: $container->addAlias('nette.httpContext', $this->prefix('context'));
50: $container->addAlias('httpRequest', $this->prefix('request'));
51: $container->addAlias('httpResponse', $this->prefix('response'));
52: }
53: }
54:
55:
56: public function afterCompile(Nette\PhpGenerator\ClassType $class)
57: {
58: if (PHP_SAPI === 'cli') {
59: return;
60: }
61:
62: $initialize = $class->getMethod('initialize');
63: $config = $this->getConfig();
64:
65: if (isset($config['frames']) && $config['frames'] !== TRUE) {
66: $frames = $config['frames'];
67: if ($frames === FALSE) {
68: $frames = 'DENY';
69: } elseif (preg_match('#^https?:#', $frames)) {
70: $frames = "ALLOW-FROM $frames";
71: }
72: $initialize->addBody('header(?);', array("X-Frame-Options: $frames"));
73: }
74:
75: foreach ($config['headers'] as $key => $value) {
76: if ($value != NULL) {
77: $initialize->addBody('header(?);', array("$key: $value"));
78: }
79: }
80: }
81:
82: }
83: