]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Common/Class/BaculumUrlMapping.php
baculum: Throw 404 error if service not known
[bacula/bacula] / gui / baculum / protected / Common / Class / BaculumUrlMapping.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2016 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22
23 Prado::using('System.Web.TUrlMapping');
24
25 class BaculumUrlMapping extends TUrlMapping {
26
27         private $services = array(
28                 'web' => array(
29                         'url_manager' => 'Application.Web.Class.WebUrlMapping',
30                         'url_pattern' => '!^(/index\.php)?/web([/,].*)?$!',
31                         'endpoints' => 'Application.Web.endpoints'
32                 ),
33                 'api' => array(
34                         'url_manager' => 'Application.API.Class.APIUrlMapping',
35                         'url_pattern' => '!^(/index\.php)?/api([/,].*)?$!',
36                         'endpoints' => 'Application.API.endpoints'
37                 )
38         );
39
40         public function __construct() {
41                 parent::__construct();
42                 $this->setServiceUrlManager();
43         }
44
45         private function getServiceID() {
46                 $service_id = null;
47                 $url = $this->getRequestedUrl();
48                 foreach ($this->services as $id => $params) {
49                         if (preg_match($params['url_pattern'], $url) === 1) {
50                                 $service_id = $id;
51                                 break;
52                         }
53                 }
54                 return $service_id;
55         }
56
57         private function setServiceUrlManager() {
58                 $service_id = $this->getServiceID();
59                 $url = $this->getRequestedUrl();
60                 if (array_key_exists($service_id, $this->services)) {
61                         $service = $this->services[$service_id];
62                         $path = Prado::getPathOfNamespace($service['url_manager'], Prado::CLASS_FILE_EXT);
63                         if (file_exists($path)) {
64                                 $this->setDefaultMappingClass($service['url_manager']);
65                                 $this->setConfigFile($service['endpoints']);
66                         }
67                 } elseif (!empty($url)) {
68                         throw new THttpException(404, 'pageservice_page_unknown', $url);
69                 }
70         }
71
72         private function getRequestedUrl() {
73                 $url = $this->getRequest()->getPathInfo();
74                 return $url;
75         }
76 }
77 ?>