ImapLib.php 19 KB

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