1: <?php
2:
3: 4: 5:
6:
7: namespace Mapbender\CoreBundle\Controller;
8:
9: use Mapbender\CoreBundle\Component\Application;
10: use Mapbender\CoreBundle\Entity\Application as ApplicationEntity;
11: use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
13: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14: use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15: use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16: use Symfony\Component\HttpFoundation\Response;
17:
18: 19: 20: 21: 22:
23: class ApplicationController extends Controller {
24: 25: 26: 27: 28: 29:
30: private function getUrls($slug) {
31: $base_url = $this->get('request')->getBaseUrl();
32: $element_url = $this->get('router')
33: ->generate('mapbender_core_application_element',
34: array('slug' => $slug));
35: $translation_url = $this->get('router')
36: ->generate('mapbender_core_translation_trans');
37: $proxy_url = $this->get('router')
38: ->generate('owsproxy3_core_owsproxy_entrypoint');
39:
40:
41: $drupal_mark = function_exists('mapbender_menu') ? '?q=mapbender' : 'mapbender';
42: $base_url = str_replace('mapbender', $drupal_mark, $base_url);
43: $element_url = str_replace('mapbender', $drupal_mark, $element_url);
44: $translation_url = str_replace('mapbender', $drupal_mark, $translation_url);
45: $proxy_url = str_replace('mapbender', $drupal_mark, $proxy_url);
46:
47: return array(
48: 'base' => $base_url,
49:
50: 'asset' => rtrim($this->get('templating.helper.assets')
51: ->getUrl('.'), '.'),
52: 'element' => $element_url,
53: 'trans' => $translation_url,
54: 'proxy' => $proxy_url);
55: }
56:
57: 58: 59: 60: 61: 62: 63: 64:
65: public function assetsAction($slug, $type) {
66: $response = new Response();
67: $application = $this->getApplication($slug);
68: $assets = $application->getAssets($type);
69: $asset_modification_time = new \DateTime();
70: $asset_modification_time->setTimestamp($assets->getLastModified());
71:
72:
73:
74:
75:
76: $filters = array(
77: 'js' => array(),
78: 'css' => array($this->container->get('assetic.filter.cssrewrite')));
79:
80:
81:
82: $path = $this->get('request')->server->get('PATH_INFO');
83: if(!$path) {
84: $path = $this->get('request')->server->get('REQUEST_URI');
85: }
86:
87: $target = str_replace('\\', '/', $this->get('request')->server->get('SCRIPT_FILENAME')
88: . $path);
89:
90: $mimetypes = array(
91: 'css' => 'text/css',
92: 'js' => 'application/javascript');
93:
94: $application_update_time = new \DateTime();
95: $application_entity = $this->getApplication($slug)->getEntity();
96:
97:
98: if($application->getEntity()->getSource() === ApplicationEntity::SOURCE_DB) {
99: $updateTime = max($application->getEntity()->getUpdated(),
100: $asset_modification_time);
101: } else {
102: $cacheUpdateTime = new \DateTime($this->container->getParameter('mapbender.cache_creation'));
103: $updateTime = max($cacheUpdateTime, $asset_modification_time);
104: }
105:
106: $response->setLastModified($updateTime);
107: if($response->isNotModified($this->get('request'))) {
108: return $response;
109: }
110:
111:
112:
113: $parts = array();
114: foreach($assets->all() as $asset) {
115: foreach($filters[$type] as $filter) {
116: $asset->ensureFilter($filter);
117: }
118: $asset->setTargetPath($target);
119: $parts[] = $asset->dump();
120: }
121:
122:
123: $response->headers->set('Content-Type', $mimetypes[$type]);
124: $response->setContent(implode("\n", $parts));
125: return $response;
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135:
136: public function elementAction($slug, $id, $action) {
137: $element = $this->getApplication($slug)->getElement($id);
138:
139:
140:
141: return $element->httpAction($action);
142: }
143:
144: 145: 146: 147: 148: 149:
150: public function applicationAction($slug) {
151: $application = $this->getApplication($slug);
152:
153:
154:
155: $this->get("session")->set("proxyAllowed",true);
156:
157: return new Response($application->render());
158: }
159:
160: 161: 162: 163: 164: 165: 166: 167: 168:
169: private function getApplication($slug) {
170: $application = $this->get('mapbender')
171: ->getApplication($slug, $this->getUrls($slug));
172:
173: if($application === null) {
174: throw new NotFoundHttpException(
175: 'The application can not be found.');
176: }
177:
178: $this->checkApplicationAccess($application);
179:
180: return $application;
181: }
182:
183: 184: 185: 186: 187: 188: 189: 190:
191: public function checkApplicationAccess(Application $application) {
192: $securityContext = $this->get('security.context');
193:
194: $application_entity = $application->getEntity();
195: if($application_entity::SOURCE_YAML === $application_entity->getSource()
196: && count($application_entity->yaml_roles)) {
197: $passed = false;
198: foreach($application_entity->yaml_roles as $role) {
199: if($securityContext->isGranted($role)) {
200: $passed = true;
201: break;
202: }
203: }
204: if(!$passed) {
205: throw new AccessDeniedException('You are not granted view permissions for this application.');
206: }
207: }
208:
209: $granted = $securityContext->isGranted('VIEW', $application_entity);
210: if(false === $granted) {
211: throw new AccessDeniedException('You are not granted view permissions for this application.');
212: }
213:
214: if(!$application_entity->isPublished() and !$securityContext->isGranted('EDIT', $application_entity)) {
215: throw new AccessDeniedException('This application is not published at the moment');
216: }
217: }
218: }
219: