3 class Miscellaneous extends TModule {
5 const LICENCE_FILE = 'LICENSE';
7 private $jobLevels = array('F' => 'Full', 'I' => 'Incremental', 'D'=> 'Differential', 'B' => 'Base', 'f' => 'VirtualFull');
10 * Getting the licence from file.
13 * @return string licence text
15 public function getLicence() {
16 return nl2br(htmlspecialchars(file_get_contents(self::LICENCE_FILE)));
19 public function getJobLevels() {
20 return $this->jobLevels;
23 public function isValidJobLevel($jobLevel) {
24 return array_key_exists($jobLevel, $this->getJobLevels());
28 * Writing INI-style configuration file.
30 * Functions has been got from StackOverflow.com service (http://stackoverflow.com/questions/4082626/save-ini-file-with-comments).
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
37 public function writeINIFile($file, array $options){
39 foreach($options as $section => $values){
40 $tmp .= "[$section]\n";
41 foreach($values as $key => $val){
43 foreach($val as $k =>$v){
44 $tmp .= "{$key}[$k] = \"$v\"\n";
47 $tmp .= "$key = \"$val\"\n";
52 return file_put_contents($file, $tmp);
56 * Parse INI-style configuration file.
59 * @param string $file file localization
60 * @return array data of configuration file
62 public static function parseINIFile($file) {
63 return file_exists($file) ? parse_ini_file($file, true) : array();
68 * This method is copied from http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
70 public function objectToArray($data) {
71 if (is_array($data) || is_object($data)) {
73 foreach ($data as $key => $value) {
74 $result[$key] = $this->objectToArray($value);
81 public function decode_bacula_lstat($lstat) {
82 $base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
83 $lstat = trim($lstat);
84 $lstat_fields = explode(' ', $lstat);
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');
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);
94 foreach($encoded_values as $key => $val) {
99 if(substr($val, 0, 1) === '-') {
104 for($i = $start; $i < strlen($val); $i++) {
105 $result = bcmul($result, bcpow(2,6));
106 $result += strpos($base64, substr($val, $i , 1));
108 $ret[$key] = ($is_minus === true) ? -$result : $result;
113 public function parseBvfsList($list) {
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'] == '.') {
119 } elseif($match['name'] != '..') {
120 $match['name'] .= '/';
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'] == '.') {
127 $elements[] = array('pathid' => $match['pathid'], 'filenameid' => $match['filenameid'], 'fileid' => $match['fileid'], 'jobid' => $match['jobid'], 'lstat' => $match['lstat'], 'name' => $match['name'], 'type' => 'file');
133 public function parseFileVersions($filename, $list) {
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');