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.

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