imap_general.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. <?php
  2. /**
  3. * imap_general.php
  4. *
  5. * This implements all functions that do general IMAP functions.
  6. *
  7. * @copyright &copy; 1999-2005 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage imap
  12. */
  13. /** Includes.. */
  14. require_once(SM_PATH . 'functions/page_header.php');
  15. require_once(SM_PATH . 'functions/auth.php');
  16. include_once(SM_PATH . 'functions/rfc822address.php');
  17. /**
  18. * Generates a new session ID by incrementing the last one used;
  19. * this ensures that each command has a unique ID.
  20. * @param bool $unique_id (since 1.3.0) controls use of unique
  21. * identifiers/message sequence numbers in IMAP commands. See IMAP
  22. * rfc 'UID command' chapter.
  23. * @return string IMAP session id of the form 'A000'.
  24. * @since 1.2.0
  25. */
  26. function sqimap_session_id($unique_id = FALSE) {
  27. static $sqimap_session_id = 1;
  28. if (!$unique_id) {
  29. return( sprintf("A%03d", $sqimap_session_id++) );
  30. } else {
  31. return( sprintf("A%03d", $sqimap_session_id++) . ' UID' );
  32. }
  33. }
  34. /**
  35. * Both send a command and accept the result from the command.
  36. * This is to allow proper session number handling.
  37. * @param stream $imap_stream imap connection resource
  38. * @param string $query imap command
  39. * @param boolean $handle_errors see sqimap_retrieve_imap_response()
  40. * @param array $response
  41. * @param array $message
  42. * @param boolean $unique_id (since 1.3.0) see sqimap_session_id().
  43. * @return mixed returns false on imap error. displays error message
  44. * if imap stream is not available.
  45. * @since 1.2.3
  46. */
  47. function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
  48. if ($imap_stream) {
  49. $sid = sqimap_session_id($unique_id);
  50. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  51. $tag_uid_a = explode(' ',trim($sid));
  52. $tag = $tag_uid_a[0];
  53. $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query );
  54. /* get the response and the message */
  55. $message = $message[$tag];
  56. $response = $response[$tag];
  57. return $read[$tag];
  58. } else {
  59. global $squirrelmail_language, $color;
  60. set_up_language($squirrelmail_language);
  61. require_once(SM_PATH . 'functions/display_messages.php');
  62. $string = "<b><font color=\"$color[2]\">\n" .
  63. _("ERROR: No available IMAP stream.") .
  64. "</b></font>\n";
  65. error_box($string,$color);
  66. return false;
  67. }
  68. }
  69. /**
  70. * @param stream $imap_stream imap connection resource
  71. * @param string $query imap command
  72. * @param boolean $handle_errors see sqimap_retrieve_imap_response()
  73. * @param array $response empty string, if return = false
  74. * @param array $message empty string, if return = false
  75. * @param boolean $unique_id (since 1.3.0) see sqimap_session_id()
  76. * @param boolean $filter (since 1.4.1 and 1.5.0) see sqimap_fread()
  77. * @param mixed $outputstream (since 1.4.1 and 1.5.0) see sqimap_fread()
  78. * @param boolean $no_return (since 1.4.1 and 1.5.0) see sqimap_fread()
  79. * @return mixed returns false on imap error. displays error message
  80. * if imap stream is not available.
  81. * @since 1.2.3
  82. */
  83. function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response,
  84. &$message, $unique_id = false,$filter=false,
  85. $outputstream=false,$no_return=false) {
  86. if ($imap_stream) {
  87. $sid = sqimap_session_id($unique_id);
  88. fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
  89. $tag_uid_a = explode(' ',trim($sid));
  90. $tag = $tag_uid_a[0];
  91. $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response,
  92. $message, $query,$filter,$outputstream,$no_return);
  93. if (empty($read)) { //IMAP server dropped its connection
  94. $response = '';
  95. $message = '';
  96. return false;
  97. }
  98. /* retrieve the response and the message */
  99. $response = $response[$tag];
  100. $message = $message[$tag];
  101. if (!empty($read[$tag])) {
  102. return $read[$tag][0];
  103. } else {
  104. return $read[$tag];
  105. }
  106. } else {
  107. global $squirrelmail_language, $color;
  108. set_up_language($squirrelmail_language);
  109. require_once(SM_PATH . 'functions/display_messages.php');
  110. $string = "<b><font color=\"$color[2]\">\n" .
  111. _("ERROR: No available IMAP stream.") .
  112. "</b></font>\n";
  113. error_box($string,$color);
  114. return false;
  115. }
  116. }
  117. /**
  118. * @param mixed $new_query
  119. * @param string $tag
  120. * @param array $aQuery
  121. * @param boolean $unique_id see sqimap_session_id()
  122. * @since 1.5.0
  123. */
  124. function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) {
  125. $sid = sqimap_session_id($unique_id);
  126. $tag_uid_a = explode(' ',trim($sid));
  127. $tag = $tag_uid_a[0];
  128. $query = $sid . ' '.$new_query."\r\n";
  129. $aQuery[$tag] = $query;
  130. }
  131. /**
  132. * @param stream $imap_stream imap stream
  133. * @param array $aQueryList
  134. * @param boolean $handle_errors
  135. * @param array $aServerResponse
  136. * @param array $aServerMessage
  137. * @param boolean $unique_id see sqimap_session_id()
  138. * @param boolean $filter see sqimap_fread()
  139. * @param mixed $outputstream see sqimap_fread()
  140. * @param boolean $no_return see sqimap_fread()
  141. * @since 1.5.0
  142. */
  143. function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors,
  144. &$aServerResponse, &$aServerMessage, $unique_id = false,
  145. $filter=false,$outputstream=false,$no_return=false) {
  146. $aResponse = false;
  147. /*
  148. Do not fire all calls at once to the IMAP server but split the calls up
  149. in portions of $iChunkSize. If we do not do that I think we misbehave as
  150. IMAP client or should handle BYE calls if the IMAP server drops the
  151. connection because the number of queries is to large. This isn't tested
  152. but a wild guess how it could work in the field.
  153. After testing it on Exchange 2000 we discovered that a chunksize of 32
  154. was quicker then when we raised it to 128.
  155. */
  156. $iQueryCount = count($aQueryList);
  157. $iChunkSize = 32;
  158. // array_chunk would also do the job but it's supported from php > 4.2
  159. $aQueryChunks = array();
  160. $iLoops = floor($iQueryCount / $iChunkSize);
  161. if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops;
  162. if (!function_exists('array_chunk')) { // arraychunk replacement
  163. reset($aQueryList);
  164. for($i=0;$i<$iLoops;++$i) {
  165. for($j=0;$j<$iChunkSize;++$j) {
  166. $key = key($aQueryList);
  167. $aTmp[$key] = $aQueryList[$key];
  168. if (next($aQueryList) === false) break;
  169. }
  170. $aQueryChunks[] = $aTmp;
  171. }
  172. } else {
  173. $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true);
  174. }
  175. for ($i=0;$i<$iLoops;++$i) {
  176. $aQuery = $aQueryChunks[$i];
  177. foreach($aQuery as $tag => $query) {
  178. fputs($imap_stream,$query);
  179. $aResults[$tag] = false;
  180. }
  181. foreach($aQuery as $tag => $query) {
  182. if ($aResults[$tag] == false) {
  183. $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag,
  184. $handle_errors, $response, $message, $query,
  185. $filter,$outputstream,$no_return);
  186. foreach ($aReturnedResponse as $returned_tag => $aResponse) {
  187. if (!empty($aResponse)) {
  188. $aResults[$returned_tag] = $aResponse[0];
  189. } else {
  190. $aResults[$returned_tag] = $aResponse;
  191. }
  192. $aServerResponse[$returned_tag] = $response[$returned_tag];
  193. $aServerMessage[$returned_tag] = $message[$returned_tag];
  194. }
  195. }
  196. }
  197. }
  198. return $aResults;
  199. }
  200. /**
  201. * Custom fgets function: gets a line from the IMAP server,
  202. * no matter how big it may be.
  203. * @param stream $imap_stream the stream to read from
  204. * @return string a line
  205. * @since 1.2.8
  206. */
  207. function sqimap_fgets($imap_stream) {
  208. $read = '';
  209. $buffer = 4096;
  210. $results = '';
  211. $offset = 0;
  212. while (strpos($results, "\r\n", $offset) === false) {
  213. if (!($read = fgets($imap_stream, $buffer))) {
  214. /* this happens in case of an error */
  215. /* reset $results because it's useless */
  216. $results = false;
  217. break;
  218. }
  219. if ( $results != '' ) {
  220. $offset = strlen($results) - 1;
  221. }
  222. $results .= $read;
  223. }
  224. return $results;
  225. }
  226. /**
  227. * @param stream $imap_stream
  228. * @param integer $iSize
  229. * @param boolean $filter
  230. * @param mixed $outputstream stream or 'php://stdout' string
  231. * @param boolean $no_return controls data returned by function
  232. * @return string
  233. * @since 1.4.1
  234. */
  235. function sqimap_fread($imap_stream,$iSize,$filter=false,
  236. $outputstream=false, $no_return=false) {
  237. if (!$filter || !$outputstream) {
  238. $iBufferSize = $iSize;
  239. } else {
  240. // see php bug 24033. They changed fread behaviour %$^&$%
  241. $iBufferSize = 7800; // multiple of 78 in case of base64 decoding.
  242. }
  243. if ($iSize < $iBufferSize) {
  244. $iBufferSize = $iSize;
  245. }
  246. $iRetrieved = 0;
  247. $results = '';
  248. $sRead = $sReadRem = '';
  249. // NB: fread can also stop at end of a packet on sockets.
  250. while ($iRetrieved < $iSize) {
  251. $sRead = fread($imap_stream,$iBufferSize);
  252. $iLength = strlen($sRead);
  253. $iRetrieved += $iLength ;
  254. $iRemaining = $iSize - $iRetrieved;
  255. if ($iRemaining < $iBufferSize) {
  256. $iBufferSize = $iRemaining;
  257. }
  258. if ($sRead == '') {
  259. $results = false;
  260. break;
  261. }
  262. if ($sReadRem != '') {
  263. $sRead = $sReadRem . $sRead;
  264. $sReadRem = '';
  265. }
  266. if ($filter && $sRead != '') {
  267. // in case the filter is base64 decoding we return a remainder
  268. $sReadRem = $filter($sRead);
  269. }
  270. if ($outputstream && $sRead != '') {
  271. if (is_resource($outputstream)) {
  272. fwrite($outputstream,$sRead);
  273. } else if ($outputstream == 'php://stdout') {
  274. echo $sRead;
  275. }
  276. }
  277. if ($no_return) {
  278. $sRead = '';
  279. } else {
  280. $results .= $sRead;
  281. }
  282. }
  283. return $results;
  284. }
  285. /**
  286. * Obsolete function, inform plugins that use it
  287. * @param stream $imap_stream
  288. * @param string $tag
  289. * @param boolean $handle_errors
  290. * @param array $response
  291. * @param array $message
  292. * @param string $query
  293. * @since 1.1.3
  294. * @deprecated (since 1.5.0) use sqimap_run_command or sqimap_run_command_list instead
  295. */
  296. function sqimap_read_data_list($imap_stream, $tag, $handle_errors,
  297. &$response, &$message, $query = '') {
  298. global $color, $squirrelmail_language;
  299. set_up_language($squirrelmail_language);
  300. require_once(SM_PATH . 'functions/display_messages.php');
  301. $string = "<b><font color=\"$color[2]\">\n" .
  302. _("ERROR: Bad function call.") .
  303. "</b><br />\n" .
  304. _("Reason:") . ' '.
  305. 'There is a plugin installed which make use of the <br />' .
  306. 'SquirrelMail internal function sqimap_read_data_list.<br />'.
  307. 'Please adapt the installed plugin and let it use<br />'.
  308. 'sqimap_run_command or sqimap_run_command_list instead<br /><br />'.
  309. 'The following query was issued:<br />'.
  310. htmlspecialchars($query) . '<br />' . "</font><br />\n";
  311. error_box($string,$color);
  312. echo '</body></html>';
  313. exit;
  314. }
  315. /**
  316. * Function to display an error related to an IMAP query.
  317. * @param string title the caption of the error box
  318. * @param string query the query that went wrong
  319. * @param string message_title optional message title
  320. * @param string message optional error message
  321. * @param string $link an optional link to try again
  322. * @return void
  323. * @since 1.5.0
  324. */
  325. function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '')
  326. {
  327. global $color, $squirrelmail_language;
  328. set_up_language($squirrelmail_language);
  329. require_once(SM_PATH . 'functions/display_messages.php');
  330. $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n";
  331. $cmd = explode(' ',$query);
  332. $cmd= strtolower($cmd[0]);
  333. if ($query != '' && $cmd != 'login')
  334. $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />';
  335. if ($message_title != '')
  336. $string .= $message_title;
  337. if ($message != '')
  338. $string .= htmlspecialchars($message);
  339. $string .= "</font><br />\n";
  340. if ($link != '')
  341. $string .= $link;
  342. error_box($string,$color);
  343. }
  344. /**
  345. * Reads the output from the IMAP stream. If handle_errors is set to true,
  346. * this will also handle all errors that are received. If it is not set,
  347. * the errors will be sent back through $response and $message.
  348. * @param stream $imap_stream imap stream
  349. * @param string $tag
  350. * @param boolean $handle_errors handle errors internally or send them in $response and $message.
  351. * @param array $response
  352. * @param array $message
  353. * @param string $query command that can be printed if something fails
  354. * @param boolean $filter see sqimap_fread()
  355. * @param mixed $outputstream see sqimap_fread()
  356. * @param boolean $no_return see sqimap_fread()
  357. * @since 1.5.0
  358. */
  359. function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
  360. &$response, &$message, $query = '',
  361. $filter = false, $outputstream = false, $no_return = false) {
  362. global $color, $squirrelmail_language;
  363. $read = '';
  364. if (!is_array($message)) $message = array();
  365. if (!is_array($response)) $response = array();
  366. $aResponse = '';
  367. $resultlist = array();
  368. $data = array();
  369. $read = sqimap_fgets($imap_stream);
  370. $i = 0;
  371. while ($read) {
  372. $char = $read{0};
  373. switch ($char)
  374. {
  375. case '+':
  376. default:
  377. $read = sqimap_fgets($imap_stream);
  378. break;
  379. case $tag{0}:
  380. {
  381. /* get the command */
  382. $arg = '';
  383. $i = strlen($tag)+1;
  384. $s = substr($read,$i);
  385. if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) {
  386. $arg = substr($s,0,$j);
  387. }
  388. $found_tag = substr($read,0,$i-1);
  389. if ($found_tag) {
  390. switch ($arg)
  391. {
  392. case 'OK':
  393. case 'BAD':
  394. case 'NO':
  395. case 'BYE':
  396. case 'PREAUTH':
  397. $response[$found_tag] = $arg;
  398. $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
  399. if (!empty($data)) {
  400. $resultlist[] = $data;
  401. }
  402. $aResponse[$found_tag] = $resultlist;
  403. $data = $resultlist = array();
  404. if ($found_tag == $tag) {
  405. break 3; /* switch switch while */
  406. }
  407. break;
  408. default:
  409. /* this shouldn't happen */
  410. $response[$found_tag] = $arg;
  411. $message[$found_tag] = trim(substr($read,$i+strlen($arg)));
  412. if (!empty($data)) {
  413. $resultlist[] = $data;
  414. }
  415. $aResponse[$found_tag] = $resultlist;
  416. $data = $resultlist = array();
  417. if ($found_tag == $tag) {
  418. break 3; /* switch switch while */
  419. }
  420. }
  421. }
  422. $read = sqimap_fgets($imap_stream);
  423. if ($read === false) { /* error */
  424. break 2; /* switch while */
  425. }
  426. break;
  427. } // end case $tag{0}
  428. case '*':
  429. {
  430. if (preg_match('/^\*\s\d+\sFETCH/',$read)) {
  431. /* check for literal */
  432. $s = substr($read,-3);
  433. $fetch_data = array();
  434. do { /* outer loop, continue until next untagged fetch
  435. or tagged reponse */
  436. do { /* innerloop for fetching literals. with this loop
  437. we prohibid that literal responses appear in the
  438. outer loop so we can trust the untagged and
  439. tagged info provided by $read */
  440. if ($s === "}\r\n") {
  441. $j = strrpos($read,'{');
  442. $iLit = substr($read,$j+1,-3);
  443. $fetch_data[] = $read;
  444. $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return);
  445. if ($sLiteral === false) { /* error */
  446. break 4; /* while while switch while */
  447. }
  448. /* backwards compattibility */
  449. $aLiteral = explode("\n", $sLiteral);
  450. /* release not neaded data */
  451. unset($sLiteral);
  452. foreach ($aLiteral as $line) {
  453. $fetch_data[] = $line ."\n";
  454. }
  455. /* release not neaded data */
  456. unset($aLiteral);
  457. /* next fgets belongs to this fetch because
  458. we just got the exact literalsize and there
  459. must follow data to complete the response */
  460. $read = sqimap_fgets($imap_stream);
  461. if ($read === false) { /* error */
  462. break 4; /* while while switch while */
  463. }
  464. $fetch_data[] = $read;
  465. } else {
  466. $fetch_data[] = $read;
  467. }
  468. /* retrieve next line and check in the while
  469. statements if it belongs to this fetch response */
  470. $read = sqimap_fgets($imap_stream);
  471. if ($read === false) { /* error */
  472. break 4; /* while while switch while */
  473. }
  474. /* check for next untagged reponse and break */
  475. if ($read{0} == '*') break 2;
  476. $s = substr($read,-3);
  477. } while ($s === "}\r\n");
  478. $s = substr($read,-3);
  479. } while ($read{0} !== '*' &&
  480. substr($read,0,strlen($tag)) !== $tag);
  481. $resultlist[] = $fetch_data;
  482. /* release not neaded data */
  483. unset ($fetch_data);
  484. } else {
  485. $s = substr($read,-3);
  486. do {
  487. if ($s === "}\r\n") {
  488. $j = strrpos($read,'{');
  489. $iLit = substr($read,$j+1,-3);
  490. $data[] = $read;
  491. $sLiteral = fread($imap_stream,$iLit);
  492. if ($sLiteral === false) { /* error */
  493. $read = false;
  494. break 3; /* while switch while */
  495. }
  496. $data[] = $sLiteral;
  497. $data[] = sqimap_fgets($imap_stream);
  498. } else {
  499. $data[] = $read;
  500. }
  501. $read = sqimap_fgets($imap_stream);
  502. if ($read === false) {
  503. break 3; /* while switch while */
  504. } else if ($read{0} == '*') {
  505. break;
  506. }
  507. $s = substr($read,-3);
  508. } while ($s === "}\r\n");
  509. break 1;
  510. }
  511. break;
  512. } // end case '*'
  513. } // end switch
  514. } // end while
  515. /* error processing in case $read is false */
  516. if ($read === false) {
  517. // try to retrieve an untagged bye respons from the results
  518. $sResponse = array_pop($data);
  519. if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) {
  520. if (!$handle_errors) {
  521. $query = '';
  522. }
  523. sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:"),$sResponse);
  524. echo '</body></html>';
  525. exit;
  526. } else if ($handle_errors) {
  527. unset($data);
  528. sqimap_error_box(_("ERROR: Connection dropped by IMAP server."), $query);
  529. exit;
  530. }
  531. }
  532. /* Set $resultlist array */
  533. if (!empty($data)) {
  534. //$resultlist[] = $data;
  535. }
  536. elseif (empty($resultlist)) {
  537. $resultlist[] = array();
  538. }
  539. /* Return result or handle errors */
  540. if ($handle_errors == false) {
  541. return $aResponse;
  542. }
  543. switch ($response[$tag]) {
  544. case 'OK':
  545. return $aResponse;
  546. break;
  547. case 'NO':
  548. /* ignore this error from M$ exchange, it is not fatal (aka bug) */
  549. if (strstr($message[$tag], 'command resulted in') === false) {
  550. sqimap_error_box(_("ERROR: Could not complete request."), $query, _("Reason Given:") . ' ', $message[$tag]);
  551. echo '</body></html>';
  552. exit;
  553. }
  554. break;
  555. case 'BAD':
  556. sqimap_error_box(_("ERROR: Bad or malformed request."), $query, _("Server responded:") . ' ', $message[$tag]);
  557. echo '</body></html>';
  558. exit;
  559. case 'BYE':
  560. sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:") . ' ', $message[$tag]);
  561. echo '</body></html>';
  562. exit;
  563. default:
  564. sqimap_error_box(_("ERROR: Unknown IMAP response."), $query, _("Server responded:") . ' ', $message[$tag]);
  565. /* the error is displayed but because we don't know the reponse we
  566. return the result anyway */
  567. return $aResponse;
  568. break;
  569. }
  570. }
  571. /**
  572. * @param stream $imap_stream imap string
  573. * @param string $tag_uid
  574. * @param boolean $handle_errors
  575. * @param array $response
  576. * @param array $message
  577. * @param string $query (since 1.2.5)
  578. * @param boolean $filter (since 1.4.1) see sqimap_fread()
  579. * @param mixed $outputstream (since 1.4.1) see sqimap_fread()
  580. * @param boolean $no_return (since 1.4.1) see sqimap_fread()
  581. */
  582. function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors,
  583. &$response, &$message, $query = '',
  584. $filter=false,$outputstream=false,$no_return=false) {
  585. $tag_uid_a = explode(' ',trim($tag_uid));
  586. $tag = $tag_uid_a[0];
  587. $res = sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors,
  588. $response, $message, $query,$filter,$outputstream,$no_return);
  589. return $res;
  590. }
  591. /**
  592. * Connects to the IMAP server and returns a resource identifier for use with
  593. * the other SquirrelMail IMAP functions. Does NOT login!
  594. * @param string server hostname of IMAP server
  595. * @param int port port number to connect to
  596. * @param bool tls whether to use TLS when connecting.
  597. * @return imap-stream resource identifier
  598. * @since 1.5.0 (usable only in 1.5.1 or later)
  599. */
  600. function sqimap_create_stream($server,$port,$tls=false) {
  601. global $squirrelmail_language;
  602. if ($tls == true) {
  603. if ((check_php_version(4,3)) and (extension_loaded('openssl'))) {
  604. /* Use TLS by prefixing "tls://" to the hostname */
  605. $server = 'tls://' . $server;
  606. } else {
  607. require_once(SM_PATH . 'functions/display_messages.php');
  608. logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server).
  609. '<br />'.
  610. _("TLS is enabled, but this version of PHP does not support TLS sockets, or is missing the openssl extension.").
  611. '<br /><br />'.
  612. _("Please contact your system administrator and report this error."),
  613. sprintf(_("Error connecting to IMAP server: %s."), $server));
  614. }
  615. }
  616. $imap_stream = @fsockopen($server, $port, $error_number, $error_string, 15);
  617. /* Do some error correction */
  618. if (!$imap_stream) {
  619. set_up_language($squirrelmail_language, true);
  620. require_once(SM_PATH . 'functions/display_messages.php');
  621. logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server).
  622. "<br />\r\n$error_number : $error_string<br />\r\n",
  623. sprintf(_("Error connecting to IMAP server: %s."), $server) );
  624. exit;
  625. }
  626. $server_info = fgets ($imap_stream, 1024);
  627. return $imap_stream;
  628. }
  629. /**
  630. * Logs the user into the IMAP server. If $hide is set, no error messages
  631. * will be displayed. This function returns the IMAP connection handle.
  632. * @param string $username user name
  633. * @param string $password encrypted password
  634. * @param string $imap_server_address address of imap server
  635. * @param integer $imap_port port of imap server
  636. * @param boolean $hide controls display connection errors
  637. * @return stream
  638. */
  639. function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
  640. global $color, $squirrelmail_language, $onetimepad, $use_imap_tls,
  641. $imap_auth_mech, $sqimap_capabilities;
  642. if (!isset($onetimepad) || empty($onetimepad)) {
  643. sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION );
  644. }
  645. if (!isset($sqimap_capabilities)) {
  646. sqgetglobalvar('sqimap_capabilities' , $capability , SQ_SESSION );
  647. }
  648. $host = $imap_server_address;
  649. $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
  650. $imap_stream = sqimap_create_stream($imap_server_address,$imap_port,$use_imap_tls);
  651. /* Decrypt the password */
  652. $password = OneTimePadDecrypt($password, $onetimepad);
  653. if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) {
  654. // We're using some sort of authentication OTHER than plain or login
  655. $tag=sqimap_session_id(false);
  656. if ($imap_auth_mech == 'digest-md5') {
  657. $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n";
  658. } elseif ($imap_auth_mech == 'cram-md5') {
  659. $query = $tag . " AUTHENTICATE CRAM-MD5\r\n";
  660. }
  661. fputs($imap_stream,$query);
  662. $answer=sqimap_fgets($imap_stream);
  663. // Trim the "+ " off the front
  664. $response=explode(" ",$answer,3);
  665. if ($response[0] == '+') {
  666. // Got a challenge back
  667. $challenge=$response[1];
  668. if ($imap_auth_mech == 'digest-md5') {
  669. $reply = digest_md5_response($username,$password,$challenge,'imap',$host);
  670. } elseif ($imap_auth_mech == 'cram-md5') {
  671. $reply = cram_md5_response($username,$password,$challenge);
  672. }
  673. fputs($imap_stream,$reply);
  674. $read=sqimap_fgets($imap_stream);
  675. if ($imap_auth_mech == 'digest-md5') {
  676. // DIGEST-MD5 has an extra step..
  677. if (substr($read,0,1) == '+') { // OK so far..
  678. fputs($imap_stream,"\r\n");
  679. $read=sqimap_fgets($imap_stream);
  680. }
  681. }
  682. $results=explode(" ",$read,3);
  683. $response=$results[1];
  684. $message=$results[2];
  685. } else {
  686. // Fake the response, so the error trap at the bottom will work
  687. $response="BAD";
  688. $message='IMAP server does not appear to support the authentication method selected.';
  689. $message .= ' Please contact your system administrator.';
  690. }
  691. } elseif ($imap_auth_mech == 'login') {
  692. // Original IMAP login code
  693. $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"';
  694. $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
  695. } elseif ($imap_auth_mech == 'plain') {
  696. /***
  697. * SASL PLAIN
  698. *
  699. * RFC 2595 Chapter 6
  700. *
  701. * The mechanism consists of a single message from the client to the
  702. * server. The client sends the authorization identity (identity to
  703. * login as), followed by a US-ASCII NUL character, followed by the
  704. * authentication identity (identity whose password will be used),
  705. * followed by a US-ASCII NUL character, followed by the clear-text
  706. * password. The client may leave the authorization identity empty to
  707. * indicate that it is the same as the authentication identity.
  708. *
  709. **/
  710. $tag=sqimap_session_id(false);
  711. $sasl = (isset($capability['SASL-IR']) && $capability['SASL-IR']) ? true : false;
  712. $auth = base64_encode("$username\0$username\0$password");
  713. if ($sasl) {
  714. // IMAP Extension for SASL Initial Client Response
  715. // <draft-siemborski-imap-sasl-initial-response-01b.txt>
  716. $query = $tag . " AUTHENTICATE PLAIN $auth\r\n";
  717. fputs($imap_stream, $query);
  718. $read = sqimap_fgets($imap_stream);
  719. } else {
  720. $query = $tag . " AUTHENTICATE PLAIN\r\n";
  721. fputs($imap_stream, $query);
  722. $read=sqimap_fgets($imap_stream);
  723. if (substr($read,0,1) == '+') { // OK so far..
  724. fputs($imap_stream, "$auth\r\n");
  725. $read = sqimap_fgets($imap_stream);
  726. }
  727. }
  728. $results=explode(" ",$read,3);
  729. $response=$results[1];
  730. $message=$results[2];
  731. } else {
  732. $response="BAD";
  733. $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers.";
  734. }
  735. /* If the connection was not successful, lets see why */
  736. if ($response != 'OK') {
  737. if (!$hide) {
  738. if ($response != 'NO') {
  739. /* "BAD" and anything else gets reported here. */
  740. $message = htmlspecialchars($message);
  741. set_up_language($squirrelmail_language, true);
  742. require_once(SM_PATH . 'functions/display_messages.php');
  743. if ($response == 'BAD') {
  744. $string = sprintf (_("Bad request: %s")."<br />\r\n", $message);
  745. } else {
  746. $string = sprintf (_("Unknown error: %s") . "<br />\n", $message);
  747. }
  748. if (isset($read) && is_array($read)) {
  749. $string .= '<br />' . _("Read data:") . "<br />\n";
  750. foreach ($read as $line) {
  751. $string .= htmlspecialchars($line) . "<br />\n";
  752. }
  753. }
  754. error_box($string,$color);
  755. exit;
  756. } else {
  757. /*
  758. * If the user does not log in with the correct
  759. * username and password it is not possible to get the
  760. * correct locale from the user's preferences.
  761. * Therefore, apply the same hack as on the login
  762. * screen.
  763. *
  764. * $squirrelmail_language is set by a cookie when
  765. * the user selects language and logs out
  766. */
  767. set_up_language($squirrelmail_language, true);
  768. include_once(SM_PATH . 'functions/display_messages.php' );
  769. sqsession_destroy();
  770. /* terminate the session nicely */
  771. sqimap_logout($imap_stream);
  772. logout_error( _("Unknown user or password incorrect.") );
  773. exit;
  774. }
  775. } else {
  776. exit;
  777. }
  778. }
  779. /* Special error case:
  780. * Login referrals. The server returns:
  781. * ? OK [REFERRAL <imap url>]
  782. * Check RFC 2221 for details. Since we do not support login referrals yet
  783. * we log the user out.
  784. */
  785. if ( stristr($message, 'REFERRAL imap') === TRUE ) {
  786. sqimap_logout($imap_stream);
  787. set_up_language($squirrelmail_language, true);
  788. include_once(SM_PATH . 'functions/display_messages.php' );
  789. sqsession_destroy();
  790. logout_error( _("Your mailbox is not located at this server. Try a different server or consult your system administrator") );
  791. exit;
  792. }
  793. return $imap_stream;
  794. }
  795. /**
  796. * Simply logs out the IMAP session
  797. * @param stream $imap_stream the IMAP connection to log out.
  798. * @return void
  799. */
  800. function sqimap_logout ($imap_stream) {
  801. /* Logout is not valid until the server returns 'BYE'
  802. * If we don't have an imap_ stream we're already logged out */
  803. if(isset($imap_stream) && $imap_stream)
  804. sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
  805. }
  806. /**
  807. * Retrieve the CAPABILITY string from the IMAP server.
  808. * If capability is set, returns only that specific capability,
  809. * else returns array of all capabilities.
  810. * @param stream $imap_stream
  811. * @param string $capability (optional since 1.3.0)
  812. * @return mixed (string if $capability is set and found,
  813. * false, if $capability is set and not found,
  814. * array if $capability not set)
  815. */
  816. function sqimap_capability($imap_stream, $capability='') {
  817. sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION);
  818. if (!is_array($sqimap_capabilities)) {
  819. $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
  820. $c = explode(' ', $read[0]);
  821. for ($i=2; $i < count($c); $i++) {
  822. $cap_list = explode('=', $c[$i]);
  823. if (isset($cap_list[1])) {
  824. $sqimap_capabilities[trim($cap_list[0])][] = $cap_list[1];
  825. } else {
  826. $sqimap_capabilities[trim($cap_list[0])] = TRUE;
  827. }
  828. }
  829. }
  830. if ($capability) {
  831. if (isset($sqimap_capabilities[$capability])) {
  832. return $sqimap_capabilities[$capability];
  833. } else {
  834. return false;
  835. }
  836. }
  837. return $sqimap_capabilities;
  838. }
  839. /**
  840. * Returns the delimiter between mailboxes: INBOX/Test, or INBOX.Test
  841. * @param stream $imap_stream
  842. * @return string
  843. */
  844. function sqimap_get_delimiter ($imap_stream = false) {
  845. global $sqimap_delimiter, $optional_delimiter;
  846. /* Use configured delimiter if set */
  847. if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
  848. return $optional_delimiter;
  849. }
  850. /* Delimiter is stored in the session from redirect. Try fetching from there first */
  851. if (empty($sqimap_delimiter)) {
  852. sqgetGlobalVar('delimiter',$sqimap_delimiter,SQ_SESSION);
  853. }
  854. /* Do some caching here */
  855. if (!$sqimap_delimiter) {
  856. if (sqimap_capability($imap_stream, 'NAMESPACE')) {
  857. /*
  858. * According to something that I can't find, this is supposed to work on all systems
  859. * OS: This won't work in Courier IMAP.
  860. * OS: According to rfc2342 response from NAMESPACE command is:
  861. * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
  862. * OS: We want to lookup all personal NAMESPACES...
  863. */
  864. $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
  865. if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
  866. if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
  867. $pn = $data2[1];
  868. }
  869. $pna = explode(')(', $pn);
  870. while (list($k, $v) = each($pna)) {
  871. $lst = explode('"', $v);
  872. if (isset($lst[3])) {
  873. $pn[$lst[1]] = $lst[3];
  874. } else {
  875. $pn[$lst[1]] = '';
  876. }
  877. }
  878. }
  879. $sqimap_delimiter = $pn[0];
  880. } else {
  881. fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
  882. $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
  883. $read = $read['.'][0]; //sqimap_read_data() now returns a tag array of response array
  884. $quote_position = strpos ($read[0], '"');
  885. $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
  886. }
  887. }
  888. return $sqimap_delimiter;
  889. }
  890. /**
  891. * This encodes a mailbox name for use in IMAP commands.
  892. * @param string $what the mailbox to encode
  893. * @return string the encoded mailbox string
  894. * @since 1.5.0
  895. */
  896. function sqimap_encode_mailbox_name($what)
  897. {
  898. if (ereg("[\"\\\r\n]", $what))
  899. return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */
  900. return '"' . $what . '"'; /* 4.3 quoted string form */
  901. }
  902. /**
  903. * Gets the number of messages in the current mailbox.
  904. *
  905. * OBSOLETE use sqimap_status_messages instead.
  906. * @param stream $imap_stream imap stream
  907. * @param string $mailbox
  908. * @deprecated
  909. */
  910. function sqimap_get_num_messages ($imap_stream, $mailbox) {
  911. $read_ary = sqimap_run_command ($imap_stream, 'EXAMINE ' . sqimap_encode_mailbox_name($mailbox), false, $result, $message);
  912. for ($i = 0; $i < count($read_ary); $i++) {
  913. if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
  914. return $regs[1];
  915. }
  916. }
  917. return false; //"BUG! Couldn't get number of messages in $mailbox!";
  918. }
  919. /**
  920. * OBSOLETE FUNCTION should be removed after mailbox_display,
  921. * printMessage function is adapted
  922. * $addr_ar = array(), $group = '' and $host='' arguments are used in 1.4.0
  923. * @param string $address
  924. * @param integer $max
  925. * @since 1.4.0
  926. * @deprecated See Rfc822Address.php
  927. */
  928. function parseAddress($address, $max=0) {
  929. $aAddress = parseRFC822Address($address,array('limit'=> $max));
  930. /*
  931. * Because the expected format of the array element is changed we adapt it now.
  932. * This also implies that this function is obsolete and should be removed after the
  933. * rest of the source is adapted. See Rfc822Address.php for the new function.
  934. */
  935. array_walk($aAddress, '_adaptAddress');
  936. return $aAddress;
  937. }
  938. /**
  939. * OBSOLETE FUNCTION should be removed after mailbox_display,
  940. * printMessage function is adapted
  941. *
  942. * callback function used for formating of addresses array in
  943. * parseAddress() function
  944. * @param array $aAddr
  945. * @param integer $k array key
  946. * @since 1.5.1
  947. * @deprecated
  948. */
  949. function _adaptAddress(&$aAddr,$k) {
  950. $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL]) && $aAddr[SQM_ADDR_PERSONAL]) ?
  951. $aAddr[SQM_ADDR_PERSONAL] : '';
  952. $sEmail = ($aAddr[SQM_ADDR_HOST]) ?
  953. $aAddr[SQM_ADDR_MAILBOX] . '@'.$aAddr[SQM_ADDR_HOST] :
  954. $aAddr[SQM_ADDR_MAILBOX];
  955. $aAddr = array($sEmail,$sPersonal);
  956. }
  957. /**
  958. * Returns the number of unseen messages in this folder.
  959. * obsoleted by sqimap_status_messages !
  960. * Arguments differ in 1.0.x
  961. * @param stream $imap_stream
  962. * @param string $mailbox
  963. * @return integer
  964. * @deprecated
  965. */
  966. function sqimap_unseen_messages ($imap_stream, $mailbox) {
  967. $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('UNSEEN'));
  968. return $aStatus['UNSEEN'];
  969. }
  970. /**
  971. * Returns the status items of a mailbox.
  972. * Default it returns MESSAGES,UNSEEN and RECENT
  973. * Supported status items are MESSAGES, UNSEEN, RECENT (since 1.4.0),
  974. * UIDNEXT (since 1.5.1) and UIDVALIDITY (since 1.5.1)
  975. * @param stream $imap_stream imap stream
  976. * @param string $mailbox mail folder
  977. * @param array $aStatusItems status items
  978. * @return array
  979. * @since 1.3.2
  980. */
  981. function sqimap_status_messages ($imap_stream, $mailbox,
  982. $aStatusItems = array('MESSAGES','UNSEEN','RECENT')) {
  983. $aStatusItems = implode(' ',$aStatusItems);
  984. $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) .
  985. " ($aStatusItems)", false, $result, $message);
  986. $i = 0;
  987. $messages = $unseen = $recent = $uidnext = $uidvalidity = false;
  988. $regs = array(false,false);
  989. while (isset($read_ary[$i])) {
  990. if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  991. $unseen = $regs[1];
  992. }
  993. if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  994. $messages = $regs[1];
  995. }
  996. if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  997. $recent = $regs[1];
  998. }
  999. if (preg_match('/UIDNEXT\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  1000. $uidnext = $regs[1];
  1001. }
  1002. if (preg_match('/UIDVALIDITY\s+([0-9]+)/i', $read_ary[$i], $regs)) {
  1003. $uidvalidity = $regs[1];
  1004. }
  1005. $i++;
  1006. }
  1007. return array('MESSAGES' => $messages,
  1008. 'UNSEEN'=>$unseen,
  1009. 'RECENT' => $recent,
  1010. 'UIDNEXT' => $uidnext,
  1011. 'UIDVALIDITY' => $uidvalidity);
  1012. }
  1013. /**
  1014. * Saves a message to a given folder -- used for saving sent messages
  1015. * @param stream $imap_stream
  1016. * @param string $sent_folder
  1017. * @param $length
  1018. */
  1019. function sqimap_append ($imap_stream, $sent_folder, $length) {
  1020. fputs ($imap_stream, sqimap_session_id() . ' APPEND ' . sqimap_encode_mailbox_name($sent_folder) . " (\\Seen) {".$length."}\r\n");
  1021. $tmp = fgets ($imap_stream, 1024);
  1022. sqimap_append_checkresponse($tmp, $sent_folder);
  1023. }
  1024. /**
  1025. * @param stream imap_stream
  1026. * @param string $folder (since 1.3.2)
  1027. */
  1028. function sqimap_append_done ($imap_stream, $folder='') {
  1029. fputs ($imap_stream, "\r\n");
  1030. $tmp = fgets ($imap_stream, 1024);
  1031. sqimap_append_checkresponse($tmp, $folder);
  1032. }
  1033. /**
  1034. * Displays error messages, if there are errors in responses to
  1035. * commands issues by sqimap_append() and sqimap_append_done() functions.
  1036. * @param string $response
  1037. * @param string $folder
  1038. * @since 1.5.1
  1039. */
  1040. function sqimap_append_checkresponse($response, $folder) {
  1041. if (preg_match("/(.*)(BAD|NO)(.*)$/", $response, $regs)) {
  1042. global $squirrelmail_language, $color;
  1043. set_up_language($squirrelmail_language);
  1044. require_once(SM_PATH . 'functions/display_messages.php');
  1045. $reason = $regs[3];
  1046. if ($regs[2] == 'NO') {
  1047. $string = "<b><font color=\"$color[2]\">\n" .
  1048. _("ERROR: Could not append message to") ." $folder." .
  1049. "</b><br />\n" .
  1050. _("Server responded:") . ' ' .
  1051. $reason . "<br />\n";
  1052. if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) {
  1053. $string .= _("Solution:") . ' ' .
  1054. _("Remove unneccessary messages from your folder and start with your Trash folder.")
  1055. ."<br />\n";
  1056. }
  1057. $string .= "</font>\n";
  1058. error_box($string,$color);
  1059. } else {
  1060. $string = "<b><font color=\"$color[2]\">\n" .
  1061. _("ERROR: Bad or malformed request.") .
  1062. "</b><br />\n" .
  1063. _("Server responded:") . ' ' .
  1064. $reason . "</font><br />\n";
  1065. error_box($string,$color);
  1066. exit;
  1067. }
  1068. }
  1069. }
  1070. /**
  1071. * Allows mapping of IMAP server address with custom function
  1072. * see map_yp_alias()
  1073. * @param string $imap_server imap server address or mapping
  1074. * @param string $username
  1075. * @return string
  1076. * @since 1.3.0
  1077. */
  1078. function sqimap_get_user_server ($imap_server, $username) {
  1079. if (substr($imap_server, 0, 4) != "map:") {
  1080. return $imap_server;
  1081. }
  1082. $function = substr($imap_server, 4);
  1083. return $function($username);
  1084. }
  1085. /**
  1086. * This is an example that gets IMAP servers from yellowpages (NIS).
  1087. * you can simple put map:map_yp_alias in your $imap_server_address
  1088. * in config.php use your own function instead map_yp_alias to map your
  1089. * LDAP whatever way to find the users IMAP server.
  1090. *
  1091. * Requires access to external ypmatch program
  1092. * FIXME: it can be implemented in php yp extension or pecl (since php 5.1.0)
  1093. * @param string $username
  1094. * @return string
  1095. * @since 1.3.0
  1096. */
  1097. function map_yp_alias($username) {
  1098. $yp = `ypmatch $username aliases`;
  1099. return chop(substr($yp, strlen($username)+1));
  1100. }
  1101. ?>