mailbox_display.php 52 KB

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