]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/API/Pages/API/RestoreRun.php
baculum: Add state, number, boolean and id validators
[bacula/bacula] / gui / baculum / protected / API / Pages / API / RestoreRun.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 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('Application.API.Class.APIDbModule');
24  
25 class RestoreRun extends BaculumAPIServer {
26
27         public function create($params) {
28                 $misc = $this->getModule('misc');
29                 $client = null;
30                 if (property_exists($params, 'clientid')) {
31                         $clientid = intval($params->clientid);
32                         $client_row = $this->getModule('client')->getClientById($clientid);
33                         $client = is_object($client_row) ? $client_row->name : null;
34                 } elseif (property_exists($params, 'client') && $misc->isValidName($params->client)) {
35                         $client = $params->client;
36                 }
37
38                 $rfile = property_exists($params, 'rpath') ? $params->rpath : null;
39                 $priority = property_exists($params, 'priority') ? intval($params->priority) : 10; // default priority is set to 10
40                 $where = property_exists($params, 'where') ? $params->where : null;
41                 $replace = property_exists($params, 'replace') ? $params->replace : null;
42
43                 $restorejob = null;
44                 if (property_exists($params, 'restorejob') && $misc->isValidName($params->restorejob)) {
45                         $restorejob = $params->restorejob;
46                 }
47                 $strip_prefix = null;
48                 if (property_exists($params, 'strip_prefix') && $misc->isValidPath($params->strip_prefix)) {
49                         $strip_prefix = $params->strip_prefix;
50                 }
51                 $add_prefix = null;
52                 if (property_exists($params, 'add_prefix') && $misc->isValidPath($params->add_prefix)) {
53                         $add_prefix = $params->add_prefix;
54                 }
55                 $add_suffix = null;
56                 if (property_exists($params, 'add_suffix') && $misc->isValidPath($params->add_suffix)) {
57                         $add_suffix = $params->add_suffix;
58                 }
59                 $regex_where = null;
60                 if (property_exists($params, 'regex_where') && $misc->isValidPath($params->regex_where)) {
61                         $regex_where = $params->regex_where;
62                 }
63
64                 if(is_null($client)) {
65                         $this->output = JobError::MSG_ERROR_CLIENT_DOES_NOT_EXISTS;
66                         $this->error = JobError::ERROR_CLIENT_DOES_NOT_EXISTS;
67                         return;
68                 }
69
70                 if(preg_match($misc::RPATH_PATTERN, $rfile) !== 1) {
71                         $this->output = JobError::MSG_ERROR_INVALID_RPATH;
72                         $this->error = JobError::ERROR_INVALID_RPATH;
73                         return;
74                 }
75                 if(!is_null($where) && !$misc->isValidPath($where)) {
76                         $this->output = JobError::MSG_ERROR_INVALID_WHERE_OPTION;
77                         $this->error = JobError::ERROR_INVALID_WHERE_OPTION;
78                         return;
79                 }
80
81                 if(!is_null($replace) && !$misc->isValidReplace($replace)) {
82                         $this->output = JobError::MSG_ERROR_INVALID_REPLACE_OPTION;
83                         $this->error = JobError::ERROR_INVALID_REPLACE_OPTION;
84                         return;
85                 }
86
87                 $command = array('restore',
88                         'file="?' . $rfile . '"',
89                         'client="' . $client . '"'
90                 );
91
92                 if (is_string($replace)) {
93                         $command[] = 'replace="' . $replace . '"';
94                 }
95                 if (is_int($priority)) {
96                         $command[] = 'priority="' . $priority . '"';
97                 }
98                 if (is_string($restorejob)) {
99                         $command[] = 'restorejob="' . $restorejob . '"';
100                 }
101                 if (is_string($strip_prefix)) {
102                         $command[] = 'strip_prefix="' . $strip_prefix . '"';
103                 }
104                 if (is_string($add_prefix)) {
105                         $command[] = 'add_prefix="' . $add_prefix . '"';
106                 } elseif (is_string($where)) {
107                         $command[] = 'where="' . $where . '"';
108                 }
109                 if (is_string($add_suffix)) {
110                         $command[] = 'add_suffix="' . $add_suffix . '"';
111                 }
112                 if (is_string($regex_where)) {
113                         $command[] = 'regexwhere="' . $regex_where . '"';
114                 }
115                 $command[] = 'yes';
116
117                 $restore = $this->getModule('bconsole')->bconsoleCommand($this->director, $command, $this->user);
118                 $this->removeTmpRestoreTable($rfile);
119                 $this->output = $restore->output;
120                 $this->error = (integer)$restore->exitcode;
121         }
122
123         private function removeTmpRestoreTable($tableName) {
124                 $misc = $this->getModule('misc');
125                 if (preg_match($misc::RPATH_PATTERN, $tableName) === 1) {
126                         // @TODO: Move it to API module. It shouldn't look like here.
127                         $db_params = $this->getModule('api_config')->getConfig('db');
128                         $connection = APIDbModule::getAPIDbConnection($db_params);
129                         $connection->setActive(true);
130                         $sql = "DROP TABLE $tableName";
131                         $pdo = $connection->getPdoInstance();
132                         try {
133                                 $pdo->exec($sql);
134                         } catch(PDOException $e) {
135                                 $emsg = 'Problem during delete temporary Bvfs table. ' . $e->getMessage();
136                                 $this->getModule('logging')->log(
137                                         __FUNCTION__,
138                                         $emsg,
139                                         Logging::CATEGORY_APPLICATION,
140                                         __FILE__,
141                                         __LINE__
142                                 );
143                         }
144                         $pdo = null;
145                 }
146         }
147 }
148
149 ?>