imap_mailbox.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. <?php
  2. /**
  3. * imap_mailbox.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 impliments all functions that manipulate mailboxes
  9. *
  10. * $Id$
  11. * @package squirrelmail
  12. */
  13. /** UTF7 support */
  14. require_once(SM_PATH . 'functions/imap_utf7_local.php');
  15. global $boxesnew;
  16. /**
  17. * Mailboxes class
  18. *
  19. * FIXME. This class should be extracted and placed in a separate file that
  20. * can be included before we start the session. That makes caching of the tree
  21. * possible. On a refresh mailboxes from left_main.php the only function that
  22. * should be called is the sqimap_get_status_mbx_tree. In case of subscribe
  23. * / rename / delete / new we have to create methods for adding/changing the
  24. * mailbox in the mbx_tree without the need for a refresh.
  25. * @package squirrelmail
  26. */
  27. class mailboxes {
  28. var $mailboxname_full = '', $mailboxname_sub= '', $is_noselect = false, $is_noinferiors = false,
  29. $is_special = false, $is_root = false, $is_inbox = false, $is_sent = false,
  30. $is_trash = false, $is_draft = false, $mbxs = array(),
  31. $unseen = false, $total = false;
  32. function addMbx($mbx, $delimiter, $start, $specialfirst) {
  33. $ary = explode($delimiter, $mbx->mailboxname_full);
  34. $mbx_parent =& $this;
  35. for ($i = $start, $c = count($ary)-1; $i < $c; $i++) {
  36. $mbx_childs =& $mbx_parent->mbxs;
  37. $found = false;
  38. if ($mbx_childs) {
  39. foreach ($mbx_childs as $key => $parent) {
  40. if ($parent->mailboxname_sub == $ary[$i]) {
  41. $mbx_parent =& $mbx_parent->mbxs[$key];
  42. $found = true;
  43. break;
  44. }
  45. }
  46. }
  47. if (!$found) {
  48. $no_select_mbx = new mailboxes();
  49. if (isset($mbx_parent->mailboxname_full) && $mbx_parent->mailboxname_full != '') {
  50. $no_select_mbx->mailboxname_full = $mbx_parent->mailboxname_full.$delimiter.$ary[$i];
  51. } else {
  52. $no_select_mbx->mailboxname_full = $ary[$i];
  53. }
  54. $no_select_mbx->mailboxname_sub = $ary[$i];
  55. $no_select_mbx->is_noselect = true;
  56. $mbx_parent->mbxs[] = $no_select_mbx;
  57. $i--;
  58. }
  59. }
  60. $mbx_parent->mbxs[] = $mbx;
  61. if ($mbx->is_special && $specialfirst) {
  62. usort($mbx_parent->mbxs, 'sortSpecialMbx');
  63. }
  64. }
  65. }
  66. function sortSpecialMbx($a, $b) {
  67. if ($a->is_inbox) {
  68. $acmp = '0'. $a->mailboxname_full;
  69. } else if ($a->is_special) {
  70. $acmp = '1'. $a->mailboxname_full;
  71. } else {
  72. $acmp = '2' . $a->mailboxname_full;
  73. }
  74. if ($b->is_inbox) {
  75. $bcmp = '0'. $b->mailboxname_full;
  76. }else if ($b->is_special) {
  77. $bcmp = '1' . $b->mailboxname_full;
  78. } else {
  79. $bcmp = '2' . $b->mailboxname_full;
  80. }
  81. return strnatcasecmp($acmp, $bcmp);
  82. }
  83. function compact_mailboxes_response($ary)
  84. {
  85. /*
  86. * Workaround for mailboxes returned as literal
  87. * FIXME : Doesn't work if the mailbox name is multiple lines
  88. * (larger then fgets buffer)
  89. */
  90. for ($i = 0, $iCnt=count($ary); $i < $iCnt; $i++) {
  91. if (isset($ary[$i + 1]) && substr($ary[$i], -3) == "}\r\n") {
  92. if (ereg("^(\\* [A-Z]+.*)\\{[0-9]+\\}([ \n\r\t]*)$",
  93. $ary[$i], $regs)) {
  94. $ary[$i] = $regs[1] . '"' . addslashes(trim($ary[$i+1])) . '"' . $regs[2];
  95. array_splice($ary, $i+1, 2);
  96. }
  97. }
  98. }
  99. /* remove duplicates and ensure array is contiguous */
  100. return array_values(array_unique($ary));
  101. }
  102. /**
  103. * Extract the mailbox name from an untagged LIST (7.2.2) or LSUB (7.2.3) answer
  104. * (LIST|LSUB) (<Flags list>) (NIL|"<separator atom>") <mailbox name string>\r\n
  105. * mailbox name in quoted string MUST be unquoted and stripslashed (sm API)
  106. */
  107. function find_mailbox_name($line)
  108. {
  109. if (preg_match('/^\* (?:LIST|LSUB) \([^\)]*\) (?:NIL|\"[^\"]*\") ([^\r\n]*)[\r\n]*$/i', $line, $regs)) {
  110. if (substr($regs[1], 0, 1) == '"')
  111. return stripslashes(substr($regs[1], 1, -1));
  112. return $regs[1];
  113. }
  114. return '';
  115. }
  116. /**
  117. * @return bool whether this is a Noselect mailbox.
  118. */
  119. function check_is_noselect ($lsub_line) {
  120. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noselect[^\)]*\)/i", $lsub_line);
  121. }
  122. /**
  123. * @return bool whether this is a Noinferiors mailbox.
  124. */
  125. function check_is_noinferiors ($lsub_line) {
  126. return preg_match("/^\* (LSUB|LIST) \([^\)]*\\\\Noinferiors[^\)]*\)/i", $lsub_line);
  127. }
  128. /**
  129. * If $haystack is a full mailbox name, and $needle is the mailbox
  130. * separator character, returns the second last part of the full
  131. * mailbox name (i.e. the mailbox's parent mailbox)
  132. */
  133. function readMailboxParent($haystack, $needle) {
  134. if ($needle == '') {
  135. $ret = '';
  136. } else {
  137. $parts = explode($needle, $haystack);
  138. $elem = array_pop($parts);
  139. while ($elem == '' && count($parts)) {
  140. $elem = array_pop($parts);
  141. }
  142. $ret = join($needle, $parts);
  143. }
  144. return( $ret );
  145. }
  146. /**
  147. * Check if $subbox is below the specified $parentbox
  148. */
  149. function isBoxBelow( $subbox, $parentbox ) {
  150. global $delimiter;
  151. /*
  152. * Eliminate the obvious mismatch, where the
  153. * subfolder path is shorter than that of the potential parent
  154. */
  155. if ( strlen($subbox) < strlen($parentbox) ) {
  156. return false;
  157. }
  158. /* check for delimiter */
  159. if (!substr($parentbox,-1) == $delimiter) {
  160. $parentbox.=$delimiter;
  161. }
  162. if (substr($subbox,0,strlen($parentbox)) == $parentbox) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Defines special mailboxes: given a mailbox name, it checks if this is a
  170. * "special" one: INBOX, Trash, Sent or Draft.
  171. */
  172. function isSpecialMailbox( $box ) {
  173. $ret = ( (strtolower($box) == 'inbox') ||
  174. isTrashMailbox($box) || isSentMailbox($box) || isDraftMailbox($box) );
  175. if ( !$ret ) {
  176. $ret = boolean_hook_function('special_mailbox',$box,1);
  177. }
  178. return $ret;
  179. }
  180. /**
  181. * @return bool whether this is a Trash folder
  182. */
  183. function isTrashMailbox ($box) {
  184. global $trash_folder, $move_to_trash;
  185. return $move_to_trash && $trash_folder &&
  186. ( $box == $trash_folder || isBoxBelow($box, $trash_folder) );
  187. }
  188. /**
  189. * @return bool whether this is a Sent folder
  190. */
  191. function isSentMailbox($box) {
  192. global $sent_folder, $move_to_sent;
  193. return $move_to_sent && $sent_folder &&
  194. ( $box == $sent_folder || isBoxBelow($box, $sent_folder) );
  195. }
  196. /**
  197. * @return bool whether this is a Draft folder
  198. */
  199. function isDraftMailbox($box) {
  200. global $draft_folder, $save_as_draft;
  201. return $save_as_draft &&
  202. ( $box == $draft_folder || isBoxBelow($box, $draft_folder) );
  203. }
  204. /**
  205. * Expunges a mailbox, ie. delete all contents.
  206. */
  207. function sqimap_mailbox_expunge ($imap_stream, $mailbox, $handle_errors = true, $id='') {
  208. if ($id) {
  209. if (is_array($id)) {
  210. $id = sqimap_message_list_squisher($id);
  211. }
  212. $id = ' '.$id;
  213. $uid = TRUE;
  214. } else {
  215. $uid = false;
  216. }
  217. $read = sqimap_run_command($imap_stream, 'EXPUNGE'.$id, $handle_errors,
  218. $response, $message, $uid);
  219. $cnt = 0;
  220. if (is_array($read)) {
  221. foreach ($read as $r) {
  222. if (preg_match('/^\*\s[0-9]+\sEXPUNGE/AUi',$r,$regs)) {
  223. $cnt++;
  224. }
  225. }
  226. }
  227. return $cnt;
  228. }
  229. /**
  230. * Expunge specified message, updated $msgs and $msort
  231. *
  232. * Until Marc and I come up with a better way to maintain
  233. * these stupid arrays, we'll use this wrapper function to
  234. * remove the message with the matching UID .. the order
  235. * won't be changed - the array element for the message
  236. * will just be removed.
  237. */
  238. function sqimap_mailbox_expunge_dmn($message_id)
  239. {
  240. global $msgs, $msort, $sort, $imapConnection,
  241. $mailbox, $mbx_response, $auto_expunge,
  242. $sort, $allow_server_sort, $thread_sort_messages, $allow_thread_sort,
  243. $username, $data_dir;
  244. // Got to grab this out of prefs, since it isn't saved from mailbox_view.php
  245. if ($allow_thread_sort) {
  246. $thread_sort_messages = getPref($data_dir, $username, "thread_$mailbox",0);
  247. }
  248. for ($i = 0; $i < count($msort); $i++) {
  249. if ($msgs[$i]['ID'] == $message_id) {
  250. break;
  251. }
  252. }
  253. unset($msgs[$i]);
  254. unset($msort[$i]);
  255. $msgs = array_values($msgs);
  256. $msort = array_values($msort);
  257. sqsession_register($msgs, 'msgs');
  258. sqsession_register($msort, 'msort');
  259. if ($auto_expunge) {
  260. sqimap_mailbox_expunge($imapConnection, $mailbox, true);
  261. }
  262. // And after all that mucking around, update the sort list!
  263. // Remind me why the hell we need those two arrays again?!
  264. if ( $allow_thread_sort && $thread_sort_messages ) {
  265. $server_sort_array = get_thread_sort($imapConnection);
  266. } elseif ( $allow_server_sort ) {
  267. $server_sort_array = sqimap_get_sort_order($imapConnection, $sort, $mbx_response);
  268. } else {
  269. $server_sort_array = sqimap_get_php_sort_order($imapConnection, $mbx_response);
  270. }
  271. }
  272. /**
  273. * Checks whether or not the specified mailbox exists
  274. */
  275. function sqimap_mailbox_exists ($imap_stream, $mailbox) {
  276. if (!isset($mailbox) || empty($mailbox)) {
  277. return false;
  278. }
  279. $mbx = sqimap_run_command($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mailbox),
  280. true, $response, $message);
  281. return isset($mbx[0]);
  282. }
  283. /**
  284. * Selects a mailbox
  285. */
  286. function sqimap_mailbox_select ($imap_stream, $mailbox) {
  287. global $auto_expunge;
  288. if ($mailbox == 'None') {
  289. return;
  290. }
  291. $read = sqimap_run_command($imap_stream, 'SELECT ' . sqimap_encode_mailbox_name($mailbox),
  292. true, $response, $message);
  293. $result = array();
  294. for ($i = 0, $cnt = count($read); $i < $cnt; $i++) {
  295. if (preg_match('/^\*\s+OK\s\[(\w+)\s(\w+)\]/',$read[$i], $regs)) {
  296. $result[strtoupper($regs[1])] = $regs[2];
  297. } else if (preg_match('/^\*\s([0-9]+)\s(\w+)/',$read[$i], $regs)) {
  298. $result[strtoupper($regs[2])] = $regs[1];
  299. } else {
  300. if (preg_match("/PERMANENTFLAGS(.*)/i",$read[$i], $regs)) {
  301. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/","/\]/") ,'', $regs[1])) ;
  302. $result['PERMANENTFLAGS'] = $regs[1];
  303. } else if (preg_match("/FLAGS(.*)/i",$read[$i], $regs)) {
  304. $regs[1]=trim(preg_replace ( array ("/\(/","/\)/") ,'', $regs[1])) ;
  305. $result['FLAGS'] = $regs[1];
  306. }
  307. }
  308. }
  309. if (preg_match('/^\[(.+)\]/',$message, $regs)) {
  310. $result['RIGHTS']=$regs[1];
  311. }
  312. if ($auto_expunge) {
  313. $tmp = sqimap_run_command($imap_stream, 'EXPUNGE', false, $a, $b);
  314. }
  315. return $result;
  316. }
  317. /**
  318. * Creates a folder.
  319. */
  320. function sqimap_mailbox_create ($imap_stream, $mailbox, $type) {
  321. global $delimiter;
  322. if (strtolower($type) == 'noselect') {
  323. $mailbox .= $delimiter;
  324. }
  325. $read_ary = sqimap_run_command($imap_stream, 'CREATE ' .
  326. sqimap_encode_mailbox_name($mailbox),
  327. true, $response, $message);
  328. sqimap_subscribe ($imap_stream, $mailbox);
  329. }
  330. /**
  331. * Subscribes to an existing folder.
  332. */
  333. function sqimap_subscribe ($imap_stream, $mailbox) {
  334. $read_ary = sqimap_run_command($imap_stream, 'SUBSCRIBE ' .
  335. sqimap_encode_mailbox_name($mailbox),
  336. true, $response, $message);
  337. }
  338. /**
  339. * Unsubscribes from an existing folder
  340. */
  341. function sqimap_unsubscribe ($imap_stream, $mailbox) {
  342. $read_ary = sqimap_run_command($imap_stream, 'UNSUBSCRIBE ' .
  343. sqimap_encode_mailbox_name($mailbox),
  344. false, $response, $message);
  345. }
  346. /**
  347. * Deletes the given folder
  348. */
  349. function sqimap_mailbox_delete ($imap_stream, $mailbox) {
  350. global $data_dir, $username;
  351. sqimap_unsubscribe ($imap_stream, $mailbox);
  352. $read_ary = sqimap_run_command($imap_stream, 'DELETE ' .
  353. sqimap_encode_mailbox_name($mailbox),
  354. true, $response, $message);
  355. if ($response !== 'OK') {
  356. // subscribe again
  357. sqimap_subscribe ($imap_stream, $mailbox);
  358. } else {
  359. do_hook_function('rename_or_delete_folder', $args = array($mailbox, 'delete', ''));
  360. removePref($data_dir, $username, "thread_$mailbox");
  361. }
  362. }
  363. /**
  364. * Determines if the user is subscribed to the folder or not
  365. */
  366. function sqimap_mailbox_is_subscribed($imap_stream, $folder) {
  367. $boxesall = sqimap_mailbox_list ($imap_stream);
  368. foreach ($boxesall as $ref) {
  369. if ($ref['unformatted'] == $folder) {
  370. return true;
  371. }
  372. }
  373. return false;
  374. }
  375. /**
  376. * Renames a mailbox.
  377. */
  378. function sqimap_mailbox_rename( $imap_stream, $old_name, $new_name ) {
  379. if ( $old_name != $new_name ) {
  380. global $delimiter, $imap_server_type, $data_dir, $username;
  381. if ( substr( $old_name, -1 ) == $delimiter ) {
  382. $old_name = substr( $old_name, 0, strlen( $old_name ) - 1 );
  383. $new_name = substr( $new_name, 0, strlen( $new_name ) - 1 );
  384. $postfix = $delimiter;
  385. } else {
  386. $postfix = '';
  387. }
  388. $boxesall = sqimap_mailbox_list($imap_stream);
  389. $cmd = 'RENAME ' . sqimap_encode_mailbox_name($old_name) .
  390. ' ' . sqimap_encode_mailbox_name($new_name);
  391. $data = sqimap_run_command($imap_stream, $cmd, true, $response, $message);
  392. sqimap_unsubscribe($imap_stream, $old_name.$postfix);
  393. $oldpref = getPref($data_dir, $username, 'thread_'.$old_name.$postfix);
  394. removePref($data_dir, $username, 'thread_'.$old_name.$postfix);
  395. sqimap_subscribe($imap_stream, $new_name.$postfix);
  396. setPref($data_dir, $username, 'thread_'.$new_name.$postfix, $oldpref);
  397. do_hook_function('rename_or_delete_folder',$args = array($old_name, 'rename', $new_name));
  398. $l = strlen( $old_name ) + 1;
  399. $p = 'unformatted';
  400. foreach ($boxesall as $box) {
  401. if (substr($box[$p], 0, $l) == $old_name . $delimiter) {
  402. $new_sub = $new_name . $delimiter . substr($box[$p], $l);
  403. if ($imap_server_type == 'cyrus') {
  404. $cmd = 'RENAME "' . $box[$p] . '" "' . $new_sub . '"';
  405. $data = sqimap_run_command($imap_stream, $cmd, true,
  406. $response, $message);
  407. }
  408. sqimap_unsubscribe($imap_stream, $box[$p]);
  409. $oldpref = getPref($data_dir, $username, 'thread_'.$box[$p]);
  410. removePref($data_dir, $username, 'thread_'.$box[$p]);
  411. sqimap_subscribe($imap_stream, $new_sub);
  412. setPref($data_dir, $username, 'thread_'.$new_sub, $oldpref);
  413. do_hook_function('rename_or_delete_folder',
  414. $args = array($box[$p], 'rename', $new_sub));
  415. }
  416. }
  417. }
  418. }
  419. /**
  420. * Formats a mailbox into parts for the $boxesall array
  421. *
  422. * The parts are:
  423. *
  424. * raw - Raw LIST/LSUB response from the IMAP server
  425. * formatted - nicely formatted folder name
  426. * unformatted - unformatted, but with delimiter at end removed
  427. * unformatted-dm - folder name as it appears in raw response
  428. * unformatted-disp - unformatted without $folder_prefix
  429. */
  430. function sqimap_mailbox_parse ($line, $line_lsub) {
  431. global $folder_prefix, $delimiter;
  432. /* Process each folder line */
  433. for ($g = 0, $cnt = count($line); $g < $cnt; ++$g) {
  434. /* Store the raw IMAP reply */
  435. if (isset($line[$g])) {
  436. $boxesall[$g]['raw'] = $line[$g];
  437. } else {
  438. $boxesall[$g]['raw'] = '';
  439. }
  440. /* Count number of delimiters ($delimiter) in folder name */
  441. $mailbox = /*trim(*/$line_lsub[$g]/*)*/;
  442. $dm_count = substr_count($mailbox, $delimiter);
  443. if (substr($mailbox, -1) == $delimiter) {
  444. /* If name ends in delimiter, decrement count by one */
  445. $dm_count--;
  446. }
  447. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  448. $boxesallbyname[$mailbox] = $g;
  449. $parentfolder = readMailboxParent($mailbox, $delimiter);
  450. if ( (strtolower(substr($mailbox, 0, 5)) == "inbox") ||
  451. (substr($mailbox, 0, strlen($folder_prefix)) == $folder_prefix) ||
  452. (isset($boxesallbyname[$parentfolder]) &&
  453. (strlen($parentfolder) > 0) ) ) {
  454. $indent = $dm_count - (substr_count($folder_prefix, $delimiter));
  455. if ($indent > 0) {
  456. $boxesall[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $indent);
  457. } else {
  458. $boxesall[$g]['formatted'] = '';
  459. }
  460. $boxesall[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  461. } else {
  462. $boxesall[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  463. }
  464. $boxesall[$g]['unformatted-dm'] = $mailbox;
  465. if (substr($mailbox, -1) == $delimiter) {
  466. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  467. }
  468. $boxesall[$g]['unformatted'] = $mailbox;
  469. if (substr($mailbox,0,strlen($folder_prefix))==$folder_prefix) {
  470. $mailbox = substr($mailbox, strlen($folder_prefix));
  471. }
  472. $boxesall[$g]['unformatted-disp'] = $mailbox;
  473. $boxesall[$g]['id'] = $g;
  474. $boxesall[$g]['flags'] = array();
  475. if (isset($line[$g])) {
  476. ereg("\(([^)]*)\)",$line[$g],$regs);
  477. // FIXME Flags do contain the \ character. \NoSelect \NoInferiors
  478. // and $MDNSent <= last one doesn't have the \
  479. // It's better to follow RFC3501 instead of using our own naming.
  480. $flags = trim(strtolower(str_replace('\\', '',$regs[1])));
  481. if ($flags) {
  482. $boxesall[$g]['flags'] = explode(' ', $flags);
  483. }
  484. }
  485. }
  486. return $boxesall;
  487. }
  488. /**
  489. * Returns list of options (to be echoed into select statement
  490. * based on available mailboxes and separators
  491. * Caller should surround options with <SELECT..> </SELECT> and
  492. * any formatting.
  493. * $imap_stream - $imapConnection to query for mailboxes
  494. * $show_selected - array containing list of mailboxes to pre-select (0 if none)
  495. * $folder_skip - array of folders to keep out of option list (compared in lower)
  496. * $boxes - list of already fetched boxes (for places like folder panel, where
  497. * you know these options will be shown 3 times in a row.. (most often unset).
  498. * $flag - flag to check for in mailbox flags, used to filter out mailboxes.
  499. * 'noselect' by default to remove unselectable mailboxes.
  500. * 'noinferiors' used to filter out folders that can not contain subfolders.
  501. * NULL to avoid flag check entirely.
  502. * NOTE: noselect and noiferiors are used internally. The IMAP representation is
  503. * \NoSelect and \NoInferiors
  504. * $use_long_format - override folder display preference and always show full folder name.
  505. */
  506. function sqimap_mailbox_option_list($imap_stream, $show_selected = 0, $folder_skip = 0, $boxes = 0,
  507. $flag = 'noselect', $use_long_format = false ) {
  508. global $username, $data_dir;
  509. $mbox_options = '';
  510. if ( $use_long_format ) {
  511. $shorten_box_names = 0;
  512. } else {
  513. $shorten_box_names = getPref($data_dir, $username, 'mailbox_select_style', SMPREF_OFF);
  514. }
  515. if ($boxes == 0) {
  516. $boxes = sqimap_mailbox_list($imap_stream);
  517. }
  518. foreach ($boxes as $boxes_part) {
  519. if ($flag == NULL || !in_array($flag, $boxes_part['flags'])) {
  520. $box = $boxes_part['unformatted'];
  521. if ($folder_skip != 0 && in_array($box, $folder_skip) ) {
  522. continue;
  523. }
  524. $lowerbox = strtolower($box);
  525. // mailboxes are casesensitive => inbox.sent != inbox.Sent
  526. // nevermind, to many dependencies this should be fixed!
  527. if (strtolower($box) == 'inbox') { // inbox is special and not casesensitive
  528. $box2 = _("INBOX");
  529. } else {
  530. switch ($shorten_box_names)
  531. {
  532. case 2: /* delimited, style = 2 */
  533. $box2 = str_replace('&nbsp;&nbsp;', '.&nbsp;', $boxes_part['formatted']);
  534. break;
  535. case 1: /* indent, style = 1 */
  536. $box2 = $boxes_part['formatted'];
  537. break;
  538. default: /* default, long names, style = 0 */
  539. $box2 = str_replace(' ', '&nbsp;', htmlspecialchars(imap_utf7_decode_local($boxes_part['unformatted-disp'])));
  540. break;
  541. }
  542. }
  543. if ($show_selected != 0 && in_array($lowerbox, $show_selected) ) {
  544. $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'" SELECTED>'.$box2.'</OPTION>' . "\n";
  545. } else {
  546. $mbox_options .= '<OPTION VALUE="' . htmlspecialchars($box) .'">'.$box2.'</OPTION>' . "\n";
  547. }
  548. }
  549. }
  550. return $mbox_options;
  551. }
  552. /**
  553. * Returns sorted mailbox lists in several different ways.
  554. * See comment on sqimap_mailbox_parse() for info about the returned array.
  555. */
  556. function sqimap_mailbox_list($imap_stream) {
  557. global $default_folder_prefix;
  558. if (!isset($boxesnew)) {
  559. global $data_dir, $username, $list_special_folders_first,
  560. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  561. $move_to_trash, $move_to_sent, $save_as_draft,
  562. $delimiter, $noselect_fix_enable;
  563. $inbox_in_list = false;
  564. $inbox_subscribed = false;
  565. require_once(SM_PATH . 'include/load_prefs.php');
  566. if ($noselect_fix_enable) {
  567. $lsub_args = "LSUB \"$folder_prefix\" \"*%\"";
  568. } else {
  569. $lsub_args = "LSUB \"$folder_prefix\" \"*\"";
  570. }
  571. /* LSUB array */
  572. $lsub_ary = sqimap_run_command ($imap_stream, $lsub_args,
  573. true, $response, $message);
  574. $lsub_ary = compact_mailboxes_response($lsub_ary);
  575. $sorted_lsub_ary = array();
  576. for ($i = 0, $cnt = count($lsub_ary);$i < $cnt; $i++) {
  577. $temp_mailbox_name = find_mailbox_name($lsub_ary[$i]);
  578. $sorted_lsub_ary[] = $temp_mailbox_name;
  579. if (!$inbox_subscribed && strtoupper($temp_mailbox_name) == 'INBOX') {
  580. $inbox_subscribed = true;
  581. }
  582. }
  583. /* natural sort mailboxes */
  584. if (isset($sorted_lsub_ary)) {
  585. usort($sorted_lsub_ary, 'strnatcasecmp');
  586. }
  587. /*
  588. * The LSUB response doesn't provide us information about \Noselect
  589. * mail boxes. The LIST response does, that's why we need to do a LIST
  590. * call to retrieve the flags for the mailbox
  591. * Note: according RFC2060 an imap server may provide \NoSelect flags in the LSUB response.
  592. * in other words, we cannot rely on it.
  593. */
  594. $sorted_list_ary = array();
  595. for ($i=0; $i < count($sorted_lsub_ary); $i++) {
  596. if (substr($sorted_lsub_ary[$i], -1) == $delimiter) {
  597. $mbx = substr($sorted_lsub_ary[$i], 0, strlen($sorted_lsub_ary[$i])-1);
  598. }
  599. else {
  600. $mbx = $sorted_lsub_ary[$i];
  601. }
  602. $mbx = stripslashes($mbx);
  603. $read = sqimap_run_command ($imap_stream, 'LIST "" ' . sqimap_encode_mailbox_name($mbx),
  604. true, $response, $message);
  605. $read = compact_mailboxes_response($read);
  606. if (isset($read[0])) {
  607. $sorted_list_ary[$i] = $read[0];
  608. } else {
  609. $sorted_list_ary[$i] = '';
  610. }
  611. }
  612. /*
  613. * Just in case they're not subscribed to their inbox,
  614. * we'll get it for them anyway
  615. */
  616. if (!$inbox_subscribed) {
  617. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
  618. true, $response, $message);
  619. $sorted_list_ary[] = implode('', compact_mailboxes_response($inbox_ary));
  620. $sorted_lsub_ary[] = find_mailbox_name($inbox_ary[0]);
  621. }
  622. $boxesall = sqimap_mailbox_parse ($sorted_list_ary, $sorted_lsub_ary);
  623. /* Now, lets sort for special folders */
  624. $boxesnew = $used = array();
  625. /* Find INBOX */
  626. $cnt = count($boxesall);
  627. $used = array_pad($used,$cnt,false);
  628. for($k = 0; $k < $cnt; ++$k) {
  629. if (strtolower($boxesall[$k]['unformatted']) == 'inbox') {
  630. $boxesnew[] = $boxesall[$k];
  631. $used[$k] = true;
  632. break;
  633. }
  634. }
  635. /* List special folders and their subfolders, if requested. */
  636. if ($list_special_folders_first) {
  637. for($k = 0; $k < $cnt; ++$k) {
  638. if (!$used[$k] && isSpecialMailbox($boxesall[$k]['unformatted'])) {
  639. $boxesnew[] = $boxesall[$k];
  640. $used[$k] = true;
  641. }
  642. }
  643. }
  644. /* Rest of the folders */
  645. for($k = 0; $k < $cnt; $k++) {
  646. if (!$used[$k]) {
  647. $boxesnew[] = $boxesall[$k];
  648. }
  649. }
  650. }
  651. return $boxesnew;
  652. }
  653. /**
  654. * Returns a list of all folders, subscribed or not
  655. */
  656. function sqimap_mailbox_list_all($imap_stream) {
  657. global $list_special_folders_first, $folder_prefix, $delimiter;
  658. $read_ary = sqimap_run_command($imap_stream,"LIST \"$folder_prefix\" *",true,$response, $message,false);
  659. $read_ary = compact_mailboxes_response($read_ary);
  660. $g = 0;
  661. $phase = 'inbox';
  662. $fld_pre_length = strlen($folder_prefix);
  663. for ($i = 0, $cnt = count($read_ary); $i < $cnt; $i++) {
  664. /* Store the raw IMAP reply */
  665. $boxes[$g]['raw'] = $read_ary[$i];
  666. /* Count number of delimiters ($delimiter) in folder name */
  667. $mailbox = find_mailbox_name($read_ary[$i]);
  668. $dm_count = substr_count($mailbox, $delimiter);
  669. if (substr($mailbox, -1) == $delimiter) {
  670. /* If name ends in delimiter - decrement count by one */
  671. $dm_count--;
  672. }
  673. /* Format folder name, but only if it's a INBOX.* or has a parent. */
  674. $boxesallbyname[$mailbox] = $g;
  675. $parentfolder = readMailboxParent($mailbox, $delimiter);
  676. if((eregi('^inbox'.quotemeta($delimiter), $mailbox)) ||
  677. (ereg('^'.$folder_prefix, $mailbox)) ||
  678. ( isset($boxesallbyname[$parentfolder]) && (strlen($parentfolder) > 0) ) ) {
  679. if ($dm_count) {
  680. $boxes[$g]['formatted'] = str_repeat('&nbsp;&nbsp;', $dm_count);
  681. } else {
  682. $boxes[$g]['formatted'] = '';
  683. }
  684. $boxes[$g]['formatted'] .= imap_utf7_decode_local(readShortMailboxName($mailbox, $delimiter));
  685. } else {
  686. $boxes[$g]['formatted'] = imap_utf7_decode_local($mailbox);
  687. }
  688. $boxes[$g]['unformatted-dm'] = $mailbox;
  689. if (substr($mailbox, -1) == $delimiter) {
  690. $mailbox = substr($mailbox, 0, strlen($mailbox) - 1);
  691. }
  692. $boxes[$g]['unformatted'] = $mailbox;
  693. $boxes[$g]['unformatted-disp'] = substr($mailbox,$fld_pre_length);
  694. $boxes[$g]['id'] = $g;
  695. /* Now lets get the flags for this mailbox */
  696. $read_mlbx = $read_ary[$i];
  697. $flags = substr($read_mlbx, strpos($read_mlbx, '(')+1);
  698. $flags = substr($flags, 0, strpos($flags, ')'));
  699. $flags = str_replace('\\', '', $flags);
  700. $flags = trim(strtolower($flags));
  701. if ($flags) {
  702. $boxes[$g]['flags'] = explode(' ', $flags);
  703. } else {
  704. $boxes[$g]['flags'] = array();
  705. }
  706. $g++;
  707. }
  708. if(is_array($boxes)) {
  709. sort ($boxes);
  710. }
  711. return $boxes;
  712. }
  713. function sqimap_mailbox_tree($imap_stream) {
  714. global $boxesnew, $default_folder_prefix, $unseen_notify, $unseen_type;
  715. if (!isset($boxesnew)) {
  716. global $data_dir, $username, $list_special_folders_first,
  717. $folder_prefix, $delimiter, $trash_folder, $move_to_trash,
  718. $imap_server_type;
  719. $inbox_in_list = false;
  720. $inbox_subscribed = false;
  721. $noselect = false;
  722. $noinferiors = false;
  723. require_once(SM_PATH . 'include/load_prefs.php');
  724. /* LSUB array */
  725. $lsub_ary = sqimap_run_command ($imap_stream, "LSUB \"$folder_prefix\" \"*\"",
  726. true, $response, $message);
  727. $lsub_ary = compact_mailboxes_response($lsub_ary);
  728. /* Check to see if we have an INBOX */
  729. $has_inbox = false;
  730. for ($i = 0, $cnt = count($lsub_ary); $i < $cnt; $i++) {
  731. if (preg_match("/^\*\s+LSUB.*\s\"?INBOX\"?[^(\/\.)].*$/i",$lsub_ary[$i])) {
  732. $lsub_ary[$i] = strtoupper($lsub_ary[$i]);
  733. // in case of an unsubscribed inbox an imap server can
  734. // return the inbox in the lsub results with a \NoSelect
  735. // flag.
  736. if (!preg_match("/\*\s+LSUB\s+\(.*\\\\NoSelect.*\).*/i",$lsub_ary[$i])) {
  737. $has_inbox = true;
  738. } else {
  739. // remove the result and request it again with a list
  740. // response at a later stage.
  741. unset($lsub_ary[$i]);
  742. // re-index the array otherwise the addition of the LIST
  743. // response will fail in PHP 4.1.2 and probably other older versions
  744. $lsub_ary = array_values($lsub_ary);
  745. }
  746. break;
  747. }
  748. }
  749. if ($has_inbox == false) {
  750. // do a list request for inbox because we should always show
  751. // inbox even if the user isn't subscribed to it.
  752. $inbox_ary = sqimap_run_command ($imap_stream, 'LIST "" INBOX',
  753. true, $response, $message);
  754. $inbox_ary = compact_mailboxes_response($inbox_ary);
  755. if (count($inbox_ary)) {
  756. $lsub_ary[] = $inbox_ary[0];
  757. }
  758. }
  759. /*
  760. * Section about removing the last element was removed
  761. * We don't return "* OK" anymore from sqimap_read_data
  762. */
  763. $sorted_lsub_ary = array();
  764. $cnt = count($lsub_ary);
  765. for ($i = 0; $i < $cnt; $i++) {
  766. $mbx = find_mailbox_name($lsub_ary[$i]);
  767. // only do the noselect test if !uw, is checked later. FIX ME see conf.pl setting
  768. if ($imap_server_type != "uw") {
  769. $noselect = check_is_noselect($lsub_ary[$i]);
  770. $noinferiors = check_is_noinferiors($lsub_ary[$i]);
  771. }
  772. if (substr($mbx, -1) == $delimiter) {
  773. $mbx = substr($mbx, 0, strlen($mbx) - 1);
  774. }
  775. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  776. }
  777. // FIX ME this requires a config setting inside conf.pl instead of checking on server type
  778. if ($imap_server_type == "uw") {
  779. $aQuery = array();
  780. $aTag = array();
  781. // prepare an array with queries
  782. foreach ($sorted_lsub_ary as $aMbx) {
  783. $mbx = stripslashes($aMbx['mbx']);
  784. sqimap_prepare_pipelined_query('LIST "" ' . sqimap_encode_mailbox_name($mbx), $tag, $aQuery, false);
  785. $aTag[$tag] = $mbx;
  786. }
  787. $sorted_lsub_ary = array();
  788. // execute all the queries at once
  789. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  790. foreach($aTag as $tag => $mbx) {
  791. if ($aServerResponse[$tag] == 'OK') {
  792. $sResponse = implode('', $aResponse[$tag]);
  793. $noselect = check_is_noselect($sResponse);
  794. $noinferiors = check_is_noinferiors($sResponse);
  795. $sorted_lsub_ary[] = array ('mbx' => $mbx, 'noselect' => $noselect, 'noinferiors' => $noinferiors);
  796. }
  797. }
  798. $cnt = count($sorted_lsub_ary);
  799. }
  800. $sorted_lsub_ary = array_values($sorted_lsub_ary);
  801. array_multisort($sorted_lsub_ary, SORT_ASC, SORT_REGULAR);
  802. $boxesnew = sqimap_fill_mailbox_tree($sorted_lsub_ary,false,$imap_stream);
  803. return $boxesnew;
  804. }
  805. }
  806. function sqimap_fill_mailbox_tree($mbx_ary, $mbxs=false,$imap_stream) {
  807. global $data_dir, $username, $list_special_folders_first,
  808. $folder_prefix, $trash_folder, $sent_folder, $draft_folder,
  809. $move_to_trash, $move_to_sent, $save_as_draft,
  810. $delimiter, $imap_server_type;
  811. $special_folders = array ('INBOX', $sent_folder, $draft_folder, $trash_folder);
  812. /* create virtual root node */
  813. $mailboxes= new mailboxes();
  814. $mailboxes->is_root = true;
  815. $trail_del = false;
  816. $start = 0;
  817. if (isset($folder_prefix) && ($folder_prefix != '')) {
  818. $start = substr_count($folder_prefix,$delimiter);
  819. if (strrpos($folder_prefix, $delimiter) == (strlen($folder_prefix)-1)) {
  820. $trail_del = true;
  821. $mailboxes->mailboxname_full = substr($folder_prefix,0, (strlen($folder_prefix)-1));
  822. } else {
  823. $mailboxes->mailboxname_full = $folder_prefix;
  824. $start++;
  825. }
  826. $mailboxes->mailboxname_sub = $mailboxes->mailboxname_full;
  827. } else {
  828. $start = 0;
  829. }
  830. $cnt = count($mbx_ary);
  831. for ($i=0; $i < $cnt; $i++) {
  832. if ($mbx_ary[$i]['mbx'] !='' ) {
  833. $mbx = new mailboxes();
  834. $mailbox = $mbx_ary[$i]['mbx'];
  835. /*
  836. sent subfolders messes up using existing code as subfolders
  837. were marked, but the parents were ordered somewhere else in
  838. the list, despite having "special folders at top" option set.
  839. Need a better method than this.
  840. */
  841. /*
  842. if ($mailbox == 'INBOX') {
  843. $mbx->is_special = true;
  844. } elseif (stristr($trash_folder , $mailbox)) {
  845. $mbx->is_special = true;
  846. } elseif (stristr($sent_folder , $mailbox)) {
  847. $mbx->is_special = true;
  848. } elseif (stristr($draft_folder , $mailbox)) {
  849. $mbx->is_special = true;
  850. }
  851. switch ($mailbox) {
  852. case 'INBOX':
  853. $mbx->is_inbox = true;
  854. $mbx->is_special = true;
  855. $mbx_ary[$i]['noselect'] = false;
  856. break;
  857. case $trash_folder:
  858. $mbx->is_trash = true;
  859. $mbx->is_special = true;
  860. break;
  861. case $sent_folder:
  862. $mbx->is_sent = true;
  863. $mbx->is_special = true;
  864. break;
  865. case $draft_folder:
  866. $mbx->is_draft = true;
  867. $mbx->is_special = true;
  868. break;
  869. }
  870. */
  871. $mbx->is_special |= ($mbx->is_inbox = (strtoupper($mailbox) == 'INBOX'));
  872. $mbx->is_special |= ($mbx->is_trash = isTrashMailbox($mailbox));
  873. $mbx->is_special |= ($mbx->is_sent = isSentMailbox($mailbox));
  874. $mbx->is_special |= ($mbx->is_draft = isDraftMailbox($mailbox));
  875. if (!$mbx->is_special)
  876. $mbx->is_special = boolean_hook_function('special_mailbox', $mailbox, 1);
  877. if (isset($mbx_ary[$i]['unseen'])) {
  878. $mbx->unseen = $mbx_ary[$i]['unseen'];
  879. }
  880. if (isset($mbx_ary[$i]['nummessages'])) {
  881. $mbx->total = $mbx_ary[$i]['nummessages'];
  882. }
  883. $mbx->is_noselect = $mbx_ary[$i]['noselect'];
  884. $mbx->is_noinferiors = $mbx_ary[$i]['noinferiors'];
  885. $r_del_pos = strrpos($mbx_ary[$i]['mbx'], $delimiter);
  886. if ($r_del_pos) {
  887. $mbx->mailboxname_sub = substr($mbx_ary[$i]['mbx'],$r_del_pos+1);
  888. } else { /* mailbox is root folder */
  889. $mbx->mailboxname_sub = $mbx_ary[$i]['mbx'];
  890. }
  891. $mbx->mailboxname_full = $mbx_ary[$i]['mbx'];
  892. $mailboxes->addMbx($mbx, $delimiter, $start, $list_special_folders_first);
  893. }
  894. }
  895. sqimap_utf7_decode_mbx_tree($mailboxes);
  896. sqimap_get_status_mbx_tree($imap_stream,$mailboxes);
  897. return $mailboxes;
  898. }
  899. function sqimap_utf7_decode_mbx_tree(&$mbx_tree) {
  900. if (strtoupper($mbx_tree->mailboxname_full) == 'INBOX')
  901. $mbx_tree->mailboxname_sub = _("INBOX");
  902. else
  903. $mbx_tree->mailboxname_sub = imap_utf7_decode_local($mbx_tree->mailboxname_sub);
  904. if ($mbx_tree->mbxs) {
  905. $iCnt = count($mbx_tree->mbxs);
  906. for ($i=0;$i<$iCnt;++$i) {
  907. $mbxs_tree->mbxs[$i] = sqimap_utf7_decode_mbx_tree($mbx_tree->mbxs[$i]);
  908. }
  909. }
  910. }
  911. function sqimap_tree_to_ref_array(&$mbx_tree,&$aMbxs) {
  912. if ($mbx_tree)
  913. $aMbxs[] =& $mbx_tree;
  914. if ($mbx_tree->mbxs) {
  915. $iCnt = count($mbx_tree->mbxs);
  916. for ($i=0;$i<$iCnt;++$i) {
  917. sqimap_tree_to_ref_array($mbx_tree->mbxs[$i],$aMbxs);
  918. }
  919. }
  920. }
  921. function sqimap_get_status_mbx_tree($imap_stream,&$mbx_tree) {
  922. global $unseen_notify, $unseen_type, $trash_folder,$move_to_trash;
  923. $aMbxs = $aQuery = $aTag = array();
  924. sqimap_tree_to_ref_array($mbx_tree,$aMbxs);
  925. // remove the root node
  926. array_shift($aMbxs);
  927. if($unseen_notify == 3) {
  928. $cnt = count($aMbxs);
  929. for($i=0;$i<$cnt;++$i) {
  930. $oMbx =& $aMbxs[$i];
  931. if (!$oMbx->is_noselect) {
  932. $mbx = $oMbx->mailboxname_full;
  933. if ($unseen_type == 2 ||
  934. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  935. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (MESSAGES UNSEEN)';
  936. } else {
  937. $query = 'STATUS ' . sqimap_encode_mailbox_name($mbx) . ' (UNSEEN)';
  938. }
  939. sqimap_prepare_pipelined_query($query,$tag,$aQuery,false);
  940. } else {
  941. $oMbx->unseen = $oMbx->total = false;
  942. $tag = false;
  943. }
  944. $oMbx->tag = $tag;
  945. $aMbxs[$i] =& $oMbx;
  946. }
  947. // execute all the queries at once
  948. $aResponse = sqimap_run_pipelined_command ($imap_stream, $aQuery, false, $aServerResponse, $aServerMessage);
  949. $cnt = count($aMbxs);
  950. for($i=0;$i<$cnt;++$i) {
  951. $oMbx =& $aMbxs[$i];
  952. $tag = $oMbx->tag;
  953. if ($tag && $aServerResponse[$tag] == 'OK') {
  954. $sResponse = implode('', $aResponse[$tag]);
  955. if (preg_match('/UNSEEN\s+([0-9]+)/i', $sResponse, $regs)) {
  956. $oMbx->unseen = $regs[1];
  957. }
  958. if (preg_match('/MESSAGES\s+([0-9]+)/i', $sResponse, $regs)) {
  959. $oMbx->total = $regs[1];
  960. }
  961. }
  962. unset($oMbx->tag);
  963. }
  964. } else if ($unseen_notify == 2) { // INBOX only
  965. $cnt = count($aMbxs);
  966. for($i=0;$i<$cnt;++$i) {
  967. $oMbx =& $aMbxs[$i];
  968. if (strtoupper($oMbx->mailboxname_full) == 'INBOX' ||
  969. ($move_to_trash && $oMbx->mailboxname_full == $trash_folder)) {
  970. if ($unseen_type == 2 ||
  971. ($oMbx->mailboxname_full == $trash_folder && $move_to_trash)) {
  972. $aStatus = sqimap_status_messages($imap_stream,$oMbx->mailboxname_full);
  973. $oMbx->unseen = $aStatus['UNSEEN'];
  974. $oMbx->total = $aStatus['MESSAGES'];
  975. } else {
  976. $oMbx->unseen = sqimap_unseen_messages($imap_stream,$oMbx->mailboxname_full);
  977. }
  978. $aMbxs[$i] =& $oMbx;
  979. if (!$move_to_trash && $trash_folder) {
  980. break;
  981. } else {
  982. // trash comes after INBOX
  983. if ($oMbx->mailboxname_full == $trash_folder) {
  984. break;
  985. }
  986. }
  987. }
  988. }
  989. }
  990. }
  991. ?>