PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/program/include/rcube_message_header.php

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