Skip to content

Commit a8d29f5

Browse files
committed
cancelled reformated code
1 parent 6d2b4e0 commit a8d29f5

File tree

1 file changed

+61
-91
lines changed

1 file changed

+61
-91
lines changed

src/Fetch/Message.php

Lines changed: 61 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,7 @@ class Message
8383
*
8484
* @var string
8585
*/
86-
protected static $flagTypes = array(
87-
self::FLAG_RECENT,
88-
self::FLAG_FLAGGED,
89-
self::FLAG_ANSWERED,
90-
self::FLAG_DELETED,
91-
self::FLAG_SEEN,
92-
self::FLAG_DRAFT
93-
);
86+
protected static $flagTypes = array(self::FLAG_RECENT, self::FLAG_FLAGGED, self::FLAG_ANSWERED, self::FLAG_DELETED, self::FLAG_SEEN, self::FLAG_DRAFT);
9487

9588
/**
9689
* This holds the plantext email message.
@@ -213,18 +206,17 @@ class Message
213206
* message should be opened from. This constructor should generally not be called directly, but rather retrieved
214207
* through the apprioriate Imap functions.
215208
*
216-
* @param int $messageUniqueId
217-
* @param Server $connection
209+
* @param int $messageUniqueId
210+
* @param Server $mailbox
218211
*/
219212
public function __construct($messageUniqueId, Server $connection)
220213
{
221214
$this->imapConnection = $connection;
222-
$this->mailbox = $connection->getMailBox();
223-
$this->uid = $messageUniqueId;
224-
$this->imapStream = $this->imapConnection->getImapStream();
225-
if ($this->loadMessage() !== true) {
215+
$this->mailbox = $connection->getMailBox();
216+
$this->uid = $messageUniqueId;
217+
$this->imapStream = $this->imapConnection->getImapStream();
218+
if($this->loadMessage() !== true)
226219
throw new \RuntimeException('Message with ID ' . $messageUniqueId . ' not found.');
227-
}
228220
}
229221

230222
/**
@@ -237,39 +229,34 @@ protected function loadMessage()
237229

238230
/* First load the message overview information */
239231

240-
if (!is_object($messageOverview = $this->getOverview())) {
232+
if(!is_object($messageOverview = $this->getOverview()))
233+
241234
return false;
242-
}
243235

244236
$this->subject = MIME::decode($messageOverview->subject, self::$charset);
245-
$this->date = strtotime($messageOverview->date);
246-
$this->size = $messageOverview->size;
237+
$this->date = strtotime($messageOverview->date);
238+
$this->size = $messageOverview->size;
247239

248-
foreach (self::$flagTypes as $flag) {
240+
foreach (self::$flagTypes as $flag)
249241
$this->status[$flag] = ($messageOverview->$flag == 1);
250-
}
251242

252243
/* Next load in all of the header information */
253244

254245
$headers = $this->getHeaders();
255246

256-
if (isset($headers->to)) {
247+
if (isset($headers->to))
257248
$this->to = $this->processAddressObject($headers->to);
258-
}
259249

260-
if (isset($headers->cc)) {
250+
if (isset($headers->cc))
261251
$this->cc = $this->processAddressObject($headers->cc);
262-
}
263252

264-
if (isset($headers->bcc)) {
253+
if (isset($headers->bcc))
265254
$this->bcc = $this->processAddressObject($headers->bcc);
266-
}
267255

268-
if (isset($headers->sender)) {
256+
if (isset($headers->sender))
269257
$this->sender = $this->processAddressObject($headers->sender);
270-
}
271258

272-
$this->from = isset($headers->from) ? $this->processAddressObject($headers->from) : array('');
259+
$this->from = isset($headers->from) ? $this->processAddressObject($headers->from) : array('');
273260
$this->replyTo = isset($headers->reply_to) ? $this->processAddressObject($headers->reply_to) : $this->from;
274261

275262
/* Finally load the structure itself */
@@ -281,9 +268,8 @@ protected function loadMessage()
281268
$this->processStructure($structure);
282269
} else {
283270
// multipart
284-
foreach ($structure->parts as $id => $part) {
271+
foreach ($structure->parts as $id => $part)
285272
$this->processStructure($part, $id + 1);
286-
}
287273
}
288274

289275
return true;
@@ -294,19 +280,19 @@ protected function loadMessage()
294280
* imap_fetch_overview function, only instead of an array of message overviews only a single result is returned. The
295281
* results are only retrieved from the server once unless passed true as a parameter.
296282
*
297-
* @param bool $forceReload
283+
* @param bool $forceReload
298284
* @return \stdClass
299285
*/
300286
public function getOverview($forceReload = false)
301287
{
302288
if ($forceReload || !isset($this->messageOverview)) {
303289
// returns an array, and since we just want one message we can grab the only result
304-
$results = imap_fetch_overview($this->imapStream, $this->uid, FT_UID);
305-
if (sizeof($results) == 0) {
290+
$results = imap_fetch_overview($this->imapStream, $this->uid, FT_UID);
291+
if ( sizeof($results) == 0 ) {
306292
throw new \RuntimeException('Error fetching overview');
307293
}
308294
$this->messageOverview = array_shift($results);
309-
if (!isset($this->messageOverview->date)) {
295+
if ( ! isset($this->messageOverview->date)) {
310296
$this->messageOverview->date = null;
311297
}
312298
}
@@ -317,7 +303,7 @@ public function getOverview($forceReload = false)
317303
/**
318304
* This function returns an object containing the raw headers of the message.
319305
*
320-
* @param bool $forceReload
306+
* @param bool $forceReload
321307
* @return string
322308
*/
323309
public function getRawHeaders($forceReload = false)
@@ -335,7 +321,7 @@ public function getRawHeaders($forceReload = false)
335321
* and running them through the imap_rfc822_parse_headers function. The results are only retrieved from the server
336322
* once unless passed true as a parameter.
337323
*
338-
* @param bool $forceReload
324+
* @param bool $forceReload
339325
* @return \stdClass
340326
*/
341327
public function getHeaders($forceReload = false)
@@ -366,7 +352,7 @@ public function getHeaders($forceReload = false)
366352
* returned by imap_fetchstructure. The results are only retrieved from the server once unless passed true as a
367353
* parameter.
368354
*
369-
* @param bool $forceReload
355+
* @param bool $forceReload
370356
* @return \stdClass
371357
*/
372358
public function getStructure($forceReload = false)
@@ -384,7 +370,7 @@ public function getStructure($forceReload = false)
384370
* the plaintext version is given some html formatting and returned. If neither are present the return value will be
385371
* false.
386372
*
387-
* @param bool $html Pass true to receive an html response.
373+
* @param bool $html Pass true to receive an html response.
388374
* @return string|bool Returns false if no body is present.
389375
*/
390376
public function getMessageBody($html = false)
@@ -400,7 +386,7 @@ public function getMessageBody($html = false)
400386
}
401387
} else {
402388
if (!isset($this->plaintextMessage) && isset($this->htmlMessage)) {
403-
$output = preg_replace('/\s*\<br\s*\/?\>/i', PHP_EOL, trim($this->htmlMessage));
389+
$output = preg_replace('/\s*\<br\s*\/?\>/i', PHP_EOL, trim($this->htmlMessage) );
404390
$output = strip_tags($output);
405391

406392
return $output;
@@ -434,36 +420,32 @@ public function getHtmlBody()
434420
* This function returns either an array of email addresses and names or, optionally, a string that can be used in
435421
* mail headers.
436422
*
437-
* @param string $type Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.
438-
* @param bool $asString
423+
* @param string $type Should be 'to', 'cc', 'bcc', 'from', 'sender', or 'reply-to'.
424+
* @param bool $asString
439425
* @return array|string|bool
440426
*/
441427
public function getAddresses($type, $asString = false)
442428
{
443-
$type = ($type == 'reply-to') ? 'replyTo' : $type;
429+
$type = ( $type == 'reply-to' ) ? 'replyTo' : $type;
444430
$addressTypes = array('to', 'cc', 'bcc', 'from', 'sender', 'replyTo');
445431

446-
if (!in_array($type, $addressTypes) || !isset($this->$type) || count($this->$type) < 1) {
432+
if (!in_array($type, $addressTypes) || !isset($this->$type) || count($this->$type) < 1)
447433
return false;
448-
}
449434

450435
if (!$asString) {
451-
if ($type == 'from') {
436+
if ($type == 'from')
452437
return $this->from[0];
453-
} elseif ($type == 'sender') {
438+
elseif ($type == 'sender')
454439
return $this->sender[0];
455-
}
456440

457441
return $this->$type;
458442
} else {
459443
$outputString = '';
460444
foreach ($this->$type as $address) {
461-
if (isset($set)) {
445+
if (isset($set))
462446
$outputString .= ', ';
463-
}
464-
if (!isset($set)) {
447+
if (!isset($set))
465448
$set = true;
466-
}
467449

468450
$outputString .= isset($address['name']) ?
469451
$address['name'] . ' <' . $address['address'] . '>'
@@ -520,7 +502,7 @@ public function getImapBox()
520502
* message has its own subparts, those are recursively processed using this function.
521503
*
522504
* @param \stdClass $structure
523-
* @param string $partIdentifier
505+
* @param string $partIdentifier
524506
*/
525507
protected function processStructure($structure, $partIdentifier = null)
526508
{
@@ -529,7 +511,7 @@ protected function processStructure($structure, $partIdentifier = null)
529511
if ((isset($parameters['name']) || isset($parameters['filename']))
530512
|| (isset($structure->subtype) && strtolower($structure->subtype) == 'rfc822')
531513
) {
532-
$attachment = new Attachment($this, $structure, $partIdentifier);
514+
$attachment = new Attachment($this, $structure, $partIdentifier);
533515
$this->attachments[] = $attachment;
534516
} elseif ($structure->type == 0 || $structure->type == 1) {
535517
$messageBody = isset($partIdentifier) ?
@@ -541,23 +523,21 @@ protected function processStructure($structure, $partIdentifier = null)
541523
if (!empty($parameters['charset']) && $parameters['charset'] !== self::$charset) {
542524
$mb_converted = false;
543525
if (function_exists('mb_convert_encoding')) {
544-
$mb_encoding = false;
545-
foreach (mb_list_encodings() as $encoding) {
546-
if (strtolower($encoding) === strtolower($parameters['charset'])) {
547-
$mb_encoding = true;
548-
}
549-
}
550-
551-
if ($mb_encoding === false) {
526+
$encodings = mb_list_encodings();
527+
if ($key = array_search(
528+
strtolower($parameters['charset']),
529+
array_map(function($val){return mb_strtolower($val);}, $encodings)
530+
) !== false) {
531+
$parameters['charset'] = $encodings[$key];
532+
}else {
552533
$parameters['charset'] = ($structure->encoding === 0 ? 'US-ASCII' : 'UTF-8');
553534
}
554535

555536
$messageBody = @mb_convert_encoding($messageBody, self::$charset, $parameters['charset']);
556537
$mb_converted = true;
557538
}
558539
if (!$mb_converted) {
559-
$messageBodyConv = @iconv($parameters['charset'], self::$charset . self::$charsetFlag,
560-
$messageBody);
540+
$messageBodyConv = @iconv($parameters['charset'], self::$charset . self::$charsetFlag, $messageBody);
561541

562542
if ($messageBodyConv !== false) {
563543
$messageBody = $messageBodyConv;
@@ -589,9 +569,8 @@ protected function processStructure($structure, $partIdentifier = null)
589569
foreach ($structure->parts as $partIndex => $part) {
590570
$partId = $partIndex + 1;
591571

592-
if (isset($partIdentifier)) {
572+
if (isset($partIdentifier))
593573
$partId = $partIdentifier . '.' . $partId;
594-
}
595574

596575
$this->processStructure($part, $partId);
597576
}
@@ -601,7 +580,7 @@ protected function processStructure($structure, $partIdentifier = null)
601580
/**
602581
* This function takes in the message data and encoding type and returns the decoded data.
603582
*
604-
* @param string $data
583+
* @param string $data
605584
* @param int|string $encoding
606585
* @return string
607586
*/
@@ -628,7 +607,7 @@ public static function decode($data, $encoding)
628607
/**
629608
* This function returns the body type that an imap integer maps to.
630609
*
631-
* @param int $id
610+
* @param int $id
632611
* @return string
633612
*/
634613
public static function typeIdToString($id)
@@ -670,17 +649,13 @@ public static function typeIdToString($id)
670649
public static function getParametersFromStructure($structure)
671650
{
672651
$parameters = array();
673-
if (isset($structure->parameters)) {
674-
foreach ($structure->parameters as $parameter) {
652+
if (isset($structure->parameters))
653+
foreach ($structure->parameters as $parameter)
675654
$parameters[strtolower($parameter->attribute)] = $parameter->value;
676-
}
677-
}
678655

679-
if (isset($structure->dparameters)) {
680-
foreach ($structure->dparameters as $parameter) {
656+
if (isset($structure->dparameters))
657+
foreach ($structure->dparameters as $parameter)
681658
$parameters[strtolower($parameter->attribute)] = $parameter->value;
682-
}
683-
}
684659

685660
return $parameters;
686661
}
@@ -695,7 +670,7 @@ public static function getParametersFromStructure($structure)
695670
protected function processAddressObject($addresses)
696671
{
697672
$outputAddresses = array();
698-
if (is_array($addresses)) {
673+
if (is_array($addresses))
699674
foreach ($addresses as $address) {
700675
if (property_exists($address, 'mailbox') && $address->mailbox != 'undisclosed-recipients') {
701676
$currentAddress = array();
@@ -706,7 +681,6 @@ protected function processAddressObject($addresses)
706681
$outputAddresses[] = $currentAddress;
707682
}
708683
}
709-
}
710684

711685
return $outputAddresses;
712686
}
@@ -725,24 +699,21 @@ public function getUid()
725699
* This function returns the attachments a message contains. If a filename is passed then just that ImapAttachment
726700
* is returned, unless
727701
*
728-
* @param null|string $filename
702+
* @param null|string $filename
729703
* @return array|bool|Attachment[]
730704
*/
731705
public function getAttachments($filename = null)
732706
{
733-
if (!isset($this->attachments) || count($this->attachments) < 1) {
707+
if (!isset($this->attachments) || count($this->attachments) < 1)
734708
return false;
735-
}
736709

737-
if (!isset($filename)) {
710+
if (!isset($filename))
738711
return $this->attachments;
739-
}
740712

741713
$results = array();
742714
foreach ($this->attachments as $attachment) {
743-
if ($attachment->getFileName() == $filename) {
715+
if ($attachment->getFileName() == $filename)
744716
$results[] = $attachment;
745-
}
746717
}
747718

748719
switch (count($results)) {
@@ -772,8 +743,8 @@ public function checkFlag($flag = self::FLAG_FLAGGED)
772743
/**
773744
* This function is used to enable or disable one or more flags on the imap message.
774745
*
775-
* @param string|array $flag Flagged, Answered, Deleted, Seen, Draft
776-
* @param bool $enable
746+
* @param string|array $flag Flagged, Answered, Deleted, Seen, Draft
747+
* @param bool $enable
777748
* @throws \InvalidArgumentException
778749
* @return bool
779750
*/
@@ -783,9 +754,8 @@ public function setFlag($flag, $enable = true)
783754

784755
foreach ($flags as $i => $flag) {
785756
$flag = ltrim(strtolower($flag), '\\');
786-
if (!in_array($flag, self::$flagTypes) || $flag == self::FLAG_RECENT) {
757+
if (!in_array($flag, self::$flagTypes) || $flag == self::FLAG_RECENT)
787758
throw new \InvalidArgumentException('Unable to set invalid flag "' . $flag . '"');
788-
}
789759

790760
if ($enable) {
791761
$this->status[$flag] = true;
@@ -796,7 +766,7 @@ public function setFlag($flag, $enable = true)
796766
$flags[$i] = $flag;
797767
}
798768

799-
$imapifiedFlag = '\\' . implode(' \\', array_map('ucfirst', $flags));
769+
$imapifiedFlag = '\\'.implode(' \\', array_map('ucfirst', $flags));
800770

801771
if ($enable === true) {
802772
return imap_setflag_full($this->imapStream, $this->uid, $imapifiedFlag, ST_UID);

0 commit comments

Comments
 (0)