attachment_common.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * attachment_common.php
  4. *
  5. * This file provides the handling of often-used attachment types.
  6. *
  7. * @copyright 1999-2025 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. $attachment_common_show_images_list = array();
  14. /**
  15. * Mapping of file extensions to mime types
  16. *
  17. * Used for application/octet-stream mime type detection.
  18. * Supported extensions: bmp, gif, htm, html, jpg, jpeg, php,
  19. * png, rtf, txt, patch (since 1.4.2), vcf
  20. * @global array $FileExtensionToMimeType
  21. */
  22. $FileExtensionToMimeType = array('bmp' => 'image/x-bitmap',
  23. 'gif' => 'image/gif',
  24. 'htm' => 'text/html',
  25. 'html' => 'text/html',
  26. 'jpe' => 'image/jpeg',
  27. 'jpg' => 'image/jpeg',
  28. 'jpeg' => 'image/jpeg',
  29. 'php' => 'text/plain',
  30. 'png' => 'image/png',
  31. 'rtf' => 'text/richtext',
  32. 'txt' => 'text/plain',
  33. 'patch'=> 'text/plain',
  34. 'vcf' => 'text/x-vcard');
  35. /* Register browser-supported image types */
  36. sqgetGlobalVar('attachment_common_types', $attachment_common_types);
  37. // FIXME: do we use $attachment_common_types that is not extracted by sqgetGlobalVar() ?
  38. if (isset($attachment_common_types)) {
  39. // var is used to detect activation of jpeg image types
  40. unset($jpeg_done);
  41. /* Don't run this before being logged in. That may happen
  42. when plugins include mime.php */
  43. foreach ($attachment_common_types as $val => $v) {
  44. if ($val == 'image/gif')
  45. register_attachment_common('image/gif', 'link_image');
  46. elseif (($val == 'image/jpeg' || $val == 'image/pjpeg' || $val == 'image/jpg') and
  47. (!isset($jpeg_done))) {
  48. $jpeg_done = 1;
  49. register_attachment_common('image/jpg', 'link_image');
  50. register_attachment_common('image/jpeg', 'link_image');
  51. register_attachment_common('image/pjpeg', 'link_image');
  52. }
  53. elseif ($val == 'image/png')
  54. register_attachment_common('image/png', 'link_image');
  55. elseif ($val == 'image/x-xbitmap')
  56. register_attachment_common('image/x-xbitmap', 'link_image');
  57. elseif ($val == '*/*' || $val == 'image/*') {
  58. /**
  59. * browser (Firefox) declared that anything is acceptable.
  60. * Lets register some common image types.
  61. */
  62. if (! isset($jpeg_done)) {
  63. $jpeg_done = 1;
  64. register_attachment_common('image/jpg', 'link_image');
  65. register_attachment_common('image/jpeg', 'link_image');
  66. register_attachment_common('image/pjpeg', 'link_image');
  67. }
  68. register_attachment_common('image/gif', 'link_image');
  69. register_attachment_common('image/png', 'link_image');
  70. register_attachment_common('image/x-xbitmap', 'link_image');
  71. // register_attachment_common('image/x-ico', 'link_image');
  72. // register_attachment_common('image/x-icon', 'link_image');
  73. // register_attachment_common('image/bmp', 'link_image');
  74. // register_attachment_common('image/x-ms-bmp', 'link_image');
  75. }
  76. }
  77. unset($jpeg_done);
  78. }
  79. /* Register text-type attachments */
  80. register_attachment_common('message/rfc822', 'link_message');
  81. register_attachment_common('text/plain', 'link_text');
  82. register_attachment_common('text/richtext', 'link_text');
  83. /* Register HTML */
  84. register_attachment_common('text/html', 'link_html');
  85. /* Register vcards */
  86. register_attachment_common('text/x-vcard', 'link_vcard');
  87. register_attachment_common('text/directory', 'link_vcard');
  88. /* Register ics (calendar) */
  89. register_attachment_common('application/ics', 'link_text');
  90. /* Register rules for general types.
  91. * These will be used if there isn't a more specific rule available. */
  92. register_attachment_common('text/*', 'link_text');
  93. register_attachment_common('message/*', 'link_text');
  94. /* Register "unknown" attachments */
  95. register_attachment_common('application/octet-stream', 'octet_stream');
  96. /**
  97. * Function which optimizes readability of the above code, and also
  98. * ensures that the attachment_common code is exectuted before any
  99. * plugin, so that the latter may override the default processing.
  100. *
  101. * Registers 'attachment $type' hooks.
  102. *
  103. * @param string $type Attachment type
  104. * @param string $func Suffix of attachment_common_* function, which
  105. * handles $type attachments.
  106. *
  107. * @since 1.2.0
  108. */
  109. function register_attachment_common($type, $func) {
  110. global $squirrelmail_plugin_hooks;
  111. $plugin_type = 'attachment ' . $type;
  112. $fn = 'attachment_common_' . $func;
  113. if (!empty($squirrelmail_plugin_hooks[$plugin_type])) {
  114. $plugins = $squirrelmail_plugin_hooks[$plugin_type];
  115. $plugins = array_merge(array('attachment_common', $fn), $plugins);
  116. $squirrelmail_plugin_hooks[$plugin_type] = $plugins;
  117. } else {
  118. $squirrelmail_plugin_hooks[$plugin_type]['attachment_common'] = $fn;
  119. }
  120. }
  121. /**
  122. * Adds href and text keys to attachment_common array for text attachments
  123. * @param array $Args attachment $type hook arguments
  124. * @since 1.2.0
  125. */
  126. function attachment_common_link_text(&$Args) {
  127. global $base_uri;
  128. /* If there is a text attachment, we would like to create a "View" button
  129. that links to the text attachment viewer.
  130. $Args[0] = the array of actions
  131. Use the name of this file for adding an action
  132. $Args[0]['attachment_common'] = Array for href and text
  133. $Args[0]['attachment_common']['text'] = What is displayed
  134. $Args[0]['attachment_common']['href'] = Where it links to */
  135. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  136. // if sm_encode_html_special_chars() breaks something - find other way to encode & in url.
  137. $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. $QUERY_STRING;
  138. $Args[0]['attachment_common']['href'] =
  139. set_url_var($Args[0]['attachment_common']['href'],
  140. 'ent_id',$Args[4]);
  141. /* The link that we created needs a name. */
  142. $Args[0]['attachment_common']['text'] = _("View");
  143. /* Each attachment has a filename on the left, which is a link.
  144. Where that link points to can be changed. Just in case the link above
  145. for viewing text attachments is not the same as the default link for
  146. this file, we'll change it.
  147. This is a lot better in the image links, since the defaultLink will just
  148. download the image, but the one that we set it to will format the page
  149. to have an image tag in the center (looking a lot like this text viewer) */
  150. $Args[5] = $Args[0]['attachment_common']['href'];
  151. }
  152. /**
  153. * Adds href and text keys to attachment_common array for rfc822 attachments
  154. * @param array $Args attachment $type hook arguments
  155. * @since 1.2.6
  156. */
  157. function attachment_common_link_message(&$Args) {
  158. global $base_uri;
  159. $Args[0]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
  160. $Args[1] . '&amp;passed_id=' . $Args[2] . '&amp;mailbox=' . $Args[3] .
  161. '&amp;passed_ent_id=' . $Args[4] . '&amp;override_type0=message&amp;override_type1=rfc822';
  162. $Args[0]['attachment_common']['text'] = _("View");
  163. $Args[5] = $Args[0]['attachment_common']['href'];
  164. }
  165. /**
  166. * Adds href and text keys to attachment_common array for html attachments
  167. * @param array $Args attachment $type hook arguments
  168. * @since 1.2.0
  169. */
  170. function attachment_common_link_html(&$Args) {
  171. global $base_uri;
  172. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  173. $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. $QUERY_STRING.
  174. /* why use the overridetype? can this be removed */
  175. /* override_type might be needed only when we want view other type of messages as html */
  176. '&amp;override_type0=text&amp;override_type1=html';
  177. $Args[0]['attachment_common']['href'] =
  178. set_url_var($Args[0]['attachment_common']['href'],
  179. 'ent_id',$Args[4]);
  180. $Args[0]['attachment_common']['text'] = _("View");
  181. $Args[5] = $Args[0]['attachment_common']['href'];
  182. }
  183. /**
  184. * Adds href and text keys to attachment_common array for image attachments
  185. * @param array $Args attachment $type hook arguments
  186. * @since 1.2.0
  187. */
  188. function attachment_common_link_image(&$Args) {
  189. global $attachment_common_show_images_list, $base_uri ;
  190. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  191. $info['passed_id'] = $Args[2];
  192. $info['mailbox'] = $Args[3];
  193. $info['ent_id'] = $Args[4];
  194. $info['name'] = $Args[6];
  195. $info['download_href'] = isset($Args[0]['download link']) ? $Args[0]['download link']['href'] : '';
  196. $attachment_common_show_images_list[] = $info;
  197. $Args[0]['attachment_common']['href'] = $base_uri . 'src/image.php?'. $QUERY_STRING;
  198. $Args[0]['attachment_common']['href'] =
  199. set_url_var($Args[0]['attachment_common']['href'],
  200. 'ent_id',$Args[4]);
  201. $Args[0]['attachment_common']['text'] = _("View");
  202. $Args[5] = $Args[0]['attachment_common']['href'];
  203. }
  204. /**
  205. * Adds href and text keys to attachment_common array for vcard attachments
  206. * @param array $Args attachment $type hook arguments
  207. * @since 1.2.0
  208. */
  209. function attachment_common_link_vcard(&$Args) {
  210. global $base_uri;
  211. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  212. $Args[0]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. $QUERY_STRING;
  213. $Args[0]['attachment_common']['href'] =
  214. set_url_var($Args[0]['attachment_common']['href'],
  215. 'ent_id',$Args[4]);
  216. $Args[0]['attachment_common']['text'] = _("View Business Card");
  217. $Args[5] = $Args[0]['attachment_common']['href'];
  218. }
  219. /**
  220. * Processes octet-stream attachments.
  221. * Calls attachment_common-load_mime_types and attachment $type hooks.
  222. * @param array $Args attachment $type hook arguments
  223. * @since 1.2.0
  224. */
  225. function attachment_common_octet_stream(&$Args) {
  226. global $FileExtensionToMimeType, $null;
  227. //FIXME: I propose removing this hook; I don't like having two hooks close together, but moreover, this hook appears to merely give plugins the chance to add to the global $FileExtensionToMimeType variable, which they can do in any hook before now - I'd recommend prefs_backend (which is what config_override used to be) because it's the one hook run at the beginning of almost all page requests in init.php -- the con is that we don't need it run on ALL page requests, do we? There may be another hook in THIS page request that we can recommend, in which case, we *really should* remove this hook here.
  228. //FIXME: or at least we can move this hook up to the top of this file where $FileExtensionToMimeType is defined. What else is this hook here for? What plugins use it?
  229. do_hook('attachment_common-load_mime_types', $null);
  230. preg_match('/\.([^.]+)$/', $Args[6], $Regs);
  231. $Ext = '';
  232. if (is_array($Regs) && isset($Regs[1])) {
  233. $Ext = strtolower($Regs[1]);
  234. }
  235. if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
  236. return;
  237. $temp = array(&$Args[0], &$Args[1], &$Args[2], &$Args[3], &$Args[4], &$Args[5],
  238. &$Args[6], &$Args[7], &$Args[8]);
  239. do_hook('attachment ' . $FileExtensionToMimeType[$Ext], $temp);
  240. }