ImapLib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. * @return array
  171. */
  172. public function msgList($msgList = array()) {
  173. $return = array();
  174. if (empty($msgList)) {
  175. $count = $this->msgCount();
  176. for ($i = 1; $i <= $count; $i++) {
  177. $header = imap_headerinfo($this->stream, $i);
  178. $msgNo = trim($header->Msgno);
  179. foreach ($header as $id => $value) {
  180. # fix to remove whitespaces
  181. // Simple array
  182. if (!is_array($value)) {
  183. $return[$msgNo][$id] = imap_utf8($value);
  184. } else {
  185. foreach ($value as $newid => $arrayValue) {
  186. foreach ($value[0] as $key => $aValue) {
  187. $return[$msgNo][$id][$key] = quoted_printable_decode($aValue);
  188. }
  189. }
  190. }
  191. }
  192. //lets add attachments
  193. $return[$msgNo]['structure'] = (array)imap_fetchstructure($this->stream, $msgNo);
  194. $encodingValue = $return[$msgNo]['structure']['encoding'];
  195. if (!empty($return[$msgNo]['structure']['parts'])) {
  196. $part = $return[$msgNo]['structure']['parts'][0];
  197. $encodingValue = $part->encoding;
  198. }
  199. // Let's add the body
  200. $return[$msgNo]['body'] = $this->_getDecodedValue(imap_fetchbody($this->stream, $msgNo, 1), $encodingValue);
  201. //debug(imap_fetchstructure($this->stream, $header->Msgno, FT_UID));
  202. $return[$msgNo]['attachments'] = $this->attachments($header);
  203. }
  204. }
  205. // We want to search a specific array of messages
  206. else {
  207. foreach ($msgList as $i) {
  208. $header = imap_headerinfo($this->stream, $i);
  209. foreach ($header as $id => $value) {
  210. // Simple array
  211. if (!is_array($value)) {
  212. $return[$header->Msgno][$id] = $value;
  213. } else {
  214. foreach ($value as $newid => $arrayValue) {
  215. foreach ($value[0] as $key => $aValue) {
  216. $return[$header->Msgno][$id][$key] = quoted_printable_decode($aValue);
  217. }
  218. }
  219. }
  220. // Let's add the body too!
  221. $return[$header->Msgno]['body'] = imap_fetchbody($this->stream, $header->Msgno, 0);
  222. $return[$header->Msgno]['structure'] = imap_fetchstructure($this->stream, $header->Msgno);
  223. $return[$header->Msgno]['attachments'] = $this->attachments($header);
  224. }
  225. }
  226. }
  227. return $return;
  228. }
  229. /**
  230. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  231. */
  232. public function attachments($header) {
  233. $structure = imap_fetchstructure($this->stream, $header->Msgno);
  234. if (!$structure || !isset($structure->parts)) {
  235. return array();
  236. }
  237. $parts = $structure->parts;
  238. $fpos = 2;
  239. $message = array();
  240. $message["attachment"]["type"][0] = 'text';
  241. $message["attachment"]["type"][1] = 'multipart';
  242. $message["attachment"]["type"][2] = 'message';
  243. $message["attachment"]["type"][3] = 'application';
  244. $message["attachment"]["type"][4] = 'audio';
  245. $message["attachment"]["type"][5] = 'image';
  246. $message["attachment"]["type"][6] = 'video';
  247. $message["attachment"]["type"][7] = 'other';
  248. $attachments = array();
  249. for ($i = 1; $i < count($parts); $i++) {
  250. $attachment = array();
  251. $part = $parts[$i];
  252. if (isset($part->disposition) && $part->disposition === 'ATTACHMENT') {
  253. $attachment["pid"] = $i;
  254. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  255. $attachment["subtype"][$i] = strtolower($part->subtype);
  256. $ext = $part->subtype;
  257. $params = $part->dparameters;
  258. $mege = '';
  259. $data = '';
  260. $mege = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  261. $attachment['filename'] = $part->dparameters[0]->value;
  262. $attachment['data'] = $this->_getDecodedValue($mege, $part->encoding);
  263. $attachment['filesize'] = strlen($attachment['data']);
  264. $fpos++;
  265. $attachments[] = $attachment;
  266. } elseif (isset($part->subtype) && $part->subtype === "OCTET-STREAM") {
  267. $attachment["pid"] = $i;
  268. $attachment["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
  269. $attachment["subtype"][$i] = strtolower($part->subtype);
  270. $ext = $part->subtype;
  271. $params = $part->parameters;
  272. //die(returns($part));
  273. $mege = '';
  274. $data = '';
  275. $mege = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  276. $attachment['filename'] = $part->parameters[0]->value;
  277. $attachment['data'] = $this->_getDecodedValue($mege, $part->encoding);
  278. $attachment['filesize'] = strlen($attachment['data']);
  279. $fpos++;
  280. $attachments[] = $attachment;
  281. } else { // inline attachments etc
  282. $attachment["pid"] = $i;
  283. $type = '';
  284. if (!empty($message["attachment"]["type"][$part->type])) {
  285. $type = $message["attachment"]["type"][$part->type] . "/";
  286. }
  287. $attachment["type"][$i] = $type . strtolower($part->subtype);
  288. $attachment["subtype"][$i] = strtolower($part->subtype);
  289. $ext = $part->subtype;
  290. $params = $part->parameters;
  291. //CakeLog::write('import', print_r($part, true)); die('TEST');
  292. $mege = '';
  293. $data = '';
  294. $mege = imap_fetchbody($this->stream, $header->Msgno, $fpos);
  295. $attachment['filename'] = !is_object($part->parameters) ? $part->parameters[0]->value : '';
  296. $attachment['data'] = $this->_getDecodedValue($mege, $part->encoding);
  297. $attachment['filesize'] = strlen($attachment['data']);
  298. $fpos++;
  299. $attachments[] = $attachment;
  300. }
  301. }
  302. return $attachments;
  303. }
  304. /**
  305. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  306. */
  307. protected function _getDecodedValue($message, $coding) {
  308. if ($coding == 0) {
  309. $message = imap_8bit($message);
  310. } elseif ($coding == 1) {
  311. $message = imap_8bit($message);
  312. } elseif ($coding == 2) {
  313. $message = imap_binary($message);
  314. } elseif ($coding == 3) {
  315. $message = imap_base64($message);
  316. } elseif ($coding == 4) {
  317. $message = imap_qprint($message);
  318. } elseif ($coding == 5) {
  319. // plain
  320. //$message = imap_base64($message);
  321. }
  322. return $message;
  323. }
  324. public function search($params) {
  325. if ($this->stream) {
  326. if (is_array($params)) {
  327. $searchString = '';
  328. foreach ($params as $field => $value) {
  329. if (is_numeric($field)) {
  330. // Make sure the value is uppercase
  331. $searchString .= strtoupper($value) . ' ';
  332. } else {
  333. $searchString .= strtoupper($field) . ' "' . $value . '" ';
  334. }
  335. }
  336. // Perform the search
  337. #echo "'$search_string'";
  338. return imap_search($this->stream, $searchString);
  339. }
  340. return imap_last_error();
  341. }
  342. return imap_last_error();
  343. }
  344. public function flag($flag) {
  345. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  346. }
  347. /** testing only **/
  348. public function delete($emails, $delete = false) {
  349. $emails = (array )$emails;
  350. foreach ($emails as $email) {
  351. if ($delete) {
  352. imap_delete($this->stream, (int)$email);
  353. } else {
  354. imap_mail_move($this->stream, (int)$email, "Inbox/Trash");
  355. }
  356. }
  357. return imap_expunge($this->stream);
  358. }
  359. /**
  360. * deprecated
  361. */
  362. public function delx($emails, $delete = false) {
  363. if (!$this->stream) {
  364. return false;
  365. }
  366. $emails = (array )$emails;
  367. foreach ($emails as $key => $val) {
  368. $emails[$key] = (int)$val;
  369. }
  370. // Let's delete multiple emails
  371. if (count($emails) > 0) {
  372. $deleteString = '';
  373. $emailError = array();
  374. foreach ($emails as $email) {
  375. if ($delete) {
  376. if (!imap_delete($this->stream, $email)) {
  377. $emailError[] = $email;
  378. }
  379. }
  380. }
  381. if (!$delete) {
  382. // Need to take the last comma out!
  383. $deleteString = implode(',', $emails);
  384. echo $deleteString;
  385. imap_mail_move($this->stream, $deleteString, "Inbox/Trash");
  386. //imap_expunge($this->stream);
  387. } else {
  388. // NONE of the emails were deleted
  389. //imap_expunge($this->stream);
  390. if (count($emailError) === count($emails)) {
  391. return imap_last_error();
  392. }
  393. $return['status'] = false;
  394. $return['not_deleted'] = $emailError;
  395. return $return;
  396. }
  397. }
  398. // Not connected
  399. return imap_last_error();
  400. }
  401. public function switch_mailbox($mailbox = '') {
  402. if ($this->stream) {
  403. $this->mbox = '{' . $this->server;
  404. if ($this->port) {
  405. $this->mbox .= ':' . $this->port;
  406. }
  407. if ($this->flags) {
  408. $this->mbox .= $this->flags;
  409. }
  410. $this->mbox .= '/user="' . $this->user . '"';
  411. $this->mbox .= '}';
  412. $this->mbox .= $this->default_mailbox;
  413. if ($mailbox) {
  414. $this->mbox .= '.' . $mailbox;
  415. }
  416. return @imap_reopen($this->stream, $this->mbox);
  417. }
  418. // Not connected
  419. return imap_last_error();
  420. }
  421. public function current_mailbox() {
  422. if ($this->stream) {
  423. $info = imap_mailboxmsginfo($this->stream);
  424. if ($info) {
  425. return $info->Mailbox;
  426. }
  427. // There was an error
  428. return imap_last_error();
  429. }
  430. // Not connected
  431. return imap_last_error();
  432. }
  433. public function mailbox_info($type = 'obj') {
  434. if ($this->stream) {
  435. $info = imap_mailboxmsginfo($this->stream);
  436. if ($info) {
  437. if ($type === 'array') {
  438. $infoArray = get_object_vars($info);
  439. return $infoArray;
  440. }
  441. return $info;
  442. }
  443. // There was an error
  444. return imap_last_error();
  445. }
  446. // Not connected
  447. return imap_last_error();
  448. }
  449. /**
  450. * Makes sure imap_open is available etc
  451. * @throws InternalErrorException
  452. * @return boolean Success
  453. */
  454. public function dependenciesMatch() {
  455. if (!function_exists('imap_open')) {
  456. throw new InternalErrorException('imap_open not available. Please install extension/module.');
  457. }
  458. return true;
  459. }
  460. }
  461. // Currently NOT IN USE: //
  462. /**
  463. * IMAP Postf�cher mit CakePHP abfragen
  464. * @see http://www.interaktionsdesigner.de/2009/05/11/imap-postfacher-mit-cakephp-abfragen/
  465. *
  466. * $this->Imap->connect();
  467. * Der R�ckgabewert dieser Funktion ist negativ wenn es nicht funktioniert hat.
  468. *
  469. * 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.
  470. * F�r den Text der Mail w�re das dann innerhalb der foreach-Schleife:
  471. * debug(utf8_encode(quoted_printable_decode($message['body'])));
  472. *
  473. * fixes: pop3 connect etc
  474. */
  475. class ImapMessageInfoLib {
  476. const BS = "\\";
  477. public $ImapFolder;
  478. public $ImapMessage;
  479. public $subject;
  480. public $from;
  481. public $to;
  482. public $date;
  483. public $message_id;
  484. public $references;
  485. public $in_reply_to;
  486. public $size;
  487. public $uid;
  488. public $msgno;
  489. public $recent;
  490. public $flagged;
  491. public $answered;
  492. public $deleted;
  493. public $seen;
  494. public $draft;
  495. public function __construct($ImapFolder, $data) {
  496. if (!is_object($data)) {
  497. $list = new ImapMessagesListLib($ImapFolder, array($data));
  498. $list = $list->overview(false);
  499. $data = $list[0];
  500. }
  501. foreach ($data as $key => $value) {
  502. $this->{$key} = $value;
  503. }
  504. $this->ImapFolder = $ImapFolder;
  505. }
  506. public function messageObject() {
  507. if (!isset($this->ImapMessage)) {
  508. return $this->ImapMessage = new ImapMessageLib($this->ImapFolder, $this->uid, $this);
  509. }
  510. return $this->ImapMessage;
  511. }
  512. public function flag($flag) {
  513. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  514. }
  515. public function unFlag($flag) {
  516. return imap_clearflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  517. }
  518. public function seen($set = null) {
  519. if ($set === null) {
  520. return $this->seen;
  521. }
  522. if ($set) {
  523. return $this->flag(self::BS . 'Seen');
  524. }
  525. return $this->unFlag(self::BS . 'Seen');
  526. }
  527. public function answered($set = null) {
  528. if ($set === null) {
  529. return $this->answered;
  530. }
  531. if ($set) {
  532. return $this->flag(self::BS . 'Answered');
  533. }
  534. return $this->unFlag(self::BS . 'Answered');
  535. }
  536. public function flagged($set = null) {
  537. if ($set === null) {
  538. return $this->flagged;
  539. }
  540. if ($set) {
  541. return $this->flag(self::BS . 'Flagged');
  542. }
  543. return $this->unFlag(self::BS . 'Flagged');
  544. }
  545. public function deleted($set = null) {
  546. if ($set === null) {
  547. return $this->deleted;
  548. }
  549. if ($set) {
  550. return $this->flag(self::BS . 'Deleted');
  551. }
  552. return $this->unFlag(self::BS . 'Deleted');
  553. }
  554. public function draft($set = null) {
  555. if ($set === null) {
  556. return $this->draft;
  557. }
  558. if ($set) {
  559. return $this->flag(self::BS . 'Draft');
  560. }
  561. return $this->unFlag(self::BS . 'Draft');
  562. }
  563. }
  564. class ImapMessageLib {
  565. public $ImapFolder;
  566. public $MessageInfo;
  567. public $uid;
  568. public function __construct($ImapFolder, $uid, $ImapMessageInfo = null) {
  569. $this->ImapFolder = $ImapFolder;
  570. if ($ImapMessageInfo === null) {
  571. $this->MessageInfo = new ImapMessageInfoLib($this->ImapFolder, $uid);
  572. } else {
  573. $this->MessageInfo = $ImapMessageInfo;
  574. }
  575. $this->MessageInfo->ImapMessage = $this;
  576. $this->uid = $uid;
  577. }
  578. public function move($folder) {
  579. }
  580. public function id() {
  581. //CHANGE DIR TO CURRENT
  582. return imap_msgno($this->ImapFolder->Imap->stream, $this->uid);
  583. }
  584. public function uid($ID) {
  585. return $this->uid;
  586. }
  587. public function fetchstructure() {
  588. return imap_fetchstructure($this->ImapFolder->Imap->stream, $this->uid, FT_UID);
  589. }
  590. public function fetchbody($section = 0) {
  591. return imap_fetchbody($this->ImapFolder->Imap->stream, $this->uid, $section, (FT_UID + FT_PEEK));
  592. }
  593. }
  594. class ImapMessagesListLib {
  595. public $ImapFolder;
  596. public $messageUIDs = array();
  597. public function __construct($ImapFolder, $messageUIDs) {
  598. $this->ImapFolder = $ImapFolder;
  599. $this->messageUIDs = $messageUIDs;
  600. }
  601. public function messgageObject($id) {
  602. if (isset($this->messageUIDs[$id])) {
  603. if (is_object($this->messageUIDs[$id])) {
  604. return $this->messageUIDs[$id];
  605. }
  606. return $this->messageUIDs[$id] = new ImapMessageLib($this->ImapFolder, $this->messageUIDs[$id]);
  607. }
  608. return false;
  609. }
  610. public function count() {
  611. return count($this->messageUIDs);
  612. }
  613. public function overview($returnInfo = true) {
  614. //CHANGE DIR TO CURRENT
  615. $overview = imap_fetch_overview($this->ImapFolder->Imap->stream, implode(',', $this->messageUIDs), FT_UID);
  616. if ($returnInfo) {
  617. $msgObjs = array();
  618. foreach ($overview as $info) {
  619. $msgObjs[] = new ImapMessageInfoLib($this->ImapFolder, $info);
  620. }
  621. return $msgObjs;
  622. }
  623. return $overview;
  624. }
  625. }
  626. class ImapFolderLib {
  627. const S_ALL = 'ALL';
  628. const S_ANSWERED = 'ANSWERED';
  629. const S_BCC = 'BCC';
  630. const S_BEFORE = 'BEFORE';
  631. const S_BODY = 'BODY';
  632. const S_CC = 'CC';
  633. const S_DELETED = 'DELETED';
  634. const S_FLAGGED = 'FLAGGED';
  635. const S_FROM = 'FROM';
  636. const S_KEYWORD = 'KEYWORD';
  637. const S_NEW = 'NEW';
  638. const S_OLD = 'OLD';
  639. const S_ON = 'ON';
  640. const S_RECENT = 'RECENT';
  641. const S_SEEN = 'SEEN';
  642. const S_SINCE = 'SINCE';
  643. const S_SUBJECT = 'SUBJECT';
  644. const S_TEXT = 'TEXT';
  645. const S_TO = 'TO';
  646. const S_UNANSWERED = 'UNANSWERED';
  647. const S_UNDELETED = 'UNDELETED';
  648. const S_UNFLAGGED = 'UNFLAGGED';
  649. const S_UNKEYWORD = 'UNKEYWORD';
  650. const S_UNSEEN = 'UNSEEN';
  651. public $Imap;
  652. public $currentRef = '';
  653. public function __construct($Imap) {
  654. $this->Imap = $Imap;
  655. $this->currentRef = $this->Imap->currentRef;
  656. }
  657. public function listFolder() {
  658. }
  659. public function searchMessages($options = array(self::ALL)) {
  660. $optionstring = '';
  661. foreach ($options as $key => $value) {
  662. if (is_int($key)) {
  663. $key = $value;
  664. $value = null;
  665. }
  666. switch ($key) {
  667. case self::S_FROM:
  668. $param = '"' . $value . '" ';
  669. break;
  670. default:
  671. $param = '';
  672. break;
  673. }
  674. $optionstring .= $key . ' ' . $param;
  675. }
  676. //CHANGE DIR TO CURRENT
  677. $msg = imap_search($this->Imap->stream, $optionstring, SE_UID);
  678. if ($msg !== false) {
  679. return new ImapMessagesListLib($this, $msg);
  680. }
  681. return false;
  682. }
  683. public function allMessages() {
  684. return $this->searchMessages();
  685. }
  686. }
  687. class ImapException extends CakeException {
  688. }