1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: /**
13: * Send tar files through a http socket
14: *
15: * @copyright XOOPS Project (http://xoops.org)
16: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17: * @package class
18: * @since 2.0.0
19: * @author Kazumi Ono (http://www.myweb.ne.jp/, http://jp.xoops.org/)
20: * @version $Id$
21: */
22:
23: class XoopsTarDownloader extends XoopsDownloader
24: {
25: /**
26: * Constructor
27: *
28: * @param string $ext file extension
29: * @param string $mimyType Mimetype
30: */
31: public function __construct($ext = '.tar.gz', $mimyType = 'application/x-gzip')
32: {
33: $this->archiver = new tar();
34: $this->ext = trim($ext);
35: $this->mimeType = trim($mimyType);
36: }
37:
38: /**
39: * Add a file to the archive
40: *
41: * @param string $filepath Full path to the file
42: * @param string $newfilename Filename (if you don't want to use the original)
43: *
44: * @return false|null
45: */
46: public function addFile($filepath, $newfilename = null)
47: {
48: $result = $this->archiver->addFile($filepath);
49: if ($result === false) {
50: return false;
51: }
52: if (isset($newfilename)) {
53: // dirty, but no other way
54: for ($i = 0; $i < $this->archiver->numFiles; ++$i) {
55: if ($this->archiver->files[$i]['name'] == $filepath) {
56: $this->archiver->files[$i]['name'] = trim($newfilename);
57: break;
58: }
59: }
60: }
61: }
62:
63: /**
64: * Add a binary file to the archive
65: *
66: * @param string $filepath Full path to the file
67: * @param string $newfilename Filename (if you don't want to use the original)
68: *
69: * @return false|null
70: */
71: public function addBinaryFile($filepath, $newfilename = null)
72: {
73: $result = $this->archiver->addFile($filepath, true);
74: if ($result === false) {
75: return false;
76: }
77: if (isset($newfilename)) {
78: // dirty, but no other way
79: for ($i = 0; $i < $this->archiver->numFiles; ++$i) {
80: if ($this->archiver->files[$i]['name'] == $filepath) {
81: $this->archiver->files[$i]['name'] = trim($newfilename);
82: break;
83: }
84: }
85: }
86: }
87:
88: /**
89: * Add a dummy file to the archive
90: *
91: * @param string &$data Data to write
92: * @param string $filename Name for the file in the archive
93: * @param integer $time time
94: *
95: * @return false|null
96: */
97: public function addFileData(&$data, $filename, $time = 0)
98: {
99: $dummyfile = \XoopsBaseConfig::get('caches-path') . '/dummy_' . time() . '.html';
100: $fp = @fopen($dummyfile, 'w');
101: if ($fp === false) {
102: return false;
103: }
104: fwrite($fp, $data);
105: fclose($fp);
106: $result = $this->archiver->addFile($dummyfile);
107: unlink($dummyfile);
108: if ($result === false) {
109: return false;
110: }
111: // dirty, but no other way
112: for ($i = 0; $i < $this->archiver->numFiles; ++$i) {
113: if ($this->archiver->files[$i]['name'] == $dummyfile) {
114: $this->archiver->files[$i]['name'] = $filename;
115: if ($time != 0) {
116: $this->archiver->files[$i]['time'] = $time;
117: }
118: break;
119: }
120: }
121: }
122:
123: /**
124: * Add a binary dummy file to the archive
125: *
126: * @param string &$data Data to write
127: * @param string $filename Name for the file in the archive
128: * @param integer $time time
129: *
130: * @return false|null
131: */
132: public function addBinaryFileData(&$data, $filename, $time = 0)
133: {
134: $dummyfile = \XoopsBaseConfig::get('caches-path') . '/dummy_' . time() . '.html';
135: $fp = @fopen($dummyfile, 'wb');
136: if ($fp === false) {
137: return false;
138: }
139: fwrite($fp, $data);
140: fclose($fp);
141: $result = $this->archiver->addFile($dummyfile, true);
142: unlink($dummyfile);
143: if ($result === false) {
144: return false;
145: }
146: // dirty, but no other way
147: for ($i = 0; $i < $this->archiver->numFiles; ++$i) {
148: if ($this->archiver->files[$i]['name'] == $dummyfile) {
149: $this->archiver->files[$i]['name'] = $filename;
150: if ($time != 0) {
151: $this->archiver->files[$i]['time'] = $time;
152: }
153: break;
154: }
155: }
156: }
157:
158: /**
159: * Send the file to the client
160: *
161: * @param string $name Filename
162: * @param boolean $gzip Use GZ compression
163: *
164: * @return void
165: */
166: public function download($name, $gzip = true)
167: {
168: $this->_header($name . $this->ext);
169: $str = $this->archiver->toTarOutput($name . $this->ext, $gzip);
170: if ($str !== false) {
171: echo $str;
172: }
173: }
174: }
175: