ImapLib.php 19 KB

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