]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Common/Class/BaculumPage.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / protected / Common / Class / BaculumPage.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.UI.TPage');
24
25 /**
26  * Base pages module.
27  * The module contains methods that are common for all pages (wizards, main
28  * page and error pages).
29  *
30  * @author Marcin Haba <marcin.haba@bacula.pl>
31  */
32 class BaculumPage extends TPage {
33
34         public function onPreInit($param) {
35                 parent::onPreInit($param);
36                 $this->setURLPrefixForSubdir();
37         }
38
39         /**
40          * Shortcut method for getting application modules instances by
41          * module name.
42          *
43          * @access public
44          * @param string $name application module name
45          * @return object module class instance
46          */
47         public function getModule($name) {
48                 return $this->getApplication()->getModule($name);
49         }
50
51         /**
52          * Redirection to a page.
53          * Page name is given in PRADO notation with "dot", for example: (Home.SomePage).
54          *
55          * @access public
56          * @param string $page_name page name to redirect
57          * @param array $params HTTP GET method parameters in associative array
58          * @return none
59          */
60         public function goToPage($page_name, $params = null) {
61                 $url = $this->Service->constructUrl($page_name, $params, false);
62                 $url = str_replace('/index.php', '', $url);
63                 header('Location: ' . $url);
64                 exit();
65         }
66
67         /**
68          * Redirection to default page defined in application config.
69          *
70          * @access public
71          * @param array $params HTTP GET method parameters in associative array
72          * @return none
73          */
74         public function goToDefaultPage($params = null) {
75                 $this->goToPage($this->Service->DefaultPage, $params);
76         }
77
78         /**
79          * Set prefix when Baculum is running in document root subdirectory.
80          * For example:
81          *   web server document root: /var/www/
82          *   Baculum directory /var/www/baculum/
83          *   URL prefix: /baculum/
84          * In this case to base url is added '/baculum/' such as:
85          * http://localhost:9095/baculum/
86          *
87          * @access private
88          * @return none
89          */
90         private function setURLPrefixForSubdir() {
91                 $full_document_root = preg_replace('#(\/)$#', '', $this->getFullDocumentRoot());
92                 $url_prefix = str_replace($full_document_root, '', APPLICATION_DIRECTORY);
93                 if (!empty($url_prefix)) {
94                         $this->Application->getModule('url_manager')->setUrlPrefix($url_prefix);
95                 }
96         }
97
98         /**
99          * Get full document root directory path.
100          * Symbolic links in document root path are translated to full paths.
101          *
102          * @access private
103          * return string full document root directory path
104          */
105         private function getFullDocumentRoot() {
106                 $root_dir = array();
107                 $dirs = explode('/', $_SERVER['DOCUMENT_ROOT']);
108                 for($i = 0; $i < count($dirs); $i++) {
109                         $document_root_part =  implode('/', $root_dir) . '/' . $dirs[$i];
110                         if (is_link($document_root_part)) {
111                                 $root_dir = array(readlink($document_root_part));
112                         } else {
113                                 $root_dir[] = $dirs[$i];
114                         }
115                 }
116
117                 $root_dir = implode('/', $root_dir);
118                 return $root_dir;
119         }
120
121         /**
122          * Log in as specific user.
123          *
124          * Note, usually after this method call there required is using exit() just
125          * after method execution. Otherwise the HTTP redirection may be canceled on some
126          * web servers.
127          *
128          * @access public
129          * @param string $user user name to log in
130          * @param string $string plain text user's password
131          * @return none
132          */
133         public function switchToUser($user, $password) {
134                 $http_protocol = isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) ? 'https' : 'http';
135                 $host = $_SERVER['SERVER_NAME'];
136                 $port = $_SERVER['SERVER_PORT'];
137                 $url_prefix = $this->Application->getModule('url_manager')->getUrlPrefix();
138                 $url_prefix = str_replace('/index.php', '', $url_prefix);
139                 $location = sprintf("%s://%s:%s@%s:%d%s", $http_protocol, $user, $password, $host, $port, $url_prefix);
140
141                 // Log in by header
142                 header("Location: $location");
143         }
144 }
145 ?>