/wp-content/plugins/groupdocs-viewer/tree_viewer/lib/groupdocs-php/FileStream.php

https://bitbucket.org/rchlmrtn/chiari · PHP · 144 lines · 107 code · 18 blank · 19 comment · 37 complexity · 3a12f0e9dc2406ddafa59c1c00c24090 MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright 2012 GroupDocs.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. class FileStream {
  18. private $fileName = null;
  19. private $contentType = null;
  20. private $size = null;
  21. private $inputStream = null;
  22. private function __construct($inputFilePath=null, $downloadDirectory=null){
  23. $this->filePath = $inputFilePath;
  24. $this->downloadDirectory = $downloadDirectory;
  25. }
  26. public static function fromFile($inFilePath){
  27. if(!file_exists($inFilePath)){
  28. throw new Exception("File $inFilePath doesn't exist.");
  29. }
  30. return new self($inFilePath);
  31. }
  32. /**
  33. * by default filename is derived from content-disposition header or url
  34. */
  35. public static function fromHttp($downloadDirectory, $outFileName=null){
  36. if(!file_exists($downloadDirectory)){
  37. throw new Exception("Directory $downloadDirectory doesn't exist.");
  38. }
  39. $instance = new self(null, $downloadDirectory);
  40. $instance->fileName = $outFileName;
  41. $instance->requestUrl = null;
  42. return $instance;
  43. }
  44. public function getFileName(){
  45. if ($this->fileName == null and $this->filePath != null){
  46. $this->fileName = basename($this->filePath);
  47. } else if ($this->fileName == null and $this->downloadDirectory != null){
  48. // guess from url
  49. $this->fileName = basename(parse_url($this->requestUrl, PHP_URL_PATH));
  50. }
  51. return $this->fileName;
  52. }
  53. public function getContentType(){
  54. if ($this->contentType == null and $this->filePath != null){
  55. $this->contentType = APIClient::getMimeType($this->filePath);
  56. }
  57. return $this->contentType;
  58. }
  59. public function getSize(){
  60. if ($this->size == null and $this->filePath != null){
  61. $this->size = filesize($this->filePath);
  62. }
  63. return $this->size;
  64. }
  65. public function getInputStream(){
  66. if ($this->inputStream == null and $this->filePath != null){
  67. $this->inputStream = fopen($this->filePath, "rb");
  68. } else if ($this->inputStream == null and $this->downloadDirectory != null){
  69. $outFilePath = $this->downloadDirectory."/".$this->getFileName();
  70. $this->inputStream = fopen($outFilePath, "wb");
  71. }
  72. return $this->inputStream;
  73. }
  74. public function headerCallback($ch, $string){
  75. // this method is called multiple times
  76. $len = strlen($string);
  77. if( !strstr($string, ':') ) {
  78. return $len;
  79. }
  80. list($name, $value) = explode(':', $string, 2);
  81. if($this->fileName == null){
  82. if( strcasecmp($name, 'Content-Disposition') == 0) {
  83. $parts = explode(';', $value);
  84. if( count($parts) > 1 ) {
  85. foreach($parts AS $crumb) {
  86. if( strstr($crumb, '=') ) {
  87. list($pname, $pval) = explode('=', $crumb);
  88. $pname = trim($pname);
  89. if( strcasecmp($pname, 'filename') == 0 ) {
  90. // Using basename to prevent path injection in malicious headers.
  91. $this->fileName = basename($this->unquote(trim($pval)));
  92. }
  93. }
  94. }
  95. }
  96. } else if($this->requestUrl == null){
  97. $this->requestUrl = $this->getCurlInfo($ch, CURLINFO_EFFECTIVE_URL);
  98. }
  99. }
  100. $this->headers[$name] = trim($value);
  101. if( strcasecmp($name, 'Content-Type') == 0) {
  102. $this->contentType = $this->headers[$name];
  103. }
  104. return $len;
  105. }
  106. public function bodyCallback($ch, $string) {
  107. // this method is called multiple times
  108. $len = null;
  109. if(!empty($this->headers['jsonerror'])){
  110. $this->jsonError = $string;
  111. $len = strlen($string);
  112. } else {
  113. $len = fwrite($this->getInputStream(), $string);
  114. }
  115. $this->size += $len;
  116. return $len;
  117. }
  118. public function getCurlInfo($ch, $name){
  119. return curl_getinfo($ch, $name);
  120. }
  121. private function unquote($string) {
  122. return str_replace(array("'", '"'), '', $string);
  123. }
  124. }