ImapLib.php 20 KB

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