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 const RPATH_PATTERN = '/^b2\d+$/';
25 private $jobTypes = array(
38 private $jobLevels = array(
41 'D' => 'Differential',
46 'O' => 'VolumeToCatalog',
47 'd' => 'DiskToCatalog'
50 private $jobStates = array(
51 'C' => array('value' => 'Created', 'description' =>'Created but not yet running'),
52 'R' => array('value' => 'Running', 'description' => 'Running'),
53 'B' => array('value' => 'Blocked', 'description' => 'Blocked'),
54 'T' => array('value' => 'Terminated', 'description' =>'Terminated normally'),
55 'W' => array('value' => 'Terminated', 'description' =>'Terminated normally with warnings'),
56 'E' => array('value' => 'Error', 'description' =>'Terminated in Error'),
57 'e' => array('value' => 'Non-fatal error', 'description' =>'Non-fatal error'),
58 'f' => array('value' => 'Fatal error', 'description' =>'Fatal error'),
59 'D' => array('value' => 'Verify Diff.', 'description' =>'Verify Differences'),
60 'A' => array('value' => 'Canceled', 'description' =>'Canceled by the user'),
61 'I' => array('value' => 'Incomplete', 'description' =>'Incomplete Job'),
62 'F' => array('value' => 'Waiting on FD', 'description' =>'Waiting on the File daemon'),
63 'S' => array('value' => 'Waiting on SD', 'description' =>'Waiting on the Storage daemon'),
64 'm' => array('value' => 'Waiting for new vol.', 'description' =>'Waiting for a new Volume to be mounted'),
65 'M' => array('value' => 'Waiting for mount', 'description' =>'Waiting for a Mount'),
66 's' => array('value' => 'Waiting for storage', 'description' =>'Waiting for Storage resource'),
67 'j' => array('value' => 'Waiting for job', 'description' =>'Waiting for Job resource'),
68 'c' => array('value' => 'Waiting for client', 'description' =>'Waiting for Client resource'),
69 'd' => array('value' => 'Waiting for Max. jobs', 'description' =>'Wating for Maximum jobs'),
70 't' => array('value' => 'Waiting for start', 'description' =>'Waiting for Start Time'),
71 'p' => array('value' => 'Waiting for higher priority', 'description' =>'Waiting for higher priority job to finish'),
72 'i' => array('value' => 'Batch insert', 'description' =>'Doing batch insert file records'),
73 'a' => array('value' => 'Despooling attributes', 'description' =>'SD despooling attributes'),
74 'l' => array('value' => 'Data despooling', 'description' =>'Doing data despooling'),
75 'L' => array('value' => 'Commiting data', 'description' =>'Committing data (last despool)')
78 private $jobStatesOK = array('T', 'D');
79 private $jobStatesWarning = array('W');
80 private $jobStatesError = array('E', 'e', 'f', 'I');
81 private $jobStatesCancel = array('A');
82 private $jobStatesRunning = array('C', 'R', 'B', 'F', 'S', 'm', 'M', 's', 'j', 'c', 'd','t', 'p', 'i', 'a', 'l', 'L');
84 private $runningJobStates = array('C', 'R');
87 * Getting the licence from file.
90 * @return string licence text
92 public function getLicence() {
93 return nl2br(htmlspecialchars(file_get_contents(self::LICENCE_FILE)));
96 public function getJobLevels() {
97 return $this->jobLevels;
100 public function getJobState($jobStateLetter = null) {
102 if(is_null($jobStateLetter)) {
103 $state = $this->jobStates;
105 $state = array_key_exists($jobStateLetter, $this->jobStates) ? $this->jobStates[$jobStateLetter] : null;
110 public function getRunningJobStates() {
111 return $this->runningJobStates;
115 public function getJobType($jobTypeLetter = null) {
117 if(is_null($jobTypeLetter)) {
118 $type = $this->jobTypes;
120 $type = array_key_exists($jobTypeLetter, $this->jobTypes) ? $this->jobTypes[$jobTypeLetter] : null;
126 public function getJobStatesByType($type) {
127 $statesByType = array();
131 $states = $this->jobStatesOK;
134 $states = $this->jobStatesWarning;
137 $states = $this->jobStatesError;
140 $states = $this->jobStatesCancel;
143 $states = $this->jobStatesRunning;
147 for ($i = 0; $i < count($states); $i++) {
148 $statesByType[$states[$i]] = $this->getJobState($states[$i]);
151 return $statesByType;
154 public function isValidJobLevel($jobLevel) {
155 return array_key_exists($jobLevel, $this->getJobLevels());
159 * Writing INI-style configuration file.
161 * Functions has been got from StackOverflow.com service (http://stackoverflow.com/questions/4082626/save-ini-file-with-comments).
164 * @param string $file file localization
165 * @param array $options structure of config file params
166 * @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
168 public function writeINIFile($file, array $options){
170 foreach($options as $section => $values){
171 $tmp .= "[$section]\n";
172 foreach($values as $key => $val){
174 foreach($val as $k =>$v){
175 $tmp .= "{$key}[$k] = \"$v\"\n";
178 $tmp .= "$key = \"$val\"\n";
183 $old_umask = umask(0);
185 $result = file_put_contents($file, $tmp);
191 * Parse INI-style configuration file.
194 * @param string $file file localization
195 * @return array data of configuration file
197 public static function parseINIFile($file) {
198 return file_exists($file) ? parse_ini_file($file, true) : array();
203 * This method is copied from http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
205 public function objectToArray($data) {
206 if (is_array($data) || is_object($data)) {
208 foreach ($data as $key => $value) {
209 $result[$key] = $this->objectToArray($value);
216 public function decode_bacula_lstat($lstat) {
217 $base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
218 $lstat = trim($lstat);
219 $lstat_fields = explode(' ', $lstat);
221 if(count($lstat_fields) !== 16) {
222 die('Błąd! Niepoprawna ilość pól wartości LStat. Proszę upewnić się, że podany ciąg znaków jest poprawną wartością LStat');
225 list($dev, $inode, $mode, $nlink, $uid, $gid, $rdev, $size, $blocksize, $blocks, $atime, $mtime, $ctime, $linkfi, $flags, $data) = $lstat_fields;
226 $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);
229 foreach($encoded_values as $key => $val) {
234 if(substr($val, 0, 1) === '-') {
239 for($i = $start; $i < strlen($val); $i++) {
240 $result = bcmul($result, bcpow(2,6));
241 $result += strpos($base64, substr($val, $i , 1));
243 $ret[$key] = ($is_minus === true) ? -$result : $result;
248 public function parseBvfsList($list) {
250 for($i = 0; $i < count($list); $i++) {
251 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) {
252 if($match['name'] == '.') {
254 } elseif($match['name'] != '..') {
255 $match['name'] .= '/';
257 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'dir');
258 } 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) {
259 if($match['name'] == '.') {
262 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'file');
265 usort($elements, 'sortFilesListByName');
269 public function parseFileVersions($filename, $list) {
271 for($i = 0; $i < count($list); $i++) {
272 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) {
273 $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');
279 public function findJobIdStartedJob($output) {
281 $output = array_reverse($output); // jobid is ussually at the end of output
282 for ($i = 0; $i < count($output); $i++) {
283 if (preg_match('/^Job queued\.\sJobId=(?P<jobid>\d+)$/', $output[$i], $match) === 1) {
284 $jobid = $match['jobid'];