Header.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. <?php
  2. /*
  3. * File: Header.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 Carbon\Carbon;
  14. use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
  15. use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
  16. /**
  17. * Class Header
  18. *
  19. * @package Webklex\PHPIMAP
  20. */
  21. class Header {
  22. /**
  23. * Raw header
  24. *
  25. * @var string $raw
  26. */
  27. public $raw = "";
  28. /**
  29. * Attribute holder
  30. *
  31. * @var Attribute[]|array $attributes
  32. */
  33. protected $attributes = [];
  34. /**
  35. * Config holder
  36. *
  37. * @var array $config
  38. */
  39. protected $config = [];
  40. /**
  41. * Fallback Encoding
  42. *
  43. * @var string
  44. */
  45. public $fallback_encoding = 'UTF-8';
  46. /**
  47. * Convert parsed values to attributes
  48. *
  49. * @var bool
  50. */
  51. protected $attributize = false;
  52. /**
  53. * Header constructor.
  54. * @param string $raw_header
  55. * @param boolean $attributize
  56. *
  57. * @throws InvalidMessageDateException
  58. */
  59. public function __construct($raw_header, $attributize = true) {
  60. $this->raw = $raw_header;
  61. $this->config = ClientManager::get('options');
  62. $this->attributize = $attributize;
  63. $this->parse();
  64. }
  65. /**
  66. * Call dynamic attribute setter and getter methods
  67. * @param string $method
  68. * @param array $arguments
  69. *
  70. * @return Attribute|mixed
  71. * @throws MethodNotFoundException
  72. */
  73. public function __call($method, $arguments) {
  74. if(strtolower(substr($method, 0, 3)) === 'get') {
  75. $name = preg_replace('/(.)(?=[A-Z])/u', '$1_', substr(strtolower($method), 3));
  76. if(in_array($name, array_keys($this->attributes))) {
  77. return $this->attributes[$name];
  78. }
  79. }
  80. throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
  81. }
  82. /**
  83. * Magic getter
  84. * @param $name
  85. *
  86. * @return Attribute|null
  87. */
  88. public function __get($name) {
  89. return $this->get($name);
  90. }
  91. /**
  92. * Get a specific header attribute
  93. * @param $name
  94. *
  95. * @return Attribute|mixed
  96. */
  97. public function get($name) {
  98. if(isset($this->attributes[$name])) {
  99. return $this->attributes[$name];
  100. }
  101. return null;
  102. }
  103. /**
  104. * Set a specific attribute
  105. * @param string $name
  106. * @param array|mixed $value
  107. * @param boolean $strict
  108. *
  109. * @return Attribute
  110. */
  111. public function set($name, $value, $strict = false) {
  112. if(isset($this->attributes[$name]) && $strict === false) {
  113. if ($this->attributize) {
  114. $this->attributes[$name]->add($value, true);
  115. }else{
  116. if(isset($this->attributes[$name])) {
  117. if (is_array($this->attributes[$name]) == false) {
  118. $this->attributes[$name] = [$this->attributes[$name], $value];
  119. }else{
  120. $this->attributes[$name][] = $value;
  121. }
  122. }else{
  123. $this->attributes[$name] = $value;
  124. }
  125. }
  126. }elseif($this->attributize == false){
  127. $this->attributes[$name] = $value;
  128. }else{
  129. $this->attributes[$name] = new Attribute($name, $value);
  130. }
  131. return $this->attributes[$name];
  132. }
  133. /**
  134. * Perform a regex match all on the raw header and return the first result
  135. * @param $pattern
  136. *
  137. * @return mixed|null
  138. */
  139. public function find($pattern) {
  140. if (preg_match_all($pattern, $this->raw, $matches)) {
  141. if (isset($matches[1])) {
  142. if(count($matches[1]) > 0) {
  143. return $matches[1][0];
  144. }
  145. }
  146. }
  147. return null;
  148. }
  149. /**
  150. * Try to find a boundary if possible
  151. *
  152. * @return string|null
  153. */
  154. public function getBoundary(){
  155. $boundary = $this->find("/boundary\=(.*)/i");
  156. if ($boundary === null) {
  157. return null;
  158. }
  159. return $this->clearBoundaryString($boundary);
  160. }
  161. /**
  162. * Remove all unwanted chars from a given boundary
  163. * @param string $str
  164. *
  165. * @return string
  166. */
  167. private function clearBoundaryString($str) {
  168. return str_replace(['"', '\r', '\n', "\n", "\r", ";", "\s"], "", $str);
  169. }
  170. /**
  171. * Parse the raw headers
  172. *
  173. * @throws InvalidMessageDateException
  174. */
  175. protected function parse(){
  176. $header = $this->rfc822_parse_headers($this->raw);
  177. $this->extractAddresses($header);
  178. if (property_exists($header, 'subject')) {
  179. $this->set("subject", $this->decode($header->subject));
  180. }
  181. if (property_exists($header, 'references')) {
  182. $this->set("references", $this->decode($header->references));
  183. }
  184. if (property_exists($header, 'message_id')) {
  185. $this->set("message_id", str_replace(['<', '>'], '', $header->message_id));
  186. }
  187. $this->parseDate($header);
  188. foreach ($header as $key => $value) {
  189. $key = trim(rtrim(strtolower($key)));
  190. if(!isset($this->attributes[$key])){
  191. $this->set($key, $value);
  192. }
  193. }
  194. $this->extractHeaderExtensions();
  195. $this->findPriority();
  196. }
  197. /**
  198. * Parse mail headers from a string
  199. * @link https://php.net/manual/en/function.imap-rfc822-parse-headers.php
  200. * @param $raw_headers
  201. *
  202. * @return object
  203. */
  204. public function rfc822_parse_headers($raw_headers){
  205. $headers = [];
  206. $imap_headers = [];
  207. if (extension_loaded('imap') && $this->config["rfc822"]) {
  208. $raw_imap_headers = (array) \imap_rfc822_parse_headers($this->raw);
  209. foreach($raw_imap_headers as $key => $values) {
  210. $key = str_replace("-", "_", $key);
  211. $imap_headers[$key] = $values;
  212. }
  213. }
  214. $lines = explode("\r\n", str_replace("\r\n\t", ' ', $raw_headers));
  215. $prev_header = null;
  216. foreach($lines as $line) {
  217. if (substr($line, 0, 1) === "\n") {
  218. $line = substr($line, 1);
  219. }
  220. if (substr($line, 0, 1) === "\t") {
  221. $line = substr($line, 1);
  222. $line = trim(rtrim($line));
  223. if ($prev_header !== null) {
  224. $headers[$prev_header][] = $line;
  225. }
  226. }elseif (substr($line, 0, 1) === " ") {
  227. $line = substr($line, 1);
  228. $line = trim(rtrim($line));
  229. if ($prev_header !== null) {
  230. if (!isset($headers[$prev_header])) {
  231. $headers[$prev_header] = "";
  232. }
  233. if (is_array($headers[$prev_header])) {
  234. $headers[$prev_header][] = $line;
  235. }else{
  236. $headers[$prev_header] .= $line;
  237. }
  238. }
  239. }else{
  240. if (($pos = strpos($line, ":")) > 0) {
  241. $key = trim(rtrim(strtolower(substr($line, 0, $pos))));
  242. $key = str_replace("-", "_", $key);
  243. $value = trim(rtrim(substr($line, $pos + 1)));
  244. if (isset($headers[$key])) {
  245. $headers[$key][] = $value;
  246. }else{
  247. $headers[$key] = [$value];
  248. }
  249. $prev_header = $key;
  250. }
  251. }
  252. }
  253. foreach($headers as $key => $values) {
  254. if (isset($imap_headers[$key])) continue;
  255. $value = null;
  256. switch($key){
  257. case 'from':
  258. case 'to':
  259. case 'cc':
  260. case 'bcc':
  261. case 'reply_to':
  262. case 'sender':
  263. $value = $this->decodeAddresses($values);
  264. $headers[$key."address"] = implode(", ", $values);
  265. break;
  266. case 'subject':
  267. $value = implode(" ", $values);
  268. break;
  269. default:
  270. if (is_array($values)) {
  271. foreach($values as $k => $v) {
  272. if ($v == "") {
  273. unset($values[$k]);
  274. }
  275. }
  276. $available_values = count($values);
  277. if ($available_values === 1) {
  278. $value = array_pop($values);
  279. } elseif ($available_values === 2) {
  280. $value = implode(" ", $values);
  281. } elseif ($available_values > 2) {
  282. $value = array_values($values);
  283. } else {
  284. $value = "";
  285. }
  286. }
  287. break;
  288. }
  289. $headers[$key] = $value;
  290. }
  291. return (object) array_merge($headers, $imap_headers);
  292. }
  293. /**
  294. * Decode MIME header elements
  295. * @link https://php.net/manual/en/function.imap-mime-header-decode.php
  296. * @param string $text The MIME text
  297. *
  298. * @return array The decoded elements are returned in an array of objects, where each
  299. * object has two properties, charset and text.
  300. */
  301. public function mime_header_decode($text){
  302. if (extension_loaded('imap')) {
  303. return \imap_mime_header_decode($text);
  304. }
  305. $charset = $this->getEncoding($text);
  306. return [(object)[
  307. "charset" => $charset,
  308. "text" => $this->convertEncoding($text, $charset)
  309. ]];
  310. }
  311. /**
  312. * Check if a given pair of strings has ben decoded
  313. * @param $encoded
  314. * @param $decoded
  315. *
  316. * @return bool
  317. */
  318. private function notDecoded($encoded, $decoded) {
  319. return 0 === strpos($decoded, '=?')
  320. && strlen($decoded) - 2 === strpos($decoded, '?=')
  321. && false !== strpos($encoded, $decoded);
  322. }
  323. /**
  324. * Convert the encoding
  325. * @param $str
  326. * @param string $from
  327. * @param string $to
  328. *
  329. * @return mixed|string
  330. */
  331. public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
  332. $from = EncodingAliases::get($from, $this->fallback_encoding);
  333. $to = EncodingAliases::get($to, $this->fallback_encoding);
  334. if ($from === $to) {
  335. return $str;
  336. }
  337. // We don't need to do convertEncoding() if charset is ASCII (us-ascii):
  338. // ASCII is a subset of UTF-8, so all ASCII files are already UTF-8 encoded
  339. // https://stackoverflow.com/a/11303410
  340. //
  341. // us-ascii is the same as ASCII:
  342. // ASCII is the traditional name for the encoding system; the Internet Assigned Numbers Authority (IANA)
  343. // prefers the updated name US-ASCII, which clarifies that this system was developed in the US and
  344. // based on the typographical symbols predominantly in use there.
  345. // https://en.wikipedia.org/wiki/ASCII
  346. //
  347. // convertEncoding() function basically means convertToUtf8(), so when we convert ASCII string into UTF-8 it gets broken.
  348. if (strtolower($from) == 'us-ascii' && $to == 'UTF-8') {
  349. return $str;
  350. }
  351. try {
  352. if (function_exists('iconv') && $from != 'UTF-7' && $to != 'UTF-7') {
  353. return iconv($from, $to, $str);
  354. } else {
  355. if (!$from) {
  356. return mb_convert_encoding($str, $to);
  357. }
  358. return mb_convert_encoding($str, $to, $from);
  359. }
  360. } catch (\Exception $e) {
  361. if (strstr($from, '-')) {
  362. $from = str_replace('-', '', $from);
  363. return $this->convertEncoding($str, $from, $to);
  364. } else {
  365. return $str;
  366. }
  367. }
  368. }
  369. /**
  370. * Get the encoding of a given abject
  371. * @param object|string $structure
  372. *
  373. * @return string
  374. */
  375. public function getEncoding($structure) {
  376. if (property_exists($structure, 'parameters')) {
  377. foreach ($structure->parameters as $parameter) {
  378. if (strtolower($parameter->attribute) == "charset") {
  379. return EncodingAliases::get($parameter->value, $this->fallback_encoding);
  380. }
  381. }
  382. }elseif (property_exists($structure, 'charset')) {
  383. return EncodingAliases::get($structure->charset, $this->fallback_encoding);
  384. }elseif (is_string($structure) === true){
  385. return mb_detect_encoding($structure);
  386. }
  387. return $this->fallback_encoding;
  388. }
  389. /**
  390. * Test if a given value is utf-8 encoded
  391. * @param $value
  392. *
  393. * @return bool
  394. */
  395. private function is_uft8($value) {
  396. return strpos(strtolower($value), '=?utf-8?') === 0;
  397. }
  398. /**
  399. * Try to decode a specific header
  400. * @param mixed $value
  401. *
  402. * @return mixed
  403. */
  404. private function decode($value) {
  405. if (is_array($value)) {
  406. return $this->decodeArray($value);
  407. }
  408. $original_value = $value;
  409. $decoder = $this->config['decoder']['message'];
  410. if ($value !== null) {
  411. $is_utf8_base = $this->is_uft8($value);
  412. if($decoder === 'utf-8' && extension_loaded('imap')) {
  413. $value = \imap_utf8($value);
  414. $is_utf8_base = $this->is_uft8($value);
  415. if ($is_utf8_base) {
  416. $value = mb_decode_mimeheader($value);
  417. }
  418. if ($this->notDecoded($original_value, $value)) {
  419. $decoded_value = $this->mime_header_decode($value);
  420. if (count($decoded_value) > 0) {
  421. if(property_exists($decoded_value[0], "text")) {
  422. $value = $decoded_value[0]->text;
  423. }
  424. }
  425. }
  426. }elseif($decoder === 'iconv' && $is_utf8_base) {
  427. $value = iconv_mime_decode($value);
  428. }elseif($is_utf8_base){
  429. $value = mb_decode_mimeheader($value);
  430. }
  431. if ($this->is_uft8($value)) {
  432. $value = mb_decode_mimeheader($value);
  433. }
  434. if ($this->notDecoded($original_value, $value)) {
  435. $value = $this->convertEncoding($original_value, $this->getEncoding($original_value));
  436. }
  437. }
  438. return $value;
  439. }
  440. /**
  441. * Decode a given array
  442. * @param array $values
  443. *
  444. * @return array
  445. */
  446. private function decodeArray($values) {
  447. foreach($values as $key => $value) {
  448. $values[$key] = $this->decode($value);
  449. }
  450. return $values;
  451. }
  452. /**
  453. * Try to extract the priority from a given raw header string
  454. */
  455. private function findPriority() {
  456. if(($priority = $this->get("x_priority")) === null) return;
  457. switch((int)"$priority"){
  458. case IMAP::MESSAGE_PRIORITY_HIGHEST;
  459. $priority = IMAP::MESSAGE_PRIORITY_HIGHEST;
  460. break;
  461. case IMAP::MESSAGE_PRIORITY_HIGH;
  462. $priority = IMAP::MESSAGE_PRIORITY_HIGH;
  463. break;
  464. case IMAP::MESSAGE_PRIORITY_NORMAL;
  465. $priority = IMAP::MESSAGE_PRIORITY_NORMAL;
  466. break;
  467. case IMAP::MESSAGE_PRIORITY_LOW;
  468. $priority = IMAP::MESSAGE_PRIORITY_LOW;
  469. break;
  470. case IMAP::MESSAGE_PRIORITY_LOWEST;
  471. $priority = IMAP::MESSAGE_PRIORITY_LOWEST;
  472. break;
  473. default:
  474. $priority = IMAP::MESSAGE_PRIORITY_UNKNOWN;
  475. break;
  476. }
  477. $this->set("priority", $priority);
  478. }
  479. /**
  480. * Extract a given part as address array from a given header
  481. * @param $values
  482. *
  483. * @return array
  484. */
  485. private function decodeAddresses($values) {
  486. $addresses = [];
  487. if (extension_loaded('mailparse') && $this->config["rfc822"]) {
  488. foreach ($values as $address) {
  489. foreach (\mailparse_rfc822_parse_addresses($address) as $parsed_address) {
  490. if (isset($parsed_address['address'])) {
  491. $mail_address = explode('@', $parsed_address['address']);
  492. if (count($mail_address) == 2) {
  493. $addresses[] = (object)[
  494. "personal" => isset($parsed_address['display']) ? $parsed_address['display'] : '',
  495. "mailbox" => $mail_address[0],
  496. "host" => $mail_address[1],
  497. ];
  498. }
  499. }
  500. }
  501. }
  502. return $addresses;
  503. }
  504. foreach($values as $address) {
  505. foreach (preg_split('/, (?=(?:[^"]*"[^"]*")*[^"]*$)/', $address) as $split_address) {
  506. $split_address = trim(rtrim($split_address));
  507. if (strpos($split_address, ",") == strlen($split_address) - 1) {
  508. $split_address = substr($split_address, 0, -1);
  509. }
  510. if (preg_match(
  511. '/^(?:(?P<name>.+)\s)?(?(name)<|<?)(?P<email>[^\s]+?)(?(name)>|>?)$/',
  512. $split_address,
  513. $matches
  514. )) {
  515. $name = trim(rtrim($matches["name"]));
  516. $email = trim(rtrim($matches["email"]));
  517. list($mailbox, $host) = array_pad(explode("@", $email), 2, null);
  518. $addresses[] = (object)[
  519. "personal" => $name,
  520. "mailbox" => $mailbox,
  521. "host" => $host,
  522. ];
  523. }
  524. }
  525. }
  526. return $addresses;
  527. }
  528. /**
  529. * Extract a given part as address array from a given header
  530. * @param object $header
  531. */
  532. private function extractAddresses($header) {
  533. foreach(['from', 'to', 'cc', 'bcc', 'reply_to', 'sender'] as $key){
  534. if (property_exists($header, $key)) {
  535. $this->set($key, $this->parseAddresses($header->$key));
  536. }
  537. }
  538. }
  539. /**
  540. * Parse Addresses
  541. * @param $list
  542. *
  543. * @return array
  544. */
  545. private function parseAddresses($list) {
  546. $addresses = [];
  547. if (is_array($list) === false) {
  548. return $addresses;
  549. }
  550. foreach ($list as $item) {
  551. $address = (object) $item;
  552. if (!property_exists($address, 'mailbox')) {
  553. $address->mailbox = false;
  554. }
  555. if (!property_exists($address, 'host')) {
  556. $address->host = false;
  557. }
  558. if (!property_exists($address, 'personal')) {
  559. $address->personal = false;
  560. } else {
  561. $personalParts = $this->mime_header_decode($address->personal);
  562. if(is_array($personalParts)) {
  563. $address->personal = '';
  564. foreach ($personalParts as $p) {
  565. $address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
  566. }
  567. }
  568. if (strpos($address->personal, "'") === 0) {
  569. $address->personal = str_replace("'", "", $address->personal);
  570. }
  571. }
  572. $address->mail = ($address->mailbox && $address->host) ? $address->mailbox.'@'.$address->host : false;
  573. $address->full = ($address->personal) ? $address->personal.' <'.$address->mail.'>' : $address->mail;
  574. $addresses[] = new Address($address);
  575. }
  576. return $addresses;
  577. }
  578. /**
  579. * Search and extract potential header extensions
  580. */
  581. private function extractHeaderExtensions(){
  582. foreach ($this->attributes as $key => $value) {
  583. if (is_array($value)) {
  584. $value = implode(", ", $value);
  585. }else{
  586. $value = (string)$value;
  587. }
  588. // Only parse strings and don't parse any attributes like the user-agent
  589. if (in_array($key, ["user_agent"]) === false) {
  590. if (($pos = strpos($value, ";")) !== false){
  591. $original = substr($value, 0, $pos);
  592. $this->set($key, trim(rtrim($original)), true);
  593. // Get all potential extensions
  594. $extensions = explode(";", substr($value, $pos + 1));
  595. foreach($extensions as $extension) {
  596. if (($pos = strpos($extension, "=")) !== false){
  597. $key = substr($extension, 0, $pos);
  598. $key = trim(rtrim(strtolower($key)));
  599. if (isset($this->attributes[$key]) === false) {
  600. $value = substr($extension, $pos + 1);
  601. $value = str_replace('"', "", $value);
  602. $value = trim(rtrim($value));
  603. $this->set($key, $value);
  604. }
  605. }
  606. }
  607. }
  608. }
  609. }
  610. }
  611. /**
  612. * Exception handling for invalid dates
  613. *
  614. * Currently known invalid formats:
  615. * ^ Datetime ^ Problem ^ Cause
  616. * | Mon, 20 Nov 2017 20:31:31 +0800 (GMT+8:00) | Double timezone specification | A Windows feature
  617. * | Thu, 8 Nov 2018 08:54:58 -0200 (-02) |
  618. * | | and invalid timezone (max 6 char) |
  619. * | 04 Jan 2018 10:12:47 UT | Missing letter "C" | Unknown
  620. * | Thu, 31 May 2018 18:15:00 +0800 (added by) | Non-standard details added by the | Unknown
  621. * | | mail server |
  622. * | Sat, 31 Aug 2013 20:08:23 +0580 | Invalid timezone | PHPMailer bug https://sourceforge.net/p/phpmailer/mailman/message/6132703/
  623. *
  624. * Please report any new invalid timestamps to [#45](https://github.com/Webklex/php-imap/issues)
  625. *
  626. * @param object $header
  627. *
  628. * @throws InvalidMessageDateException
  629. */
  630. private function parseDate($header) {
  631. if (property_exists($header, 'date')) {
  632. $parsed_date = null;
  633. $date = $header->date;
  634. if(preg_match('/\+0580/', $date)) {
  635. $date = str_replace('+0580', '+0530', $date);
  636. }
  637. $date = trim(rtrim($date));
  638. try {
  639. $parsed_date = Carbon::parse($date);
  640. } catch (\Exception $e) {
  641. switch (true) {
  642. case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
  643. case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ UT)+$/i', $date) > 0:
  644. $date .= 'C';
  645. break;
  646. case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ \+[0-9]{2,4}\ \(\+[0-9]{1,2}\))+$/i', $date) > 0:
  647. case preg_match('/([A-Z]{2,3}[\,|\ \,]\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}.*)+$/i', $date) > 0:
  648. case preg_match('/([A-Z]{2,3}\,\ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
  649. case preg_match('/([A-Z]{2,3}\, \ [0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{4}\ [0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}\ [\-|\+][0-9]{4}\ \(.*)\)+$/i', $date) > 0:
  650. case preg_match('/([0-9]{1,2}\ [A-Z]{2,3}\ [0-9]{2,4}\ [0-9]{2}\:[0-9]{2}\:[0-9]{2}\ [A-Z]{2}\ \-[0-9]{2}\:[0-9]{2}\ \([A-Z]{2,3}\ \-[0-9]{2}:[0-9]{2}\))+$/i', $date) > 0:
  651. $array = explode('(', $date);
  652. $array = array_reverse($array);
  653. $date = trim(array_pop($array));
  654. break;
  655. }
  656. try{
  657. $parsed_date = Carbon::parse($date);
  658. } catch (\Exception $_e) {
  659. throw new InvalidMessageDateException("Invalid message date. ID:".$this->get("message_id"), 1100, $e);
  660. }
  661. }
  662. $this->set("date", $parsed_date);
  663. }
  664. }
  665. /**
  666. * Get all available attributes
  667. *
  668. * @return array
  669. */
  670. public function getAttributes() {
  671. return $this->attributes;
  672. }
  673. }