]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/Miscellaneous.php
cf149e9b80f2a604554cc0cb15ba8bd086ce4531
[bacula/bacula] / gui / baculum / protected / Class / Miscellaneous.php
1 <?php
2
3 /*
4  * Small sorting callback function to sort files and directories by name.
5  * Function keeps '.' and '..' names always in the beginning of array.
6  * Used to sort files and directories from Bvfs.
7  */
8 function sortFilesListByName($a, $b) {
9         $firstLeft = substr($a['name'], 0, 1);
10         $firstRight = substr($b['name'], 0, 1);
11         if ($firstLeft == '.' && $firstRight != '.') {
12                 return -1;
13         } else if ($firstRight == '.' && $firstLeft != '.') {
14                 return 1;
15         }
16         return strcasecmp($a['name'], $b['name']);
17 }
18
19 class Miscellaneous extends TModule {
20
21         const LICENCE_FILE = 'LICENSE';
22
23         private $jobTypes = array(
24                 'B' => 'Backup',
25                 'M' => 'Migrated',
26                 'V' => 'Verify',
27                 'R' => 'Restore',
28                 'I' => 'Internal',
29                 'D' => 'Admin',
30                 'A' => 'Archive',
31                 'C' => 'Copy',
32                 'g' => 'Migration'
33         );
34
35         private $jobLevels = array(
36                 'F' => 'Full',
37                 'I' => 'Incremental',
38                 'D' => 'Differential',
39                 'B' => 'Base',
40                 'f' => 'VirtualFull',
41                 'V' => 'InitCatalog',
42                 'C' => 'Catalog',
43                 'O' => 'VolumeToCatalog',
44                 'd' => 'DiskToCatalog'
45         );
46
47         private $jobStates =  array(
48                 'C' => array('value' => 'Created', 'description' =>'Created but not yet running'),
49                 'R' => array('value' => 'Running', 'description' => 'Running'),
50                 'B' => array('value' => 'Blocked', 'description' => 'Blocked'),
51                 'T' => array('value' => 'Terminated', 'description' =>'Terminated normally'),
52                 'W' => array('value' => 'Terminated with warnings', 'description' =>'Terminated normally with warnings'),
53                 'E' => array('value' => 'Error', 'description' =>'Terminated in Error'),
54                 'e' => array('value' => 'Non-fatal error', 'description' =>'Non-fatal error'),
55                 'f' => array('value' => 'Fatal error', 'description' =>'Fatal error'),
56                 'D' => array('value' => 'Verify Diff.', 'description' =>'Verify Differences'),
57                 'A' => array('value' => 'Canceled by user', 'description' =>'Canceled by the user'),
58                 'I' => array('value' => 'Incomplete', 'description' =>'Incomplete Job'),
59                 'F' => array('value' => 'Waiting on FD', 'description' =>'Waiting on the File daemon'),
60                 'S' => array('value' => 'Waiting on SD', 'description' =>'Waiting on the Storage daemon'),
61                 'm' => array('value' => 'Waiting for new vol.', 'description' =>'Waiting for a new Volume to be mounted'),
62                 'M' => array('value' => 'Waiting for mount', 'description' =>'Waiting for a Mount'),
63                 's' => array('value' => 'Waiting for storage', 'description' =>'Waiting for Storage resource'),
64                 'j' => array('value' => 'Waiting for job', 'description' =>'Waiting for Job resource'),
65                 'c' => array('value' => 'Waiting for client', 'description' =>'Waiting for Client resource'),
66                 'd' => array('value' => 'Waiting for Max. jobs', 'description' =>'Wating for Maximum jobs'),
67                 't' => array('value' => 'Waiting for start', 'description' =>'Waiting for Start Time'),
68                 'p' => array('value' => 'Waiting for higher priority', 'description' =>'Waiting for higher priority job to finish'),
69                 'i' => array('value' => 'Batch insert', 'description' =>'Doing batch insert file records'),
70                 'a' => array('value' => 'Despooling attributes', 'description' =>'SD despooling attributes'),
71                 'l' => array('value' => 'Data despooling', 'description' =>'Doing data despooling'),
72                 'L' => array('value' => 'Commiting data', 'description' =>'Committing data (last despool)')
73         );
74
75         private $runningJobStates = array('C', 'R');
76
77         /**
78          * Getting the licence from file.
79          * 
80          * @access public
81          * @return string licence text
82          */
83         public function getLicence() {
84                 return nl2br(htmlspecialchars(file_get_contents(self::LICENCE_FILE)));
85         }
86
87         public function getJobLevels() {
88                 return $this->jobLevels;
89         }
90
91         public function getJobState($jobStateLetter = null) {
92                 $state;
93                 if(is_null($jobStateLetter)) {
94                         $state = $this->jobStates;
95                 } else {
96                         $state = array_key_exists($jobStateLetter, $this->jobStates) ? $this->jobStates[$jobStateLetter] : null;
97                 }
98                 return $state;
99         }
100
101         public function getRunningJobStates() {
102                 return $this->runningJobStates;
103         }
104
105
106         public function getJobType($jobTypeLetter = null) {
107                 $type;
108                 if(is_null($jobTypeLetter)) {
109                         $type = $this->jobTypes;
110                 } else {
111                         $type = array_key_exists($jobTypeLetter, $this->jobTypes) ? $this->jobTypes[$jobTypeLetter] : null;
112                 }
113                 return $type;
114
115         }
116
117         public function isValidJobLevel($jobLevel) {
118                 return array_key_exists($jobLevel, $this->getJobLevels());
119         }
120
121         /**
122          * Writing INI-style configuration file.
123          * 
124          * Functions has been got from StackOverflow.com service (http://stackoverflow.com/questions/4082626/save-ini-file-with-comments).
125          * 
126          * @access public
127          * @param string $file file localization
128          * @param array $options structure of config file params
129          * @return mixed if success then returns the number of bytes that were written to the file as the integer type, if failure then returns false
130          */
131         public function writeINIFile($file, array $options){
132                 $tmp = '';
133                 foreach($options as $section => $values){
134                         $tmp .= "[$section]\n";
135                                 foreach($values as $key => $val){
136                                         if(is_array($val)){
137                                                 foreach($val as $k =>$v){
138                                                         $tmp .= "{$key}[$k] = \"$v\"\n";
139                                                 }
140                                         } else {
141                                                 $tmp .= "$key = \"$val\"\n";
142                                         }
143                                 }
144                         $tmp .= "\n";
145                 }
146                 return file_put_contents($file, $tmp);
147         }
148
149         /**
150          * Parse INI-style configuration file.
151          * 
152          * @access public
153          * @param string $file file localization
154          * @return array data of configuration file
155          */
156         public static function parseINIFile($file) {
157                 return file_exists($file) ? parse_ini_file($file, true) : array();
158         }
159
160
161         /**
162          * This method is copied from http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
163          */
164         public function objectToArray($data) {
165                 if (is_array($data) || is_object($data)) {
166                         $result = array();
167                         foreach ($data as $key => $value) {
168                                 $result[$key] = $this->objectToArray($value);
169                         }
170                         return $result;
171                 }
172                 return $data;
173         }
174
175         public function decode_bacula_lstat($lstat) {
176                 $base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
177                 $lstat = trim($lstat);
178                 $lstat_fields = explode(' ', $lstat);
179
180                 if(count($lstat_fields) !== 16) {
181                         die('Błąd! Niepoprawna ilość pól wartości LStat. Proszę upewnić się, że podany ciąg znaków jest poprawną wartością LStat');
182                 }
183
184                 list($dev, $inode, $mode, $nlink, $uid, $gid, $rdev, $size, $blocksize, $blocks, $atime, $mtime, $ctime, $linkfi, $flags, $data) = $lstat_fields;
185                 $encoded_values = array('dev' => $dev, 'inode' => $inode, 'mode' => $mode, 'nlink' => $nlink, 'uid' => $uid, 'gid' => $gid, 'rdev' => $rdev, 'size' => $size, 'blocksize' => $blocksize, 'blocks' => $blocks, 'atime' => $atime, 'mtime' => $mtime, 'ctime' => $ctime, 'linkfi' => $linkfi, 'flags' => $flags, 'data' => $data);
186
187                 $ret = array();
188                 foreach($encoded_values as $key => $val) {
189                         $result = 0;
190                         $is_minus = false;
191                         $start = 0;
192
193                         if(substr($val, 0, 1) === '-') {
194                                 $is_minus = true;
195                                 $start++;
196                         }
197
198                         for($i = $start; $i < strlen($val); $i++) {
199                                 $result = bcmul($result, bcpow(2,6));
200                                 $result +=  strpos($base64, substr($val, $i , 1));
201                         }
202                         $ret[$key] = ($is_minus === true) ? -$result : $result;
203                 }
204                 return $ret;
205         }
206
207         public function parseBvfsList($list) {
208                 $elements = array();
209                 for($i = 0; $i < count($list); $i++) {
210                         if(preg_match('/^(?P<pathid>\d+)\t(?P<filenameid>\d+)\t(?P<fileid>\d+)\t(?P<jobid>\d+)\t(?P<lstat>[a-zA-z0-9\+\/\ ]+)\t(?P<name>.*)\/$/', $list[$i], $match) == 1 || preg_match('/^(?P<pathid>\d+)\t(?P<filenameid>\d+)\t(?P<fileid>\d+)\t(?P<jobid>\d+)\t(?P<lstat>[a-zA-z0-9\+\/\ ]+)\t(?P<name>\.{2})$/', $list[$i], $match) == 1) {
211                                 if($match['name'] == '.') {
212                                         continue;
213                                 } elseif($match['name'] != '..') {
214                                         $match['name'] .= '/';
215                                 }
216                                 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'dir');
217                         } elseif(preg_match('/^(?P<pathid>\d+)\t(?P<filenameid>\d+)\t(?P<fileid>\d+)\t(?P<jobid>\d+)\t(?P<lstat>[a-zA-z0-9\+\/\ ]+)\t(?P<name>[^\/]+)$/', $list[$i], $match) == 1) {
218                                 if($match['name'] == '.') {
219                                         continue;
220                                 }
221                                 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'file'); 
222                         }
223                 }
224                 usort($elements, 'sortFilesListByName');
225                 return $elements;
226         }
227
228         public function parseFileVersions($filename, $list) {
229                 $elements = array();
230                 for($i = 0; $i < count($list); $i++) {
231                         if(preg_match('/^(?P<pathid>\d+)\t(?P<filenameid>\d+)\t(?P<fileid>\d+)\t(?P<jobid>\d+)\t(?P<lstat>[a-zA-z0-9\+\/\ ]+)\t(?P<md5>.+)\t(?P<volname>.+)\t(?P<inchanger>\d+)$/', $list[$i], $match) == 1) {
232                                 $elements[$match['fileid']] = array('name' => $filename, 'pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $this->decode_bacula_lstat($match['lstat']), 'md5' => $match['md5'], 'volname' => $match['volname'], 'inchanger' => $match['inchanger'], 'type' => 'file');
233                         }
234                 }
235                 return $elements;
236         }
237 }
238 ?>