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