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