[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class AphrontFileResponse extends AphrontResponse { 4 5 private $content; 6 private $mimeType; 7 private $download; 8 private $rangeMin; 9 private $rangeMax; 10 private $allowOrigins = array(); 11 12 public function addAllowOrigin($origin) { 13 $this->allowOrigins[] = $origin; 14 return $this; 15 } 16 17 public function setDownload($download) { 18 $download = preg_replace('/[^A-Za-z0-9_.-]/', '_', $download); 19 if (!strlen($download)) { 20 $download = 'untitled_document.txt'; 21 } 22 $this->download = $download; 23 return $this; 24 } 25 26 public function getDownload() { 27 return $this->download; 28 } 29 30 public function setMimeType($mime_type) { 31 $this->mimeType = $mime_type; 32 return $this; 33 } 34 35 public function getMimeType() { 36 return $this->mimeType; 37 } 38 39 public function setContent($content) { 40 $this->content = $content; 41 return $this; 42 } 43 44 public function buildResponseString() { 45 if ($this->rangeMin || $this->rangeMax) { 46 $length = ($this->rangeMax - $this->rangeMin) + 1; 47 return substr($this->content, $this->rangeMin, $length); 48 } else { 49 return $this->content; 50 } 51 } 52 53 public function setRange($min, $max) { 54 $this->rangeMin = $min; 55 $this->rangeMax = $max; 56 return $this; 57 } 58 59 public function getHeaders() { 60 $headers = array( 61 array('Content-Type', $this->getMimeType()), 62 array('Content-Length', strlen($this->buildResponseString())), 63 ); 64 65 if ($this->rangeMin || $this->rangeMax) { 66 $len = strlen($this->content); 67 $min = $this->rangeMin; 68 $max = $this->rangeMax; 69 $headers[] = array('Content-Range', "bytes {$min}-{$max}/{$len}"); 70 } 71 72 if (strlen($this->getDownload())) { 73 $headers[] = array('X-Download-Options', 'noopen'); 74 75 $filename = $this->getDownload(); 76 $headers[] = array( 77 'Content-Disposition', 78 'attachment; filename='.$filename, 79 ); 80 } 81 82 if ($this->allowOrigins) { 83 $headers[] = array( 84 'Access-Control-Allow-Origin', 85 implode(',', $this->allowOrigins), 86 ); 87 } 88 89 $headers = array_merge(parent::getHeaders(), $headers); 90 return $headers; 91 } 92 93 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |