ImapLib.php 20 KB

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