ImapLib.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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.0
  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 buildConnector($data = array()) {
  55. $data = array_merge($this->settings, $data);
  56. $string = '{';
  57. $string .= $data[self::S_SERVER];
  58. if ($data[self::S_PORT]) {
  59. $string .= ':' . $data[self::S_PORT];
  60. }
  61. if ($data[self::S_SERVICE]) {
  62. $string .= '/service=' . $data[self::S_SERVICE];
  63. }
  64. if ($data[self::S_USER]) {
  65. $string .= '/user=' . $data[self::S_USER];
  66. } else {
  67. $string .= '/anonymous';
  68. }
  69. if ($data[self::S_AUTHUSER]) {
  70. $string .= '/authuser=' . $data[self::S_AUTHUSER];
  71. }
  72. if ($data[self::S_DEBUG]) {
  73. $string .= '/debug';
  74. }
  75. if ($data[self::S_SECURE]) {
  76. $string .= '/secure';
  77. }
  78. if ($data[self::S_NORSH]) {
  79. $string .= '/norsh';
  80. }
  81. if ($data[self::S_SSL]) {
  82. $string .= '/ssl';
  83. }
  84. if ($data[self::S_VALIDATECERT]) {
  85. $string .= '/validate-cert';
  86. } else {
  87. $string .= '/novalidate-cert';
  88. }
  89. if ($data[self::S_TLS]) {
  90. $string .= '/tls';
  91. }
  92. if ($data[self::S_NOTLS]) {
  93. $string .= '/notls';
  94. }
  95. if ($data[self::S_READONLY]) {
  96. $string .= '/readonly';
  97. }
  98. $string .= '}';
  99. $string .= $data[self::S_MAILBOX];
  100. $this->currentRef = $string;
  101. $this->currentSettings = $data;
  102. return $string;
  103. }
  104. public function set($key, $value) {
  105. return $this->settings[$key] = $value;
  106. }
  107. public function lastError() {
  108. return imap_last_error();
  109. }
  110. /**
  111. * @return bool $success
  112. * 2011-10-25 ms
  113. */
  114. public function connect($user, $pass, $server, $port = null) {
  115. if (!$this->dependenciesMatch()) {
  116. return false;
  117. }
  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. // Let's add the body
  197. $return[$msgNo]['body'] = imap_fetchbody($this->stream, $msgNo, 1);
  198. //lets add attachments
  199. $return[$msgNo]['structure'] = imap_fetchstructure($this->stream, $msgNo);
  200. //debug(imap_fetchstructure($this->stream, $header->Msgno, FT_UID));
  201. $return[$msgNo]['attachments'] = $this->attachments($header);
  202. }
  203. }
  204. // We want to search a specific array of messages
  205. else {
  206. foreach ($msg_list as $i) {
  207. $header = imap_headerinfo($this->stream, $i);
  208. foreach ($header as $id => $value) {
  209. // Simple array
  210. if (!is_array($value)) {
  211. $return[$header->Msgno][$id] = $value;
  212. } else {
  213. foreach ($value as $newid => $array_value) {
  214. foreach ($value[0] as $key => $aValue) {
  215. $return[$header->Msgno][$id][$key] = quoted_printable_decode($aValue);
  216. }
  217. }
  218. }
  219. // Let's add the body too!
  220. $return[$header->Msgno]['body'] = imap_fetchbody($this->stream, $header->Msgno, 0);
  221. $return[$header->Msgno]['structure'] = imap_fetchstructure($this->stream, $header->Msgno);
  222. $return[$header->Msgno]['attachments'] = $this->attachments($header);
  223. }
  224. }
  225. }
  226. return $return;
  227. }
  228. /**
  229. * @see http://www.nerdydork.com/download-pop3imap-email-attachments-with-php.html
  230. * 2011-09-02 ms
  231. */
  232. public function attachments($header) {
  233. $structure = imap_fetchstructure($this->stream, $header->Msgno);
  234. if (!$structure) {
  235. return false;
  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. * 2011-09-02 ms
  307. */
  308. public function _getDecodedValue($message, $coding) {
  309. if ($coding == 0) {
  310. $message = imap_8bit($message);
  311. } elseif ($coding == 1) {
  312. $message = imap_8bit($message);
  313. } elseif ($coding == 2) {
  314. $message = imap_binary($message);
  315. } elseif ($coding == 3) {
  316. $message = imap_base64($message);
  317. } elseif ($coding == 4) {
  318. $message = imap_qprint($message);
  319. } elseif ($coding == 5) {
  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. $search_string = '';
  328. foreach ($params as $field => $value) {
  329. if (is_numeric($field)) {
  330. // Make sure the value is uppercase
  331. $search_string .= strtoupper($value) . ' ';
  332. } else {
  333. $search_string .= strtoupper($field) . ' "' . $value . '" ';
  334. }
  335. }
  336. // Perform the search
  337. #echo "'$search_string'";
  338. return imap_search($this->stream, $search_string);
  339. } else {
  340. return imap_last_error();
  341. }
  342. }
  343. return imap_last_error();
  344. }
  345. public function flag($flag) {
  346. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  347. }
  348. /** testing only **/
  349. public function delete($emails, $delete = false) {
  350. $emails = (array )$emails;
  351. foreach ($emails as $email) {
  352. if ($delete) {
  353. imap_delete($this->stream, (int)$email);
  354. } else {
  355. imap_mail_move($this->stream, (int)$email, "Inbox/Trash");
  356. }
  357. }
  358. return imap_expunge($this->stream);
  359. }
  360. /**
  361. * deprecated
  362. */
  363. public function delx($emails, $delete = false) {
  364. if (!$this->stream) {
  365. return false;
  366. }
  367. $emails = (array )$emails;
  368. foreach ($emails as $key => $val) {
  369. $emails[$key] = (int)$val;
  370. }
  371. // Let's delete multiple emails
  372. if (count($emails) > 0) {
  373. $delete_string = '';
  374. $email_error = array();
  375. foreach ($emails as $email) {
  376. if ($delete) {
  377. if (!imap_delete($this->stream, $email)) {
  378. $email_error[] = $email;
  379. }
  380. }
  381. }
  382. if (!$delete) {
  383. // Need to take the last comma out!
  384. $delete_string = implode(',', $emails);
  385. echo $delete_string;
  386. imap_mail_move($this->stream, $delete_string, "Inbox/Trash");
  387. //imap_expunge($this->stream);
  388. } else {
  389. // NONE of the emails were deleted
  390. //imap_expunge($this->stream);
  391. if (count($email_error) === count($emails)) {
  392. return imap_last_error();
  393. } else {
  394. $return['status'] = false;
  395. $return['not_deleted'] = $email_error;
  396. return $return;
  397. }
  398. }
  399. }
  400. // Not connected
  401. return imap_last_error();
  402. }
  403. public function switch_mailbox($mailbox = '') {
  404. if ($this->stream) {
  405. $this->mbox = '{' . $this->server;
  406. if ($this->port) {
  407. $this->mbox .= ':' . $this->port;
  408. }
  409. if ($this->flags) {
  410. $this->mbox .= $this->flags;
  411. }
  412. $this->mbox .= '/user="' . $this->user . '"';
  413. $this->mbox .= '}';
  414. $this->mbox .= $this->default_mailbox;
  415. if ($mailbox) {
  416. $this->mbox .= '.' . $mailbox;
  417. }
  418. return @imap_reopen($this->stream, $this->mbox);
  419. }
  420. // Not connected
  421. return imap_last_error();
  422. }
  423. public function current_mailbox() {
  424. if ($this->stream) {
  425. $info = imap_mailboxmsginfo($this->stream);
  426. if ($info) {
  427. return $info->Mailbox;
  428. } else {
  429. // There was an error
  430. return imap_last_error();
  431. }
  432. }
  433. // Not connected
  434. return imap_last_error();
  435. }
  436. public function mailbox_info($type = 'obj') {
  437. if ($this->stream) {
  438. $info = imap_mailboxmsginfo($this->stream);
  439. if ($info) {
  440. if ($type == 'array') {
  441. $info_array = get_object_vars($info);
  442. return $info_array;
  443. } else {
  444. return $info;
  445. }
  446. } else {
  447. // There was an error
  448. return imap_last_error();
  449. }
  450. }
  451. // Not connected
  452. return imap_last_error();
  453. }
  454. /**
  455. * makes sure imap_open is available etc
  456. * @return bool $success
  457. * 2011-10-25 ms
  458. */
  459. public function dependenciesMatch() {
  460. if (!function_exists('imap_open')) {
  461. trigger_error('imap_open not available. Please install extension/module.');
  462. return false;
  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. } else {
  517. return $this->ImapMessage;
  518. }
  519. }
  520. public function flag($flag) {
  521. return imap_setflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  522. }
  523. public function unFlag($flag) {
  524. return imap_clearflag_full($this->ImapFolder->Imap->stream, $this->uid, $flag, ST_UID);
  525. }
  526. public function seen($set = null) {
  527. if ($set === null) {
  528. return $this->seen;
  529. } elseif ($set) {
  530. return $this->flag(self::BS . 'Seen');
  531. } else {
  532. return $this->unFlag(self::BS . 'Seen');
  533. }
  534. }
  535. public function answered($set = null) {
  536. if ($set === null) {
  537. return $this->answered;
  538. } elseif ($set) {
  539. return $this->flag(self::BS . 'Answered');
  540. } else {
  541. return $this->unFlag(self::BS . 'Answered');
  542. }
  543. }
  544. public function flagged($set = null) {
  545. if ($set === null) {
  546. return $this->flagged;
  547. } elseif ($set) {
  548. return $this->flag(self::BS . 'Flagged');
  549. } else {
  550. return $this->unFlag(self::BS . 'Flagged');
  551. }
  552. }
  553. public function deleted($set = null) {
  554. if ($set === null) {
  555. return $this->deleted;
  556. } elseif ($set) {
  557. return $this->flag(self::BS . 'Deleted');
  558. } else {
  559. return $this->unFlag(self::BS . 'Deleted');
  560. }
  561. }
  562. public function draft($set = null) {
  563. if ($set === null) {
  564. return $this->draft;
  565. } elseif ($set) {
  566. return $this->flag(self::BS . 'Draft');
  567. } else {
  568. return $this->unFlag(self::BS . 'Draft');
  569. }
  570. }
  571. }
  572. class ImapMessageLib {
  573. public $ImapFolder;
  574. public $MessageInfo;
  575. public $uid;
  576. public function __construct($ImapFolder, $uid, $ImapMessageInfo = null) {
  577. $this->ImapFolder = $ImapFolder;
  578. if ($ImapMessageInfo === null) {
  579. $this->MessageInfo = new ImapMessageInfoLib($this->ImapFolder, $uid);
  580. } else {
  581. $this->MessageInfo = $ImapMessageInfo;
  582. }
  583. $this->MessageInfo->ImapMessage = $this;
  584. $this->uid = $uid;
  585. }
  586. public function move($folder) {
  587. }
  588. public function id() {
  589. //CHANGE DIR TO CURRENT
  590. return imap_msgno($this->ImapFolder->Imap->stream, $this->uid);
  591. }
  592. public function uid($ID) {
  593. return $this->uid;
  594. }
  595. public function fetchstructure() {
  596. return imap_fetchstructure($this->ImapFolder->Imap->stream, $this->uid, FT_UID);
  597. }
  598. public function fetchbody($section = 0) {
  599. return imap_fetchbody($this->ImapFolder->Imap->stream, $this->uid, $section, (FT_UID + FT_PEEK));
  600. }
  601. }
  602. class ImapMessagesListLib {
  603. public $ImapFolder;
  604. public $messageUIDs = array();
  605. public function __construct($ImapFolder, $messageUIDs) {
  606. $this->ImapFolder = $ImapFolder;
  607. $this->messageUIDs = $messageUIDs;
  608. }
  609. public function messgageObject($id) {
  610. if (isset($this->messageUIDs[$id])) {
  611. if (is_object($this->messageUIDs[$id])) {
  612. return $this->messageUIDs[$id];
  613. } else {
  614. return $this->messageUIDs[$id] = new ImapMessageLib($this->ImapFolder, $this->messageUIDs[$id]);
  615. }
  616. }
  617. return false;
  618. }
  619. public function count() {
  620. return count($this->messageUIDs);
  621. }
  622. public function overview($returnInfo = true) {
  623. //CHANGE DIR TO CURRENT
  624. $overview = imap_fetch_overview($this->ImapFolder->Imap->stream, implode(',', $this->messageUIDs), FT_UID);
  625. if ($returnInfo) {
  626. $msgObjs = array();
  627. foreach ($overview as $info) {
  628. $msgObjs[] = new ImapMessageInfoLib($this->ImapFolder, $info);
  629. }
  630. return $msgObjs;
  631. } else {
  632. return $overview;
  633. }
  634. }
  635. }
  636. class ImapFolderLib {
  637. const S_ALL = 'ALL';
  638. const S_ANSWERED = 'ANSWERED';
  639. const S_BCC = 'BCC';
  640. const S_BEFORE = 'BEFORE';
  641. const S_BODY = 'BODY';
  642. const S_CC = 'CC';
  643. const S_DELETED = 'DELETED';
  644. const S_FLAGGED = 'FLAGGED';
  645. const S_FROM = 'FROM';
  646. const S_KEYWORD = 'KEYWORD';
  647. const S_NEW = 'NEW';
  648. const S_OLD = 'OLD';
  649. const S_ON = 'ON';
  650. const S_RECENT = 'RECENT';
  651. const S_SEEN = 'SEEN';
  652. const S_SINCE = 'SINCE';
  653. const S_SUBJECT = 'SUBJECT';
  654. const S_TEXT = 'TEXT';
  655. const S_TO = 'TO';
  656. const S_UNANSWERED = 'UNANSWERED';
  657. const S_UNDELETED = 'UNDELETED';
  658. const S_UNFLAGGED = 'UNFLAGGED';
  659. const S_UNKEYWORD = 'UNKEYWORD';
  660. const S_UNSEEN = 'UNSEEN';
  661. public $Imap;
  662. public $currentRef = '';
  663. public function __construct($Imap) {
  664. $this->Imap = $Imap;
  665. $this->currentRef = $this->Imap->currentRef;
  666. }
  667. public function listFolder() {
  668. }
  669. public function searchMessages($options = array(self::ALL)) {
  670. $optionstring = '';
  671. foreach ($options as $key => $value) {
  672. if (is_int($key)) {
  673. $key = $value;
  674. $value = null;
  675. }
  676. switch ($key) {
  677. case self::S_FROM:
  678. $param = '"' . $value . '" ';
  679. break;
  680. default:
  681. $param = '';
  682. break;
  683. }
  684. $optionstring .= $key . ' ' . $param;
  685. }
  686. //CHANGE DIR TO CURRENT
  687. $msg = imap_search($this->Imap->stream, $optionstring, SE_UID);
  688. if ($msg !== false) {
  689. return new ImapMessagesListLib($this, $msg);
  690. }
  691. return false;
  692. }
  693. public function allMessages() {
  694. return $this->searchMessages();
  695. }
  696. }
  697. class ImapException extends CakeException {
  698. }