1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: Downloadable.php 866 2013-08-25 16:22:35Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\Downloadable class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The Scrivo Downloadable class is a simple conveniance class to pass 40 * downloadable file data from an action. 41 */ 42 class Downloadable { 43 44 /** 45 * Constant to denote that we pass the actual file content in a variable. 46 */ 47 const type_data = 1; 48 49 /** 50 * Constant to denote that we pass the actual file content in a file. 51 */ 52 const TYPE_FILE = 2; 53 54 /** 55 * The download source type: either type_data or TYPE_FILE 56 * @var int 57 */ 58 private $type; 59 60 /** 61 * The file name to use in the download. 62 * @var \Scrivo\String 63 */ 64 private $action; 65 66 /** 67 * The binary data to pass in case of type_data or the physical location 68 * of the file in case of TYPE_FILE 69 * @var string|\Scrivo\String 70 */ 71 private $data; 72 73 /** 74 * Construct a Downloadable object to pass from an action. 75 * 76 * For smaller files we can simply pass the file data as a variable 77 * (type_data), for larger files you can write the data to a temporary file 78 * and pass the file name (TYPE_FILE). 79 * 80 * @param \Scrivo\Context $context A valid Scrivo context. 81 * @param \Scrivo\String $action The file name to use in the 82 * download headers, this will be prepended with a string representation 83 * of the WWW_ROOT variabele. 84 * @param int $type the type of data to download, either type_data or 85 * TYPE_FILE. 86 * @param string|\Scrivo\String $data The file data (type_data), or 87 * file name (TYPE_FILE). 88 */ 89 public function __construct(\Scrivo\Context $context, 90 \Scrivo\String $action, $type, $data) { 91 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 92 null, 93 null, 94 array(\Scrivo\ArgumentCheck::TYPE_INTEGER, 95 array(self::type_data, self::TYPE_FILE)), 96 array(array(\Scrivo\ArgumentCheck::TYPE_STRING, "Scrivo\String")) 97 )); 98 if ($type == self::TYPE_FILE) { 99 if (!($data instanceof \Scrivo\String)) { 100 throw new \Scrivo\SystemException("Invalid argument type"); 101 } 102 } else { 103 \Scrivo\ArgumentCheck::assert( 104 $data, \Scrivo\ArgumentCheck::TYPE_STRING); 105 } 106 // Get a 'filename save' representation of the WWW_ROOT variable. 107 $wr = $context->config->WWW_ROOT->replace( 108 \Scrivo\String::create(array("http://", "https://")), 109 new \Scrivo\String(""))->replace( 110 new \Scrivo\String("/"), new \Scrivo\String("_")); 111 112 $this->action = new \Scrivo\String($wr . "_" . $action); 113 $this->type = $type; 114 $this->data = $data; 115 } 116 117 /** 118 * Output the file data to stdout. If the file data was read from a 119 * temporary file, the file will be deleted afterwards. 120 */ 121 public function outputData() { 122 if ($this->type == self::TYPE_FILE) { 123 readfile($this->data); 124 unlink($this->data); 125 } else if ($this->type == self::type_data) { 126 echo $this->data; 127 } 128 } 129 130 /** 131 * Get the file name to use in the download headers. 132 */ 133 public function getFileName() { 134 return $this->action; 135 136 } 137 138 } 139 140 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013