1<?php
2
3/**
4 *
5 * bareos-webui - Bareos Web-Frontend
6 *
7 * @link      https://github.com/bareos/bareos-webui for the canonical source repository
8 * @copyright Copyright (c) 2013-2016 Bareos GmbH & Co. KG (http://www.bareos.org/)
9 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
10 * @author    Frank Bergkemper
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27namespace Application;
28
29use Zend\Mvc\ModuleRouteListener;
30use Zend\Mvc\MvcEvent;
31use Zend\Session\SessionManager;
32use Zend\Session\Container;
33use Bareos\BSock\BareosBsock;
34
35class Module
36{
37
38   public function onBootstrap(MvcEvent $e)
39   {
40      $eventManager = $e->getApplication()->getEventManager();
41      $moduleRouteListener = new ModuleRouteListener();
42      $moduleRouteListener->attach($eventManager);
43      $this->initSession($e);
44
45      $translator = $e->getApplication()->getServiceManager()->get('MVCTranslator');
46      $translator->setLocale( ( isset( $_SESSION['bareos']['locale'] ) ? $_SESSION['bareos']['locale'] : 'en_EN' ) )->setFallbackLocale('en_EN');
47
48      $viewModel = $e->getApplication()->getMVCEvent()->getViewModel();
49      $viewModel->dirdUpdate = $_SESSION['bareos']['dird-update-available'];
50
51   }
52
53   public function getConfig()
54   {
55      return include __DIR__ . '/config/module.config.php';
56   }
57
58   public function getAutoloaderConfig()
59   {
60      return array(
61            'Zend\Loader\ClassMapAutoloader' => array(
62            'application' => __DIR__ . '/autoload_classmap.php',
63         ),
64         'Zend\Loader\StandardAutoloader' => array(
65            'namespaces' => array(
66                __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
67               'Bareos' => __DIR__ .'/../../vendor/Bareos/library/Bareos',
68            ),
69         ),
70      );
71   }
72
73   public function initSession($e)
74   {
75      $session = $e->getApplication()->getServiceManager()->get('Zend\Session\SessionManager');
76
77      if($session->isValid()) {
78         // do nothing
79      }
80      else {
81         $session->expireSessionCookie();
82         $session->destroy();
83         $session->start();
84      }
85
86      $container = new Container('bareos');
87
88      if(!isset($container->init)) {
89
90         $serviceManager = $e->getApplication()->getServiceManager();
91         $request = $serviceManager->get('Request');
92
93         $session->regenerateId(true);
94         $container->init      = 1;
95         $container->remoteAddr      = $request->getServer()->get('REMOTE_ADDR');
96         $container->httpUserAgent   = $request->getServer()->get('HTTP_USER_AGENT');
97         $container->username       = "";
98         $container->authenticated   = false;
99
100         $config = $serviceManager->get('Config');
101
102         if(!isset($config['session'])) {
103            return;
104         }
105
106         $sessionConfig = $config['session'];
107
108         if(isset($sessionConfig['validators'])) {
109            $chain = $session->getValidatorChain();
110            foreach($sessionConfig['validators'] as $validator) {
111               switch ($validator) {
112                  case 'Zend\Session\Validator\HttpUserAgent':
113                     $validator = new $validator($container->httpUserAgent);
114                     break;
115                  case 'Zend\Session\Validator\RemoteAddr':
116                     $validator = new $validator($container->remoteAddr);
117                     break;
118                  default:
119                     $validator = new $validator();
120               }
121               $chain->attach('session.validate', array($validator, 'isValid'));
122            }
123         }
124
125      }
126
127   }
128
129   public function getServiceConfig()
130   {
131      return array(
132         'factories' => array(
133            'Zend\Session\SessionManager' => function ($sm) {
134
135               $config = $sm->get('config');
136
137               if (isset($config['session'])) {
138
139                  $session = $config['session'];
140
141                  $sessionConfig = null;
142
143                  if(isset($session['config'])) {
144                     $class = isset($session['config']['class'])  ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
145                     $options = isset($session['config']['options']) ? $session['config']['options'] : array();
146                     $sessionConfig = new $class();
147                     $sessionConfig->setOptions($options);
148                  }
149
150                  $sessionStorage = null;
151
152                  if (isset($session['storage'])) {
153                     $class = $session['storage'];
154                     $sessionStorage = new $class();
155                  }
156
157                  $sessionSaveHandler = null;
158
159                  if (isset($session['save_handler'])) {
160                     // class should be fetched from service manager since it will require constructor arguments
161                     $sessionSaveHandler = $sm->get($session['save_handler']);
162                  }
163
164                  $sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);
165
166                  } else {
167                     $sessionManager = new SessionManager();
168                  }
169
170                  Container::setDefaultManager($sessionManager);
171
172                  return $sessionManager;
173
174            },
175
176         ),
177
178      );
179
180   }
181
182}
183