ImapLib.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?php
  2. /**
  3. * LICENSE: The MIT License
  4. * Copyright (c) 2010 Chris Nizzardini (http://www.cnizz.com)
  5. */
  6. /**
  7. * Basic idea from
  8. * http://www.phpclasses.org/package/6256-PHP-Retrieve-messages-from-an-IMAP-server.html
  9. * added enhancements
  10. *
  11. * @modified 2011-11-13 Mark Scherer
  12. * @php 5
  13. * @cakephp 2.x
  14. *
  15. * ImapLib for accessing IMAP and POP email accounts
  16. */
  17. class ImapLib {
  18. const S_MAILBOX = 'mailbox';
  19. const S_SERVER = 'server';
  20. const S_PORT = 'port';
  21. const S_SERVICE = 'service';
  22. const S_USER = 'user';
  23. const S_PASSWORD = 'password';
  24. const S_AUTHUSER = 'authuser';
  25. const S_DEBUG = 'debug';
  26. const S_SECURE = 'secure';
  27. const S_NORSH = 'norsa';
  28. const S_SSL = 'ssl';
  29. const S_VALIDATECERT = 'validatecert';
  30. const S_TLS = 'tls';
  31. const S_NOTLS = 'notls';
  32. const S_READONLY = 'readonly';
  33. public $stream;
  34. public $settings = array(
  35. self::S_MAILBOX => 'INBOX',
  36. self::S_SERVER => '',
  37. self::S_PORT => '',
  38. self::S_SERVICE => 'imap',
  39. self::S_USER => false,
  40. self::S_PASSWORD => '',
  41. self::S_AUTHUSER => false,
  42. self::S_DEBUG => false,
  43. self::S_SECURE => false,
  44. self::S_NORSH => false,
  45. self::S_SSL => false,
  46. self::S_VALIDATECERT => false,
  47. self::S_TLS => false,
  48. self::S_NOTLS => false,
  49. self::S_READONLY => false
  50. );
  51. public $currentSettings = array();
  52. public $currentRef = '';
  53. /**
  54. * Check for basic dependencies in constructor.
  55. */
  56. public function __construct() {
  57. $this->dependenciesMatch();
  58. }
  59. /**
  60. * ImapLib::buildConnector()
  61. *
  62. * @param array $data
  63. * @return string
  64. */
  65. public function buildConnector($data = array()) {
  66. $data = array_merge($this->settings, $data);
  67. $string = '{';
  68. $string .= $data[self::S_SERVER];
  69. if ($data[self::S_PORT]) {
  70. $string .= ':' . $data[self::S_PORT];
  71. }
  72. if ($data[self::S_SERVICE]) {
  73. $string .= '/service=' . $data[self::S_SERVICE];
  74. }
  75. if ($data[self::S_USER]) {
  76. $string .= '/user=' . $data[self::S_USER];
  77. } else {
  78. $string .= '/anonymous';
  79. }
  80. if ($data[self::S_AUTHUSER]) {
  81. $string .= '/authuser=' . $data[self::S_AUTHUSER];
  82. }
  83. if ($data[self::S_DEBUG]) {
  84. $string .= '/debug';
  85. }
  86. if ($data[self::S_SECURE]) {
  87. $string .= '/secure';
  88. }
  89. if ($data[self::S_NORSH]) {
  90. $string .= '/norsh';
  91. }
  92. if ($data[self::S_SSL]) {
  93. $string .= '/ssl';
  94. }
  95. if ($data[self::S_VALIDATECERT]) {
  96. $string .= '/validate-cert';
  97. } else {
  98. $string .= '/novalidate-cert';
  99. }
  100. if ($data[self::S_TLS]) {
  101. $string .= '/tls';
  102. }
  103. if ($data[self::S_NOTLS]) {
  104. $string .= '/notls';
  105. }
  106. if ($data[self::S_READONLY]) {
  107. $string .= '/readonly';
  108. }
  109. $string .= '}';
  110. $string .= $data[self::S_MAILBOX];
  111. $this->currentRef = $string;
  112. $this->currentSettings = $data;
  113. return $string;
  114. }
  115. /**
  116. * ImapLib::set()
  117. *
  118. * @param string $key
  119. * @param mixed $value
  120. * @return void
  121. */
  122. public function set($key, $value) {
  123. $this->settings[$key] = $value;
  124. }
  125. /**
  126. * ImapLib::lastError()
  127. *
  128. * @return string
  129. */
  130. public function lastError() {
  131. return imap_last_error();
  132. }
  133. /**
  134. * @return boolean Success
  135. */
  136. public function connect($user, $pass, $server, $port = null) {
  137. $this->settings[self::S_SERVER] = $server;
  138. if ($port || !$port && $this->settings[self::S_SERVICE] === 'imap') {
  139. $this->settings[self::S_PORT] = $port;
  140. }
  141. $this->settings[self::S_USER] = $user;
  142. $this->settings[self::S_PASSWORD] = $pass;
  143. $connector = $this->buildConnector();
  144. //$options = OP_DEBUG;
  145. $this->stream = @imap_open($connector, $user, $pass, $options);
  146. if ($this->stream === false) {
  147. if ($error = $this->checkConnection()) {
  148. throw new ImapException($error);
  149. }
  150. return false;
  151. }
  152. return true;
  153. }
  154. /**
  155. * ImapLib::checkConnection()
  156. *
  157. * @return mixed
  158. */
  159. public function checkConnection() {
  160. if ($this->stream) {
  161. return $this->lastError();
  162. }
  163. return false;
  164. }
  165. /**
  166. * ImapLib::msgCount()
  167. *
  168. * @return integer Count
  169. */
  170. public function msgCount() {
  171. return imap_num_msg($this->stream);
  172. }
  173. public function listMailboxes($current = true) {
  174. if (is_bool($current)) {
  175. if ($current) {
  176. $current = '%';
  177. } else {
  178. $current = '*';
  179. }
  180. }
  181. return imap_list($this->stream, $this->currentRef, $current);
  182. }
  183. public function getFolder() {
  184. return new ImapFolderLib($this);
  185. }
  186. public function expunge() {
  187. return imap_expunge($this->stream);
  188. }
  189. public function close($expunge = false) {
  190. if ($expunge) {
  191. return @imap_close($this->stream, CL_EXPUNGE);
  192. }
  193. return @imap_close($this->stream);
  194. }
  195. public function __destruct() {
  196. $this->close();
  197. }
  198. /**
  199. * Main listing of messages
  200. * - body, structure, attachments
  201. *
  202. * @return array
  203. */
  204. public function msgList($msgList = array()) {
  205. $return = array();
  206. if (empty($msgList)) {
  207. $count = $this->msgCount();
  208. for ($i = 1; $i <= $count; $i++) {
  209. $header = imap_headerinfo($this->stream, $i);
  210. $msgNo = trim($header->Msgno);
  211. foreach ($header as $id => $value) {
  212. # fix to remove whitespaces
  213. // Simple array
  214. if (!is_array($value)) {
  215. $return[$msgNo][$id] = imap_utf8($value);
  216. } else {
  217. foreach ($value as $newid => $arrayValue) {
  218. foreach ($value[0] as $key => $aValue) {
  219. $return[$msgNo][$id][$key] = quoted_printable_decode($aValue);
  220. }
  221. }
  222. }
  223. }
  224. //lets add attachments
  225. $return[$msgNo]['structure'] = (array)imap_fetchstructure($this->stream, $msgNo);
  226. $encodingValue = $return[$msgNo]['structure']['encoding'];
  227. if (!empty($return[$msgNo]['structure']['parts'])) {
  228. $part = $return[$msgNo]['structure']['parts'][0];
  229. $encodingValue = $part->encoding;
  230. }
  231. // Let's add the body
  232. $return[$msgNo]['body'] = $this->_getDecodedValue(imap_fetchbody($this->stream, $msgNo, 1), $encodingValue);
  233. //debug(imap_fetchstructure($this->stream, $header->Msgno, FT_UID));
  234. $return[$msgNo]['attachments'] = $this->attachments($header);
  235. }
  236. }
  237. // We want to search a specific array of messages
  238. else {
  239. foreach ($msgList as $i) {
  240. $header = imap_headerinfo($this->stream, $i);
  241. foreach ($header as $id => $value) {
  242. // Simple array
  243. if (!is_array($value)) {
  244. $return[$header->Msgno][$id] = $value;
  245. } else {
  246. foreach ($value as $newid => $arrayValue) {
  247. foreach ($value[0] as $key => $aValue) {
  248. $return[$header->Msgno][$id][$key] = quoted_printable_decode($aValue);
  249. }
  250. }
  251. }
  252. // Let's add the body too!
  253. $return[$header->Msgno]['body'] = imap_fetchbody($this->stream, $header->Msgno, 0);
  254. $return[$header->Msgno]['structure'] = imap_fetchstructure($this->stream, $header->Msgno);
  255. $return[$header->Msgno]['attachments'] = $this->attachments($header);
  256. }
  257. }
  258. }
  259. return $return;
  260. }
  261. /**
  262. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  263. * @param object $header
  264. * @return array
  265. */
  266. public function attachments($header) {
  267. $structure = imap_fetchstructure($this->stream, $header->Msgno);
  268. if (!$structure || !isset($structure->parts)) {
  269. return array();
  270. }
  271. $parts = $structure->parts;
  272. $fpos = 2;
  273. $message = array();
  274. $message["attachment"]["type"][0] = 'text';
  275. $message["attachment"]["type"][1] = 'multipart';
  276. $message["attachment"]["type"][2] = 'message';
  277. $message["attachment"]["type"][3] = 'application';
  278. $message["attachment"]["type"][4] = 'audio';
  279. $message["attachment"]["type"][5] = 'image';
  280. $message["attachment"]["type"][6] = 'video';
  281. $message["attachment"]["type"][7] = 'other';
  282. $attachments = array();
  283. $count = count($parts);
  284. for ($i = 1; $i < $count; $i++) {
  285. $attachment = array();
  286. $part = $parts[$i];
  287. if (isset($part->disposition) && $part->disposition === 'ATTACHMENT') {
  288. $attachment["pid"] = $i;
  289. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  290. $attachment["subtype"][$i] = strtolower($part->subtype);
  291. $ext = $part->subtype;
  292. $params = $part->dparameters;
  293. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  294. $attachment['filename'] = $part->dparameters[0]->value;
  295. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  296. $attachment['filesize'] = strlen($attachment['data']);
  297. $fpos++;
  298. $attachments[] = $attachment;
  299. } elseif (isset($part->subtype) && $part->subtype === "OCTET-STREAM") {
  300. $attachment["pid"] = $i;
  301. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  302. $attachment["subtype"][$i] = strtolower($part->subtype);
  303. $ext = $part->subtype;
  304. $params = $part->parameters;
  305. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  306. $attachment['filename'] = $part->parameters[0]->value;
  307. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  308. $attachment['filesize'] = strlen($attachment['data']);
  309. $fpos++;
  310. $attachments[] = $attachment;
  311. } else { // inline attachments etc
  312. $attachment["pid"] = $i;
  313. $type = '';
  314. if (!empty($message["attachment"]["type"][$part->type])) {
  315. $type = $message["attachment"]["type"][$part->type] . "/";
  316. }
  317. $attachment["type"][$i] = $type . strtolower($part->subtype);
  318. $attachment["subtype"][$i] = strtolower($part->subtype);
  319. $ext = $part->subtype;
  320. $params = $part->parameters;
  321. $data = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  322. $attachment['filename'] = !is_object($part->parameters) ? $part->parameters[0]->value : '';
  323. $attachment['data'] = $this->_getDecodedValue($data, $part->encoding);
  324. $attachment['filesize'] = strlen($attachment['data']);
  325. $fpos++;
  326. $attachments[] = $attachment;
  327. }
  328. }
  329. return $attachments;
  330. }
  331. /**
  332. * Decode message text.
  333. *
  334. * @param string $message
  335. * @param int $encoding
  336. * @return string Message
  337. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  338. */
  339. protected function _getDecodedValue($message, $encoding) {
  340. if ($encoding == 0) {
  341. $message = imap_8bit($message);
  342. $message = $this->_decode7Bit($message);
  343. } elseif ($encoding == 1) {
  344. $message = imap_8bit($message); // Additionally needs quoted_printable_decode()?
  345. } elseif ($encoding == 2) {
  346. $message = imap_binary($message);
  347. } elseif ($encoding == 3) {
  348. $message = imap_base64($message);
  349. } elseif ($encoding == 4) {
  350. $message = imap_qprint($message);
  351. } elseif ($encoding == 5) {
  352. // plain
  353. }
  354. return $message;
  355. }
  356. /**
  357. * Decodes 7-Bit text.
  358. *
  359. * @param string $text 7-Bit text to convert.
  360. * @return string Decoded text.
  361. */
  362. protected function _decode7Bit($text) {
  363. // Manually convert common encoded characters into their UTF-8 equivalents.
  364. $characters = array(
  365. '=20' => ' ', // space.
  366. '=E2=80=99' => "'", // single quote.
  367. '=0A' => "\r\n", // line break.
  368. '=A0' => ' ', // non-breaking space.
  369. '=C2=A0' => ' ', // non-breaking space.
  370. "=\r\n" => '', // joined line.
  371. '=E2=80=A6' => '…', // ellipsis.
  372. '=E2=80=A2' => '•', // bullet.
  373. );
  374. foreach ($characters as $key => $value) {
  375. $text = str_replace($key, $value, $text);
  376. }
  377. return $text;
  378. }
  379. /**
  380. * ImapLib::search()
  381. *
  382. * @param mixed $params
  383. * @return string
  384. */
  385. public function search($params) {
  386. if ($this->stream) {
  387. if (is_array($params)) {
  388. $searchString = '';
  389. foreach ($params as $field => $value) {
  390. if (is_numeric($field)) {
  391. // Make sure the value is uppercase
  392. $searchString .= strtoupper($value) . ' ';
  393. } else {
  394. $searchString .= strtoupper($field) . ' "' . $value . '" ';
  395. }
  396. }
  397. // Perform the search
  398. #echo "'$searchString'";
  399. return imap_search($this->stream, $searchString);
  400. }
  401. return imap_last_error();
  402. }
  403. return imap_last_error();
  404. }
  405. /**
  406. * ImapLib::flag()
  407. *
  408. * @param mixed $flag
  409. * @return boolean
  410. */
  411. public function flag($flag) {
  412. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  413. }
  414. /**
  415. * ImapLib::delete()
  416. *
  417. * @param string|array $emails
  418. * @param bool $delete
  419. * @return boolean Success
  420. */
  421. public function delete($emails, $delete = false) {
  422. $emails = (array)$emails;
  423. foreach ($emails as $email) {
  424. if ($delete) {
  425. imap_delete($this->stream, (int)$email);
  426. } else {
  427. imap_mail_move($this->stream, (int)$email, "Inbox/Trash");
  428. }
  429. }
  430. return imap_expunge($this->stream);
  431. }
  432. /**
  433. * @deprecated
  434. */
  435. public function delx($emails, $delete = false) {
  436. if (!$this->stream) {
  437. return false;
  438. }
  439. $emails = (array)$emails;
  440. foreach ($emails as $key => $val) {
  441. $emails[$key] = (int)$val;
  442. }
  443. // Let's delete multiple emails
  444. if (count($emails) > 0) {
  445. $deleteString = '';
  446. $emailError = array();
  447. foreach ($emails as $email) {
  448. if ($delete) {
  449. if (!imap_delete($this->stream, $email)) {
  450. $emailError[] = $email;
  451. }
  452. }
  453. }
  454. if (!$delete) {
  455. // Need to take the last comma out!
  456. $deleteString = implode(',', $emails);
  457. echo $deleteString;
  458. imap_mail_move($this->stream, $deleteString, "Inbox/Trash");
  459. //imap_expunge($this->stream);
  460. } else {
  461. // NONE of the emails were deleted
  462. //imap_expunge($this->stream);
  463. if (count($emailError) === count($emails)) {
  464. return imap_last_error();
  465. }
  466. $return['status'] = false;
  467. $return['not_deleted'] = $emailError;
  468. return $return;
  469. }
  470. }
  471. // Not connected
  472. return imap_last_error();
  473. }
  474. /**
  475. * @deprecated
  476. */
  477. public function switch_mailbox($mailbox = '') {
  478. if ($this->stream) {
  479. $this->mbox = '{' . $this->server;
  480. if ($this->port) {
  481. $this->mbox .= ':' . $this->port;
  482. }
  483. if ($this->flags) {
  484. $this->mbox .= $this->flags;
  485. }
  486. $this->mbox .= '/user="' . $this->user . '"';
  487. $this->mbox .= '}';
  488. $this->mbox .= $this->defaultMailbox;
  489. if ($mailbox) {
  490. $this->mbox .= '.' . $mailbox;
  491. }
  492. return @imap_reopen($this->stream, $this->mbox);
  493. }
  494. // Not connected
  495. return imap_last_error();
  496. }
  497. /**
  498. * @deprecated
  499. */
  500. public function current_mailbox() {
  501. if ($this->stream) {
  502. $info = imap_mailboxmsginfo($this->stream);
  503. if ($info) {
  504. return $info->Mailbox;
  505. }
  506. // There was an error
  507. return imap_last_error();
  508. }
  509. // Not connected
  510. return imap_last_error();
  511. }
  512. /**
  513. * @deprecated
  514. */
  515. public function mailbox_info($type = 'obj') {
  516. if ($this->stream) {
  517. $info = imap_mailboxmsginfo($this->stream);
  518. if ($info) {
  519. if ($type === 'array') {
  520. $infoArray = get_object_vars($info);
  521. return $infoArray;
  522. }
  523. return $info;
  524. }
  525. // There was an error
  526. return imap_last_error();
  527. }
  528. // Not connected
  529. return imap_last_error();
  530. }
  531. /**
  532. * Makes sure imap_open is available etc
  533. *
  534. * @throws InternalErrorException
  535. * @return boolean Success
  536. */
  537. public function dependenciesMatch() {
  538. if (!function_exists('imap_open')) {
  539. throw new InternalErrorException('imap_open not available. Please install extension/module.');
  540. }
  541. return true;
  542. }
  543. }
  544. // Currently NOT IN USE: //
  545. /**
  546. * IMAP Postf�cher mit CakePHP abfragen
  547. * @see http://www.interaktionsdesigner.de/2009/05/11/imap-postfacher-mit-cakephp-abfragen/
  548. *
  549. * $this->Imap->connect();
  550. * Der R�ckgabewert dieser Funktion ist negativ wenn es nicht funktioniert hat.
  551. *
  552. * Eine gute Hilfe gegen verr�ckte Sonderzeichen und Kodierungen ist die Kombination von utf8_encode und quoted_printable_decode. Damit werden die meisten Umlaute richtig dargestellt.
  553. * F�r den Text der Mail w�re das dann innerhalb der foreach-Schleife:
  554. * debug(utf8_encode(quoted_printable_decode($message['body'])));
  555. *
  556. * fixes: pop3 connect etc
  557. */
  558. class ImapMessageInfoLib {
  559. const BS = "\\";
  560. public $ImapFolder;
  561. public $ImapMessage;
  562. public $subject;
  563. public $from;
  564. public $to;
  565. public $date;
  566. public $messageId;
  567. public $references;
  568. public $inReplyTo;
  569. public $size;
  570. public $uid;
  571. public $msgno;
  572. public $recent;
  573. public $flagged;
  574. public $answered;
  575. public $deleted;
  576. public $seen;
  577. public $draft;
  578. public function __construct($ImapFolder, $data) {
  579. if (!is_object($data)) {
  580. $list = new ImapMessagesListLib($ImapFolder, array($data));
  581. $list = $list->overview(false);
  582. $data = $list[0];
  583. }
  584. foreach ($data as $key => $value) {
  585. $this->{$key} = $value;
  586. }
  587. $this->ImapFolder = $ImapFolder;
  588. }
  589. public function messageObject() {
  590. if (!isset($this->ImapMessage)) {
  591. return $this->ImapMessage = new ImapMessageLib($this->ImapFolder, $this->uid, $this);
  592. }
  593. return $this->ImapMessage;
  594. }
  595. public function flag($flag) {
  596. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  597. }
  598. public function unFlag($flag) {
  599. return imap_clearflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  600. }
  601. public function seen($set = null) {
  602. if ($set === null) {
  603. return $this->seen;
  604. }
  605. if ($set) {
  606. return $this->flag(self::BS . 'Seen');
  607. }
  608. return $this->unFlag(self::BS . 'Seen');
  609. }
  610. public function answered($set = null) {
  611. if ($set === null) {
  612. return $this->answered;
  613. }
  614. if ($set) {
  615. return $this->flag(self::BS . 'Answered');
  616. }
  617. return $this->unFlag(self::BS . 'Answered');
  618. }
  619. public function flagged($set = null) {
  620. if ($set === null) {
  621. return $this->flagged;
  622. }
  623. if ($set) {
  624. return $this->flag(self::BS . 'Flagged');
  625. }
  626. return $this->unFlag(self::BS . 'Flagged');
  627. }
  628. public function deleted($set = null) {
  629. if ($set === null) {
  630. return $this->deleted;
  631. }
  632. if ($set) {
  633. return $this->flag(self::BS . 'Deleted');
  634. }
  635. return $this->unFlag(self::BS . 'Deleted');
  636. }
  637. public function draft($set = null) {
  638. if ($set === null) {
  639. return $this->draft;
  640. }
  641. if ($set) {
  642. return $this->flag(self::BS . 'Draft');
  643. }
  644. return $this->unFlag(self::BS . 'Draft');
  645. }
  646. }
  647. class ImapMessageLib {
  648. public $ImapFolder;
  649. public $MessageInfo;
  650. public $uid;
  651. public function __construct($ImapFolder, $uid, $ImapMessageInfo = null) {
  652. $this->ImapFolder = $ImapFolder;
  653. if ($ImapMessageInfo === null) {
  654. $this->MessageInfo = new ImapMessageInfoLib($this->ImapFolder, $uid);
  655. } else {
  656. $this->MessageInfo = $ImapMessageInfo;
  657. }
  658. $this->MessageInfo->ImapMessage = $this;
  659. $this->uid = $uid;
  660. }
  661. public function move($folder) {
  662. }
  663. public function id() {
  664. //CHANGE DIR TO CURRENT
  665. return imap_msgno($this->ImapFolder->Imap->stream, $this->uid);
  666. }
  667. public function uid($ID) {
  668. return $this->uid;
  669. }
  670. public function fetchstructure() {
  671. return imap_fetchstructure($this->ImapFolder->Imap->stream, $this->uid, FT_UID);
  672. }
  673. public function fetchbody($section = 0) {
  674. return imap_fetchbody($this->ImapFolder->Imap->stream, $this->uid, $section, (FT_UID + FT_PEEK));
  675. }
  676. }
  677. class ImapMessagesListLib {
  678. public $ImapFolder;
  679. public $messageUIDs = array();
  680. public function __construct($ImapFolder, $messageUIDs) {
  681. $this->ImapFolder = $ImapFolder;
  682. $this->messageUIDs = $messageUIDs;
  683. }
  684. public function messgageObject($id) {
  685. if (isset($this->messageUIDs[$id])) {
  686. if (is_object($this->messageUIDs[$id])) {
  687. return $this->messageUIDs[$id];
  688. }
  689. return $this->messageUIDs[$id] = new ImapMessageLib($this->ImapFolder, $this->messageUIDs[$id]);
  690. }
  691. return false;
  692. }
  693. public function count() {
  694. return count($this->messageUIDs);
  695. }
  696. public function overview($returnInfo = true) {
  697. //CHANGE DIR TO CURRENT
  698. $overview = imap_fetch_overview($this->ImapFolder->Imap->stream, implode(',', $this->messageUIDs), FT_UID);
  699. if ($returnInfo) {
  700. $msgObjs = array();
  701. foreach ($overview as $info) {
  702. $msgObjs[] = new ImapMessageInfoLib($this->ImapFolder, $info);
  703. }
  704. return $msgObjs;
  705. }
  706. return $overview;
  707. }
  708. }
  709. class ImapFolderLib {
  710. const S_ALL = 'ALL';
  711. const S_ANSWERED = 'ANSWERED';
  712. const S_BCC = 'BCC';
  713. const S_BEFORE = 'BEFORE';
  714. const S_BODY = 'BODY';
  715. const S_CC = 'CC';
  716. const S_DELETED = 'DELETED';
  717. const S_FLAGGED = 'FLAGGED';
  718. const S_FROM = 'FROM';
  719. const S_KEYWORD = 'KEYWORD';
  720. const S_NEW = 'NEW';
  721. const S_OLD = 'OLD';
  722. const S_ON = 'ON';
  723. const S_RECENT = 'RECENT';
  724. const S_SEEN = 'SEEN';
  725. const S_SINCE = 'SINCE';
  726. const S_SUBJECT = 'SUBJECT';
  727. const S_TEXT = 'TEXT';
  728. const S_TO = 'TO';
  729. const S_UNANSWERED = 'UNANSWERED';
  730. const S_UNDELETED = 'UNDELETED';
  731. const S_UNFLAGGED = 'UNFLAGGED';
  732. const S_UNKEYWORD = 'UNKEYWORD';
  733. const S_UNSEEN = 'UNSEEN';
  734. public $Imap;
  735. public $currentRef = '';
  736. public function __construct($Imap) {
  737. $this->Imap = $Imap;
  738. $this->currentRef = $this->Imap->currentRef;
  739. }
  740. public function listFolder() {
  741. }
  742. public function searchMessages($options = array(self::ALL)) {
  743. $optionstring = '';
  744. foreach ($options as $key => $value) {
  745. if (is_int($key)) {
  746. $key = $value;
  747. $value = null;
  748. }
  749. switch ($key) {
  750. case self::S_FROM:
  751. $param = '"' . $value . '" ';
  752. break;
  753. default:
  754. $param = '';
  755. break;
  756. }
  757. $optionstring .= $key . ' ' . $param;
  758. }
  759. //CHANGE DIR TO CURRENT
  760. $msg = imap_search($this->Imap->stream, $optionstring, SE_UID);
  761. if ($msg !== false) {
  762. return new ImapMessagesListLib($this, $msg);
  763. }
  764. return false;
  765. }
  766. public function allMessages() {
  767. return $this->searchMessages();
  768. }
  769. }
  770. class ImapException extends CakeException {
  771. }