attachment_common.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * attachment_common.php
  4. *
  5. * This file provides the handling of often-used attachment types.
  6. *
  7. * @copyright &copy; 1999-2007 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 rules for general types.
  89. * These will be used if there isn't a more specific rule available. */
  90. register_attachment_common('text/*', 'link_text');
  91. register_attachment_common('message/*', 'link_text');
  92. /* Register "unknown" attachments */
  93. register_attachment_common('application/octet-stream', 'octet_stream');
  94. /**
  95. * Function which optimizes readability of the above code
  96. * Registers 'attachment $type' hooks.
  97. * @param string $type attachment type
  98. * @param string $func suffix of attachment_common_* function, which handles $type attachments.
  99. * @since 1.2.0
  100. */
  101. function register_attachment_common($type, $func) {
  102. global $squirrelmail_plugin_hooks;
  103. $squirrelmail_plugin_hooks['attachment ' . $type]['attachment_common'] =
  104. 'attachment_common_' . $func;
  105. }
  106. /**
  107. * Adds href and text keys to attachment_common array for text attachments
  108. * @param array $Args attachment $type hook arguments
  109. * @since 1.2.0
  110. */
  111. function attachment_common_link_text(&$Args) {
  112. global $base_uri;
  113. /* If there is a text attachment, we would like to create a "View" button
  114. that links to the text attachment viewer.
  115. $Args[0] = the array of actions
  116. Use the name of this file for adding an action
  117. $Args[0]['attachment_common'] = Array for href and text
  118. $Args[0]['attachment_common']['text'] = What is displayed
  119. $Args[0]['attachment_common']['href'] = Where it links to */
  120. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  121. // if htmlspecialchars() breaks something - find other way to encode & in url.
  122. $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING);
  123. $Args[0]['attachment_common']['href'] =
  124. set_url_var($Args[0]['attachment_common']['href'],
  125. 'ent_id',$Args[4]);
  126. /* The link that we created needs a name. */
  127. $Args[0]['attachment_common']['text'] = _("View");
  128. /* Each attachment has a filename on the left, which is a link.
  129. Where that link points to can be changed. Just in case the link above
  130. for viewing text attachments is not the same as the default link for
  131. this file, we'll change it.
  132. This is a lot better in the image links, since the defaultLink will just
  133. download the image, but the one that we set it to will format the page
  134. to have an image tag in the center (looking a lot like this text viewer) */
  135. $Args[5] = $Args[1]['attachment_common']['href'];
  136. }
  137. /**
  138. * Adds href and text keys to attachment_common array for rfc822 attachments
  139. * @param array $Args attachment $type hook arguments
  140. * @since 1.2.6
  141. */
  142. function attachment_common_link_message(&$Args) {
  143. global $base_uri;
  144. $Args[0]['attachment_common']['href'] = $base_uri . 'src/read_body.php?startMessage=' .
  145. $Args[1] . '&amp;passed_id=' . $Args[2] . '&amp;mailbox=' . $Args[3] .
  146. '&amp;passed_ent_id=' . $Args[4] . '&amp;override_type0=message&amp;override_type1=rfc822';
  147. $Args[0]['attachment_common']['text'] = _("View");
  148. $Args[5] = $Args[0]['attachment_common']['href'];
  149. }
  150. /**
  151. * Adds href and text keys to attachment_common array for html attachments
  152. * @param array $Args attachment $type hook arguments
  153. * @since 1.2.0
  154. */
  155. function attachment_common_link_html(&$Args) {
  156. global $base_uri;
  157. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  158. $Args[0]['attachment_common']['href'] = $base_uri . 'src/view_text.php?'. htmlspecialchars($QUERY_STRING).
  159. /* why use the overridetype? can this be removed */
  160. /* override_type might be needed only when we want view other type of messages as html */
  161. '&amp;override_type0=text&amp;override_type1=html';
  162. $Args[0]['attachment_common']['href'] =
  163. set_url_var($Args[0]['attachment_common']['href'],
  164. 'ent_id',$Args[4]);
  165. $Args[0]['attachment_common']['text'] = _("View");
  166. $Args[5] = $Args[0]['attachment_common']['href'];
  167. }
  168. /**
  169. * Adds href and text keys to attachment_common array for image attachments
  170. * @param array $Args attachment $type hook arguments
  171. * @since 1.2.0
  172. */
  173. function attachment_common_link_image(&$Args) {
  174. global $attachment_common_show_images_list, $base_uri ;
  175. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  176. $info['passed_id'] = $Args[2];
  177. $info['mailbox'] = $Args[3];
  178. $info['ent_id'] = $Args[4];
  179. $info['name'] = $Args[6];
  180. $info['download_href'] = isset($Args[0]['download link']) ? $Args[0]['download link']['href'] : '';
  181. $attachment_common_show_images_list[] = $info;
  182. $Args[0]['attachment_common']['href'] = $base_uri . 'src/image.php?'. htmlspecialchars($QUERY_STRING);
  183. $Args[0]['attachment_common']['href'] =
  184. set_url_var($Args[0]['attachment_common']['href'],
  185. 'ent_id',$Args[4]);
  186. $Args[0]['attachment_common']['text'] = _("View");
  187. $Args[5] = $Args[0]['attachment_common']['href'];
  188. }
  189. /**
  190. * Adds href and text keys to attachment_common array for vcard attachments
  191. * @param array $Args attachment $type hook arguments
  192. * @since 1.2.0
  193. */
  194. function attachment_common_link_vcard(&$Args) {
  195. global $base_uri;
  196. sqgetGlobalVar('QUERY_STRING', $QUERY_STRING, SQ_SERVER);
  197. $Args[0]['attachment_common']['href'] = $base_uri . 'src/vcard.php?'. htmlspecialchars($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 Business Card");
  202. $Args[5] = $Args[0]['attachment_common']['href'];
  203. }
  204. /**
  205. * Processes octet-stream attachments.
  206. * Calls attachment_common-load_mime_types and attachment $type hooks.
  207. * @param array $Args attachment $type hook arguments
  208. * @since 1.2.0
  209. */
  210. function attachment_common_octet_stream(&$Args) {
  211. global $FileExtensionToMimeType, $null;
  212. do_hook('attachment_common-load_mime_types', $null);
  213. ereg('\\.([^\\.]+)$', $Args[6], $Regs);
  214. $Ext = strtolower($Regs[1]);
  215. if ($Ext == '' || ! isset($FileExtensionToMimeType[$Ext]))
  216. return;
  217. do_hook('attachment ' . $FileExtensionToMimeType[$Ext],
  218. $temp=array(&$Args[0], &$Args[1], &$Args[2], &$Args[3], &$Args[4], &$Args[5],
  219. &$Args[6], &$Args[7], &$Args[8]));
  220. }