source: temp/test-xoops.ec-cube.net/html/class/tardownloader.php @ 405

Revision 405, 5.8 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: tardownloader.php,v 1.2 2005/03/18 12:51:55 onokazu Exp $
3//  ------------------------------------------------------------------------ //
4//                XOOPS - PHP Content Management System                      //
5//                    Copyright (c) 2000 XOOPS.org                           //
6//                       <http://www.xoops.org/>                             //
7//  ------------------------------------------------------------------------ //
8//  This program is free software; you can redistribute it and/or modify     //
9//  it under the terms of the GNU General Public License as published by     //
10//  the Free Software Foundation; either version 2 of the License, or        //
11//  (at your option) any later version.                                      //
12//                                                                           //
13//  You may not change or alter any portion of this comment or credits       //
14//  of supporting developers from this source code or any supporting         //
15//  source code which is considered copyrighted (c) material of the          //
16//  original comment or credit authors.                                      //
17//                                                                           //
18//  This program is distributed in the hope that it will be useful,          //
19//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
20//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
21//  GNU General Public License for more details.                             //
22//                                                                           //
23//  You should have received a copy of the GNU General Public License        //
24//  along with this program; if not, write to the Free Software              //
25//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
26//  ------------------------------------------------------------------------ //
27// Author: Kazumi Ono (AKA onokazu)                                          //
28// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29// Project: The XOOPS Project                                                //
30// ------------------------------------------------------------------------- //
31if (!defined('XOOPS_ROOT_PATH')) {
32    exit();
33}
34
35/**
36 * base class
37 */
38include_once XOOPS_ROOT_PATH.'/class/downloader.php';
39/**
40 * Class to handle tar files
41 */
42include_once XOOPS_ROOT_PATH.'/class/class.tar.php';
43
44/**
45 * Send tar files through a http socket
46 *
47 * @package     kernel
48 * @subpackage  core
49 *
50 * @author      Kazumi Ono  <[email protected]>
51 * @copyright   (c) 2000-2003 The Xoops Project - www.xoops.org
52 */
53class XoopsTarDownloader extends XoopsDownloader
54{
55
56    /**
57     * Constructor
58     *
59     * @param string $ext       file extension
60     * @param string $mimyType  Mimetype
61     **/
62    function XoopsTarDownloader($ext = '.tar.gz', $mimyType = 'application/x-gzip')
63    {
64        $this->archiver = new tar();
65        $this->ext = trim($ext);
66        $this->mimeType = trim($mimyType);
67    }
68
69    /**
70     * Add a file to the archive
71     *
72     * @param   string  $filepath       Full path to the file
73     * @param   string  $newfilename    Filename (if you don't want to use the original)
74     **/
75    function addFile($filepath, $newfilename=null)
76    {
77        $this->archiver->addFile($filepath);
78        if (isset($newfilename)) {
79            // dirty, but no other way
80            for ($i = 0; $i < $this->archiver->numFiles; $i++) {
81                if ($this->archiver->files[$i]['name'] == $filepath) {
82                    $this->archiver->files[$i]['name'] = trim($newfilename);
83                    break;
84                }
85            }
86        }
87    }
88
89    /**
90     * Add a binary file to the archive
91     *
92     * @param   string  $filepath       Full path to the file
93     * @param   string  $newfilename    Filename (if you don't want to use the original)
94     **/
95    function addBinaryFile($filepath, $newfilename=null)
96    {
97        $this->archiver->addFile($filepath, true);
98        if (isset($newfilename)) {
99            // dirty, but no other way
100            for ($i = 0; $i < $this->archiver->numFiles; $i++) {
101                if ($this->archiver->files[$i]['name'] == $filepath) {
102                    $this->archiver->files[$i]['name'] = trim($newfilename);
103                    break;
104                }
105            }
106        }
107    }
108
109    /**
110     * Add a dummy file to the archive
111     *
112     * @param   string  $data       Data to write
113     * @param   string  $filename   Name for the file in the archive
114     * @param   integer $time
115     **/
116    function addFileData(&$data, $filename, $time=0)
117    {
118        $dummyfile = XOOPS_CACHE_PATH.'/dummy_'.time().'.html';
119        $fp = fopen($dummyfile, 'w');
120        fwrite($fp, $data);
121        fclose($fp);
122        $this->archiver->addFile($dummyfile);
123        unlink($dummyfile);
124
125        // dirty, but no other way
126        for ($i = 0; $i < $this->archiver->numFiles; $i++) {
127            if ($this->archiver->files[$i]['name'] == $dummyfile) {
128                $this->archiver->files[$i]['name'] = $filename;
129                if ($time != 0) {
130                    $this->archiver->files[$i]['time'] = $time;
131                }
132                break;
133            }
134        }
135    }
136
137    /**
138     * Add a binary dummy file to the archive
139     *
140     * @param   string  $data   Data to write
141     * @param   string  $filename   Name for the file in the archive
142     * @param   integer $time
143     **/
144    function addBinaryFileData(&$data, $filename, $time=0)
145    {
146        $dummyfile = XOOPS_CACHE_PATH.'/dummy_'.time().'.html';
147        $fp = fopen($dummyfile, 'wb');
148        fwrite($fp, $data);
149        fclose($fp);
150        $this->archiver->addFile($dummyfile, true);
151        unlink($dummyfile);
152
153        // dirty, but no other way
154        for ($i = 0; $i < $this->archiver->numFiles; $i++) {
155            if ($this->archiver->files[$i]['name'] == $dummyfile) {
156                $this->archiver->files[$i]['name'] = $filename;
157                if ($time != 0) {
158                    $this->archiver->files[$i]['time'] = $time;
159                }
160                break;
161            }
162        }
163    }
164
165    /**
166     * Send the file to the client
167     *
168     * @param   string  $name   Filename
169     * @param   boolean $gzip   Use GZ compression
170     **/
171    function download($name, $gzip = true)
172    {
173        $this->_header($name.$this->ext);
174        echo $this->archiver->toTarOutput($name.$this->ext, $gzip);
175    }
176}
177?>
Note: See TracBrowser for help on using the repository browser.