[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright 2011-2014 Horde LLC (http://www.horde.org/) 4 * 5 * See the enclosed file COPYING for license information (LGPL). If you 6 * did not receive this file, see http://www.horde.org/licenses/lgpl21. 7 * 8 * @category Horde 9 * @copyright 2011-2014 Horde LLC 10 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 11 * @package Imap_Client 12 */ 13 14 /** 15 * Object containing data returned by the Horde_Imap_Client_Base#fetch() 16 * command. 17 * 18 * @author Michael Slusarz <slusarz@horde.org> 19 * @category Horde 20 * @copyright 2011-2014 Horde LLC 21 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 22 * @package Imap_Client 23 */ 24 class Horde_Imap_Client_Data_Fetch 25 { 26 /** Header formatting constants. */ 27 const HEADER_PARSE = 1; 28 const HEADER_STREAM = 2; 29 30 /** 31 * Internal data array. 32 * 33 * @var array 34 */ 35 protected $_data = array(); 36 37 /** 38 * Set the full message property. 39 * 40 * @param mixed $msg The full message text, as either a string or stream 41 * resource. 42 */ 43 public function setFullMsg($msg) 44 { 45 $this->_data[Horde_Imap_Client::FETCH_FULLMSG] = $msg; 46 } 47 48 /** 49 * Returns the full message. 50 * 51 * @param boolean $stream Return as a stream? 52 * 53 * @return mixed The full text of the entire message. 54 */ 55 public function getFullMsg($stream = false) 56 { 57 return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_FULLMSG]) ? $this->_data[Horde_Imap_Client::FETCH_FULLMSG] : null); 58 } 59 60 /** 61 * Set the message structure. 62 * 63 * @param Horde_Mime_Part $structure The base MIME part of the message. 64 */ 65 public function setStructure(Horde_Mime_Part $structure) 66 { 67 $this->_data[Horde_Imap_Client::FETCH_STRUCTURE] = $structure; 68 } 69 70 /** 71 * Get the message structure. 72 * 73 * @return Horde_Mime_Part $structure The base MIME part of the message. 74 */ 75 public function getStructure() 76 { 77 return isset($this->_data[Horde_Imap_Client::FETCH_STRUCTURE]) 78 ? clone $this->_data[Horde_Imap_Client::FETCH_STRUCTURE] 79 : new Horde_Mime_Part(); 80 } 81 82 /** 83 * Set a header entry. 84 * 85 * @param string $label The search label. 86 * @param mixed $data Either a Horde_Mime_Headers object or the raw 87 * header text. 88 */ 89 public function setHeaders($label, $data) 90 { 91 $this->_data[Horde_Imap_Client::FETCH_HEADERS][$label] = $data; 92 } 93 94 /** 95 * Get a header entry. 96 * 97 * @param string $label The search label. 98 * @param integer $format The return format. If self::HEADER_PARSE, 99 * returns a Horde_Mime_Headers object. If 100 * self::HEADER_STREAM, returns a stream. 101 * Otherwise, returns header text. 102 * 103 * @return mixed See $format. 104 */ 105 public function getHeaders($label, $format = 0) 106 { 107 return $this->_getHeaders($label, $format, Horde_Imap_Client::FETCH_HEADERS); 108 } 109 110 /** 111 * Set a header text entry. 112 * 113 * @param string $id The MIME ID. 114 * @param string $text The header text. 115 */ 116 public function setHeaderText($id, $text) 117 { 118 $this->_data[Horde_Imap_Client::FETCH_HEADERTEXT][$id] = $text; 119 } 120 121 /** 122 * Get a header text entry. 123 * 124 * @param string $id The MIME ID. 125 * @param integer $format The return format. If self::HEADER_PARSE, 126 * returns a Horde_Mime_Headers object. If 127 * self::HEADER_STREAM, returns a stream. 128 * Otherwise, returns header text. 129 * 130 * @return mixed See $format. 131 */ 132 public function getHeaderText($id = 0, $format = 0) 133 { 134 return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_HEADERTEXT); 135 } 136 137 /** 138 * Set a MIME header entry. 139 * 140 * @param string $id The MIME ID. 141 * @param string $text The header text. 142 */ 143 public function setMimeHeader($id, $text) 144 { 145 $this->_data[Horde_Imap_Client::FETCH_MIMEHEADER][$id] = $text; 146 } 147 148 /** 149 * Get a MIME header entry. 150 * 151 * @param string $id The MIME ID. 152 * @param integer $format The return format. If self::HEADER_PARSE, 153 * returns a Horde_Mime_Headers object. If 154 * self::HEADER_STREAM, returns a stream. 155 * Otherwise, returns header text. 156 * 157 * @return mixed See $format. 158 */ 159 public function getMimeHeader($id, $format = 0) 160 { 161 return $this->_getHeaders($id, $format, Horde_Imap_Client::FETCH_MIMEHEADER); 162 } 163 164 /** 165 * Set a body part entry. 166 * 167 * @param string $id The MIME ID. 168 * @param mixed $text The body part text, as either a string or stream 169 * resource. 170 * @param string $decode Either '8bit', 'binary', or null. 171 */ 172 public function setBodyPart($id, $text, $decode = null) 173 { 174 $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id] = array( 175 'd' => $decode, 176 't' => $text 177 ); 178 } 179 180 /** 181 * Set the body part size for a body part. 182 * 183 * @param string $id The MIME ID. 184 * @param integer $size The size (in bytes). 185 */ 186 public function setBodyPartSize($id, $size) 187 { 188 $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id] = intval($size); 189 } 190 191 /** 192 * Get a body part entry. 193 * 194 * @param string $id The MIME ID. 195 * @param boolean $stream Return as a stream? 196 * 197 * @return mixed The full text of the body part. 198 */ 199 public function getBodyPart($id, $stream = false) 200 { 201 return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['t'] : null); 202 } 203 204 /** 205 * Determines if/how a body part was MIME decoded on the server. 206 * 207 * @param string $id The MIME ID. 208 * 209 * @return string Either '8bit', 'binary', or null. 210 */ 211 public function getBodyPartDecode($id) 212 { 213 return isset($this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]) 214 ? $this->_data[Horde_Imap_Client::FETCH_BODYPART][$id]['d'] 215 : null; 216 } 217 218 /** 219 * Returns the body part size, if returned by the server. 220 * 221 * @param string $id The MIME ID. 222 * 223 * @return integer The body part size, in bytes. 224 */ 225 public function getBodyPartSize($id) 226 { 227 return isset($this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id]) 228 ? $this->_data[Horde_Imap_Client::FETCH_BODYPARTSIZE][$id] 229 : null; 230 } 231 232 /** 233 * Set a body text entry. 234 * 235 * @param string $id The MIME ID. 236 * @param mixed $text The body part text, as either a string or stream 237 * resource. 238 */ 239 public function setBodyText($id, $text) 240 { 241 $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] = $text; 242 } 243 244 /** 245 * Get a body text entry. 246 * 247 * @param string $id The MIME ID. 248 * @param boolean $stream Return as a stream? 249 * 250 * @return mixed The full text of the body text. 251 */ 252 public function getBodyText($id = 0, $stream = false) 253 { 254 return $this->_msgText($stream, isset($this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id]) ? $this->_data[Horde_Imap_Client::FETCH_BODYTEXT][$id] : null); 255 } 256 257 /** 258 * Set envelope data. 259 * 260 * @param array $data The envelope data to pass to the Envelope object 261 * constructor, or an Envelope object. 262 */ 263 public function setEnvelope($data) 264 { 265 $this->_data[Horde_Imap_Client::FETCH_ENVELOPE] = is_array($data) 266 ? new Horde_Imap_Client_Data_Envelope($data) 267 : $data; 268 } 269 270 /** 271 * Get envelope data. 272 * 273 * @return Horde_Imap_Client_Data_Envelope An envelope object. 274 */ 275 public function getEnvelope() 276 { 277 return isset($this->_data[Horde_Imap_Client::FETCH_ENVELOPE]) 278 ? clone $this->_data[Horde_Imap_Client::FETCH_ENVELOPE] 279 : new Horde_Imap_Client_Data_Envelope(); 280 } 281 282 /** 283 * Set IMAP flags. 284 * 285 * @param array $flags An array of IMAP flags. 286 */ 287 public function setFlags(array $flags) 288 { 289 $this->_data[Horde_Imap_Client::FETCH_FLAGS] = array_map('strtolower', $flags); 290 } 291 292 /** 293 * Get IMAP flags. 294 * 295 * @return array An array of IMAP flags (all flags in lowercase). 296 */ 297 public function getFlags() 298 { 299 return isset($this->_data[Horde_Imap_Client::FETCH_FLAGS]) 300 ? $this->_data[Horde_Imap_Client::FETCH_FLAGS] 301 : array(); 302 } 303 304 /** 305 * Set IMAP internal date. 306 * 307 * @param mixed $date Either a Horde_Imap_Client_DateTime object or a 308 * date string. 309 */ 310 public function setImapDate($date) 311 { 312 $this->_data[Horde_Imap_Client::FETCH_IMAPDATE] = is_object($date) 313 ? $date 314 : new Horde_Imap_Client_DateTime($date); 315 } 316 317 /** 318 * Get internal IMAP date. 319 * 320 * @return Horde_Imap_Client_DateTime A date object. 321 */ 322 public function getImapDate() 323 { 324 return isset($this->_data[Horde_Imap_Client::FETCH_IMAPDATE]) 325 ? clone $this->_data[Horde_Imap_Client::FETCH_IMAPDATE] 326 : new Horde_Imap_Client_DateTime(); 327 } 328 329 /** 330 * Set message size. 331 * 332 * @param integer $size The size of the message, in bytes. 333 */ 334 public function setSize($size) 335 { 336 $this->_data[Horde_Imap_Client::FETCH_SIZE] = intval($size); 337 } 338 339 /** 340 * Get message size. 341 * 342 * @return integer The size of the message, in bytes. 343 */ 344 public function getSize() 345 { 346 return isset($this->_data[Horde_Imap_Client::FETCH_SIZE]) 347 ? $this->_data[Horde_Imap_Client::FETCH_SIZE] 348 : 0; 349 } 350 351 /** 352 * Set UID. 353 * 354 * @param integer $uid The message UID. 355 */ 356 public function setUid($uid) 357 { 358 $this->_data[Horde_Imap_Client::FETCH_UID] = intval($uid); 359 } 360 361 /** 362 * Get UID. 363 * 364 * @return integer The message UID. 365 */ 366 public function getUid() 367 { 368 return isset($this->_data[Horde_Imap_Client::FETCH_UID]) 369 ? $this->_data[Horde_Imap_Client::FETCH_UID] 370 : null; 371 } 372 373 /** 374 * Set message sequence number. 375 * 376 * @param integer $seq The message sequence number. 377 */ 378 public function setSeq($seq) 379 { 380 $this->_data[Horde_Imap_Client::FETCH_SEQ] = intval($seq); 381 } 382 383 /** 384 * Get message sequence number. 385 * 386 * @return integer The message sequence number. 387 */ 388 public function getSeq() 389 { 390 return isset($this->_data[Horde_Imap_Client::FETCH_SEQ]) 391 ? $this->_data[Horde_Imap_Client::FETCH_SEQ] 392 : null; 393 } 394 395 /** 396 * Set the modified sequence value for the message. 397 * 398 * @param integer $modseq The modseq value. 399 */ 400 public function setModSeq($modseq) 401 { 402 $this->_data[Horde_Imap_Client::FETCH_MODSEQ] = intval($modseq); 403 } 404 405 /** 406 * Get the modified sequence value for the message. 407 * 408 * @return integer The modseq value. 409 */ 410 public function getModSeq() 411 { 412 return isset($this->_data[Horde_Imap_Client::FETCH_MODSEQ]) 413 ? $this->_data[Horde_Imap_Client::FETCH_MODSEQ] 414 : null; 415 } 416 417 /** 418 * Set the internationalized downgraded status for the message. 419 * 420 * @since 2.11.0 421 * 422 * @param boolean $downgraded True if at least one message component has 423 * been downgraded. 424 */ 425 public function setDowngraded($downgraded) 426 { 427 if ($downgraded) { 428 $this->_data[Horde_Imap_Client::FETCH_DOWNGRADED] = true; 429 } else { 430 unset($this->_data[Horde_Imap_Client::FETCH_DOWNGRADED]); 431 } 432 } 433 434 /** 435 * Does the message contain internationalized downgraded data (i.e. it 436 * is a "surrogate" message)? 437 * 438 * @since 2.11.0 439 * 440 * @return boolean True if at least one message components has been 441 * downgraded. 442 */ 443 public function isDowngraded() 444 { 445 return !empty($this->_data[Horde_Imap_Client::FETCH_DOWNGRADED]); 446 } 447 448 /** 449 * Return the internal representation of the data. 450 * 451 * @return array The data array. 452 */ 453 public function getRawData() 454 { 455 return $this->_data; 456 } 457 458 /** 459 * Merge a fetch object into this one. 460 * 461 * @param Horde_Imap_Client_Data_Fetch $data A fetch object. 462 */ 463 public function merge(Horde_Imap_Client_Data_Fetch $data) 464 { 465 $this->_data = array_replace_recursive($this->_data, $data->getRawData()); 466 } 467 468 /** 469 * Does this object containing cacheable data of the given type? 470 * 471 * @param integer $type The type to query. 472 * 473 * @return boolean True if the type is cacheable. 474 */ 475 public function exists($type) 476 { 477 return isset($this->_data[$type]); 478 } 479 480 /** 481 * Does this object contain only default values for all fields? 482 * 483 * @return boolean True if object contains default data. 484 */ 485 public function isDefault() 486 { 487 return empty($this->_data); 488 } 489 490 /** 491 * Return text representation of a field. 492 * 493 * @param boolean $stream Return as a stream? 494 * @param mixed $data The field data (string or resource) or null if 495 * field does not exist. 496 * 497 * @return mixed Requested text representation. 498 */ 499 protected function _msgText($stream, $data) 500 { 501 if ($stream) { 502 if (is_resource($data)) { 503 rewind($data); 504 return $data; 505 } 506 507 $tmp = fopen('php://temp', 'w+'); 508 509 if (!is_null($data)) { 510 fwrite($tmp, $data); 511 rewind($tmp); 512 } 513 514 return $tmp; 515 } 516 517 if (is_resource($data)) { 518 rewind($data); 519 return stream_get_contents($data); 520 } 521 522 return strval($data); 523 } 524 525 /** 526 * Return representation of a header field. 527 * 528 * @param string $id The header id. 529 * @param integer $format The return format. If self::HEADER_PARSE, 530 * returns a Horde_Mime_Headers object. If 531 * self::HEADER_STREAM, returns a stream. 532 * Otherwise, returns header text. 533 * @param integer $key The array key where the data is stored in the 534 * internal array. 535 * 536 * @return mixed The data in the format specified by $format. 537 */ 538 protected function _getHeaders($id, $format, $key) 539 { 540 switch ($format) { 541 case self::HEADER_STREAM: 542 if (!isset($this->_data[$key][$id])) { 543 return $this->_msgText(true, null); 544 } elseif (is_object($this->_data[$key][$id])) { 545 return $this->_getHeaders($id, 0, $key); 546 } 547 return $this->_msgText(true, $this->_data[$key][$id]); 548 549 case self::HEADER_PARSE: 550 if (!isset($this->_data[$key][$id])) { 551 return new Horde_Mime_Headers(); 552 } elseif (is_object($this->_data[$key][$id])) { 553 return clone $this->_data[$key][$id]; 554 } 555 return Horde_Mime_Headers::parseHeaders($this->_getHeaders($id, self::HEADER_STREAM, $key)); 556 } 557 558 if (!isset($this->_data[$key][$id])) { 559 return ''; 560 } 561 562 return is_object($this->_data[$key][$id]) 563 ? $this->_data[$key][$id]->toString(array('nowrap' => true)) 564 : $this->_msgText(false, $this->_data[$key][$id]); 565 } 566 567 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |