/imp/lib/Maillog.php

https://github.com/ewandor/horde · PHP · 266 lines · 156 code · 31 blank · 79 comment · 15 complexity · b37de3ec6888d9c29cdb080c23c3be4a MD5 · raw file

  1. <?php
  2. /**
  3. * This class contains all functions related to handling logging of responses
  4. * to individual e-mail messages.
  5. *
  6. * Copyright 2003-2012 Horde LLC (http://www.horde.org/)
  7. *
  8. * See the enclosed file COPYING for license information (GPL). If you
  9. * did not receive this file, see http://www.horde.org/licenses/gpl.
  10. *
  11. * @author Michael Slusarz <slusarz@horde.org>
  12. * @category Horde
  13. * @license http://www.horde.org/licenses/gpl GPL
  14. * @package IMP
  15. */
  16. class IMP_Maillog
  17. {
  18. /* Log Actions. */
  19. const FORWARD = 'forward';
  20. const MDN = 'mdn';
  21. const REDIRECT = 'redirect';
  22. const REPLY = 'reply';
  23. const REPLY_ALL = 'reply_all';
  24. const REPLY_LIST = 'reply_list';
  25. /**
  26. * Create a log entry.
  27. *
  28. * @param mixed $type Either an IMP_Compose:: constant or an
  29. * IMP_Maillog:: constant.
  30. * @param mixed $msg_ids Either a single Message-ID or an array of
  31. * Message-IDs to log.
  32. * @param string $data Any additional data to store. For forward and
  33. * redirect this is the list of recipients the
  34. * message was sent to. For mdn this is the
  35. * MDN-type of the message that was sent.
  36. */
  37. static public function log($type, $msg_ids, $data = null)
  38. {
  39. $history = $GLOBALS['injector']->getInstance('Horde_History');
  40. if (!is_array($msg_ids)) {
  41. $msg_ids = array($msg_ids);
  42. }
  43. foreach ($msg_ids as $val) {
  44. switch ($type) {
  45. case IMP_Compose::FORWARD:
  46. case IMP_Compose::FORWARD_ATTACH:
  47. case IMP_Compose::FORWARD_BODY:
  48. case IMP_Compose::FORWARD_BOTH:
  49. $params = array(
  50. 'action' => self::FORWARD,
  51. 'recipients' => $data
  52. );
  53. break;
  54. case self::MDN:
  55. $params = array(
  56. 'action' => self::MDN,
  57. 'type' => $data
  58. );
  59. break;
  60. case IMP_Compose::REDIRECT:
  61. $params = array(
  62. 'action' => self::REDIRECT,
  63. 'recipients' => $data
  64. );
  65. break;
  66. case IMP_Compose::REPLY:
  67. case IMP_Compose::REPLY_SENDER:
  68. $params = array(
  69. 'action' => self::REPLY
  70. );
  71. break;
  72. case IMP_Compose::REPLY_ALL:
  73. $params = array(
  74. 'action' => self::REPLY_ALL
  75. );
  76. break;
  77. case IMP_Compose::REPLY_LIST:
  78. $params = array(
  79. 'action' => self::REPLY_LIST
  80. );
  81. break;
  82. default:
  83. $params = null;
  84. break;
  85. }
  86. if ($params) {
  87. try {
  88. $history->log(self::_getUniqueHistoryId($val), $params);
  89. } catch (Exception $e) {
  90. /* On error, log the error message only since informing the
  91. * user is just a waste of time and a potential point of
  92. * confusion, especially since they most likely don't even
  93. * know the message is being logged. */
  94. $entry = sprintf('Could not log message details to Horde_History. Error returned: %s', $e->getMessage());
  95. Horde::logMessage($entry, 'ERR');
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * Retrieve any history for the given Message-ID.
  102. *
  103. * @param string $msg_id The Message-ID of the message.
  104. *
  105. * @return Horde_History_Log The object containing the log information.
  106. * @throws Horde_Exception
  107. */
  108. static public function getLog($msg_id)
  109. {
  110. return $GLOBALS['injector']->getInstance('Horde_History')->getHistory(self::_getUniqueHistoryId($msg_id));
  111. }
  112. /**
  113. * Determines if an MDN notification of a certain type has been sent
  114. * previously for this message-ID.
  115. *
  116. * @param string $msg_id The Message-ID of the message.
  117. * @param string $type The type of MDN.
  118. *
  119. * @return boolean True if a MDN has been sent for this message with
  120. * the given type.
  121. */
  122. static public function sentMDN($msg_id, $type)
  123. {
  124. try {
  125. $msg_history = self::getLog($msg_id);
  126. } catch (Horde_Exception $e) {
  127. return false;
  128. }
  129. if ($msg_history) {
  130. foreach ($msg_history as $entry) {
  131. if (($entry['action'] == 'mdn') && ($entry['type'] == $type)) {
  132. return true;
  133. }
  134. }
  135. }
  136. return false;
  137. }
  138. /**
  139. * Retrieve any history for the given Message-ID and (optionally) display
  140. * via the Horde notification system.
  141. *
  142. * @param string $msg_id The Message-ID of the message.
  143. */
  144. static public function displayLog($msg_id)
  145. {
  146. foreach (self::parseLog($msg_id) as $entry) {
  147. $GLOBALS['notification']->push($entry['msg'], 'imp.' . $entry['action']);
  148. }
  149. }
  150. /**
  151. * Returns log information for a message.
  152. *
  153. * @param string $msg_id The Message-ID of the message.
  154. *
  155. * @return array List of log information. Each element is an array with
  156. * the following keys:
  157. * - action: (string) The log action.
  158. * - msg: (string) The log message.
  159. */
  160. static public function parseLog($msg_id)
  161. {
  162. try {
  163. if (!$msg_history = self::getLog($msg_id)) {
  164. return array();
  165. }
  166. } catch (Horde_Exception $e) {
  167. return array();
  168. }
  169. $df = $GLOBALS['prefs']->getValue('date_format');
  170. $tf = $GLOBALS['prefs']->getValue('time_format');
  171. $ret = array();
  172. foreach ($msg_history as $entry) {
  173. $msg = null;
  174. if (isset($entry['desc'])) {
  175. $msg = $entry['desc'];
  176. } else {
  177. switch ($entry['action']) {
  178. case self::FORWARD:
  179. $msg = sprintf(_("You forwarded this message on %%s to: %s."), $entry['recipients']);
  180. break;
  181. case self::MDN:
  182. /* We don't display 'mdn' log entries. */
  183. break;
  184. case self::REDIRECT:
  185. $msg = sprintf(_("You redirected this message to %s on %%s."), $entry['recipients']);
  186. break;
  187. case self::REPLY:
  188. $msg = _("You replied to this message on %s.");
  189. break;
  190. case self::REPLY_ALL:
  191. $msg = _("You replied to all recipients of this message on %s.");
  192. break;
  193. case self::REPLY_LIST:
  194. $msg = _("You replied to this message via the mailing list on %s.");
  195. break;
  196. }
  197. }
  198. if ($msg) {
  199. $ret[] = array(
  200. 'action' => $entry['action'],
  201. 'msg' => @sprintf($msg, strftime($df . ' ' . $tf, $entry['ts']))
  202. );
  203. }
  204. }
  205. return $ret;
  206. }
  207. /**
  208. * Delete the log entries for a given list of Message-IDs.
  209. *
  210. * @param mixed $msg_ids Either a single Message-ID or an array
  211. * of Message-IDs to delete.
  212. */
  213. static public function deleteLog($msg_ids)
  214. {
  215. if (!is_array($msg_ids)) {
  216. $msg_ids = array($msg_ids);
  217. }
  218. $msg_ids = array_map(array('IMP_Maillog', '_getUniqueHistoryId'), $msg_ids);
  219. $GLOBALS['injector']->getInstance('Horde_History')->removeByNames($msg_ids);
  220. }
  221. /**
  222. * Generate the unique log ID for a forward/reply/redirect event.
  223. *
  224. * @param string $msgid The Message-ID of the original message.
  225. *
  226. * @return string The unique log ID to use with Horde_History::.
  227. */
  228. static protected function _getUniqueHistoryId($msgid)
  229. {
  230. if (is_array($msgid)) {
  231. return '';
  232. }
  233. return implode('.', array('imp', str_replace('.', '*', $GLOBALS['registry']->getAuth()), $msgid));
  234. }
  235. }