/frameworks/solar/1.1.1/source/solar/Solar/Mail/Message/Part.php

https://github.com/ggunlugu/ornekler · PHP · 436 lines · 139 code · 39 blank · 258 comment · 6 complexity · 8ba7c414c8ed72e289dc5cf1bb6138ad MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Represents one MIME part of a Solar_Mail_Message.
  5. *
  6. * Refactored and modified from Zend_Mail_Message and related classes.
  7. *
  8. * @category Solar
  9. *
  10. * @package Solar_Mail
  11. *
  12. * @author Paul M. Jones <pmjones@solarphp.com>
  13. *
  14. * @license http://opensource.org/licenses/bsd-license.php BSD
  15. *
  16. * @version $Id: Part.php 4416 2010-02-23 19:52:43Z pmjones $
  17. *
  18. */
  19. class Solar_Mail_Message_Part extends Solar_Base
  20. {
  21. /**
  22. *
  23. * The character set for this part.
  24. *
  25. * @var string
  26. *
  27. */
  28. protected $_charset = 'utf-8';
  29. /**
  30. *
  31. * The CRLF sequence for this part.
  32. *
  33. * @var string
  34. *
  35. */
  36. protected $_crlf = "\r\n";
  37. /**
  38. *
  39. * The Content-Disposition for this part.
  40. *
  41. * Typically 'inline' or 'attachment'.
  42. *
  43. * @var string
  44. *
  45. */
  46. protected $_disposition = 'attachment';
  47. /**
  48. *
  49. * The Content-Transfer-Encoding for this part.
  50. *
  51. * @var string
  52. *
  53. */
  54. protected $_encoding = 'base64';
  55. /**
  56. *
  57. * When the part represents a file, use this as the filename.
  58. *
  59. * @var string
  60. *
  61. */
  62. protected $_filename = null;
  63. /**
  64. *
  65. * The Content-Type for this part.
  66. *
  67. * @var string
  68. *
  69. */
  70. protected $_type = 'application/octet-stream';
  71. /**
  72. *
  73. * The body content for this part.
  74. *
  75. * @var string
  76. *
  77. */
  78. protected $_content = null;
  79. /**
  80. *
  81. * The boundary string to use in this part.
  82. *
  83. * @var string
  84. *
  85. */
  86. protected $_boundary = null;
  87. /**
  88. *
  89. * Array of custom headers for this part.
  90. *
  91. * @var array
  92. *
  93. */
  94. protected $_headers = array();
  95. /**
  96. *
  97. * Sets the CRLF sequence for this part.
  98. *
  99. * @param string $crlf The CRLF line-ending string.
  100. *
  101. * @return void
  102. *
  103. */
  104. public function setCrlf($crlf)
  105. {
  106. $this->_crlf = $crlf;
  107. }
  108. /**
  109. *
  110. * Returns the CRLF sequence for this part.
  111. *
  112. * @return string
  113. *
  114. */
  115. public function getCrlf()
  116. {
  117. return $this->_crlf;
  118. }
  119. /**
  120. *
  121. * Sets the Content-Type for this part.
  122. *
  123. * @param string $type The content type, e.g. 'image/jpeg'.
  124. *
  125. * @return void
  126. *
  127. */
  128. public function setType($type)
  129. {
  130. $this->_type = $type;
  131. }
  132. /**
  133. *
  134. * Returns the Content-Type for this part.
  135. *
  136. * @return string
  137. *
  138. */
  139. public function getType()
  140. {
  141. return $this->_type;
  142. }
  143. /**
  144. *
  145. * Sets the Content-Type character set for this part.
  146. *
  147. * @param string $charset The character set.
  148. *
  149. * @return void
  150. *
  151. */
  152. public function setCharset($charset)
  153. {
  154. $this->_charset = $charset;
  155. }
  156. /**
  157. *
  158. * Returns the Content-Type character set for this part.
  159. *
  160. * @return string
  161. *
  162. */
  163. public function getCharset()
  164. {
  165. return $this->_charset;
  166. }
  167. /**
  168. *
  169. * Sets the Content-Type boundary for this part.
  170. *
  171. * @param string $boundary The boundary string.
  172. *
  173. * @return void
  174. *
  175. */
  176. public function setBoundary($boundary)
  177. {
  178. $this->_boundary = $boundary;
  179. }
  180. /**
  181. *
  182. * Returns the Content-Type boundary for this part.
  183. *
  184. * @return string
  185. *
  186. */
  187. public function getBoundary()
  188. {
  189. return $this->_boundary;
  190. }
  191. /**
  192. *
  193. * Sets the Content-Disposition for this part.
  194. *
  195. * @param string $disposition Typically 'inline' or 'attachment'.
  196. *
  197. * @return void
  198. *
  199. */
  200. public function setDisposition($disposition)
  201. {
  202. $this->_disposition = $disposition;
  203. }
  204. /**
  205. *
  206. * Returns the Content-Disposition for this part.
  207. *
  208. * @return string
  209. *
  210. */
  211. public function getDisposition()
  212. {
  213. return $this->_disposition;
  214. }
  215. /**
  216. *
  217. * Sets the Content-Disposition filename for this part.
  218. *
  219. * @param string $filename The file name.
  220. *
  221. * @return void
  222. *
  223. */
  224. public function setFilename($filename)
  225. {
  226. $this->_filename = $filename;
  227. }
  228. /**
  229. *
  230. * Returns the Content-Disposition filename for this part.
  231. *
  232. * @return string
  233. *
  234. */
  235. public function getFilename()
  236. {
  237. return $this->_filename;
  238. }
  239. /**
  240. *
  241. * Sets the Content-Transfer-Encoding for this part.
  242. *
  243. * @param string $encoding Typically 'base64' or 'quoted-printable'.
  244. *
  245. * @return void
  246. *
  247. */
  248. public function setEncoding($encoding)
  249. {
  250. $this->_encoding = $encoding;
  251. }
  252. /**
  253. *
  254. * Returns the Content-Transfer-Encoding for this part.
  255. *
  256. * @return string
  257. *
  258. */
  259. public function getEncoding()
  260. {
  261. return $this->_encoding;
  262. }
  263. /**
  264. *
  265. * Sets the body content for this part.
  266. *
  267. * @param string $content The body content.
  268. *
  269. * @return void
  270. *
  271. */
  272. public function setContent($content)
  273. {
  274. $this->_content = $content;
  275. }
  276. /**
  277. *
  278. * Returns the body content for this part.
  279. *
  280. * @return string
  281. *
  282. */
  283. public function getContent()
  284. {
  285. return $this->_content;
  286. }
  287. /**
  288. *
  289. * Sets (or resets) one header in the part.
  290. *
  291. * Canonicalizes the label, and strips CR/LF from the value, to help
  292. * prevent header injections.
  293. *
  294. * You can only set one label to one value; you can't have multiple
  295. * repetitions of the same label to get multiple values.
  296. *
  297. * @param string $label The header label.
  298. *
  299. * @param string $value The header value.
  300. *
  301. * @return void
  302. *
  303. */
  304. public function setHeader($label, $value)
  305. {
  306. // sanitize the header label
  307. $label = Solar_Mime::headerLabel($label);
  308. // not allowed to add headers for these labels
  309. $list = array('content-type', 'content-transfer-encoding',
  310. 'content-disposition');
  311. if (in_array(strtolower($label), $list)) {
  312. throw $this->_exception('ERR_USE_OTHER_METHOD', array(
  313. 'key' => $label,
  314. ));
  315. }
  316. // save the label and value
  317. $this->_headers[$label] = $value;
  318. }
  319. /**
  320. *
  321. * Returns the headers, a newline, and the content, all as a single block.
  322. *
  323. * @return string
  324. *
  325. */
  326. public function fetch()
  327. {
  328. return $this->fetchHeaders()
  329. . $this->_crlf
  330. . $this->fetchContent();
  331. }
  332. /**
  333. *
  334. * Returns all the headers as a string.
  335. *
  336. * @return string
  337. *
  338. */
  339. public function fetchHeaders()
  340. {
  341. // start with all the "custom" headers.
  342. // we will apply header-value encoding at the end.
  343. $headers = $this->_headers;
  344. // Content-Type:
  345. $content_type = $this->_type;
  346. if ($this->_charset) {
  347. $content_type .= '; charset="' . $this->_charset . '"';
  348. }
  349. if ($this->_boundary) {
  350. $content_type .= ';' . $this->_crlf
  351. . ' boundary="' . $this->_boundary . '"';
  352. }
  353. $headers['Content-Type'] = $content_type;
  354. // Content-Disposition:
  355. if ($this->_disposition) {
  356. $disposition = $this->_disposition;
  357. if ($this->_filename) {
  358. $disposition .= '; filename="' . $this->_filename . '"';
  359. }
  360. $headers['Content-Disposition'] = $disposition;
  361. }
  362. // Content-Transfer-Encoding:
  363. if ($this->_encoding) {
  364. $headers['Content-Transfer-Encoding'] = $this->_encoding;
  365. }
  366. // now loop through all the headers and build the header block,
  367. // using header-value encoding as we go.
  368. $output = '';
  369. foreach ($headers as $label => $value) {
  370. $label = Solar_Mime::headerLabel($label);
  371. $value = Solar_Mime::headerValue(
  372. $label,
  373. $value,
  374. $this->_charset,
  375. $this->_crlf
  376. );
  377. $output .= "$label: $value{$this->_crlf}";
  378. }
  379. return $output;
  380. }
  381. /**
  382. *
  383. * Returns the body content of this part with the proper encoding.
  384. *
  385. * @return string
  386. *
  387. */
  388. public function fetchContent()
  389. {
  390. $content = Solar_Mime::encode(
  391. $this->_encoding,
  392. $this->_content,
  393. $this->_crlf
  394. );
  395. return $content;
  396. }
  397. }