commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / PHP / Beautifier / StreamWrapper / Tarz.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3 /**
4 * Custom stream to handle Tar files (compressed and uncompressed)
5 *
6 * PHP version 5
7 *
8 * LICENSE: This source file is subject to version 3.0 of the PHP license
9 * that is available through the world-wide-web at the following URI:
10 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
11 * the PHP License and are unable to obtain it through the web, please
12 * send a note to license@php.net so we can mail you a copy immediately.
13 * @category PHP
14 * @package PHP_Beautifier
15 * @subpackage StreamWrapper
16 * @author Claudio Bustos <cdx@users.sourceforge.com>
17 * @copyright 2004-2006 Claudio Bustos
18 * @link http://pear.php.net/package/PHP_Beautifier
19 * @link http://beautifyphp.sourceforge.net
20 * @license http://www.php.net/license/3_0.txt PHP License 3.0
21 * @version CVS: $Id:$
22 */
23 /**
24 * Require Archive_Tar
25 */
26 require_once 'Archive/Tar.php';
27 /**
28 * Custom stream to handle Tar files (compressed and uncompressed)
29 * Use URL tarz://myfile.tgz#myfile.php
30 *
31 * @category PHP
32 * @package PHP_Beautifier
33 * @subpackage StreamWrapper
34 * @author Claudio Bustos <cdx@users.sourceforge.com>
35 * @copyright 2004-2006 Claudio Bustos
36 * @link http://pear.php.net/package/PHP_Beautifier
37 * @link http://beautifyphp.sourceforge.net
38 * @license http://www.php.net/license/3_0.txt PHP License 3.0
39 * @version Release: 0.1.14
40 */
41 class PHP_Beautifier_StreamWrapper_Tarz implements PHP_Beautifier_StreamWrapper_Interface {
42 public $oTar;
43 public $sTar;
44 public $sPath;
45 public $sText;
46 public $iSeek = 0;
47 public $iSeekDir = 0;
48 public $aContents = array();
49 function stream_open($sPath, $sMode, $iOptions, &$sOpenedPath)
50 {
51 if ($this->getTar($sPath)) {
52 //ArrayNested->off()
53 $aContents = $this->oTar->listContent();
54 if (array_filter($aContents, array($this, 'tarFileExists'))) {
55 $this->sText = $this->oTar->extractInString($this->sPath);
56 return true;
57 }
58 }
59 //ArrayNested->on()
60 return false;
61 }
62 function getTar($sPath)
63 {
64 if (preg_match("/tarz:\/\/(.*?(\.tgz|\.tar\.gz|\.tar\.bz2|\.tar)+)(?:#\/?(.*))*/", $sPath, $aMatch)) {
65 $this->sTar = $aMatch[1];
66 if (strpos($aMatch[2], 'gz') !== FALSE) {
67 $sCompress = 'gz';
68 } elseif (strpos($aMatch[2], 'bz2') !== FALSE) {
69 $sCompress = 'bz2';
70 } elseif (strpos($aMatch[2], 'tar') !== FALSE) {
71 $sCompress = false;
72 } else {
73 return false;
74 }
75 if (isset($aMatch[3])) {
76 $this->sPath = $aMatch[3];
77 }
78 if (file_exists($this->sTar)) {
79 $this->oTar = new Archive_Tar($this->sTar, $sCompress);
80 return true;
81 }
82 } else {
83 return false;
84 }
85 }
86 function stream_close()
87 {
88 unset($this->oTar, $this->sText, $this->sPath, $this->iSeek);
89 }
90 function stream_read($iCount)
91 {
92 $sRet = substr($this->sText, $this->iSeek, $iCount);
93 $this->iSeek+= strlen($sRet);
94 return $sRet;
95 }
96 function stream_write($sData)
97 {
98 }
99 function stream_eof()
100 {
101 // BUG in 5.0.0RC1<PHP<5.0.0.0RC4
102 // DON'T USE EOF. Use ... another option :P
103 if (version_compare(PHP_VERSION, '5.0.0.RC.1', ">=") and version_compare(PHP_VERSION, '5.0.0.RC.4', "<")) {
104 return !($this->iSeek >= strlen($this->sText));
105 } else {
106 return $this->iSeek >= strlen($this->sText);
107 }
108 }
109 function stream_tell()
110 {
111 return $this->iSeek;
112 }
113 function stream_seek($iOffset, $iWhence)
114 {
115 switch ($iWhence) {
116 case SEEK_SET:
117 if ($iOffset<strlen($this->sText) and $iOffset >= 0) {
118 $this->iSeek = $iOffset;
119 return true;
120 } else {
121 return false;
122 }
123 break;
124
125 case SEEK_CUR:
126 if ($iOffset >= 0) {
127 $this->iSeek+= $iOffset;
128 return true;
129 } else {
130 return false;
131 }
132 break;
133
134 case SEEK_END:
135 if (strlen($this->sText) +$iOffset >= 0) {
136 $this->iSeek = strlen($this->sText) +$iOffset;
137 return true;
138 } else {
139 return false;
140 }
141 break;
142
143 default:
144 return false;
145 }
146 }
147 function stream_flush()
148 {
149 }
150 function stream_stat()
151 {
152 }
153 function unlink($sPath)
154 {
155 }
156 function dir_opendir($sPath, $iOptions)
157 {
158 if ($this->getTar($sPath)) {
159 array_walk($this->oTar->listContent() , array(
160 $this,
161 'getFileList'
162 ));
163 return true;
164 } else {
165 return false;
166 }
167 }
168 function dir_readdir()
169 {
170 if ($this->iSeekDir >= count($this->aContents)) {
171 return false;
172 } else {
173 return $this->aContents[$this->iSeekDir++];
174 }
175 }
176 function dir_rewinddir()
177 {
178 $this->iSeekDir = 0;
179 }
180 function dir_closedir()
181 {
182 //unset($this->oTar, $this->aContents, $this->sPath, $this->iSeekDir);
183 //return true;
184
185 }
186 function getFileList($aInput)
187 {
188 $this->aContents[] = $aInput['filename'];
189 }
190 function tarFileExists($aInput)
191 {
192 return ($aInput['filename'] == $this->sPath and empty($aInput['typeflag']));
193 }
194 }
195 stream_wrapper_register("tarz", "PHP_Beautifier_StreamWrapper_Tarz");
196 ?>