1: <?php
2: /**
3: * Copyright (C) Apis Networks, Inc - All Rights Reserved.
4: *
5: * Unauthorized copying of this file, via any medium, is
6: * strictly prohibited without consent. Any dissemination of
7: * material herein is prohibited.
8: *
9: * For licensing inquiries email <licensing@apisnetworks.com>
10: *
11: * Written by Matt Saladna <matt@apisnetworks.com>, May 2017
12: */
13:
14: /**
15: * Provides tar compression/decompression routines in the file manager
16: *
17: * @package Compression
18: */
19: class Tar_Filter extends Archive_Base
20: {
21: protected static function extract_files_pipe($process, $destination, array $files = null, array $opts = null)
22: {
23: //$process->setEncoding('binary');
24: return self::extract_files($process,
25: $destination,
26: $files,
27: array('pipe' => 1/*$process->start()*/));
28: }
29:
30: public static function extract_files($archive, $destination, array $file = null, array $opts = null)
31: {
32: if (isset($opts['pipe']) && false !== strpos($archive, '.gz')) {
33: $cmd = 'tar -xzvf %s -C %s';
34: } else {
35: if (isset($opts['pipe']) && false !== strpos($archive, '.bz')) {
36: $cmd = 'tar -xjvf %s -C %s';
37: } else {
38: $cmd = 'tar -xvf %s -C %s';
39: }
40: }
41: $proc = parent::exec(
42: $cmd,
43: $archive,
44: $destination,
45: array(0),
46: array('run' => 1/* @XXX pipe broken for now */)
47: );
48: if (0 & isset($opts['pipe'])) {
49: $proc->pipeProg($opts['pipe']);
50: }
51:
52: return $proc;
53: }
54:
55: protected static function list_files_pipe($process, array $opts = null)
56: {
57:
58: /*$proc = self::$fc->proc_broker (
59: "echo 'Hello World!'",
60: array(0),
61: array('run' => 0)
62: );
63: $proc2 = self::$fc->proc_broker(
64: '(cat - /tmp/test ; sleep 5 ;echo "hooray!")',
65: array(0),
66: array('run' => 0)
67: );
68: $proc2->pipeProg($proc->start());
69: $proc2->setOption('run',1)->setOption('binary',1);
70: var_dump($proc2->run());*/
71: //$process->setEncoding('binary');
72: return self::list_files($process, array('pipe' => 1/*$process->start()*/));
73: }
74:
75: public static function list_files($archive, array $opts = null)
76: {
77: $tar = false;
78: if (isset($opts['pipe']) && false !== strpos($archive, '.gz') || false !== strpos($archive, '.tgz')) {
79: $cmd = 'tar -tzvf %s';
80: } else {
81: if (isset($opts['pipe']) && false !== strpos($archive, '.bz') || false !== strpos($archive, '.tbz')) {
82: $cmd = 'tar -tjvf %s';
83: } else {
84: $cmd = 'tar -tvf %s';
85: }
86: }
87: $proc = parent::exec(
88: $cmd,
89: $archive,
90: array(0),
91: array('run' => 1));
92:
93:
94: if (0 & isset($opts['pipe'])) {
95: $proc->pipeProg($opts['pipe']);
96: }
97: $proc = explode("\n", $proc['output']);
98: $files = array();
99:
100: foreach ($proc as $line) {
101: if (!preg_match(Regex::FILE_HDR_TAR, $line, $entry)) {
102: continue;
103: }
104: //list ($null, $file_name, $size, $packed_size, $date, $attr, $crc) = $matches;
105: $file_type = $entry['permissions'][0] == 'd' ? 'dir' : 'file';
106: $size = $file_type == 'dir' ? 4096 : $entry['size'];
107:
108: $files[] = array(
109: 'file_name' => $entry['name'],
110: 'file_type' => $file_type,
111: 'can_read' => true,
112: 'can_write' => true,
113: 'can_execute' => true,
114: 'size' => $size,
115: 'packed_size' => $size,
116: 'crc' => '?',
117: 'link' => 0,
118: 'date' => strtotime($entry['ts'])
119: );
120: }
121:
122: return $files;
123:
124: }
125:
126: }
127:
128: ?>
129: