PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/packages/PHP/Beautifier/StreamWrapper/Tarz.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 196 lines | 148 code | 4 blank | 44 comment | 22 complexity | 955f3b14d10d683d13e2a6fb48639d7c MD5 | raw file
  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. case SEEK_CUR:
  125. if ($iOffset >= 0) {
  126. $this->iSeek+= $iOffset;
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. break;
  132. case SEEK_END:
  133. if (strlen($this->sText) +$iOffset >= 0) {
  134. $this->iSeek = strlen($this->sText) +$iOffset;
  135. return true;
  136. } else {
  137. return false;
  138. }
  139. break;
  140. default:
  141. return false;
  142. }
  143. }
  144. function stream_flush()
  145. {
  146. }
  147. function stream_stat()
  148. {
  149. }
  150. function unlink($sPath)
  151. {
  152. }
  153. function dir_opendir($sPath, $iOptions)
  154. {
  155. if ($this->getTar($sPath)) {
  156. array_walk($this->oTar->listContent() , array(
  157. $this,
  158. 'getFileList'
  159. ));
  160. return true;
  161. } else {
  162. return false;
  163. }
  164. }
  165. function dir_readdir()
  166. {
  167. if ($this->iSeekDir >= count($this->aContents)) {
  168. return false;
  169. } else {
  170. return $this->aContents[$this->iSeekDir++];
  171. }
  172. }
  173. function dir_rewinddir()
  174. {
  175. $this->iSeekDir = 0;
  176. }
  177. function dir_closedir()
  178. {
  179. //unset($this->oTar, $this->aContents, $this->sPath, $this->iSeekDir);
  180. //return true;
  181. }
  182. function getFileList($aInput)
  183. {
  184. $this->aContents[] = $aInput['filename'];
  185. }
  186. function tarFileExists($aInput)
  187. {
  188. return ($aInput['filename'] == $this->sPath and empty($aInput['typeflag']));
  189. }
  190. }
  191. stream_wrapper_register("tarz", "PHP_Beautifier_StreamWrapper_Tarz");
  192. ?>