You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_message_header.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2012, The Roundcube Dev Team |
  6. | Copyright (C) 2011-2012, Kolab Systems AG |
  7. | |
  8. | Licensed under the GNU General Public License version 3 or |
  9. | any later version with exceptions for skins & plugins. |
  10. | See the README file for a full license statement. |
  11. | |
  12. | PURPOSE: |
  13. | E-mail message headers representation |
  14. +-----------------------------------------------------------------------+
  15. | Author: Aleksander Machniak <alec@alec.pl> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Struct representing an e-mail message header
  20. *
  21. * @package Framework
  22. * @subpackage Storage
  23. * @author Aleksander Machniak <alec@alec.pl>
  24. */
  25. class rcube_message_header
  26. {
  27. /**
  28. * Message sequence number
  29. *
  30. * @var int
  31. */
  32. public $id;
  33. /**
  34. * Message unique identifier
  35. *
  36. * @var int
  37. */
  38. public $uid;
  39. /**
  40. * Message subject
  41. *
  42. * @var string
  43. */
  44. public $subject;
  45. /**
  46. * Message sender (From)
  47. *
  48. * @var string
  49. */
  50. public $from;
  51. /**
  52. * Message recipient (To)
  53. *
  54. * @var string
  55. */
  56. public $to;
  57. /**
  58. * Message additional recipients (Cc)
  59. *
  60. * @var string
  61. */
  62. public $cc;
  63. /**
  64. * Message Reply-To header
  65. *
  66. * @var string
  67. */
  68. public $replyto;
  69. /**
  70. * Message In-Reply-To header
  71. *
  72. * @var string
  73. */
  74. public $in_reply_to;
  75. /**
  76. * Message date (Date)
  77. *
  78. * @var string
  79. */
  80. public $date;
  81. /**
  82. * Message identifier (Message-ID)
  83. *
  84. * @var string
  85. */
  86. public $messageID;
  87. /**
  88. * Message size
  89. *
  90. * @var int
  91. */
  92. public $size;
  93. /**
  94. * Message encoding
  95. *
  96. * @var string
  97. */
  98. public $encoding;
  99. /**
  100. * Message charset
  101. *
  102. * @var string
  103. */
  104. public $charset;
  105. /**
  106. * Message Content-type
  107. *
  108. * @var string
  109. */
  110. public $ctype;
  111. /**
  112. * Message timestamp (based on message date)
  113. *
  114. * @var int
  115. */
  116. public $timestamp;
  117. /**
  118. * IMAP bodystructure string
  119. *
  120. * @var string
  121. */
  122. public $bodystructure;
  123. /**
  124. * IMAP internal date
  125. *
  126. * @var string
  127. */
  128. public $internaldate;
  129. /**
  130. * Message References header
  131. *
  132. * @var string
  133. */
  134. public $references;
  135. /**
  136. * Message priority (X-Priority)
  137. *
  138. * @var int
  139. */
  140. public $priority;
  141. /**
  142. * Message receipt recipient
  143. *
  144. * @var string
  145. */
  146. public $mdn_to;
  147. /**
  148. * IMAP folder this message is stored in
  149. *
  150. * @var string
  151. */
  152. public $folder;
  153. /**
  154. * Other message headers
  155. *
  156. * @var array
  157. */
  158. public $others = array();
  159. /**
  160. * Message flags
  161. *
  162. * @var array
  163. */
  164. public $flags = array();
  165. // map header to rcube_message_header object property
  166. private $obj_headers = array(
  167. 'date' => 'date',
  168. 'from' => 'from',
  169. 'to' => 'to',
  170. 'subject' => 'subject',
  171. 'reply-to' => 'replyto',
  172. 'cc' => 'cc',
  173. 'bcc' => 'bcc',
  174. 'mbox' => 'folder',
  175. 'folder' => 'folder',
  176. 'content-transfer-encoding' => 'encoding',
  177. 'in-reply-to' => 'in_reply_to',
  178. 'content-type' => 'ctype',
  179. 'charset' => 'charset',
  180. 'references' => 'references',
  181. 'return-receipt-to' => 'mdn_to',
  182. 'disposition-notification-to' => 'mdn_to',
  183. 'x-confirm-reading-to' => 'mdn_to',
  184. 'message-id' => 'messageID',
  185. 'x-priority' => 'priority',
  186. );
  187. /**
  188. * Returns header value
  189. */
  190. public function get($name, $decode = true)
  191. {
  192. $name = strtolower($name);
  193. if (isset($this->obj_headers[$name])) {
  194. $value = $this->{$this->obj_headers[$name]};
  195. }
  196. else {
  197. $value = $this->others[$name];
  198. }
  199. if ($decode) {
  200. if (is_array($value)) {
  201. foreach ($value as $key => $val) {
  202. $val = rcube_mime::decode_header($val, $this->charset);
  203. $value[$key] = rcube_charset::clean($val);
  204. }
  205. }
  206. else {
  207. $value = rcube_mime::decode_header($value, $this->charset);
  208. $value = rcube_charset::clean($value);
  209. }
  210. }
  211. return $value;
  212. }
  213. /**
  214. * Sets header value
  215. */
  216. public function set($name, $value)
  217. {
  218. $name = strtolower($name);
  219. if (isset($this->obj_headers[$name])) {
  220. $this->{$this->obj_headers[$name]} = $value;
  221. }
  222. else {
  223. $this->others[$name] = $value;
  224. }
  225. }
  226. /**
  227. * Factory method to instantiate headers from a data array
  228. *
  229. * @param array Hash array with header values
  230. * @return object rcube_message_header instance filled with headers values
  231. */
  232. public static function from_array($arr)
  233. {
  234. $obj = new rcube_message_header;
  235. foreach ($arr as $k => $v)
  236. $obj->set($k, $v);
  237. return $obj;
  238. }
  239. }
  240. /**
  241. * Class for sorting an array of rcube_message_header objects in a predetermined order.
  242. *
  243. * @package Framework
  244. * @subpackage Storage
  245. * @author Aleksander Machniak <alec@alec.pl>
  246. */
  247. class rcube_message_header_sorter
  248. {
  249. private $uids = array();
  250. /**
  251. * Set the predetermined sort order.
  252. *
  253. * @param array $index Numerically indexed array of IMAP UIDs
  254. */
  255. function set_index($index)
  256. {
  257. $index = array_flip($index);
  258. $this->uids = $index;
  259. }
  260. /**
  261. * Sort the array of header objects
  262. *
  263. * @param array $headers Array of rcube_message_header objects indexed by UID
  264. */
  265. function sort_headers(&$headers)
  266. {
  267. uksort($headers, array($this, "compare_uids"));
  268. }
  269. /**
  270. * Sort method called by uksort()
  271. *
  272. * @param int $a Array key (UID)
  273. * @param int $b Array key (UID)
  274. */
  275. function compare_uids($a, $b)
  276. {
  277. // then find each sequence number in my ordered list
  278. $posa = isset($this->uids[$a]) ? intval($this->uids[$a]) : -1;
  279. $posb = isset($this->uids[$b]) ? intval($this->uids[$b]) : -1;
  280. // return the relative position as the comparison value
  281. return $posa - $posb;
  282. }
  283. }