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.
8 function sortFilesListByName($a, $b) {
9 $firstLeft = substr($a['name'], 0, 1);
10 $firstRight = substr($b['name'], 0, 1);
11 if ($firstLeft == '.' && $firstRight != '.') {
13 } else if ($firstRight == '.' && $firstLeft != '.') {
16 return strcasecmp($a['name'], $b['name']);
19 class Miscellaneous extends TModule {
21 const LICENCE_FILE = 'LICENSE';
23 private $jobTypes = array(
35 private $jobLevels = array(
38 'D' => 'Differential',
43 'O' => 'VolumeToCatalog',
44 'd' => 'DiskToCatalog'
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)')
75 private $runningJobStates = array('C', 'R');
78 * Getting the licence from file.
81 * @return string licence text
83 public function getLicence() {
84 return nl2br(htmlspecialchars(file_get_contents(self::LICENCE_FILE)));
87 public function getJobLevels() {
88 return $this->jobLevels;
91 public function getJobState($jobStateLetter = null) {
93 if(is_null($jobStateLetter)) {
94 $state = $this->jobStates;
96 $state = array_key_exists($jobStateLetter, $this->jobStates) ? $this->jobStates[$jobStateLetter] : null;
101 public function getRunningJobStates() {
102 return $this->runningJobStates;
106 public function getJobType($jobTypeLetter = null) {
108 if(is_null($jobTypeLetter)) {
109 $type = $this->jobTypes;
111 $type = array_key_exists($jobTypeLetter, $this->jobTypes) ? $this->jobTypes[$jobTypeLetter] : null;
117 public function isValidJobLevel($jobLevel) {
118 return array_key_exists($jobLevel, $this->getJobLevels());
122 * Writing INI-style configuration file.
124 * Functions has been got from StackOverflow.com service (http://stackoverflow.com/questions/4082626/save-ini-file-with-comments).
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
131 public function writeINIFile($file, array $options){
133 foreach($options as $section => $values){
134 $tmp .= "[$section]\n";
135 foreach($values as $key => $val){
137 foreach($val as $k =>$v){
138 $tmp .= "{$key}[$k] = \"$v\"\n";
141 $tmp .= "$key = \"$val\"\n";
146 return file_put_contents($file, $tmp);
150 * Parse INI-style configuration file.
153 * @param string $file file localization
154 * @return array data of configuration file
156 public static function parseINIFile($file) {
157 return file_exists($file) ? parse_ini_file($file, true) : array();
162 * This method is copied from http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
164 public function objectToArray($data) {
165 if (is_array($data) || is_object($data)) {
167 foreach ($data as $key => $value) {
168 $result[$key] = $this->objectToArray($value);
175 public function decode_bacula_lstat($lstat) {
176 $base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
177 $lstat = trim($lstat);
178 $lstat_fields = explode(' ', $lstat);
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');
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);
188 foreach($encoded_values as $key => $val) {
193 if(substr($val, 0, 1) === '-') {
198 for($i = $start; $i < strlen($val); $i++) {
199 $result = bcmul($result, bcpow(2,6));
200 $result += strpos($base64, substr($val, $i , 1));
202 $ret[$key] = ($is_minus === true) ? -$result : $result;
207 public function parseBvfsList($list) {
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'] == '.') {
213 } elseif($match['name'] != '..') {
214 $match['name'] .= '/';
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'] == '.') {
221 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'file');
224 usort($elements, 'sortFilesListByName');
228 public function parseFileVersions($filename, $list) {
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');