mailbox_display.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. <?php
  2. /**
  3. * mailbox_display.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains functions that display mailbox information, such as the
  9. * table row that has sender, date, subject, etc...
  10. *
  11. * $Id$
  12. * @package squirrelmail
  13. */
  14. /** The standard includes.. */
  15. require_once(SM_PATH . 'functions/strings.php');
  16. require_once(SM_PATH . 'functions/html.php');
  17. require_once(SM_PATH . 'class/html.class.php');
  18. require_once(SM_PATH . 'functions/imap_mailbox.php');
  19. require_once(SM_PATH . 'functions/imap_messages.php');
  20. require_once(SM_PATH . 'functions/mime.php');
  21. require_once(SM_PATH . 'functions/forms.php');
  22. /**
  23. * default value for page_selector_max
  24. */
  25. define('PG_SEL_MAX', 10);
  26. /**
  27. * @param mixed $start UNDOCUMENTED
  28. */
  29. function elapsed($start)
  30. {
  31. $end = microtime();
  32. list($start2, $start1) = explode(" ", $start);
  33. list($end2, $end1) = explode(" ", $end);
  34. $diff1 = $end1 - $start1;
  35. $diff2 = $end2 - $start2;
  36. if( $diff2 < 0 ){
  37. $diff1 -= 1;
  38. $diff2 += 1.0;
  39. }
  40. return $diff2 + $diff1;
  41. }
  42. /**
  43. * Displays message listing
  44. *
  45. * @param mixed $imapConnection
  46. * @param mixed $t UNDOCUMENTED
  47. * @param bool $not_last UNDOCUMENTED
  48. * @param mixed $key UNDOCUMENTED
  49. * @param string $mailbox mail folder
  50. * @param mixed $start_msg UNDOCUMENTED
  51. * @param mixed $where UNDOCUMENTED
  52. * @param mixed $what UNDOCUMENTED
  53. */
  54. function printMessageInfo($imapConnection, $t, $not_last=true, $key, $mailbox,
  55. $start_msg, $where, $what) {
  56. global $checkall,
  57. $color, $msgs, $msort, $td_str, $msg,
  58. $default_use_priority,
  59. $message_highlight_list,
  60. $index_order,
  61. $indent_array, /* indent subject by */
  62. $pos, /* Search postion (if any) */
  63. $thread_sort_messages, /* thread sorting on/off */
  64. $server_sort_order, /* sort value when using server-sorting */
  65. $row_count,
  66. $allow_server_sort, /* enable/disable server-side sorting */
  67. $truncate_sender, /* number of characters for From/To field (<= 0 for unchanged) */
  68. $email_address,
  69. $show_recipient_instead, /* show recipient name instead of default identity */
  70. $use_icons, /* indicates to use icons or text markers */
  71. $icon_theme; /* icons theming */
  72. $color_string = $color[4];
  73. if ($GLOBALS['alt_index_colors']) {
  74. if (!isset($row_count)) {
  75. $row_count = 0;
  76. }
  77. $row_count++;
  78. if ($row_count % 2) {
  79. if (!isset($color[12])) {
  80. $color[12] = '#EAEAEA';
  81. }
  82. $color_string = $color[12];
  83. }
  84. }
  85. $msg = $msgs[$key];
  86. if($mailbox == 'None') {
  87. $boxes = sqimap_mailbox_list($imapConnection);
  88. $mailbox = $boxes[0]['unformatted'];
  89. unset($boxes);
  90. }
  91. $urlMailbox = urlencode($mailbox);
  92. $bSentFolder = handleAsSent($mailbox);
  93. if ((!$bSentFolder) && ($show_recipient_instead)) {
  94. // If the From address is the same as $email_address, then handle as Sent
  95. $from_array = parseAddress($msg['FROM'], 1);
  96. if (!isset($email_address)) {
  97. global $datadir, $username;
  98. $email_address = getPref($datadir, $username, 'email_address');
  99. }
  100. $bHandleAsSent = ((isset($from_array[0][0])) && ($from_array[0][0] == $email_address));
  101. }
  102. else
  103. $bHandleAsSent = $bSentFolder;
  104. // If this is a Sent message, display To address instead of From
  105. if ($bHandleAsSent)
  106. $msg['FROM'] = $msg['TO'];
  107. // Passing 1 below results in only 1 address being parsed, thus defeating the following code
  108. $msg['FROM'] = parseAddress($msg['FROM']/*,1*/);
  109. /*
  110. * This is done in case you're looking into Sent folders,
  111. * because you can have multiple receivers.
  112. */
  113. $senderNames = $msg['FROM'];
  114. $senderName = '';
  115. $senderAddress = '';
  116. if (sizeof($senderNames)){
  117. foreach ($senderNames as $senderNames_part) {
  118. if ($senderName != '') {
  119. $senderName .= ', ';
  120. $senderAddress .= ', ';
  121. }
  122. $sender_address_part = htmlspecialchars($senderNames_part[0]);
  123. $sender_name_part = str_replace('&nbsp;',' ', decodeHeader($senderNames_part[1]));
  124. if ($sender_name_part) {
  125. $senderName .= $sender_name_part;
  126. $senderAddress .= $sender_name_part . ' <' . $sender_address_part . '>';
  127. } else {
  128. $senderName .= $sender_address_part;
  129. $senderAddress .= $sender_address_part;
  130. }
  131. }
  132. }
  133. // If Sent, prefix with To: but only if not Sent folder
  134. if ($bHandleAsSent ^ $bSentFolder) {
  135. $senderName = _("To:") . ' ' . $senderName;
  136. $senderAddress = _("To:") . ' ' . $senderAddress;
  137. }
  138. if ($truncate_sender > 0)
  139. $senderName = truncateWithEntities($senderName, $truncate_sender);
  140. echo html_tag( 'tr','','','','VALIGN="top"') . "\n";
  141. if (isset($msg['FLAG_FLAGGED']) && ($msg['FLAG_FLAGGED'] == true)) {
  142. $flag = "<font color=\"$color[2]\">";
  143. $flag_end = '</font>';
  144. } else {
  145. $flag = '';
  146. $flag_end = '';
  147. }
  148. if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN'] == false)) {
  149. $bold = '<b>';
  150. $bold_end = '</b>';
  151. } else {
  152. $bold = '';
  153. $bold_end = '';
  154. }
  155. if ($bHandleAsSent) {
  156. $italic = '<i>';
  157. $italic_end = '</i>';
  158. } else {
  159. $italic = '';
  160. $italic_end = '';
  161. }
  162. if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED']) {
  163. $fontstr = "<font color=\"$color[9]\">";
  164. $fontstr_end = '</font>';
  165. } else {
  166. $fontstr = '';
  167. $fontstr_end = '';
  168. }
  169. if ($where && $what) {
  170. $searchstr = '&amp;where='.$where.'&amp;what='.$what;
  171. } else {
  172. $searchstr = '';
  173. }
  174. if (is_array($message_highlight_list) && count($message_highlight_list)) {
  175. $msg['TO'] = parseAddress($msg['TO']);
  176. $msg['CC'] = parseAddress($msg['CC']);
  177. foreach ($message_highlight_list as $message_highlight_list_part) {
  178. if (trim($message_highlight_list_part['value']) != '') {
  179. $high_val = strtolower($message_highlight_list_part['value']);
  180. $match_type = strtoupper($message_highlight_list_part['match_type']);
  181. if($match_type == 'TO_CC') {
  182. $match = array('TO', 'CC');
  183. } else {
  184. $match = array($match_type);
  185. }
  186. foreach($match as $match_type) {
  187. switch($match_type) {
  188. case('TO'):
  189. case('CC'):
  190. case('FROM'):
  191. foreach ($msg[$match_type] as $address) {
  192. $address[0] = decodeHeader($address[0], true, false);
  193. $address[1] = decodeHeader($address[1], true, false);
  194. if (strstr('^^' . strtolower($address[0]), $high_val) ||
  195. strstr('^^' . strtolower($address[1]), $high_val)) {
  196. $hlt_color = $message_highlight_list_part['color'];
  197. break 4;
  198. }
  199. }
  200. break;
  201. default:
  202. $headertest = strtolower(decodeHeader($msg[$match_type], true, false));
  203. if (strstr('^^' . $headertest, $high_val)) {
  204. $hlt_color = $message_highlight_list_part['color'];
  205. break 3;
  206. }
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. if (!isset($hlt_color)) {
  214. $hlt_color = $color_string;
  215. }
  216. $col = 0;
  217. $msg['SUBJECT'] = str_replace('&nbsp;', ' ', decodeHeader($msg['SUBJECT']));
  218. $subject = processSubject($msg['SUBJECT'], $indent_array[$msg['ID']]);
  219. if (sizeof($index_order)) {
  220. foreach ($index_order as $index_order_part) {
  221. switch ($index_order_part) {
  222. case 1: /* checkbox */
  223. echo html_tag( 'td',
  224. addCheckBox("msg[$t]", $checkall, $msg['ID']),
  225. 'center',
  226. $hlt_color );
  227. break;
  228. case 2: /* from */
  229. if ($senderAddress != $senderName) {
  230. $senderAddress = strtr($senderAddress, array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
  231. $title = ' title="' . str_replace('"', "''", $senderAddress) . '"';
  232. }
  233. else
  234. $title = '';
  235. echo html_tag( 'td',
  236. $italic . $bold . $flag . $fontstr . $senderName .
  237. $fontstr_end . $flag_end . $bold_end . $italic_end,
  238. 'left',
  239. $hlt_color, $title );
  240. break;
  241. case 3: /* date */
  242. $date_string = $msg['DATE_STRING'] . '';
  243. if ($date_string == '') {
  244. $date_string = _("Unknown date");
  245. }
  246. echo html_tag( 'td',
  247. $bold . $flag . $fontstr . $date_string .
  248. $fontstr_end . $flag_end . $bold_end,
  249. 'center',
  250. $hlt_color,
  251. 'nowrap' );
  252. break;
  253. case 4: /* subject */
  254. $td_str = $bold;
  255. if ($thread_sort_messages == 1) {
  256. if (isset($indent_array[$msg['ID']])) {
  257. $td_str .= str_repeat("&nbsp;&nbsp;&nbsp;&nbsp;",$indent_array[$msg['ID']]);
  258. }
  259. }
  260. $td_str .= '<a href="read_body.php?mailbox='.$urlMailbox
  261. . '&amp;passed_id='. $msg["ID"]
  262. . '&amp;startMessage='.$start_msg.$searchstr.'"';
  263. $td_str .= ' ' .concat_hook_function('subject_link', array($start_msg, $searchstr));
  264. if ($subject != $msg['SUBJECT']) {
  265. $title = get_html_translation_table(HTML_SPECIALCHARS);
  266. $title = array_flip($title);
  267. $title = strtr($msg['SUBJECT'], $title);
  268. $title = str_replace('"', "''", $title);
  269. $td_str .= " title=\"$title\"";
  270. }
  271. $td_str .= ">$flag$subject$flag_end</a>$bold_end";
  272. echo html_tag( 'td', $td_str, 'left', $hlt_color );
  273. break;
  274. case 5: /* flags */
  275. // icon message markers
  276. //
  277. if ($use_icons && $icon_theme != 'none') {
  278. $td_str = "<b><small>";
  279. if (isset($msg['FLAG_FLAGGED']) && $msg['FLAG_FLAGGED'] == true) {
  280. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/flagged.png" border="0" height="10" width="10" /> ';
  281. }
  282. if ($default_use_priority) {
  283. if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
  284. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_high.png" border="0" height="10" width="5" /> ';
  285. }
  286. else if ($msg['PRIORITY'] == 5) {
  287. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/prio_low.png" border="0" height="10" width="5" /> ';
  288. }
  289. else
  290. {
  291. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="5" /> ';
  292. }
  293. }
  294. if ($msg['TYPE0'] == 'multipart') {
  295. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/attach.png" border="0" height="10" width="6" />';
  296. }
  297. else
  298. {
  299. $td_str .= '<img src="' . SM_PATH . 'images/themes/' . $icon_theme . '/transparent.png" border="0" width="6" />';
  300. }
  301. $msg_icon = '';
  302. if (!isset($msg['FLAG_SEEN']) || ($msg['FLAG_SEEN']) == false)
  303. {
  304. $msg_alt = '(' . _("New") . ')';
  305. $msg_title = '(' . _("New") . ')';
  306. $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_new';
  307. }
  308. else
  309. {
  310. $msg_alt = '(' . _("Read") . ')';
  311. $msg_title = '(' . _("Read") . ')';
  312. $msg_icon .= SM_PATH . 'images/themes/' . $icon_theme . '/msg_read';
  313. }
  314. if (isset($msg['FLAG_DELETED']) && ($msg['FLAG_DELETED']) == true)
  315. {
  316. $msg_icon .= '_deleted';
  317. }
  318. if (isset($msg['FLAG_ANSWERED']) && ($msg['FLAG_ANSWERED']) == true)
  319. {
  320. $msg_alt = '(' . _("Answered") . ')';
  321. $msg_title = '(' . _("Answered") . ')';
  322. $msg_icon .= '_reply';
  323. }
  324. $td_str .= '<img src="' . $msg_icon . '.png" border="0" alt="'. $msg_alt . '" title="' . $msg_title . '" height="12" width="18" />';
  325. $td_str .= '</small></b>';
  326. echo html_tag( 'td',
  327. $td_str,
  328. 'right',
  329. $hlt_color,
  330. 'nowrap' );
  331. }
  332. // plain text message markers
  333. //
  334. else {
  335. $stuff = false;
  336. $td_str = "<b><small>";
  337. if (isset($msg['FLAG_ANSWERED']) && $msg['FLAG_ANSWERED'] == true) {
  338. $td_str .= _("A");
  339. $stuff = true;
  340. }
  341. if ($msg['TYPE0'] == 'multipart') {
  342. $td_str .= '+';
  343. $stuff = true;
  344. }
  345. if ($default_use_priority) {
  346. if ( ($msg['PRIORITY'] == 1) || ($msg['PRIORITY'] == 2) ) {
  347. $td_str .= "<font color=\"$color[1]\">!</font>";
  348. $stuff = true;
  349. }
  350. if ($msg['PRIORITY'] == 5) {
  351. $td_str .= "<font color=\"$color[8]\">?</font>";
  352. $stuff = true;
  353. }
  354. }
  355. if (isset($msg['FLAG_DELETED']) && $msg['FLAG_DELETED'] == true) {
  356. $td_str .= "<font color=\"$color[1]\">D</font>";
  357. $stuff = true;
  358. }
  359. if (!$stuff) {
  360. $td_str .= '&nbsp;';
  361. }
  362. $td_str .= '</small></b>';
  363. echo html_tag( 'td',
  364. $td_str,
  365. 'center',
  366. $hlt_color,
  367. 'nowrap' );
  368. }
  369. break;
  370. case 6: /* size */
  371. echo html_tag( 'td',
  372. $bold . $fontstr . show_readable_size($msg['SIZE']) .
  373. $fontstr_end . $bold_end,
  374. 'right',
  375. $hlt_color );
  376. break;
  377. }
  378. ++$col;
  379. }
  380. }
  381. if ($not_last) {
  382. echo '</tr>' . "\n" . '<tr><td colspan="' . $col . '" bgcolor="' .
  383. $color[0] . '" height="1"></td></tr>' . "\n";
  384. } else {
  385. echo '</tr>'."\n";
  386. }
  387. }
  388. /**
  389. * FIXME: Undocumented function
  390. *
  391. * @param mixed $imapConnection
  392. * @param mixed $start_msg
  393. * @param mixed $show_num
  394. * @param mixed $num_msgs
  395. * @param mixed $id
  396. * @return array
  397. */
  398. function getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id) {
  399. if ($id != 'no') {
  400. $id = array_slice($id, ($start_msg-1), $show_num);
  401. $end = $start_msg + $show_num - 1;
  402. if ($num_msgs < $show_num) {
  403. $end_loop = $num_msgs;
  404. } else if ($end > $num_msgs) {
  405. $end_loop = $num_msgs - $start_msg + 1;
  406. } else {
  407. $end_loop = $show_num;
  408. }
  409. return fillMessageArray($imapConnection,$id,$end_loop,$show_num);
  410. } else {
  411. return false;
  412. }
  413. }
  414. /**
  415. * FIXME: Undocumented function
  416. *
  417. * @param mixed $imapConnection
  418. * @param mixed $start_msg
  419. * @param mixed $show_num
  420. * @param mixed $num_msgs
  421. * @return array
  422. */
  423. function getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs) {
  424. $id = get_thread_sort($imapConnection);
  425. return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  426. }
  427. /**
  428. * FIXME: Undocumented function
  429. *
  430. * @param mixed $imapConnection
  431. * @param mixed $start_msg
  432. * @param mixed $show_num
  433. * @param mixed $num_msgs
  434. * @param mixed $server_sort_order
  435. * @param mixed $mbxresponse
  436. * @return array
  437. */
  438. function getServerSortMessages($imapConnection, $start_msg, $show_num,
  439. $num_msgs, $server_sort_order, $mbxresponse) {
  440. $id = sqimap_get_sort_order($imapConnection, $server_sort_order,$mbxresponse);
  441. return getServerMessages($imapConnection, $start_msg, $show_num, $num_msgs, $id);
  442. }
  443. /**
  444. * FIXME: Undocumented function
  445. *
  446. * @param mixed $imapConnection
  447. * @param mixed $start_msg
  448. * @param mixed $show_num
  449. * @param mixed $num_msgs
  450. * @param mixed $sort
  451. * @param mixed $mbxresponse
  452. * @return array
  453. */
  454. function getSelfSortMessages($imapConnection, $start_msg, $show_num,
  455. $num_msgs, $sort, $mbxresponse) {
  456. $msgs = array();
  457. if ($num_msgs >= 1) {
  458. $id = sqimap_get_php_sort_order ($imapConnection, $mbxresponse);
  459. if ($sort < 6 ) {
  460. $end = $num_msgs;
  461. $end_loop = $end;
  462. /* set shownum to 999999 to fool sqimap_get_small_header_list
  463. and rebuild the msgs_str to 1:* */
  464. $show_num = 999999;
  465. } else {
  466. /* if it's not sorted */
  467. if ($start_msg + ($show_num - 1) < $num_msgs) {
  468. $end_msg = $start_msg + ($show_num - 1);
  469. } else {
  470. $end_msg = $num_msgs;
  471. }
  472. if ($end_msg < $start_msg) {
  473. $start_msg = $start_msg - $show_num;
  474. if ($start_msg < 1) {
  475. $start_msg = 1;
  476. }
  477. }
  478. $id = array_slice(array_reverse($id), ($start_msg-1), $show_num);
  479. $end = $start_msg + $show_num - 1;
  480. if ($num_msgs < $show_num) {
  481. $end_loop = $num_msgs;
  482. } else if ($end > $num_msgs) {
  483. $end_loop = $num_msgs - $start_msg + 1;
  484. } else {
  485. $end_loop = $show_num;
  486. }
  487. }
  488. $msgs = fillMessageArray($imapConnection,$id,$end_loop, $show_num);
  489. }
  490. return $msgs;
  491. }
  492. /**
  493. * This function loops through a group of messages in the mailbox
  494. * and shows them to the user.
  495. *
  496. * @param mixed $imapConnection
  497. * @param string $mailbox mail folder
  498. * @param mixed $num_msgs
  499. * @param mixed $start_msg
  500. * @param mixed $sort
  501. * @param mixed $color
  502. * @param mixed $show_num
  503. * @param mixed $use_cache
  504. * @param mixed $mode
  505. */
  506. function showMessagesForMailbox($imapConnection, $mailbox, $num_msgs,
  507. $start_msg, $sort, $color, $show_num,
  508. $use_cache, $mode='') {
  509. global $msgs, $msort, $auto_expunge, $thread_sort_messages,
  510. $allow_server_sort, $server_sort_order;
  511. /*
  512. * For some reason, on PHP 4.3+, this being unset, and set in the session causes havoc
  513. * so setting it to an empty array beforehand seems to clean up the issue, and stopping the
  514. * "Your script possibly relies on a session side-effect which existed until PHP 4.2.3" error
  515. */
  516. if (!isset($msort)) {
  517. $msort = array();
  518. }
  519. if (!isset($msgs)) {
  520. $msgs = array();
  521. }
  522. //$start = microtime();
  523. /* If autoexpunge is turned on, then do it now. */
  524. $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
  525. $srt = $sort;
  526. /* If autoexpunge is turned on, then do it now. */
  527. if ($auto_expunge == true) {
  528. $exp_cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, false, '');
  529. $mbxresponse['EXISTS'] = $mbxresponse['EXISTS'] - $exp_cnt;
  530. $num_msgs = $mbxresponse['EXISTS'];
  531. }
  532. if ($mbxresponse['EXISTS'] > 0) {
  533. /* if $start_msg is lower than $num_msgs, we probably deleted all messages
  534. * in the last page. We need to re-adjust the start_msg
  535. */
  536. if($start_msg > $num_msgs) {
  537. $start_msg -= $show_num;
  538. if($start_msg < 1) {
  539. $start_msg = 1;
  540. }
  541. }
  542. /* This code and the next if() block check for
  543. * server-side sorting methods. The $id array is
  544. * formatted and $sort is set to 6 to disable
  545. * SM internal sorting
  546. */
  547. if ($thread_sort_messages == 1) {
  548. $mode = 'thread';
  549. } elseif ($allow_server_sort == 1) {
  550. $mode = 'serversort';
  551. } else {
  552. $mode = '';
  553. }
  554. if ($use_cache) {
  555. sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
  556. sqgetGlobalVar('msort', $msort, SQ_SESSION);
  557. } else {
  558. sqsession_unregister('msort');
  559. sqsession_unregister('msgs'); }
  560. switch ($mode) {
  561. case 'thread':
  562. $msgs = getThreadMessages($imapConnection, $start_msg, $show_num, $num_msgs);
  563. if ($msgs === false) {
  564. echo '<b><small><center><font color=red>' .
  565. _("Thread sorting is not supported by your IMAP server.") . "<br />" .
  566. _("Please report this to the system administrator.").
  567. '</center></small></b>';
  568. $thread_sort_messages = 0;
  569. $msort = $msgs = array();
  570. } else {
  571. $msort= $msgs;
  572. $sort = 6;
  573. }
  574. break;
  575. case 'serversort':
  576. $msgs = getServerSortMessages($imapConnection, $start_msg, $show_num,
  577. $num_msgs, $sort, $mbxresponse);
  578. if ($msgs === false) {
  579. echo '<b><small><center><font color=red>' .
  580. _( "Server-side sorting is not supported by your IMAP server.") . "<br />" .
  581. _("Please report this to the system administrator.").
  582. '</center></small></b>';
  583. $sort = $server_sort_order;
  584. $allow_server_sort = FALSE;
  585. $msort = $msgs = array();
  586. $id = array();
  587. } else {
  588. $msort = $msgs;
  589. $sort = 6;
  590. }
  591. break;
  592. default:
  593. if (!$use_cache) {
  594. $msgs = getSelfSortMessages($imapConnection, $start_msg, $show_num,
  595. $num_msgs, $sort, $mbxresponse);
  596. $msort = calc_msort($msgs, $sort);
  597. } /* !use cache */
  598. break;
  599. } // switch
  600. sqsession_register($msort, 'msort');
  601. sqsession_register($msgs, 'msgs');
  602. } /* if exists > 0 */
  603. $res = getEndMessage($start_msg, $show_num, $num_msgs);
  604. $start_msg = $res[0];
  605. $end_msg = $res[1];
  606. $paginator_str = get_paginator_str($mailbox, $start_msg, $end_msg,
  607. $num_msgs, $show_num, $sort);
  608. $msg_cnt_str = get_msgcnt_str($start_msg, $end_msg, $num_msgs);
  609. do_hook('mailbox_index_before');
  610. ?>
  611. <table border="0" width="100%" cellpadding="0" cellspacing="0">
  612. <tr>
  613. <td>
  614. <?php mail_message_listing_beginning($imapConnection, $mbxresponse, $mailbox, $sort,
  615. $msg_cnt_str, $paginator_str, $start_msg); ?>
  616. </td>
  617. </tr>
  618. <tr><td height="5" bgcolor="<?php echo $color[4]; ?>"></td></tr>
  619. <tr>
  620. <td>
  621. <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[9]; ?>">
  622. <tr>
  623. <td>
  624. <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="<?php echo $color[5]; ?>">
  625. <tr>
  626. <td>
  627. <?php
  628. printHeader($mailbox, $srt, $color, !$thread_sort_messages, $start_msg);
  629. displayMessageArray($imapConnection, $num_msgs, $start_msg,
  630. $msort, $mailbox, $sort, $color, $show_num,0,0);
  631. ?>
  632. </td>
  633. </tr>
  634. </table>
  635. </td>
  636. </tr>
  637. </table>
  638. <?php
  639. mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color);
  640. ?>
  641. </td>
  642. </tr>
  643. </table>
  644. <?php
  645. //$t = elapsed($start);
  646. //echo("elapsed time = $t seconds\n");
  647. }
  648. /**
  649. * FIXME: Undocumented function
  650. *
  651. * @param array $messages
  652. * @param integer $sort sorting order
  653. * @return array
  654. */
  655. function calc_msort($msgs, $sort) {
  656. /*
  657. * 0 = Date (up)
  658. * 1 = Date (dn)
  659. * 2 = Name (up)
  660. * 3 = Name (dn)
  661. * 4 = Subject (up)
  662. * 5 = Subject (dn)
  663. */
  664. if (($sort == 0) || ($sort == 1)) {
  665. foreach ($msgs as $item) {
  666. $msort[] = $item['TIME_STAMP'];
  667. }
  668. } elseif (($sort == 2) || ($sort == 3)) {
  669. foreach ($msgs as $item) {
  670. $msort[] = $item['FROM-SORT'];
  671. }
  672. } elseif (($sort == 4) || ($sort == 5)) {
  673. foreach ($msgs as $item) {
  674. $msort[] = $item['SUBJECT-SORT'];
  675. }
  676. } else {
  677. $msort = $msgs;
  678. }
  679. if ($sort < 6) {
  680. if ($sort % 2) {
  681. asort($msort);
  682. } else {
  683. arsort($msort);
  684. }
  685. }
  686. return $msort;
  687. }
  688. /**
  689. * FIXME: Undocumented function
  690. *
  691. * @param mixed $imapConnection
  692. * @param mixed $id
  693. * @param mixed $count
  694. * @param bool $show_num
  695. */
  696. function fillMessageArray($imapConnection, $id, $count, $show_num=false) {
  697. return sqimap_get_small_header_list($imapConnection, $id, $show_num);
  698. }
  699. /**
  700. * Generic function to convert the msgs array into an HTML table.
  701. *
  702. * @param mixed $imapConnection
  703. * @param mixed $num_msgs
  704. * @param mixed $start_msg
  705. * @param mixed $msort
  706. * @param string $mailbox mail folder name
  707. * @param mixed $sort
  708. * @param mixed $color
  709. * @param mixed $show_num
  710. * @param mixed $where
  711. * @param mixed $what
  712. */
  713. function displayMessageArray($imapConnection, $num_msgs, $start_msg,
  714. $msort, $mailbox, $sort, $color,
  715. $show_num, $where=0, $what=0) {
  716. global $imapServerAddress, $use_mailbox_cache, $index_order,
  717. $indent_array, $thread_sort_messages, $allow_server_sort,
  718. $server_sort_order, $PHP_SELF;
  719. $res = getEndMessage($start_msg, $show_num, $num_msgs);
  720. $start_msg = $res[0];
  721. $end_msg = $res[1];
  722. $urlMailbox = urlencode($mailbox);
  723. /* get indent level for subject display */
  724. if ($thread_sort_messages == 1 && $num_msgs) {
  725. $indent_array = get_parent_level($imapConnection);
  726. }
  727. $real_startMessage = $start_msg;
  728. if ($sort == 6) {
  729. if ($end_msg - $start_msg < $show_num - 1) {
  730. $end_msg = $end_msg - $start_msg + 1;
  731. $start_msg = 1;
  732. } else if ($start_msg > $show_num) {
  733. $end_msg = $show_num;
  734. $start_msg = 1;
  735. }
  736. }
  737. $endVar = $end_msg + 1;
  738. /*
  739. * Loop through and display the info for each message.
  740. * ($t is used for the checkbox number)
  741. */
  742. $t = 0;
  743. /* messages display */
  744. if (!$num_msgs) {
  745. /* if there's no messages in this folder */
  746. echo html_tag( 'tr',
  747. html_tag( 'td',
  748. "<br /><b>" . _("THIS FOLDER IS EMPTY") . "</b><br />&nbsp;",
  749. 'center',
  750. $color[4],
  751. 'colspan="' . count($index_order) . '"'
  752. )
  753. );
  754. } elseif ($start_msg == $end_msg) {
  755. /* if there's only one message in the box, handle it differently. */
  756. if ($sort != 6) {
  757. $i = $start_msg;
  758. } else {
  759. $i = 1;
  760. }
  761. reset($msort);
  762. $k = 0;
  763. do {
  764. $key = key($msort);
  765. next($msort);
  766. $k++;
  767. } while (isset ($key) && ($k < $i));
  768. printMessageInfo($imapConnection, $t, true, $key, $mailbox,
  769. $real_startMessage, $where, $what);
  770. } else {
  771. $i = $start_msg;
  772. reset($msort);
  773. $k = 0;
  774. do {
  775. $key = key($msort);
  776. next($msort);
  777. $k++;
  778. } while (isset ($key) && ($k < $i));
  779. $not_last = true;
  780. do {
  781. if (!$i || $i == $endVar-1) $not_last = false;
  782. printMessageInfo($imapConnection, $t, $not_last, $key, $mailbox,
  783. $real_startMessage, $where, $what);
  784. $key = key($msort);
  785. $t++;
  786. $i++;
  787. next($msort);
  788. } while ($i && $i < $endVar);
  789. }
  790. }
  791. /**
  792. * Displays the standard message list header.
  793. *
  794. * To finish the table, you need to do a "</table></table>";
  795. *
  796. * @param mixed $imapConnection
  797. * @param array $mbxresponse the array with the results of SELECT against the current mailbox
  798. * @param string $mailbox the current mailbox
  799. * @param mixed $sort the current sorting method (-1 for no sorting available [searches])
  800. * @param mixed $msg_cnt_str
  801. * @param mixed $paginator
  802. * @param mixed $start_msg
  803. */
  804. function mail_message_listing_beginning ($imapConnection,
  805. $mbxresponse,
  806. $mailbox = '', $sort = -1,
  807. $msg_cnt_str = '',
  808. $paginator = '&nbsp;',
  809. $start_msg = 1) {
  810. global $color, $auto_expunge, $base_uri, $show_flag_buttons,
  811. $allow_server_sort, $server_sort_order,
  812. $PHP_SELF, $allow_thread_sort, $thread_sort_messages;
  813. $php_self = $PHP_SELF;
  814. /* fix for incorrect $PHP_SELF */
  815. if (strpos($php_self, 'move_messages.php')) {
  816. $php_self = str_replace('move_messages.php', 'right_main.php', $php_self);
  817. }
  818. $urlMailbox = urlencode($mailbox);
  819. if (preg_match('/^(.+)\?.+$/',$php_self,$regs)) {
  820. $source_url = $regs[1];
  821. } else {
  822. $source_url = $php_self;
  823. }
  824. if (!isset($msg)) {
  825. $msg = '';
  826. }
  827. if (!strpos($php_self,'?')) {
  828. $location = $php_self.'?mailbox=INBOX&amp;startMessage=1';
  829. } else {
  830. $location = $php_self;
  831. }
  832. $moveFields = addHidden('msg', $msg).
  833. addHidden('mailbox', $mailbox).
  834. addHidden('startMessage', $start_msg).
  835. addHidden('location', $location);
  836. /* build thread sorting links */
  837. if ($allow_thread_sort == TRUE) {
  838. if ($thread_sort_messages == 1 ) {
  839. $set_thread = 2;
  840. $thread_name = _("Unthread View");
  841. } elseif ($thread_sort_messages == 0) {
  842. $set_thread = 1;
  843. $thread_name = _("Thread View");
  844. }
  845. $thread_link_str = '<small>[<a href="' . $source_url . '?sort='
  846. . $sort . '&start_messages=1&set_thread=' . $set_thread
  847. . '&mailbox=' . urlencode($mailbox) . '">' . $thread_name
  848. . '</a>]</small>';
  849. }
  850. else
  851. $thread_link_str ='';
  852. /*
  853. * This is the beginning of the message list table.
  854. * It wraps around all messages
  855. */
  856. $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
  857. $form_name = "FormMsgs" . $safe_name;
  858. echo '<form name="' . $form_name . '" method="post" action="move_messages.php">' ."\n"
  859. . $moveFields;
  860. ?>
  861. <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
  862. <tr>
  863. <td>
  864. <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
  865. <tr>
  866. <td align="left"><small>
  867. <?php
  868. echo $paginator;
  869. echo $thread_link_str;
  870. ?>
  871. </small></td>
  872. <td align="center"></td>
  873. <td align="right"><small><?php echo $msg_cnt_str; ?></small></td>
  874. </tr>
  875. </table>
  876. </td>
  877. </tr>
  878. <tr width="100%" cellpadding="1" cellspacing="0" border="0" bgcolor="<?php echo $color[0]; ?>">
  879. <td>
  880. <table border="0" width="100%" cellpadding="1" cellspacing="0">
  881. <tr>
  882. <td align="left">
  883. <small><?php
  884. // display flag buttons only if supported
  885. if ($show_flag_buttons && $mbxresponse != NULL &&
  886. strpos($mbxresponse['PERMANENTFLAGS'], '\\Flagged') !== FALSE) {
  887. echo getButton('SUBMIT', 'markUnflagged',_("Unflag"));
  888. echo getButton('SUBMIT', 'markFlagged',_("Flag"));
  889. echo '&nbsp;';
  890. }
  891. echo getButton('SUBMIT', 'markUnread',_("Unread"));
  892. echo getButton('SUBMIT', 'markRead',_("Read"));
  893. echo '&nbsp;';
  894. echo getButton('SUBMIT', 'attache',_("Forward"));
  895. echo '&nbsp;';
  896. echo getButton('SUBMIT', 'delete',_("Delete"));
  897. echo '<input type="checkbox" name="bypass_trash" />' . _("Bypass Trash");
  898. echo '&nbsp;';
  899. if (!$auto_expunge) {
  900. echo getButton('SUBMIT', 'expungeButton',_("Expunge")) .'&nbsp;' . _("mailbox") . "\n";
  901. echo '&nbsp;';
  902. }
  903. do_hook('mailbox_display_buttons');
  904. ?></small>
  905. </td>
  906. <td align="right">
  907. <small><?php
  908. //echo $thread_link_str; //previous behaviour
  909. getMbxList($imapConnection);
  910. echo getButton('SUBMIT', 'moveButton',_("Move")) . "\n";
  911. ?></small>
  912. </td>
  913. </tr>
  914. </table>
  915. </td>
  916. </tr>
  917. </table>
  918. <?php
  919. do_hook('mailbox_form_before');
  920. /* if using server sort we highjack the
  921. * the $sort var and use $server_sort_order
  922. * instead. but here we reset sort for a bit
  923. * since its easy
  924. */
  925. if ($allow_server_sort == TRUE) {
  926. $sort = $server_sort_order;
  927. }
  928. }
  929. /**
  930. * FIXME: Undocumented function
  931. *
  932. * @param mixed $num_msgs
  933. * @param mixed $paginator_str
  934. * @param mixed $msg_cnt_str
  935. * @param mixed $color
  936. */
  937. function mail_message_listing_end($num_msgs, $paginator_str, $msg_cnt_str, $color) {
  938. if ($num_msgs) {
  939. /* space between list and footer */
  940. ?>
  941. <tr><td height="5" bgcolor="<?php echo $color[4]; ?>" colspan="1"></td></tr>
  942. <tr>
  943. <td>
  944. <table width="100%" cellpadding="1" cellspacing="0" style="border: 1px solid <?php echo $color[0]; ?>">
  945. <tr>
  946. <td>
  947. <table bgcolor="<?php echo $color[4]; ?>" border="0" width="100%" cellpadding="1" cellspacing="0">
  948. <tr>
  949. <td align="left"><small><?php echo $paginator_str; ?></small></td>
  950. <td align="right"><small><?php echo $msg_cnt_str; ?></small></td>
  951. </tr>
  952. </table>
  953. </td>
  954. </tr>
  955. </table>
  956. </td>
  957. </tr>
  958. <?php
  959. }
  960. /* End of message-list table */
  961. do_hook('mailbox_index_after');
  962. echo "</form>\n";
  963. }
  964. /**
  965. * FIXME: Undocumented function
  966. *
  967. * @param string $mailbox
  968. * @param mixed $sort
  969. * @param mixed $color
  970. * @param bool $showsort
  971. * @param mixed $start_msg
  972. */
  973. function printHeader($mailbox, $sort, $color, $showsort=true, $start_msg=1) {
  974. global $index_order;
  975. echo html_tag( 'tr' ,'' , 'center', $color[5] );
  976. /* calculate the width of the subject column based on the
  977. * widths of the other columns */
  978. $widths = array(1=>1,2=>25,3=>5,4=>0,5=>1,6=>5);
  979. $subjectwidth = 100;
  980. foreach($index_order as $item) {
  981. $subjectwidth -= $widths[$item];
  982. }
  983. foreach ($index_order as $item) {
  984. switch ($item) {
  985. case 1: /* checkbox */
  986. echo html_tag( 'td',get_selectall_link($start_msg, $sort, $mailbox) , '', '', 'width="1%"' );
  987. break;
  988. case 5: /* flags */
  989. echo html_tag( 'td','' , '', '', 'width="1%"' );
  990. break;
  991. case 2: /* from */
  992. if (handleAsSent($mailbox)) {
  993. echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
  994. . '<b>' . _("To") . '</b>';
  995. } else {
  996. echo html_tag( 'td' ,'' , 'left', '', 'width="25%"' )
  997. . '<b>' . _("From") . '</b>';
  998. }
  999. if ($showsort) {
  1000. ShowSortButton($sort, $mailbox, 2, 3);
  1001. }
  1002. echo "</td>\n";
  1003. break;
  1004. case 3: /* date */
  1005. echo html_tag( 'td' ,'' , 'left', '', 'width="5%" nowrap' )
  1006. . '<b>' . _("Date") . '</b>';
  1007. if ($showsort) {
  1008. ShowSortButton($sort, $mailbox, 0, 1);
  1009. }
  1010. echo "</td>\n";
  1011. break;
  1012. case 4: /* subject */
  1013. echo html_tag( 'td' ,'' , 'left', '', 'width="'.$subjectwidth.'%"' )
  1014. . '<b>' . _("Subject") . '</b>';
  1015. if ($showsort) {
  1016. ShowSortButton($sort, $mailbox, 4, 5);
  1017. }
  1018. echo "</td>\n";
  1019. break;
  1020. case 6: /* size */
  1021. echo html_tag( 'td', '<b>' . _("Size") . '</b>', 'center', '', 'width="5%" nowrap' );
  1022. break;
  1023. }
  1024. }
  1025. echo "</tr>\n";
  1026. }
  1027. /**
  1028. * This function shows the sort button. Isn't this a good comment?
  1029. *
  1030. * @param mixed $sort
  1031. * @param string $mailbox
  1032. * @param mixed $Down
  1033. * @param mixed $Up
  1034. */
  1035. function ShowSortButton($sort, $mailbox, $Down, $Up ) {
  1036. global $PHP_SELF;
  1037. /* Figure out which image we want to use. */
  1038. if ($sort != $Up && $sort != $Down) {
  1039. $img = 'sort_none.png';
  1040. $which = $Up;
  1041. } elseif ($sort == $Up) {
  1042. $img = 'up_pointer.png';
  1043. $which = $Down;
  1044. } else {
  1045. $img = 'down_pointer.png';
  1046. $which = 6;
  1047. }
  1048. if (preg_match('/^(.+)\?.+$/',$PHP_SELF,$regs)) {
  1049. $source_url = $regs[1];
  1050. } else {
  1051. $source_url = $PHP_SELF;
  1052. }
  1053. /* Now that we have everything figured out, show the actual button. */
  1054. echo ' <a href="' . $source_url .'?newsort=' . $which
  1055. . '&amp;startMessage=1&amp;mailbox=' . urlencode($mailbox)
  1056. . '"><img src="../images/' . $img
  1057. . '" border="0" width="12" height="10" alt="sort" title="'
  1058. . _("Click here to change the sorting of the message list") .'"></a>';
  1059. }
  1060. /**
  1061. * FIXME: Undocumented function
  1062. *
  1063. * @param mixed $start_msg
  1064. * @param mixed $sort
  1065. * @param string $mailbox
  1066. */
  1067. function get_selectall_link($start_msg, $sort, $mailbox) {
  1068. global $checkall, $what, $where, $javascript_on;
  1069. global $PHP_SELF, $PG_SHOWNUM;
  1070. $result = '';
  1071. if ($javascript_on) {
  1072. $safe_name = preg_replace("/[^0-9A-Za-z_]/", '_', $mailbox);
  1073. $func_name = "CheckAll" . $safe_name;
  1074. $form_name = "FormMsgs" . $safe_name;
  1075. $result = '<script language="JavaScript" type="text/javascript">'
  1076. . "\n<!-- \n"
  1077. . "function " . $func_name . "() {\n"
  1078. . " for (var i = 0; i < document." . $form_name . ".elements.length; i++) {\n"
  1079. . " if(document." . $form_name . ".elements[i].type == 'checkbox' && "
  1080. . "document." . $form_name . ".elements[i].name != 'bypass_trash'){\n"
  1081. . " document." . $form_name . ".elements[i].checked = "
  1082. . " !(document." . $form_name . ".elements[i].checked);\n"
  1083. . " }\n"
  1084. . " }\n"
  1085. . "}\n"
  1086. . "//-->\n"
  1087. . '</script>'
  1088. . '<input type="checkbox" name="toggleAll" title="'._("Toggle All").'" onclick="'.$func_name.'();" />';
  1089. // . <a href="javascript:void(0)" onClick="' . $func_name . '();">' . _("Toggle All")
  1090. // . "</a>\n";
  1091. } else {
  1092. if (strpos($PHP_SELF, "?")) {
  1093. $result .= "<a href=\"$PHP_SELF&amp;mailbox=" . urlencode($mailbox)
  1094. . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
  1095. } else {
  1096. $result .= "<a href=\"$PHP_SELF?mailbox=" . urlencode($mailbox)
  1097. . "&amp;startMessage=$start_msg&amp;sort=$sort&amp;checkall=";
  1098. }
  1099. if (isset($checkall) && $checkall == '1') {
  1100. $result .= '0';
  1101. } else {
  1102. $result .= '1';
  1103. }
  1104. if (isset($where) && isset($what)) {
  1105. $result .= '&amp;where=' . urlencode($where)
  1106. . '&amp;what=' . urlencode($what);
  1107. }
  1108. $result .= "\">";
  1109. $result .= _("All");
  1110. $result .= "</a>\n";
  1111. }
  1112. /* Return our final result. */
  1113. return ($result);
  1114. }
  1115. /**
  1116. * This function computes the "Viewing Messages..." string.
  1117. *
  1118. * @param integer $start_msg first message number
  1119. * @param integer $end_msg last message number
  1120. * @param integer $num_msgs total number of message in folder
  1121. * @return string
  1122. */
  1123. function get_msgcnt_str($start_msg, $end_msg, $num_msgs) {
  1124. /* Compute the $msg_cnt_str. */
  1125. $result = '';
  1126. if ($start_msg < $end_msg) {
  1127. $result = sprintf(_("Viewing Messages: %s to %s (%s total)"),
  1128. '<b>'.$start_msg.'</b>', '<b>'.$end_msg.'</b>', $num_msgs);
  1129. } else if ($start_msg == $end_msg) {
  1130. $result = sprintf(_("Viewing Message: %s (1 total)"), '<b>'.$start_msg.'</b>');
  1131. } else {
  1132. $result = '<br />';
  1133. }
  1134. /* Return our result string. */
  1135. return ($result);
  1136. }
  1137. /**
  1138. * Generate a paginator link.
  1139. *
  1140. * @param mixed $box
  1141. * @param mixed $start_msg
  1142. * @param mixed $use
  1143. * @param string $text text used for paginator link
  1144. * @return string
  1145. */
  1146. function get_paginator_link($box, $start_msg, $use, $text) {
  1147. $result = "<a href=\"right_main.php?use_mailbox_cache=$use"
  1148. . "&amp;startMessage=$start_msg&amp;mailbox=$box\" "
  1149. . ">$text</a>";
  1150. return ($result);
  1151. }
  1152. /**
  1153. * This function computes the paginator string.
  1154. *
  1155. * @param mixed $box
  1156. * @param mixed $start_msg
  1157. * @param mixed $end_msg
  1158. * @param integer $num_msgs
  1159. * @param mixed $show_num
  1160. * @param mixed $sort
  1161. */
  1162. function get_paginator_str($box, $start_msg, $end_msg, $num_msgs,
  1163. $show_num, $sort) {
  1164. global $username, $data_dir, $use_mailbox_cache, $color, $PG_SHOWNUM;
  1165. /* Initialize paginator string chunks. */
  1166. $prv_str = '';
  1167. $nxt_str = '';
  1168. $pg_str = '';
  1169. $all_str = '';
  1170. $box = urlencode($box);
  1171. /* Create simple strings that will be creating the paginator. */
  1172. $spc = '&nbsp;'; /* This will be used as a space. */
  1173. $sep = '|'; /* This will be used as a seperator. */
  1174. /* Get some paginator preference values. */
  1175. $pg_sel = getPref($data_dir, $username, 'page_selector', SMPREF_ON);
  1176. $pg_max = getPref($data_dir, $username, 'page_selector_max', PG_SEL_MAX);
  1177. /* Make sure that our start message number is not too big. */
  1178. $start_msg = min($start_msg, $num_msgs);
  1179. /* Decide whether or not we will use the mailbox cache. */
  1180. /* Not sure why $use_mailbox_cache is even passed in. */
  1181. if ($sort == 6) {
  1182. $use = 0;
  1183. } else {
  1184. $use = 1;
  1185. }
  1186. /* Compute the starting message of the previous and next page group. */
  1187. $next_grp = $start_msg + $show_num;
  1188. $prev_grp = $start_msg - $show_num;
  1189. /* Compute the basic previous and next strings. */
  1190. if (($next_grp <= $num_msgs) && ($prev_grp >= 0)) {
  1191. $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
  1192. $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
  1193. } else if (($next_grp > $num_msgs) && ($prev_grp >= 0)) {
  1194. $prv_str = get_paginator_link($box, $prev_grp, $use, _("Previous"));
  1195. $nxt_str = _("Next");
  1196. } else if (($next_grp <= $num_msgs) && ($prev_grp < 0)) {
  1197. $prv_str = _("Previous");
  1198. $nxt_str = get_paginator_link($box, $next_grp, $use, _("Next"));
  1199. }
  1200. /* Page selector block. Following code computes page links. */
  1201. if ($pg_sel && ($num_msgs > $show_num)) {
  1202. /* Most importantly, what is the current page!!! */
  1203. $cur_pg = intval($start_msg / $show_num) + 1;
  1204. /* Compute total # of pages and # of paginator page links. */
  1205. $tot_pgs = ceil($num_msgs / $show_num); /* Total number of Pages */
  1206. $vis_pgs = min($pg_max, $tot_pgs - 1); /* Visible Pages */
  1207. /* Compute the size of the four quarters of the page links. */
  1208. /* If we can, just show all the pages. */
  1209. if (($tot_pgs - 1) <= $pg_max) {
  1210. $q1_pgs = $cur_pg - 1;
  1211. $q2_pgs = $q3_pgs = 0;
  1212. $q4_pgs = $tot_pgs - $cur_pg;
  1213. /* Otherwise, compute some magic to choose the four quarters. */
  1214. } else {
  1215. /*
  1216. * Compute the magic base values. Added together,
  1217. * these values will always equal to the $pag_pgs.
  1218. * NOTE: These are DEFAULT values and do not take
  1219. * the current page into account. That is below.
  1220. */
  1221. $q1_pgs = floor($vis_pgs/4);
  1222. $q2_pgs = round($vis_pgs/4, 0);
  1223. $q3_pgs = ceil($vis_pgs/4);
  1224. $q4_pgs = round(($vis_pgs - $q2_pgs)/3, 0);
  1225. /* Adjust if the first quarter contains the current page. */
  1226. if (($cur_pg - $q1_pgs) < 1) {
  1227. $extra_pgs = ($q1_pgs - ($cur_pg - 1)) + $q2_pgs;
  1228. $q1_pgs = $cur_pg - 1;
  1229. $q2_pgs = 0;
  1230. $q3_pgs += ceil($extra_pgs / 2);
  1231. $q4_pgs += floor($extra_pgs / 2);
  1232. /* Adjust if the first and second quarters intersect. */
  1233. } else if (($cur_pg - $q2_pgs - ceil($q2_pgs/3)) <= $q1_pgs) {
  1234. $extra_pgs = $q2_pgs;
  1235. $extra_pgs -= ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  1236. $q2_pgs = ceil(($cur_pg - $q1_pgs - 1) * 3/4);
  1237. $q3_pgs += ceil($extra_pgs / 2);
  1238. $q4_pgs += floor($extra_pgs / 2);
  1239. /* Adjust if the fourth quarter contains the current page. */
  1240. } else if (($cur_pg + $q4_pgs) >= $tot_pgs) {
  1241. $extra_pgs = ($q4_pgs - ($tot_pgs - $cur_pg)) + $q3_pgs;
  1242. $q3_pgs = 0;
  1243. $q4_pgs = $tot_pgs - $cur_pg;
  1244. $q1_pgs += floor($extra_pgs / 2);
  1245. $q2_pgs += ceil($extra_pgs / 2);
  1246. /* Adjust if the third and fourth quarter intersect. */
  1247. } else if (($cur_pg + $q3_pgs + 1) >= ($tot_pgs - $q4_pgs + 1)) {
  1248. $extra_pgs = $q3_pgs;
  1249. $extra_pgs -= ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  1250. $q3_pgs = ceil(($tot_pgs - $cur_pg - $q4_pgs) * 3/4);
  1251. $q1_pgs += floor($extra_pgs / 2);
  1252. $q2_pgs += ceil($extra_pgs / 2);
  1253. }
  1254. }
  1255. /*
  1256. * I am leaving this debug code here, commented out, because
  1257. * it is a really nice way to see what the above code is doing.
  1258. * echo "qts = $q1_pgs/$q2_pgs/$q3_pgs/$q4_pgs = "
  1259. * . ($q1_pgs + $q2_pgs + $q3_pgs + $q4_pgs) . '<br>';
  1260. */
  1261. /* Print out the page links from the compute page quarters. */
  1262. /* Start with the first quarter. */
  1263. if (($q1_pgs == 0) && ($cur_pg > 1)) {
  1264. $pg_str .= "...$spc";
  1265. } else {
  1266. for ($pg = 1; $pg <= $q1_pgs; ++$pg) {
  1267. $start = (($pg-1) * $show_num) + 1;
  1268. $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1269. }
  1270. if ($cur_pg - $q2_pgs - $q1_pgs > 1) {
  1271. $pg_str .= "...$spc";
  1272. }
  1273. }
  1274. /* Continue with the second quarter. */
  1275. for ($pg = $cur_pg - $q2_pgs; $pg < $cur_pg; ++$pg) {
  1276. $start = (($pg-1) * $show_num) + 1;
  1277. $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1278. }
  1279. /* Now print the current page. */
  1280. $pg_str .= $cur_pg . $spc;
  1281. /* Next comes the third quarter. */
  1282. for ($pg = $cur_pg + 1; $pg <= $cur_pg + $q3_pgs; ++$pg) {
  1283. $start = (($pg-1) * $show_num) + 1;
  1284. $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1285. }
  1286. /* And last, print the forth quarter page links. */
  1287. if (($q4_pgs == 0) && ($cur_pg < $tot_pgs)) {
  1288. $pg_str .= "...$spc";
  1289. } else {
  1290. if (($tot_pgs - $q4_pgs) > ($cur_pg + $q3_pgs)) {
  1291. $pg_str .= "...$spc";
  1292. }
  1293. for ($pg = $tot_pgs - $q4_pgs + 1; $pg <= $tot_pgs; ++$pg) {
  1294. $start = (($pg-1) * $show_num) + 1;
  1295. $pg_str .= get_paginator_link($box, $start, $use, $pg) . $spc;
  1296. }
  1297. }
  1298. } else if ($PG_SHOWNUM == 999999) {
  1299. $pg_str = "<a href=\"right_main.php?PG_SHOWALL=0"
  1300. . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
  1301. . ">" ._("Paginate") . '</a>';
  1302. }
  1303. /* Put all the pieces of the paginator string together. */
  1304. /**
  1305. * Hairy code... But let's leave it like it is since I am not certain
  1306. * a different approach would be any easier to read. ;)
  1307. */
  1308. $result = '';
  1309. if ( $prv_str != '' || $nxt_str != '' )
  1310. {
  1311. $result .= '[';
  1312. $result .= ($prv_str != '' ? $prv_str . $spc . $sep . $spc : '');
  1313. $result .= ($nxt_str != '' ? $nxt_str : '');
  1314. $result .= ']' . $spc ;
  1315. /* Compute the 'show all' string. */
  1316. $all_str = "<a href=\"right_main.php?PG_SHOWALL=1"
  1317. . "&amp;use_mailbox_cache=$use&amp;startMessage=1&amp;mailbox=$box\" "
  1318. . ">" . _("Show All") . '</a>';
  1319. }
  1320. $result .= ($pg_str != '' ? $spc . '['.$spc.$pg_str.']' . $spc : '');
  1321. $result .= ($all_str != '' ? $spc . '['.$all_str.']' . $spc . $spc : '');
  1322. /* If the resulting string is blank, return a non-breaking space. */
  1323. if ($result == '') {
  1324. $result = '&nbsp;';
  1325. }
  1326. /* Return our final magical paginator string. */
  1327. return ($result);
  1328. }
  1329. /**
  1330. * FIXME: Undocumented function
  1331. */
  1332. function truncateWithEntities($subject, $trim_at)
  1333. {
  1334. $ent_strlen = strlen($subject);
  1335. if (($trim_at <= 0) || ($ent_strlen <= $trim_at))
  1336. return $subject;
  1337. global $languages, $squirrelmail_language;
  1338. /*
  1339. * see if this is entities-encoded string
  1340. * If so, Iterate through the whole string, find out
  1341. * the real number of characters, and if more
  1342. * than $trim_at, substr with an updated trim value.
  1343. */
  1344. $trim_val = $trim_at;
  1345. $ent_offset = 0;
  1346. $ent_loc = 0;
  1347. while ( $ent_loc < $trim_val && (($ent_loc = strpos($subject, '&', $ent_offset)) !== false) &&
  1348. (($ent_loc_end = strpos($subject, ';', $ent_loc+3)) !== false) ) {
  1349. $trim_val += ($ent_loc_end-$ent_loc);
  1350. $ent_offset = $ent_loc_end+1;
  1351. }
  1352. if (($trim_val > $trim_at) && ($ent_strlen > $trim_val) && (strpos($subject,';',$trim_val) < ($trim_val + 6))) {
  1353. $i = strpos($subject,';',$trim_val);
  1354. if ($i) {
  1355. $trim_val = strpos($subject,';',$trim_val);
  1356. }
  1357. }
  1358. // only print '...' when we're actually dropping part of the subject
  1359. if ($ent_strlen <= $trim_val)
  1360. return $subject;
  1361. if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
  1362. function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
  1363. return $languages[$squirrelmail_language]['XTRA_CODE']('strimwidth', $subject, $trim_val);
  1364. }
  1365. return substr_replace($subject, '...', $trim_val);
  1366. }
  1367. /**
  1368. * FIXME: Undocumented function
  1369. */
  1370. function processSubject($subject, $threadlevel = 0) {
  1371. /* Shouldn't ever happen -- caught too many times in the IMAP functions */
  1372. if ($subject == '') {
  1373. return _("(no subject)");
  1374. }
  1375. global $truncate_subject; /* number of characters for Subject field (<= 0 for unchanged) */
  1376. $trim_at = $truncate_subject;
  1377. /* if this is threaded, subtract two chars per indentlevel */
  1378. if (($threadlevel > 0) && ($threadlevel <= 10))
  1379. $trim_at -= (2*$threadlevel);
  1380. return truncateWithEntities($subject, $trim_at);
  1381. }
  1382. /**
  1383. * FIXME: Undocumented function
  1384. *
  1385. * @param mixed $imapConnection
  1386. * @param mixed $boxes
  1387. */
  1388. function getMbxList($imapConnection, $boxes = 0) {
  1389. global $lastTargetMailbox;
  1390. echo ' <small>&nbsp;<tt><select name="targetMailbox">';
  1391. echo sqimap_mailbox_option_list($imapConnection, array(strtolower($lastTargetMailbox)), 0, $boxes);
  1392. echo ' </select></tt>&nbsp;';
  1393. }
  1394. /**
  1395. * Creates button
  1396. *
  1397. * @deprecated see form functions available in 1.5.1 and 1.4.3.
  1398. * @param string $type
  1399. * @param string $name
  1400. * @param string $value
  1401. * @param string $js
  1402. * @param bool $enabled
  1403. */
  1404. function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
  1405. $disabled = ( $enabled ? '' : 'disabled ' );
  1406. $js = ( $js ? $js.' ' : '' );
  1407. return '<input '.$disabled.$js.
  1408. 'type="'.$type.
  1409. '" name="'.$name.
  1410. '" value="'.$value .
  1411. '" style="padding: 0px; margin: 0px" />';
  1412. }
  1413. /**
  1414. * Puts string into cell, aligns it and adds <small> tag
  1415. *
  1416. * @param string $string string
  1417. * @param string $align alignment
  1418. */
  1419. function getSmallStringCell($string, $align) {
  1420. return html_tag('td',
  1421. '<small>' . $string . ':&nbsp; </small>',
  1422. $align,
  1423. '',
  1424. 'nowrap' );
  1425. }
  1426. /**
  1427. * FIXME: Undocumented function
  1428. *
  1429. * @param integer $start_msg
  1430. * @param integer $show_num
  1431. * @param integer $num_msgs
  1432. */
  1433. function getEndMessage($start_msg, $show_num, $num_msgs) {
  1434. if ($start_msg + ($show_num - 1) < $num_msgs){
  1435. $end_msg = $start_msg + ($show_num - 1);
  1436. } else {
  1437. $end_msg = $num_msgs;
  1438. }
  1439. if ($end_msg < $start_msg) {
  1440. $start_msg = $start_msg - $show_num;
  1441. if ($start_msg < 1) {
  1442. $start_msg = 1;
  1443. }
  1444. }
  1445. return (array($start_msg,$end_msg));
  1446. }
  1447. /**
  1448. * This should go in imap_mailbox.php
  1449. * @param string $mailbox
  1450. */
  1451. function handleAsSent($mailbox) {
  1452. global $handleAsSent_result;
  1453. /* First check if this is the sent or draft folder. */
  1454. $handleAsSent_result = isSentMailbox($mailbox) || isDraftMailbox($mailbox);
  1455. /* Then check the result of the handleAsSent hook. */
  1456. do_hook('check_handleAsSent_result', $mailbox);
  1457. /* And return the result. */
  1458. return $handleAsSent_result;
  1459. }
  1460. ?>