Part.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /*
  3. * File: Part.php
  4. * Category: -
  5. * Author: M.Goldenbaum
  6. * Created: 17.09.20 20:38
  7. * Updated: -
  8. *
  9. * Description:
  10. * -
  11. */
  12. namespace Webklex\PHPIMAP;
  13. use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
  14. /**
  15. * Class Part
  16. *
  17. * @package Webklex\PHPIMAP
  18. */
  19. class Part {
  20. /**
  21. * Raw part
  22. *
  23. * @var string $raw
  24. */
  25. public $raw = "";
  26. /**
  27. * Part type
  28. *
  29. * @var int $type
  30. */
  31. public $type = IMAP::MESSAGE_TYPE_TEXT;
  32. /**
  33. * Part content
  34. *
  35. * @var string $content
  36. */
  37. public $content = "";
  38. /**
  39. * Part subtype
  40. *
  41. * @var string $subtype
  42. */
  43. public $subtype = null;
  44. /**
  45. * Part charset - if available
  46. *
  47. * @var string $charset
  48. */
  49. public $charset = "utf-8";
  50. /**
  51. * Part encoding method
  52. *
  53. * @var int $encoding
  54. */
  55. public $encoding = IMAP::MESSAGE_ENC_OTHER;
  56. /**
  57. * Alias to check if the part is an attachment
  58. *
  59. * @var boolean $ifdisposition
  60. */
  61. public $ifdisposition = false;
  62. /**
  63. * Indicates if the part is an attachment
  64. *
  65. * @var string $disposition
  66. */
  67. public $disposition = null;
  68. /**
  69. * Alias to check if the part has a description
  70. *
  71. * @var boolean $ifdescription
  72. */
  73. public $ifdescription = false;
  74. /**
  75. * Part description if available
  76. *
  77. * @var string $description
  78. */
  79. public $description = null;
  80. /**
  81. * Part filename if available
  82. *
  83. * @var string $filename
  84. */
  85. public $filename = null;
  86. /**
  87. * Part name if available
  88. *
  89. * @var string $name
  90. */
  91. public $name = null;
  92. /**
  93. * Part id if available
  94. *
  95. * @var string $id
  96. */
  97. public $id = null;
  98. /**
  99. * The part number of the current part
  100. *
  101. * @var integer $part_number
  102. */
  103. public $part_number = 0;
  104. /**
  105. * Part length in bytes
  106. *
  107. * @var integer $bytes
  108. */
  109. public $bytes = null;
  110. /**
  111. * Part content type
  112. *
  113. * @var string|null $content_type
  114. */
  115. public $content_type = null;
  116. /**
  117. * @var Header $header
  118. */
  119. private $header = null;
  120. /**
  121. * Part constructor.
  122. * @param $raw_part
  123. * @param Header $header
  124. * @param integer $part_number
  125. *
  126. * @throws InvalidMessageDateException
  127. */
  128. public function __construct($raw_part, $header = null, $part_number = 0) {
  129. $this->raw = $raw_part;
  130. $this->header = $header;
  131. $this->part_number = $part_number;
  132. $this->parse();
  133. }
  134. /**
  135. * Parse the raw parts
  136. *
  137. * @throws InvalidMessageDateException
  138. */
  139. protected function parse(){
  140. if ($this->header === null) {
  141. $body = $this->findHeaders();
  142. }else{
  143. $body = $this->raw;
  144. }
  145. $this->parseDisposition();
  146. $this->parseDescription();
  147. $this->parseEncoding();
  148. $this->charset = $this->header->get("charset");
  149. $this->name = $this->header->get("name");
  150. $this->filename = $this->header->get("filename");
  151. if(!empty($this->header->get("id"))) {
  152. $this->id = $this->header->get("id");
  153. } else if(!empty($this->header->get("x_attachment_id"))){
  154. $this->id = $this->header->get("x_attachment_id");
  155. } else if(!empty($this->header->get("content_id"))){
  156. $this->id = strtr($this->header->get("content_id"), [
  157. '<' => '',
  158. '>' => ''
  159. ]);
  160. }
  161. $content_types = $this->header->get("content_type");
  162. if(!empty($content_types)){
  163. $this->subtype = $this->parseSubtype($content_types);
  164. $content_type = $content_types;
  165. if (is_array($content_types)) {
  166. $content_type = $content_types[0];
  167. }
  168. $parts = explode(';', $content_type);
  169. $this->content_type = trim($parts[0]);
  170. }
  171. $this->content = trim(rtrim($body));
  172. $this->bytes = strlen($this->content);
  173. }
  174. /**
  175. * Find all available headers and return the left over body segment
  176. *
  177. * @return string
  178. * @throws InvalidMessageDateException
  179. */
  180. private function findHeaders(){
  181. $body = $this->raw;
  182. while (($pos = strpos($body, "\r\n")) > 0) {
  183. $body = substr($body, $pos + 2);
  184. }
  185. $headers = substr($this->raw, 0, strlen($body) * -1);
  186. $body = substr($body, 0, -2);
  187. $this->header = new Header($headers);
  188. return (string) $body;
  189. }
  190. /**
  191. * Try to parse the subtype if any is present
  192. * @param $content_type
  193. *
  194. * @return string
  195. */
  196. private function parseSubtype($content_type){
  197. if (is_array($content_type)) {
  198. foreach ($content_type as $part){
  199. if ((strpos($part, "/")) !== false){
  200. return $this->parseSubtype($part);
  201. }
  202. }
  203. return null;
  204. }
  205. if (($pos = strpos($content_type, "/")) !== false){
  206. return substr($content_type, $pos + 1);
  207. }
  208. return null;
  209. }
  210. /**
  211. * Try to parse the disposition if any is present
  212. */
  213. private function parseDisposition(){
  214. $content_disposition = $this->header->get("content_disposition");
  215. if($content_disposition !== null) {
  216. $this->ifdisposition = true;
  217. $this->disposition = (is_array($content_disposition)) ? implode(' ', $content_disposition) : $content_disposition;
  218. }
  219. }
  220. /**
  221. * Try to parse the description if any is present
  222. */
  223. private function parseDescription(){
  224. $content_description = $this->header->get("content_description");
  225. if($content_description !== null) {
  226. $this->ifdescription = true;
  227. $this->description = $content_description;
  228. }
  229. }
  230. /**
  231. * Try to parse the encoding if any is present
  232. */
  233. private function parseEncoding(){
  234. $encoding = $this->header->get("content_transfer_encoding");
  235. if($encoding !== null) {
  236. switch (strtolower($encoding)) {
  237. case "quoted-printable":
  238. $this->encoding = IMAP::MESSAGE_ENC_QUOTED_PRINTABLE;
  239. break;
  240. case "base64":
  241. $this->encoding = IMAP::MESSAGE_ENC_BASE64;
  242. break;
  243. case "7bit":
  244. $this->encoding = IMAP::MESSAGE_ENC_7BIT;
  245. break;
  246. case "8bit":
  247. $this->encoding = IMAP::MESSAGE_ENC_8BIT;
  248. break;
  249. case "binary":
  250. $this->encoding = IMAP::MESSAGE_ENC_BINARY;
  251. break;
  252. default:
  253. $this->encoding = IMAP::MESSAGE_ENC_OTHER;
  254. break;
  255. }
  256. }
  257. }
  258. /**
  259. * Check if the current part represents an attachment
  260. *
  261. * @return bool
  262. */
  263. public function isAttachment(){
  264. $valid_disposition = in_array(strtolower($this->disposition), ClientManager::get('options.dispositions'));
  265. if ($this->type == IMAP::MESSAGE_TYPE_TEXT && ($this->ifdisposition == 0 || (empty($this->disposition))) && !$valid_disposition) {
  266. if (($this->subtype == null || in_array((strtolower($this->subtype)), ["plain", "html"])) && $this->filename == null && $this->name == null) {
  267. return false;
  268. }
  269. }
  270. return true;
  271. }
  272. }