attachment_common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * attachment_common.php
  4. *
  5. * This file provides the handling of often-used attachment types.
  6. *
  7. * @copyright &copy; 1999-2005 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @todo document attachment $type hook arguments
  12. */
  13. /** @ignore */
  14. if (! defined('SM_PATH')) define('SM_PATH','../');
  15. /** sqgetGlobalVar() */
  16. include_once(SM_PATH . 'functions/global.php');
  17. /** sqm_baseuri() */
  18. include_once(SM_PATH . 'functions/display_messages.php');
  19. global $attachment_common_show_images_list;
  20. $attachment_common_show_images_list = array();
  21. global $FileExtensionToMimeType, $attachment_common_types;
  22. /**
  23. * Mapping of file extensions to mime types
  24. *
  25. * Used for application/octet-stream mime type detection.
  26. * Supported extensions: bmp, gif, htm, html, jpg, jpeg, php,
  27. * png, rtf, txt, patch (since 1.4.2), vcf
  28. * @global array $FileExtensionToMimeType
  29. */
  30. $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
  31. 'gif' => 'image/gif',
  32. 'htm' => 'text/html',
  33. 'html' => 'text/html',
  34. 'jpg' => 'image/jpeg',
  35. 'jpeg' => 'image/jpeg',
  36. 'php' => 'text/plain',
  37. 'png' => 'image/png',
  38. 'rtf' => 'text/richtext',
  39. 'txt' => 'text/plain',
  40. 'patch'=> 'text/plain',
  41. 'vcf' => 'text/x-vcard');
  42. /* Register browser-supported image types */
  43. sqgetGlobalVar('attachment_common_types', $attachment_common_types);
  44. // FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
  45. if (isset($attachment_common_types)) {
  46. // var is used to detect activation of jpeg image types
  47. unset($jpeg_done);
  48. /* Don't run this before being logged in. That may happen
  49. when plugins include mime.php */
  50. foreach ($attachment_common_types as $val => $v) {
  51. if ($val == 'image/gif')
  52. register_attachment_common('image/gif', 'link_image');
  53. elseif (($val == 'image/jpeg' || $val == 'image/pjpeg') and
  54. (!isset($jpeg_done))) {
  55. $jpeg_done = 1;
  56. register_attachment_common('image/jpeg', 'link_image');
  57. register_attachment_common('image/pjpeg', 'link_image');
  58. // register_attachment_common('image/jpg', 'link_image');
  59. }
  60. elseif ($val == 'image/png')
  61. register_attachment_common('image/png', 'link_image');
  62. elseif ($val == 'image/x-xbitmap')
  63. register_attachment_common('image/x-xbitmap', 'link_image');
  64. elseif ($val == '*/*' || $val == 'image/*') {
  65. /**
  66. * browser (Firefox) declared that anything is acceptable.
  67. * Lets register some common image types.
  68. */
  69. if (! isset($jpeg_done)) {
  70. $jpeg_done = 1;
  71. register_attachment_common('image/jpeg', 'link_image');
  72. register_attachment_common('image/pjpeg', 'link_image');
  73. // register_attachment_common('image/jpg', 'link_image');
  74. }
  75. register_attachment_common('image/gif', 'link_image');
  76. register_attachment_common('image/png', 'link_image');
  77. register_attachment_common('image/x-xbitmap', 'link_image');
  78. // register_attachment_common('image/x-ico', 'link_image');
  79. // register_attachment_common('image/x-icon', 'link_image');
  80. // register_attachment_common('image/bmp', 'link_image');
  81. // register_attachment_common('image/x-ms-bmp', 'link_image');
  82. }
  83. }
  84. unset($jpeg_done);
  85. }
  86. /* Register text-type attachments */
  87. register_attachment_common('message/rfc822', 'link_message');
  88. register_attachment_common('text/plain', 'link_text');
  89. register_attachment_common('text/richtext', 'link_text');
  90. /* Register HTML */
  91. register_attachment_common('text/html', 'link_html');
  92. /* Register vcards */
  93. register_attachment_common('text/x-vcard', 'link_vcard');
  94. register_attachment_common('text/directory', 'link_vcard');
  95. /* Register rules for general types.
  96. * These will be used if there isn't a more specific rule available. */
  97. register_attachment_common('text/*', 'link_text');
  98. register_attachment_common('message/*', 'link_text');
  99. /* Register "unknown" attachments */
  100. register_attachment_common('application/octet-stream', 'octet_stream');
  101. /**
  102. * Function which optimizes readability of the above code
  103. * Registers 'attachment $type' hooks.
  104. * @param string $type attachment type
  105. * @param string $func suffix of attachment_common_* function, which handles $type attachments.
  106. * @since 1.2.0
  107. */
  108. function register_attachment_common($type, $func) {
  109. global $squirrelmail_plugin_hooks;
  110. $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
  111. 'attachment_common_' . $func;
  112. }
  113. /**
  114. * Adds href and text keys to attachment_common array for text attachments
  115. * @param array $Args attachment $type hook arguments
  116. * @since 1.2.0
  117. */
  118. function attachment_common_link_text(&$Args) {
  119. /* If there is a text attachment, we would like to create a "View" button
  120. that links to the text attachment viewer.
  121. $Args[1] = the array of actions
  122. Use the name of this file for adding an action
  123. $Args[1]['attachment_common'] = Array for href and text
  124. $Args[1]['attachment_common']['text'] = What is displayed
  125. $Args[1]['attachment_common']['href'] = Where it links to */
  126. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  127. // if htmlspecialchars() breaks something - find other way to encode & in url.
  128. $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
  129. $Args[1]['attachment_common']['href'] =
  130. set_url_var($Args[1]['attachment_common']['href'],
  131. 'ent_id',$Args[5]);
  132. /* The link that we created needs a name. */
  133. $Args[1]['attachment_common']['text'] = _("View");
  134. /* Each attachment has a filename on the left, which is a link.
  135. Where that link points to can be changed. Just in case the link above
  136. for viewing text attachments is not the same as the default link for
  137. this file, we'll change it.
  138. This is a lot better in the image links, since the defaultLink will just
  139. download the image, but the one that we set it to will format the page
  140. to have an image tag in the center (looking a lot like this text viewer) */
  141. $Args[6] = $Args[1]['attachment_common']['href'];
  142. }
  143. /**
  144. * Adds href and text keys to attachment_common array for rfc822 attachments
  145. * @param array $Args attachment $type hook arguments
  146. * @since 1.2.6
  147. */
  148. function attachment_common_link_message(&$Args) {
  149. $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/read_body.php?startMessage=' .
  150. $Args[2] . '&amp;passed_id=' . $Args[3] . '&amp;mailbox=' . $Args[4] .
  151. '&amp;passed_ent_id=' . $Args[5] . '&amp;override_type0=message&amp;override_type1=rfc822';
  152. $Args[1]['attachment_common']['text'] = _("View");
  153. $Args[6] = $Args[1]['attachment_common']['href'];
  154. }
  155. /**
  156. * Adds href and text keys to attachment_common array for html attachments
  157. * @param array $Args attachment $type hook arguments
  158. * @since 1.2.0
  159. */
  160. function attachment_common_link_html(&$Args) {
  161. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  162. $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
  163. /* why use the overridetype? can this be removed */
  164. /* override_type might be needed only when we want view other type of messages as html */
  165. '&amp;override_type0=text&amp;override_type1=html';
  166. $Args[1]['attachment_common']['href'] =
  167. set_url_var($Args[1]['attachment_common']['href'],
  168. 'ent_id',$Args[5]);
  169. $Args[1]['attachment_common']['text'] = _("View");
  170. $Args[6] = $Args[1]['attachment_common']['href'];
  171. }
  172. /**
  173. * Adds href and text keys to attachment_common array for image attachments
  174. * @param array $Args attachment $type hook arguments
  175. * @since 1.2.0
  176. */
  177. function attachment_common_link_image(&$Args) {
  178. global $attachment_common_show_images_list;
  179. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  180. $info['passed_id'] = $Args[3];
  181. $info['mailbox'] = $Args[4];
  182. $info['ent_id'] = $Args[5];
  183. $attachment_common_show_images_list[] = $info;
  184. $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
  185. $Args[1]['attachment_common']['href'] =
  186. set_url_var($Args[1]['attachment_common']['href'],
  187. 'ent_id',$Args[5]);
  188. $Args[1]['attachment_common']['text'] = _("View");
  189. $Args[6] = $Args[1]['attachment_common']['href'];
  190. }
  191. /**
  192. * Adds href and text keys to attachment_common array for vcard attachments
  193. * @param array $Args attachment $type hook arguments
  194. * @since 1.2.0
  195. */
  196. function attachment_common_link_vcard(&$Args) {
  197. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  198. $Args[1]['attachment_common']['href'] = sqm_baseuri() . 'src/vcard.php?'. htmlspecialchars($QUERY_STRING);
  199. $Args[1]['attachment_common']['href'] =
  200. set_url_var($Args[1]['attachment_common']['href'],
  201. 'ent_id',$Args[5]);
  202. $Args[1]['attachment_common']['text'] = _("View Business Card");
  203. $Args[6] = $Args[1]['attachment_common']['href'];
  204. }
  205. /**
  206. * Processes octet-stream attachments.
  207. * Calls attachment_common-load_mime_types and attachment $type hooks.
  208. * @param array $Args attachment $type hook arguments
  209. * @since 1.2.0
  210. */
  211. function attachment_common_octet_stream(&$Args) {
  212. global $FileExtensionToMimeType;
  213. do_hook('attachment_common-load_mime_types');
  214. ereg('\\.([^\\.]+)$', $Args[7], $Regs);
  215. $Ext = strtolower($Regs[1]);
  216. if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
  217. return;
  218. $Ret = do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
  219. $Args[1], $Args[2], $Args[3], $Args[4], $Args[5], $Args[6],
  220. $Args[7], $Args[8], $Args[9]);
  221. foreach ($Ret as $a => $b) {
  222. $Args[$a] = $b;
  223. }
  224. }
  225. ?>