You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/mail/func.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2005-2014, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Provide webmail functionality and GUI objects |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. | Author: Aleksander Machniak <alec@alec.pl> |
  19. +-----------------------------------------------------------------------+
  20. */
  21. // always instantiate storage object (but not connect to server yet)
  22. $RCMAIL->storage_init();
  23. // init environment - set current folder, page, list mode
  24. rcmail_init_env();
  25. // set message set for search result
  26. if (!empty($_REQUEST['_search']) && isset($_SESSION['search'])
  27. && $_SESSION['search_request'] == $_REQUEST['_search']
  28. ) {
  29. $RCMAIL->storage->set_search_set($_SESSION['search']);
  30. $OUTPUT->set_env('search_request', $_REQUEST['_search']);
  31. $OUTPUT->set_env('search_text', $_SESSION['last_text_search']);
  32. }
  33. // remove mbox part from _uid
  34. if (($_uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_GPC)) && !is_array($_uid) && preg_match('/^\d+-.+/', $_uid)) {
  35. list($_uid, $mbox) = explode('-', $_uid, 2);
  36. if (isset($_GET['_uid'])) $_GET['_uid'] = $_uid;
  37. if (isset($_POST['_uid'])) $_POST['_uid'] = $_uid;
  38. $_REQUEST['_uid'] = $_uid;
  39. unset($_uid);
  40. // override mbox
  41. if (!empty($mbox)) {
  42. $_GET['_mbox'] = $mbox;
  43. $_POST['_mbox'] = $mbox;
  44. $RCMAIL->storage->set_folder(($_SESSION['mbox'] = $mbox));
  45. }
  46. }
  47. if (!empty($_SESSION['browser_caps']) && !$OUTPUT->ajax_call) {
  48. $OUTPUT->set_env('browser_capabilities', $_SESSION['browser_caps']);
  49. }
  50. // set main env variables, labels and page title
  51. if (empty($RCMAIL->action) || $RCMAIL->action == 'list') {
  52. // connect to storage server and trigger error on failure
  53. $RCMAIL->storage_connect();
  54. $mbox_name = $RCMAIL->storage->get_folder();
  55. if (empty($RCMAIL->action)) {
  56. $OUTPUT->set_env('search_mods', rcmail_search_mods());
  57. if (!empty($_SESSION['search_scope']))
  58. $OUTPUT->set_env('search_scope', $_SESSION['search_scope']);
  59. rcmail_list_pagetitle();
  60. }
  61. $threading = (bool) $RCMAIL->storage->get_threading();
  62. $delimiter = $RCMAIL->storage->get_hierarchy_delimiter();
  63. // set current mailbox and some other vars in client environment
  64. $OUTPUT->set_env('mailbox', $mbox_name);
  65. $OUTPUT->set_env('pagesize', $RCMAIL->storage->get_pagesize());
  66. $OUTPUT->set_env('current_page', max(1, $_SESSION['page']));
  67. $OUTPUT->set_env('delimiter', $delimiter);
  68. $OUTPUT->set_env('threading', $threading);
  69. $OUTPUT->set_env('threads', $threading || $RCMAIL->storage->get_capability('THREAD'));
  70. $OUTPUT->set_env('reply_all_mode', (int) $RCMAIL->config->get('reply_all_mode'));
  71. $OUTPUT->set_env('layout', $RCMAIL->config->get('layout') ?: 'widescreen');
  72. if ($RCMAIL->storage->get_capability('QUOTA')) {
  73. $OUTPUT->set_env('quota', true);
  74. }
  75. // set special folders
  76. foreach (array('drafts', 'trash', 'junk') as $mbox) {
  77. if ($folder = $RCMAIL->config->get($mbox . '_mbox')) {
  78. $OUTPUT->set_env($mbox . '_mailbox', $folder);
  79. }
  80. }
  81. if (!empty($_GET['_uid'])) {
  82. $OUTPUT->set_env('list_uid', $_GET['_uid']);
  83. }
  84. // set configuration
  85. $RCMAIL->set_env_config(array('delete_junk', 'flag_for_deletion', 'read_when_deleted',
  86. 'skip_deleted', 'display_next', 'message_extwin', 'forward_attachment'));
  87. if (!$OUTPUT->ajax_call) {
  88. $OUTPUT->add_label('checkingmail', 'deletemessage', 'movemessagetotrash',
  89. 'movingmessage', 'copyingmessage', 'deletingmessage', 'markingmessage',
  90. 'copy', 'move', 'quota', 'replyall', 'replylist', 'stillsearching',
  91. 'flagged', 'unflagged', 'unread', 'deleted', 'replied', 'forwarded',
  92. 'priority', 'withattachment', 'fileuploaderror', 'mark', 'markallread',
  93. 'folders-cur', 'folders-sub', 'folders-all', 'cancel');
  94. }
  95. }
  96. // register UI objects
  97. $OUTPUT->add_handlers(array(
  98. 'mailboxlist' => array($RCMAIL, 'folder_list'),
  99. 'quotadisplay' => array($RCMAIL, 'quota_display'),
  100. 'messages' => 'rcmail_message_list',
  101. 'messagecountdisplay' => 'rcmail_messagecount_display',
  102. 'listmenulink' => 'rcmail_options_menu_link',
  103. 'mailboxname' => 'rcmail_mailbox_name_display',
  104. 'messageheaders' => 'rcmail_message_headers',
  105. 'messagefullheaders' => 'rcmail_message_full_headers',
  106. 'messagebody' => 'rcmail_message_body',
  107. 'messagecontentframe' => 'rcmail_messagecontent_frame',
  108. 'messageimportform' => 'rcmail_message_import_form',
  109. 'searchfilter' => 'rcmail_search_filter',
  110. 'searchinterval' => 'rcmail_search_interval',
  111. 'searchform' => array($OUTPUT, 'search_form'),
  112. ));
  113. // register action aliases
  114. $RCMAIL->register_action_map(array(
  115. 'refresh' => 'check_recent.inc',
  116. 'preview' => 'show.inc',
  117. 'print' => 'show.inc',
  118. 'move' => 'move_del.inc',
  119. 'delete' => 'move_del.inc',
  120. 'send' => 'sendmail.inc',
  121. 'expunge' => 'folders.inc',
  122. 'purge' => 'folders.inc',
  123. 'remove-attachment' => 'attachments.inc',
  124. 'rename-attachment' => 'attachments.inc',
  125. 'display-attachment' => 'attachments.inc',
  126. 'upload' => 'attachments.inc',
  127. 'group-expand' => 'autocomplete.inc',
  128. ));
  129. /**
  130. * Sets storage properties and session
  131. */
  132. function rcmail_init_env()
  133. {
  134. global $RCMAIL;
  135. $default_threading = $RCMAIL->config->get('default_list_mode', 'list') == 'threads';
  136. $a_threading = $RCMAIL->config->get('message_threading', array());
  137. $message_sort_col = $RCMAIL->config->get('message_sort_col');
  138. $message_sort_order = $RCMAIL->config->get('message_sort_order');
  139. // set imap properties and session vars
  140. if (!strlen($mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_GPC, true))) {
  141. $mbox = strlen($_SESSION['mbox']) ? $_SESSION['mbox'] : 'INBOX';
  142. }
  143. // we handle 'page' argument on 'list' and 'getunread' to prevent from
  144. // race condition and unintentional page overwrite in session
  145. if ($RCMAIL->action == 'list' || $RCMAIL->action == 'getunread') {
  146. if (!($page = intval($_GET['_page']))) {
  147. $page = $_SESSION['page'] ?: 1;
  148. }
  149. $_SESSION['page'] = $page;
  150. }
  151. $RCMAIL->storage->set_folder($_SESSION['mbox'] = $mbox);
  152. $RCMAIL->storage->set_page($_SESSION['page']);
  153. // set default sort col/order to session
  154. if (!isset($_SESSION['sort_col'])) {
  155. $_SESSION['sort_col'] = $message_sort_col ?: '';
  156. }
  157. if (!isset($_SESSION['sort_order'])) {
  158. $_SESSION['sort_order'] = strtoupper($message_sort_order) == 'ASC' ? 'ASC' : 'DESC';
  159. }
  160. // set threads mode
  161. if (isset($_GET['_threads'])) {
  162. if ($_GET['_threads']) {
  163. // re-set current page number when listing mode changes
  164. if (!$a_threading[$_SESSION['mbox']]) {
  165. $RCMAIL->storage->set_page($_SESSION['page'] = 1);
  166. }
  167. $a_threading[$_SESSION['mbox']] = true;
  168. }
  169. else {
  170. // re-set current page number when listing mode changes
  171. if ($a_threading[$_SESSION['mbox']]) {
  172. $RCMAIL->storage->set_page($_SESSION['page'] = 1);
  173. }
  174. $a_threading[$_SESSION['mbox']] = false;
  175. }
  176. $RCMAIL->user->save_prefs(array('message_threading' => $a_threading));
  177. }
  178. $threading = isset($a_threading[$_SESSION['mbox']]) ? $a_threading[$_SESSION['mbox']] : $default_threading;
  179. $RCMAIL->storage->set_threading($threading);
  180. }
  181. /**
  182. * Sets page title
  183. */
  184. function rcmail_list_pagetitle()
  185. {
  186. global $RCMAIL;
  187. if ($RCMAIL->output->get_env('search_request')) {
  188. $pagetitle = $RCMAIL->gettext('searchresult');
  189. }
  190. else {
  191. $mbox_name = $RCMAIL->output->get_env('mailbox') ?: $RCMAIL->storage->get_folder();
  192. $delimiter = $RCMAIL->storage->get_hierarchy_delimiter();
  193. $pagetitle = $RCMAIL->localize_foldername($mbox_name, true);
  194. $pagetitle = str_replace($delimiter, " \xC2\xBB ", $pagetitle);
  195. }
  196. $RCMAIL->output->set_pagetitle($pagetitle);
  197. }
  198. /**
  199. * Returns default search mods
  200. */
  201. function rcmail_search_mods()
  202. {
  203. global $RCMAIL;
  204. $mods = $RCMAIL->config->get('search_mods');
  205. if (empty($mods)) {
  206. $mods = array('*' => array('subject' => 1, 'from' => 1));
  207. foreach (array('sent', 'drafts') as $mbox) {
  208. if ($mbox = $RCMAIL->config->get($mbox . '_mbox')) {
  209. $mods[$mbox] = array('subject' => 1, 'to' => 1);
  210. }
  211. }
  212. }
  213. return $mods;
  214. }
  215. /**
  216. * Returns 'to' if current folder is configured Sent or Drafts
  217. * or their subfolders, otherwise returns 'from'.
  218. *
  219. * @return string Column name
  220. */
  221. function rcmail_message_list_smart_column_name()
  222. {
  223. global $RCMAIL;
  224. $delim = $RCMAIL->storage->get_hierarchy_delimiter();
  225. $mbox = $RCMAIL->output->get_env('mailbox') ?: $RCMAIL->storage->get_folder();
  226. $sent_mbox = $RCMAIL->config->get('sent_mbox');
  227. $drafts_mbox = $RCMAIL->config->get('drafts_mbox');
  228. if ((strpos($mbox.$delim, $sent_mbox.$delim) === 0 || strpos($mbox.$delim, $drafts_mbox.$delim) === 0)
  229. && strtoupper($mbox) != 'INBOX'
  230. ) {
  231. return 'to';
  232. }
  233. return 'from';
  234. }
  235. /**
  236. * Returns configured messages list sorting column name
  237. * The name is context-sensitive, which means if sorting is set to 'fromto'
  238. * it will return 'from' or 'to' according to current folder type.
  239. *
  240. * @return string Column name
  241. */
  242. function rcmail_sort_column()
  243. {
  244. global $RCMAIL;
  245. if (isset($_SESSION['sort_col'])) {
  246. $column = $_SESSION['sort_col'];
  247. }
  248. else {
  249. $column = $RCMAIL->config->get('message_sort_col');
  250. }
  251. // get name of smart From/To column in folder context
  252. if ($column == 'fromto') {
  253. $column = rcmail_message_list_smart_column_name();
  254. }
  255. return $column;
  256. }
  257. /**
  258. * Returns configured message list sorting order
  259. *
  260. * @return string Sorting order (ASC|DESC)
  261. */
  262. function rcmail_sort_order()
  263. {
  264. global $RCMAIL;
  265. if (isset($_SESSION['sort_order'])) {
  266. return $_SESSION['sort_order'];
  267. }
  268. return $RCMAIL->config->get('message_sort_order');
  269. }
  270. /**
  271. * return the message list as HTML table
  272. */
  273. function rcmail_message_list($attrib)
  274. {
  275. global $RCMAIL, $OUTPUT;
  276. // add some labels to client
  277. $OUTPUT->add_label('from', 'to');
  278. // add id to message list table if not specified
  279. if (!strlen($attrib['id'])) {
  280. $attrib['id'] = 'rcubemessagelist';
  281. }
  282. // define list of cols to be displayed based on parameter or config
  283. if (empty($attrib['columns'])) {
  284. $list_cols = $RCMAIL->config->get('list_cols');
  285. $a_show_cols = !empty($list_cols) && is_array($list_cols) ? $list_cols : array('subject');
  286. $OUTPUT->set_env('col_movable', !in_array('list_cols', (array)$RCMAIL->config->get('dont_override')));
  287. }
  288. else {
  289. $a_show_cols = preg_split('/[\s,;]+/', str_replace(array("'", '"'), '', $attrib['columns']));
  290. $attrib['columns'] = $a_show_cols;
  291. }
  292. // save some variables for use in ajax list
  293. $_SESSION['list_attrib'] = $attrib;
  294. // make sure 'threads' and 'subject' columns are present
  295. if (!in_array('subject', $a_show_cols))
  296. array_unshift($a_show_cols, 'subject');
  297. if (!in_array('threads', $a_show_cols))
  298. array_unshift($a_show_cols, 'threads');
  299. $listcols = $a_show_cols;
  300. // Widescreen layout uses hardcoded list of columns
  301. if ($RCMAIL->config->get('layout', 'widescreen') == 'widescreen') {
  302. $a_show_cols = array('threads', 'subject', 'fromto', 'date', 'flag', 'attachment');
  303. $listcols = $a_show_cols;
  304. array_shift($listcols);
  305. }
  306. // set client env
  307. $OUTPUT->add_gui_object('messagelist', $attrib['id']);
  308. $OUTPUT->set_env('autoexpand_threads', intval($RCMAIL->config->get('autoexpand_threads')));
  309. $OUTPUT->set_env('sort_col', $_SESSION['sort_col']);
  310. $OUTPUT->set_env('sort_order', $_SESSION['sort_order']);
  311. $OUTPUT->set_env('messages', array());
  312. $OUTPUT->set_env('listcols', $listcols);
  313. $OUTPUT->include_script('list.js');
  314. $table = new html_table($attrib);
  315. if (!$attrib['noheader']) {
  316. foreach (rcmail_message_list_head($attrib, $a_show_cols) as $cell)
  317. $table->add_header(array('class' => $cell['className'], 'id' => $cell['id']), $cell['html']);
  318. }
  319. return $table->show();
  320. }
  321. /**
  322. * return javascript commands to add rows to the message list
  323. */
  324. function rcmail_js_message_list($a_headers, $insert_top=false, $a_show_cols=null)
  325. {
  326. global $RCMAIL, $OUTPUT;
  327. if (empty($a_show_cols)) {
  328. if (!empty($_SESSION['list_attrib']['columns']))
  329. $a_show_cols = $_SESSION['list_attrib']['columns'];
  330. else {
  331. $list_cols = $RCMAIL->config->get('list_cols');
  332. $a_show_cols = !empty($list_cols) && is_array($list_cols) ? $list_cols : array('subject');
  333. }
  334. }
  335. else {
  336. if (!is_array($a_show_cols)) {
  337. $a_show_cols = preg_split('/[\s,;]+/', str_replace(array("'", '"'), '', $a_show_cols));
  338. }
  339. $head_replace = true;
  340. }
  341. $delimiter = $RCMAIL->storage->get_hierarchy_delimiter();
  342. $search_set = $RCMAIL->storage->get_search_set();
  343. $multifolder = $search_set && $search_set[1]->multi;
  344. // add/remove 'folder' column to the list on multi-folder searches
  345. if ($multifolder && !in_array('folder', $a_show_cols)) {
  346. $a_show_cols[] = 'folder';
  347. $head_replace = true;
  348. }
  349. else if (!$multifolder && ($found = array_search('folder', $a_show_cols)) !== false) {
  350. unset($a_show_cols[$found]);
  351. $head_replace = true;
  352. }
  353. $mbox = $RCMAIL->output->get_env('mailbox') ?: $RCMAIL->storage->get_folder();
  354. // make sure 'threads' and 'subject' columns are present
  355. if (!in_array('subject', $a_show_cols))
  356. array_unshift($a_show_cols, 'subject');
  357. if (!in_array('threads', $a_show_cols))
  358. array_unshift($a_show_cols, 'threads');
  359. // Make sure there are no duplicated columns (#1486999)
  360. $a_show_cols = array_unique($a_show_cols);
  361. $_SESSION['list_attrib']['columns'] = $a_show_cols;
  362. // Widescreen layout uses hardcoded list of columns
  363. if ($RCMAIL->config->get('layout', 'widescreen') == 'widescreen') {
  364. $a_show_cols = array('threads', 'subject', 'fromto', 'date', 'flag', 'attachment');
  365. }
  366. // Plugins may set header's list_cols/list_flags and other rcube_message_header variables
  367. // and list columns
  368. $plugin = $RCMAIL->plugins->exec_hook('messages_list',
  369. array('messages' => $a_headers, 'cols' => $a_show_cols));
  370. $a_show_cols = $plugin['cols'];
  371. $a_headers = $plugin['messages'];
  372. if ($RCMAIL->config->get('layout', 'widescreen') == 'widescreen') {
  373. if (!$RCMAIL->storage->get_threading()) {
  374. if (($idx = array_search('threads', $a_show_cols)) !== false) {
  375. unset($a_show_cols[$idx]);
  376. }
  377. }
  378. }
  379. $thead = $head_replace ? rcmail_message_list_head($_SESSION['list_attrib'], $a_show_cols) : NULL;
  380. // get name of smart From/To column in folder context
  381. if (array_search('fromto', $a_show_cols) !== false) {
  382. $smart_col = rcmail_message_list_smart_column_name();
  383. }
  384. $OUTPUT->command('set_message_coltypes', array_values($a_show_cols), $thead, $smart_col);
  385. if ($multifolder && $_SESSION['search_scope'] == 'all') {
  386. $OUTPUT->command('select_folder', '');
  387. }
  388. $OUTPUT->set_env('multifolder_listing', $multifolder);
  389. if (empty($a_headers)) {
  390. return;
  391. }
  392. // remove 'threads', 'attachment', 'flag', 'status' columns, we don't need them here
  393. foreach (array('threads', 'attachment', 'flag', 'status', 'priority') as $col) {
  394. if (($key = array_search($col, $a_show_cols)) !== FALSE) {
  395. unset($a_show_cols[$key]);
  396. }
  397. }
  398. $sort_col = $_SESSION['sort_col'];
  399. // loop through message headers
  400. foreach ($a_headers as $header) {
  401. if (empty($header) || !$header->size) {
  402. continue;
  403. }
  404. // make message UIDs unique by appending the folder name
  405. if ($multifolder) {
  406. $header->uid .= '-'.$header->folder;
  407. $header->flags['skip_mbox_check'] = true;
  408. if ($header->parent_uid)
  409. $header->parent_uid .= '-'.$header->folder;
  410. }
  411. $a_msg_cols = array();
  412. $a_msg_flags = array();
  413. // format each col; similar as in rcmail_message_list()
  414. foreach ($a_show_cols as $col) {
  415. $col_name = $col == 'fromto' ? $smart_col : $col;
  416. if (in_array($col_name, array('from', 'to', 'cc', 'replyto'))) {
  417. $cont = rcmail_address_string($header->$col_name, 3, false, null, $header->charset);
  418. if (empty($cont)) $cont = '&nbsp;'; // for widescreen mode
  419. }
  420. else if ($col == 'subject') {
  421. $cont = trim(rcube_mime::decode_header($header->$col, $header->charset));
  422. if (!$cont) $cont = $RCMAIL->gettext('nosubject');
  423. $cont = rcube::Q($cont);
  424. }
  425. else if ($col == 'size')
  426. $cont = $RCMAIL->show_bytes($header->$col);
  427. else if ($col == 'date')
  428. $cont = $RCMAIL->format_date($sort_col == 'arrival' ? $header->internaldate : $header->date);
  429. else if ($col == 'folder') {
  430. if ($last_folder !== $header->folder) {
  431. $last_folder = $header->folder;
  432. $last_folder_name = $RCMAIL->localize_foldername($last_folder, true);
  433. $last_folder_name = str_replace($delimiter, " \xC2\xBB ", $last_folder_name);
  434. }
  435. $cont = rcube::Q($last_folder_name);
  436. }
  437. else
  438. $cont = rcube::Q($header->$col);
  439. $a_msg_cols[$col] = $cont;
  440. }
  441. $a_msg_flags = array_change_key_case(array_map('intval', (array) $header->flags));
  442. if ($header->depth)
  443. $a_msg_flags['depth'] = $header->depth;
  444. else if ($header->has_children)
  445. $roots[] = $header->uid;
  446. if ($header->parent_uid)
  447. $a_msg_flags['parent_uid'] = $header->parent_uid;
  448. if ($header->has_children)
  449. $a_msg_flags['has_children'] = $header->has_children;
  450. if ($header->unread_children)
  451. $a_msg_flags['unread_children'] = $header->unread_children;
  452. if ($header->flagged_children)
  453. $a_msg_flags['flagged_children'] = $header->flagged_children;
  454. if ($header->others['list-post'])
  455. $a_msg_flags['ml'] = 1;
  456. if ($header->priority)
  457. $a_msg_flags['prio'] = (int) $header->priority;
  458. $a_msg_flags['ctype'] = rcube::Q($header->ctype);
  459. $a_msg_flags['mbox'] = $header->folder;
  460. // merge with plugin result (Deprecated, use $header->flags)
  461. if (!empty($header->list_flags) && is_array($header->list_flags))
  462. $a_msg_flags = array_merge($a_msg_flags, $header->list_flags);
  463. if (!empty($header->list_cols) && is_array($header->list_cols))
  464. $a_msg_cols = array_merge($a_msg_cols, $header->list_cols);
  465. $OUTPUT->command('add_message_row', $header->uid, $a_msg_cols, $a_msg_flags, $insert_top);
  466. }
  467. if ($RCMAIL->storage->get_threading()) {
  468. $OUTPUT->command('init_threads', (array) $roots, $mbox);
  469. }
  470. }
  471. /*
  472. * Creates <THEAD> for message list table
  473. */
  474. function rcmail_message_list_head($attrib, $a_show_cols)
  475. {
  476. global $RCMAIL;
  477. // check to see if we have some settings for sorting
  478. $sort_col = $_SESSION['sort_col'];
  479. $sort_order = $_SESSION['sort_order'];
  480. $dont_override = (array) $RCMAIL->config->get('dont_override');
  481. $disabled_sort = in_array('message_sort_col', $dont_override);
  482. $disabled_order = in_array('message_sort_order', $dont_override);
  483. $RCMAIL->output->set_env('disabled_sort_col', $disabled_sort);
  484. $RCMAIL->output->set_env('disabled_sort_order', $disabled_order);
  485. // define sortable columns
  486. if ($disabled_sort)
  487. $a_sort_cols = $sort_col && !$disabled_order ? array($sort_col) : array();
  488. else
  489. $a_sort_cols = array('subject', 'date', 'from', 'to', 'fromto', 'size', 'cc');
  490. if (!empty($attrib['optionsmenuicon'])) {
  491. $list_menu = rcmail_options_menu_link($attrib);
  492. }
  493. $cells = $coltypes = array();
  494. // get name of smart From/To column in folder context
  495. if (array_search('fromto', $a_show_cols) !== false) {
  496. $smart_col = rcmail_message_list_smart_column_name();
  497. }
  498. foreach ($a_show_cols as $col) {
  499. $label = '';
  500. $sortable = false;
  501. $rel_col = $col == 'date' && $sort_col == 'arrival' ? 'arrival' : $col;
  502. // get column name
  503. switch ($col) {
  504. case 'flag':
  505. $col_name = html::span('flagged', $RCMAIL->gettext('flagged'));
  506. break;
  507. case 'attachment':
  508. case 'priority':
  509. $col_name = html::span($col, $RCMAIL->gettext($col));
  510. break;
  511. case 'status':
  512. $col_name = html::span($col, $RCMAIL->gettext('readstatus'));
  513. break;
  514. case 'threads':
  515. $col_name = (string) $list_menu;
  516. break;
  517. case 'fromto':
  518. $label = $RCMAIL->gettext($smart_col);
  519. $col_name = rcube::Q($label);
  520. break;
  521. default:
  522. $label = $RCMAIL->gettext($col);
  523. $col_name = rcube::Q($label);
  524. }
  525. // make sort links
  526. if (in_array($col, $a_sort_cols)) {
  527. $sortable = true;
  528. $col_name = html::a(array(
  529. 'href' => "./#sort",
  530. 'class' => 'sortcol',
  531. 'rel' => $rel_col,
  532. 'title' => $RCMAIL->gettext('sortby')
  533. ), $col_name);
  534. }
  535. else if ($col_name[0] != '<') {
  536. $col_name = '<span class="' . $col .'">' . $col_name . '</span>';
  537. }
  538. $sort_class = $rel_col == $sort_col && !$disabled_order ? " sorted$sort_order" : '';
  539. $class_name = $col.$sort_class;
  540. // put it all together
  541. $cells[] = array('className' => $class_name, 'id' => "rcm$col", 'html' => $col_name);
  542. $coltypes[$col] = array('className' => $class_name, 'id' => "rcm$col", 'label' => $label, 'sortable' => $sortable);
  543. }
  544. $RCMAIL->output->set_env('coltypes', $coltypes);
  545. return $cells;
  546. }
  547. function rcmail_options_menu_link($attrib)
  548. {
  549. global $RCMAIL;
  550. $onclick = 'return ' . rcmail_output::JS_OBJECT_NAME . ".command('menu-open', 'messagelistmenu', this, event)";
  551. $inner = $title = $RCMAIL->gettext('listoptions');
  552. if (is_string($attrib['optionsmenuicon']) && $attrib['optionsmenuicon'] != 'true') {
  553. $inner = html::img(array('src' => $RCMAIL->output->abs_url($attrib['optionsmenuicon'], true), 'alt' => $title));
  554. }
  555. return html::a(array(
  556. 'href' => '#list-options',
  557. 'onclick' => $onclick,
  558. 'class' => 'listmenu',
  559. 'id' => 'listmenulink',
  560. 'title' => $title,
  561. 'tabindex' => '0',
  562. ), $inner);
  563. }
  564. /**
  565. * Return an HTML iframe for loading mail content
  566. */
  567. function rcmail_messagecontent_frame($attrib)
  568. {
  569. global $OUTPUT;
  570. if (empty($attrib['id'])) {
  571. $attrib['id'] = 'rcmailcontentwindow';
  572. }
  573. return $OUTPUT->frame($attrib, true);
  574. }
  575. function rcmail_messagecount_display($attrib)
  576. {
  577. global $RCMAIL;
  578. if (!$attrib['id']) {
  579. $attrib['id'] = 'rcmcountdisplay';
  580. }
  581. $RCMAIL->output->add_gui_object('countdisplay', $attrib['id']);
  582. $content = $RCMAIL->action != 'show' ? rcmail_get_messagecount_text() : $RCMAIL->gettext('loading');
  583. return html::span($attrib, $content);
  584. }
  585. function rcmail_get_messagecount_text($count = null, $page = null)
  586. {
  587. global $RCMAIL;
  588. if ($page === null) {
  589. $page = $RCMAIL->storage->get_page();
  590. }
  591. $page_size = $RCMAIL->storage->get_pagesize();
  592. $start_msg = ($page-1) * $page_size + 1;
  593. $max = $count;
  594. if ($max === null && $RCMAIL->action) {
  595. $max = $RCMAIL->storage->count(null, $RCMAIL->storage->get_threading() ? 'THREADS' : 'ALL');
  596. }
  597. if (!$max) {
  598. $out = $RCMAIL->storage->get_search_set() ? $RCMAIL->gettext('nomessages') : $RCMAIL->gettext('mailboxempty');
  599. }
  600. else {
  601. $out = $RCMAIL->gettext(array('name' => $RCMAIL->storage->get_threading() ? 'threadsfromto' : 'messagesfromto',
  602. 'vars' => array('from' => $start_msg,
  603. 'to' => min($max, $start_msg + $page_size - 1),
  604. 'count' => $max)));
  605. }
  606. return rcube::Q($out);
  607. }
  608. function rcmail_mailbox_name_display($attrib)
  609. {
  610. global $RCMAIL;
  611. if (!$attrib['id']) {
  612. $attrib['id'] = 'rcmmailboxname';
  613. }
  614. $RCMAIL->output->add_gui_object('mailboxname', $attrib['id']);
  615. return html::span($attrib, rcmail_get_mailbox_name_text());
  616. }
  617. function rcmail_get_mailbox_name_text()
  618. {
  619. global $RCMAIL;
  620. return $RCMAIL->localize_foldername($RCMAIL->output->get_env('mailbox') ?: $RCMAIL->storage->get_folder());
  621. }
  622. function rcmail_send_unread_count($mbox_name, $force=false, $count=null, $mark='')
  623. {
  624. global $RCMAIL;
  625. $old_unseen = rcmail_get_unseen_count($mbox_name);
  626. $unseen = $count;
  627. if ($unseen === null) {
  628. $unseen = $RCMAIL->storage->count($mbox_name, 'UNSEEN', $force);
  629. }
  630. if ($unseen != $old_unseen || ($mbox_name == 'INBOX')) {
  631. $RCMAIL->output->command('set_unread_count', $mbox_name, $unseen,
  632. ($mbox_name == 'INBOX'), $unseen && $mark ? $mark : '');
  633. }
  634. rcmail_set_unseen_count($mbox_name, $unseen);
  635. return $unseen;
  636. }
  637. function rcmail_set_unseen_count($mbox_name, $count)
  638. {
  639. // @TODO: this data is doubled (session and cache tables) if caching is enabled
  640. // Make sure we have an array here (#1487066)
  641. if (!is_array($_SESSION['unseen_count'])) {
  642. $_SESSION['unseen_count'] = array();
  643. }
  644. $_SESSION['unseen_count'][$mbox_name] = $count;
  645. }
  646. function rcmail_get_unseen_count($mbox_name)
  647. {
  648. if (is_array($_SESSION['unseen_count']) && array_key_exists($mbox_name, $_SESSION['unseen_count'])) {
  649. return $_SESSION['unseen_count'][$mbox_name];
  650. }
  651. }
  652. /**
  653. * Sets message is_safe flag according to 'show_images' option value
  654. *
  655. * @param object rcube_message Message
  656. */
  657. function rcmail_check_safe($message)
  658. {
  659. global $RCMAIL;
  660. if (!$message->is_safe
  661. && ($show_images = $RCMAIL->config->get('show_images'))
  662. && $message->has_html_part()
  663. ) {
  664. switch ($show_images) {
  665. case 1: // known senders only
  666. // get default addressbook, like in addcontact.inc
  667. $CONTACTS = $RCMAIL->get_address_book(-1, true);
  668. if ($CONTACTS && $message->sender['mailto']) {
  669. $result = $CONTACTS->search('email', $message->sender['mailto'], 1, false);
  670. if ($result->count) {
  671. $message->set_safe(true);
  672. }
  673. }
  674. $RCMAIL->plugins->exec_hook('message_check_safe', array('message' => $message));
  675. break;
  676. case 2: // always
  677. $message->set_safe(true);
  678. break;
  679. }
  680. }
  681. return !empty($message->is_safe);
  682. }
  683. /**
  684. * Cleans up the given message HTML Body (for displaying)
  685. *
  686. * @param string HTML
  687. * @param array Display parameters
  688. * @param array CID map replaces (inline images)
  689. * @return string Clean HTML
  690. */
  691. function rcmail_wash_html($html, $p, $cid_replaces = array())
  692. {
  693. global $REMOTE_OBJECTS;
  694. $p += array('safe' => false, 'inline_html' => true);
  695. // charset was converted to UTF-8 in rcube_storage::get_message_part(),
  696. // change/add charset specification in HTML accordingly,
  697. // washtml cannot work without that
  698. $meta = '<meta http-equiv="Content-Type" content="text/html; charset='.RCUBE_CHARSET.'" />';
  699. // remove old meta tag and add the new one, making sure
  700. // that it is placed in the head (#1488093)
  701. $html = preg_replace('/<meta[^>]+charset=[a-z0-9-_]+[^>]*>/Ui', '', $html);
  702. $html = preg_replace('/(<head[^>]*>)/Ui', '\\1'.$meta, $html, -1, $rcount);
  703. if (!$rcount) {
  704. $html = '<head>' . $meta . '</head>' . $html;
  705. }
  706. // clean HTML with washhtml by Frederic Motte
  707. $wash_opts = array(
  708. 'show_washed' => false,
  709. 'allow_remote' => $p['safe'],
  710. 'blocked_src' => 'program/resources/blocked.gif',
  711. 'charset' => RCUBE_CHARSET,
  712. 'cid_map' => $cid_replaces,
  713. 'html_elements' => array('body'),
  714. );
  715. if (!$p['inline_html']) {
  716. $wash_opts['html_elements'] = array('html','head','title','body');
  717. }
  718. if ($p['safe']) {
  719. $wash_opts['html_elements'][] = 'link';
  720. $wash_opts['html_attribs'] = array('rel','type');
  721. }
  722. // overwrite washer options with options from plugins
  723. if (isset($p['html_elements']))
  724. $wash_opts['html_elements'] = $p['html_elements'];
  725. if (isset($p['html_attribs']))
  726. $wash_opts['html_attribs'] = $p['html_attribs'];
  727. // initialize HTML washer
  728. $washer = new rcube_washtml($wash_opts);
  729. if (!$p['skip_washer_form_callback']) {
  730. $washer->add_callback('form', 'rcmail_washtml_callback');
  731. }
  732. // allow CSS styles, will be sanitized by rcmail_washtml_callback()
  733. if (!$p['skip_washer_style_callback']) {
  734. $washer->add_callback('style', 'rcmail_washtml_callback');
  735. }
  736. // modify HTML links to open a new window if clicked
  737. if (!$p['skip_washer_link_callback']) {
  738. $washer->add_callback('a', 'rcmail_washtml_link_callback');
  739. $washer->add_callback('area', 'rcmail_washtml_link_callback');
  740. if ($p['safe'])
  741. $washer->add_callback('link', 'rcmail_washtml_link_callback');
  742. }
  743. // Remove non-UTF8 characters (#1487813)
  744. $html = rcube_charset::clean($html);
  745. $html = $washer->wash($html);
  746. $REMOTE_OBJECTS = $washer->extlinks;
  747. return $html;
  748. }
  749. /**
  750. * Convert the given message part to proper HTML
  751. * which can be displayed the message view
  752. *
  753. * @param string Message part body
  754. * @param rcube_message_part Message part
  755. * @param array Display parameters array
  756. *
  757. * @return string Formatted HTML string
  758. */
  759. function rcmail_print_body($body, $part, $p = array())
  760. {
  761. global $RCMAIL;
  762. // trigger plugin hook
  763. $data = $RCMAIL->plugins->exec_hook('message_part_before',
  764. array('type' => $part->ctype_secondary, 'body' => $body, 'id' => $part->mime_id)
  765. + $p + array('safe' => false, 'plain' => false, 'inline_html' => true));
  766. // convert html to text/plain
  767. if ($data['plain'] && ($data['type'] == 'html' || $data['type'] == 'enriched')) {
  768. if ($data['type'] == 'enriched') {
  769. $data['body'] = rcube_enriched::to_html($data['body']);
  770. }
  771. $body = $RCMAIL->html2text($data['body']);
  772. $part->ctype_secondary = 'plain';
  773. }
  774. // text/html
  775. else if ($data['type'] == 'html') {
  776. $body = rcmail_wash_html($data['body'], $data, $part->replaces);
  777. $part->ctype_secondary = $data['type'];
  778. }
  779. // text/enriched
  780. else if ($data['type'] == 'enriched') {
  781. $body = rcube_enriched::to_html($data['body']);
  782. $body = rcmail_wash_html($body, $data, $part->replaces);
  783. $part->ctype_secondary = 'html';
  784. }
  785. else {
  786. // assert plaintext
  787. $body = $data['body'];
  788. $part->ctype_secondary = $data['type'] = 'plain';
  789. }
  790. // free some memory (hopefully)
  791. unset($data['body']);
  792. // plaintext postprocessing
  793. if ($part->ctype_secondary == 'plain') {
  794. $flowed = $part->ctype_parameters['format'] == 'flowed';
  795. $delsp = $part->ctype_parameters['delsp'] == 'yes';
  796. $body = rcmail_plain_body($body, $flowed, $delsp);
  797. }
  798. // allow post-processing of the message body
  799. $data = $RCMAIL->plugins->exec_hook('message_part_after',
  800. array('type' => $part->ctype_secondary, 'body' => $body, 'id' => $part->mime_id) + $data);
  801. return $data['body'];
  802. }
  803. /**
  804. * Handle links and citation marks in plain text message
  805. *
  806. * @param string Plain text string
  807. * @param boolean Set to True if the source text is in format=flowed
  808. *
  809. * @return string Formatted HTML string
  810. */
  811. function rcmail_plain_body($body, $flowed = false, $delsp = false)
  812. {
  813. $options = array('flowed' => $flowed, 'wrap' => !$flowed, 'replacer' => 'rcmail_string_replacer',
  814. 'delsp' => $delsp);
  815. $text2html = new rcube_text2html($body, false, $options);
  816. $body = $text2html->get_html();
  817. return $body;
  818. }
  819. /**
  820. * Callback function for washtml cleaning class
  821. */
  822. function rcmail_washtml_callback($tagname, $attrib, $content, $washtml)
  823. {
  824. switch ($tagname) {
  825. case 'form':
  826. $out = html::div('form', $content);
  827. break;
  828. case 'style':
  829. // Crazy big styles may freeze the browser (#1490539)
  830. // remove content with more than 5k lines
  831. if (substr_count($content, "\n") > 5000) {
  832. $out = '';
  833. break;
  834. }
  835. // decode all escaped entities and reduce to ascii strings
  836. $stripped = preg_replace('/[^a-zA-Z\(:;]/', '', rcube_utils::xss_entity_decode($content));
  837. // now check for evil strings like expression, behavior or url()
  838. if (!preg_match('/expression|behavior|javascript:|import[^a]/i', $stripped)) {
  839. if (!$washtml->get_config('allow_remote') && preg_match('/url\((?!data:image)/', $stripped)) {
  840. $washtml->extlinks = true;
  841. }
  842. else {
  843. $out = html::tag('style', array('type' => 'text/css'), $content);
  844. }
  845. break;
  846. }
  847. default:
  848. $out = '';
  849. }
  850. return $out;
  851. }
  852. /**
  853. * return table with message headers
  854. */
  855. function rcmail_message_headers($attrib, $headers=null)
  856. {
  857. global $MESSAGE, $PRINT_MODE, $RCMAIL;
  858. static $sa_attrib;
  859. // keep header table attrib
  860. if (is_array($attrib) && !$sa_attrib && !$attrib['valueof']) {
  861. $sa_attrib = $attrib;
  862. }
  863. else if (!is_array($attrib) && is_array($sa_attrib)) {
  864. $attrib = $sa_attrib;
  865. }
  866. if (!isset($MESSAGE)) {
  867. return false;
  868. }
  869. // get associative array of headers object
  870. if (!$headers) {
  871. $headers_obj = $MESSAGE->headers;
  872. $headers = get_object_vars($MESSAGE->headers);
  873. }
  874. else if (is_object($headers)) {
  875. $headers_obj = $headers;
  876. $headers = get_object_vars($headers_obj);
  877. }
  878. else {
  879. $headers_obj = rcube_message_header::from_array($headers);
  880. }
  881. // show these headers
  882. $standard_headers = array('subject', 'from', 'sender', 'to', 'cc', 'bcc', 'replyto',
  883. 'mail-reply-to', 'mail-followup-to', 'date', 'priority');
  884. $exclude_headers = $attrib['exclude'] ? explode(',', $attrib['exclude']) : array();
  885. $output_headers = array();
  886. foreach ($standard_headers as $hkey) {
  887. if ($headers[$hkey])
  888. $value = $headers[$hkey];
  889. else if ($headers['others'][$hkey])
  890. $value = $headers['others'][$hkey];
  891. else if (!$attrib['valueof'])
  892. continue;
  893. if (in_array($hkey, $exclude_headers))
  894. continue;
  895. $ishtml = false;
  896. $header_title = $RCMAIL->gettext(preg_replace('/(^mail-|-)/', '', $hkey));
  897. if ($hkey == 'date') {
  898. if ($PRINT_MODE)
  899. $header_value = $RCMAIL->format_date($value, $RCMAIL->config->get('date_long', 'x'));
  900. else
  901. $header_value = $RCMAIL->format_date($value);
  902. }
  903. else if ($hkey == 'priority') {
  904. if ($value) {
  905. $header_value = html::span('prio' . $value, rcube::Q(rcmail_localized_priority($value)));
  906. $ishtml = true;
  907. }
  908. else {
  909. continue;
  910. }
  911. }
  912. else if ($hkey == 'replyto') {
  913. if ($headers['replyto'] != $headers['from']) {
  914. $header_value = rcmail_address_string($value, $attrib['max'], true,
  915. $attrib['addicon'], $headers['charset'], $header_title);
  916. $ishtml = true;
  917. }
  918. else {
  919. continue;
  920. }
  921. }
  922. else if ($hkey == 'mail-reply-to') {
  923. if ($headers['mail-replyto'] != $headers['replyto']
  924. && $headers['replyto'] != $headers['from']
  925. ) {
  926. $header_value = rcmail_address_string($value, $attrib['max'], true,
  927. $attrib['addicon'], $headers['charset'], $header_title);
  928. $ishtml = true;
  929. }
  930. else {
  931. continue;
  932. }
  933. }
  934. else if ($hkey == 'sender') {
  935. if ($headers['sender'] != $headers['from']) {
  936. $header_value = rcmail_address_string($value, $attrib['max'], true,
  937. $attrib['addicon'], $headers['charset'], $header_title);
  938. $ishtml = true;
  939. }
  940. else {
  941. continue;
  942. }
  943. }
  944. else if ($hkey == 'mail-followup-to') {
  945. $header_value = rcmail_address_string($value, $attrib['max'], true,
  946. $attrib['addicon'], $headers['charset'], $header_title);
  947. $ishtml = true;
  948. }
  949. else if (in_array($hkey, array('from', 'to', 'cc', 'bcc'))) {
  950. $header_value = rcmail_address_string($value, $attrib['max'], true,
  951. $attrib['addicon'], $headers['charset'], $header_title);
  952. $ishtml = true;
  953. }
  954. else if ($hkey == 'subject' && empty($value)) {
  955. $header_value = $RCMAIL->gettext('nosubject');
  956. }
  957. else {
  958. $value = is_array($value) ? implode(' ', $value) : $value;
  959. $header_value = trim(rcube_mime::decode_header($value, $headers['charset']));
  960. }
  961. $output_headers[$hkey] = array(
  962. 'title' => $header_title,
  963. 'value' => $header_value,
  964. 'raw' => $value,
  965. 'html' => $ishtml,
  966. );
  967. }
  968. $plugin = $RCMAIL->plugins->exec_hook('message_headers_output', array(
  969. 'output' => $output_headers,
  970. 'headers' => $headers_obj,
  971. 'exclude' => $exclude_headers, // readonly
  972. 'folder' => $MESSAGE->folder, // readonly
  973. 'uid' => $MESSAGE->uid, // readonly
  974. ));
  975. // single header value is requested
  976. if (!empty($attrib['valueof'])) {
  977. $row = $plugin['output'][$attrib['valueof']];
  978. return $row['html'] ? $row['value'] : rcube::Q($row['value']);
  979. }
  980. // compose html table
  981. $table = new html_table(array('cols' => 2));
  982. foreach ($plugin['output'] as $hkey => $row) {
  983. $val = $row['html'] ? $row['value'] : rcube::Q($row['value']);
  984. $table->add(array('class' => 'header-title'), rcube::Q($row['title']));
  985. $table->add(array('class' => 'header '.$hkey), $val);
  986. }
  987. return $table->show($attrib);
  988. }
  989. /**
  990. * Convert Priority header value into a localized string
  991. */
  992. function rcmail_localized_priority($value)
  993. {
  994. global $RCMAIL;
  995. $labels_map = array(
  996. '1' => 'highest',
  997. '2' => 'high',
  998. '3' => 'normal',
  999. '4' => 'low',
  1000. '5' => 'lowest',
  1001. );
  1002. if ($value && $labels_map[$value]) {
  1003. return $RCMAIL->gettext($labels_map[$value]);
  1004. }
  1005. return '';
  1006. }
  1007. /**
  1008. * return block to show full message headers
  1009. */
  1010. function rcmail_message_full_headers($attrib)
  1011. {
  1012. global $OUTPUT, $RCMAIL;
  1013. $html = html::div(array('id' => "all-headers", 'class' => "all", 'style' => 'display:none'), html::div(array('id' => 'headers-source'), ''));
  1014. $html .= html::div(array(
  1015. 'class' => "more-headers show-headers",
  1016. 'onclick' => "return ".rcmail_output::JS_OBJECT_NAME.".command('show-headers','',this)",
  1017. 'title' => $RCMAIL->gettext('togglefullheaders')
  1018. ), '');
  1019. $OUTPUT->add_gui_object('all_headers_row', 'all-headers');
  1020. $OUTPUT->add_gui_object('all_headers_box', 'headers-source');
  1021. return html::div($attrib, $html);
  1022. }
  1023. /**
  1024. * Handler for the 'messagebody' GUI object
  1025. *
  1026. * @param array Named parameters
  1027. * @return string HTML content showing the message body
  1028. */
  1029. function rcmail_message_body($attrib)
  1030. {
  1031. global $OUTPUT, $MESSAGE, $RCMAIL, $REMOTE_OBJECTS;
  1032. if (!is_array($MESSAGE->parts) && empty($MESSAGE->body)) {
  1033. return '';
  1034. }
  1035. if (!$attrib['id'])
  1036. $attrib['id'] = 'rcmailMsgBody';
  1037. $safe_mode = $MESSAGE->is_safe || intval($_GET['_safe']);
  1038. $out = '';
  1039. $part_no = 0;
  1040. $header_attrib = array();
  1041. foreach ($attrib as $attr => $value) {
  1042. if (preg_match('/^headertable([a-z]+)$/i', $attr, $regs)) {
  1043. $header_attrib[$regs[1]] = $value;
  1044. }
  1045. }
  1046. if (!empty($MESSAGE->parts)) {
  1047. foreach ($MESSAGE->parts as $part) {
  1048. if ($part->type == 'headers') {
  1049. $out .= html::div('message-partheaders', rcmail_message_headers(count($header_attrib) ? $header_attrib : null, $part->headers));
  1050. }
  1051. else if ($part->type == 'content') {
  1052. // unsupported (e.g. encrypted)
  1053. if ($part->realtype) {
  1054. if ($part->realtype == 'multipart/encrypted' || $part->realtype == 'application/pkcs7-mime') {
  1055. if (!empty($_SESSION['browser_caps']['pgpmime']) && ($pgp_mime_part = $MESSAGE->get_multipart_encrypted_part())) {
  1056. $out .= html::span('part-notice', $RCMAIL->gettext('externalmessagedecryption'));
  1057. $OUTPUT->set_env('pgp_mime_part', $pgp_mime_part->mime_id);
  1058. $OUTPUT->set_env('pgp_mime_container', '#' . $attrib['id']);
  1059. $OUTPUT->add_label('loadingdata');
  1060. }
  1061. if (!$MESSAGE->encrypted_part) {
  1062. $out .= html::span('part-notice', $RCMAIL->gettext('encryptedmessage'));
  1063. }
  1064. }
  1065. continue;
  1066. }
  1067. else if (!$part->size) {
  1068. continue;
  1069. }
  1070. // Check if we have enough memory to handle the message in it
  1071. // #1487424: we need up to 10x more memory than the body
  1072. else if (!rcube_utils::mem_check($part->size * 10)) {
  1073. $out .= html::span('part-notice', $RCMAIL->gettext('messagetoobig'). ' '
  1074. . html::a('?_task=mail&_action=get&_download=1&_uid='.$MESSAGE->uid.'&_part='.$part->mime_id
  1075. .'&_mbox='. urlencode($MESSAGE->folder), $RCMAIL->gettext('download')));
  1076. continue;
  1077. }
  1078. // fetch part body
  1079. $body = $MESSAGE->get_part_body($part->mime_id, true);
  1080. // message is cached but not exists (#1485443), or other error
  1081. if ($body === false) {
  1082. rcmail_message_error($MESSAGE->uid);
  1083. }
  1084. $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix',
  1085. array('part' => $part, 'prefix' => ''));
  1086. // Set attributes of the part container
  1087. $container_class = $part->ctype_secondary == 'html' ? 'message-htmlpart' : 'message-part';
  1088. $container_id = $container_class . (++$part_no);
  1089. $container_attrib = array('class' => $container_class, 'id' => $container_id);
  1090. // Assign container ID to a global variable for use in rcmail_washtml_link_callback()
  1091. $GLOBALS['rcmail_html_container_id'] = $container_id;
  1092. // Parse the part content for display
  1093. $body = rcmail_print_body($body, $part, array('safe' => $safe_mode, 'plain' => !$RCMAIL->config->get('prefer_html')));
  1094. // check if the message body is PGP encrypted
  1095. if (strpos($body, '-----BEGIN PGP MESSAGE-----') !== false) {
  1096. $OUTPUT->set_env('is_pgp_content', '#' . $container_id);
  1097. }
  1098. if ($part->ctype_secondary == 'html') {
  1099. $body = rcmail_html4inline($body, $container_id, 'rcmBody', $container_attrib, $safe_mode);
  1100. }
  1101. $out .= html::div($container_attrib, $plugin['prefix'] . $body);
  1102. }
  1103. }
  1104. }
  1105. else {
  1106. // Check if we have enough memory to handle the message in it
  1107. // #1487424: we need up to 10x more memory than the body
  1108. if (!rcube_utils::mem_check(strlen($MESSAGE->body) * 10)) {
  1109. $out .= html::span('part-notice', $RCMAIL->gettext('messagetoobig'). ' '
  1110. . html::a('?_task=mail&_action=get&_download=1&_uid='.$MESSAGE->uid.'&_part=0'
  1111. .'&_mbox='. urlencode($MESSAGE->folder), $RCMAIL->gettext('download')));
  1112. }
  1113. else {
  1114. $plugin = $RCMAIL->plugins->exec_hook('message_body_prefix',
  1115. array('part' => $MESSAGE, 'prefix' => ''));
  1116. $out .= html::div('message-part',
  1117. $plugin['prefix'] . rcmail_plain_body($MESSAGE->body));
  1118. }
  1119. }
  1120. // list images after mail body
  1121. if ($RCMAIL->config->get('inline_images', true) && !empty($MESSAGE->attachments)) {
  1122. $thumbnail_size = $RCMAIL->config->get('image_thumbnail_size', 240);
  1123. $client_mimetypes = (array)$RCMAIL->config->get('client_mimetypes');
  1124. foreach ($MESSAGE->attachments as $attach_prop) {
  1125. // skip inline images
  1126. if ($attach_prop->content_id && $attach_prop->disposition == 'inline') {
  1127. continue;
  1128. }
  1129. // Content-Type: image/*...
  1130. if ($mimetype = rcmail_part_image_type($attach_prop)) {
  1131. // display thumbnails
  1132. if ($thumbnail_size) {
  1133. $supported = in_array($mimetype, $client_mimetypes);
  1134. $show_link = array(
  1135. 'href' => $MESSAGE->get_part_url($attach_prop->mime_id, false),
  1136. 'onclick' => sprintf(
  1137. 'return %s.command(\'load-attachment\',\'%s\',this)',
  1138. rcmail_output::JS_OBJECT_NAME,
  1139. $attach_prop->mime_id)
  1140. );
  1141. $out .= html::p(array('class' => 'image-attachment', 'style' => $supported ? '' : 'display:none'),
  1142. html::a($show_link + array('class' => 'image-link', 'style' => sprintf('width:%dpx', $thumbnail_size)),
  1143. html::img(array(
  1144. 'class' => 'image-thumbnail',
  1145. 'src' => $MESSAGE->get_part_url($attach_prop->mime_id, 'image') . '&_thumb=1',
  1146. 'title' => $attach_prop->filename,
  1147. 'alt' => $attach_prop->filename,
  1148. 'style' => sprintf('max-width:%dpx; max-height:%dpx', $thumbnail_size, $thumbnail_size),
  1149. 'onload' => $supported ? '' : '$(this).parents(\'p.image-attachment\').show()',
  1150. ))
  1151. ) .
  1152. html::span('image-filename', rcube::Q($attach_prop->filename)) .
  1153. html::span('image-filesize', rcube::Q($RCMAIL->message_part_size($attach_prop))) .
  1154. html::span('attachment-links',
  1155. ($supported ? html::a($show_link, $RCMAIL->gettext('showattachment')) . '&nbsp;' : '') .
  1156. html::a($show_link['href'] . '&_download=1', $RCMAIL->gettext('download'))
  1157. ) .
  1158. html::br(array('style' => 'clear:both'))
  1159. );
  1160. }
  1161. else {
  1162. $out .= html::tag('fieldset', 'image-attachment',
  1163. html::tag('legend', 'image-filename', rcube::Q($attach_prop->filename)) .
  1164. html::p(array('align' => 'center'),
  1165. html::img(array(
  1166. 'src' => $MESSAGE->get_part_url($attach_prop->mime_id, 'image'),
  1167. 'title' => $attach_prop->filename,
  1168. 'alt' => $attach_prop->filename,
  1169. )))
  1170. );
  1171. }
  1172. }
  1173. }
  1174. }
  1175. // tell client that there are blocked remote objects
  1176. if ($REMOTE_OBJECTS && !$safe_mode) {
  1177. $OUTPUT->set_env('blockedobjects', true);
  1178. }
  1179. return html::div($attrib, $out);
  1180. }
  1181. function rcmail_part_image_type($part)
  1182. {
  1183. $mimetype = strtolower($part->mimetype);
  1184. // Skip TIFF/WEBP images if browser doesn't support this format
  1185. // ...until we can convert them to JPEG
  1186. $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tiff']);
  1187. $tiff_support = $tiff_support || rcube_image::is_convertable('image/tiff');
  1188. $webp_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['webp']);
  1189. $webp_support = $webp_support || rcube_image::is_convertable('image/webp');
  1190. if ((!$tiff_support && $mimetype == 'image/tiff') || (!$webp_support && $mimetype == 'image/webp')) {
  1191. return;
  1192. }
  1193. // Content-Type: image/*...
  1194. if (strpos($mimetype, 'image/') === 0) {
  1195. return rcmail_fix_mimetype($mimetype);
  1196. }
  1197. // Many clients use application/octet-stream, we'll detect mimetype
  1198. // by checking filename extension
  1199. // Supported image filename extensions to image type map
  1200. $types = array(
  1201. 'jpg' => 'image/jpeg',
  1202. 'jpeg' => 'image/jpeg',
  1203. 'png' => 'image/png',
  1204. 'gif' => 'image/gif',
  1205. 'bmp' => 'image/bmp',
  1206. );
  1207. if ($tiff_support) {
  1208. $types['tif'] = 'image/tiff';
  1209. $types['tiff'] = 'image/tiff';
  1210. }
  1211. if ($webp_support) {
  1212. $types['webp'] = 'image/webp';
  1213. }
  1214. if ($part->filename
  1215. && $mimetype == 'application/octet-stream'
  1216. && preg_match('/\.([^.]+)$/i', $part->filename, $m)
  1217. && ($extension = strtolower($m[1]))
  1218. && isset($types[$extension])
  1219. ) {
  1220. return $types[$extension];
  1221. }
  1222. }
  1223. /**
  1224. * Modify a HTML message that it can be displayed inside a HTML page
  1225. */
  1226. function rcmail_html4inline($body, $container_id, $body_class='', &$attributes=array(), $allow_remote=false)
  1227. {
  1228. $last_style_pos = 0;
  1229. $cont_id = $container_id . ($body_class ? ' div.'.$body_class : '');
  1230. // find STYLE tags
  1231. while (($pos = stripos($body, '<style', $last_style_pos)) && ($pos2 = stripos($body, '</style>', $pos))) {
  1232. $pos = strpos($body, '>', $pos) + 1;
  1233. $len = $pos2 - $pos;
  1234. // replace all css definitions with #container [def]
  1235. $styles = substr($body, $pos, $len);
  1236. $styles = rcube_utils::mod_css_styles($styles, $cont_id, $allow_remote);
  1237. $body = substr_replace($body, $styles, $pos, $len);
  1238. $last_style_pos = $pos2 + strlen($styles) - $len;
  1239. }
  1240. $body = preg_replace(array(
  1241. // add comments around html and other tags
  1242. '/(<!DOCTYPE[^>]*>)/i',
  1243. '/(<\?xml[^>]*>)/i',
  1244. '/(<\/?html[^>]*>)/i',
  1245. '/(<\/?head[^>]*>)/i',
  1246. '/(<title[^>]*>.*<\/title>)/Ui',
  1247. '/(<\/?meta[^>]*>)/i',
  1248. // quote <? of php and xml files that are specified as text/html
  1249. '/<\?/',
  1250. '/\?>/',
  1251. // replace <body> with <div>
  1252. '/<body([^>]*)>/i',
  1253. '/<\/body>/i',
  1254. ),
  1255. array(
  1256. '<!--\\1-->',
  1257. '<!--\\1-->',
  1258. '<!--\\1-->',
  1259. '<!--\\1-->',
  1260. '<!--\\1-->',
  1261. '<!--\\1-->',
  1262. '&lt;?',
  1263. '?&gt;',
  1264. '<div class="' . $body_class . '"\\1>',
  1265. '</div>',
  1266. ),
  1267. $body);
  1268. // Handle body attributes that doesn't play nicely with div elements
  1269. $regexp = '/<div class="' . preg_quote($body_class, '/') . '"([^>]*)/';
  1270. if (preg_match($regexp, $body, $m)) {
  1271. $style = array();
  1272. $attrs = $m[0];
  1273. // Get bgcolor, we'll set it as background-color of the message container
  1274. if ($m[1] && preg_match('/bgcolor=["\']*([a-z0-9#]+)["\']*/i', $attrs, $mb)) {
  1275. $style['background-color'] = $mb[1];
  1276. $attrs = preg_replace('/bgcolor=["\']*[a-z0-9#]+["\']*/i', '', $attrs);
  1277. }
  1278. // Get background, we'll set it as background-image of the message container
  1279. if ($m[1] && preg_match('/background=["\']*([^"\'>\s]+)["\']*/', $attrs, $mb)) {
  1280. $style['background-image'] = 'url('.$mb[1].')';
  1281. $attrs = preg_replace('/background=["\']*([^"\'>\s]+)["\']*/', '', $attrs);
  1282. }
  1283. if (!empty($style)) {
  1284. $body = preg_replace($regexp, rtrim($attrs), $body, 1);
  1285. }
  1286. // handle body styles related to background image
  1287. if ($style['background-image']) {
  1288. // get body style
  1289. if (preg_match('/#'.preg_quote($cont_id, '/').'\s+\{([^}]+)}/i', $body, $m)) {
  1290. // get background related style
  1291. $regexp = '/(background-position|background-repeat)\s*:\s*([^;]+);/i';
  1292. if (preg_match_all($regexp, $m[1], $matches, PREG_SET_ORDER)) {
  1293. foreach ($matches as $m) {
  1294. $style[$m[1]] = $m[2];
  1295. }
  1296. }
  1297. }
  1298. }
  1299. if (!empty($style)) {
  1300. foreach ($style as $idx => $val) {
  1301. $style[$idx] = $idx . ': ' . $val;
  1302. }
  1303. $attributes['style'] = implode('; ', $style);
  1304. }
  1305. }
  1306. // make sure there's 'rcmBody' div, we need it for proper css modification
  1307. // its name is hardcoded in rcmail_message_body() also
  1308. else {
  1309. $body = '<div class="' . $body_class . '">' . $body . '</div>';
  1310. }
  1311. return $body;
  1312. }
  1313. /**
  1314. * Parse link (a, link, area) attributes and set correct target
  1315. */
  1316. function rcmail_washtml_link_callback($tag, $attribs, $content, $washtml)
  1317. {
  1318. global $RCMAIL;
  1319. $attrib = html::parse_attrib_string($attribs);
  1320. // Remove non-printable characters in URL (#1487805)
  1321. if ($attrib['href']) {
  1322. $attrib['href'] = preg_replace('/[\x00-\x1F]/', '', $attrib['href']);
  1323. }
  1324. if ($tag == 'link' && preg_match('/^https?:\/\//i', $attrib['href'])) {
  1325. $tempurl = 'tmp-' . md5($attrib['href']) . '.css';
  1326. $_SESSION['modcssurls'][$tempurl] = $attrib['href'];
  1327. $attrib['href'] = $RCMAIL->url(array('task' => 'utils', 'action' => 'modcss', 'u' => $tempurl, 'c' => $GLOBALS['rcmail_html_container_id']));
  1328. $end = ' />';
  1329. }
  1330. else if (preg_match('/^mailto:(.+)/i', $attrib['href'], $mailto)) {
  1331. list($mailto, $url) = explode('?', html_entity_decode($mailto[1], ENT_QUOTES, 'UTF-8'), 2);
  1332. // #6020: use raw encoding for correct "+" character handling as specified in RFC6068
  1333. $url = rawurldecode($url);
  1334. $mailto = rawurldecode($mailto);
  1335. $addresses = rcube_mime::decode_address_list($mailto, null, true);
  1336. $mailto = array();
  1337. // do sanity checks on recipients
  1338. foreach ($addresses as $idx => $addr) {
  1339. if (rcube_utils::check_email($addr['mailto'], false)) {
  1340. $addresses[$idx] = $addr['mailto'];
  1341. $mailto[] = $addr['string'];
  1342. }
  1343. else {
  1344. unset($addresses[$idx]);
  1345. }
  1346. }
  1347. if (!empty($addresses)) {
  1348. $attrib['href'] = 'mailto:' . implode(',', $addresses);
  1349. $attrib['onclick'] = sprintf(
  1350. "return %s.command('compose','%s',this)",
  1351. rcmail_output::JS_OBJECT_NAME,
  1352. rcube::JQ(implode(',', $mailto) . ($url ? "?$url" : '')));
  1353. }
  1354. else {
  1355. $attrib['href'] = '#NOP';
  1356. $attrib['onclick'] = '';
  1357. }
  1358. }
  1359. else if (empty($attrib['href']) && !isset($attrib['name'])) {
  1360. $attrib['href'] = './#NOP';
  1361. $attrib['onclick'] = 'return false';
  1362. }
  1363. else if (!empty($attrib['href']) && $attrib['href'][0] != '#') {
  1364. $attrib['target'] = '_blank';
  1365. }
  1366. // Better security by adding rel="noreferrer" (#1484686)
  1367. if (($tag == 'a' || $tag == 'area') && $attrib['href'] && $attrib['href'][0] != '#') {
  1368. $attrib['rel'] = 'noreferrer';
  1369. }
  1370. // allowed attributes for a|link|area tags
  1371. $allow = array('href','name','target','onclick','id','class','style','title',
  1372. 'rel','type','media','alt','coords','nohref','hreflang','shape');
  1373. return html::tag($tag, $attrib, $content, $allow);
  1374. }
  1375. /**
  1376. * Decode address string and re-format it as HTML links
  1377. */
  1378. function rcmail_address_string($input, $max=null, $linked=false, $addicon=null, $default_charset=null, $title=null)
  1379. {
  1380. global $RCMAIL, $PRINT_MODE;
  1381. $a_parts = rcube_mime::decode_address_list($input, null, true, $default_charset);
  1382. if (!count($a_parts)) {
  1383. return $input;
  1384. }
  1385. $c = count($a_parts);
  1386. $j = 0;
  1387. $out = '';
  1388. $allvalues = array();
  1389. $show_email = $RCMAIL->config->get('message_show_email');
  1390. if ($addicon && !isset($_SESSION['writeable_abook'])) {
  1391. $_SESSION['writeable_abook'] = $RCMAIL->get_address_sources(true) ? true : false;
  1392. }
  1393. foreach ($a_parts as $part) {
  1394. $j++;
  1395. $name = $part['name'];
  1396. $mailto = $part['mailto'];
  1397. $string = $part['string'];
  1398. $valid = rcube_utils::check_email($mailto, false);
  1399. // phishing email prevention (#1488981), e.g. "valid@email.addr <phishing@email.addr>"
  1400. if (!$show_email && $valid && $name && $name != $mailto && strpos($name, '@')) {
  1401. $name = '';
  1402. }
  1403. // IDNA ASCII to Unicode
  1404. if ($name == $mailto)
  1405. $name = rcube_utils::idn_to_utf8($name);
  1406. if ($string == $mailto)
  1407. $string = rcube_utils::idn_to_utf8($string);
  1408. $mailto = rcube_utils::idn_to_utf8($mailto);
  1409. if ($PRINT_MODE) {
  1410. $address = sprintf('%s &lt;%s&gt;', rcube::Q($name), rcube::Q($mailto));
  1411. }
  1412. else if ($valid) {
  1413. if ($linked) {
  1414. $attrs = array(
  1415. 'href' => 'mailto:' . $mailto,
  1416. 'class' => 'rcmContactAddress',
  1417. 'onclick' => sprintf("return %s.command('compose','%s',this)",
  1418. rcmail_output::JS_OBJECT_NAME, rcube::JQ(format_email_recipient($mailto, $name))),
  1419. );
  1420. if ($show_email && $name && $mailto) {
  1421. $content = rcube::Q($name ? sprintf('%s <%s>', $name, $mailto) : $mailto);
  1422. }
  1423. else {
  1424. $content = rcube::Q($name ?: $mailto);
  1425. $attrs['title'] = $mailto;
  1426. }
  1427. $address = html::a($attrs, $content);
  1428. }
  1429. else {
  1430. $address = html::span(array('title' => $mailto, 'class' => "rcmContactAddress"),
  1431. rcube::Q($name ?: $mailto));
  1432. }
  1433. if ($addicon && $_SESSION['writeable_abook']) {
  1434. $address .= html::a(array(
  1435. 'href' => "#add",
  1436. 'title' => $RCMAIL->gettext('addtoaddressbook'),
  1437. 'class' => 'rcmaddcontact',
  1438. 'onclick' => sprintf("return %s.command('add-contact','%s',this)",
  1439. rcmail_output::JS_OBJECT_NAME, rcube::JQ($string)),
  1440. ),
  1441. html::img(array(
  1442. 'src' => $RCMAIL->output->abs_url($addicon, true),
  1443. 'alt' => "Add contact",
  1444. 'class' => 'noselect',
  1445. )));
  1446. }
  1447. }
  1448. else {
  1449. $address = $name ? rcube::Q($name) : '';
  1450. if ($mailto) {
  1451. $address = trim($address . ' ' . rcube::Q($name ? sprintf('<%s>', $mailto) : $mailto));
  1452. }
  1453. }
  1454. $address = html::span('adr', $address);
  1455. $allvalues[] = $address;
  1456. if (!$moreadrs) {
  1457. $out .= ($out ? ', ' : '') . $address;
  1458. }
  1459. if ($max && $j == $max && $c > $j) {
  1460. if ($linked) {
  1461. $moreadrs = $c - $j;
  1462. }
  1463. else {
  1464. $out .= '...';
  1465. break;
  1466. }
  1467. }
  1468. }
  1469. if ($moreadrs) {
  1470. $label = rcube::Q($RCMAIL->gettext(array('name' => 'andnmore', 'vars' => array('nr' => $moreadrs))));
  1471. if ($PRINT_MODE) {
  1472. $out .= ' ' . html::a(array(
  1473. 'href' => '#more',
  1474. 'class' => 'morelink',
  1475. 'onclick' => '$(this).hide().next().show()',
  1476. ), $label)
  1477. . html::span(array('style' => 'display:none'), join(', ', $allvalues));
  1478. }
  1479. else {
  1480. $out .= ' ' . html::a(array(
  1481. 'href' => '#more',
  1482. 'class' => 'morelink',
  1483. 'onclick' => sprintf("return %s.show_popup_dialog('%s','%s')",
  1484. rcmail_output::JS_OBJECT_NAME,
  1485. rcube::JQ(join(', ', $allvalues)),
  1486. rcube::JQ($title))
  1487. ), $label);
  1488. }
  1489. }
  1490. return $out;
  1491. }
  1492. /**
  1493. * Wrap text to a given number of characters per line
  1494. * but respect the mail quotation of replies messages (>).
  1495. * Finally add another quotation level by prepending the lines
  1496. * with >
  1497. *
  1498. * @param string Text to wrap
  1499. * @param int The line width
  1500. * @return string The wrapped text
  1501. */
  1502. function rcmail_wrap_and_quote($text, $length = 72)
  1503. {
  1504. // Rebuild the message body with a maximum of $max chars, while keeping quoted message.
  1505. $max = max(75, $length + 8);
  1506. $lines = preg_split('/\r?\n/', trim($text));
  1507. $out = '';
  1508. foreach ($lines as $line) {
  1509. // don't wrap already quoted lines
  1510. if ($line[0] == '>') {
  1511. $line = '>' . rtrim($line);
  1512. }
  1513. else if (mb_strlen($line) > $max) {
  1514. $newline = '';
  1515. foreach (explode("\n", rcube_mime::wordwrap($line, $length - 2)) as $l) {
  1516. $newline .= strlen($l) ? "> $l\n" : ">\n";
  1517. }
  1518. $line = rtrim($newline);
  1519. }
  1520. else {
  1521. $line = '> ' . $line;
  1522. }
  1523. // Append the line
  1524. $out .= $line . "\n";
  1525. }
  1526. return rtrim($out, "\n");
  1527. }
  1528. function rcmail_draftinfo_encode($p)
  1529. {
  1530. $parts = array();
  1531. foreach ($p as $key => $val) {
  1532. $encode = $key == 'folder' || strpos($val, ';') !== false;
  1533. $parts[] = $key . '=' . ($encode ? 'B::' . base64_encode($val) : $val);
  1534. }
  1535. return join('; ', $parts);
  1536. }
  1537. function rcmail_draftinfo_decode($str)
  1538. {
  1539. $info = array();
  1540. foreach (preg_split('/;\s+/', $str) as $part) {
  1541. list($key, $val) = explode('=', $part, 2);
  1542. if (strpos($val, 'B::') === 0) {
  1543. $val = base64_decode(substr($val, 3));
  1544. }
  1545. else if ($key == 'folder') {
  1546. $val = base64_decode($val);
  1547. }
  1548. $info[$key] = $val;
  1549. }
  1550. return $info;
  1551. }
  1552. /**
  1553. * Send the MDN response
  1554. *
  1555. * @param mixed $message Original message object (rcube_message) or UID
  1556. * @param array $smtp_error SMTP error array (reference)
  1557. *
  1558. * @return boolean Send status
  1559. */
  1560. function rcmail_send_mdn($message, &$smtp_error)
  1561. {
  1562. global $RCMAIL;
  1563. if (!is_object($message) || !is_a($message, 'rcube_message')) {
  1564. $message = new rcube_message($message);
  1565. }
  1566. if ($message->headers->mdn_to && empty($message->headers->flags['MDNSENT']) &&
  1567. ($RCMAIL->storage->check_permflag('MDNSENT') || $RCMAIL->storage->check_permflag('*'))
  1568. ) {
  1569. $identity = rcmail_identity_select($message);
  1570. $sender = format_email_recipient($identity['email'], $identity['name']);
  1571. $recipient = array_shift(rcube_mime::decode_address_list(
  1572. $message->headers->mdn_to, 1, true, $message->headers->charset));
  1573. $mailto = $recipient['mailto'];
  1574. $compose = new Mail_mime("\r\n");
  1575. $compose->setParam('text_encoding', 'quoted-printable');
  1576. $compose->setParam('html_encoding', 'quoted-printable');
  1577. $compose->setParam('head_encoding', 'quoted-printable');
  1578. $compose->setParam('head_charset', RCUBE_CHARSET);
  1579. $compose->setParam('html_charset', RCUBE_CHARSET);
  1580. $compose->setParam('text_charset', RCUBE_CHARSET);
  1581. // compose headers array
  1582. $headers = array(
  1583. 'Date' => $RCMAIL->user_date(),
  1584. 'From' => $sender,
  1585. 'To' => $message->headers->mdn_to,
  1586. 'Subject' => $RCMAIL->gettext('receiptread') . ': ' . $message->subject,
  1587. 'Message-ID' => $RCMAIL->gen_message_id($identity['email']),
  1588. 'X-Sender' => $identity['email'],
  1589. 'References' => trim($message->headers->references . ' ' . $message->headers->messageID),
  1590. 'In-Reply-To' => $message->headers->messageID,
  1591. );
  1592. $report = "Final-Recipient: rfc822; {$identity['email']}\r\n"
  1593. . "Original-Message-ID: {$message->headers->messageID}\r\n"
  1594. . "Disposition: manual-action/MDN-sent-manually; displayed\r\n";
  1595. if ($message->headers->to) {
  1596. $report .= "Original-Recipient: {$message->headers->to}\r\n";
  1597. }
  1598. if ($agent = $RCMAIL->config->get('useragent')) {
  1599. $headers['User-Agent'] = $agent;
  1600. $report .= "Reporting-UA: $agent\r\n";
  1601. }
  1602. $to = rcube_mime::decode_mime_string($message->headers->to, $message->headers->charset);
  1603. $date = $RCMAIL->format_date($message->headers->date, $RCMAIL->config->get('date_long'));
  1604. $body = $RCMAIL->gettext("yourmessage") . "\r\n\r\n" .
  1605. "\t" . $RCMAIL->gettext("to") . ": {$to}\r\n" .
  1606. "\t" . $RCMAIL->gettext("subject") . ": {$message->subject}\r\n" .
  1607. "\t" . $RCMAIL->gettext("date") . ": {$date}\r\n" .
  1608. "\r\n" . $RCMAIL->gettext("receiptnote");
  1609. $compose->headers(array_filter($headers));
  1610. $compose->setContentType('multipart/report', array('report-type'=> 'disposition-notification'));
  1611. $compose->setTXTBody(rcube_mime::wordwrap($body, 75, "\r\n"));
  1612. $compose->addAttachment($report, 'message/disposition-notification', 'MDNPart2.txt', false, '7bit', 'inline');
  1613. // SMTP options
  1614. $options = array('mdn_use_from' => (bool) $RCMAIL->config->get('mdn_use_from'));
  1615. $sent = $RCMAIL->deliver_message($compose, $identity['email'], $mailto, $smtp_error, $body_file, $options, true);
  1616. if ($sent) {
  1617. $RCMAIL->storage->set_flag($message->uid, 'MDNSENT');
  1618. return true;
  1619. }
  1620. }
  1621. return false;
  1622. }
  1623. /**
  1624. * Detect recipient identity from specified message
  1625. */
  1626. function rcmail_identity_select($MESSAGE, $identities = null, $compose_mode = 'reply')
  1627. {
  1628. $a_recipients = array();
  1629. $a_names = array();
  1630. if ($identities === null) {
  1631. $identities = rcmail::get_instance()->user->list_identities(null, true);
  1632. }
  1633. // extract all recipients of the reply-message
  1634. if (is_object($MESSAGE->headers) && in_array($compose_mode, array('reply', 'forward'))) {
  1635. $a_to = rcube_mime::decode_address_list($MESSAGE->headers->to, null, true, $MESSAGE->headers->charset);
  1636. foreach ($a_to as $addr) {
  1637. if (!empty($addr['mailto'])) {
  1638. $a_recipients[] = strtolower($addr['mailto']);
  1639. $a_names[] = $addr['name'];
  1640. }
  1641. }
  1642. if (!empty($MESSAGE->headers->cc)) {
  1643. $a_cc = rcube_mime::decode_address_list($MESSAGE->headers->cc, null, true, $MESSAGE->headers->charset);
  1644. foreach ($a_cc as $addr) {
  1645. if (!empty($addr['mailto'])) {
  1646. $a_recipients[] = strtolower($addr['mailto']);
  1647. $a_names[] = $addr['name'];
  1648. }
  1649. }
  1650. }
  1651. }
  1652. // decode From: address
  1653. $from = rcube_mime::decode_address_list($MESSAGE->headers->from, null, true, $MESSAGE->headers->charset);
  1654. $from = array_shift($from);
  1655. $from['mailto'] = strtolower($from['mailto']);
  1656. $from_idx = null;
  1657. $found_idx = array('to' => null, 'from' => null);
  1658. $check_from = in_array($compose_mode, array('draft', 'edit', 'reply'));
  1659. // Select identity
  1660. foreach ($identities as $idx => $ident) {
  1661. // use From: header when in edit/draft or reply-to-self
  1662. if ($check_from && $from['mailto'] == strtolower($ident['email_ascii'])) {
  1663. // remember first matching identity address
  1664. if ($found_idx['from'] === null) {
  1665. $found_idx['from'] = $idx;
  1666. }
  1667. // match identity name
  1668. if ($from['name'] && $ident['name'] && $from['name'] == $ident['name']) {
  1669. $from_idx = $idx;
  1670. break;
  1671. }
  1672. }
  1673. // use replied/forwarded message recipients
  1674. else if (($found = array_search(strtolower($ident['email_ascii']), $a_recipients)) !== false) {
  1675. // remember first matching identity address
  1676. if ($found_idx['to'] === null) {
  1677. $found_idx['to'] = $idx;
  1678. }
  1679. // match identity name
  1680. if ($a_names[$found] && $ident['name'] && $a_names[$found] == $ident['name']) {
  1681. $from_idx = $idx;
  1682. break;
  1683. }
  1684. }
  1685. }
  1686. // If matching by name+address didn't find any matches,
  1687. // get first found identity (address) if any
  1688. if ($from_idx === null) {
  1689. $from_idx = $found_idx['from'] !== null ? $found_idx['from'] : $found_idx['to'];
  1690. }
  1691. // Try Return-Path
  1692. if ($from_idx === null && ($return_path = $MESSAGE->headers->others['return-path'])) {
  1693. $return_path = array_map('strtolower', (array) $return_path);
  1694. foreach ($identities as $idx => $ident) {
  1695. // Return-Path header contains an email address, but on some mailing list
  1696. // it can be e.g. <pear-dev-return-55250-local=domain.tld@lists.php.net>
  1697. // where local@domain.tld is the address we're looking for (#1489241)
  1698. $ident1 = strtolower($ident['email_ascii']);
  1699. $ident2 = str_replace('@', '=', $ident1);
  1700. $ident1 = '<' . $ident1 . '>';
  1701. $ident2 = '-' . $ident2 . '@';
  1702. foreach ($return_path as $path) {
  1703. if ($path == $ident1 || stripos($path, $ident2)) {
  1704. $from_idx = $idx;
  1705. break 2;
  1706. }
  1707. }
  1708. }
  1709. }
  1710. // See identity_select plugin for example usage of this hook
  1711. $plugin = rcmail::get_instance()->plugins->exec_hook('identity_select',
  1712. array('message' => $MESSAGE, 'identities' => $identities, 'selected' => $from_idx));
  1713. $selected = $plugin['selected'];
  1714. // default identity is always first on the list
  1715. return $identities[$selected !== null ? $selected : 0];
  1716. }
  1717. // Fixes some content-type names
  1718. function rcmail_fix_mimetype($name)
  1719. {
  1720. $map = array(
  1721. 'image/x-ms-bmp' => 'image/bmp', // #1490282
  1722. );
  1723. $name = strtolower($name);
  1724. if ($alias = $map[$name]) {
  1725. $name = $alias;
  1726. }
  1727. // Some versions of Outlook create garbage Content-Type:
  1728. // application/pdf.A520491B_3BF7_494D_8855_7FAC2C6C0608
  1729. else if (preg_match('/^application\/pdf.+/', $name)) {
  1730. $name = 'application/pdf';
  1731. }
  1732. // treat image/pjpeg (image/pjpg, image/jpg) as image/jpeg (#1489097)
  1733. else if (preg_match('/^image\/p?jpe?g$/', $name)) {
  1734. $name = 'image/jpeg';
  1735. }
  1736. return $name;
  1737. }
  1738. // return attachment filename, handle empty filename case
  1739. function rcmail_attachment_name($attachment, $display = false)
  1740. {
  1741. global $RCMAIL;
  1742. $filename = (string) $attachment->filename;
  1743. $filename = preg_replace('/[\x00-\x1F\x7F]/', '', $filename);
  1744. if ($filename === '') {
  1745. if ($attachment->mimetype == 'text/html') {
  1746. $filename = $RCMAIL->gettext('htmlmessage');
  1747. }
  1748. else {
  1749. $ext = (array) rcube_mime::get_mime_extensions($attachment->mimetype);
  1750. $ext = array_shift($ext);
  1751. $filename = $RCMAIL->gettext('messagepart') . ' ' . $attachment->mime_id;
  1752. if ($ext) {
  1753. $filename .= '.' . $ext;
  1754. }
  1755. }
  1756. }
  1757. // Display smart names for some known mimetypes
  1758. if ($display) {
  1759. if (preg_match('/application\/(pgp|pkcs7)-signature/i', $attachment->mimetype)) {
  1760. $filename = $RCMAIL->gettext('digitalsig');
  1761. }
  1762. }
  1763. return $filename;
  1764. }
  1765. function rcmail_search_filter($attrib)
  1766. {
  1767. global $RCMAIL;
  1768. if (!strlen($attrib['id'])) {
  1769. $attrib['id'] = 'rcmlistfilter';
  1770. }
  1771. $attrib['onchange'] = rcmail_output::JS_OBJECT_NAME.'.filter_mailbox(this.value)';
  1772. // Content-Type values of messages with attachments
  1773. // the same as in app.js:add_message_row()
  1774. $ctypes = array('application/', 'multipart/m', 'multipart/signed', 'multipart/report');
  1775. // Build search string of "with attachment" filter
  1776. $attachment = trim(str_repeat(' OR', count($ctypes)-1));
  1777. foreach ($ctypes as $type) {
  1778. $attachment .= ' HEADER Content-Type ' . rcube_imap_generic::escape($type);
  1779. }
  1780. $select = new html_select($attrib);
  1781. $select->add($RCMAIL->gettext('all'), 'ALL');
  1782. $select->add($RCMAIL->gettext('unread'), 'UNSEEN');
  1783. $select->add($RCMAIL->gettext('flagged'), 'FLAGGED');
  1784. $select->add($RCMAIL->gettext('unanswered'), 'UNANSWERED');
  1785. if (!$RCMAIL->config->get('skip_deleted')) {
  1786. $select->add($RCMAIL->gettext('deleted'), 'DELETED');
  1787. $select->add($RCMAIL->gettext('undeleted'), 'UNDELETED');
  1788. }
  1789. $select->add($RCMAIL->gettext('withattachment'), $attachment);
  1790. $select->add($RCMAIL->gettext('priority').': '.$RCMAIL->gettext('highest'), 'HEADER X-PRIORITY 1');
  1791. $select->add($RCMAIL->gettext('priority').': '.$RCMAIL->gettext('high'), 'HEADER X-PRIORITY 2');
  1792. $select->add($RCMAIL->gettext('priority').': '.$RCMAIL->gettext('normal'), 'NOT HEADER X-PRIORITY 1 NOT HEADER X-PRIORITY 2 NOT HEADER X-PRIORITY 4 NOT HEADER X-PRIORITY 5');
  1793. $select->add($RCMAIL->gettext('priority').': '.$RCMAIL->gettext('low'), 'HEADER X-PRIORITY 4');
  1794. $select->add($RCMAIL->gettext('priority').': '.$RCMAIL->gettext('lowest'), 'HEADER X-PRIORITY 5');
  1795. $RCMAIL->output->add_gui_object('search_filter', $attrib['id']);
  1796. return $select->show($_REQUEST['_search'] ? $_SESSION['search_filter'] : 'ALL');
  1797. }
  1798. function rcmail_search_interval($attrib)
  1799. {
  1800. global $RCMAIL;
  1801. if (!strlen($attrib['id'])) {
  1802. $attrib['id'] = 'rcmsearchinterval';
  1803. }
  1804. $select = new html_select($attrib);
  1805. $select->add('', '');
  1806. foreach (array('1W', '1M', '1Y', '-1W', '-1M', '-1Y') as $value) {
  1807. $select->add($RCMAIL->gettext('searchinterval' . $value), $value);
  1808. }
  1809. $RCMAIL->output->add_gui_object('search_interval', $attrib['id']);
  1810. return $select->show($_REQUEST['_search'] ? $_SESSION['search_interval'] : '');
  1811. }
  1812. function rcmail_message_error()
  1813. {
  1814. global $RCMAIL;
  1815. // Set env variables for messageerror.html template
  1816. if ($RCMAIL->action == 'show') {
  1817. $mbox_name = $RCMAIL->storage->get_folder();
  1818. $RCMAIL->output->set_env('mailbox', $mbox_name);
  1819. $RCMAIL->output->set_env('uid', null);
  1820. }
  1821. // display error message
  1822. $RCMAIL->output->show_message('messageopenerror', 'error');
  1823. // ... display message error page
  1824. $RCMAIL->output->send('messageerror');
  1825. }
  1826. function rcmail_message_import_form($attrib = array())
  1827. {
  1828. global $RCMAIL;
  1829. $RCMAIL->output->add_label('selectimportfile','importwait');
  1830. $input_attr = array(
  1831. 'multiple' => true,
  1832. 'name' => '_file[]',
  1833. 'accept' => ".eml, .mbox, message/rfc822, text/*",
  1834. );
  1835. $attrib['prefix'] = html::tag('input', array('type' => 'hidden', 'name' => '_unlock', 'value' => ''))
  1836. . html::tag('input', array('type' => 'hidden', 'name' => '_framed', 'value' => '1'));
  1837. return $RCMAIL->upload_form($attrib, 'importform', 'import-messages', $input_attr);
  1838. }
  1839. /**
  1840. * Add groups from the given address source to the address book widget
  1841. */
  1842. function rcmail_compose_contact_groups($abook, $source_id, $search = null, $search_mode = 0)
  1843. {
  1844. global $RCMAIL, $OUTPUT;
  1845. $jsresult = array();
  1846. foreach ($abook->list_groups($search, $search_mode) as $group) {
  1847. $abook->reset();
  1848. $abook->set_group($group['ID']);
  1849. // group (distribution list) with email address(es)
  1850. if ($group['email']) {
  1851. foreach ((array)$group['email'] as $email) {
  1852. $row_id = 'G'.$group['ID'];
  1853. $jsresult[$row_id] = format_email_recipient($email, $group['name']);
  1854. $OUTPUT->command('add_contact_row', $row_id, array(
  1855. 'contactgroup' => html::span(array('title' => $email), rcube::Q($group['name']))), 'group');
  1856. }
  1857. }
  1858. // make virtual groups clickable to list their members
  1859. else if ($group['virtual']) {
  1860. $row_id = 'G'.$group['ID'];
  1861. $OUTPUT->command('add_contact_row', $row_id, array(
  1862. 'contactgroup' => html::a(array(
  1863. 'href' => '#list',
  1864. 'rel' => $group['ID'],
  1865. 'title' => $RCMAIL->gettext('listgroup'),
  1866. 'onclick' => sprintf("return %s.command('pushgroup',{'source':'%s','id':'%s'},this,event)",
  1867. rcmail_output::JS_OBJECT_NAME, $source_id, $group['ID']),
  1868. ), rcube::Q($group['name']) . '&nbsp;' . html::span('action', '&raquo;'))),
  1869. 'group',
  1870. array('ID' => $group['ID'], 'name' => $group['name'], 'virtual' => true));
  1871. }
  1872. // show group with count
  1873. else if (($result = $abook->count()) && $result->count) {
  1874. $row_id = 'E'.$group['ID'];
  1875. $jsresult[$row_id] = $group['name'];
  1876. $OUTPUT->command('add_contact_row', $row_id, array(
  1877. 'contactgroup' => rcube::Q($group['name'] . ' (' . intval($result->count) . ')')), 'group');
  1878. }
  1879. }
  1880. $abook->reset();
  1881. $abook->set_group(0);
  1882. return $jsresult;
  1883. }
  1884. function rcmail_save_attachment($message, $pid, $compose_id, $params = array())
  1885. {
  1886. global $COMPOSE;
  1887. $rcmail = rcmail::get_instance();
  1888. $storage = $rcmail->get_storage();
  1889. if ($pid) {
  1890. // attachment requested
  1891. $part = $message->mime_parts[$pid];
  1892. $size = $part->size;
  1893. $mimetype = $part->ctype_primary . '/' . $part->ctype_secondary;
  1894. $filename = $params['filename'] ?: rcmail_attachment_name($part);
  1895. }
  1896. else if (is_object($message)) {
  1897. // the whole message requested
  1898. $size = $message->size;
  1899. $mimetype = 'message/rfc822';
  1900. $filename = $params['filename'] ?: 'message_rfc822.eml';
  1901. }
  1902. else if (is_string($message)) {
  1903. // the whole message requested
  1904. $size = strlen($message);
  1905. $data = $message;
  1906. $mimetype = $params['mimetype'];
  1907. $filename = $params['filename'];
  1908. }
  1909. if (!isset($data)) {
  1910. // don't load too big attachments into memory
  1911. if (!rcube_utils::mem_check($size)) {
  1912. $temp_dir = unslashify($rcmail->config->get('temp_dir'));
  1913. $path = tempnam($temp_dir, 'rcmAttmnt');
  1914. if ($fp = fopen($path, 'w')) {
  1915. if ($pid) {
  1916. // part body
  1917. $message->get_part_body($pid, false, 0, $fp);
  1918. }
  1919. else {
  1920. // complete message
  1921. $storage->get_raw_body($message->uid, $fp);
  1922. }
  1923. fclose($fp);
  1924. }
  1925. else {
  1926. return false;
  1927. }
  1928. }
  1929. else if ($pid) {
  1930. // part body
  1931. $data = $message->get_part_body($pid);
  1932. }
  1933. else {
  1934. // complete message
  1935. $data = $storage->get_raw_body($message->uid);
  1936. }
  1937. }
  1938. $attachment = array(
  1939. 'group' => $compose_id,
  1940. 'name' => $filename,
  1941. 'mimetype' => $mimetype,
  1942. 'content_id' => $part ? $part->content_id : null,
  1943. 'data' => $data,
  1944. 'path' => $path,
  1945. 'size' => $path ? filesize($path) : strlen($data),
  1946. 'charset' => $part ? $part->charset : $params['charset'],
  1947. );
  1948. $attachment = $rcmail->plugins->exec_hook('attachment_save', $attachment);
  1949. if ($attachment['status']) {
  1950. unset($attachment['data'], $attachment['status'], $attachment['content_id'], $attachment['abort']);
  1951. // rcube_session::append() replaces current session data with the old values
  1952. // (in rcube_session::reload()). This is a problem in 'compose' action, because before
  1953. // the first append() use we set some important data in the session.
  1954. // It also overwrites attachments list. Fixing reload() is not so simple if possible
  1955. // as we don't really know what has been added and what removed in meantime.
  1956. // So, for now we'll do not use append() on 'compose' action (#1490608).
  1957. if ($rcmail->action == 'compose') {
  1958. $COMPOSE['attachments'][$attachment['id']] = $attachment;
  1959. }
  1960. else {
  1961. $rcmail->session->append('compose_data_' . $compose_id . '.attachments', $attachment['id'], $attachment);
  1962. }
  1963. return $attachment;
  1964. }
  1965. else if ($path) {
  1966. @unlink($path);
  1967. }
  1968. return false;
  1969. }
  1970. // Return mimetypes supported by the browser
  1971. function rcmail_supported_mimetypes()
  1972. {
  1973. $rcmail = rcube::get_instance();
  1974. // mimetypes supported by the browser (default settings)
  1975. $mimetypes = (array) $rcmail->config->get('client_mimetypes');
  1976. // Remove unsupported types, which makes that attachment which cannot be
  1977. // displayed in a browser will be downloaded directly without displaying an overlay page
  1978. if (empty($_SESSION['browser_caps']['pdf']) && ($key = array_search('application/pdf', $mimetypes)) !== false) {
  1979. unset($mimetypes[$key]);
  1980. }
  1981. if (empty($_SESSION['browser_caps']['flash']) && ($key = array_search('application/x-shockwave-flash', $mimetypes)) !== false) {
  1982. unset($mimetypes[$key]);
  1983. }
  1984. foreach (array('tiff', 'webp') as $type) {
  1985. if (empty($_SESSION['browser_caps'][$type]) && ($key = array_search('image/' . $type, $mimetypes)) !== false) {
  1986. // can we convert it to jpeg?
  1987. if (!rcube_image::is_convertable('image/' . $type)) {
  1988. unset($mimetypes[$key]);
  1989. }
  1990. }
  1991. }
  1992. // @TODO: support mail preview for compose attachments
  1993. if ($rcmail->action != 'compose' && !in_array('message/rfc822', $mimetypes)) {
  1994. $mimetypes[] = 'message/rfc822';
  1995. }
  1996. return array_values($mimetypes);
  1997. }