commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / ezc / Mail / src / parts / fileparts / virtual_file.php
1 <?php
2 /**
3 * File containing the ezcMailVirtualFile class
4 *
5 * @package Mail
6 * @version 1.7beta1
7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
8 * @license http://ez.no/licenses/new_bsd New BSD License
9 */
10
11 /**
12 * Mail part for binary data in memory.
13 *
14 * @property string $contents
15 * The contents to be added as an attachment. The mimeType and
16 * contentType are set in the constructor or if not specified they
17 * are extracted with the fileinfo extension if it is available,
18 * otherwise they are set to application/octet-stream.
19 *
20 * @package Mail
21 * @version 1.7beta1
22 */
23 class ezcMailVirtualFile extends ezcMailFilePart
24 {
25 /**
26 * Constructs a new attachment with $fileName and $contents.
27 *
28 * If the $mimeType and $contentType are not specified they are extracted
29 * with the fileinfo extension if it is available, otherwise they are set
30 * to application/octet-stream.
31 *
32 * @param string $fileName
33 * @param string $contents
34 * @param string $contentType
35 * @param string $mimeType
36 */
37 public function __construct( $fileName, $contents, $contentType = null, $mimeType = null )
38 {
39 parent::__construct( $fileName );
40 $this->contents = $contents;
41
42 if ( $contentType != null && $mimeType != null )
43 {
44 $this->contentType = $contentType;
45 $this->mimeType = $mimeType;
46 }
47 elseif ( ezcBaseFeatures::hasExtensionSupport( 'fileinfo' ) )
48 {
49 // get mime and content type
50 $fileInfo = new finfo( FILEINFO_MIME );
51 $mimeParts = $fileInfo->buffer( $contents );
52 if ( $mimeParts !== false && strpos( $mimeParts, '/' ) !== false )
53 {
54 list( $this->contentType, $this->mimeType ) = explode( '/', $mimeParts );
55 }
56 else
57 {
58 // default to mimetype application/octet-stream
59 $this->contentType = self::CONTENT_TYPE_APPLICATION;
60 $this->mimeType = "octet-stream";
61 }
62 }
63 else
64 {
65 // default to mimetype application/octet-stream
66 $this->contentType = self::CONTENT_TYPE_APPLICATION;
67 $this->mimeType = "octet-stream";
68 }
69 }
70
71 /**
72 * Sets the property $name to $value.
73 *
74 * @throws ezcBasePropertyNotFoundException
75 * if the property does not exist.
76 * @param string $name
77 * @param mixed $value
78 * @ignore
79 */
80 public function __set( $name, $value )
81 {
82 switch ( $name )
83 {
84 case 'contents':
85 $this->properties[$name] = $value;
86 break;
87 default:
88 return parent::__set( $name, $value );
89 break;
90 }
91 }
92
93 /**
94 * Returns the value of property $value.
95 *
96 * @throws ezcBasePropertyNotFoundException
97 * if the property does not exist.
98 * @param string $name
99 * @return mixed
100 * @ignore
101 */
102 public function __get( $name )
103 {
104 switch ( $name )
105 {
106 case 'contents':
107 return $this->properties[$name];
108 break;
109 default:
110 return parent::__get( $name );
111 break;
112 }
113 }
114
115 /**
116 * Returns true if the property $name is set, otherwise false.
117 *
118 * @param string $name
119 * @return bool
120 * @ignore
121 */
122 public function __isset( $name )
123 {
124 switch ( $name )
125 {
126 case 'contents':
127 return isset( $this->properties[$name] );
128
129 default:
130 return parent::__isset( $name );
131 }
132 }
133
134 /**
135 * Returns the contents of the file with the correct encoding.
136 *
137 * @return string
138 */
139 public function generateBody()
140 {
141 return chunk_split( base64_encode( $this->contents ), 76, ezcMailTools::lineBreak() );
142 }
143 }
144 ?>