attachment_common.php 10 KB

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