]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Class/Miscellaneous.php
Add Baculum
[bacula/bacula] / gui / baculum / protected / Class / Miscellaneous.php
1 <?php
2  
3 class Miscellaneous extends TModule {
4
5         const LICENCE_FILE = 'LICENSE';
6
7         private $jobLevels = array('F' => 'Full', 'I' => 'Incremental', 'D'=> 'Differential', 'B' => 'Base', 'f' => 'VirtualFull');
8
9         /**
10          * Getting the licence from file.
11          * 
12          * @access public
13          * @return string licence text
14          */
15         public function getLicence() {
16                 return nl2br(htmlspecialchars(file_get_contents(self::LICENCE_FILE)));
17         }
18
19         public function getJobLevels() {
20                 return $this->jobLevels;
21         }
22
23         public function isValidJobLevel($jobLevel) {
24                 return array_key_exists($jobLevel, $this->getJobLevels());
25         }
26
27         /**
28          * Writing INI-style configuration file.
29          * 
30          * Functions has been got from StackOverflow.com service (http://stackoverflow.com/questions/4082626/save-ini-file-with-comments).
31          * 
32          * @access public
33          * @param string $file file localization
34          * @param array $options structure of config file params
35          * @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
36          */
37         public function writeINIFile($file, array $options){
38                 $tmp = '';
39                 foreach($options as $section => $values){
40                         $tmp .= "[$section]\n";
41                                 foreach($values as $key => $val){
42                                         if(is_array($val)){
43                                                 foreach($val as $k =>$v){
44                                                         $tmp .= "{$key}[$k] = \"$v\"\n";
45                                                 }
46                                         } else {
47                                                 $tmp .= "$key = \"$val\"\n";
48                                         }
49                                 }
50                         $tmp .= "\n";
51                 }
52                 return file_put_contents($file, $tmp);
53         }
54
55         /**
56          * Parse INI-style configuration file.
57          * 
58          * @access public
59          * @param string $file file localization
60          * @return array data of configuration file
61          */
62         public static function parseINIFile($file) {
63                 return file_exists($file) ? parse_ini_file($file, true) : array();
64         }
65
66
67         /**
68          * This method is copied from http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
69          */
70         public function objectToArray($data) {
71                 if (is_array($data) || is_object($data)) {
72                         $result = array();
73                         foreach ($data as $key => $value) {
74                                 $result[$key] = $this->objectToArray($value);
75                         }
76                         return $result;
77                 }
78                 return $data;
79         }
80
81         public function decode_bacula_lstat($lstat) {
82                 $base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
83                 $lstat = trim($lstat);
84                 $lstat_fields = explode(' ', $lstat);
85
86                 if(count($lstat_fields) !== 16) {
87                         die('Błąd! Niepoprawna ilość pól wartości LStat. Proszę upewnić się, że podany ciąg znaków jest poprawną wartością LStat');
88                 }
89
90                 list($dev, $inode, $mode, $nlink, $uid, $gid, $rdev, $size, $blocksize, $blocks, $atime, $mtime, $ctime, $linkfi, $flags, $data) = $lstat_fields;
91                 $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);
92
93                 $ret = array();
94                 foreach($encoded_values as $key => $val) {
95                         $result = 0;
96                         $is_minus = false;
97                         $start = 0;
98
99                         if(substr($val, 0, 1) === '-') {
100                                 $is_minus = true;
101                                 $start++;
102                         }
103
104                         for($i = $start; $i < strlen($val); $i++) {
105                                 $result = bcmul($result, bcpow(2,6));
106                                 $result +=  strpos($base64, substr($val, $i , 1));
107                         }
108                         $ret[$key] = ($is_minus === true) ? -$result : $result;
109                 }
110                 return $ret;
111         }
112
113         public function parseBvfsList($list) {
114                 $elements = array();
115                 for($i = 0; $i < count($list); $i++) {
116                         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) {
117                                 if($match['name'] == '.') {
118                                         continue;
119                                 } elseif($match['name'] != '..') {
120                                         $match['name'] .= '/';
121                                 }
122                                 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'dir');
123                         } 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) {
124                                 if($match['name'] == '.') {
125                                         continue;
126                                 }
127                                 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'file'); 
128                         }
129                 }
130                 return $elements;
131         }
132
133         public function parseFileVersions($filename, $list) {
134                 $elements = array();
135                 for($i = 0; $i < count($list); $i++) {
136                         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) {
137                                 $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');
138                         }
139                 }
140                 return $elements;
141         }
142 }
143 ?>